| 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 |
pyo3::{buffer::PyBuffer, exceptions::PyValueError, prelude::*, IntoPyObjectExt}, |
| 9 |
zstd_sys::ZSTD_inBuffer, |
| 10 |
}; |
| 11 |
|
| 12 |
/// Describes a type that can be resolved to a `zstd_sys::ZSTD_inBuffer`. |
| 13 |
pub trait InBufferSource { |
| 14 |
/// Obtain the PyObject this instance is reading from. |
| 15 |
fn source_object(&self) -> &PyObject; |
| 16 |
|
| 17 |
/// The size of the input object, if available. |
| 18 |
fn source_size(&self) -> Option<usize>; |
| 19 |
|
| 20 |
/// Obtain a `zstd_sys::ZSTD_inBuffer` with input to feed to a compressor. |
| 21 |
fn input_buffer(&mut self, py: Python) -> PyResult<Option<ZSTD_inBuffer>>; |
| 22 |
|
| 23 |
/// Record that `count` bytes were read from the input buffer. |
| 24 |
fn record_bytes_read(&mut self, count: usize); |
| 25 |
|
| 26 |
/// Whether source data has been fully consumed. |
| 27 |
fn finished(&self) -> bool; |
| 28 |
} |
| 29 |
|
| 30 |
/// A data source where data is obtaine by calling `read()`. |
| 31 |
struct ReadSource { |
| 32 |
source: PyObject, |
| 33 |
buffer: Option<PyBuffer<u8>>, |
| 34 |
read_size: usize, |
| 35 |
finished: bool, |
| 36 |
offset: usize, |
| 37 |
} |
| 38 |
|
| 39 |
impl InBufferSource for ReadSource { |
| 40 |
fn source_object(&self) -> &PyObject { |
| 41 |
&self.source |
| 42 |
} |
| 43 |
|
| 44 |
fn source_size(&self) -> Option<usize> { |
| 45 |
None |
| 46 |
} |
| 47 |
|
| 48 |
fn input_buffer(&mut self, py: Python) -> PyResult<Option<ZSTD_inBuffer>> { |
| 49 |
if self.finished() { |
| 50 |
Ok(None) |
| 51 |
// If we have a buffer, return remaining data in it. |
| 52 |
} else if let Some(buffer) = &self.buffer { |
| 53 |
Ok(Some(ZSTD_inBuffer { |
| 54 |
src: buffer.buf_ptr(), |
| 55 |
size: buffer.len_bytes(), |
| 56 |
pos: self.offset, |
| 57 |
})) |
| 58 |
// Attempt to read new data. |
| 59 |
} else { |
| 60 |
let data = self.source.call_method1(py, "read", (self.read_size,))?; |
| 61 |
let buffer = PyBuffer::get(data.bind(py))?; |
| 62 |
|
| 63 |
if buffer.len_bytes() == 0 { |
| 64 |
self.finished = true; |
| 65 |
Ok(None) |
| 66 |
} else { |
| 67 |
self.buffer = Some(buffer); |
| 68 |
self.offset = 0; |
| 69 |
|
| 70 |
Ok(Some(ZSTD_inBuffer { |
| 71 |
src: self.buffer.as_ref().unwrap().buf_ptr(), |
| 72 |
size: self.buffer.as_ref().unwrap().len_bytes(), |
| 73 |
pos: self.offset, |
| 74 |
})) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
fn record_bytes_read(&mut self, count: usize) { |
| 80 |
let buffer = self.buffer.as_ref().expect("buffer should be present"); |
| 81 |
|
| 82 |
self.offset += count; |
| 83 |
|
| 84 |
// If we've exhausted the input buffer, drop it. On next call |
| 85 |
// to input_buffer() we'll try to read() more data and finish |
| 86 |
// the stream if nothing can be read. |
| 87 |
if self.offset >= buffer.len_bytes() { |
| 88 |
self.buffer = None; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
fn finished(&self) -> bool { |
| 93 |
self.finished |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// A data source where data is obtained from a `PyObject` |
| 98 |
/// conforming to the buffer protocol. |
| 99 |
struct BufferSource { |
| 100 |
source: PyObject, |
| 101 |
buffer: PyBuffer<u8>, |
| 102 |
offset: usize, |
| 103 |
} |
| 104 |
|
| 105 |
impl InBufferSource for BufferSource { |
| 106 |
fn source_object(&self) -> &PyObject { |
| 107 |
&self.source |
| 108 |
} |
| 109 |
|
| 110 |
fn source_size(&self) -> Option<usize> { |
| 111 |
Some(self.buffer.len_bytes()) |
| 112 |
} |
| 113 |
|
| 114 |
fn input_buffer(&mut self, _py: Python) -> PyResult<Option<ZSTD_inBuffer>> { |
| 115 |
if self.finished() { |
| 116 |
Ok(None) |
| 117 |
} else { |
| 118 |
Ok(Some(ZSTD_inBuffer { |
| 119 |
src: self.buffer.buf_ptr(), |
| 120 |
size: self.buffer.len_bytes(), |
| 121 |
pos: self.offset, |
| 122 |
})) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
fn record_bytes_read(&mut self, count: usize) { |
| 127 |
self.offset += count; |
| 128 |
} |
| 129 |
|
| 130 |
fn finished(&self) -> bool { |
| 131 |
self.offset >= self.buffer.len_bytes() |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
pub(crate) fn make_in_buffer_source( |
| 136 |
py: Python, |
| 137 |
source: &Bound<'_, PyAny>, |
| 138 |
read_size: usize, |
| 139 |
) -> PyResult<Box<dyn InBufferSource + Send>> { |
| 140 |
if source.hasattr("read")? { |
| 141 |
Ok(Box::new(ReadSource { |
| 142 |
source: source.into_py_any(py)?, |
| 143 |
buffer: None, |
| 144 |
read_size, |
| 145 |
finished: false, |
| 146 |
offset: 0, |
| 147 |
})) |
| 148 |
} else { |
| 149 |
let buffer = PyBuffer::get(&source.as_borrowed()).map_err(|_| { |
| 150 |
PyValueError::new_err( |
| 151 |
"must pass an object with a read() method or conforms to buffer protocol", |
| 152 |
) |
| 153 |
})?; |
| 154 |
|
| 155 |
Ok(Box::new(BufferSource { |
| 156 |
source: source.into_py_any(py)?, |
| 157 |
buffer, |
| 158 |
offset: 0, |
| 159 |
})) |
| 160 |
} |
| 161 |
} |