@@ -54,43 +54,49 @@ static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
5454static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1 ; // parse zlib header and adler32 checksum
5555static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000 ; // write zlib header and adler32 checksum
5656
57- fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> CVec < u8 > {
57+ fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Option < CVec < u8 > > {
5858 unsafe {
5959 let mut outsz : size_t = 0 ;
6060 let res = rustrt:: tdefl_compress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
6161 bytes. len ( ) as size_t ,
6262 & mut outsz,
6363 flags) ;
64- assert ! ( !res. is_null( ) ) ;
65- CVec :: new_with_dtor ( res as * mut u8 , outsz as uint , proc ( ) libc:: free ( res) )
64+ if !res. is_null ( ) {
65+ Some ( CVec :: new_with_dtor ( res as * mut u8 , outsz as uint , proc ( ) libc:: free ( res) ) )
66+ } else {
67+ None
68+ }
6669 }
6770}
6871
69- pub fn deflate_bytes ( bytes : & [ u8 ] ) -> CVec < u8 > {
72+ pub fn deflate_bytes ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
7073 deflate_bytes_internal ( bytes, LZ_NORM )
7174}
7275
73- pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> CVec < u8 > {
76+ pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
7477 deflate_bytes_internal ( bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER )
7578}
7679
77- fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> CVec < u8 > {
80+ fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Option < CVec < u8 > > {
7881 unsafe {
7982 let mut outsz : size_t = 0 ;
8083 let res = rustrt:: tinfl_decompress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
8184 bytes. len ( ) as size_t ,
8285 & mut outsz,
8386 flags) ;
84- assert ! ( !res. is_null( ) ) ;
85- CVec :: new_with_dtor ( res as * mut u8 , outsz as uint , proc ( ) libc:: free ( res) )
87+ if !res. is_null ( ) {
88+ Some ( CVec :: new_with_dtor ( res as * mut u8 , outsz as uint , proc ( ) libc:: free ( res) ) )
89+ } else {
90+ None
91+ }
8692 }
8793}
8894
89- pub fn inflate_bytes ( bytes : & [ u8 ] ) -> CVec < u8 > {
95+ pub fn inflate_bytes ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
9096 inflate_bytes_internal ( bytes, 0 )
9197}
9298
93- pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> CVec < u8 > {
99+ pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
94100 inflate_bytes_internal ( bytes, TINFL_FLAG_PARSE_ZLIB_HEADER )
95101}
96102
@@ -117,8 +123,8 @@ mod tests {
117123 }
118124 debug ! ( "de/inflate of {} bytes of random word-sequences" ,
119125 input. len( ) ) ;
120- let cmp = deflate_bytes ( input) ;
121- let out = inflate_bytes ( cmp. as_slice ( ) ) ;
126+ let cmp = deflate_bytes ( input) . expect ( "deflation failed" ) ;
127+ let out = inflate_bytes ( cmp. as_slice ( ) ) . expect ( "inflation failed" ) ;
122128 debug ! ( "{} bytes deflated to {} ({:.1f}% size)" ,
123129 input. len( ) , cmp. len( ) ,
124130 100.0 * ( ( cmp. len( ) as f64 ) / ( input. len( ) as f64 ) ) ) ;
@@ -129,8 +135,8 @@ mod tests {
129135 #[ test]
130136 fn test_zlib_flate ( ) {
131137 let bytes = vec ! ( 1 , 2 , 3 , 4 , 5 ) ;
132- let deflated = deflate_bytes ( bytes. as_slice ( ) ) ;
133- let inflated = inflate_bytes ( deflated. as_slice ( ) ) ;
138+ let deflated = deflate_bytes ( bytes. as_slice ( ) ) . expect ( "deflation failed" ) ;
139+ let inflated = inflate_bytes ( deflated. as_slice ( ) ) . expect ( "inflation failed" ) ;
134140 assert_eq ! ( inflated. as_slice( ) , bytes. as_slice( ) ) ;
135141 }
136142}
0 commit comments