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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::{char, usize};

mod huffman_tree;
use huffman_tree::node::NodeType::Character;
use huffman_tree::HuffmanTree;
pub mod config;
use config::Config;
mod errors;
use errors::InputError;

pub fn run(config: Config) -> Result<(), String> {
    match &config.flag[..] {
        "-c" | "--compress" => {
            if let Err(err) = compress(config.filename) {
                return Err(err.to_string());
            }
            Ok(())
        }
        "-d" | "--decompress" => {
            if let Err(err) = decompress(config.filename) {
                return Err(err.to_string());
            }
            Ok(())
        }
        "-V" | "--version" => Ok(print_version_message()),
        "-h" | "--help" | "" => Ok(print_help_message()),
        _ => {
            let error_message = format!("\n\tFound argument '{}' which wasn't expected\n\nSee 'huffcomp --help' for more information.\n", config.flag);
            return Err(error_message);
        }
    }
}

fn compress(filename: String) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(&filename)?;

    if contents.len() == 0 {
        let error_message = format!("File must not be empty");
        return Err(Box::new(InputError(error_message)));
    }

    // Generate characters' frequency map with contents.
    let freq_map = huffman_tree::char_freq(&contents);

    // Create tree with characters' frequency map.
    let tree = HuffmanTree::new(&freq_map);
    // tree._print();

    // Serialize HuffmanTree struct.
    let tree_bytes: Vec<u8> = bincode::serialize(&tree)?;
    let tree_size = tree_bytes.len().to_be_bytes();

    // Create output file.
    let output_filename = format!("{}.huff", &filename);
    let mut output_file = File::create(&output_filename)?;

    println!("Compressing '{}'. . .", filename);

    // Write HuffmanTree byte len and HuffmanTree bytes.
    output_file.write_all(&tree_size)?;
    output_file.write_all(&tree_bytes)?;

    // Create and fill the code table map.
    let mut code_table: HashMap<u32, String> = HashMap::new();
    huffman_tree::fill_code_table(&mut code_table, &tree);

    // Generate the encoded string.
    let mut encoded_string = String::from("");
    for c in contents.chars() {
        encoded_string.push_str(code_table.get(&(c as u32)).unwrap());
    }

    // Write encoded string bits length.
    output_file.write(&encoded_string.len().to_be_bytes())?;

    while encoded_string.len() % 8 != 0 {
        encoded_string.push_str("0");
    }

    let mut encoded_bytes: Vec<u8> = "".bytes().collect();
    for _ in 0..encoded_string.len() / 8 {
        encoded_bytes.push(0);
    }

    // Save all the bits into a bytes vector and write to output file.
    for (index, c) in encoded_string.char_indices() {
        encoded_bytes[index / 8] <<= 1;
        encoded_bytes[index / 8] += c as u8 - '0' as u8;
    }
    output_file.write(&encoded_bytes)?;

    println!("Compression finished!");
    println!("Output file: {}", output_filename);

    Ok(())
}

fn decompress(filename: String) -> Result<(), Box<dyn Error>> {
    let filename_extension = Path::new(&filename).extension();

    match filename_extension {
        Some(ext) => {
            if ext != "huff" {
                let error_message = format!("File must have the correct extension.\n\tExpected:\t\"huff\"\n\tFound:\t\t{:?}\n", ext);
                return Err(Box::new(InputError(error_message)));
            }
        }
        None => {
            let error_message = String::from("File must have \"huff\" extension.\n");
            return Err(Box::new(InputError(error_message)));
        }
    }

    let encoded = fs::read(&filename)?;

    if encoded.len() == 0 {
        let error_message = format!("File must not be empty");
        return Err(Box::new(InputError(error_message)));
    }

    let mut tree_size: [u8; 8] = [0; 8];

    for i in 0..8 {
        tree_size[i] = encoded[i];
    }

    let tree_size_value = usize::from_be_bytes(tree_size);
    let tree_encoded = &encoded[8..(tree_size_value + 8)];
    let tree: HuffmanTree = bincode::deserialize(tree_encoded)?;
    let mut node = tree.get_root();

    println!("Decompressing '{}'. . .", filename);

    let mut bits_to_decode: [u8; 8] = [0; 8];
    for i in 0..8 {
        bits_to_decode[i] = encoded[i + (tree_size_value + 8)];
    }
    let bits_to_decode = usize::from_be_bytes(bits_to_decode);

    let bytes_encoded = &encoded[(tree_size_value + 16)..];

    let output_filename = format!("{}d.txt", &filename);
    let mut output_file = File::create(&output_filename)?;
    let mut output_string = String::from("");

    let mut bit_counter = 0;
    for byte in bytes_encoded {
        for i in 0..8 {
            let mask = 0x80 >> i;
            let bit = (mask & byte) >> (7 - i);

            if let (Some(left), Some(right)) = (&node.left, &node.right) {
                node = if bit == 1 { &*right } else { &*left };
                if let None = node.left {
                    if let Character(character) = node.value {
                        let chars = char::from_u32(character).unwrap();
                        output_string.push(chars);
                    }
                    node = tree.get_root();
                }
            }

            bit_counter += 1;
            if bit_counter == bits_to_decode {
                break;
            }
        }
    }

    output_file.write(output_string.as_bytes())?;

    println!("Decompression finished!");
    println!("Output file: {}", output_filename);

    Ok(())
}

fn print_help_message() {
    const DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
    println!("{}", DESCRIPTION);
    println!();
    println!("USAGE:");
    println!("\thuffcomp [OPTION] [FILENAME]");
    println!();
    println!("OPTIONS:");
    println!("\t-c, --compress\t\tCompress the given text file");
    println!("\t-d, --decompress\tDecompress a valid .huff file");
    println!("\t-V, --version\t\tPrint version info and exit");
    println!();
}

fn print_version_message() {
    const VERSION: &'static str = env!("CARGO_PKG_VERSION");
    const NAME: &'static str = env!("CARGO_PKG_NAME");
    println!("{} {}", NAME, VERSION);
}