@@ -30,6 +30,7 @@ pub const MZ_FINISH: isize = DeflateFlush::Finish as isize;
3030pub const MZ_DEFAULT_WINDOW_BITS : core:: ffi:: c_int = 15 ;
3131
3232use super :: * ;
33+ use crate :: mem:: { compress_failed, decompress_failed} ;
3334
3435impl From < :: zlib_rs:: Status > for crate :: mem:: Status {
3536 fn from ( value : :: zlib_rs:: Status ) -> Self {
@@ -52,15 +53,18 @@ impl ErrorMessage {
5253
5354pub struct Inflate {
5455 pub ( crate ) inner : :: zlib_rs:: Inflate ,
56+ // NOTE: these counts do not count the dictionary.
57+ total_in : u64 ,
58+ total_out : u64 ,
5559}
5660
5761impl fmt:: Debug for Inflate {
5862 fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
5963 write ! (
6064 f,
6165 "zlib_rs inflate internal state. total_in: {}, total_out: {}" ,
62- self . inner . total_in( ) ,
63- self . inner . total_out( ) ,
66+ self . total_in( ) ,
67+ self . total_out( ) ,
6468 )
6569 }
6670}
@@ -79,6 +83,8 @@ impl InflateBackend for Inflate {
7983 fn make ( zlib_header : bool , window_bits : u8 ) -> Self {
8084 Inflate {
8185 inner : :: zlib_rs:: Inflate :: new ( zlib_header, window_bits) ,
86+ total_in : 0 ,
87+ total_out : 0 ,
8288 }
8389 }
8490
@@ -94,41 +100,67 @@ impl InflateBackend for Inflate {
94100 FlushDecompress :: Finish => InflateFlush :: Finish ,
95101 } ;
96102
97- match self . inner . decompress ( input, output, flush) {
103+ let total_in_start = self . inner . total_in ( ) ;
104+ let total_out_start = self . inner . total_out ( ) ;
105+
106+ let result = self . inner . decompress ( input, output, flush) ;
107+
108+ self . total_in += self . inner . total_in ( ) - total_in_start;
109+ self . total_out += self . inner . total_out ( ) - total_out_start;
110+
111+ match result {
98112 Ok ( status) => Ok ( status. into ( ) ) ,
99113 Err ( InflateError :: NeedDict { dict_id } ) => crate :: mem:: decompress_need_dict ( dict_id) ,
100- Err ( e ) => crate :: mem :: decompress_failed ( ErrorMessage ( Some ( e . as_str ( ) ) ) ) ,
114+ Err ( _ ) => self . decompress_error ( ) ,
101115 }
102116 }
103117
104118 fn reset ( & mut self , zlib_header : bool ) {
119+ self . total_in = 0 ;
120+ self . total_out = 0 ;
105121 self . inner . reset ( zlib_header) ;
106122 }
107123}
108124
109125impl Backend for Inflate {
110126 #[ inline]
111127 fn total_in ( & self ) -> u64 {
112- self . inner . total_in ( )
128+ self . total_in
113129 }
114130
115131 #[ inline]
116132 fn total_out ( & self ) -> u64 {
117- self . inner . total_out ( )
133+ self . total_out
134+ }
135+ }
136+
137+ impl Inflate {
138+ fn decompress_error < T > ( & self ) -> Result < T , DecompressError > {
139+ decompress_failed ( ErrorMessage ( self . inner . error_message ( ) ) )
140+ }
141+
142+ pub fn set_dictionary ( & mut self , dictionary : & [ u8 ] ) -> Result < u32 , DecompressError > {
143+ match self . inner . set_dictionary ( dictionary) {
144+ Ok ( v) => Ok ( v) ,
145+ Err ( _) => self . decompress_error ( ) ,
146+ }
118147 }
119148}
120149
121150pub struct Deflate {
122151 pub ( crate ) inner : :: zlib_rs:: Deflate ,
152+ // NOTE: these counts do not count the dictionary.
153+ total_in : u64 ,
154+ total_out : u64 ,
123155}
124156
125157impl fmt:: Debug for Deflate {
126158 fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
127159 write ! (
128160 f,
129161 "zlib_rs deflate internal state. total_in: {}, total_out: {}" ,
130- self . inner . total_in( ) ,
131- self . inner . total_out( ) ,
162+ self . total_in( ) ,
163+ self . total_out( ) ,
132164 )
133165 }
134166}
@@ -140,6 +172,8 @@ impl DeflateBackend for Deflate {
140172
141173 Deflate {
142174 inner : :: zlib_rs:: Deflate :: new ( level. level ( ) as i32 , zlib_header, window_bits) ,
175+ total_in : 0 ,
176+ total_out : 0 ,
143177 }
144178 }
145179
@@ -157,25 +191,64 @@ impl DeflateBackend for Deflate {
157191 FlushCompress :: Finish => DeflateFlush :: Finish ,
158192 } ;
159193
160- match self . inner . compress ( input, output, flush) {
194+ let total_in_start = self . inner . total_in ( ) ;
195+ let total_out_start = self . inner . total_out ( ) ;
196+
197+ let result = self . inner . compress ( input, output, flush) ;
198+
199+ self . total_in += self . inner . total_in ( ) - total_in_start;
200+ self . total_out += self . inner . total_out ( ) - total_out_start;
201+
202+ match result {
161203 Ok ( status) => Ok ( status. into ( ) ) ,
162- Err ( e ) => crate :: mem :: compress_failed ( ErrorMessage ( Some ( e . as_str ( ) ) ) ) ,
204+ Err ( _ ) => self . compress_error ( ) ,
163205 }
164206 }
165207
166208 fn reset ( & mut self ) {
209+ self . total_in = 0 ;
210+ self . total_out = 0 ;
167211 self . inner . reset ( ) ;
168212 }
169213}
170214
171215impl Backend for Deflate {
172216 #[ inline]
173217 fn total_in ( & self ) -> u64 {
174- self . inner . total_in ( )
218+ self . total_in
175219 }
176220
177221 #[ inline]
178222 fn total_out ( & self ) -> u64 {
179- self . inner . total_out ( )
223+ self . total_out
224+ }
225+ }
226+
227+ impl Deflate {
228+ fn compress_error < T > ( & self ) -> Result < T , CompressError > {
229+ compress_failed ( ErrorMessage ( self . inner . error_message ( ) ) )
230+ }
231+
232+ pub fn set_dictionary ( & mut self , dictionary : & [ u8 ] ) -> Result < u32 , CompressError > {
233+ match self . inner . set_dictionary ( dictionary) {
234+ Ok ( v) => Ok ( v) ,
235+ Err ( _) => self . compress_error ( ) ,
236+ }
237+ }
238+
239+ pub fn set_level ( & mut self , level : Compression ) -> Result < ( ) , CompressError > {
240+ use :: zlib_rs:: Status ;
241+
242+ match self . inner . set_level ( level. level ( ) as i32 ) {
243+ Ok ( status) => match status {
244+ Status :: Ok => Ok ( ( ) ) ,
245+
246+ Status :: BufError => compress_failed ( ErrorMessage ( Some ( "insufficient space" ) ) ) ,
247+
248+ Status :: StreamEnd => unreachable ! ( ) ,
249+ } ,
250+
251+ Err ( _) => self . compress_error ( ) ,
252+ }
180253 }
181254}
0 commit comments