@@ -20,9 +20,10 @@ Simple compression
20
20
#[ license = "MIT/ASL2" ] ;
21
21
#[ allow( missing_doc) ] ;
22
22
23
+ extern crate extra;
23
24
use std:: libc:: { c_void, size_t, c_int} ;
24
25
use std:: libc;
25
- use std :: vec ;
26
+ use extra :: c_vec :: CVec ;
26
27
27
28
pub mod rustrt {
28
29
use std:: libc:: { c_int, c_void, size_t} ;
@@ -33,63 +34,57 @@ pub mod rustrt {
33
34
src_buf_len : size_t ,
34
35
pout_len : * mut size_t ,
35
36
flags : c_int )
36
- -> * c_void ;
37
+ -> * mut c_void ;
37
38
38
39
pub fn tinfl_decompress_mem_to_heap ( psrc_buf : * c_void ,
39
40
src_buf_len : size_t ,
40
41
pout_len : * mut size_t ,
41
42
flags : c_int )
42
- -> * c_void ;
43
+ -> * mut c_void ;
43
44
}
44
45
}
45
46
46
47
static LZ_NORM : c_int = 0x80 ; // LZ with 128 probes, "normal"
47
48
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1 ; // parse zlib header and adler32 checksum
48
49
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000 ; // write zlib header and adler32 checksum
49
50
50
- fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> ~ [ u8 ] {
51
+ fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> CVec < u8 > {
51
52
unsafe {
52
53
let mut outsz : size_t = 0 ;
53
54
let res = rustrt:: tdefl_compress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
54
55
bytes. len ( ) as size_t ,
55
56
& mut outsz,
56
57
flags) ;
57
- assert ! ( res as int != 0 ) ;
58
- let out = vec:: raw:: from_buf_raw ( res as * u8 ,
59
- outsz as uint ) ;
60
- libc:: free ( res as * mut c_void ) ;
61
- out
58
+ assert ! ( !res. is_null( ) ) ;
59
+ CVec :: new_with_dtor ( res as * mut u8 , outsz as uint , proc ( ) libc:: free ( res) )
62
60
}
63
61
}
64
62
65
- pub fn deflate_bytes ( bytes : & [ u8 ] ) -> ~ [ u8 ] {
63
+ pub fn deflate_bytes ( bytes : & [ u8 ] ) -> CVec < u8 > {
66
64
deflate_bytes_internal ( bytes, LZ_NORM )
67
65
}
68
66
69
- pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> ~ [ u8 ] {
67
+ pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> CVec < u8 > {
70
68
deflate_bytes_internal ( bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER )
71
69
}
72
70
73
- fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> ~ [ u8 ] {
71
+ fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> CVec < u8 > {
74
72
unsafe {
75
73
let mut outsz : size_t = 0 ;
76
74
let res = rustrt:: tinfl_decompress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
77
75
bytes. len ( ) as size_t ,
78
76
& mut outsz,
79
77
flags) ;
80
- assert ! ( res as int != 0 ) ;
81
- let out = vec:: raw:: from_buf_raw ( res as * u8 ,
82
- outsz as uint ) ;
83
- libc:: free ( res as * mut c_void ) ;
84
- out
78
+ assert ! ( !res. is_null( ) ) ;
79
+ CVec :: new_with_dtor ( res as * mut u8 , outsz as uint , proc ( ) libc:: free ( res) )
85
80
}
86
81
}
87
82
88
- pub fn inflate_bytes ( bytes : & [ u8 ] ) -> ~ [ u8 ] {
83
+ pub fn inflate_bytes ( bytes : & [ u8 ] ) -> CVec < u8 > {
89
84
inflate_bytes_internal ( bytes, 0 )
90
85
}
91
86
92
- pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> ~ [ u8 ] {
87
+ pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> CVec < u8 > {
93
88
inflate_bytes_internal ( bytes, TINFL_FLAG_PARSE_ZLIB_HEADER )
94
89
}
95
90
@@ -115,19 +110,19 @@ mod tests {
115
110
debug ! ( "de/inflate of {} bytes of random word-sequences" ,
116
111
input. len( ) ) ;
117
112
let cmp = deflate_bytes ( input) ;
118
- let out = inflate_bytes ( cmp) ;
113
+ let out = inflate_bytes ( cmp. as_slice ( ) ) ;
119
114
debug ! ( "{} bytes deflated to {} ({:.1f}% size)" ,
120
115
input. len( ) , cmp. len( ) ,
121
116
100.0 * ( ( cmp. len( ) as f64 ) / ( input. len( ) as f64 ) ) ) ;
122
- assert_eq ! ( input, out) ;
117
+ assert_eq ! ( input. as_slice ( ) , out. as_slice ( ) ) ;
123
118
}
124
119
}
125
120
126
121
#[ test]
127
122
fn test_zlib_flate ( ) {
128
123
let bytes = ~[ 1 , 2 , 3 , 4 , 5 ] ;
129
124
let deflated = deflate_bytes ( bytes) ;
130
- let inflated = inflate_bytes ( deflated) ;
131
- assert_eq ! ( inflated, bytes) ;
125
+ let inflated = inflate_bytes ( deflated. as_slice ( ) ) ;
126
+ assert_eq ! ( inflated. as_slice ( ) , bytes. as_slice ( ) ) ;
132
127
}
133
128
}
0 commit comments