1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
 *
 * 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.
 */

//! Huffman is a library to decode huffman-encoded data.
//!
//! # Usage
//!
//! Create a Huffman `Tree` and call the `decode` function to decode the input.

/*
 * TODO: use nom.
 * TODO: write safe code only (have a way to have a reference to a node).
 * TODO: decode should return a Result.
 * TODO: using an array of 2 Option<Tree> might be faster than having the fields left and right.
 * TODO: compile the huffman tree just in time (using llvm?).
 * TODO: update the travis script to use coveralls (since travis-cargo does not work).
 */

extern crate bitreader;

use bitreader::BitReader;

/// Go deeper in the tree, following the direction specified by `$dir`.
/// When a leaf is reached, add the value to `$result` and reset the `$current_node` to the
/// `$root`.
macro_rules! visit {
    ($current_node:expr, $root:expr, $result:expr, $dir:ident) => {{
        let node = unsafe { &*$current_node };
        let $dir: &Tree = node.$dir.as_ref().unwrap();

        if let Some(byte) = $dir.value {
            $result.push(byte);
            $current_node = $root as *const Tree;
        }
        else {
            $current_node = $dir as *const Tree;
        }
    }};
}

/// A huffman tree.
#[derive(Default)]
pub struct Tree {
    pub left: Option<Box<Tree>>,
    pub right: Option<Box<Tree>>,
    pub value: Option<u8>,
}

impl Tree {
    pub fn new() -> Self {
        Tree {
            left: None,
            right: None,
            value: None,
        }
    }

    pub fn new_with_value(value: u8) -> Self {
        Tree {
            left: None,
            right: None,
            value: Some(value),
        }
    }
}

/// Decode the huffman-encoded `input` using the Huffman `tree`.
/// The decoding ends when `decompressed_size` is reached.
pub fn decode(input: &[u8], tree: &Tree, decompressed_size: usize) -> Vec<u8> {
    decode_with_offset(input, 0, tree, decompressed_size)
}

/// Decode the huffman-encoded `input` using the Huffman `tree`.
/// The decoding starts at input + `offset`, where `offset` is the number bits to skip (number between
/// 0 and 8).
/// The decoding ends when `decompressed_size` is reached.
pub fn decode_with_offset(input: &[u8], offset: u8, tree: &Tree, decompressed_size: usize) -> Vec<u8> {
    debug_assert!(offset <= 8);
    let mut result = vec![];
    let mut reader = BitReader::new(input);

    let mut current_node = tree as *const Tree;

    // Skip the bits.
    reader.read_u8(offset).unwrap();

    while let Ok(bit) = reader.read_u8(1) {
        if result.len() == decompressed_size {
            break;
        }

        if bit == 1 {
            visit!(current_node, tree, result, right);
        }
        else {
            visit!(current_node, tree, result, left);
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, HashMap};
    use std::u8;

    use super::{Tree, decode, decode_with_offset};

    /// Create a Huffman tree from the `bytes`.
    fn create_tree(bytes: &[u8]) -> Tree {
        let mut occurences = HashMap::new();
        for byte in bytes {
            let entry = occurences.entry(byte).or_insert(0);
            *entry += 1;
        }

        let mut nodes = BTreeMap::new();

        for (&byte, occurence) in occurences {
            nodes.insert((occurence, byte), Tree::new_with_value(byte));
        }

        let mut index = u8::MAX;

        while nodes.len() > 1 {
            let ((occurence1, _), node1) = pop_first(&mut nodes);
            let ((occurence2, _), node2) = pop_first(&mut nodes);
            let new_occurence = occurence1 + occurence2;
            let mut new_node = Tree::new();
            new_node.left = Some(Box::new(node1));
            new_node.right = Some(Box::new(node2));
            nodes.insert((new_occurence, index), new_node);
            index -= 1;
        }

        pop_first(&mut nodes).1
    }

    /// Remove the first element of the `map`.
    fn pop_first<K, V>(map: &mut BTreeMap<K, V>) -> (K, V)
        where K: Clone + Ord
    {
        let key = map.keys().next().unwrap().clone();
        let value = map.remove(&key).unwrap();
        (key, value)
    }

    #[test]
    fn test_decode() {
        let input = [0b01110010, 0b00110110, 0b11100110, 0b11011110, 0b00001111, 0b10101001, 0b10000000, 0b10101100, 0b01011111, 0b10011101, 0b11110011, 0b10010010, 0b00110111, 0b01000010, 0b00001111, 0b01110101, 0b11011010];
        let string = b"this is an example of a huffman tree";
        let tree = create_tree(string);

        let result = decode(&input, &tree, string.len());

        let expected: Vec<u8> = string.iter()
            .cloned()
            .collect();

        assert_eq!(expected, result);

        // Test that the additional bits are not used.
        let input = [0b01110010, 0b00110110, 0b11100110, 0b11011110, 0b00001111, 0b10101001, 0b10000000, 0b10101100, 0b01011111, 0b10011101, 0b11110011, 0b10010010, 0b00110111, 0b01000010, 0b00001111, 0b01110101, 0b11011010, 0b00000000];

        // Test with an offset.
        let result = decode_with_offset(&input, 4, &tree, string.len() - 1);
        assert_eq!(&expected[1..], &result[..]);
    }
}