inflate-0.4.5/.gitignore010064400007650000024000000000241343106754200133520ustar0000000000000000/target/ Cargo.lock inflate-0.4.5/.travis.yml010064400007650000024000000001141343106754200134730ustar0000000000000000language: rust rust: - 1.0.0 - stable - nightly script: cargo test -v inflate-0.4.5/Cargo.toml.orig010064400007650000024000000005321343106754200142550ustar0000000000000000[package] name = "inflate" version = "0.4.5" license = "MIT" description = "DEFLATE decoding" authors = ["nwin "] repository = "https://github.com/PistonDevelopers/inflate.git" keywords = ["deflate", "decompression", "compression", "piston"] [features] default = [] unstable = [] [dependencies] adler32 = "1.0.2" inflate-0.4.5/Cargo.toml0000644000000015550000000000000105270ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "inflate" version = "0.4.5" authors = ["nwin "] description = "DEFLATE decoding" keywords = ["deflate", "decompression", "compression", "piston"] license = "MIT" repository = "https://github.com/PistonDevelopers/inflate.git" [dependencies.adler32] version = "1.0.2" [features] default = [] unstable = [] inflate-0.4.5/LICENSE010064400007650000024000000020741343106754200123760ustar0000000000000000The MIT License (MIT) Copyright (c) 2015 PistonDevelopers Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. inflate-0.4.5/README.md010064400007650000024000000040461343106754200126510ustar0000000000000000# inflate A [DEFLATE](http://www.gzip.org/zlib/rfc-deflate.html) decoder written in rust. This library provides functionality to decompress data compressed with the DEFLATE algorithm, both with and without a [zlib](https://tools.ietf.org/html/rfc1950) header/trailer. # Examples The easiest way to get `std::Vec` containing the decompressed bytes is to use either `inflate::inflate_bytes` or `inflate::inflate_bytes_zlib` (depending on whether the encoded data has zlib headers and trailers or not). The following example decodes the DEFLATE encoded string "Hello, world" and prints it: ```rust use inflate::inflate_bytes; use std::str::from_utf8; let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; let decoded = inflate_bytes(&encoded).unwrap(); println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" ``` If you need more flexibility, then the library also provides an implementation of `std::io::Writer` in `inflate::writer`. Below is an example using an `inflate::writer::InflateWriter` to decode the DEFLATE encoded string "Hello, world": ```rust use inflate::InflateWriter; use std::io::Write; use std::str::from_utf8; let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; let mut decoder = InflateWriter::new(Vec::new()); decoder.write(&encoded).unwrap(); let decoded = decoder.finish().unwrap(); println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" ``` Finally, if you need even more flexibility, or if you only want to depend on `core`, you can use the `inflate::InflateStream` API. The below example decodes an array of DEFLATE encoded bytes: ```rust use inflate::InflateStream; let data = [0x73, 0x49, 0x4d, 0xcb, 0x49, 0x2c, 0x49, 0x55, 0x00, 0x11, 0x00]; let mut inflater = InflateStream::new(); let mut out = Vec::::new(); let mut n = 0; while n < data.len() { let res = inflater.update(&data[n..]); if let Ok((num_bytes_read, result)) = res { n += num_bytes_read; out.extend(result.iter().cloned()); } else { res.unwrap(); } } ``` inflate-0.4.5/src/checksum.rs010064400007650000024000000040761343106754200143340ustar0000000000000000use adler32::RollingAdler32; pub fn adler32_from_bytes(bytes: &[u8; 4]) -> u32 { (bytes[3] as u32) | ((bytes[2] as u32) << 8) | ((bytes[1] as u32) << 16) | ((bytes[0] as u32) << 24) } /// Whether we should validate the checksum, and what type of checksum it is. pub enum ChecksumType { /// No validation. /// /// For raw deflate streams or when we don't bother checking. None, /// Adler32 /// /// Used in the zlib format. Adler32(RollingAdler32), } pub struct Checksum { checksum_type: ChecksumType, } impl Checksum { #[inline] pub fn none() -> Checksum { Checksum::new(ChecksumType::None) } #[inline] pub fn is_none(&self) -> bool { match self.checksum_type { ChecksumType::None => true, _ => false, } } #[inline] pub fn zlib() -> Checksum { Checksum::new(ChecksumType::Adler32(RollingAdler32::new())) } pub fn new(checksum_type: ChecksumType) -> Checksum { Checksum { checksum_type: checksum_type, } } #[inline] pub fn update(&mut self, bytes: &[u8]) { match self.checksum_type { ChecksumType::None => (), ChecksumType::Adler32(ref mut c) => { c.update_buffer(bytes); } } } pub fn check(&self, expected: u32) -> Result<(), String> { match self.checksum_type { ChecksumType::None => Ok(()), ChecksumType::Adler32(ref c) => { if c.hash() == expected { Ok(()) } else { Err("Checksum mismatch!".to_owned()) } }, } } #[inline] pub fn current_value(&self) -> u32 { match self.checksum_type { ChecksumType::Adler32(ref c) => c.hash(), _ => 0, } } } #[cfg(test)] mod test { use super::adler32_from_bytes; #[test] fn adler32() { let bytes = [0x00, 0x00, 0x01, 0x0b]; assert_eq!(adler32_from_bytes(&bytes), 267); } } inflate-0.4.5/src/lib.rs010064400007650000024000001224161343106754200132770ustar0000000000000000// Copyright 2014-2015 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! A [DEFLATE](http://www.gzip.org/zlib/rfc-deflate.html) decoder written in rust. //! //! This library provides functionality to decompress data compressed with the DEFLATE algorithm, //! both with and without a [zlib](https://tools.ietf.org/html/rfc1950) header/trailer. //! //! # Examples //! The easiest way to get `std::Vec` containing the decompressed bytes is to use either //! `inflate::inflate_bytes` or `inflate::inflate_bytes_zlib` (depending on whether //! the encoded data has zlib headers and trailers or not). The following example //! decodes the DEFLATE encoded string "Hello, world" and prints it: //! //! ```rust //! use inflate::inflate_bytes; //! use std::str::from_utf8; //! //! let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; //! let decoded = inflate_bytes(&encoded).unwrap(); //! println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" //! ``` //! //! If you need more flexibility, then the library also provides an implementation //! of `std::io::Writer` in `inflate::writer`. Below is an example using an //! `inflate::writer::InflateWriter` to decode the DEFLATE encoded string "Hello, world": //! //! ```rust //! use inflate::InflateWriter; //! use std::io::Write; //! use std::str::from_utf8; //! //! let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; //! let mut decoder = InflateWriter::new(Vec::new()); //! decoder.write(&encoded).unwrap(); //! let decoded = decoder.finish().unwrap(); //! println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" //! ``` //! //! Finally, if you need even more flexibility, or if you only want to depend on //! `core`, you can use the `inflate::InflateStream` API. The below example //! decodes an array of DEFLATE encoded bytes: //! //! ```rust //! use inflate::InflateStream; //! //! let data = [0x73, 0x49, 0x4d, 0xcb, 0x49, 0x2c, 0x49, 0x55, 0x00, 0x11, 0x00]; //! let mut inflater = InflateStream::new(); //! let mut out = Vec::::new(); //! let mut n = 0; //! while n < data.len() { //! let res = inflater.update(&data[n..]); //! if let Ok((num_bytes_read, result)) = res { //! n += num_bytes_read; //! out.extend(result.iter().cloned()); //! } else { //! res.unwrap(); //! } //! } //! ``` extern crate adler32; use std::cmp; use std::slice; mod checksum; use checksum::{Checksum, adler32_from_bytes}; mod writer; pub use self::writer::{InflateWriter}; mod utils; pub use self::utils::{inflate_bytes, inflate_bytes_zlib, inflate_bytes_zlib_no_checksum}; mod reader; pub use self::reader::{DeflateDecoder, DeflateDecoderBuf}; static BIT_REV_U8: [u8; 256] = [ 0b0000_0000, 0b1000_0000, 0b0100_0000, 0b1100_0000, 0b0010_0000, 0b1010_0000, 0b0110_0000, 0b1110_0000, 0b0001_0000, 0b1001_0000, 0b0101_0000, 0b1101_0000, 0b0011_0000, 0b1011_0000, 0b0111_0000, 0b1111_0000, 0b0000_1000, 0b1000_1000, 0b0100_1000, 0b1100_1000, 0b0010_1000, 0b1010_1000, 0b0110_1000, 0b1110_1000, 0b0001_1000, 0b1001_1000, 0b0101_1000, 0b1101_1000, 0b0011_1000, 0b1011_1000, 0b0111_1000, 0b1111_1000, 0b0000_0100, 0b1000_0100, 0b0100_0100, 0b1100_0100, 0b0010_0100, 0b1010_0100, 0b0110_0100, 0b1110_0100, 0b0001_0100, 0b1001_0100, 0b0101_0100, 0b1101_0100, 0b0011_0100, 0b1011_0100, 0b0111_0100, 0b1111_0100, 0b0000_1100, 0b1000_1100, 0b0100_1100, 0b1100_1100, 0b0010_1100, 0b1010_1100, 0b0110_1100, 0b1110_1100, 0b0001_1100, 0b1001_1100, 0b0101_1100, 0b1101_1100, 0b0011_1100, 0b1011_1100, 0b0111_1100, 0b1111_1100, 0b0000_0010, 0b1000_0010, 0b0100_0010, 0b1100_0010, 0b0010_0010, 0b1010_0010, 0b0110_0010, 0b1110_0010, 0b0001_0010, 0b1001_0010, 0b0101_0010, 0b1101_0010, 0b0011_0010, 0b1011_0010, 0b0111_0010, 0b1111_0010, 0b0000_1010, 0b1000_1010, 0b0100_1010, 0b1100_1010, 0b0010_1010, 0b1010_1010, 0b0110_1010, 0b1110_1010, 0b0001_1010, 0b1001_1010, 0b0101_1010, 0b1101_1010, 0b0011_1010, 0b1011_1010, 0b0111_1010, 0b1111_1010, 0b0000_0110, 0b1000_0110, 0b0100_0110, 0b1100_0110, 0b0010_0110, 0b1010_0110, 0b0110_0110, 0b1110_0110, 0b0001_0110, 0b1001_0110, 0b0101_0110, 0b1101_0110, 0b0011_0110, 0b1011_0110, 0b0111_0110, 0b1111_0110, 0b0000_1110, 0b1000_1110, 0b0100_1110, 0b1100_1110, 0b0010_1110, 0b1010_1110, 0b0110_1110, 0b1110_1110, 0b0001_1110, 0b1001_1110, 0b0101_1110, 0b1101_1110, 0b0011_1110, 0b1011_1110, 0b0111_1110, 0b1111_1110, 0b0000_0001, 0b1000_0001, 0b0100_0001, 0b1100_0001, 0b0010_0001, 0b1010_0001, 0b0110_0001, 0b1110_0001, 0b0001_0001, 0b1001_0001, 0b0101_0001, 0b1101_0001, 0b0011_0001, 0b1011_0001, 0b0111_0001, 0b1111_0001, 0b0000_1001, 0b1000_1001, 0b0100_1001, 0b1100_1001, 0b0010_1001, 0b1010_1001, 0b0110_1001, 0b1110_1001, 0b0001_1001, 0b1001_1001, 0b0101_1001, 0b1101_1001, 0b0011_1001, 0b1011_1001, 0b0111_1001, 0b1111_1001, 0b0000_0101, 0b1000_0101, 0b0100_0101, 0b1100_0101, 0b0010_0101, 0b1010_0101, 0b0110_0101, 0b1110_0101, 0b0001_0101, 0b1001_0101, 0b0101_0101, 0b1101_0101, 0b0011_0101, 0b1011_0101, 0b0111_0101, 0b1111_0101, 0b0000_1101, 0b1000_1101, 0b0100_1101, 0b1100_1101, 0b0010_1101, 0b1010_1101, 0b0110_1101, 0b1110_1101, 0b0001_1101, 0b1001_1101, 0b0101_1101, 0b1101_1101, 0b0011_1101, 0b1011_1101, 0b0111_1101, 0b1111_1101, 0b0000_0011, 0b1000_0011, 0b0100_0011, 0b1100_0011, 0b0010_0011, 0b1010_0011, 0b0110_0011, 0b1110_0011, 0b0001_0011, 0b1001_0011, 0b0101_0011, 0b1101_0011, 0b0011_0011, 0b1011_0011, 0b0111_0011, 0b1111_0011, 0b0000_1011, 0b1000_1011, 0b0100_1011, 0b1100_1011, 0b0010_1011, 0b1010_1011, 0b0110_1011, 0b1110_1011, 0b0001_1011, 0b1001_1011, 0b0101_1011, 0b1101_1011, 0b0011_1011, 0b1011_1011, 0b0111_1011, 0b1111_1011, 0b0000_0111, 0b1000_0111, 0b0100_0111, 0b1100_0111, 0b0010_0111, 0b1010_0111, 0b0110_0111, 0b1110_0111, 0b0001_0111, 0b1001_0111, 0b0101_0111, 0b1101_0111, 0b0011_0111, 0b1011_0111, 0b0111_0111, 0b1111_0111, 0b0000_1111, 0b1000_1111, 0b0100_1111, 0b1100_1111, 0b0010_1111, 0b1010_1111, 0b0110_1111, 0b1110_1111, 0b0001_1111, 0b1001_1111, 0b0101_1111, 0b1101_1111, 0b0011_1111, 0b1011_1111, 0b0111_1111, 0b1111_1111 ]; #[derive(Clone, Copy)] struct BitState { n: u8, v: u32, } #[derive(Clone)] struct BitStream<'a> { bytes: slice::Iter<'a, u8>, used: usize, state: BitState, } #[cfg(debug)] macro_rules! debug { ($($x:tt)*) => (println!($($x)*)) } #[cfg(not(debug))] macro_rules! debug { ($($x:tt)*) => (()) } impl<'a> BitStream<'a> { fn new(bytes: &'a [u8], state: BitState) -> BitStream<'a> { BitStream { bytes: bytes.iter(), used: 0, state: state, } } fn use_byte(&mut self) -> bool { match self.bytes.next() { Some(&b) => { self.state.v |= (b as u32) << self.state.n; self.state.n += 8; self.used += 1; true } None => false, } } fn need(&mut self, n: u8) -> bool { if self.state.n < n { if !self.use_byte() { return false; } if n > 8 && self.state.n < n { assert!(n <= 16); if !self.use_byte() { return false; } } } true } fn take16(&mut self, n: u8) -> Option { if self.need(n) { self.state.n -= n; let v = self.state.v & ((1 << n) - 1); self.state.v >>= n; Some(v as u16) } else { None } } fn take(&mut self, n: u8) -> Option { assert!(n <= 8); self.take16(n).map(|v: u16| v as u8) } fn fill(&mut self) -> BitState { while self.state.n + 8 <= 32 && self.use_byte() {} self.state } fn align_byte(&mut self) { if self.state.n > 0 { let n = self.state.n % 8; self.take(n); } } fn trailing_bytes(&mut self) -> (u8, [u8; 4]) { let mut len = 0; let mut bytes = [0; 4]; self.align_byte(); while self.state.n >= 8 { bytes[len as usize] = self.state.v as u8; len += 1; self.state.n -= 8; self.state.v >>= 8; } (len, bytes) } } /// Generate huffman codes from the given set of lengths and run `$cb` on them except the first /// code for each length. /// /// See also the [deflate specification](http://www.gzip.org/zlib/rfc-deflate.html#huffman) /// for an explanation of the algorithm. macro_rules! with_codes (($clens:expr, $max_bits:expr => $code_ty:ty, $cb:expr) => ({ // Count the number of codes for each bit length. let mut bl_count = [0 as $code_ty; ($max_bits+1)]; for &bits in $clens.iter() { if bits != 0 { // This should be safe from overflow as the number of lengths read from the input // is bounded by the number of bits the number of lengths is represented by in the // deflate compressed data. bl_count[bits as usize] += 1; } } // Compute the first code value for each bit length. let mut next_code = [0 as $code_ty; ($max_bits+1)]; let mut code = 0 as $code_ty; // TODO use range_inclusive as soon as it is stable //for bits in range_inclusive(1, $max_bits) { for bits in 1..$max_bits + 1 { code = try!( code.checked_add(bl_count[bits as usize - 1]) .ok_or_else(|| "Error generating huffman codes: Invalid set of code lengths") ) << 1; next_code[bits as usize] = code; } // Compute the rest of the codes for (i, &bits) in $clens.iter().enumerate() { if bits != 0 { let code = next_code[bits as usize]; // If there is an overflow here, the given set of code lengths won't allow enough // unique codes to be generated. let new_code = try!( code.checked_add(1) .ok_or_else(|| "Error generating huffman codes: Invalid set of code lengths!") ); next_code[bits as usize] = new_code; match $cb(i as $code_ty, code, bits) { Ok(()) => (), Err(err) => return Err(err) } } } })); struct CodeLengthReader { patterns: Box<[u8; 128]>, clens: Box<[u8; 19]>, result: Vec, num_lit: u16, num_dist: u8, } impl CodeLengthReader { fn new(clens: Box<[u8; 19]>, num_lit: u16, num_dist: u8) -> Result { // Fill in the 7-bit patterns that match each code. let mut patterns = Box::new([0xffu8; 128]); with_codes!(clens, 7 => u8, |i: u8, code: u8, bits| -> _ { /*let base = match BIT_REV_U8.get((code << (8 - bits)) as usize) { Some(&base) => base, None => return Err("invalid length code".to_owned()) }*/ let base = BIT_REV_U8[(code << (8 - bits)) as usize]; for rest in 0u8 .. 1u8 << (7 - bits) { patterns[(base | (rest << bits)) as usize] = i; } Ok(()) }); Ok(CodeLengthReader { patterns: patterns, clens: clens, result: Vec::with_capacity(num_lit as usize + num_dist as usize), num_lit: num_lit, num_dist: num_dist, }) } fn read(&mut self, stream: &mut BitStream) -> Result { let total_len = self.num_lit as usize + self.num_dist as usize; while self.result.len() < total_len { if !stream.need(7) { return Ok(false); } let save = stream.clone(); macro_rules! take (($n:expr) => (match stream.take($n) { Some(v) => v, None => { *stream = save; return Ok(false); } })); let code = self.patterns[(stream.state.v & 0x7f) as usize]; stream.take(match self.clens.get(code as usize) { Some(&len) => len, None => return Err("invalid length code".to_owned()), }); match code { 0...15 => self.result.push(code), 16 => { let last = match self.result.last() { Some(&v) => v, // 16 appeared before anything else None => return Err("invalid length code".to_owned()), }; for _ in 0..3 + take!(2) { self.result.push(last); } } 17 => { for _ in 0..3 + take!(3) { self.result.push(0); } } 18 => { for _ in 0..11 + take!(7) { self.result.push(0); } } _ => unreachable!(), } } Ok(true) } fn to_lit_and_dist(&self) -> Result<(DynHuffman16, DynHuffman16), String> { let num_lit = self.num_lit as usize; let lit = try!(DynHuffman16::new(&self.result[..num_lit])); let dist = try!(DynHuffman16::new(&self.result[num_lit..])); Ok((lit, dist)) } } struct Trie8bit { data: [T; 16], children: [Option>; 16], } struct DynHuffman16 { patterns: Box<[u16; 256]>, rest: Vec>, } impl DynHuffman16 { fn new(clens: &[u8]) -> Result { // Fill in the 8-bit patterns that match each code. // Longer patterns go into the trie. let mut patterns = Box::new([0xffffu16; 256]); let mut rest = Vec::new(); with_codes!(clens, 15 => u16, |i: u16, code: u16, bits: u8| -> _ { let entry = i | ((bits as u16) << 12); if bits <= 8 { let base = match BIT_REV_U8.get((code << (8 - bits)) as usize) { Some(&v) => v, None => return Err("invalid length code".to_owned()) }; for rest in 0u8 .. 1 << (8 - bits) { patterns[(base | (rest << (bits & 7))) as usize] = entry; } } else { let low = match BIT_REV_U8.get((code >> (bits - 8)) as usize) { Some(&v) => v, None => return Err("invalid length code".to_owned()) }; let high = BIT_REV_U8[((code << (16 - bits)) & 0xff) as usize]; let (min_bits, idx) = if patterns[low as usize] != 0xffff { let bits_prev = (patterns[low as usize] >> 12) as u8; (cmp::min(bits_prev, bits), patterns[low as usize] & 0x7ff) } else { rest.push(Trie8bit { data: [0xffff; 16], children: [ None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None ] }); (bits, (rest.len() - 1) as u16) }; patterns[low as usize] = idx | 0x800 | ((min_bits as u16) << 12); let trie_entry = match rest.get_mut(idx as usize) { Some(v) => v, None => return Err("invalid huffman code".to_owned()) }; if bits <= 12 { for rest in 0u8 .. 1 << (12 - bits) { trie_entry.data[(high | (rest << (bits - 8))) as usize] = entry; } } else { let child = &mut trie_entry.children[(high & 0xf) as usize]; if child.is_none() { *child = Some(Box::new([0xffff; 16])); } let child = &mut **child.as_mut().unwrap(); let high_top = high >> 4; for rest in 0u8 .. 1 << (16 - bits) { child[(high_top | (rest << (bits - 12))) as usize] = entry; } } } Ok(()) }); debug!("=== DYN HUFFMAN ==="); for _i in 0..256 { debug!("{:08b} {:04x}", _i, patterns[BIT_REV_U8[_i] as usize]); } debug!("==================="); Ok(DynHuffman16 { patterns: patterns, rest: rest, }) } fn read<'a>(&self, stream: &mut BitStream<'a>) -> Result, u16)>, String> { let has8 = stream.need(8); let entry = self.patterns[(stream.state.v & 0xff) as usize]; let bits = (entry >> 12) as u8; Ok(if !has8 { if bits <= stream.state.n { let save = stream.clone(); stream.state.n -= bits; stream.state.v >>= bits; Some((save, entry & 0xfff)) } else { None } } else if bits <= 8 { let save = stream.clone(); stream.state.n -= bits; stream.state.v >>= bits; Some((save, entry & 0xfff)) } else { let has16 = stream.need(16); let trie = match self.rest.get((entry & 0x7ff) as usize) { Some(trie) => trie, None => return Err("invalid entry in stream".to_owned()), }; let idx = stream.state.v >> 8; let trie_entry = match trie.children[(idx & 0xf) as usize] { Some(ref child) => child[((idx >> 4) & 0xf) as usize], None => trie.data[(idx & 0xf) as usize], }; let trie_bits = (trie_entry >> 12) as u8; if has16 || trie_bits <= stream.state.n { let save = stream.clone(); stream.state.n -= trie_bits; stream.state.v >>= trie_bits; Some((save, trie_entry & 0xfff)) } else { None } }) } } enum State { ZlibMethodAndFlags, // CMF ZlibFlags(/* CMF */ u8), // FLG, Bits(BitsNext, BitState), LenDist((BitsNext, BitState), /* len */ u16, /* dist */ u16), Uncompressed(/* len */ u16), CheckCRC(/* len */ u8, /* bytes */ [u8; 4]), Finished } use self::State::*; enum BitsNext { BlockHeader, BlockUncompressedLen, BlockUncompressedNlen(/* len */ u16), BlockDynHlit, BlockDynHdist(/* hlit */ u8), BlockDynHclen(/* hlit */ u8, /* hdist */ u8), BlockDynClenCodeLengths(/* hlit */ u8, /* hdist */ u8, /* hclen */ u8, /* idx */ u8, /* clens */ Box<[u8; 19]>), BlockDynCodeLengths(CodeLengthReader), BlockDyn(/* lit/len */ DynHuffman16, /* dist */ DynHuffman16, /* prev_len */ u16) } use self::BitsNext::*; pub struct InflateStream { buffer: Vec, pos: u16, state: Option, final_block: bool, checksum: Checksum, read_checksum: Option, } impl InflateStream { #[allow(dead_code)] /// Create a new stream for decoding raw deflate encoded data. pub fn new() -> InflateStream { let state = Bits(BlockHeader, BitState { n: 0, v: 0 }); let buffer = Vec::with_capacity(32 * 1024); InflateStream::with_state_and_buffer(state, buffer, Checksum::none()) } /// Create a new stream for decoding deflate encoded data with a zlib header and footer pub fn from_zlib() -> InflateStream { InflateStream::with_state_and_buffer(ZlibMethodAndFlags, Vec::new(), Checksum::zlib()) } /// Create a new stream for decoding deflate encoded data with a zlib header and footer /// /// This version creates a decoder that does not checksum the data to validate it with the /// checksum provided with the zlib wrapper. pub fn from_zlib_no_checksum() -> InflateStream { InflateStream::with_state_and_buffer(ZlibMethodAndFlags, Vec::new(), Checksum::none()) } pub fn reset(&mut self) { self.buffer.clear(); self.pos = 0; self.state = Some(Bits(BlockHeader, BitState { n: 0, v: 0 })); self.final_block = false; } pub fn reset_to_zlib(&mut self) { self.reset(); self.state = Some(ZlibMethodAndFlags); } fn with_state_and_buffer(state: State, buffer: Vec, checksum: Checksum) -> InflateStream { InflateStream { buffer: buffer, pos: 0, state: Some(state), final_block: false, checksum: checksum, read_checksum: None, } } fn run_len_dist(&mut self, len: u16, dist: u16) -> Result, String> { debug!("RLE -{}; {} (cap={} len={})", dist, len, self.buffer.capacity(), self.buffer.len()); if dist < 1 { return Err("invalid run length in stream".to_owned()); } // `buffer_size` is used for validating `unsafe` below, handle with care let buffer_size = self.buffer.capacity() as u16; let len = if self.pos < dist { // Handle copying from ahead, until we hit the end reading. let pos_end = self.pos + len; let (pos_end, left) = if pos_end < dist { (pos_end, 0) } else { (dist, pos_end - dist) }; if dist > buffer_size { return Err("run length distance is bigger than the window size".to_owned()); } let forward = buffer_size - dist; if pos_end + forward > self.buffer.len() as u16 { return Err("invalid run length in stream".to_owned()); } for i in self.pos as usize..pos_end as usize { self.buffer[i] = self.buffer[i + forward as usize]; } self.pos = pos_end; left } else { len }; // Handle copying from before, until we hit the end writing. let pos_end = self.pos + len; let (pos_end, left) = if pos_end <= buffer_size { (pos_end, None) } else { (buffer_size, Some(pos_end - buffer_size)) }; if self.pos < dist && pos_end > self.pos { return Err("invalid run length in stream".to_owned()); } if self.buffer.len() < pos_end as usize { // ensure the buffer length will not exceed the amount of allocated memory assert!(pos_end <= buffer_size); // ensure that the uninitialized chunk of memory will be fully overwritten assert!(self.pos as usize <= self.buffer.len()); unsafe { self.buffer.set_len(pos_end as usize); } } assert!(dist > 0); // validation against reading uninitialized memory for i in self.pos as usize..pos_end as usize { self.buffer[i] = self.buffer[i - dist as usize]; } self.pos = pos_end; Ok(left) } fn next_state(&mut self, data: &[u8]) -> Result { macro_rules! ok_bytes (($n:expr, $state:expr) => ({ self.state = Some($state); Ok($n) })); let debug_byte = |_i, _b| debug!("[{:04x}] {:02x}", _i, _b); macro_rules! push_or (($b:expr, $ret:expr) => (if self.pos < self.buffer.capacity() as u16 { let b = $b; debug_byte(self.pos, b); if (self.pos as usize) < self.buffer.len() { self.buffer[self.pos as usize] = b; } else { assert_eq!(self.pos as usize, self.buffer.len()); self.buffer.push(b); } self.pos += 1; } else { return $ret; })); macro_rules! run_len_dist (($len:expr, $dist:expr => ($bytes:expr, $next:expr, $state:expr)) => ({ let dist = $dist; let left = try!(self.run_len_dist($len, dist)); if let Some(len) = left { return ok_bytes!($bytes, LenDist(($next, $state), len, dist)); } })); match self.state.take().unwrap() { ZlibMethodAndFlags => { let b = match data.get(0) { Some(&x) => x, None => { self.state = Some(ZlibMethodAndFlags); return Ok(0); } }; let (method, info) = (b & 0xF, b >> 4); debug!("ZLIB CM=0x{:x} CINFO=0x{:x}", method, info); // CM = 8 (DEFLATE) is the only method defined by the ZLIB specification. match method { 8 => {/* DEFLATE */} _ => return Err(format!("unknown ZLIB method CM=0x{:x}", method)) } if info > 7 { return Err(format!("invalid ZLIB info CINFO=0x{:x}", info)); } self.buffer = Vec::with_capacity(1 << (8 + info)); ok_bytes!(1, ZlibFlags(b)) } ZlibFlags(cmf) => { let b = match data.get(0) { Some(&x) => x, None => { self.state = Some(ZlibFlags(cmf)); return Ok(0); } }; let (_check, dict, _level) = (b & 0x1F, (b & 0x20) != 0, b >> 6); debug!("ZLIB FCHECK=0x{:x} FDICT={} FLEVEL=0x{:x}", _check, dict, _level); if (((cmf as u16) << 8) | b as u16) % 31 != 0 { return Err(format!("invalid ZLIB checksum CMF=0x{:x} FLG=0x{:x}", cmf, b)); } if dict { return Err("unimplemented ZLIB FDICT=1".into()); } ok_bytes!(1, Bits(BlockHeader, BitState { n: 0, v: 0 })) } Bits(next, state) => { let mut stream = BitStream::new(data, state); macro_rules! ok_state (($state:expr) => ({self.state = Some($state); Ok(stream.used)})); macro_rules! ok (($next:expr) => (ok_state!(Bits($next, stream.fill())))); macro_rules! take ( ($n:expr => $next:expr) => (match stream.take($n) { Some(v) => v, None => return ok!($next) }); ($n:expr) => (take!($n => next)) ); macro_rules! take16 ( ($n:expr => $next:expr) => (match stream.take16($n) { Some(v) => v, None => return ok!($next) }); ($n:expr) => (take16!($n => next)) ); macro_rules! len_dist ( ($len:expr, $code:expr, $bits:expr => $next_early:expr, $next:expr) => ({ let dist = 1 + if $bits == 0 { 0 } else { // new_base 2 << $bits } + (($code as u16 - if $bits == 0 { 0 } else { // old_base $bits * 2 + 2 }) << $bits) + take16!($bits => $next_early) as u16; run_len_dist!($len, dist => (stream.used, $next, stream.state)); }); ($len:expr, $code:expr, $bits:expr) => ( len_dist!($len, $code, $bits => next, next) ) ); match next { BlockHeader => { if self.final_block { let (len, bytes) = stream.trailing_bytes(); return ok_state!(CheckCRC(len, bytes)); } let h = take!(3); let (final_, block_type) = ((h & 1) != 0, (h >> 1) & 0b11); self.final_block = final_; match block_type { 0 => { // Skip to the next byte for an uncompressed block. stream.align_byte(); ok!(BlockUncompressedLen) } 1 => { // Unwrap is safe because the data is valid. let lit = DynHuffman16::new(&[ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 0-15 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 16-31 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 32-47 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 48-63 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 64-79 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 80-95 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 96-101 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 112-127 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 128-143 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 144-159 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 160-175 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 176-191 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 192-207 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 208-223 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 224-239 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 240-255 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 256-271 7, 7, 7, 7, 7, 7, 7, 7, // 272-279 8, 8, 8, 8, 8, 8, 8, 8, // 280-287 ]).unwrap(); let dist = DynHuffman16::new(&[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ]).unwrap(); ok!(BlockDyn(lit, dist, 0)) } 2 => ok!(BlockDynHlit), _ => { Err(format!("unimplemented DEFLATE block type 0b{:?}", block_type)) } } } BlockUncompressedLen => { let len = take16!(16); ok_state!(Bits(BlockUncompressedNlen(len), stream.state)) } BlockUncompressedNlen(len) => { let nlen = take16!(16); assert_eq!(stream.state.n, 0); if !len != nlen { return Err(format!("invalid uncompressed block len: LEN: {:04x} NLEN: {:04x}", len, nlen)); } ok_state!(Uncompressed(len)) } BlockDynHlit => ok!(BlockDynHdist(take!(5) + 1)), BlockDynHdist(hlit) => ok!(BlockDynHclen(hlit, take!(5) + 1)), BlockDynHclen(hlit, hdist) => { ok!(BlockDynClenCodeLengths(hlit, hdist, take!(4) + 4, 0, Box::new([0; 19]))) } BlockDynClenCodeLengths(hlit, hdist, hclen, i, mut clens) => { let v = match stream.take(3) { Some(v) => v, None => return ok!(BlockDynClenCodeLengths(hlit, hdist, hclen, i, clens)), }; clens[[16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15][i as usize]] = v; if i < hclen - 1 { ok!(BlockDynClenCodeLengths(hlit, hdist, hclen, i + 1, clens)) } else { ok!(BlockDynCodeLengths(try!(CodeLengthReader::new(clens, hlit as u16 + 256, hdist)))) } } BlockDynCodeLengths(mut reader) => { let finished = try!(reader.read(&mut stream)); if finished { let (lit, dist) = try!(reader.to_lit_and_dist()); ok!(BlockDyn(lit, dist, 0)) } else { ok!(BlockDynCodeLengths(reader)) } } BlockDyn(huff_lit_len, huff_dist, mut prev_len) => { macro_rules! next (($save_len:expr) => (BlockDyn(huff_lit_len, huff_dist, $save_len))); loop { let len = if prev_len != 0 { let len = prev_len; prev_len = 0; len } else { let (save, code16) = match try!(huff_lit_len.read(&mut stream)) { Some(data) => data, None => return ok!(next!(0)), }; let code = code16 as u8; debug!("{:09b}", code16); match code16 { 0...255 => { push_or!(code, ok!({stream = save; next!(0)})); continue; } 256...285 => {} _ => return Err(format!("bad DEFLATE len code {}", code)), } macro_rules! len (($code:expr, $bits:expr) => ( 3 + if $bits == 0 { 0 } else { // new_base 4 << $bits } + ((if $code == 29 { 256 } else { $code as u16 } - if $bits == 0 { 0 } else { // old_base $bits * 4 + 4 } - 1) << $bits) + take!($bits => {stream = save; next!(0)}) as u16 )); match code { 0 => { return if self.final_block { let (len, bytes) = stream.trailing_bytes(); ok_state!(CheckCRC(len, bytes)) } else { ok!(BlockHeader) } } 1...8 => len!(code, 0), 9...12 => len!(code, 1), 13...16 => len!(code, 2), 17...20 => len!(code, 3), 21...24 => len!(code, 4), 25...28 => len!(code, 5), 29 => len!(29, 0), _ => return Err(format!("bad DEFLATE len code {}", code as u16 + 256)), } }; let (save, dist_code) = match try!(huff_dist.read(&mut stream)) { Some(data) => data, None => return ok!(next!(len)), }; debug!(" {:05b}", dist_code); macro_rules! len_dist_case (($bits:expr) => ( len_dist!(len, dist_code, $bits => {stream = save; next!(len)}, next!(0)) )); match dist_code { 0...3 => len_dist_case!(0), 4...5 => len_dist_case!(1), 6...7 => len_dist_case!(2), 8...9 => len_dist_case!(3), 10...11 => len_dist_case!(4), 12...13 => len_dist_case!(5), 14...15 => len_dist_case!(6), 16...17 => len_dist_case!(7), 18...19 => len_dist_case!(8), 20...21 => len_dist_case!(9), 22...23 => len_dist_case!(10), 24...25 => len_dist_case!(11), 26...27 => len_dist_case!(12), 28...29 => len_dist_case!(13), _ => return Err(format!("bad DEFLATE dist code {}", dist_code)), } } } } } LenDist((next, state), len, dist) => { run_len_dist!(len, dist => (0, next, state)); ok_bytes!(0, Bits(next, state)) } Uncompressed(mut len) => { for (i, &b) in data.iter().enumerate() { if len == 0 { return ok_bytes!(i, Bits(BlockHeader, BitState { n: 0, v: 0 })); } push_or!(b, ok_bytes!(i, Uncompressed(len))); len -= 1; } ok_bytes!(data.len(), Uncompressed(len)) } CheckCRC(mut len, mut bytes) => { if self.checksum.is_none() { // TODO: inform caller of unused bytes return ok_bytes!(0, Finished); } // Get the checksum value from the end of the stream. let mut used = 0; while len < 4 && used < data.len() { bytes[len as usize] = data[used]; len += 1; used += 1; } if len < 4 { return ok_bytes!(used, CheckCRC(len, bytes)); } self.read_checksum = Some(adler32_from_bytes(&bytes)); ok_bytes!(used, Finished) } Finished => { // TODO: inform caller of unused bytes ok_bytes!(data.len(), Finished) } } } /// Try to uncompress/decode the data in `data`. /// /// On success, returns how many bytes of the input data was decompressed, and a reference to /// the buffer containing the decompressed data. /// /// This function may not uncompress all the provided data in one call, so it has to be called /// repeatedly with the data that hasn't been decompressed yet as an input until the number of /// bytes decoded returned is 0. (See the [top level crate documentation](index.html) /// for an example.) /// /// # Errors /// If invalid input data is encountered, a string describing what went wrong is returned. pub fn update<'a>(&'a mut self, mut data: &[u8]) -> Result<(usize, &'a [u8]), String> { let original_size = data.len(); let original_pos = self.pos as usize; let mut empty = false; while !empty && ((self.pos as usize) < self.buffer.capacity() || self.buffer.capacity() == 0) { // next_state must be called at least once after the data is empty. empty = data.is_empty(); match self.next_state(data) { Ok(n) => { data = &data[n..]; } Err(m) => return Err(m), } } let output = &self.buffer[original_pos..self.pos as usize]; if self.pos as usize >= self.buffer.capacity() { self.pos = 0; } // Update the checksum.. self.checksum.update(output); // and validate if we are done decoding. if let Some(c) = self.read_checksum { try!(self.checksum.check(c)); } Ok((original_size - data.len(), output)) } /// Returns the calculated checksum value of the currently decoded data. /// /// Will return 0 for cases where the checksum is not validated. pub fn current_checksum(&self) -> u32 { self.checksum.current_value() } } inflate-0.4.5/src/reader.rs010064400007650000024000000300121343106754200137610ustar0000000000000000use std::io::{self, BufRead, Read, BufReader, Error, ErrorKind, Write}; use std::{cmp,mem}; use super::InflateStream; /// Workaround for lack of copy_from_slice on pre-1.9 rust. #[inline] fn copy_from_slice(mut to: &mut [u8], from: &[u8]) { assert_eq!(to.len(), from.len()); to.write_all(from).unwrap(); } /// A DEFLATE decoder/decompressor. /// /// This structure implements a `Read` interface and takes a stream /// of compressed data that implements the `BufRead` trait as input, /// providing the decompressed data when read from. /// /// # Example /// ``` /// use std::io::Read; /// use inflate::DeflateDecoderBuf; /// /// const TEST_STRING: &'static str = "Hello, world"; /// let encoded = vec![243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; /// let mut decoder = DeflateDecoderBuf::new(&encoded[..]); /// let mut output = Vec::new(); /// let status = decoder.read_to_end(&mut output); /// # let _ = status; /// assert_eq!(String::from_utf8(output).unwrap(), TEST_STRING); /// ``` pub struct DeflateDecoderBuf { /// The inner reader instance reader: R, /// The raw decompressor decompressor: InflateStream, /// How many bytes of the decompressor's output buffer still need to be output. pending_output_bytes: usize, /// Total number of bytes read from the underlying reader. total_in: u64, /// Total number of bytes written in `read` calls. total_out: u64, } impl DeflateDecoderBuf { /// Create a new `Deflatedecoderbuf` to read from a raw deflate stream. pub fn new(reader: R) -> DeflateDecoderBuf { DeflateDecoderBuf { reader: reader, decompressor: InflateStream::new(), pending_output_bytes: 0, total_in: 0, total_out: 0, } } /// Create a new `DeflateDecoderbuf` that reads from a zlib wrapped deflate stream. pub fn from_zlib(reader: R) -> DeflateDecoderBuf { DeflateDecoderBuf { reader: reader, decompressor: InflateStream::from_zlib(), pending_output_bytes: 0, total_in: 0, total_out: 0, } } /// Create a new `DeflateDecoderbuf` that reads from a zlib wrapped deflate stream. /// without calculating and validating the checksum. pub fn from_zlib_no_checksum(reader: R) -> DeflateDecoderBuf { DeflateDecoderBuf { reader: reader, decompressor: InflateStream::from_zlib_no_checksum(), pending_output_bytes: 0, total_in: 0, total_out: 0, } } } impl DeflateDecoderBuf { /// Resets the decompressor, and replaces the current inner `BufRead` instance by `r`. /// without doing any extra reallocations. /// /// Note that this function doesn't ensure that all data has been output. #[inline] pub fn reset(&mut self, r: R) -> R { self.decompressor.reset(); mem::replace(&mut self.reader, r) } /// Resets the decoder, but continue to read from the same reader. /// /// Note that this function doesn't ensure that all data has been output. #[inline] pub fn reset_data(&mut self) { self.decompressor.reset() } /// Returns a reference to the underlying `BufRead` instance. #[inline] pub fn get_ref(&self) -> &R { &self.reader } /// Returns a mutable reference to the underlying `BufRead` instance. /// /// Note that mutation of the reader may cause surprising results if the decoder is going to /// keep being used. #[inline] pub fn get_mut(&mut self) -> &mut R { &mut self.reader } /// Drops the decoder and return the inner `BufRead` instance. /// /// Note that this function doesn't ensure that all data has been output. #[inline] pub fn into_inner(self) -> R { self.reader } /// Returns the total bytes read from the underlying `BufRead` instance. #[inline] pub fn total_in(&self) -> u64 { self.total_in } /// Returns the total number of bytes output from this decoder. #[inline] pub fn total_out(&self) -> u64 { self.total_out } /// Returns the calculated checksum value of the currently decoded data. /// /// Will return 0 for cases where the checksum is not validated. #[inline] pub fn current_checksum(&self) -> u32 { self.decompressor.current_checksum() } } impl Read for DeflateDecoderBuf { fn read(&mut self, buf: &mut [u8]) -> io::Result { let mut bytes_out = 0; // If there is still data left to ouput from the last call to `update()`, that needs to be // output first if self.pending_output_bytes != 0 { // Get the part of the buffer that has not been output yet. // The decompressor sets `pos` to 0 when it reaches the end of it's internal buffer, // so we have to check for that. let start = if self.decompressor.pos != 0 { self.decompressor.pos as usize - self.pending_output_bytes } else { self.decompressor.buffer.len() - self.pending_output_bytes }; // Copy as much decompressed as possible to buf. let bytes_to_copy = cmp::min(buf.len(), self.pending_output_bytes); let pending_data = &self.decompressor.buffer[start.. start + bytes_to_copy]; copy_from_slice(&mut buf[..bytes_to_copy],pending_data); bytes_out += bytes_to_copy; // This won't underflow since `bytes_to_copy` will be at most // the same value as `pending_output_bytes`. self.pending_output_bytes -= bytes_to_copy; if self.pending_output_bytes != 0 { self.total_out += bytes_out as u64; // If there is still decompressed data left that didn't // fit in `buf`, return what we read. return Ok(bytes_out); } } // There is space in `buf` for more data, so try to read more. let (input_bytes_read, remaining_bytes) = { self.pending_output_bytes = 0; let input = try!(self.reader.fill_buf()); if input.len() == 0 { self.total_out += bytes_out as u64; //If there is nothing more to read, return. return Ok(bytes_out); } let (input_bytes_read, data) = match self.decompressor.update(&input) { Ok(res) => res, Err(m) => return Err(Error::new(ErrorKind::Other, m)) }; // Space left in `buf` let space_left = buf.len() - bytes_out; let bytes_to_copy = cmp::min(space_left, data.len()); copy_from_slice(&mut buf[bytes_out..bytes_out + bytes_to_copy], &data[..bytes_to_copy]); bytes_out += bytes_to_copy; // Can't underflow as bytes_to_copy is bounded by data.len(). (input_bytes_read, data.len() - bytes_to_copy) }; self.pending_output_bytes += remaining_bytes; self.total_in += input_bytes_read as u64; self.total_out += bytes_out as u64; self.reader.consume(input_bytes_read); Ok(bytes_out) } } /// A DEFLATE decoder/decompressor. /// /// This structure implements a `Read` interface and takes a stream of compressed data that /// implements the `Read` trait as input, /// provoding the decompressed data when read from. /// # Example /// ``` /// use std::io::Read; /// use inflate::DeflateDecoder; /// const TEST_STRING: &'static str = "Hello, world"; /// let encoded = vec![243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; /// let mut decoder = DeflateDecoder::new(&encoded[..]); /// let mut output = Vec::new(); /// let status = decoder.read_to_end(&mut output); /// # let _ = status; /// assert_eq!(String::from_utf8(output).unwrap(), TEST_STRING); /// ``` pub struct DeflateDecoder { /// Inner DeflateDecoderBuf, with R wrapped in a `BufReader`. inner: DeflateDecoderBuf> } impl DeflateDecoder { /// Create a new `Deflatedecoderbuf` to read from a raw deflate stream. pub fn new(reader: R) -> DeflateDecoder { DeflateDecoder { inner: DeflateDecoderBuf::new(BufReader::new(reader)) } } /// Create a new `DeflateDecoderbuf` that reads from a zlib wrapped deflate stream. pub fn from_zlib(reader: R) -> DeflateDecoder { DeflateDecoder { inner: DeflateDecoderBuf::from_zlib(BufReader::new(reader)) } } /// Create a new `DeflateDecoderbuf` that reads from a zlib wrapped deflate stream. /// without calculating and validating the checksum. pub fn from_zlib_no_checksum(reader: R) -> DeflateDecoder { DeflateDecoder { inner: DeflateDecoderBuf::from_zlib_no_checksum(BufReader::new(reader)) } } /// Resets the decompressor, and replaces the current inner `BufRead` instance by `r`. /// without doing any extra reallocations. /// /// Note that this function doesn't ensure that all data has been output. #[inline] pub fn reset(&mut self, r: R) -> R { self.inner.reset(BufReader::new(r)).into_inner() } /// Returns a reference to the underlying reader. #[inline] pub fn get_ref(&self) -> &R { self.inner.get_ref().get_ref() } /// Returns a mutable reference to the underlying reader. /// /// Note that mutation of the reader may cause surprising results if the decoder is going to /// keep being used. #[inline] pub fn get_mut(&mut self) -> &mut R { self.inner.get_mut().get_mut() } /// Returns the total number of bytes output from this decoder. #[inline] pub fn into_inner(self) -> R { self.inner.into_inner().into_inner() } } impl DeflateDecoder { /// Resets the decoder, but continue to read from the same reader. /// /// Note that this function doesn't ensure that all data has been output. #[inline] pub fn reset_data(&mut self) { self.inner.reset_data() } /// Returns the total bytes read from the underlying reader. #[inline] pub fn total_in(&self) -> u64 { self.inner.total_in } /// Returns the total number of bytes output from this decoder. #[inline] pub fn total_out(&self) -> u64 { self.inner.total_out } /// Returns the calculated checksum value of the currently decoded data. /// /// Will return 0 for cases where the checksum is not validated. #[inline] pub fn current_checksum(&self) -> u32 { self.inner.current_checksum() } } impl Read for DeflateDecoder { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result { self.inner.read(buf) } } #[cfg(test)] mod test { use super::{DeflateDecoder}; use std::io::Read; #[test] fn deflate_reader() { const TEST_STRING: &'static str = "Hello, world"; let encoded = vec![243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; let mut decoder = DeflateDecoder::new(&encoded[..]); let mut output = Vec::new(); decoder.read_to_end(&mut output).unwrap(); assert_eq!(String::from_utf8(output).unwrap(), TEST_STRING); assert_eq!(decoder.total_in(), encoded.len() as u64); assert_eq!(decoder.total_out(), TEST_STRING.len() as u64); } #[test] fn zlib_reader() { const TEST_STRING: &'static str = "Hello, zlib!"; let encoded = vec![120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201, 76, 82, 4, 0, 27, 101, 4, 19]; let mut decoder = DeflateDecoder::from_zlib(&encoded[..]); let mut output = Vec::new(); decoder.read_to_end(&mut output).unwrap(); assert_eq!(String::from_utf8(output).unwrap(), TEST_STRING); assert_eq!(decoder.total_in(), encoded.len() as u64); assert_eq!(decoder.total_out(), TEST_STRING.len() as u64); } } inflate-0.4.5/src/utils.rs010064400007650000024000000056371343106754200136760ustar0000000000000000/// Convenience functions for inflating DEFLATE compressed data. /// /// # Example /// ``` /// use inflate::inflate_bytes; /// use std::str::from_utf8; /// /// let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; /// let decoded = inflate_bytes(&encoded).unwrap(); /// println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" /// ``` use InflateStream; fn inflate(inflater: &mut InflateStream, data: &[u8]) -> Result, String> { let mut decoded = Vec::::new(); let mut n = 0; loop { let (num_bytes_read, bytes) = try!(inflater.update(&data[n..])); if bytes.len() == 0 { break; } n += num_bytes_read; decoded.extend(bytes.iter().map(|v| *v)); } Ok(decoded) } /// Decompress the given slice of DEFLATE compressed data. /// /// Returns a `Vec` with the decompressed data or an error message. pub fn inflate_bytes(data: &[u8]) -> Result, String> { inflate(&mut InflateStream::new(), data) } /// Decompress the given slice of DEFLATE compressed (with zlib headers and trailers) data. /// /// Returns a `Vec` with the decompressed data or an error message. pub fn inflate_bytes_zlib(data: &[u8]) -> Result, String> { inflate(&mut InflateStream::from_zlib(), data) } /// Decompress the given slice of DEFLATE compressed (with zlib headers and trailers) data, /// without calculating and validating the checksum. /// /// Returns a `Vec` with the decompressed data or an error message. pub fn inflate_bytes_zlib_no_checksum(data: &[u8]) -> Result, String> { inflate(&mut InflateStream::from_zlib_no_checksum(), data) } #[cfg(test)] mod test { #[test] fn inflate_bytes_with_zlib() { use super::inflate_bytes_zlib; use std::str::from_utf8; let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201, 76, 82, 4, 0, 27, 101, 4, 19]; let decoded = inflate_bytes_zlib(&encoded).unwrap(); assert!(from_utf8(&decoded).unwrap() == "Hello, zlib!"); } #[test] fn inflate_bytes_with_zlib_checksum_fail() { use super::inflate_bytes_zlib; // The last 4 bytes are the checksum, we set them to 0 here to check that decoding fails // if the checksum is wrong. let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201, 76, 82, 4, 0, 0, 0, 0, 0]; inflate_bytes_zlib(&encoded).unwrap_err(); } #[test] fn inflate_bytes_with_zlib_trailing() { use super::inflate_bytes_zlib; use std::str::from_utf8; // The additional 4 bytes should be ignored. let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201, 76, 82, 4, 0, 27, 101, 4, 19, 0, 0, 0, 0]; let decoded = inflate_bytes_zlib(&encoded).unwrap(); assert!(from_utf8(&decoded).unwrap() == "Hello, zlib!"); } } inflate-0.4.5/src/writer.rs010064400007650000024000000051521343106754200140420ustar0000000000000000use std::io::{Write, Error, ErrorKind}; use std::io; use InflateStream; /// A DEFLATE decoder. /// /// A struct implementing the `std::io::Write` trait that decompresses DEFLATE /// encoded data into the given writer `w`. /// /// # Example /// /// ``` /// use inflate::InflateWriter; /// use std::io::Write; /// /// let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; /// let mut decoder = InflateWriter::new(Vec::new()); /// decoder.write(&encoded).unwrap(); /// let decoded = decoder.finish().unwrap(); /// println!("{}", std::str::from_utf8(&decoded).unwrap()); // prints "Hello, world" /// ``` pub struct InflateWriter { inflater: InflateStream, writer: W } impl InflateWriter { pub fn new(w: W) -> InflateWriter { InflateWriter { inflater: InflateStream::new(), writer: w } } pub fn from_zlib(w: W) -> InflateWriter { InflateWriter { inflater: InflateStream::from_zlib(), writer: w } } pub fn finish(mut self) -> io::Result { try!(self.flush()); Ok(self.writer) } } fn update<'a>(inflater: &'a mut InflateStream, buf: &[u8]) -> io::Result<(usize, &'a [u8])> { match inflater.update(buf) { Ok(res) => Ok(res), Err(m) => return Err(Error::new(ErrorKind::Other, m)), } } impl Write for InflateWriter { fn write(&mut self, buf: &[u8]) -> io::Result { let mut n = 0; while n < buf.len() { let (num_bytes_read, result) = try!(update(&mut self.inflater, &buf[n..])); n += num_bytes_read; try!(self.writer.write(result)); } Ok(n) } fn flush(&mut self) -> io::Result<()> { let (_, result) = try!(update(&mut self.inflater, &[])); try!(self.writer.write(result)); Ok(()) } } #[cfg(test)] mod test { use super::InflateWriter; use std::io::Write; #[test] fn inflate_writer() { let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; let mut decoder = InflateWriter::new(Vec::new()); decoder.write(&encoded).unwrap(); let decoded = decoder.finish().unwrap(); assert!(String::from_utf8(decoded).unwrap() == "Hello, world"); } #[test] fn inflate_writer_from_zlib() { let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201, 76, 82, 4, 0, 27, 101, 4, 19]; let mut decoder = InflateWriter::from_zlib(Vec::new()); decoder.write(&encoded).unwrap(); let decoded = decoder.finish().unwrap(); assert!(String::from_utf8(decoded).unwrap() == "Hello, zlib!"); } } inflate-0.4.5/tests/issue_14.zlib010064400007650000024000000060531343106754200150520ustar0000000000000000H “č’óßó‰PNG  IHDR üķ£ IHDR üķ£fdMA± üabKGD’’’ ½§“ pHõ õõIMEį7*ŖæPkith GIMPd.‰PNG  IHDR üķ£gAMA±‰PNG  IHDR ķ£gAMA± üabKGD’’’ ½§“ pHõõõõõõõõõõõõõõõõõõõõY‰PNG  IHDRs  šœt IMEį7*ŖæPkiTXtCommentCreated with GIMPd.@d.@‰PNG  IHDR üķ£gAMA± üabKGD’’’ ½§“ pHõõõõõõõõõõõõõõõõõõõõY‰PNG  IHDRs  šœt šœt IMEį7*ŖæPkiTXtCommentCreatinflate-0.4.5/tests/test.rs010064400007650000024000000162721343106754200140650ustar0000000000000000extern crate inflate; fn get_test_file_data(name: &str) -> Vec { use std::fs::File; use std::io::Read; let mut input = Vec::new(); let mut f = File::open(name).unwrap(); f.read_to_end(&mut input).unwrap(); input } #[test] /// See https://github.com/PistonDevelopers/inflate/issues/14 fn issue_14() { let test_data = get_test_file_data("tests/issue_14.zlib"); let res = inflate::inflate_bytes_zlib(&test_data); // This should fail as the file specifies code lengths that won't work. assert!(res.is_err()); } #[test] /// Another input that produce invalid code lengths. fn issue_16() { let data = b"M\xff\xffM*\xad\xad\xad\xad\xad\xad\xad\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xad\xad\xad\xad\xad\xad\xad\xad\xad\xad\xad\xadMCMMMM\x00\x00\x00\x00\x00\x00\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd9\xcb!\x0a"; let mut stream = inflate::InflateStream::new(); let res = stream.update(data); assert!(res.is_err()); } #[test] /// Similar to 16 but with CLENS being invalid. fn issue_17() { let data = b"\xdd\xff\xff*M\x94ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\x01\x09\x00\x00\xf2\xf2MM\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00*M\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00MMMM\xff\xffM\xff\x00;MM0*\x00\x00\x00\x00\x00\x00\x0a"; let mut stream = inflate::InflateStream::new(); let res = stream.update(data); assert!(res.is_err()); } #[test] fn issue_30_realworld() { // In this case, the LZ77 window size is misreported as 256B. The first two bytes should be // e.g. [0x78, 0xda] to set the window size to a maximum value (32kiB). let data = &[ 0x8, 0xd7, 99, 100, 48, 158, 201, 128, 3, 196, 91, 158, 48, 231, 153, 127, 242, 75, 226, 194, 227, 22, 184, 212, 48, 51, 72, 249, 98, 213, 57, 47, 180, 153, 225, 195, 133, 148, 64, 230, 87, 247, 206, 53, 249, 111, 249, 201, 40, 122, 241, 137, 12, 166, 74, 70, 172, 246, 255, 154, 150, 149, 186, 56, 14, 110, 109, 188, 229, 137, 217, 177, 139, 216, 178, 166, 49, 50, 48, 252, 199, 175, 63, 222, 242, 68, 130, 238, 124, 199, 89, 88, 12, 221, 159, 150, 190, 224, 50, 186, 95, 152, 208, 20, 213, 56, 207, 95, 112, 57, 17, 171, 87, 23, 92, 78, 172, 113, 158, 143, 207, 255, 241, 150, 39, 126, 190, 190, 208, 118, 48, 19, 171, 254, 139, 79, 100, 52, 217, 183, 136, 136, 163, 4, 4, 138, 253, 230, 60, 243, 83, 2, 153, 25, 209, 253, 136, 0, 41, 129, 204, 230, 60, 40, 78, 96, 65, 230, 156, 252, 146, 200, 176, 126, 254, 127, 6, 70, 92, 209, 57, 103, 253, 223, 147, 95, 18, 113, 250, 127, 225, 113, 11, 87, 115, 134, 120, 203, 19, 88, 245, 31, 186, 165, 234, 106, 206, 64, 32, 252, 90, 246, 38, 38, 232, 206, 199, 170, 127, 94, 112, 85, 203, 222, 68, 2, 241, 207, 200, 192, 192, 192, 240, 255, 231, 180, 108, 204, 248, 103, 207, 154, 198, 128, 17, 255, 44, 104, 230, 253, 103, 96, 96, 96, 96, 76, 93, 28, 151, 160, 59, 31, 18, 156, 115, 214, 255, 213, 84, 100, 72, 93, 156, 136, 53, 84, 25, 41, 76, 255, 0, 17, 24, 113, 221]; inflate::inflate_bytes_zlib(data); } #[test] fn issue_30_small() { // Invalid data, but should not panic // generated by cargo-fuzz let data = &[0x08, 0xd7, 0x3a, 0xff, 0x3b, 0x8, 0xd7, 0xfd, 0xff, 0xff, 0xff]; inflate::inflate_bytes_zlib(data); } #[test] // no checksum present at the end of the data stream (cargo-fuzz test-case) fn no_checksum() { let data = b"\x13\xff\xed\xff\xff\x12\xbfM\x00\x00\x00\x00\xd1"; let mut stream = inflate::InflateStream::new(); let res = stream.update(data); // This is not an error, because the checksum may be included in the next // call to `stream.update`. See issue #27. assert!(res.is_ok()); } #[test] /// The first byte of the CRC is already read into the BitStream buffer. fn issue_26() { let data = &[120, 156, 189, 138, 65, 13, 0, 32, 16, 195, 64, 2, 22, 176, 128, 5, 44, 96, 1, 11, 216, 103, 19, 176, 123, 118, 73, 155, 61, 218, 155, 54, 10, 136, 192, 170, 32, 130, 41, 249, 36, 136, 96, 73, 62, 9, 34, 216, 146, 79, 130, 8, 142, 228, 147, 32, 130, 43, 249, 36, 136, 224, 73, 62, 9, 32, 248, 250, 192, 22, 113, 123]; let mut stream = inflate::InflateStream::from_zlib(); let res = stream.update(data); assert!(res.is_ok()); } #[test] /// InflateStream::update() should try to process bytes in the Bits state. fn issue_38_update_bits() { // compressed final block 011, literal 111111111=255, end 0000000 let data = &[0b1111_1011, 0b0000_1111, 0b0000_0000]; let bytes = inflate::inflate_bytes(data).unwrap(); assert_eq!(bytes.len(), 1); } #[test] /// InflateStream::update() should try to process bytes in the LenDist state. fn issue_38_update_lendist() { // compressed final block 011, literal 00110000=0 x 32766, len 0000001=3, dist 00000=1, end 0000000 let mut data = vec![0b0110_0011]; data.extend(std::iter::repeat(0b0110_0000).take(32765)); data.extend(vec![0b0000_0000, 0b0000_0010, 0b0000_00000]); let bytes = inflate::inflate_bytes(&data).unwrap(); assert_eq!(bytes.len(), 32769); } #[test] /// InflateStream::update() should return if it can't make progress fn issue_38_update_no_progress() { // compressed final block 011, 110010000 x 5, 110010000 truncated to 8 bits let data = &[0b1001_1011, 0b0011_0000, 0b0110_0001, 0b1100_0010, 0b10000100, 0b0000_1001, 0b00010011]; let bytes = inflate::inflate_bytes(data).unwrap(); assert_eq!(bytes.len(), 5); } #[test] /// inflate() should loop until no data is produced. fn issue_38_inflate() { // compressed final block 011, literal 00110000=0 x 32769, end 0000000 let mut data = vec![0b0110_0011]; data.extend(std::iter::repeat(0b0110_0000).take(32768)); data.extend(vec![0b0000_0000, 0b0000_0000]); let bytes = inflate::inflate_bytes(&data).unwrap(); assert_eq!(bytes.len(), 32769); } #[test] /// InflateStream::update() should handle end of input between uncompressed len and nlen. fn issue_47_uncompressed() { let len = 50; let hilen = (len >> 8) as u8; let lolen = (len & 0xff) as u8; // uncompressed block 001 let mut data = vec![0b001, lolen, hilen, !lolen, !hilen]; data.extend(std::iter::repeat(0).take(len + 1)); let mut stream = inflate::InflateStream::new(); { // Update with input to the end of len. let (update_len, bytes) = stream.update(&data[..3]).unwrap(); assert_eq!(update_len, 3); assert_eq!(bytes, &[]); } { // Update with remainder of input. let (update_len, bytes) = stream.update(&data[3..]).unwrap(); assert_eq!(update_len, data.len() - 3); assert_eq!(bytes.len(), len); } } inflate-0.4.5/.cargo_vcs_info.json0000644000000001120000000000000125150ustar00{ "git": { "sha1": "7a536d75cd609b01e8f507f209970fb48b2dea75" } }