From c6a89d18ac4978f9b5b41935d5cf3fc27e76bd79 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Tue, 21 Feb 2023 14:31:02 +1100 Subject: [PATCH] std.compress.zstandard: clean up streaming API --- lib/std/compress/zstandard.zig | 35 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/std/compress/zstandard.zig b/lib/std/compress/zstandard.zig index fcffed99f1cc..959552c89b14 100644 --- a/lib/std/compress/zstandard.zig +++ b/lib/std/compress/zstandard.zig @@ -8,10 +8,14 @@ pub const compressed_block = types.compressed_block; pub const decompress = @import("zstandard/decompress.zig"); +pub const DecompressStreamOptions = struct { + verify_checksum: bool = true, + window_size_max: usize = 1 << 23 // 8MiB default maximum window size, +}; + pub fn DecompressStream( comptime ReaderType: type, - comptime verify_checksum: bool, - comptime window_size_max: usize, + comptime options: DecompressStreamOptions, ) type { return struct { const Self = @This(); @@ -27,7 +31,7 @@ pub fn DecompressStream( offset_fse_buffer: []types.compressed_block.Table.Fse, literals_buffer: []u8, sequence_buffer: []u8, - checksum: if (verify_checksum) ?u32 else void, + checksum: if (options.verify_checksum) ?u32 else void, current_frame_decompressed_size: usize, pub const Error = ReaderType.Error || error{ @@ -69,8 +73,8 @@ pub fn DecompressStream( const frame_context = context: { break :context try decompress.FrameContext.init( header, - window_size_max, - verify_checksum, + options.window_size_max, + options.verify_checksum, ); }; @@ -99,10 +103,10 @@ pub fn DecompressStream( ); const buffer = try RingBuffer.init(self.allocator, frame_context.window_size); - const literals_data = try self.allocator.alloc(u8, window_size_max); + const literals_data = try self.allocator.alloc(u8, options.window_size_max); errdefer self.allocator.free(literals_data); - const sequence_data = try self.allocator.alloc(u8, window_size_max); + const sequence_data = try self.allocator.alloc(u8, options.window_size_max); errdefer self.allocator.free(sequence_data); self.literal_fse_buffer = literal_fse_buffer; @@ -116,7 +120,7 @@ pub fn DecompressStream( self.decode_state = decode_state; self.frame_context = frame_context; - self.checksum = if (verify_checksum) null else {}; + self.checksum = if (options.verify_checksum) null else {}; self.current_frame_decompressed_size = 0; self.state = .InFrame; @@ -199,7 +203,7 @@ pub fn DecompressStream( if (self.frame_context.has_checksum) { const checksum = source_reader.readIntLittle(u32) catch return error.MalformedFrame; - if (comptime verify_checksum) { + if (comptime options.verify_checksum) { if (self.frame_context.hasher_opt) |*hasher| { if (checksum != decompress.computeChecksum(hasher)) return error.ChecksumFailure; @@ -232,12 +236,19 @@ pub fn DecompressStream( }; } +pub fn decompressStreamOptions( + allocator: Allocator, + reader: anytype, + comptime options: DecompressStreamOptions, +) DecompressStream(@TypeOf(reader, options)) { + return DecompressStream(@TypeOf(reader), options).init(allocator, reader); +} + pub fn decompressStream( allocator: Allocator, reader: anytype, - comptime window_size_max: usize, -) DecompressStream(@TypeOf(reader), true, window_size_max) { - return DecompressStream(@TypeOf(reader), true, 8 * (1 << 20)).init(allocator, reader); +) DecompressStream(@TypeOf(reader), .{}) { + return DecompressStream(@TypeOf(reader, .{})).init(allocator, reader); } fn testDecompress(data: []const u8) ![]u8 {