Made libflate functions return Options instead of outright failing
This commit is contained in:
parent
dc49018679
commit
bc234ae130
@ -54,43 +54,49 @@ static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
|
||||
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) -> CVec<u8> {
|
||||
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
|
||||
unsafe {
|
||||
let mut outsz : size_t = 0;
|
||||
let res = rustrt::tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
|
||||
bytes.len() as size_t,
|
||||
&mut outsz,
|
||||
flags);
|
||||
assert!(!res.is_null());
|
||||
CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res))
|
||||
if !res.is_null() {
|
||||
Some(CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deflate_bytes(bytes: &[u8]) -> CVec<u8> {
|
||||
pub fn deflate_bytes(bytes: &[u8]) -> Option<CVec<u8>> {
|
||||
deflate_bytes_internal(bytes, LZ_NORM)
|
||||
}
|
||||
|
||||
pub fn deflate_bytes_zlib(bytes: &[u8]) -> CVec<u8> {
|
||||
pub fn deflate_bytes_zlib(bytes: &[u8]) -> Option<CVec<u8>> {
|
||||
deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
|
||||
}
|
||||
|
||||
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> CVec<u8> {
|
||||
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
|
||||
unsafe {
|
||||
let mut outsz : size_t = 0;
|
||||
let res = rustrt::tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
|
||||
bytes.len() as size_t,
|
||||
&mut outsz,
|
||||
flags);
|
||||
assert!(!res.is_null());
|
||||
CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res))
|
||||
if !res.is_null() {
|
||||
Some(CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inflate_bytes(bytes: &[u8]) -> CVec<u8> {
|
||||
pub fn inflate_bytes(bytes: &[u8]) -> Option<CVec<u8>> {
|
||||
inflate_bytes_internal(bytes, 0)
|
||||
}
|
||||
|
||||
pub fn inflate_bytes_zlib(bytes: &[u8]) -> CVec<u8> {
|
||||
pub fn inflate_bytes_zlib(bytes: &[u8]) -> Option<CVec<u8>> {
|
||||
inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
|
||||
}
|
||||
|
||||
@ -117,8 +123,8 @@ mod tests {
|
||||
}
|
||||
debug!("de/inflate of {} bytes of random word-sequences",
|
||||
input.len());
|
||||
let cmp = deflate_bytes(input);
|
||||
let out = inflate_bytes(cmp.as_slice());
|
||||
let cmp = deflate_bytes(input).expect("deflation failed");
|
||||
let out = inflate_bytes(cmp.as_slice()).expect("inflation failed");
|
||||
debug!("{} bytes deflated to {} ({:.1f}% size)",
|
||||
input.len(), cmp.len(),
|
||||
100.0 * ((cmp.len() as f64) / (input.len() as f64)));
|
||||
@ -129,8 +135,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_zlib_flate() {
|
||||
let bytes = vec!(1, 2, 3, 4, 5);
|
||||
let deflated = deflate_bytes(bytes.as_slice());
|
||||
let inflated = inflate_bytes(deflated.as_slice());
|
||||
let deflated = deflate_bytes(bytes.as_slice()).expect("deflation failed");
|
||||
let inflated = inflate_bytes(deflated.as_slice()).expect("inflation failed");
|
||||
assert_eq!(inflated.as_slice(), bytes.as_slice());
|
||||
}
|
||||
}
|
||||
|
@ -945,11 +945,14 @@ fn link_rlib<'a>(sess: &'a Session,
|
||||
let bc_deflated = obj_filename.with_extension("bc.deflate");
|
||||
match fs::File::open(&bc).read_to_end().and_then(|data| {
|
||||
fs::File::create(&bc_deflated)
|
||||
.write(flate::deflate_bytes(data.as_slice()).as_slice())
|
||||
.write(match flate::deflate_bytes(data.as_slice()) {
|
||||
Some(compressed) => compressed,
|
||||
None => sess.fatal("failed to compress bytecode")
|
||||
}.as_slice())
|
||||
}) {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
sess.err(format!("failed to compress bytecode: {}", e));
|
||||
sess.err(format!("failed to write compressed bytecode: {}", e));
|
||||
sess.abort_if_errors()
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,10 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
|
||||
archive.read(format!("{}.bc.deflate", name)));
|
||||
let bc = bc.expect("missing compressed bytecode in archive!");
|
||||
let bc = time(sess.time_passes(), format!("inflate {}.bc", name), (), |_|
|
||||
flate::inflate_bytes(bc));
|
||||
match flate::inflate_bytes(bc) {
|
||||
Some(bc) => bc,
|
||||
None => sess.fatal(format!("failed to decompress bc of `{}`", name))
|
||||
});
|
||||
let ptr = bc.as_slice().as_ptr();
|
||||
debug!("linking {}", name);
|
||||
time(sess.time_passes(), format!("ll link {}", name), (), |()| unsafe {
|
||||
|
@ -494,14 +494,17 @@ fn get_metadata_section_imp(os: Os, filename: &Path) -> Result<MetadataBlob, ~st
|
||||
let version_ok = slice::raw::buf_as_slice(cvbuf, minsz,
|
||||
|buf0| buf0 == encoder::metadata_encoding_version);
|
||||
if !version_ok { return Err(format!("incompatible metadata version found: '{}'",
|
||||
filename.display()));}
|
||||
filename.display())); }
|
||||
|
||||
let cvbuf1 = cvbuf.offset(vlen as int);
|
||||
debug!("inflating {} bytes of compressed metadata",
|
||||
csz - vlen);
|
||||
slice::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {
|
||||
let inflated = flate::inflate_bytes(bytes);
|
||||
found = Ok(MetadataVec(inflated));
|
||||
match flate::inflate_bytes(bytes) {
|
||||
Some(inflated) => found = Ok(MetadataVec(inflated)),
|
||||
None => found = Err(format!("failed to decompress metadata for: '{}'",
|
||||
filename.display()))
|
||||
}
|
||||
});
|
||||
if found.is_ok() {
|
||||
return found;
|
||||
|
@ -2236,7 +2236,10 @@ pub fn write_metadata(cx: &CrateContext, krate: &ast::Crate) -> Vec<u8> {
|
||||
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
|
||||
let metadata = encoder::encode_metadata(encode_parms, krate);
|
||||
let compressed = encoder::metadata_encoding_version +
|
||||
flate::deflate_bytes(metadata.as_slice()).as_slice();
|
||||
match flate::deflate_bytes(metadata.as_slice()) {
|
||||
Some(compressed) => compressed,
|
||||
None => cx.sess().fatal(format!("failed to compress metadata", ))
|
||||
}.as_slice();
|
||||
let llmeta = C_bytes(cx, compressed);
|
||||
let llconst = C_struct(cx, [llmeta], false);
|
||||
let name = format!("rust_metadata_{}_{}_{}", cx.link_meta.crateid.name,
|
||||
|
Loading…
x
Reference in New Issue
Block a user