rust/src/libextra/flate.rs

139 lines
4.5 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
Simple compression
*/
#[allow(missing_doc)];
use std::libc::{c_void, size_t, c_int};
use std::libc;
use std::vec;
pub mod rustrt {
use std::libc::{c_int, c_void, size_t};
Add generation of static libraries to rustc This commit implements the support necessary for generating both intermediate and result static rust libraries. This is an implementation of my thoughts in https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html. When compiling a library, we still retain the "lib" option, although now there are "rlib", "staticlib", and "dylib" as options for crate_type (and these are stackable). The idea of "lib" is to generate the "compiler default" instead of having too choose (although all are interchangeable). For now I have left the "complier default" to be a dynamic library for size reasons. Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a dynamic object. I chose this for size reasons, but also because you're probably not going to be embedding the rustc compiler anywhere any time soon. Other than the options outlined above, there are a few defaults/preferences that are now opinionated in the compiler: * If both a .dylib and .rlib are found for a rust library, the compiler will prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option * If generating a "lib", the compiler will generate a dynamic library. This is overridable by explicitly saying what flavor you'd like (rlib, staticlib, dylib). * If no options are passed to the command line, and no crate_type is found in the destination crate, then an executable is generated With this change, you can successfully build a rust program with 0 dynamic dependencies on rust libraries. There is still a dynamic dependency on librustrt, but I plan on removing that in a subsequent commit. This change includes no tests just yet. Our current testing infrastructure/harnesses aren't very amenable to doing flavorful things with linking, so I'm planning on adding a new mode of testing which I believe belongs as a separate commit. Closes #552
2013-11-15 16:03:29 -06:00
#[link(name = "rustrt")]
extern {
pub fn tdefl_compress_mem_to_heap(psrc_buf: *c_void,
src_buf_len: size_t,
pout_len: *mut size_t,
flags: c_int)
-> *c_void;
pub fn tinfl_decompress_mem_to_heap(psrc_buf: *c_void,
src_buf_len: size_t,
pout_len: *mut size_t,
flags: c_int)
-> *c_void;
}
}
static LZ_NONE : c_int = 0x0; // Huffman-coding only.
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"
2013-08-05 00:28:53 -05:00
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
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
bytes.as_imm_buf(|b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
len as size_t,
2013-04-30 13:10:21 -05:00
&mut outsz,
flags);
2013-03-28 20:39:09 -05:00
assert!(res as int != 0);
2012-10-11 17:37:37 -05:00
let out = vec::raw::from_buf_raw(res as *u8,
2013-04-30 13:10:21 -05:00
outsz as uint);
libc::free(res);
2013-02-15 02:51:28 -06:00
out
}
})
}
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
deflate_bytes_internal(bytes, LZ_NORM)
}
pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
}
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
bytes.as_imm_buf(|b, len| {
unsafe {
2013-04-30 13:10:21 -05:00
let mut outsz : size_t = 0;
let res =
rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
len as size_t,
2013-04-30 13:10:21 -05:00
&mut outsz,
2013-08-05 00:28:53 -05:00
flags);
2013-03-28 20:39:09 -05:00
assert!(res as int != 0);
2012-10-11 17:37:37 -05:00
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
libc::free(res);
2013-02-15 02:51:28 -06:00
out
}
})
}
2013-08-05 00:28:53 -05:00
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
inflate_bytes_internal(bytes, 0)
2013-08-05 00:28:53 -05:00
}
pub fn inflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
2013-08-05 00:28:53 -05:00
}
2013-05-23 11:39:00 -05:00
#[cfg(test)]
mod tests {
use super::*;
use std::rand;
use std::rand::Rng;
2013-05-23 11:39:00 -05:00
#[test]
fn test_flate_round_trip() {
let mut r = rand::rng();
let mut words = ~[];
20.times(|| {
let range = r.gen_range(1u, 10);
words.push(r.gen_vec::<u8>(range));
});
20.times(|| {
let mut input = ~[];
2000.times(|| {
input.push_all(r.choose(words));
});
debug!("de/inflate of {} bytes of random word-sequences",
input.len());
let cmp = deflate_bytes(input);
2013-05-23 11:39:00 -05:00
let out = inflate_bytes(cmp);
debug!("{} bytes deflated to {} ({:.1f}% size)",
input.len(), cmp.len(),
100.0 * ((cmp.len() as f64) / (input.len() as f64)));
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);
}
}