@@ -4,6 +4,33 @@ use pyo3::types::{PyByteArray, PyBytes};
44
55const 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 ( ) ) ) ?
0 commit comments