@@ -44,16 +44,17 @@ static LZ_FAST : c_int = 0x1; // LZ with only one probe
44
44
static LZ_NORM : c_int = 0x80 ; // LZ with 128 probes, "normal"
45
45
static LZ_BEST : c_int = 0xfff ; // LZ with 4095 probes, "best"
46
46
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1 ; // parse zlib header and adler32 checksum
47
+ static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000 ; // write zlib header and adler32 checksum
47
48
48
- pub fn deflate_bytes ( bytes : & [ u8 ] ) -> ~[ u8 ] {
49
+ fn deflate_bytes_ ( bytes : & [ u8 ] , flags : c_int ) -> ~[ u8 ] {
49
50
do bytes. as_imm_buf |b, len| {
50
51
unsafe {
51
52
let mut outsz : size_t = 0 ;
52
53
let res =
53
54
rustrt:: tdefl_compress_mem_to_heap ( b as * c_void ,
54
55
len as size_t ,
55
56
& mut outsz,
56
- LZ_NORM ) ;
57
+ flags ) ;
57
58
assert ! ( res as int != 0 ) ;
58
59
let out = vec:: raw:: from_buf_raw ( res as * u8 ,
59
60
outsz as uint ) ;
@@ -63,6 +64,14 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
63
64
}
64
65
}
65
66
67
+ pub fn deflate_bytes ( bytes : & [ u8 ] ) -> ~[ u8 ] {
68
+ deflate_bytes_ ( bytes, LZ_NORM )
69
+ }
70
+
71
+ pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> ~[ u8 ] {
72
+ deflate_bytes_ ( bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER )
73
+ }
74
+
66
75
fn inflate_bytes_ ( bytes : & [ u8 ] , flags : c_int ) -> ~[ u8 ] {
67
76
do bytes. as_imm_buf |b, len| {
68
77
unsafe {
@@ -118,4 +127,12 @@ mod tests {
118
127
assert_eq ! ( input, out) ;
119
128
}
120
129
}
130
+
131
+ #[ test]
132
+ fn test_zlib_flate ( ) {
133
+ let bytes = ~[ 1 , 2 , 3 , 4 , 5 ] ;
134
+ let deflated = deflate_bytes ( bytes) ;
135
+ let inflated = inflate_bytes ( deflated) ;
136
+ assert_eq ! ( inflated, bytes) ;
137
+ }
121
138
}
0 commit comments