1- use std:: fmt :: Display ;
1+ use std:: ffi :: c_int ;
22
3+ /// A type to hold all state needed for decompressing a ZLIB encoded stream.
34pub struct Decompress ( Box < libz_rs_sys:: z_stream > ) ;
45
56unsafe impl Sync for Decompress { }
67unsafe impl Send for Decompress { }
78
9+ impl Default for Decompress {
10+ fn default ( ) -> Self {
11+ Self :: new ( )
12+ }
13+ }
14+
815impl Decompress {
16+ /// The amount of bytes consumed from the input so far.
917 pub fn total_in ( & self ) -> u64 {
10- self . 0 . total_in
18+ self . 0 . total_in as _
1119 }
1220
21+ /// The amount of decompressed bytes that have been written to the output thus far.
1322 pub fn total_out ( & self ) -> u64 {
14- self . 0 . total_out
23+ self . 0 . total_out as _
1524 }
1625
17- pub fn new ( _zlib_header : bool ) -> Self {
26+ /// Create a new instance. Note that it allocates in various ways and thus should be re-used.
27+ pub fn new ( ) -> Self {
1828 let mut this = Box :: new ( libz_rs_sys:: z_stream:: default ( ) ) ;
1929
2030 unsafe {
@@ -28,10 +38,12 @@ impl Decompress {
2838 Self ( this)
2939 }
3040
31- pub fn reset ( & mut self , _: bool ) {
41+ /// Reset the state to allow handling a new stream.
42+ pub fn reset ( & mut self ) {
3243 unsafe { libz_rs_sys:: inflateReset ( & mut * self . 0 ) } ;
3344 }
3445
46+ /// Decompress `input` and write all decompressed bytes into `output`, with `flush` defining some details about this.
3547 pub fn decompress (
3648 & mut self ,
3749 input : & [ u8 ] ,
@@ -49,11 +61,10 @@ impl Decompress {
4961 libz_rs_sys:: Z_BUF_ERROR => Ok ( Status :: BufError ) ,
5062 libz_rs_sys:: Z_STREAM_END => Ok ( Status :: StreamEnd ) ,
5163
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) ,
64+ libz_rs_sys:: Z_STREAM_ERROR => Err ( DecompressError :: StreamError ) ,
65+ libz_rs_sys:: Z_DATA_ERROR => Err ( DecompressError :: DataError ) ,
66+ libz_rs_sys:: Z_MEM_ERROR => Err ( DecompressError :: InsufficientMemory ) ,
67+ err => Err ( DecompressError :: Unknown { err } ) ,
5768 }
5869 }
5970}
@@ -64,19 +75,29 @@ impl Drop for Decompress {
6475 }
6576}
6677
78+ /// The error produced by [`Decompress::decompress()`].
6779#[ 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- }
80+ #[ allow( missing_docs) ]
81+ pub enum DecompressError {
82+ #[ error( "stream error" ) ]
83+ StreamError ,
84+ #[ error( "Not enough memory" ) ]
85+ InsufficientMemory ,
86+ #[ error( "Invalid input data" ) ]
87+ DataError ,
88+ #[ error( "An unknown error occurred: {err}" ) ]
89+ Unknown { err : c_int } ,
7490}
7591
92+ /// The status returned by [`Decompress::decompress()`].
7693#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
7794pub enum Status {
95+ /// The decompress operation went well. Not to be confused with `StreamEnd`, so one can continue
96+ /// the decompression.
7897 Ok ,
98+ /// An error occurred when decompression.
7999 BufError ,
100+ /// The stream was fully decompressed.
80101 StreamEnd ,
81102}
82103
@@ -123,19 +144,12 @@ pub mod inflate {
123144}
124145
125146/// Decompress a few bytes of a zlib stream without allocation
147+ #[ derive( Default ) ]
126148pub struct Inflate {
127149 /// The actual decompressor doing all the work.
128150 pub state : Decompress ,
129151}
130152
131- impl Default for Inflate {
132- fn default ( ) -> Self {
133- Inflate {
134- state : Decompress :: new ( true ) ,
135- }
136- }
137- }
138-
139153impl Inflate {
140154 /// Run the decompressor exactly once. Cannot be run multiple times
141155 pub fn once ( & mut self , input : & [ u8 ] , out : & mut [ u8 ] ) -> Result < ( Status , usize , usize ) , inflate:: Error > {
@@ -151,7 +165,7 @@ impl Inflate {
151165
152166 /// Ready this instance for decoding another data stream.
153167 pub fn reset ( & mut self ) {
154- self . state . reset ( true ) ;
168+ self . state . reset ( ) ;
155169 }
156170}
157171
0 commit comments