Skip to content

Commit 0d9598f

Browse files
committed
Process review
1 parent bef01b5 commit 0d9598f

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

dissect/util/_native.src/src/compression/lz4.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ use pyo3::types::{PyByteArray, PyBytes};
44

55
const MAX_DISCOVER_OUTPUT_SIZE: usize = 1024 * 1024 * 1024;
66

7+
fn decompress_to_unknown_size(src: &[u8]) -> Result<Vec<u8>, PyErr> {
8+
let mut output_size = lz4_flex::block::get_maximum_output_size(src.len());
9+
loop {
10+
// If the output size is too large, we should not attempt to decompress further
11+
if output_size > MAX_DISCOVER_OUTPUT_SIZE {
12+
return Err(PyErr::new::<PyValueError, _>(
13+
"output size is too large".to_string(),
14+
));
15+
}
16+
17+
match lz4_flex::block::decompress(&src, output_size) {
18+
Ok(result) => {
19+
break Ok(result);
20+
}
21+
Err(lz4_flex::block::DecompressError::OutputTooSmall {
22+
expected,
23+
actual: _,
24+
}) => {
25+
output_size = expected;
26+
}
27+
Err(e) => {
28+
return Err(PyErr::new::<PyValueError, _>(e.to_string()));
29+
}
30+
}
31+
}
32+
}
33+
734
/// LZ4 decompress bytes up to a certain length. Assumes no header.
835
///
936
/// Unlike the Python implementation, this function does not support streaming decompression
@@ -15,7 +42,7 @@ const MAX_DISCOVER_OUTPUT_SIZE: usize = 1024 * 1024 * 1024;
1542
/// return_bytearray: Whether to return ``bytearray`` or ``bytes``.
1643
///
1744
/// Returns:
18-
/// The decompressed data or a tuple of the decompressed data and the amount of bytes read.
45+
/// The decompressed data.
1946
///
2047
#[pyfunction]
2148
#[pyo3(signature = (src, uncompressed_size=-1, return_bytearray=false))]
@@ -27,30 +54,7 @@ fn decompress(
2754
) -> PyResult<PyObject> {
2855
let result = if uncompressed_size < 0 {
2956
// If the uncompressed size is not provided, we need to discover it first
30-
let mut output_size = lz4_flex::block::get_maximum_output_size(src.len());
31-
loop {
32-
// If the output size is too large, we should not attempt to decompress further
33-
if output_size > MAX_DISCOVER_OUTPUT_SIZE {
34-
return Err(PyErr::new::<PyValueError, _>(
35-
"output size is too large".to_string(),
36-
));
37-
}
38-
39-
match lz4_flex::block::decompress(&src, output_size) {
40-
Ok(result) => {
41-
break result;
42-
}
43-
Err(lz4_flex::block::DecompressError::OutputTooSmall {
44-
expected,
45-
actual: _,
46-
}) => {
47-
output_size = expected;
48-
}
49-
Err(e) => {
50-
return Err(PyErr::new::<PyValueError, _>(e.to_string()));
51-
}
52-
}
53-
}
57+
decompress_to_unknown_size(&src)?
5458
} else {
5559
lz4_flex::block::decompress(&src, uncompressed_size as usize)
5660
.map_err(|e| PyErr::new::<PyValueError, _>(e.to_string()))?

dissect/util/_native.src/src/compression/lzo.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,20 @@ fn decompress(
2424
header: bool,
2525
buflen: isize,
2626
) -> PyResult<Bound<'_, PyBytes>> {
27-
let (src, out_len) = if header {
27+
let (body, out_len) = if header {
28+
// Compatibility with python-lzo, which can include a header
29+
// https://github.com/jd-boyd/python-lzo/blob/80ca60416c6657d373c5308a1eb511903a3ff9b1/lzomodule.c#L238-L269
2830
if src.len() < 8 || src[0] < 0xf0 || src[0] > 0xf1 {
29-
return Err(PyErr::new::<PyValueError, _>(
30-
"Invalid header value".to_string(),
31-
));
31+
return Err(PyValueError::new_err("Invalid header value"));
3232
}
3333
let len = u32::from_le_bytes([src[1], src[2], src[3], src[4]]) as usize;
34-
(src[5..].to_vec(), len)
35-
} else if buflen < 0 {
36-
return Err(PyErr::new::<PyValueError, _>(
37-
"Buffer length must be provided".to_string(),
38-
));
34+
(src[5..].to_vec(), Some(len))
3935
} else {
40-
(src, buflen as usize)
36+
(src, (buflen >= 0).then_some(buflen as usize))
4137
};
4238

43-
let mut cursor = std::io::Cursor::new(src);
44-
lzokay_native::decompress(&mut cursor, Some(out_len))
39+
let mut cursor = std::io::Cursor::new(body);
40+
lzokay_native::decompress(&mut cursor, out_len)
4541
.map_err(|e| PyErr::new::<PyValueError, _>(e.to_string()))
4642
.map(|result| PyBytes::new(py, &result))
4743
}

dissect/util/compression/lz4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def decompress(
3737
return_bytearray: Whether to return ``bytearray`` or ``bytes``.
3838
3939
Returns:
40-
The decompressed data or a tuple of the decompressed data and the amount of bytes read.
40+
The decompressed data.
4141
"""
4242
if not hasattr(src, "read"):
4343
src = io.BytesIO(src)

0 commit comments

Comments
 (0)