about summary refs log tree commit diff
path: root/src/libextra
diff options
context:
space:
mode:
authordarkf <lw9k123@gmail.com>2013-08-04 22:37:09 -0700
committerCorey Richardson <corey@octayn.net>2013-08-07 22:41:13 -0400
commitdd5e8b218fb7a6f45beca1cddb0c19949307ea3d (patch)
tree5a19b4383036e40232d84923cc42b1fde7ff2973 /src/libextra
parent9b221f56f1d9f923fad48b7e30f886acaeb3f741 (diff)
downloadrust-dd5e8b218fb7a6f45beca1cddb0c19949307ea3d.tar.gz
rust-dd5e8b218fb7a6f45beca1cddb0c19949307ea3d.zip
add extra::flate::deflate_bytes_zlib and a test
Diffstat (limited to 'src/libextra')
-rw-r--r--src/libextra/flate.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/libextra/flate.rs b/src/libextra/flate.rs
index 5d5180c152b..c974148ee12 100644
--- a/src/libextra/flate.rs
+++ b/src/libextra/flate.rs
@@ -44,8 +44,9 @@ static LZ_FAST : c_int = 0x1;   // LZ with only one probe
 static LZ_NORM : c_int = 0x80;  // LZ with 128 probes, "normal"
 static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
 static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adler32 checksum
+static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
 
-pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
+fn deflate_bytes_(bytes: &[u8], flags: c_int) -> ~[u8] {
     do bytes.as_imm_buf |b, len| {
         unsafe {
             let mut outsz : size_t = 0;
@@ -53,7 +54,7 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
                 rustrt::tdefl_compress_mem_to_heap(b as *c_void,
                                                    len as size_t,
                                                    &mut outsz,
-                                                   LZ_NORM);
+                                                   flags);
             assert!(res as int != 0);
             let out = vec::raw::from_buf_raw(res as *u8,
                                              outsz as uint);
@@ -63,6 +64,14 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
     }
 }
 
+pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
+    deflate_bytes_(bytes, LZ_NORM)
+}
+
+pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
+    deflate_bytes_(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
+}
+
 fn inflate_bytes_(bytes: &[u8], flags: c_int) -> ~[u8] {
     do bytes.as_imm_buf |b, len| {
         unsafe {
@@ -118,4 +127,12 @@ mod tests {
             assert_eq!(input, out);
         }
     }
+
+    #[test]
+    fn test_zlib_flate() {
+        let bytes = ~[1, 2, 3, 4, 5];
+        let deflated = deflate_bytes(bytes);
+        let inflated = inflate_bytes(deflated);
+        assert_eq!(inflated, bytes);
+    }
 }