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.
17 #[allow(missing_doc)];
19 use std::libc::{c_void, size_t, c_int};
24 use std::libc::{c_int, c_void, size_t};
26 #[link_name = "rustrt"]
28 pub fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
30 pout_len: *mut size_t,
34 pub fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
36 pout_len: *mut size_t,
42 static LZ_NONE : c_int = 0x0; // Huffman-coding only.
43 static LZ_FAST : c_int = 0x1; // LZ with only one probe
44 static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
45 static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
47 pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
48 do bytes.as_imm_buf |b, len| {
50 let mut outsz : size_t = 0;
52 rustrt::tdefl_compress_mem_to_heap(b as *c_void,
56 assert!(res as int != 0);
57 let out = vec::raw::from_buf_raw(res as *u8,
65 pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
66 do bytes.as_imm_buf |b, len| {
68 let mut outsz : size_t = 0;
70 rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
74 assert!(res as int != 0);
75 let out = vec::raw::from_buf_raw(res as *u8,
87 use std::rand::RngUtil;
90 fn test_flate_round_trip() {
91 let mut r = rand::rng();
94 let range = r.gen_uint_range(1, 10);
95 words.push(r.gen_bytes(range));
100 input.push_all(r.choose(words));
102 debug!("de/inflate of %u bytes of random word-sequences",
104 let cmp = deflate_bytes(input);
105 let out = inflate_bytes(cmp);
106 debug!("%u bytes deflated to %u (%.1f%% size)",
107 input.len(), cmp.len(),
108 100.0 * ((cmp.len() as float) / (input.len() as float)));
109 assert_eq!(input, out);