Skip to content

Commit c6ef83e

Browse files
author
dweiller
committed
std.compress.zstandard: clean up streaming API
1 parent c7c35bf commit c6ef83e

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/std/compress/zstandard.zig

+24-13
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ pub const compressed_block = types.compressed_block;
88

99
pub const decompress = @import("zstandard/decompress.zig");
1010

11+
pub const DecompressStreamOptions = struct {
12+
verify_checksum: bool = true,
13+
window_size_max: usize = 1 << 23, // 8MiB default maximum window size,
14+
};
15+
1116
pub fn DecompressStream(
1217
comptime ReaderType: type,
13-
comptime verify_checksum: bool,
14-
comptime window_size_max: usize,
18+
comptime options: DecompressStreamOptions,
1519
) type {
1620
return struct {
1721
const Self = @This();
@@ -27,7 +31,7 @@ pub fn DecompressStream(
2731
offset_fse_buffer: []types.compressed_block.Table.Fse,
2832
literals_buffer: []u8,
2933
sequence_buffer: []u8,
30-
checksum: if (verify_checksum) ?u32 else void,
34+
checksum: if (options.verify_checksum) ?u32 else void,
3135
current_frame_decompressed_size: usize,
3236

3337
pub const Error = ReaderType.Error || error{
@@ -69,8 +73,8 @@ pub fn DecompressStream(
6973
const frame_context = context: {
7074
break :context try decompress.FrameContext.init(
7175
header,
72-
window_size_max,
73-
verify_checksum,
76+
options.window_size_max,
77+
options.verify_checksum,
7478
);
7579
};
7680

@@ -99,10 +103,10 @@ pub fn DecompressStream(
99103
);
100104
const buffer = try RingBuffer.init(self.allocator, frame_context.window_size);
101105

102-
const literals_data = try self.allocator.alloc(u8, window_size_max);
106+
const literals_data = try self.allocator.alloc(u8, options.window_size_max);
103107
errdefer self.allocator.free(literals_data);
104108

105-
const sequence_data = try self.allocator.alloc(u8, window_size_max);
109+
const sequence_data = try self.allocator.alloc(u8, options.window_size_max);
106110
errdefer self.allocator.free(sequence_data);
107111

108112
self.literal_fse_buffer = literal_fse_buffer;
@@ -116,7 +120,7 @@ pub fn DecompressStream(
116120
self.decode_state = decode_state;
117121
self.frame_context = frame_context;
118122

119-
self.checksum = if (verify_checksum) null else {};
123+
self.checksum = if (options.verify_checksum) null else {};
120124
self.current_frame_decompressed_size = 0;
121125

122126
self.state = .InFrame;
@@ -199,7 +203,7 @@ pub fn DecompressStream(
199203
if (self.frame_context.has_checksum) {
200204
const checksum = source_reader.readIntLittle(u32) catch
201205
return error.MalformedFrame;
202-
if (comptime verify_checksum) {
206+
if (comptime options.verify_checksum) {
203207
if (self.frame_context.hasher_opt) |*hasher| {
204208
if (checksum != decompress.computeChecksum(hasher))
205209
return error.ChecksumFailure;
@@ -232,17 +236,24 @@ pub fn DecompressStream(
232236
};
233237
}
234238

239+
pub fn decompressStreamOptions(
240+
allocator: Allocator,
241+
reader: anytype,
242+
comptime options: DecompressStreamOptions,
243+
) DecompressStream(@TypeOf(reader, options)) {
244+
return DecompressStream(@TypeOf(reader), options).init(allocator, reader);
245+
}
246+
235247
pub fn decompressStream(
236248
allocator: Allocator,
237249
reader: anytype,
238-
comptime window_size_max: usize,
239-
) DecompressStream(@TypeOf(reader), true, window_size_max) {
240-
return DecompressStream(@TypeOf(reader), true, 8 * (1 << 20)).init(allocator, reader);
250+
) DecompressStream(@TypeOf(reader), .{}) {
251+
return DecompressStream(@TypeOf(reader), .{}).init(allocator, reader);
241252
}
242253

243254
fn testDecompress(data: []const u8) ![]u8 {
244255
var in_stream = std.io.fixedBufferStream(data);
245-
var zstd_stream = decompressStream(std.testing.allocator, in_stream.reader(), 1 << 23);
256+
var zstd_stream = decompressStream(std.testing.allocator, in_stream.reader());
246257
defer zstd_stream.deinit();
247258
const result = zstd_stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize));
248259
return result;

0 commit comments

Comments
 (0)