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.
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.
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.
14 //! [def]: https://en.wikipedia.org/wiki/DEFLATE
15 //! [mz]: https://code.google.com/p/miniz/
17 #![crate_name = "flate"]
18 #![unstable(feature = "rustc_private", issue = "27812")]
19 #![crate_type = "rlib"]
20 #![crate_type = "dylib"]
21 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
22 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
23 html_root_url = "https://doc.rust-lang.org/nightly/",
24 test(attr(deny(warnings))))]
28 #![feature(staged_api)]
30 #![cfg_attr(test, feature(rand))]
34 use libc::{c_int, c_void, size_t};
40 #[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
51 impl fmt::Debug for Error {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 "decompression error".fmt(f)
62 impl Deref for Bytes {
64 fn deref(&self) -> &[u8] {
65 unsafe { slice::from_raw_parts(*self.ptr, self.len) }
72 libc::free(*self.ptr as *mut _);
77 #[link(name = "miniz", kind = "static")]
78 #[cfg(not(cargobuild))]
82 /// Raw miniz compression function.
83 fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
85 pout_len: *mut size_t,
89 /// Raw miniz decompression function.
90 fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
92 pout_len: *mut size_t,
97 const LZ_FAST: c_int = 0x01; // LZ with 1 probe, "fast"
98 const TDEFL_GREEDY_PARSING_FLAG: c_int = 0x04000; // fast greedy parsing instead of lazy parsing
100 /// Compress a buffer without writing any sort of header on the output. Fast
101 /// compression is used because it is almost twice as fast as default
102 /// compression and the compression ratio is only marginally worse.
103 pub fn deflate_bytes(bytes: &[u8]) -> Bytes {
104 let flags = LZ_FAST | TDEFL_GREEDY_PARSING_FLAG;
106 let mut outsz: size_t = 0;
107 let res = tdefl_compress_mem_to_heap(bytes.as_ptr() as *const _,
108 bytes.len() as size_t,
111 assert!(!res.is_null());
113 ptr: Unique::new(res as *mut u8),
119 /// Decompress a buffer without parsing any sort of header on the input.
120 pub fn inflate_bytes(bytes: &[u8]) -> Result<Bytes, Error> {
123 let mut outsz: size_t = 0;
124 let res = tinfl_decompress_mem_to_heap(bytes.as_ptr() as *const _,
125 bytes.len() as size_t,
130 ptr: Unique::new(res as *mut u8),
141 #![allow(deprecated)]
142 use super::{deflate_bytes, inflate_bytes};
143 use std::__rand::{Rng, thread_rng};
146 fn test_flate_round_trip() {
147 let mut r = thread_rng();
148 let mut words = vec![];
150 let range = r.gen_range(1, 10);
151 let v = r.gen_iter::<u8>().take(range).collect::<Vec<u8>>();
155 let mut input = vec![];
157 input.extend_from_slice(r.choose(&words).unwrap());
159 let cmp = deflate_bytes(&input);
160 let out = inflate_bytes(&cmp).unwrap();
161 assert_eq!(&*input, &*out);
166 fn test_zlib_flate() {
167 let bytes = vec![1, 2, 3, 4, 5];
168 let deflated = deflate_bytes(&bytes);
169 let inflated = inflate_bytes(&deflated).unwrap();
170 assert_eq!(&*inflated, &*bytes);