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
mod utils;

extern crate lzf;
extern crate byteorder;

/// Takes a _compressed_ payload (with LZF header) and returns the decompressed payload
///
/// # Examples
///
/// ```
/// let compressed_content = ...
/// match decompress_lzf(compressed_content) {
///     Ok(decompressed) => println!("Decompressed: {:?}", decompressed),
///     Err(error) => // Handle error
/// };
/// ```
pub fn decompress_lzf(payload: &[u8]) -> Result<Vec<u8>, String> {
    match utils::headers::lzf_structure_from_compressed(payload) {
        Ok(structure) => structure.get_decompressed_payload(),
        Err(error) => Err(String::from(error))
    }
}

/// Takes an _uncompressed_ payload and returns the compressed payload (with LZF headers)
///
/// # Examples
///
/// ```
/// let test_string = "An adequately long string. Longer than this..."
/// match compress_lzf(test_string.as_bytes()) {
///     Ok(decompressed) => // Handle decompressed data,
///     Err(error) => // Handle error
/// };
/// ```
pub fn compress_lzf(payload: &[u8]) -> Result<Vec<u8>, String> {
    match utils::headers::lzf_structure_from_uncompressed(payload) {
        Ok(structure) => Ok(structure.to_compressed()),
        Err(error) => Err(String::from(error))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    static TEST_STRING: &str = "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain";

    #[test]
    fn test_compress_decompress() {
        
        let compressed_structure = utils::headers::lzf_structure_from_uncompressed(TEST_STRING.as_bytes()).unwrap();

        match decompress_lzf(&compressed_structure.to_compressed()) {
            Ok(decompressed) => {
                println!("Decompressed: {:?}", decompressed);
                assert_eq!(decompressed.len(), compressed_structure.header.original_len);
                assert_eq!(390, compressed_structure.header.original_len);
                assert_eq!(351, compressed_structure.header.compressed_len);
            },
            Err(error) => {
                println!("Got error: {}", error);
                assert!(false);
            }
        };
    }

    #[test]
    fn test_decompress() {
        let compressed = compress_lzf(TEST_STRING.as_bytes()).unwrap();

        match decompress_lzf(&compressed) {
            Ok(decompressed) => {
                println!("Decompressed: {:?}", decompressed);
                assert_eq!(358, compressed.len());
            },
            Err(error) => {
                println!("Got error: {}", error);
                assert!(false);
            }
        };
    }
}