|
1 |
| -// spell-checker:ignore compressobj decompressobj zdict chunksize zlibmodule miniz chunker |
| 1 | +// cspell:ignore chunker |
| 2 | + |
| 3 | +//! internal shared module for compression libraries |
2 | 4 |
|
3 | 5 | use crate::vm::function::{ArgBytesLike, ArgSize, OptionalArg};
|
4 | 6 | use crate::vm::{
|
5 | 7 | PyResult, VirtualMachine,
|
6 | 8 | builtins::{PyBaseExceptionRef, PyBytesRef},
|
7 | 9 | convert::ToPyException,
|
8 | 10 | };
|
9 |
| -pub(crate) mod bz2; |
10 |
| -#[cfg(not(any(target_os = "android", target_arch = "wasm32")))] |
11 |
| -pub(crate) mod lzma; |
12 |
| -pub(crate) mod zlib; |
13 | 11 |
|
14 | 12 | pub const USE_AFTER_FINISH_ERR: &str = "Error -2: inconsistent stream state";
|
15 | 13 | // TODO: don't hardcode
|
16 | 14 | const CHUNKSIZE: usize = u32::MAX as usize;
|
17 | 15 |
|
| 16 | +#[derive(FromArgs)] |
| 17 | +pub struct DecompressArgs { |
| 18 | + #[pyarg(positional)] |
| 19 | + data: ArgBytesLike, |
| 20 | + #[pyarg(any, optional)] |
| 21 | + max_length: OptionalArg<ArgSize>, |
| 22 | +} |
| 23 | + |
| 24 | +impl DecompressArgs { |
| 25 | + pub fn data(&self) -> crate::common::borrow::BorrowedValue<'_, [u8]> { |
| 26 | + self.data.borrow_buf() |
| 27 | + } |
| 28 | + pub fn raw_max_length(&self) -> Option<isize> { |
| 29 | + self.max_length.into_option().map(|ArgSize { value }| value) |
| 30 | + } |
| 31 | + |
| 32 | + // negative is None |
| 33 | + pub fn max_length(&self) -> Option<usize> { |
| 34 | + self.max_length |
| 35 | + .into_option() |
| 36 | + .and_then(|ArgSize { value }| usize::try_from(value).ok()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub trait Decompressor { |
| 41 | + type Flush: DecompressFlushKind; |
| 42 | + type Status: DecompressStatus; |
| 43 | + type Error; |
| 44 | + |
| 45 | + fn total_in(&self) -> u64; |
| 46 | + fn decompress_vec( |
| 47 | + &mut self, |
| 48 | + input: &[u8], |
| 49 | + output: &mut Vec<u8>, |
| 50 | + flush: Self::Flush, |
| 51 | + ) -> Result<Self::Status, Self::Error>; |
| 52 | + fn maybe_set_dict(&mut self, err: Self::Error) -> Result<(), Self::Error> { |
| 53 | + Err(err) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +pub trait DecompressStatus { |
| 58 | + fn is_stream_end(&self) -> bool; |
| 59 | +} |
| 60 | + |
| 61 | +pub trait DecompressFlushKind: Copy { |
| 62 | + const SYNC: Self; |
| 63 | +} |
| 64 | + |
| 65 | +impl DecompressFlushKind for () { |
| 66 | + const SYNC: Self = (); |
| 67 | +} |
| 68 | + |
| 69 | +pub fn flush_sync<T: DecompressFlushKind>(_final_chunk: bool) -> T { |
| 70 | + T::SYNC |
| 71 | +} |
| 72 | + |
18 | 73 | #[derive(Clone)]
|
19 | 74 | pub struct Chunker<'a> {
|
20 | 75 | data1: &'a [u8],
|
@@ -57,6 +112,17 @@ impl<'a> Chunker<'a> {
|
57 | 112 | }
|
58 | 113 | }
|
59 | 114 |
|
| 115 | +pub fn _decompress<D: Decompressor>( |
| 116 | + data: &[u8], |
| 117 | + d: &mut D, |
| 118 | + bufsize: usize, |
| 119 | + max_length: Option<usize>, |
| 120 | + calc_flush: impl Fn(bool) -> D::Flush, |
| 121 | +) -> Result<(Vec<u8>, bool), D::Error> { |
| 122 | + let mut data = Chunker::new(data); |
| 123 | + _decompress_chunks(&mut data, d, bufsize, max_length, calc_flush) |
| 124 | +} |
| 125 | + |
60 | 126 | pub fn _decompress_chunks<D: Decompressor>(
|
61 | 127 | data: &mut Chunker<'_>,
|
62 | 128 | d: &mut D,
|
@@ -207,39 +273,6 @@ impl<C: Compressor> CompressState<C> {
|
207 | 273 | }
|
208 | 274 | }
|
209 | 275 |
|
210 |
| -pub trait Decompressor { |
211 |
| - type Flush: DecompressFlushKind; |
212 |
| - type Status: DecompressStatus; |
213 |
| - type Error; |
214 |
| - |
215 |
| - fn total_in(&self) -> u64; |
216 |
| - fn decompress_vec( |
217 |
| - &mut self, |
218 |
| - input: &[u8], |
219 |
| - output: &mut Vec<u8>, |
220 |
| - flush: Self::Flush, |
221 |
| - ) -> Result<Self::Status, Self::Error>; |
222 |
| - fn maybe_set_dict(&mut self, err: Self::Error) -> Result<(), Self::Error> { |
223 |
| - Err(err) |
224 |
| - } |
225 |
| -} |
226 |
| - |
227 |
| -pub trait DecompressStatus { |
228 |
| - fn is_stream_end(&self) -> bool; |
229 |
| -} |
230 |
| - |
231 |
| -pub trait DecompressFlushKind: Copy { |
232 |
| - const SYNC: Self; |
233 |
| -} |
234 |
| - |
235 |
| -impl DecompressFlushKind for () { |
236 |
| - const SYNC: Self = (); |
237 |
| -} |
238 |
| - |
239 |
| -pub fn flush_sync<T: DecompressFlushKind>(_final_chunk: bool) -> T { |
240 |
| - T::SYNC |
241 |
| -} |
242 |
| - |
243 | 276 | #[derive(Debug)]
|
244 | 277 | pub struct DecompressState<D> {
|
245 | 278 | decompress: D,
|
@@ -339,25 +372,3 @@ impl ToPyException for EofError {
|
339 | 372 | vm.new_eof_error("End of stream already reached".to_owned())
|
340 | 373 | }
|
341 | 374 | }
|
342 |
| - |
343 |
| -#[derive(FromArgs)] |
344 |
| -pub struct DecompressArgs { |
345 |
| - #[pyarg(positional)] |
346 |
| - data: ArgBytesLike, |
347 |
| - #[pyarg(any, optional)] |
348 |
| - max_length: OptionalArg<ArgSize>, |
349 |
| -} |
350 |
| - |
351 |
| -impl DecompressArgs { |
352 |
| - pub fn data(&self) -> crate::common::borrow::BorrowedValue<'_, [u8]> { |
353 |
| - self.data.borrow_buf() |
354 |
| - } |
355 |
| - pub fn raw_max_length(&self) -> Option<isize> { |
356 |
| - self.max_length.into_option().map(|ArgSize { value }| value) |
357 |
| - } |
358 |
| - pub fn max_length(&self) -> Option<usize> { |
359 |
| - self.max_length |
360 |
| - .into_option() |
361 |
| - .and_then(|ArgSize { value }| usize::try_from(value).ok()) |
362 |
| - } |
363 |
| -} |
|
0 commit comments