authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-08 00:28:06+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-20 09:09:06+11:00
logd9a90e181873d06b9e8632a86ff8c399e024974e
tree9dc99f96fdc50136e1565e19ebab65b55c8caf49
parent2134769436f68547a5c1d93184a10fab03085b2a

std.compress.zstandard: fix decodeAlloc() and remove decodeFrameAlloc()


1 files changed, 10 insertions(+), 38 deletions(-)

lib/std/compress/zstandard/decompress.zig+10-38
......@@ -77,7 +77,7 @@ pub fn decodeAlloc(
7777
7878 var read_count: usize = 0;
7979 while (read_count < src.len) {
80 read_count += try decodeZstandardFrameArrayList(
80 read_count += try decodeFrameArrayList(
8181 allocator,
8282 &result,
8383 src[read_count..],
......@@ -140,8 +140,7 @@ pub const DecodedFrame = union(enum) {
140140};
141141
142142/// Decodes the frame at the start of `src` into `dest`. Returns the number of
143/// bytes read from `src` and the decoded bytes for a Zstandard frame, or the
144/// frame header for a Skippable frame.
143/// bytes read from `src`.
145144///
146145/// Errors returned:
147146/// - `error.BadMagic` if the first 4 bytes of `src` is not a valid magic
......@@ -160,30 +159,24 @@ pub const DecodedFrame = union(enum) {
160159/// - an error in `block.Error` if there are errors decoding a block
161160/// - `error.SkippableSizeTooLarge` if the frame is skippable and reports a
162161/// size greater than `src.len`
163pub fn decodeFrameAlloc(
162pub fn decodeFrameArrayList(
164163 allocator: Allocator,
164 dest: *std.ArrayList(u8),
165165 src: []const u8,
166166 verify_checksum: bool,
167167 window_size_max: usize,
168) !DecodedFrame {
168) !usize {
169169 var fbs = std.io.fixedBufferStream(src);
170170 const reader = fbs.reader();
171171 const magic = try reader.readIntLittle(u32);
172172 switch (try frameType(magic)) {
173 .zstandard => return .{
174 .zstandard = try decodeZstandardFrameAlloc(allocator, src, verify_checksum, window_size_max),
175 },
173 .zstandard => return decodeZstandardFrameArrayList(allocator, dest, src, verify_checksum, window_size_max),
176174 .skippable => {
177175 const content_size = try fbs.reader().readIntLittle(u32);
178176 if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge;
179177 const read_count = @as(usize, content_size) + 8;
180178 if (read_count > src.len) return error.SkippableSizeTooLarge;
181 return .{
182 .skippable = .{
183 .magic_number = magic,
184 .frame_size = content_size,
185 },
186 };
179 return read_count;
187180 },
188181 }
189182}
......@@ -319,11 +312,9 @@ pub const FrameContext = struct {
319312 }
320313};
321314
322/// Decode a Zstandard from from `src` and return the decompressed bytes and the
323/// number of bytes read; see `decodeZstandardFrame()`. `allocator` is used to
324/// allocate both the returned slice and internal buffers used during decoding.
325/// The first four bytes of `src` must be the magic number for a Zstandard
326/// frame.
315/// Decode a Zstandard from from `src` and return number of bytes read; see
316/// `decodeZstandardFrame()`. The first four bytes of `src` must be the magic
317/// number for a Zstandard frame.
327318///
328319/// Errors returned:
329320/// - `error.WindowSizeUnknown` if the frame does not have a valid window size
......@@ -340,25 +331,6 @@ pub const FrameContext = struct {
340331/// - an error in `block.Error` if there are errors decoding a block
341332/// - `error.BadContentSize` if the content size declared by the frame does
342333/// not equal the size of decompressed data
343pub fn decodeZstandardFrameAlloc(
344 allocator: Allocator,
345 src: []const u8,
346 verify_checksum: bool,
347 window_size_max: usize,
348) (error{OutOfMemory} || FrameContext.Error || FrameError)!DecodeResult {
349 var result = std.ArrayList(u8).init(allocator);
350 errdefer result.deinit();
351 const read_count = try decodeZstandardFrameArrayList(
352 allocator,
353 &result,
354 src,
355 verify_checksum,
356 window_size_max,
357 );
358 return DecodeResult{ .bytes = try result.toOwnedSlice(), .read_count = read_count };
359}
360
361/// Decode a ZStandard frame into `dest`; see `decodeZStandardFrameAlloc()`.
362334pub fn decodeZstandardFrameArrayList(
363335 allocator: Allocator,
364336 dest: *std.ArrayList(u8),