Debian Codegraph @

contrib/python-zstandard/rust-ext/src/decompressionobj.rs

raw
1 // Copyright (c) 2021-present, Gregory Szorc
2 // All rights reserved.
3 //
4 // This software may be modified and distributed under the terms
5 // of the BSD license. See the LICENSE file for details.
6
7 use {
8 crate::{exceptions::ZstdError, zstd_safe::DCtx},
9 pyo3::{
10 buffer::PyBuffer,
11 prelude::*,
12 types::{PyBytes, PyList},
13 },
14 std::sync::Arc,
15 };
16
17 #[pyclass(module = "zstandard.backend_rust")]
18 pub struct ZstdDecompressionObj {
19 dctx: Arc<DCtx<'static>>,
20 write_size: usize,
21 read_across_frames: bool,
22 finished: bool,
23 unused_data: Vec<u8>,
24 }
25
26 impl ZstdDecompressionObj {
27 pub fn new(
28 dctx: Arc<DCtx<'static>>,
29 write_size: usize,
30 read_across_frames: bool,
31 ) -> PyResult<Self> {
32 Ok(ZstdDecompressionObj {
33 dctx,
34 write_size,
35 read_across_frames,
36 finished: false,
37 unused_data: vec![],
38 })
39 }
40 }
41
42 #[pymethods]
43 impl ZstdDecompressionObj {
44 fn decompress<'p>(&mut self, py: Python<'p>, data: PyBuffer<u8>) -> PyResult<Bound<'p, PyAny>> {
45 if self.finished {
46 return Err(ZstdError::new_err(
47 "cannot use a decompressobj multiple times",
48 ));
49 }
50
51 if data.len_bytes() == 0 {
52 return Ok(PyBytes::new_bound(py, &[]).into_any());
53 }
54
55 let mut in_buffer = zstd_sys::ZSTD_inBuffer {
56 src: data.buf_ptr(),
57 size: data.len_bytes(),
58 pos: 0,
59 };
60
61 let mut dest_buffer: Vec<u8> = Vec::with_capacity(self.write_size);
62
63 let chunks = PyList::empty_bound(py);
64
65 loop {
66 let zresult = self
67 .dctx
68 .decompress_into_vec(&mut dest_buffer, &mut in_buffer)
69 .map_err(|msg| ZstdError::new_err(format!("zstd decompress error: {}", msg)))?;
70
71 if !dest_buffer.is_empty() {
72 // TODO avoid buffer copy.
73 let chunk = PyBytes::new_bound(py, &dest_buffer);
74 chunks.append(chunk)?;
75 }
76
77 if zresult == 0 && !self.read_across_frames {
78 self.finished = true;
79 // TODO clear out decompressor?
80
81 if let Some(data) = data.as_slice(py) {
82 let unused = &data[in_buffer.pos..in_buffer.size];
83 self.unused_data = unused.iter().map(|x| x.get()).collect::<Vec<_>>();
84 }
85
86 break;
87 } else if zresult == 0 && self.read_across_frames {
88 if in_buffer.pos == in_buffer.size {
89 break;
90 } else {
91 dest_buffer.clear();
92 }
93 } else if in_buffer.pos == in_buffer.size && dest_buffer.len() < dest_buffer.capacity()
94 {
95 break;
96 } else {
97 dest_buffer.clear();
98 }
99 }
100
101 let empty = PyBytes::new_bound(py, &[]);
102 empty.call_method1("join", (chunks,))
103 }
104
105 #[allow(unused_variables)]
106 fn flush<'p>(&self, py: Python<'p>, length: Option<usize>) -> PyResult<Bound<'p, PyBytes>> {
107 Ok(PyBytes::new_bound(py, &[]))
108 }
109
110 #[getter]
111 fn unused_data<'p>(&self, py: Python<'p>) -> Bound<'p, PyBytes> {
112 PyBytes::new_bound(py, &self.unused_data)
113 }
114
115 #[getter]
116 fn unconsumed_tail<'p>(&self, py: Python<'p>) -> Bound<'p, PyBytes> {
117 PyBytes::new_bound(py, &[])
118 }
119
120 #[getter]
121 fn eof(&self) -> bool {
122 self.finished
123 }
124 }