]> git.lizzy.rs Git - rust.git/blob - src/libflate/lib.rs
rollup merge of #20518: nagisa/weighted-bool
[rust.git] / src / libflate / lib.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Simple [DEFLATE][def]-based compression. This is a wrapper around the
12 //! [`miniz`][mz] library, which is a one-file pure-C implementation of zlib.
13 //!
14 //! [def]: https://en.wikipedia.org/wiki/DEFLATE
15 //! [mz]: https://code.google.com/p/miniz/
16
17 #![crate_name = "flate"]
18 #![experimental]
19 #![crate_type = "rlib"]
20 #![crate_type = "dylib"]
21 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
22        html_favicon_url = "http://www.rust-lang.org/favicon.ico",
23        html_root_url = "http://doc.rust-lang.org/nightly/")]
24 #![feature(phase, unboxed_closures, associated_types)]
25
26 #[cfg(test)] #[phase(plugin, link)] extern crate log;
27
28 extern crate libc;
29
30 use libc::{c_void, size_t, c_int};
31 use std::ops::Deref;
32 use std::ptr::Unique;
33 use std::slice;
34
35 pub struct Bytes {
36     ptr: Unique<u8>,
37     len: uint,
38 }
39
40 impl Deref for Bytes {
41     type Target = [u8];
42     fn deref(&self) -> &[u8] {
43         unsafe { slice::from_raw_mut_buf(&self.ptr.0, self.len) }
44     }
45 }
46
47 impl Drop for Bytes {
48     fn drop(&mut self) {
49         unsafe { libc::free(self.ptr.0 as *mut _); }
50     }
51 }
52
53 #[link(name = "miniz", kind = "static")]
54 extern {
55     /// Raw miniz compression function.
56     fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
57                                   src_buf_len: size_t,
58                                   pout_len: *mut size_t,
59                                   flags: c_int)
60                                   -> *mut c_void;
61
62     /// Raw miniz decompression function.
63     fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
64                                     src_buf_len: size_t,
65                                     pout_len: *mut size_t,
66                                     flags: c_int)
67                                     -> *mut c_void;
68 }
69
70 static LZ_NORM : c_int = 0x80;  // LZ with 128 probes, "normal"
71 static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adler32 checksum
72 static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
73
74 fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<Bytes> {
75     unsafe {
76         let mut outsz : size_t = 0;
77         let res = tdefl_compress_mem_to_heap(bytes.as_ptr() as *const _,
78                                              bytes.len() as size_t,
79                                              &mut outsz,
80                                              flags);
81         if !res.is_null() {
82             let res = Unique(res as *mut u8);
83             Some(Bytes { ptr: res, len: outsz as uint })
84         } else {
85             None
86         }
87     }
88 }
89
90 /// Compress a buffer, without writing any sort of header on the output.
91 pub fn deflate_bytes(bytes: &[u8]) -> Option<Bytes> {
92     deflate_bytes_internal(bytes, LZ_NORM)
93 }
94
95 /// Compress a buffer, using a header that zlib can understand.
96 pub fn deflate_bytes_zlib(bytes: &[u8]) -> Option<Bytes> {
97     deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
98 }
99
100 fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<Bytes> {
101     unsafe {
102         let mut outsz : size_t = 0;
103         let res = tinfl_decompress_mem_to_heap(bytes.as_ptr() as *const _,
104                                                bytes.len() as size_t,
105                                                &mut outsz,
106                                                flags);
107         if !res.is_null() {
108             let res = Unique(res as *mut u8);
109             Some(Bytes { ptr: res, len: outsz as uint })
110         } else {
111             None
112         }
113     }
114 }
115
116 /// Decompress a buffer, without parsing any sort of header on the input.
117 pub fn inflate_bytes(bytes: &[u8]) -> Option<Bytes> {
118     inflate_bytes_internal(bytes, 0)
119 }
120
121 /// Decompress a buffer that starts with a zlib header.
122 pub fn inflate_bytes_zlib(bytes: &[u8]) -> Option<Bytes> {
123     inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
124 }
125
126 #[cfg(test)]
127 mod tests {
128     use super::{inflate_bytes, deflate_bytes};
129     use std::rand;
130     use std::rand::Rng;
131
132     #[test]
133     fn test_flate_round_trip() {
134         let mut r = rand::thread_rng();
135         let mut words = vec!();
136         for _ in range(0u, 20) {
137             let range = r.gen_range(1u, 10);
138             let v = r.gen_iter::<u8>().take(range).collect::<Vec<u8>>();
139             words.push(v);
140         }
141         for _ in range(0u, 20) {
142             let mut input = vec![];
143             for _ in range(0u, 2000) {
144                 input.push_all(r.choose(words.as_slice()).unwrap().as_slice());
145             }
146             debug!("de/inflate of {} bytes of random word-sequences",
147                    input.len());
148             let cmp = deflate_bytes(input.as_slice()).expect("deflation failed");
149             let out = inflate_bytes(cmp.as_slice()).expect("inflation failed");
150             debug!("{} bytes deflated to {} ({:.1}% size)",
151                    input.len(), cmp.len(),
152                    100.0 * ((cmp.len() as f64) / (input.len() as f64)));
153             assert_eq!(input, out.as_slice());
154         }
155     }
156
157     #[test]
158     fn test_zlib_flate() {
159         let bytes = vec!(1, 2, 3, 4, 5);
160         let deflated = deflate_bytes(bytes.as_slice()).expect("deflation failed");
161         let inflated = inflate_bytes(deflated.as_slice()).expect("inflation failed");
162         assert_eq!(inflated.as_slice(), bytes);
163     }
164 }