| ... | ... | @@ -7,13 +7,16 @@ pub const compressed_block = types.compressed_block; |
| 7 | 7 | |
| 8 | 8 | pub const decompress = @import("zstandard/decompress.zig"); |
| 9 | 9 | |
| 10 | | pub const DecompressStreamOptions = struct { |
| 10 | pub const DecompressorOptions = struct { |
| 11 | 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( |
| 15 | | comptime ReaderType: type, |
| 16 | | ) type { |
| 19 | pub fn Decompressor(comptime ReaderType: type) type { |
| 17 | 20 | return struct { |
| 18 | 21 | const Self = @This(); |
| 19 | 22 | |
| ... | ... | @@ -49,13 +52,13 @@ pub fn DecompressStream( |
| 49 | 52 | |
| 50 | 53 | pub const Reader = std.io.Reader(*Self, Error, read); |
| 51 | 54 | |
| 52 | | pub fn init(source: ReaderType, window_buffer: []u8, options: DecompressStreamOptions) Self { |
| 53 | | return Self{ |
| 55 | pub fn init(source: ReaderType, options: DecompressorOptions) Self { |
| 56 | return .{ |
| 54 | 57 | .source = std.io.countingReader(source), |
| 55 | 58 | .state = .NewFrame, |
| 56 | 59 | .decode_state = undefined, |
| 57 | 60 | .frame_context = undefined, |
| 58 | | .buffer = .{ .data = window_buffer }, |
| 61 | .buffer = .{ .data = options.window_buffer }, |
| 59 | 62 | .literal_fse_buffer = undefined, |
| 60 | 63 | .match_fse_buffer = undefined, |
| 61 | 64 | .offset_fse_buffer = undefined, |
| ... | ... | @@ -199,20 +202,8 @@ pub fn DecompressStream( |
| 199 | 202 | }; |
| 200 | 203 | } |
| 201 | 204 | |
| 202 | | pub fn decompressStreamOptions( |
| 203 | | reader: anytype, |
| 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); |
| 205 | pub fn decompressor(reader: anytype, options: DecompressorOptions) Decompressor(@TypeOf(reader)) { |
| 206 | return Decompressor(@TypeOf(reader)).init(reader, options); |
| 216 | 207 | } |
| 217 | 208 | |
| 218 | 209 | fn testDecompress(data: []const u8) ![]u8 { |
| ... | ... | @@ -220,7 +211,7 @@ fn testDecompress(data: []const u8) ![]u8 { |
| 220 | 211 | defer std.testing.allocator.free(window_buffer); |
| 221 | 212 | |
| 222 | 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 | 215 | const result = zstd_stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); |
| 225 | 216 | return result; |
| 226 | 217 | } |
| ... | ... | @@ -249,7 +240,7 @@ test "zstandard decompression" { |
| 249 | 240 | } |
| 250 | 241 | |
| 251 | 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 | 244 | // over 1MiB stack space is needed via the --stack CLI flag |
| 254 | 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 | 273 | defer std.testing.allocator.free(window_buffer); |
| 283 | 274 | |
| 284 | 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 | 278 | const result = try stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); |
| 288 | 279 | defer std.testing.allocator.free(result); |
| ... | ... | @@ -307,7 +298,7 @@ test "zero sized block" { |
| 307 | 298 | } |
| 308 | 299 | |
| 309 | 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 | 302 | // over 1MiB stack space is needed via the --stack CLI flag |
| 312 | 303 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; |
| 313 | 304 | |