| ... | @@ -7,13 +7,16 @@ pub const compressed_block = types.compressed_block; | ... | @@ -7,13 +7,16 @@ pub const compressed_block = types.compressed_block; |
| 7 | | 7 | |
| 8 | pub const decompress = @import("zstandard/decompress.zig"); | 8 | pub const decompress = @import("zstandard/decompress.zig"); |
| 9 | | 9 | |
| 10 | pub const DecompressStreamOptions = struct { | 10 | pub const DecompressorOptions = struct { |
| 11 | verify_checksum: bool = true, | 11 | verify_checksum: bool = true, |
| | 12 | window_buffer: []u8, |
| | 13 | |
| | 14 | /// Recommended amount by the standard. Lower than this may result |
| | 15 | /// in inability to decompress common streams. |
| | 16 | pub const default_window_buffer_len = 8 * 1024 * 1024; |
| 12 | }; | 17 | }; |
| 13 | | 18 | |
| 14 | pub fn DecompressStream( | 19 | pub fn Decompressor(comptime ReaderType: type) type { |
| 15 | comptime ReaderType: type, | | |
| 16 | ) type { | | |
| 17 | return struct { | 20 | return struct { |
| 18 | const Self = @This(); | 21 | const Self = @This(); |
| 19 | | 22 | |
| ... | @@ -49,13 +52,13 @@ pub fn DecompressStream( | ... | @@ -49,13 +52,13 @@ pub fn DecompressStream( |
| 49 | | 52 | |
| 50 | pub const Reader = std.io.Reader(*Self, Error, read); | 53 | pub const Reader = std.io.Reader(*Self, Error, read); |
| 51 | | 54 | |
| 52 | pub fn init(source: ReaderType, window_buffer: []u8, options: DecompressStreamOptions) Self { | 55 | pub fn init(source: ReaderType, options: DecompressorOptions) Self { |
| 53 | return Self{ | 56 | return .{ |
| 54 | .source = std.io.countingReader(source), | 57 | .source = std.io.countingReader(source), |
| 55 | .state = .NewFrame, | 58 | .state = .NewFrame, |
| 56 | .decode_state = undefined, | 59 | .decode_state = undefined, |
| 57 | .frame_context = undefined, | 60 | .frame_context = undefined, |
| 58 | .buffer = .{ .data = window_buffer }, | 61 | .buffer = .{ .data = options.window_buffer }, |
| 59 | .literal_fse_buffer = undefined, | 62 | .literal_fse_buffer = undefined, |
| 60 | .match_fse_buffer = undefined, | 63 | .match_fse_buffer = undefined, |
| 61 | .offset_fse_buffer = undefined, | 64 | .offset_fse_buffer = undefined, |
| ... | @@ -199,20 +202,8 @@ pub fn DecompressStream( | ... | @@ -199,20 +202,8 @@ pub fn DecompressStream( |
| 199 | }; | 202 | }; |
| 200 | } | 203 | } |
| 201 | | 204 | |
| 202 | pub fn decompressStreamOptions( | 205 | pub fn decompressor(reader: anytype, options: DecompressorOptions) Decompressor(@TypeOf(reader)) { |
| 203 | reader: anytype, | 206 | return Decompressor(@TypeOf(reader)).init(reader, options); |
| 204 | comptime options: DecompressStreamOptions, | | |
| 205 | window_buffer: *[options.window_size_max]u8, | | |
| 206 | ) DecompressStream(@TypeOf(reader), options) { | | |
| 207 | return DecompressStream(@TypeOf(reader), options).init(reader, window_buffer); | | |
| 208 | } | | |
| 209 | | | |
| 210 | pub fn decompressStream( | | |
| 211 | reader: anytype, | | |
| 212 | window_buffer: []u8, | | |
| 213 | options: DecompressStreamOptions, | | |
| 214 | ) DecompressStream(@TypeOf(reader)) { | | |
| 215 | return DecompressStream(@TypeOf(reader)).init(reader, window_buffer, options); | | |
| 216 | } | 207 | } |
| 217 | | 208 | |
| 218 | fn testDecompress(data: []const u8) ![]u8 { | 209 | fn testDecompress(data: []const u8) ![]u8 { |
| ... | @@ -220,7 +211,7 @@ fn testDecompress(data: []const u8) ![]u8 { | ... | @@ -220,7 +211,7 @@ fn testDecompress(data: []const u8) ![]u8 { |
| 220 | defer std.testing.allocator.free(window_buffer); | 211 | defer std.testing.allocator.free(window_buffer); |
| 221 | | 212 | |
| 222 | var in_stream = std.io.fixedBufferStream(data); | 213 | var in_stream = std.io.fixedBufferStream(data); |
| 223 | var zstd_stream = decompressStream(in_stream.reader(), window_buffer, .{}); | 214 | var zstd_stream = decompressor(in_stream.reader(), .{ .window_buffer = window_buffer }); |
| 224 | const result = zstd_stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); | 215 | const result = zstd_stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); |
| 225 | return result; | 216 | return result; |
| 226 | } | 217 | } |
| ... | @@ -249,7 +240,7 @@ test "zstandard decompression" { | ... | @@ -249,7 +240,7 @@ test "zstandard decompression" { |
| 249 | } | 240 | } |
| 250 | | 241 | |
| 251 | test "zstandard streaming decompression" { | 242 | test "zstandard streaming decompression" { |
| 252 | // default stack size for wasm32 is too low for DecompressStream - slightly | 243 | // default stack size for wasm32 is too low for Decompressor - slightly |
| 253 | // over 1MiB stack space is needed via the --stack CLI flag | 244 | // over 1MiB stack space is needed via the --stack CLI flag |
| 254 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; | 245 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; |
| 255 | | 246 | |
| ... | @@ -282,7 +273,7 @@ fn expectEqualDecodedStreaming(expected: []const u8, input: []const u8) !void { | ... | @@ -282,7 +273,7 @@ fn expectEqualDecodedStreaming(expected: []const u8, input: []const u8) !void { |
| 282 | defer std.testing.allocator.free(window_buffer); | 273 | defer std.testing.allocator.free(window_buffer); |
| 283 | | 274 | |
| 284 | var in_stream = std.io.fixedBufferStream(input); | 275 | var in_stream = std.io.fixedBufferStream(input); |
| 285 | var stream = decompressStream(in_stream.reader(), window_buffer, .{}); | 276 | var stream = decompressor(in_stream.reader(), .{ .window_buffer = window_buffer }); |
| 286 | | 277 | |
| 287 | const result = try stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); | 278 | const result = try stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); |
| 288 | defer std.testing.allocator.free(result); | 279 | defer std.testing.allocator.free(result); |
| ... | @@ -307,7 +298,7 @@ test "zero sized block" { | ... | @@ -307,7 +298,7 @@ test "zero sized block" { |
| 307 | } | 298 | } |
| 308 | | 299 | |
| 309 | test "zero sized block streaming" { | 300 | test "zero sized block streaming" { |
| 310 | // default stack size for wasm32 is too low for DecompressStream - slightly | 301 | // default stack size for wasm32 is too low for Decompressor - slightly |
| 311 | // over 1MiB stack space is needed via the --stack CLI flag | 302 | // over 1MiB stack space is needed via the --stack CLI flag |
| 312 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; | 303 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; |
| 313 | | 304 | |