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 _);
78 /// Raw miniz compression function.
79 fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
81 pout_len: *mut size_t,
85 /// Raw miniz decompression function.
86 fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
88 pout_len: *mut size_t,
93 const LZ_FAST: c_int = 0x01; // LZ with 1 probe, "fast"
94 const TDEFL_GREEDY_PARSING_FLAG: c_int = 0x04000; // fast greedy parsing instead of lazy parsing
96 /// Compress a buffer without writing any sort of header on the output. Fast
97 /// compression is used because it is almost twice as fast as default
98 /// compression and the compression ratio is only marginally worse.
99 pub fn deflate_bytes(bytes: &[u8]) -> Bytes {
100 let flags = LZ_FAST | TDEFL_GREEDY_PARSING_FLAG;
102 let mut outsz: size_t = 0;
103 let res = tdefl_compress_mem_to_heap(bytes.as_ptr() as *const _,
104 bytes.len() as size_t,
107 assert!(!res.is_null());
109 ptr: Unique::new(res as *mut u8),
115 /// Decompress a buffer without parsing any sort of header on the input.
116 pub fn inflate_bytes(bytes: &[u8]) -> Result<Bytes, Error> {
119 let mut outsz: size_t = 0;
120 let res = tinfl_decompress_mem_to_heap(bytes.as_ptr() as *const _,
121 bytes.len() as size_t,
126 ptr: Unique::new(res as *mut u8),
137 #![allow(deprecated)]
138 use super::{deflate_bytes, inflate_bytes};
139 use std::__rand::{Rng, thread_rng};
142 fn test_flate_round_trip() {
143 let mut r = thread_rng();
144 let mut words = vec![];
146 let range = r.gen_range(1, 10);
147 let v = r.gen_iter::<u8>().take(range).collect::<Vec<u8>>();
151 let mut input = vec![];
153 input.extend_from_slice(r.choose(&words).unwrap());
155 let cmp = deflate_bytes(&input);
156 let out = inflate_bytes(&cmp).unwrap();
157 assert_eq!(&*input, &*out);
162 fn test_zlib_flate() {
163 let bytes = vec![1, 2, 3, 4, 5];
164 let deflated = deflate_bytes(&bytes);
165 let inflated = inflate_bytes(&deflated).unwrap();
166 assert_eq!(&*inflated, &*bytes);