inflate/
utils.rs

1/// Convenience functions for inflating DEFLATE compressed data.
2///
3/// # Example
4/// ```
5/// use inflate::inflate_bytes;
6/// use std::str::from_utf8;
7///
8/// let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0];
9/// let decoded = inflate_bytes(&encoded).unwrap();
10/// println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world"
11/// ```
12
13use InflateStream;
14
15fn inflate(inflater: &mut InflateStream, data: &[u8]) -> Result<Vec<u8>, String> {
16    let mut decoded = Vec::<u8>::new();
17
18    let mut n = 0;
19    loop {
20        let (num_bytes_read, bytes) = try!(inflater.update(&data[n..]));
21        if bytes.len() == 0 {
22            break;
23        }
24        n += num_bytes_read;
25        decoded.extend(bytes.iter().map(|v| *v));
26    }
27
28    Ok(decoded)
29}
30
31/// Decompress the given slice of DEFLATE compressed data.
32///
33/// Returns a `Vec` with the decompressed data or an error message.
34pub fn inflate_bytes(data: &[u8]) -> Result<Vec<u8>, String> {
35    inflate(&mut InflateStream::new(), data)
36}
37
38/// Decompress the given slice of DEFLATE compressed (with zlib headers and trailers) data.
39///
40/// Returns a `Vec` with the decompressed data or an error message.
41pub fn inflate_bytes_zlib(data: &[u8]) -> Result<Vec<u8>, String> {
42    inflate(&mut InflateStream::from_zlib(), data)
43}
44
45/// Decompress the given slice of DEFLATE compressed (with zlib headers and trailers) data,
46/// without calculating and validating the checksum.
47///
48/// Returns a `Vec` with the decompressed data or an error message.
49pub fn inflate_bytes_zlib_no_checksum(data: &[u8]) -> Result<Vec<u8>, String> {
50    inflate(&mut InflateStream::from_zlib_no_checksum(), data)
51}
52
53#[cfg(test)]
54mod test {
55    #[test]
56    fn inflate_bytes_with_zlib() {
57        use super::inflate_bytes_zlib;
58        use std::str::from_utf8;
59
60        let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201,
61                       76, 82, 4, 0, 27, 101, 4, 19];
62        let decoded = inflate_bytes_zlib(&encoded).unwrap();
63        assert!(from_utf8(&decoded).unwrap() == "Hello, zlib!");
64    }
65
66    #[test]
67    fn inflate_bytes_with_zlib_checksum_fail() {
68        use super::inflate_bytes_zlib;
69
70        // The last 4 bytes are the checksum, we set them to 0 here to check that decoding fails
71        // if the checksum is wrong.
72        let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201,
73                       76, 82, 4, 0, 0, 0, 0, 0];
74        inflate_bytes_zlib(&encoded).unwrap_err();
75    }
76
77    #[test]
78    fn inflate_bytes_with_zlib_trailing() {
79        use super::inflate_bytes_zlib;
80        use std::str::from_utf8;
81
82        // The additional 4 bytes should be ignored.
83        let encoded = [120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201,
84                       76, 82, 4, 0, 27, 101, 4, 19, 0, 0, 0, 0];
85        let decoded = inflate_bytes_zlib(&encoded).unwrap();
86        assert!(from_utf8(&decoded).unwrap() == "Hello, zlib!");
87    }
88
89}