Skip to content

Commit b6d3161

Browse files
committed
add extra::flate::deflate_bytes_zlib and a test
1 parent e45857c commit b6d3161

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/libextra/flate.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ static LZ_FAST : c_int = 0x1; // LZ with only one probe
4444
static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
4545
static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
4646
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
4748

48-
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
49+
fn deflate_bytes_(bytes: &[u8], flags: c_int) -> ~[u8] {
4950
do bytes.as_imm_buf |b, len| {
5051
unsafe {
5152
let mut outsz : size_t = 0;
5253
let res =
5354
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
5455
len as size_t,
5556
&mut outsz,
56-
LZ_NORM);
57+
flags);
5758
assert!(res as int != 0);
5859
let out = vec::raw::from_buf_raw(res as *u8,
5960
outsz as uint);
@@ -63,6 +64,14 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
6364
}
6465
}
6566

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+
6675
fn inflate_bytes_(bytes: &[u8], flags: c_int) -> ~[u8] {
6776
do bytes.as_imm_buf |b, len| {
6877
unsafe {
@@ -118,4 +127,12 @@ mod tests {
118127
assert_eq!(input, out);
119128
}
120129
}
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+
}
121138
}

0 commit comments

Comments
 (0)