authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-10 01:33:38+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-20 09:09:06+11:00
logee5af3c74c27ebf366ef51486119487650f80468
treef86094baf16f8c00df2bd19855df6af962cb07d5
parent31cc4605aba68edc44a31f8383eaa39a906d6ec8

std.compress.zstandard: cleanup high-level api docs and error sets


5 files changed, 173 insertions(+), 69 deletions(-)

lib/std/compress/zstandard.zig+28-12
......@@ -7,7 +7,11 @@ const RingBuffer = @import("zstandard/RingBuffer.zig");
77pub const decompress = @import("zstandard/decompress.zig");
88pub usingnamespace @import("zstandard/types.zig");
99
10pub fn ZstandardStream(comptime ReaderType: type, comptime verify_checksum: bool, comptime window_size_max: usize) type {
10pub fn ZstandardStream(
11 comptime ReaderType: type,
12 comptime verify_checksum: bool,
13 comptime window_size_max: usize,
14) type {
1115 return struct {
1216 const Self = @This();
1317
......@@ -24,11 +28,16 @@ pub fn ZstandardStream(comptime ReaderType: type, comptime verify_checksum: bool
2428 sequence_buffer: []u8,
2529 checksum: if (verify_checksum) ?u32 else void,
2630
27 pub const Error = ReaderType.Error || error{ ChecksumFailure, MalformedBlock, MalformedFrame, OutOfMemory };
31 pub const Error = ReaderType.Error || error{
32 ChecksumFailure,
33 MalformedBlock,
34 MalformedFrame,
35 OutOfMemory,
36 };
2837
2938 pub const Reader = std.io.Reader(*Self, Error, read);
3039
31 pub fn init(allocator: Allocator, source: ReaderType) !Self {
40 pub fn init(allocator: Allocator, source: ReaderType) Self {
3241 return Self{
3342 .allocator = allocator,
3443 .source = std.io.countingReader(source),
......@@ -146,7 +155,8 @@ pub fn ZstandardStream(comptime ReaderType: type, comptime verify_checksum: bool
146155
147156 const source_reader = self.source.reader();
148157 while (self.buffer.isEmpty() and self.state != .LastBlock) {
149 const header_bytes = source_reader.readBytesNoEof(3) catch return error.MalformedFrame;
158 const header_bytes = source_reader.readBytesNoEof(3) catch
159 return error.MalformedFrame;
150160 const block_header = decompress.block.decodeBlockHeader(&header_bytes);
151161
152162 decompress.block.decodeBlockReader(
......@@ -171,10 +181,12 @@ pub fn ZstandardStream(comptime ReaderType: type, comptime verify_checksum: bool
171181 if (block_header.last_block) {
172182 self.state = .LastBlock;
173183 if (self.frame_context.has_checksum) {
174 const checksum = source_reader.readIntLittle(u32) catch return error.MalformedFrame;
184 const checksum = source_reader.readIntLittle(u32) catch
185 return error.MalformedFrame;
175186 if (comptime verify_checksum) {
176187 if (self.frame_context.hasher_opt) |*hasher| {
177 if (checksum != decompress.computeChecksum(hasher)) return error.ChecksumFailure;
188 if (checksum != decompress.computeChecksum(hasher))
189 return error.ChecksumFailure;
178190 }
179191 }
180192 }
......@@ -182,9 +194,9 @@ pub fn ZstandardStream(comptime ReaderType: type, comptime verify_checksum: bool
182194 }
183195
184196 const decoded_data_len = self.buffer.len();
185 var written_count: usize = 0;
186 while (written_count < decoded_data_len and written_count < buffer.len) : (written_count += 1) {
187 buffer[written_count] = self.buffer.read().?;
197 var count: usize = 0;
198 while (count < decoded_data_len and count < buffer.len) : (count += 1) {
199 buffer[count] = self.buffer.read().?;
188200 }
189201 if (self.state == .LastBlock and self.buffer.len() == 0) {
190202 self.state = .NewFrame;
......@@ -195,18 +207,22 @@ pub fn ZstandardStream(comptime ReaderType: type, comptime verify_checksum: bool
195207 self.allocator.free(self.sequence_buffer);
196208 self.buffer.deinit(self.allocator);
197209 }
198 return written_count;
210 return count;
199211 }
200212 };
201213}
202214
203pub fn zstandardStream(allocator: Allocator, reader: anytype) !ZstandardStream(@TypeOf(reader), true, 8 * (1 << 20)) {
215pub fn zstandardStream(
216 allocator: Allocator,
217 reader: anytype,
218 comptime window_size_max: usize,
219) ZstandardStream(@TypeOf(reader), true, window_size_max) {
204220 return ZstandardStream(@TypeOf(reader), true, 8 * (1 << 20)).init(allocator, reader);
205221}
206222
207223fn testDecompress(data: []const u8) ![]u8 {
208224 var in_stream = std.io.fixedBufferStream(data);
209 var stream = try zstandardStream(std.testing.allocator, in_stream.reader());
225 var stream = zstandardStream(std.testing.allocator, in_stream.reader(), 1 << 23);
210226 defer stream.deinit();
211227 const result = stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize));
212228 return result;
lib/std/compress/zstandard/RingBuffer.zig+6-3
......@@ -13,6 +13,8 @@ data: []u8,
1313read_index: usize,
1414write_index: usize,
1515
16pub const Error = error{Full};
17
1618/// Allocate a new `RingBuffer`
1719pub fn init(allocator: Allocator, capacity: usize) Allocator.Error!RingBuffer {
1820 const bytes = try allocator.alloc(u8, capacity);
......@@ -41,7 +43,7 @@ pub fn mask2(self: RingBuffer, index: usize) usize {
4143
4244/// Write `byte` into the ring buffer. Returns `error.Full` if the ring
4345/// buffer is full.
44pub fn write(self: *RingBuffer, byte: u8) !void {
46pub fn write(self: *RingBuffer, byte: u8) Error!void {
4547 if (self.isFull()) return error.Full;
4648 self.writeAssumeCapacity(byte);
4749}
......@@ -55,7 +57,7 @@ pub fn writeAssumeCapacity(self: *RingBuffer, byte: u8) void {
5557
5658/// Write `bytes` into the ring bufffer. Returns `error.Full` if the ring
5759/// buffer does not have enough space, without writing any data.
58pub fn writeSlice(self: *RingBuffer, bytes: []const u8) !void {
60pub fn writeSlice(self: *RingBuffer, bytes: []const u8) Error!void {
5961 if (self.len() + bytes.len > self.data.len) return error.Full;
6062 self.writeSliceAssumeCapacity(bytes);
6163}
......@@ -87,7 +89,8 @@ pub fn isFull(self: RingBuffer) bool {
8789
8890/// Returns the length
8991pub fn len(self: RingBuffer) usize {
90 const adjusted_write_index = self.write_index + @as(usize, @boolToInt(self.write_index < self.read_index)) * 2 * self.data.len;
92 const wrap_offset = 2 * self.data.len * @boolToInt(self.write_index < self.read_index);
93 const adjusted_write_index = self.write_index + wrap_offset;
9194 return adjusted_write_index - self.read_index;
9295}
9396
lib/std/compress/zstandard/decode/block.zig+3-3
......@@ -413,7 +413,7 @@ pub const DecodeState = struct {
413413
414414 const DecodeLiteralsError = error{
415415 MalformedLiteralsLength,
416 PrefixNotFound,
416 NotFound,
417417 } || LiteralBitsError;
418418
419419 /// Decode `len` bytes of literals into `dest`.
......@@ -422,8 +422,8 @@ pub const DecodeState = struct {
422422 /// - `error.MalformedLiteralsLength` if the number of literal bytes
423423 /// decoded by `self` plus `len` is greater than the regenerated size of
424424 /// `literals`
425 /// - `error.UnexpectedEndOfLiteralStream` and `error.PrefixNotFound` if
426 /// there are problems decoding Huffman compressed literals
425 /// - `error.UnexpectedEndOfLiteralStream` and `error.NotFound` if there
426 /// are problems decoding Huffman compressed literals
427427 pub fn decodeLiteralsSlice(
428428 self: *DecodeState,
429429 dest: []u8,
lib/std/compress/zstandard/decompress.zig+126-43
......@@ -6,6 +6,8 @@ const types = @import("types.zig");
66const frame = types.frame;
77const LiteralsSection = types.compressed_block.LiteralsSection;
88const SequencesSection = types.compressed_block.SequencesSection;
9const SkippableHeader = types.frame.Skippable.Header;
10const ZstandardHeader = types.frame.Zstandard.Header;
911const Table = types.compressed_block.Table;
1012
1113pub const block = @import("decode/block.zig");
......@@ -16,15 +18,13 @@ const readers = @import("readers.zig");
1618
1719const readInt = std.mem.readIntLittle;
1820const readIntSlice = std.mem.readIntSliceLittle;
19fn readVarInt(comptime T: type, bytes: []const u8) T {
20 return std.mem.readVarInt(T, bytes, .Little);
21}
2221
22/// Returns `true` is `magic` is a valid magic number for a skippable frame
2323pub fn isSkippableMagic(magic: u32) bool {
2424 return frame.Skippable.magic_number_min <= magic and magic <= frame.Skippable.magic_number_max;
2525}
2626
27/// Returns the kind of frame at the beginning of `src`.
27/// Returns the kind of frame at the beginning of `source`.
2828///
2929/// Errors returned:
3030/// - `error.BadMagic` if `source` begins with bytes not equal to the
......@@ -50,11 +50,22 @@ pub fn frameType(magic: u32) error{BadMagic}!frame.Kind {
5050}
5151
5252pub const FrameHeader = union(enum) {
53 zstandard: types.frame.Zstandard.Header,
54 skippable: types.frame.Skippable.Header,
53 zstandard: ZstandardHeader,
54 skippable: SkippableHeader,
5555};
5656
57pub fn decodeFrameHeader(source: anytype) error{ BadMagic, EndOfStream, ReservedBitSet }!FrameHeader {
57pub const HeaderError = error{ BadMagic, EndOfStream, ReservedBitSet };
58
59/// Returns the header of the frame at the beginning of `source`.
60///
61/// Errors returned:
62/// - `error.BadMagic` if `source` begins with bytes not equal to the
63/// Zstandard frame magic number, or outside the range of magic numbers for
64/// skippable frames.
65/// - `error.EndOfStream` if `source` contains fewer than 4 bytes
66/// - `error.ReservedBitSet` if the frame is a Zstandard frame and any of the
67/// reserved bits are set
68pub fn decodeFrameHeader(source: anytype) HeaderError!FrameHeader {
5869 const magic = try source.readIntLittle(u32);
5970 const frame_type = try frameType(magic);
6071 switch (frame_type) {
......@@ -68,41 +79,74 @@ pub fn decodeFrameHeader(source: anytype) error{ BadMagic, EndOfStream, Reserved
6879 }
6980}
7081
71const ReadWriteCount = struct {
82pub const ReadWriteCount = struct {
7283 read_count: usize,
7384 write_count: usize,
7485};
7586
76/// Decodes frames from `src` into `dest`; see `decodeFrame()`.
77pub fn decode(dest: []u8, src: []const u8, verify_checksum: bool) !usize {
87/// Decodes frames from `src` into `dest`; returns the length of the result.
88/// The stream should not have extra trailing bytes - either all bytes in `src`
89/// will be decoded, or an error will be returned. An error will be returned if
90/// a Zstandard frame in `src` does not declare its content size.
91///
92/// Errors returned:
93/// - `error.DictionaryIdFlagUnsupported` if a `src` contains a frame that
94/// uses a dictionary
95/// - `error.MalformedFrame` if a frame in `src` is invalid
96/// - `error.UnknownContentSizeUnsupported` if a frame in `src` does not
97/// declare its content size
98pub fn decode(dest: []u8, src: []const u8, verify_checksum: bool) error{
99 MalformedFrame,
100 UnknownContentSizeUnsupported,
101 DictionaryIdFlagUnsupported,
102}!usize {
78103 var write_count: usize = 0;
79104 var read_count: usize = 0;
80105 while (read_count < src.len) {
81 const counts = try decodeFrame(dest, src[read_count..], verify_checksum);
106 const counts = decodeFrame(dest, src[read_count..], verify_checksum) catch |err| {
107 switch (err) {
108 error.UnknownContentSizeUnsupported => return error.UnknownContentSizeUnsupported,
109 error.DictionaryIdFlagUnsupported => return error.DictionaryIdFlagUnsupported,
110 else => return error.MalformedFrame,
111 }
112 };
82113 read_count += counts.read_count;
83114 write_count += counts.write_count;
84115 }
85116 return write_count;
86117}
87118
119/// Decodes a stream of frames from `src`; returns the decoded bytes. The stream
120/// should not have extra trailing bytes - either all bytes in `src` will be
121/// decoded, or an error will be returned.
122///
123/// Errors returned:
124/// - `error.DictionaryIdFlagUnsupported` if a `src` contains a frame that
125/// uses a dictionary
126/// - `error.MalformedFrame` if a frame in `src` is invalid
127/// - `error.OutOfMemory` if `allocator` cannot allocate enough memory
88128pub fn decodeAlloc(
89129 allocator: Allocator,
90130 src: []const u8,
91131 verify_checksum: bool,
92132 window_size_max: usize,
93) ![]u8 {
133) error{ DictionaryIdFlagUnsupported, MalformedFrame, OutOfMemory }![]u8 {
94134 var result = std.ArrayList(u8).init(allocator);
95135 errdefer result.deinit();
96136
97137 var read_count: usize = 0;
98138 while (read_count < src.len) {
99 read_count += try decodeFrameArrayList(
139 read_count += decodeFrameArrayList(
100140 allocator,
101141 &result,
102142 src[read_count..],
103143 verify_checksum,
104144 window_size_max,
105 );
145 ) catch |err| switch (err) {
146 error.OutOfMemory => return error.OutOfMemory,
147 error.DictionaryIdFlagUnsupported => return error.DictionaryIdFlagUnsupported,
148 else => return error.MalformedFrame,
149 };
106150 }
107151 return result.toOwnedSlice();
108152}
......@@ -112,18 +156,24 @@ pub fn decodeAlloc(
112156/// frames that declare the decompressed content size.
113157///
114158/// Errors returned:
159/// - `error.BadMagic` if the first 4 bytes of `src` is not a valid magic
160/// number for a Zstandard or skippable frame
115161/// - `error.UnknownContentSizeUnsupported` if the frame does not declare the
116162/// uncompressed content size
163/// - `error.WindowSizeUnknown` if the frame does not have a valid window size
117164/// - `error.ContentTooLarge` if `dest` is smaller than the uncompressed data
118165/// size declared by the frame header
119/// - `error.BadMagic` if the first 4 bytes of `src` is not a valid magic
120/// number for a Zstandard or Skippable frame
166/// - `error.ContentSizeTooLarge` if the frame header indicates a content size
167/// that is larger than `std.math.maxInt(usize)`
121168/// - `error.DictionaryIdFlagUnsupported` if the frame uses a dictionary
122169/// - `error.ChecksumFailure` if `verify_checksum` is true and the frame
123170/// contains a checksum that does not match the checksum of the decompressed
124171/// data
125/// - `error.ReservedBitSet` if the reserved bit of the frame header is set
172/// - `error.ReservedBitSet` if any of the reserved bits of the frame header
173/// are set
126174/// - `error.EndOfStream` if `src` does not contain a complete frame
175/// - `error.BadContentSize` if the content size declared by the frame does
176/// not equal the actual size of decompressed data
127177/// - an error in `block.Error` if there are errors decoding a block
128178/// - `error.SkippableSizeTooLarge` if the frame is skippable and reports a
129179/// size greater than `src.len`
......@@ -131,7 +181,15 @@ pub fn decodeFrame(
131181 dest: []u8,
132182 src: []const u8,
133183 verify_checksum: bool,
134) !ReadWriteCount {
184) (error{
185 BadMagic,
186 UnknownContentSizeUnsupported,
187 ContentTooLarge,
188 ContentSizeTooLarge,
189 WindowSizeUnknown,
190 DictionaryIdFlagUnsupported,
191 SkippableSizeTooLarge,
192} || FrameError)!ReadWriteCount {
135193 var fbs = std.io.fixedBufferStream(src);
136194 switch (try decodeFrameType(fbs.reader())) {
137195 .zstandard => return decodeZstandardFrame(dest, src, verify_checksum),
......@@ -153,16 +211,21 @@ pub fn decodeFrame(
153211///
154212/// Errors returned:
155213/// - `error.BadMagic` if the first 4 bytes of `src` is not a valid magic
156/// number for a Zstandard or Skippable frame
214/// number for a Zstandard or skippable frame
157215/// - `error.WindowSizeUnknown` if the frame does not have a valid window size
158216/// - `error.WindowTooLarge` if the window size is larger than
159217/// `window_size_max`
218/// - `error.ContentSizeTooLarge` if the frame header indicates a content size
219/// that is larger than `std.math.maxInt(usize)`
160220/// - `error.DictionaryIdFlagUnsupported` if the frame uses a dictionary
161221/// - `error.ChecksumFailure` if `verify_checksum` is true and the frame
162222/// contains a checksum that does not match the checksum of the decompressed
163223/// data
164/// - `error.ReservedBitSet` if the reserved bit of the frame header is set
224/// - `error.ReservedBitSet` if any of the reserved bits of the frame header
225/// are set
165226/// - `error.EndOfStream` if `src` does not contain a complete frame
227/// - `error.BadContentSize` if the content size declared by the frame does
228/// not equal the actual size of decompressed data
166229/// - `error.OutOfMemory` if `allocator` cannot allocate enough memory
167230/// - an error in `block.Error` if there are errors decoding a block
168231/// - `error.SkippableSizeTooLarge` if the frame is skippable and reports a
......@@ -173,12 +236,18 @@ pub fn decodeFrameArrayList(
173236 src: []const u8,
174237 verify_checksum: bool,
175238 window_size_max: usize,
176) !usize {
239) (error{ BadMagic, OutOfMemory, SkippableSizeTooLarge } || FrameContext.Error || FrameError)!usize {
177240 var fbs = std.io.fixedBufferStream(src);
178241 const reader = fbs.reader();
179242 const magic = try reader.readIntLittle(u32);
180243 switch (try frameType(magic)) {
181 .zstandard => return decodeZstandardFrameArrayList(allocator, dest, src, verify_checksum, window_size_max),
244 .zstandard => return decodeZstandardFrameArrayList(
245 allocator,
246 dest,
247 src,
248 verify_checksum,
249 window_size_max,
250 ),
182251 .skippable => {
183252 const content_size = try fbs.reader().readIntLittle(u32);
184253 if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge;
......@@ -211,7 +280,10 @@ const FrameError = error{
211280/// uncompressed content size
212281/// - `error.ContentTooLarge` if `dest` is smaller than the uncompressed data
213282/// size declared by the frame header
283/// - `error.WindowSizeUnknown` if the frame does not have a valid window size
214284/// - `error.DictionaryIdFlagUnsupported` if the frame uses a dictionary
285/// - `error.ContentSizeTooLarge` if the frame header indicates a content size
286/// that is larger than `std.math.maxInt(usize)`
215287/// - `error.ChecksumFailure` if `verify_checksum` is true and the frame
216288/// contains a checksum that does not match the checksum of the decompressed
217289/// data
......@@ -239,7 +311,11 @@ pub fn decodeZstandardFrame(
239311 var source = fbs.reader();
240312 const frame_header = try decodeZstandardHeader(source);
241313 consumed_count += fbs.pos;
242 break :context FrameContext.init(frame_header, std.math.maxInt(usize), verify_checksum) catch |err| switch (err) {
314 break :context FrameContext.init(
315 frame_header,
316 std.math.maxInt(usize),
317 verify_checksum,
318 ) catch |err| switch (err) {
243319 error.WindowTooLarge => unreachable,
244320 inline else => |e| return e,
245321 };
......@@ -260,7 +336,8 @@ pub fn decodeZStandardFrameBlocks(
260336 src: []const u8,
261337 frame_context: *FrameContext,
262338) (error{ ContentTooLarge, UnknownContentSizeUnsupported } || FrameError)!ReadWriteCount {
263 const content_size = frame_context.content_size orelse return error.UnknownContentSizeUnsupported;
339 const content_size = frame_context.content_size orelse
340 return error.UnknownContentSizeUnsupported;
264341 if (dest.len < content_size) return error.ContentTooLarge;
265342
266343 var consumed_count: usize = 0;
......@@ -304,14 +381,19 @@ pub const FrameContext = struct {
304381 ///
305382 /// Errors returned:
306383 /// - `error.DictionaryIdFlagUnsupported` if the frame uses a dictionary
307 /// - `error.WindowSizeUnknown` if the frame does not have a valid window size
308 /// - `error.WindowTooLarge` if the window size is larger than `window_size_max`
384 /// - `error.WindowSizeUnknown` if the frame does not have a valid window
385 /// size
386 /// - `error.WindowTooLarge` if the window size is larger than
387 /// `window_size_max`
388 /// - `error.ContentSizeTooLarge` if the frame header indicates a content
389 /// size larger than `std.math.maxInt(usize)`
309390 pub fn init(
310 frame_header: frame.Zstandard.Header,
391 frame_header: ZstandardHeader,
311392 window_size_max: usize,
312393 verify_checksum: bool,
313394 ) Error!FrameContext {
314 if (frame_header.descriptor.dictionary_id_flag != 0) return error.DictionaryIdFlagUnsupported;
395 if (frame_header.descriptor.dictionary_id_flag != 0)
396 return error.DictionaryIdFlagUnsupported;
315397
316398 const window_size_raw = frameWindowSize(frame_header) orelse return error.WindowSizeUnknown;
317399 const window_size = if (window_size_raw > window_size_max)
......@@ -319,7 +401,8 @@ pub const FrameContext = struct {
319401 else
320402 @intCast(usize, window_size_raw);
321403
322 const should_compute_checksum = frame_header.descriptor.content_checksum_flag and verify_checksum;
404 const should_compute_checksum =
405 frame_header.descriptor.content_checksum_flag and verify_checksum;
323406
324407 const content_size = if (frame_header.content_size) |size|
325408 std.math.cast(usize, size) orelse return error.ContentSizeTooLarge
......@@ -345,6 +428,8 @@ pub const FrameContext = struct {
345428/// - `error.WindowTooLarge` if the window size is larger than
346429/// `window_size_max`
347430/// - `error.DictionaryIdFlagUnsupported` if the frame uses a dictionary
431/// - `error.ContentSizeTooLarge` if the frame header indicates a content size
432/// that is larger than `std.math.maxInt(usize)`
348433/// - `error.ChecksumFailure` if `verify_checksum` is true and the frame
349434/// contains a checksum that does not match the checksum of the decompressed
350435/// data
......@@ -441,8 +526,6 @@ pub fn decodeZstandardFrameBlocksArrayList(
441526 return consumed_count;
442527}
443528
444/// Convenience wrapper for decoding all blocks in a frame; see
445/// `decodeZStandardFrameBlocks()`.
446529fn decodeFrameBlocksInner(
447530 dest: []u8,
448531 src: []const u8,
......@@ -459,7 +542,7 @@ fn decodeFrameBlocksInner(
459542 var bytes_read: usize = 3;
460543 defer consumed_count.* += bytes_read;
461544 var decode_state = block.DecodeState.init(&literal_fse_data, &match_fse_data, &offset_fse_data);
462 var written_count: usize = 0;
545 var count: usize = 0;
463546 while (true) : ({
464547 block_header = try block.decodeBlockHeaderSlice(src[bytes_read..]);
465548 bytes_read += 3;
......@@ -471,18 +554,18 @@ fn decodeFrameBlocksInner(
471554 &decode_state,
472555 &bytes_read,
473556 block_size_max,
474 written_count,
557 count,
475558 );
476 if (hash) |hash_state| hash_state.update(dest[written_count .. written_count + written_size]);
477 written_count += written_size;
559 if (hash) |hash_state| hash_state.update(dest[count .. count + written_size]);
560 count += written_size;
478561 if (block_header.last_block) break;
479562 }
480 return written_count;
563 return count;
481564}
482565
483566/// Decode the header of a skippable frame. The first four bytes of `src` must
484/// be a valid magic number for a Skippable frame.
485pub fn decodeSkippableHeader(src: *const [8]u8) frame.Skippable.Header {
567/// be a valid magic number for a skippable frame.
568pub fn decodeSkippableHeader(src: *const [8]u8) SkippableHeader {
486569 const magic = readInt(u32, src[0..4]);
487570 assert(isSkippableMagic(magic));
488571 const frame_size = readInt(u32, src[4..8]);
......@@ -494,7 +577,7 @@ pub fn decodeSkippableHeader(src: *const [8]u8) frame.Skippable.Header {
494577
495578/// Returns the window size required to decompress a frame, or `null` if it
496579/// cannot be determined (which indicates a malformed frame header).
497pub fn frameWindowSize(header: frame.Zstandard.Header) ?u64 {
580pub fn frameWindowSize(header: ZstandardHeader) ?u64 {
498581 if (header.window_descriptor) |descriptor| {
499582 const exponent = (descriptor & 0b11111000) >> 3;
500583 const mantissa = descriptor & 0b00000111;
......@@ -508,10 +591,10 @@ pub fn frameWindowSize(header: frame.Zstandard.Header) ?u64 {
508591/// Decode the header of a Zstandard frame.
509592///
510593/// Errors returned:
511/// - `error.ReservedBitSet` if the reserved bits of the header are set
594/// - `error.ReservedBitSet` if any of the reserved bits of the header are set
512595/// - `error.EndOfStream` if `source` does not contain a complete header
513pub fn decodeZstandardHeader(source: anytype) error{ EndOfStream, ReservedBitSet }!frame.Zstandard.Header {
514 const descriptor = @bitCast(frame.Zstandard.Header.Descriptor, try source.readByte());
596pub fn decodeZstandardHeader(source: anytype) error{ EndOfStream, ReservedBitSet }!ZstandardHeader {
597 const descriptor = @bitCast(ZstandardHeader.Descriptor, try source.readByte());
515598
516599 if (descriptor.reserved) return error.ReservedBitSet;
517600
......@@ -534,7 +617,7 @@ pub fn decodeZstandardHeader(source: anytype) error{ EndOfStream, ReservedBitSet
534617 if (field_size == 2) content_size.? += 256;
535618 }
536619
537 const header = frame.Zstandard.Header{
620 const header = ZstandardHeader{
538621 .descriptor = descriptor,
539622 .window_descriptor = window_descriptor,
540623 .dictionary_id = dictionary_id,
lib/std/compress/zstandard/types.zig+10-8
......@@ -92,13 +92,13 @@ pub const compressed_block = struct {
9292 index: usize,
9393 };
9494
95 pub fn query(self: HuffmanTree, index: usize, prefix: u16) error{PrefixNotFound}!Result {
95 pub fn query(self: HuffmanTree, index: usize, prefix: u16) error{NotFound}!Result {
9696 var node = self.nodes[index];
9797 const weight = node.weight;
9898 var i: usize = index;
9999 while (node.weight == weight) {
100100 if (node.prefix == prefix) return Result{ .symbol = node.symbol };
101 if (i == 0) return error.PrefixNotFound;
101 if (i == 0) return error.NotFound;
102102 i -= 1;
103103 node = self.nodes[i];
104104 }
......@@ -164,12 +164,14 @@ pub const compressed_block = struct {
164164 };
165165
166166 pub const match_length_code_table = [53]struct { u32, u5 }{
167 .{ 3, 0 }, .{ 4, 0 }, .{ 5, 0 }, .{ 6, 0 }, .{ 7, 0 }, .{ 8, 0 }, .{ 9, 0 }, .{ 10, 0 },
168 .{ 11, 0 }, .{ 12, 0 }, .{ 13, 0 }, .{ 14, 0 }, .{ 15, 0 }, .{ 16, 0 }, .{ 17, 0 }, .{ 18, 0 },
169 .{ 19, 0 }, .{ 20, 0 }, .{ 21, 0 }, .{ 22, 0 }, .{ 23, 0 }, .{ 24, 0 }, .{ 25, 0 }, .{ 26, 0 },
170 .{ 27, 0 }, .{ 28, 0 }, .{ 29, 0 }, .{ 30, 0 }, .{ 31, 0 }, .{ 32, 0 }, .{ 33, 0 }, .{ 34, 0 },
171 .{ 35, 1 }, .{ 37, 1 }, .{ 39, 1 }, .{ 41, 1 }, .{ 43, 2 }, .{ 47, 2 }, .{ 51, 3 }, .{ 59, 3 },
172 .{ 67, 4 }, .{ 83, 4 }, .{ 99, 5 }, .{ 131, 7 }, .{ 259, 8 }, .{ 515, 9 }, .{ 1027, 10 }, .{ 2051, 11 },
167 .{ 3, 0 }, .{ 4, 0 }, .{ 5, 0 }, .{ 6, 0 }, .{ 7, 0 }, .{ 8, 0 },
168 .{ 9, 0 }, .{ 10, 0 }, .{ 11, 0 }, .{ 12, 0 }, .{ 13, 0 }, .{ 14, 0 },
169 .{ 15, 0 }, .{ 16, 0 }, .{ 17, 0 }, .{ 18, 0 }, .{ 19, 0 }, .{ 20, 0 },
170 .{ 21, 0 }, .{ 22, 0 }, .{ 23, 0 }, .{ 24, 0 }, .{ 25, 0 }, .{ 26, 0 },
171 .{ 27, 0 }, .{ 28, 0 }, .{ 29, 0 }, .{ 30, 0 }, .{ 31, 0 }, .{ 32, 0 },
172 .{ 33, 0 }, .{ 34, 0 }, .{ 35, 1 }, .{ 37, 1 }, .{ 39, 1 }, .{ 41, 1 },
173 .{ 43, 2 }, .{ 47, 2 }, .{ 51, 3 }, .{ 59, 3 }, .{ 67, 4 }, .{ 83, 4 },
174 .{ 99, 5 }, .{ 131, 7 }, .{ 259, 8 }, .{ 515, 9 }, .{ 1027, 10 }, .{ 2051, 11 },
173175 .{ 4099, 12 }, .{ 8195, 13 }, .{ 16387, 14 }, .{ 32771, 15 }, .{ 65539, 16 },
174176 };
175177