Decode frames that declare no content size

Streaming encoders leave Frame_Content_Size out unless told the size up front,
and their readers often can't know it either: every zstd frame in an Anki
package (its collection, its media list, each media file) is written that way.
Such a frame needed the exact size passed as expectedSize, and anything else
was refused.

Now it gets the most its blocks can hold, Block_Maximum_Size (128 KiB) each,
found by walking the block headers alone, and the output is trimmed to what
the blocks held. The output is still allocated once. expectedSize keeps its
exact check when given, and maxOutputSize bounds the allocation as before.

Checked against libzstd (python-zstandard) on Anki 26.09.3's exports: a small
package's four frames and a 30 MB collection decode byte for byte the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-02 04:28:06 +02:00
commit 57aa532069
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -121,5 +121,31 @@ void main() {
);
});
}
// Streaming encoders write frames like this one, and their callers often
// can't know the size: an Anki package's collection, for one.
test('decodes without being told its size, to exactly what it holds', () {
final out = ZstdDecoder().decode(entry.frame);
expect(out.length, size);
expect(out, orderedEquals(entry.plain));
});
test('is refused, not crashed on, when cut short', () {
for (final cut in [1, 3, entry.frame.length ~/ 2]) {
expect(
() => ZstdDecoder().decode(
Uint8List.sublistView(entry.frame, 0, entry.frame.length - cut),
),
throwsA(isA<ZstdException>()),
);
}
});
test('is refused when its blocks could hold more than allowed', () {
expect(
() => ZstdDecoder(maxOutputSize: size).decode(entry.frame),
throwsA(isA<ZstdException>()),
);
});
});
}