| 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::{ |
| 9 |
exceptions::ZstdError, |
| 10 |
stream::{make_in_buffer_source, InBufferSource}, |
| 11 |
zstd_safe::DCtx, |
| 12 |
}, |
| 13 |
pyo3::{exceptions::PyValueError, prelude::*, types::PyBytes, IntoPyObjectExt}, |
| 14 |
std::{cmp::min, sync::Arc}, |
| 15 |
}; |
| 16 |
|
| 17 |
#[pyclass(module = "zstandard.backend_rust")] |
| 18 |
pub struct ZstdDecompressorIterator { |
| 19 |
dctx: Arc<DCtx<'static>>, |
| 20 |
source: Box<dyn InBufferSource + Send>, |
| 21 |
write_size: usize, |
| 22 |
finished_output: bool, |
| 23 |
} |
| 24 |
|
| 25 |
unsafe impl Sync for ZstdDecompressorIterator {} |
| 26 |
|
| 27 |
#[pymethods] |
| 28 |
impl ZstdDecompressorIterator { |
| 29 |
// PyIterProtocol. |
| 30 |
|
| 31 |
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> { |
| 32 |
slf |
| 33 |
} |
| 34 |
|
| 35 |
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyObject>> { |
| 36 |
if slf.finished_output { |
| 37 |
return Ok(None); |
| 38 |
} |
| 39 |
|
| 40 |
let py = unsafe { Python::assume_gil_acquired() }; |
| 41 |
|
| 42 |
let mut dest_buffer: Vec<u8> = Vec::with_capacity(slf.write_size); |
| 43 |
|
| 44 |
// While input is available. |
| 45 |
while let Some(mut in_buffer) = slf.source.input_buffer(py)? { |
| 46 |
let old_pos = in_buffer.pos; |
| 47 |
|
| 48 |
let zresult = slf |
| 49 |
.dctx |
| 50 |
.decompress_into_vec(&mut dest_buffer, &mut in_buffer) |
| 51 |
.map_err(|msg| ZstdError::new_err(format!("zstd decompress error: {}", msg)))?; |
| 52 |
|
| 53 |
slf.source.record_bytes_read(in_buffer.pos - old_pos); |
| 54 |
|
| 55 |
if zresult == 0 { |
| 56 |
slf.finished_output = true; |
| 57 |
} |
| 58 |
|
| 59 |
// Emit chunk if output buffer has data. |
| 60 |
if !dest_buffer.is_empty() { |
| 61 |
// TODO avoid buffer copy. |
| 62 |
let chunk = PyBytes::new(py, &dest_buffer); |
| 63 |
return Ok(Some(chunk.into_py_any(py)?)); |
| 64 |
} |
| 65 |
|
| 66 |
// Repeat loop to collect more input data. |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
// Input is exhausted. Emit what we have or finish. |
| 71 |
if !dest_buffer.is_empty() { |
| 72 |
// TODO avoid buffer copy. |
| 73 |
let chunk = PyBytes::new(py, &dest_buffer); |
| 74 |
Ok(Some(chunk.into_py_any(py)?)) |
| 75 |
} else { |
| 76 |
Ok(None) |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl ZstdDecompressorIterator { |
| 82 |
pub fn new( |
| 83 |
py: Python, |
| 84 |
dctx: Arc<DCtx<'static>>, |
| 85 |
reader: &Bound<'_, PyAny>, |
| 86 |
read_size: usize, |
| 87 |
write_size: usize, |
| 88 |
skip_bytes: usize, |
| 89 |
) -> PyResult<Self> { |
| 90 |
let mut source = make_in_buffer_source(py, reader, read_size)?; |
| 91 |
|
| 92 |
let mut skip_bytes = skip_bytes; |
| 93 |
while skip_bytes > 0 { |
| 94 |
let in_buffer = source |
| 95 |
.input_buffer(py)? |
| 96 |
.ok_or_else(|| PyValueError::new_err("skip_bytes larger than first input chunk"))?; |
| 97 |
|
| 98 |
let read = min(skip_bytes, in_buffer.size - in_buffer.pos); |
| 99 |
source.record_bytes_read(read); |
| 100 |
skip_bytes -= read; |
| 101 |
} |
| 102 |
|
| 103 |
Ok(Self { |
| 104 |
dctx, |
| 105 |
source, |
| 106 |
write_size, |
| 107 |
finished_output: false, |
| 108 |
}) |
| 109 |
} |
| 110 |
} |