forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.rs
More file actions
373 lines (320 loc) · 9.88 KB
/
compression.rs
File metadata and controls
373 lines (320 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// spell-checker:ignore chunker
//! internal shared module for compression libraries
use crate::vm::function::{ArgBytesLike, ArgSize, OptionalArg};
use crate::vm::{
PyResult, VirtualMachine,
builtins::{PyBaseExceptionRef, PyBytesRef},
convert::ToPyException,
};
pub const USE_AFTER_FINISH_ERR: &str = "Error -2: inconsistent stream state";
// TODO: don't hardcode
const CHUNKSIZE: usize = u32::MAX as usize;
#[derive(FromArgs)]
pub struct DecompressArgs {
#[pyarg(positional)]
data: ArgBytesLike,
#[pyarg(any, optional)]
max_length: OptionalArg<ArgSize>,
}
impl DecompressArgs {
pub fn data(&self) -> crate::common::borrow::BorrowedValue<'_, [u8]> {
self.data.borrow_buf()
}
pub fn raw_max_length(&self) -> Option<isize> {
self.max_length.into_option().map(|ArgSize { value }| value)
}
// negative is None
pub fn max_length(&self) -> Option<usize> {
self.max_length
.into_option()
.and_then(|ArgSize { value }| usize::try_from(value).ok())
}
}
pub trait Decompressor {
type Flush: DecompressFlushKind;
type Status: DecompressStatus;
type Error;
fn total_in(&self) -> u64;
fn decompress_vec(
&mut self,
input: &[u8],
output: &mut Vec<u8>,
flush: Self::Flush,
) -> Result<Self::Status, Self::Error>;
fn maybe_set_dict(&mut self, err: Self::Error) -> Result<(), Self::Error> {
Err(err)
}
}
pub trait DecompressStatus {
fn is_stream_end(&self) -> bool;
}
pub trait DecompressFlushKind: Copy {
const SYNC: Self;
}
impl DecompressFlushKind for () {
const SYNC: Self = ();
}
pub const fn flush_sync<T: DecompressFlushKind>(_final_chunk: bool) -> T {
T::SYNC
}
#[derive(Clone)]
pub struct Chunker<'a> {
data1: &'a [u8],
data2: &'a [u8],
}
impl<'a> Chunker<'a> {
pub const fn new(data: &'a [u8]) -> Self {
Self {
data1: data,
data2: &[],
}
}
pub const fn chain(data1: &'a [u8], data2: &'a [u8]) -> Self {
if data1.is_empty() {
Self {
data1: data2,
data2: &[],
}
} else {
Self { data1, data2 }
}
}
pub const fn len(&self) -> usize {
self.data1.len() + self.data2.len()
}
pub const fn is_empty(&self) -> bool {
self.data1.is_empty()
}
pub fn to_vec(&self) -> Vec<u8> {
[self.data1, self.data2].concat()
}
pub fn chunk(&self) -> &'a [u8] {
self.data1.get(..CHUNKSIZE).unwrap_or(self.data1)
}
pub fn advance(&mut self, consumed: usize) {
self.data1 = &self.data1[consumed..];
if self.data1.is_empty() {
self.data1 = std::mem::take(&mut self.data2);
}
}
}
pub fn _decompress<D: Decompressor>(
data: &[u8],
d: &mut D,
bufsize: usize,
max_length: Option<usize>,
calc_flush: impl Fn(bool) -> D::Flush,
) -> Result<(Vec<u8>, bool), D::Error> {
let mut data = Chunker::new(data);
_decompress_chunks(&mut data, d, bufsize, max_length, calc_flush)
}
pub fn _decompress_chunks<D: Decompressor>(
data: &mut Chunker<'_>,
d: &mut D,
bufsize: usize,
max_length: Option<usize>,
calc_flush: impl Fn(bool) -> D::Flush,
) -> Result<(Vec<u8>, bool), D::Error> {
if data.is_empty() {
return Ok((Vec::new(), true));
}
let max_length = max_length.unwrap_or(usize::MAX);
let mut buf = Vec::new();
'outer: loop {
let chunk = data.chunk();
let flush = calc_flush(chunk.len() == data.len());
loop {
let additional = std::cmp::min(bufsize, max_length - buf.capacity());
if additional == 0 {
return Ok((buf, false));
}
buf.reserve_exact(additional);
let prev_in = d.total_in();
let res = d.decompress_vec(chunk, &mut buf, flush);
let consumed = d.total_in() - prev_in;
data.advance(consumed as usize);
match res {
Ok(status) => {
let stream_end = status.is_stream_end();
if stream_end || data.is_empty() {
// we've reached the end of the stream, we're done
buf.shrink_to_fit();
return Ok((buf, stream_end));
} else if !chunk.is_empty() && consumed == 0 {
// we're gonna need a bigger buffer
continue;
} else {
// next chunk
continue 'outer;
}
}
Err(e) => {
d.maybe_set_dict(e)?;
// now try the next chunk
continue 'outer;
}
};
}
}
}
pub trait Compressor {
type Status: CompressStatusKind;
type Flush: CompressFlushKind;
const CHUNKSIZE: usize;
const DEF_BUF_SIZE: usize;
fn compress_vec(
&mut self,
input: &[u8],
output: &mut Vec<u8>,
flush: Self::Flush,
vm: &VirtualMachine,
) -> PyResult<Self::Status>;
fn total_in(&mut self) -> usize;
fn new_error(message: impl Into<String>, vm: &VirtualMachine) -> PyBaseExceptionRef;
}
pub trait CompressFlushKind: Copy {
const NONE: Self;
const FINISH: Self;
fn to_usize(self) -> usize;
}
pub trait CompressStatusKind: Copy {
const EOF: Self;
fn to_usize(self) -> usize;
}
#[derive(Debug)]
pub struct CompressState<C: Compressor> {
compressor: Option<C>,
}
impl<C: Compressor> CompressState<C> {
pub const fn new(compressor: C) -> Self {
Self {
compressor: Some(compressor),
}
}
fn get_compressor(&mut self, vm: &VirtualMachine) -> PyResult<&mut C> {
self.compressor
.as_mut()
.ok_or_else(|| C::new_error(USE_AFTER_FINISH_ERR, vm))
}
pub fn compress(&mut self, data: &[u8], vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let mut buf = Vec::new();
let compressor = self.get_compressor(vm)?;
for mut chunk in data.chunks(C::CHUNKSIZE) {
while !chunk.is_empty() {
buf.reserve(C::DEF_BUF_SIZE);
let prev_in = compressor.total_in();
compressor.compress_vec(chunk, &mut buf, C::Flush::NONE, vm)?;
let consumed = compressor.total_in() - prev_in;
chunk = &chunk[consumed..];
}
}
buf.shrink_to_fit();
Ok(buf)
}
pub fn flush(&mut self, mode: C::Flush, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let mut buf = Vec::new();
let compressor = self.get_compressor(vm)?;
let status = loop {
if buf.len() == buf.capacity() {
buf.reserve(C::DEF_BUF_SIZE);
}
let status = compressor.compress_vec(&[], &mut buf, mode, vm)?;
if buf.len() != buf.capacity() {
break status;
}
};
if status.to_usize() == C::Status::EOF.to_usize() {
if mode.to_usize() == C::Flush::FINISH.to_usize() {
self.compressor = None;
} else {
return Err(C::new_error("unexpected eof", vm));
}
}
buf.shrink_to_fit();
Ok(buf)
}
}
#[derive(Debug)]
pub struct DecompressState<D> {
decompress: D,
unused_data: PyBytesRef,
input_buffer: Vec<u8>,
eof: bool,
needs_input: bool,
}
impl<D: Decompressor> DecompressState<D> {
pub fn new(decompress: D, vm: &VirtualMachine) -> Self {
Self {
decompress,
unused_data: vm.ctx.empty_bytes.clone(),
input_buffer: Vec::new(),
eof: false,
needs_input: true,
}
}
pub const fn eof(&self) -> bool {
self.eof
}
pub fn unused_data(&self) -> PyBytesRef {
self.unused_data.clone()
}
pub const fn needs_input(&self) -> bool {
self.needs_input
}
pub fn decompress(
&mut self,
data: &[u8],
max_length: Option<usize>,
bufsize: usize,
vm: &VirtualMachine,
) -> Result<Vec<u8>, DecompressError<D::Error>> {
if self.eof {
return Err(DecompressError::Eof(EofError));
}
let input_buffer = &mut self.input_buffer;
let d = &mut self.decompress;
let mut chunks = Chunker::chain(input_buffer, data);
let prev_len = chunks.len();
let (ret, stream_end) =
match _decompress_chunks(&mut chunks, d, bufsize, max_length, flush_sync) {
Ok((buf, stream_end)) => (Ok(buf), stream_end),
Err(err) => (Err(err), false),
};
let consumed = prev_len - chunks.len();
self.eof |= stream_end;
if self.eof {
self.needs_input = false;
if !chunks.is_empty() {
self.unused_data = vm.ctx.new_bytes(chunks.to_vec());
}
} else if chunks.is_empty() {
input_buffer.clear();
self.needs_input = true;
} else {
self.needs_input = false;
if let Some(n_consumed_from_data) = consumed.checked_sub(input_buffer.len()) {
input_buffer.clear();
input_buffer.extend_from_slice(&data[n_consumed_from_data..]);
} else {
input_buffer.drain(..consumed);
input_buffer.extend_from_slice(data);
}
}
ret.map_err(DecompressError::Decompress)
}
}
pub enum DecompressError<E> {
Decompress(E),
Eof(EofError),
}
impl<E> From<E> for DecompressError<E> {
fn from(err: E) -> Self {
Self::Decompress(err)
}
}
pub struct EofError;
impl ToPyException for EofError {
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
vm.new_eof_error("End of stream already reached")
}
}