@@ -54,43 +54,49 @@ static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
54
54
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1 ; // parse zlib header and adler32 checksum
55
55
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000 ; // write zlib header and adler32 checksum
56
56
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 > > {
58
58
unsafe {
59
59
let mut outsz : size_t = 0 ;
60
60
let res = rustrt:: tdefl_compress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
61
61
bytes. len ( ) as size_t ,
62
62
& mut outsz,
63
63
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
+ }
66
69
}
67
70
}
68
71
69
- pub fn deflate_bytes ( bytes : & [ u8 ] ) -> CVec < u8 > {
72
+ pub fn deflate_bytes ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
70
73
deflate_bytes_internal ( bytes, LZ_NORM )
71
74
}
72
75
73
- pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> CVec < u8 > {
76
+ pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
74
77
deflate_bytes_internal ( bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER )
75
78
}
76
79
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 > > {
78
81
unsafe {
79
82
let mut outsz : size_t = 0 ;
80
83
let res = rustrt:: tinfl_decompress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
81
84
bytes. len ( ) as size_t ,
82
85
& mut outsz,
83
86
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
+ }
86
92
}
87
93
}
88
94
89
- pub fn inflate_bytes ( bytes : & [ u8 ] ) -> CVec < u8 > {
95
+ pub fn inflate_bytes ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
90
96
inflate_bytes_internal ( bytes, 0 )
91
97
}
92
98
93
- pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> CVec < u8 > {
99
+ pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
94
100
inflate_bytes_internal ( bytes, TINFL_FLAG_PARSE_ZLIB_HEADER )
95
101
}
96
102
@@ -117,8 +123,8 @@ mod tests {
117
123
}
118
124
debug ! ( "de/inflate of {} bytes of random word-sequences" ,
119
125
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" ) ;
122
128
debug ! ( "{} bytes deflated to {} ({:.1f}% size)" ,
123
129
input. len( ) , cmp. len( ) ,
124
130
100.0 * ( ( cmp. len( ) as f64 ) / ( input. len( ) as f64 ) ) ) ;
@@ -129,8 +135,8 @@ mod tests {
129
135
#[ test]
130
136
fn test_zlib_flate ( ) {
131
137
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" ) ;
134
140
assert_eq ! ( inflated. as_slice( ) , bytes. as_slice( ) ) ;
135
141
}
136
142
}
0 commit comments