Skip to content

Commit 8605599

Browse files
compress,compress.gzip: add decompress_with_callback API (#24904)
1 parent 0d8cc45 commit 8605599

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

vlib/compress/compress.c.v

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,51 @@ pub fn decompress(data []u8, flags int) ![]u8 {
5151
return ret
5252
}
5353
}
54+
55+
// ChunkCallback is used to receive decompressed chunks of maximum 32768 bytes.
56+
// After processing the chunk this function should return the chunk's length to indicate
57+
// the decompressor to send more chunks, otherwise the decompression stops.
58+
// The userdata parameter comes from the call to decompress_with_callback/4, and can be used
59+
// to pass arbitrary data, without having to create a closure.
60+
pub type ChunkCallback = fn (chunk []u8, userdata voidptr) int
61+
62+
// decompress_with_callback decompresses an array of bytes based on the provided flags,
63+
// and a V fn callback to receive decompressed chunks, of at most 32 kilobytes each.
64+
// It returns the total decompressed length, or a decompression error.
65+
// NB: this is a low level api, a high level implementation like zlib/gzip should be preferred.
66+
pub fn decompress_with_callback(data []u8, cb ChunkCallback, userdata voidptr, flags int) !u64 {
67+
cbdata := DecompressionCallBackData{
68+
data: data.data
69+
size: usize(data.len)
70+
cb: cb
71+
userdata: userdata
72+
}
73+
status := C.tinfl_decompress_mem_to_callback(cbdata.data, &cbdata.size, c_cb_for_decompress_mem,
74+
&cbdata, flags)
75+
if status == 0 {
76+
return error('decompression error')
77+
}
78+
return cbdata.decompressed_size
79+
}
80+
81+
struct DecompressionCallBackData {
82+
mut:
83+
data voidptr
84+
size usize
85+
decompressed_size u64
86+
userdata voidptr
87+
cb ChunkCallback = unsafe { nil }
88+
}
89+
90+
fn c_cb_for_decompress_mem(buf &char, len int, pdcbd voidptr) int {
91+
mut cbdata := unsafe { &DecompressionCallBackData(pdcbd) }
92+
if cbdata.cb(unsafe { voidptr(buf).vbytes(len) }, cbdata.userdata) == len {
93+
cbdata.decompressed_size += u64(len)
94+
return 1 // continue decompressing
95+
}
96+
return 0 // stop decompressing
97+
}
98+
99+
type DecompressCallback = fn (const_buffer voidptr, len int, userdata voidptr) int
100+
101+
fn C.tinfl_decompress_mem_to_callback(const_input_buffer voidptr, psize &usize, put_buf_cb DecompressCallback, userdata voidptr, flags int) int

vlib/compress/gzip/gzip.v

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn validate(data []u8, params DecompressParams) !GzipHeader {
203203
return header
204204
}
205205

206-
// decompresses an array of bytes using zlib and returns the decompressed bytes in a new array
206+
// decompress an array of bytes using zlib and returns the decompressed bytes in a new array
207207
// Example: decompressed := gzip.decompress(b)!
208208
pub fn decompress(data []u8, params DecompressParams) ![]u8 {
209209
gzip_header := validate(data, params)!
@@ -221,3 +221,20 @@ pub fn decompress(data []u8, params DecompressParams) ![]u8 {
221221
}
222222
return decompressed
223223
}
224+
225+
// decompress_with_callback decompresses the given `data`, using zlib. It calls `cb` with each chunk of decompressed bytes.
226+
// A chunk is usually 32 KB or less. Note: the chunk data received by `cb` should be cloned, if you need to store it for later,
227+
// and not process it right away.
228+
// The callback function should return the chunk length, if it wants to continue decompressing, or 0, if it wants to abort the decompression early.
229+
// See also compress.ChunkCallback for more details.
230+
pub fn decompress_with_callback(data []u8, cb compr.ChunkCallback, userdata voidptr, params DecompressParams) !int {
231+
gzip_header := validate(data, params)!
232+
header_len := gzip_header.length
233+
expected_len := int((u32(data[data.len - 1]) << 24) | (u32(data[data.len - 2]) << 16) | (u32(data[data.len - 3]) << 8) | data[data.len - 4])
234+
body := data[header_len..data.len - 8]
235+
chunks_len := int(compr.decompress_with_callback(body, cb, userdata, 0)!)
236+
if params.verify_length && expected_len != chunks_len {
237+
return error('Decompress error: expected length:${expected_len}, got:${chunks_len}')
238+
}
239+
return chunks_len
240+
}

vlib/compress/gzip/gzip_test.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,18 @@ fn test_gzip_with_invalid_flags() {
132132
compressed[3] |= 0b1000_0000
133133
assert_decompress_error(compressed, 'reserved flags are set, unsupported field detected')!
134134
}
135+
136+
fn test_gzip_decompress_callback() {
137+
uncompressed := '321323'.repeat(10_000)
138+
gz := compress(uncompressed.bytes())!
139+
mut size := 0
140+
mut ref := &size
141+
decoded := decompress_with_callback(gz, fn (chunk []u8, ref &int) int {
142+
unsafe {
143+
*ref += chunk.len
144+
}
145+
return chunk.len
146+
}, ref)!
147+
assert decoded == size
148+
assert decoded == uncompressed.len
149+
}

0 commit comments

Comments
 (0)