1- pub use flate2:: { Decompress , Status } ;
1+ use std:: fmt:: Display ;
2+
3+ pub struct Decompress ( Box < libz_rs_sys:: z_stream > ) ;
4+
5+ unsafe impl Sync for Decompress { }
6+ unsafe impl Send for Decompress { }
7+
8+ impl Decompress {
9+ pub fn total_in ( & self ) -> u64 {
10+ self . 0 . total_in
11+ }
12+
13+ pub fn total_out ( & self ) -> u64 {
14+ self . 0 . total_out
15+ }
16+
17+ pub fn new ( _zlib_header : bool ) -> Self {
18+ let mut this = Box :: new ( libz_rs_sys:: z_stream:: default ( ) ) ;
19+
20+ unsafe {
21+ libz_rs_sys:: inflateInit_ (
22+ & mut * this,
23+ libz_rs_sys:: zlibVersion ( ) ,
24+ core:: mem:: size_of :: < libz_rs_sys:: z_stream > ( ) as core:: ffi:: c_int ,
25+ ) ;
26+ }
27+
28+ Self ( this)
29+ }
30+
31+ pub fn reset ( & mut self , _: bool ) {
32+ unsafe { libz_rs_sys:: inflateReset ( & mut * self . 0 ) } ;
33+ }
34+
35+ pub fn decompress (
36+ & mut self ,
37+ input : & [ u8 ] ,
38+ output : & mut [ u8 ] ,
39+ flush : FlushDecompress ,
40+ ) -> Result < Status , DecompressError > {
41+ self . 0 . avail_in = input. len ( ) as _ ;
42+ self . 0 . avail_out = output. len ( ) as _ ;
43+
44+ self . 0 . next_in = input. as_ptr ( ) ;
45+ self . 0 . next_out = output. as_mut_ptr ( ) ;
46+
47+ match unsafe { libz_rs_sys:: inflate ( & mut * self . 0 , flush as _ ) } {
48+ libz_rs_sys:: Z_OK => Ok ( Status :: Ok ) ,
49+ libz_rs_sys:: Z_BUF_ERROR => Ok ( Status :: BufError ) ,
50+ libz_rs_sys:: Z_STREAM_END => Ok ( Status :: StreamEnd ) ,
51+
52+ libz_rs_sys:: Z_STREAM_ERROR => Err ( DecompressError ( "stream error" ) ) ,
53+ libz_rs_sys:: Z_DATA_ERROR => Err ( DecompressError ( "data error" ) ) ,
54+ libz_rs_sys:: Z_MEM_ERROR => Err ( DecompressError ( "insufficient memory" ) ) ,
55+ libz_rs_sys:: Z_NEED_DICT => Err ( DecompressError ( "need dictionary" ) ) ,
56+ c => panic ! ( "unknown return code: {}" , c) ,
57+ }
58+ }
59+ }
60+
61+ impl Drop for Decompress {
62+ fn drop ( & mut self ) {
63+ unsafe { libz_rs_sys:: inflateEnd ( & mut * self . 0 ) } ;
64+ }
65+ }
66+
67+ #[ derive( Debug , thiserror:: Error ) ]
68+ pub struct DecompressError ( & ' static str ) ;
69+
70+ impl Display for DecompressError {
71+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
72+ f. write_str ( self . 0 )
73+ }
74+ }
75+
76+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
77+ pub enum Status {
78+ Ok ,
79+ BufError ,
80+ StreamEnd ,
81+ }
82+
83+ /// Values which indicate the form of flushing to be used when
84+ /// decompressing in-memory data.
85+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
86+ #[ non_exhaustive]
87+ #[ allow( clippy:: unnecessary_cast) ]
88+ pub enum FlushDecompress {
89+ /// A typical parameter for passing to compression/decompression functions,
90+ /// this indicates that the underlying stream to decide how much data to
91+ /// accumulate before producing output in order to maximize compression.
92+ None = libz_rs_sys:: Z_NO_FLUSH as isize ,
93+
94+ /// All pending output is flushed to the output buffer and the output is
95+ /// aligned on a byte boundary so that the decompressor can get all input
96+ /// data available so far.
97+ ///
98+ /// Flushing may degrade compression for some compression algorithms and so
99+ /// it should only be used when necessary. This will complete the current
100+ /// deflate block and follow it with an empty stored block.
101+ Sync = libz_rs_sys:: Z_SYNC_FLUSH as isize ,
102+
103+ /// Pending input is processed and pending output is flushed.
104+ ///
105+ /// The return value may indicate that the stream is not yet done and more
106+ /// data has yet to be processed.
107+ Finish = libz_rs_sys:: Z_FINISH as isize ,
108+ }
2109
3110/// non-streaming interfaces for decompression
4111pub mod inflate {
@@ -8,10 +115,10 @@ pub mod inflate {
8115 pub enum Error {
9116 #[ error( "Could not write all bytes when decompressing content" ) ]
10117 WriteInflated ( #[ from] std:: io:: Error ) ,
11- #[ error( "Could not decode zip stream, status was '{0:? }'" ) ]
12- Inflate ( #[ from] flate2 :: DecompressError ) ,
118+ #[ error( "Could not decode zip stream, status was '{0}'" ) ]
119+ Inflate ( #[ from] super :: DecompressError ) ,
13120 #[ error( "The zlib status indicated an error, status was '{0:?}'" ) ]
14- Status ( flate2 :: Status ) ,
121+ Status ( super :: Status ) ,
15122 }
16123}
17124
@@ -31,10 +138,10 @@ impl Default for Inflate {
31138
32139impl Inflate {
33140 /// Run the decompressor exactly once. Cannot be run multiple times
34- pub fn once ( & mut self , input : & [ u8 ] , out : & mut [ u8 ] ) -> Result < ( flate2 :: Status , usize , usize ) , inflate:: Error > {
141+ pub fn once ( & mut self , input : & [ u8 ] , out : & mut [ u8 ] ) -> Result < ( Status , usize , usize ) , inflate:: Error > {
35142 let before_in = self . state . total_in ( ) ;
36143 let before_out = self . state . total_out ( ) ;
37- let status = self . state . decompress ( input, out, flate2 :: FlushDecompress :: None ) ?;
144+ let status = self . state . decompress ( input, out, FlushDecompress :: None ) ?;
38145 Ok ( (
39146 status,
40147 ( self . state . total_in ( ) - before_in) as usize ,
0 commit comments