| 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::CCtx, |
| 12 |
}, |
| 13 |
pyo3::{prelude::*, types::PyBytes, IntoPyObjectExt}, |
| 14 |
std::sync::Arc, |
| 15 |
}; |
| 16 |
|
| 17 |
#[pyclass(module = "zstandard.backend_rust")] |
| 18 |
pub struct ZstdCompressorIterator { |
| 19 |
cctx: Arc<CCtx<'static>>, |
| 20 |
source: Box<dyn InBufferSource + Send>, |
| 21 |
write_size: usize, |
| 22 |
finished_output: bool, |
| 23 |
} |
| 24 |
|
| 25 |
unsafe impl Sync for ZstdCompressorIterator {} |
| 26 |
|
| 27 |
#[pymethods] |
| 28 |
impl ZstdCompressorIterator { |
| 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 |
// Feed data into the compressor until there is output data. |
| 45 |
while let Some(mut in_buffer) = slf.source.input_buffer(py)? { |
| 46 |
let old_pos = in_buffer.pos; |
| 47 |
|
| 48 |
slf.cctx |
| 49 |
.compress_into_vec( |
| 50 |
&mut dest_buffer, |
| 51 |
&mut in_buffer, |
| 52 |
zstd_sys::ZSTD_EndDirective::ZSTD_e_continue, |
| 53 |
) |
| 54 |
.map_err(|msg| ZstdError::new_err(format!("zstd compress error: {}", msg)))?; |
| 55 |
|
| 56 |
slf.source.record_bytes_read(in_buffer.pos - old_pos); |
| 57 |
|
| 58 |
// Emit compressed data, if available. |
| 59 |
if !dest_buffer.is_empty() { |
| 60 |
// TODO avoid buffer copy |
| 61 |
let chunk = PyBytes::new(py, &dest_buffer); |
| 62 |
|
| 63 |
return Ok(Some(chunk.into_py_any(py)?)); |
| 64 |
} |
| 65 |
|
| 66 |
// Else read another chunk in hopes of producing output data. |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
// Input data is exhausted. End the stream and emit what remains. |
| 71 |
|
| 72 |
let mut in_buffer = zstd_sys::ZSTD_inBuffer { |
| 73 |
src: std::ptr::null_mut(), |
| 74 |
size: 0, |
| 75 |
pos: 0, |
| 76 |
}; |
| 77 |
|
| 78 |
let zresult = slf |
| 79 |
.cctx |
| 80 |
.compress_into_vec( |
| 81 |
&mut dest_buffer, |
| 82 |
&mut in_buffer, |
| 83 |
zstd_sys::ZSTD_EndDirective::ZSTD_e_end, |
| 84 |
) |
| 85 |
.map_err(|msg| { |
| 86 |
ZstdError::new_err(format!("error ending compression stream: {}", msg)) |
| 87 |
})?; |
| 88 |
|
| 89 |
if zresult == 0 { |
| 90 |
slf.finished_output = true; |
| 91 |
} |
| 92 |
|
| 93 |
if !dest_buffer.is_empty() { |
| 94 |
// TODO avoid buffer copy. |
| 95 |
let chunk = PyBytes::new(py, &dest_buffer); |
| 96 |
|
| 97 |
return Ok(Some(chunk.into_py_any(py)?)); |
| 98 |
} |
| 99 |
|
| 100 |
Ok(None) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl ZstdCompressorIterator { |
| 105 |
pub fn new( |
| 106 |
py: Python, |
| 107 |
cctx: Arc<CCtx<'static>>, |
| 108 |
reader: &Bound<'_, PyAny>, |
| 109 |
size: u64, |
| 110 |
read_size: usize, |
| 111 |
write_size: usize, |
| 112 |
) -> PyResult<Self> { |
| 113 |
let source = make_in_buffer_source(py, reader, read_size)?; |
| 114 |
|
| 115 |
let size = match source.source_size() { |
| 116 |
Some(size) => size as _, |
| 117 |
None => size, |
| 118 |
}; |
| 119 |
|
| 120 |
cctx.set_pledged_source_size(size) |
| 121 |
.map_err(|msg| ZstdError::new_err(format!("error setting source size: {}", msg)))?; |
| 122 |
|
| 123 |
Ok(Self { |
| 124 |
cctx, |
| 125 |
source, |
| 126 |
write_size, |
| 127 |
finished_output: false, |
| 128 |
}) |
| 129 |
} |
| 130 |
} |