11use crate :: zlib:: Status ;
2- use std :: ffi :: c_int ;
2+ use zlib_rs :: DeflateError ;
33
44const BUF_SIZE : usize = 4096 * 8 ;
55
2626}
2727
2828/// Hold all state needed for compressing data.
29- pub struct Compress ( libz_rs_sys:: z_stream ) ;
30-
31- unsafe impl Sync for Compress { }
32- unsafe impl Send for Compress { }
29+ pub struct Compress ( zlib_rs:: Deflate ) ;
3330
3431impl Default for Compress {
3532 fn default ( ) -> Self {
@@ -40,72 +37,63 @@ impl Default for Compress {
4037impl Compress {
4138 /// The number of bytes that were read from the input.
4239 pub fn total_in ( & self ) -> u64 {
43- self . 0 . total_in as _
40+ self . 0 . total_in ( )
4441 }
4542
4643 /// The number of compressed bytes that were written to the output.
4744 pub fn total_out ( & self ) -> u64 {
48- self . 0 . total_out as _
45+ self . 0 . total_out ( )
4946 }
5047
5148 /// Create a new instance - this allocates so should be done with care.
5249 pub fn new ( ) -> Self {
53- let mut this = libz_rs_sys:: z_stream:: default ( ) ;
54-
55- unsafe {
56- libz_rs_sys:: deflateInit_ (
57- & mut this,
58- libz_rs_sys:: Z_BEST_SPEED ,
59- libz_rs_sys:: zlibVersion ( ) ,
60- core:: mem:: size_of :: < libz_rs_sys:: z_stream > ( ) as core:: ffi:: c_int ,
61- ) ;
62- }
63-
64- Self ( this)
50+ let inner = zlib_rs:: Deflate :: new ( zlib_rs:: c_api:: Z_BEST_SPEED , true , zlib_rs:: MAX_WBITS as u8 ) ;
51+ Self ( inner)
6552 }
6653
6754 /// Prepare the instance for a new stream.
6855 pub fn reset ( & mut self ) {
69- unsafe { libz_rs_sys :: deflateReset ( & mut self . 0 ) } ;
56+ self . 0 . reset ( ) ;
7057 }
7158
7259 /// Compress `input` and write compressed bytes to `output`, with `flush` controlling additional characteristics.
7360 pub fn compress ( & mut self , input : & [ u8 ] , output : & mut [ u8 ] , flush : FlushCompress ) -> Result < Status , CompressError > {
74- self . 0 . avail_in = input. len ( ) as _ ;
75- self . 0 . avail_out = output. len ( ) as _ ;
76-
77- self . 0 . next_in = input. as_ptr ( ) ;
78- self . 0 . next_out = output. as_mut_ptr ( ) ;
79-
80- match unsafe { libz_rs_sys:: deflate ( & mut self . 0 , flush as _ ) } {
81- libz_rs_sys:: Z_OK => Ok ( Status :: Ok ) ,
82- libz_rs_sys:: Z_BUF_ERROR => Ok ( Status :: BufError ) ,
83- libz_rs_sys:: Z_STREAM_END => Ok ( Status :: StreamEnd ) ,
84-
85- libz_rs_sys:: Z_STREAM_ERROR => Err ( CompressError :: StreamError ) ,
86- libz_rs_sys:: Z_MEM_ERROR => Err ( CompressError :: InsufficientMemory ) ,
87- err => Err ( CompressError :: Unknown { err } ) ,
61+ let flush = match flush {
62+ FlushCompress :: None => zlib_rs:: DeflateFlush :: NoFlush ,
63+ FlushCompress :: Partial => zlib_rs:: DeflateFlush :: PartialFlush ,
64+ FlushCompress :: Sync => zlib_rs:: DeflateFlush :: SyncFlush ,
65+ FlushCompress :: Full => zlib_rs:: DeflateFlush :: FullFlush ,
66+ FlushCompress :: Finish => zlib_rs:: DeflateFlush :: Finish ,
67+ } ;
68+ let status = self . 0 . compress ( input, output, flush) ?;
69+ match status {
70+ zlib_rs:: Status :: Ok => Ok ( Status :: Ok ) ,
71+ zlib_rs:: Status :: BufError => Ok ( Status :: BufError ) ,
72+ zlib_rs:: Status :: StreamEnd => Ok ( Status :: StreamEnd ) ,
8873 }
8974 }
9075}
9176
92- impl Drop for Compress {
93- fn drop ( & mut self ) {
94- unsafe { libz_rs_sys:: deflateEnd ( & mut self . 0 ) } ;
95- }
96- }
97-
9877/// The error produced by [`Compress::compress()`].
9978#[ derive( Debug , thiserror:: Error ) ]
100- #[ error( "{msg}" ) ]
10179#[ allow( missing_docs) ]
10280pub enum CompressError {
10381 #[ error( "stream error" ) ]
10482 StreamError ,
83+ #[ error( "The input is not a valid deflate stream." ) ]
84+ DataError ,
10585 #[ error( "Not enough memory" ) ]
10686 InsufficientMemory ,
107- #[ error( "An unknown error occurred: {err}" ) ]
108- Unknown { err : c_int } ,
87+ }
88+
89+ impl From < zlib_rs:: DeflateError > for CompressError {
90+ fn from ( value : zlib_rs:: DeflateError ) -> Self {
91+ match value {
92+ DeflateError :: StreamError => CompressError :: StreamError ,
93+ DeflateError :: DataError => CompressError :: DataError ,
94+ DeflateError :: MemError => CompressError :: InsufficientMemory ,
95+ }
96+ }
10997}
11098
11199/// Values which indicate the form of flushing to be used when compressing
@@ -117,7 +105,7 @@ pub enum FlushCompress {
117105 /// A typical parameter for passing to compression/decompression functions,
118106 /// this indicates that the underlying stream to decide how much data to
119107 /// accumulate before producing output in order to maximize compression.
120- None = libz_rs_sys :: Z_NO_FLUSH as isize ,
108+ None = 0 ,
121109
122110 /// All pending output is flushed to the output buffer, but the output is
123111 /// not aligned to a byte boundary.
@@ -127,7 +115,7 @@ pub enum FlushCompress {
127115 /// with an empty fixed codes block that is 10 bits long, and it assures
128116 /// that enough bytes are output in order for the decompressor to finish the
129117 /// block before the empty fixed code block.
130- Partial = libz_rs_sys :: Z_PARTIAL_FLUSH as isize ,
118+ Partial = 1 ,
131119
132120 /// All pending output is flushed to the output buffer and the output is
133121 /// aligned on a byte boundary so that the decompressor can get all input
@@ -136,20 +124,20 @@ pub enum FlushCompress {
136124 /// Flushing may degrade compression for some compression algorithms and so
137125 /// it should only be used when necessary. This will complete the current
138126 /// deflate block and follow it with an empty stored block.
139- Sync = libz_rs_sys :: Z_SYNC_FLUSH as isize ,
127+ Sync = 2 ,
140128
141129 /// All output is flushed as with `Flush::Sync` and the compression state is
142130 /// reset so decompression can restart from this point if previous
143131 /// compressed data has been damaged or if random access is desired.
144132 ///
145133 /// Using this option too often can seriously degrade compression.
146- Full = libz_rs_sys :: Z_FULL_FLUSH as isize ,
134+ Full = 3 ,
147135
148136 /// Pending input is processed and pending output is flushed.
149137 ///
150138 /// The return value may indicate that the stream is not yet done and more
151139 /// data has yet to be processed.
152- Finish = libz_rs_sys :: Z_FINISH as isize ,
140+ Finish = 4 ,
153141}
154142
155143mod impls {
0 commit comments