Skip to content

Commit 19e32f4

Browse files
committed
Add field last_input_offset to report consumed input buf size
Useful when needing more output, while users need to slice the input to continue
1 parent 7e565a5 commit 19e32f4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/stream.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct CompressStream {
2626
state: BrotliEncoderStateStruct<StandardAlloc>,
2727
result: i32,
2828
total_out: usize,
29+
last_input_offset: usize,
2930
}
3031

3132
impl Drop for CompressStream {
@@ -57,6 +58,7 @@ impl CompressStream {
5758
state,
5859
result: BrotliStreamResult::Init as i32,
5960
total_out: 0,
61+
last_input_offset: 0,
6062
})
6163
}
6264

@@ -94,17 +96,21 @@ impl CompressStream {
9496
if ret != 0 {
9597
if available_out == 0 {
9698
self.result = BrotliStreamResult::NeedsMoreOutput as i32;
99+
self.last_input_offset = input_offset;
97100
Ok(output.into_boxed_slice())
98101
} else if available_in == 0 {
99102
output.truncate(output_offset);
100103
self.result = BrotliStreamResult::NeedsMoreInput as i32;
104+
self.last_input_offset = input.len();
101105
Ok(output.into_boxed_slice())
102106
} else {
103107
self.result = -1;
108+
self.last_input_offset = 0;
104109
Err(JsValue::from_str("Unexpected Brotli streaming compress: both available_in & available_out are not 0 after a successful processing"))
105110
}
106111
} else {
107112
self.result = -1;
113+
self.last_input_offset = 0;
108114
Err(JsValue::from_str(
109115
"Brotli streaming compress failed: When processing",
110116
))
@@ -136,6 +142,7 @@ impl CompressStream {
136142
}
137143
output.truncate(output_offset);
138144
self.result = BrotliStreamResult::ResultSuccess as i32;
145+
self.last_input_offset = 0;
139146
Ok(output.into_boxed_slice())
140147
}
141148
}
@@ -148,13 +155,18 @@ impl CompressStream {
148155
pub fn result(&self) -> i32 {
149156
self.result
150157
}
158+
159+
pub fn last_input_offset(&self) -> usize {
160+
self.last_input_offset
161+
}
151162
}
152163

153164
#[wasm_bindgen]
154165
pub struct DecompressStream {
155166
state: BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>,
156167
result: i32,
157168
total_out: usize,
169+
last_input_offset: usize,
158170
}
159171

160172
#[wasm_bindgen]
@@ -168,6 +180,7 @@ impl DecompressStream {
168180
state: BrotliState::new(alloc, alloc, alloc),
169181
result: BrotliStreamResult::Init as i32,
170182
total_out: 0,
183+
last_input_offset: 0,
171184
}
172185
}
173186

@@ -194,23 +207,27 @@ impl DecompressStream {
194207
BrotliResult::ResultFailure => {
195208
// It should be a negative error code
196209
self.result = self.state.error_code as i32;
210+
self.last_input_offset = 0;
197211
Err(JsValue::from_str(&format!(
198212
"Brotli streaming decompress failed: Error code {}",
199213
self.result
200214
)))
201215
}
202216
BrotliResult::NeedsMoreOutput => {
203217
self.result = BrotliStreamResult::NeedsMoreOutput as i32;
218+
self.last_input_offset = input_offset;
204219
Ok(output.into_boxed_slice())
205220
}
206221
BrotliResult::ResultSuccess => {
207222
output.truncate(output_offset);
208223
self.result = BrotliStreamResult::ResultSuccess as i32;
224+
self.last_input_offset = input.len();
209225
Ok(output.into_boxed_slice())
210226
}
211227
BrotliResult::NeedsMoreInput => {
212228
output.truncate(output_offset);
213229
self.result = BrotliStreamResult::NeedsMoreInput as i32;
230+
self.last_input_offset = input.len();
214231
Ok(output.into_boxed_slice())
215232
}
216233
}
@@ -223,4 +240,8 @@ impl DecompressStream {
223240
pub fn result(&self) -> i32 {
224241
self.result
225242
}
243+
244+
pub fn last_input_offset(&self) -> usize {
245+
self.last_input_offset
246+
}
226247
}

0 commit comments

Comments
 (0)