├── .github └── workflows │ └── tests.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── benches └── bench_basic.rs ├── examples ├── cli.rs └── lorem.rs ├── quickcheck ├── Cargo.lock ├── Cargo.toml ├── build.rs ├── lzf │ ├── lzf.h │ ├── lzfP.h │ ├── lzf_c.c │ └── lzf_d.c └── src │ └── lib.rs └── src ├── compress.rs ├── decompress.rs └── lib.rs /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [push, pull_request] 4 | 5 | env: 6 | RUSTFLAGS: -Dwarnings 7 | CARGO_NET_RETRY: 10 8 | CI: 1 9 | RUST_BACKTRACE: short 10 | RUSTUP_MAX_RETRIES: 10 11 | 12 | jobs: 13 | build_and_test: 14 | name: Build and test 15 | runs-on: ${{ matrix.os }} 16 | strategy: 17 | matrix: 18 | os: [ubuntu-latest] 19 | 20 | steps: 21 | - uses: actions/checkout@master 22 | - uses: dtolnay/rust-toolchain@stable 23 | - uses: Swatinem/rust-cache@v2 24 | 25 | - name: check 26 | run: cargo check --all 27 | 28 | - name: tests 29 | run: cargo test --all 30 | 31 | check_fmt_and_docs: 32 | name: Checking fmt and docs 33 | runs-on: ubuntu-latest 34 | steps: 35 | - uses: actions/checkout@master 36 | - uses: dtolnay/rust-toolchain@stable 37 | with: 38 | components: rustfmt, clippy 39 | - uses: Swatinem/rust-cache@v2 40 | 41 | - name: fmt 42 | run: cargo fmt --all -- --check 43 | 44 | - name: Docs 45 | run: cargo doc --no-deps 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | *.o 4 | *.a 5 | /quickcheck/target 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### 1.0.0 (2022-11-06) 2 | 3 | This is the same as v0.3.2 4 | 5 | This crate is now DEPRECATED. 6 | The API now is suboptimal and not as fast as I hoped. 7 | 8 | ### 0.3.2 (2022-11-06) 9 | 10 | DO NOT USE <= v0.3.1. 11 | It's broken, wrong and unsound. 12 | 13 | * Remove wrong usage of `mem::unitialized` and replace it with a heap-allocated `Vec`. 14 | This might be slower than the initial version, but at least not broken. 15 | 16 | ### 0.3.1 (2015-09-27) 17 | 18 | * Correct output length calculation 19 | 20 | ### 0.3.0 (2015-09-16) 21 | 22 | * Replace C code with Rust code 23 | 24 | ### 0.2.3 (2015-04-16) 25 | 26 | * Fix tests to work on beta 27 | 28 | ### 0.2.2 (2015-04-16) 29 | 30 | * Make it work with Rust 1.0 Beta 31 | 32 | ### 0.2.1 (2015-02-15) 33 | 34 | * Add documentation 35 | 36 | ### 0.2.0 (2015-02-15) 37 | 38 | * Return unknown error as i32 39 | 40 | ### 0.1.4 (2015-02-15) 41 | 42 | * Adopt features and Debug trait 43 | 44 | ### 0.1.3 (2015-01-16) 45 | 46 | * Implement Display for LzfError to allow for unwrap of the result 47 | * Use Show to display an error 48 | 49 | ### 0.1.2 (2015-01-16) 50 | 51 | * Updated to work with Rust 1.0 52 | 53 | ### 0.1.1 (2015-01-16) 54 | 55 | * Derive copy as suggested by latest nightly 56 | 57 | ### 0.1.0 (2014-11-16) 58 | 59 | * Basic functionality 60 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lzf" 3 | version = "1.0.0" 4 | authors = ["Jan-Erik Rediger "] 5 | keywords = ["compression"] 6 | description = "DEPRECATED! An implementation of LZF, a very small data compression algorithm" 7 | homepage = "https://github.com/badboy/lzf-rs" 8 | repository = "https://github.com/badboy/lzf-rs" 9 | documentation = "https://docs.rs/lzf/" 10 | license = "BSD-3-Clause" 11 | readme = "README.md" 12 | edition = "2021" 13 | 14 | include = [ 15 | "README.md", 16 | "LICENSE", 17 | "Cargo.toml", 18 | "src/**/*", 19 | "benches/**/*", 20 | "examples/**/*" 21 | ] 22 | 23 | [lib] 24 | bench = false 25 | 26 | [dev-dependencies] 27 | quickcheck = "1.0" 28 | criterion = "0.4" 29 | 30 | [[bench]] 31 | name = "bench_basic" 32 | harness = false 33 | 34 | [features] 35 | default = [] 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Jan-Erik Rediger 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 7 | * Neither the name of Redis nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 8 | 9 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LZF - a very small data compression library 2 | 3 | --- 4 | 5 | **DEPRECATED** 6 | 7 | --- 8 | 9 | [LibLZF][] is a super small and fast compression library, originally written by Marc Lehmann. 10 | It's written in C and consists of only 4 files. 11 | And this is the rewrite in Rust. 12 | 13 | ~~Instead of rewriting the whole thing in Rust, I used Rust's [Foreign Function Interface][ffi] and wrote a wrapper. 14 | The whole Rust code is under 50 lines (yes, there is more test code than actual implementation code). 15 | And it is super easy to use, though I'm not happy with the current interface.~~ 16 | 17 | I sat down and tried to understand the original C code and then rewrote it in (mostly) safe Rust code. 18 | ~~And the best thing: it's still super fast (on some basic benchmarks it's nearly as fast as the original code).~~ 19 | It now consists of roughly 200 lines of code, which is probably around the same as the original implementation. 20 | 21 | 22 | ## Build 23 | 24 | ``` 25 | cargo build --release 26 | ``` 27 | 28 | ## Usage 29 | 30 | ```rust 31 | fn main() { 32 | let data = "foobar"; 33 | 34 | let compressed = lzf::compress(data.as_bytes()).unwrap(); 35 | 36 | let decompressed = lzf::decompress(&compressed, data.len()).unwrap(); 37 | } 38 | 39 | ``` 40 | 41 | ## Tests 42 | 43 | Run tests with: 44 | 45 | ``` 46 | cargo test 47 | ``` 48 | 49 | Run benchmarks with: 50 | 51 | ``` 52 | cargo bench 53 | ``` 54 | 55 | ## Contribute 56 | 57 | If you find bugs or want to help otherwise, please [open an issue](https://github.com/badboy/lzf-rs/issues). 58 | This is my first released library in Rust and I'm still learning. So if there are better ways to do things in Rust, I'm happy to hear about it. 59 | 60 | ## License 61 | 62 | BSD. See [LICENSE](LICENSE). 63 | liblzf is BSD as well. See [lzf.h](lzf/lzf.h). 64 | 65 | [liblzf]: http://software.schmorp.de/pkg/liblzf.html 66 | [ffi]: http://doc.rust-lang.org/guide-ffi.html 67 | -------------------------------------------------------------------------------- /benches/bench_basic.rs: -------------------------------------------------------------------------------- 1 | use criterion::{criterion_group, criterion_main, Criterion}; 2 | 3 | fn bench_lzf_compression(c: &mut Criterion) { 4 | static KB: usize = 1024; 5 | 6 | let data = [KB, 2 * KB, 4 * KB, 6 * KB]; 7 | 8 | let mut group = c.benchmark_group("lzf 0"); 9 | for size in data { 10 | let name = format!("size={}", size); 11 | group.bench_function(name, |b| { 12 | let buffer = std::iter::repeat(0u8).take(size).collect::>(); 13 | b.iter(|| lzf::compress(&buffer).unwrap()); 14 | }); 15 | } 16 | group.finish(); 17 | 18 | let mut group = c.benchmark_group("lzf 17"); 19 | for size in data { 20 | let name = format!("size={}", size); 21 | group.bench_function(name, |b| { 22 | let buffer = std::iter::repeat(17u8).take(size).collect::>(); 23 | b.iter(|| lzf::compress(&buffer).unwrap()); 24 | }); 25 | } 26 | group.finish(); 27 | 28 | let mut group = c.benchmark_group("lzf lorem"); 29 | for size in data { 30 | let name = format!("size={}", size); 31 | group.bench_function(name, |b| { 32 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."; 33 | let mut buffer = String::new(); 34 | while buffer.len() < size { 35 | buffer.push_str(lorem); 36 | } 37 | b.iter(|| lzf::compress(&buffer.as_bytes()[0..size]).unwrap()); 38 | }); 39 | } 40 | group.finish(); 41 | 42 | c.bench_function("lzf decompression", |b| { 43 | let lorem = [ 44 | 31, 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 45 | 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 99, 111, 110, 115, 4, 101, 116, 101, 46 | 116, 117, 32, 20, 1, 97, 100, 32, 35, 31, 99, 105, 110, 103, 32, 101, 108, 105, 116, 47 | 114, 44, 32, 115, 101, 100, 32, 100, 105, 97, 109, 32, 110, 111, 110, 117, 109, 121, 48 | 32, 101, 105, 114, 109, 6, 111, 100, 32, 116, 101, 109, 112, 32, 68, 14, 105, 110, 118, 49 | 105, 100, 117, 110, 116, 32, 117, 116, 32, 108, 97, 98, 32, 100, 2, 32, 101, 116, 128, 50 | 96, 13, 101, 32, 109, 97, 103, 110, 97, 32, 97, 108, 105, 113, 117, 121, 32, 64, 2, 51 | 101, 114, 97, 32, 108, 224, 0, 79, 20, 118, 111, 108, 117, 112, 116, 117, 97, 46, 32, 52 | 65, 116, 32, 118, 101, 114, 111, 32, 101, 111, 115, 64, 61, 4, 97, 99, 99, 117, 115, 53 | 64, 47, 9, 116, 32, 106, 117, 115, 116, 111, 32, 100, 117, 32, 3, 64, 179, 0, 101, 96, 54 | 31, 10, 101, 97, 32, 114, 101, 98, 117, 109, 46, 32, 83, 32, 180, 1, 32, 99, 32, 167, 55 | 16, 97, 32, 107, 97, 115, 100, 32, 103, 117, 98, 101, 114, 103, 114, 101, 110, 44, 32, 56 | 173, 32, 105, 7, 97, 32, 116, 97, 107, 105, 109, 97, 32, 31, 5, 115, 97, 110, 99, 116, 57 | 117, 32, 63, 3, 115, 116, 32, 76, 32, 73, 225, 13, 11, 0, 46, 224, 18, 27, 225, 118, 58 | 39, 1, 97, 46, 59 | ]; 60 | b.iter(|| lzf::decompress(&lorem, 451).unwrap()); 61 | }); 62 | } 63 | 64 | criterion_group!(benches, bench_lzf_compression); 65 | criterion_main!(benches); 66 | -------------------------------------------------------------------------------- /examples/cli.rs: -------------------------------------------------------------------------------- 1 | extern crate lzf; 2 | use std::io::{self, Read, Write}; 3 | 4 | fn main() { 5 | let mut contents: Vec = Vec::new(); 6 | let _ = io::stdin().read_to_end(&mut contents).unwrap(); 7 | 8 | let compressed = lzf::compress(&contents[..]).unwrap(); 9 | let _ = io::stdout().write_all(&compressed[..]).unwrap(); 10 | } 11 | -------------------------------------------------------------------------------- /examples/lorem.rs: -------------------------------------------------------------------------------- 1 | extern crate lzf; 2 | use lzf::compress; 3 | use lzf::decompress; 4 | 5 | fn main() { 6 | let lorem = "\r\n\r\n\r\n\r\n ALICE'S ADVENTURES IN WONDERLAND\r\n"; 7 | 8 | println!("lorem.len: {}", lorem.len()); 9 | 10 | let compressed = compress(lorem.as_bytes()).unwrap(); 11 | println!("l: {}", compressed.len()); 12 | 13 | let decompressed = decompress(&compressed[..], lorem.len()).unwrap(); 14 | println!("l: {:?}", decompressed.len()); 15 | } 16 | -------------------------------------------------------------------------------- /quickcheck/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.19" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "cc" 16 | version = "1.0.73" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" 19 | 20 | [[package]] 21 | name = "cfg-if" 22 | version = "1.0.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 25 | 26 | [[package]] 27 | name = "env_logger" 28 | version = "0.8.4" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" 31 | dependencies = [ 32 | "log", 33 | "regex", 34 | ] 35 | 36 | [[package]] 37 | name = "getrandom" 38 | version = "0.2.8" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" 41 | dependencies = [ 42 | "cfg-if", 43 | "libc", 44 | "wasi", 45 | ] 46 | 47 | [[package]] 48 | name = "libc" 49 | version = "0.2.136" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "55edcf6c0bb319052dea84732cf99db461780fd5e8d3eb46ab6ff312ab31f197" 52 | 53 | [[package]] 54 | name = "log" 55 | version = "0.4.17" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" 58 | dependencies = [ 59 | "cfg-if", 60 | ] 61 | 62 | [[package]] 63 | name = "lzf" 64 | version = "0.3.1" 65 | 66 | [[package]] 67 | name = "lzf-qc" 68 | version = "0.0.1" 69 | dependencies = [ 70 | "cc", 71 | "libc", 72 | "lzf", 73 | "quickcheck", 74 | ] 75 | 76 | [[package]] 77 | name = "memchr" 78 | version = "2.5.0" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" 81 | 82 | [[package]] 83 | name = "quickcheck" 84 | version = "1.0.3" 85 | source = "registry+https://github.com/rust-lang/crates.io-index" 86 | checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" 87 | dependencies = [ 88 | "env_logger", 89 | "log", 90 | "rand", 91 | ] 92 | 93 | [[package]] 94 | name = "rand" 95 | version = "0.8.5" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 98 | dependencies = [ 99 | "rand_core", 100 | ] 101 | 102 | [[package]] 103 | name = "rand_core" 104 | version = "0.6.4" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 107 | dependencies = [ 108 | "getrandom", 109 | ] 110 | 111 | [[package]] 112 | name = "regex" 113 | version = "1.6.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" 116 | dependencies = [ 117 | "aho-corasick", 118 | "memchr", 119 | "regex-syntax", 120 | ] 121 | 122 | [[package]] 123 | name = "regex-syntax" 124 | version = "0.6.27" 125 | source = "registry+https://github.com/rust-lang/crates.io-index" 126 | checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" 127 | 128 | [[package]] 129 | name = "wasi" 130 | version = "0.11.0+wasi-snapshot-preview1" 131 | source = "registry+https://github.com/rust-lang/crates.io-index" 132 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 133 | -------------------------------------------------------------------------------- /quickcheck/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lzf-qc" 3 | version = "0.0.1" 4 | authors = ["Jan-Erik Rediger "] 5 | description = "Compare lzf-rs to native C library" 6 | license = "BSD-3-Clause" 7 | edition = "2021" 8 | 9 | build = "build.rs" 10 | 11 | [dependencies] 12 | quickcheck = "1.0" 13 | libc = "0.2" 14 | lzf = {path = ".."} 15 | 16 | [build-dependencies] 17 | cc = "1.0" 18 | -------------------------------------------------------------------------------- /quickcheck/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | cc::Build::new() 3 | .file("lzf/lzf_c.c") 4 | .file("lzf/lzf_d.c") 5 | .compile("lzf"); 6 | } 7 | -------------------------------------------------------------------------------- /quickcheck/lzf/lzf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2008 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #ifndef LZF_H 38 | #define LZF_H 39 | 40 | /*********************************************************************** 41 | ** 42 | ** lzf -- an extremely fast/free compression/decompression-method 43 | ** http://liblzf.plan9.de/ 44 | ** 45 | ** This algorithm is believed to be patent-free. 46 | ** 47 | ***********************************************************************/ 48 | 49 | #define LZF_VERSION 0x0105 /* 1.5, API version */ 50 | 51 | /* 52 | * Compress in_len bytes stored at the memory block starting at 53 | * in_data and write the result to out_data, up to a maximum length 54 | * of out_len bytes. 55 | * 56 | * If the output buffer is not large enough or any error occurs return 0, 57 | * otherwise return the number of bytes used, which might be considerably 58 | * more than in_len (but less than 104% of the original size), so it 59 | * makes sense to always use out_len == in_len - 1), to ensure _some_ 60 | * compression, and store the data uncompressed otherwise (with a flag, of 61 | * course. 62 | * 63 | * lzf_compress might use different algorithms on different systems and 64 | * even different runs, thus might result in different compressed strings 65 | * depending on the phase of the moon or similar factors. However, all 66 | * these strings are architecture-independent and will result in the 67 | * original data when decompressed using lzf_decompress. 68 | * 69 | * The buffers must not be overlapping. 70 | * 71 | * If the option LZF_STATE_ARG is enabled, an extra argument must be 72 | * supplied which is not reflected in this header file. Refer to lzfP.h 73 | * and lzf_c.c. 74 | * 75 | */ 76 | unsigned int 77 | lzf_compress (const void *const in_data, unsigned int in_len, 78 | void *out_data, unsigned int out_len); 79 | 80 | /* 81 | * Decompress data compressed with some version of the lzf_compress 82 | * function and stored at location in_data and length in_len. The result 83 | * will be stored at out_data up to a maximum of out_len characters. 84 | * 85 | * If the output buffer is not large enough to hold the decompressed 86 | * data, a 0 is returned and errno is set to E2BIG. Otherwise the number 87 | * of decompressed bytes (i.e. the original length of the data) is 88 | * returned. 89 | * 90 | * If an error in the compressed data is detected, a zero is returned and 91 | * errno is set to EINVAL. 92 | * 93 | * This function is very fast, about as fast as a copying loop. 94 | */ 95 | unsigned int 96 | lzf_decompress (const void *const in_data, unsigned int in_len, 97 | void *out_data, unsigned int out_len); 98 | 99 | #endif 100 | 101 | -------------------------------------------------------------------------------- /quickcheck/lzf/lzfP.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #ifndef LZFP_h 38 | #define LZFP_h 39 | 40 | #define STANDALONE 1 /* at the moment, this is ok. */ 41 | 42 | #ifndef STANDALONE 43 | # include "lzf.h" 44 | #endif 45 | 46 | /* 47 | * Size of hashtable is (1 << HLOG) * sizeof (char *) 48 | * decompression is independent of the hash table size 49 | * the difference between 15 and 14 is very small 50 | * for small blocks (and 14 is usually a bit faster). 51 | * For a low-memory/faster configuration, use HLOG == 13; 52 | * For best compression, use 15 or 16 (or more, up to 23). 53 | */ 54 | #ifndef HLOG 55 | # define HLOG 16 56 | #endif 57 | 58 | /* 59 | * Sacrifice very little compression quality in favour of compression speed. 60 | * This gives almost the same compression as the default code, and is 61 | * (very roughly) 15% faster. This is the preferred mode of operation. 62 | */ 63 | #ifndef VERY_FAST 64 | # define VERY_FAST 1 65 | #endif 66 | 67 | /* 68 | * Sacrifice some more compression quality in favour of compression speed. 69 | * (roughly 1-2% worse compression for large blocks and 70 | * 9-10% for small, redundant, blocks and >>20% better speed in both cases) 71 | * In short: when in need for speed, enable this for binary data, 72 | * possibly disable this for text data. 73 | */ 74 | #ifndef ULTRA_FAST 75 | # define ULTRA_FAST 0 76 | #endif 77 | 78 | /* 79 | * Unconditionally aligning does not cost very much, so do it if unsure 80 | */ 81 | #ifndef STRICT_ALIGN 82 | # define STRICT_ALIGN !(defined(__i386) || defined (__amd64)) 83 | #endif 84 | 85 | /* 86 | * You may choose to pre-set the hash table (might be faster on some 87 | * modern cpus and large (>>64k) blocks, and also makes compression 88 | * deterministic/repeatable when the configuration otherwise is the same). 89 | */ 90 | #ifndef INIT_HTAB 91 | # define INIT_HTAB 0 92 | #endif 93 | 94 | /* 95 | * Avoid assigning values to errno variable? for some embedding purposes 96 | * (linux kernel for example), this is necessary. NOTE: this breaks 97 | * the documentation in lzf.h. 98 | */ 99 | #ifndef AVOID_ERRNO 100 | # define AVOID_ERRNO 0 101 | #endif 102 | 103 | /* 104 | * Whether to pass the LZF_STATE variable as argument, or allocate it 105 | * on the stack. For small-stack environments, define this to 1. 106 | * NOTE: this breaks the prototype in lzf.h. 107 | */ 108 | #ifndef LZF_STATE_ARG 109 | # define LZF_STATE_ARG 0 110 | #endif 111 | 112 | /* 113 | * Whether to add extra checks for input validity in lzf_decompress 114 | * and return EINVAL if the input stream has been corrupted. This 115 | * only shields against overflowing the input buffer and will not 116 | * detect most corrupted streams. 117 | * This check is not normally noticeable on modern hardware 118 | * (<1% slowdown), but might slow down older cpus considerably. 119 | */ 120 | #ifndef CHECK_INPUT 121 | # define CHECK_INPUT 1 122 | #endif 123 | 124 | /*****************************************************************************/ 125 | /* nothing should be changed below */ 126 | 127 | typedef unsigned char u8; 128 | 129 | typedef const u8 *LZF_STATE[1 << (HLOG)]; 130 | 131 | #if !STRICT_ALIGN 132 | /* for unaligned accesses we need a 16 bit datatype. */ 133 | # include 134 | # if USHRT_MAX == 65535 135 | typedef unsigned short u16; 136 | # elif UINT_MAX == 65535 137 | typedef unsigned int u16; 138 | # else 139 | # undef STRICT_ALIGN 140 | # define STRICT_ALIGN 1 141 | # endif 142 | #endif 143 | 144 | #if ULTRA_FAST 145 | # if defined(VERY_FAST) 146 | # undef VERY_FAST 147 | # endif 148 | #endif 149 | 150 | #if INIT_HTAB 151 | # ifdef __cplusplus 152 | # include 153 | # else 154 | # include 155 | # endif 156 | #endif 157 | 158 | #endif 159 | 160 | -------------------------------------------------------------------------------- /quickcheck/lzf/lzf_c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2008 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #include "lzfP.h" 38 | 39 | #define HSIZE (1 << (HLOG)) 40 | 41 | /* 42 | * don't play with this unless you benchmark! 43 | * decompression is not dependent on the hash function 44 | * the hashing function might seem strange, just believe me 45 | * it works ;) 46 | */ 47 | #ifndef FRST 48 | # define FRST(p) (((p[0]) << 8) | p[1]) 49 | # define NEXT(v,p) (((v) << 8) | p[2]) 50 | # if ULTRA_FAST 51 | # define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1)) 52 | # elif VERY_FAST 53 | # define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) 54 | # else 55 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) 56 | # endif 57 | #endif 58 | /* 59 | * IDX works because it is very similar to a multiplicative hash, e.g. 60 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) 61 | * the latter is also quite fast on newer CPUs, and compresses similarly. 62 | * 63 | * the next one is also quite good, albeit slow ;) 64 | * (int)(cos(h & 0xffffff) * 1e6) 65 | */ 66 | 67 | #if 0 68 | /* original lzv-like hash function, much worse and thus slower */ 69 | # define FRST(p) (p[0] << 5) ^ p[1] 70 | # define NEXT(v,p) ((v) << 5) ^ p[2] 71 | # define IDX(h) ((h) & (HSIZE - 1)) 72 | #endif 73 | 74 | #define MAX_LIT (1 << 5) 75 | #define MAX_OFF (1 << 13) 76 | #define MAX_REF ((1 << 8) + (1 << 3)) 77 | 78 | #if __GNUC__ >= 3 79 | # define expect(expr,value) __builtin_expect ((expr),(value)) 80 | # define inline inline 81 | #else 82 | # define expect(expr,value) (expr) 83 | # define inline static 84 | #endif 85 | 86 | #define expect_false(expr) expect ((expr) != 0, 0) 87 | #define expect_true(expr) expect ((expr) != 0, 1) 88 | 89 | /* 90 | * compressed format 91 | * 92 | * 000LLLLL ; literal 93 | * LLLooooo oooooooo ; backref L 94 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 95 | * 96 | */ 97 | 98 | unsigned int 99 | lzf_compress (const void *const in_data, unsigned int in_len, 100 | void *out_data, unsigned int out_len 101 | #if LZF_STATE_ARG 102 | , LZF_STATE htab 103 | #endif 104 | ) 105 | { 106 | #if !LZF_STATE_ARG 107 | LZF_STATE htab; 108 | #endif 109 | const u8 **hslot; 110 | const u8 *ip = (const u8 *)in_data; 111 | u8 *op = (u8 *)out_data; 112 | const u8 *in_end = ip + in_len; 113 | u8 *out_end = op + out_len; 114 | const u8 *ref; 115 | 116 | /* off requires a type wide enough to hold a general pointer difference. 117 | * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only 118 | * works for differences within a single object). We also assume that no 119 | * no bit pattern traps. Since the only platform that is both non-POSIX 120 | * and fails to support both assumptions is windows 64 bit, we make a 121 | * special workaround for it. 122 | */ 123 | #if defined (WIN32) && defined (_M_X64) 124 | unsigned _int64 off; /* workaround for missing POSIX compliance */ 125 | #else 126 | unsigned long off; 127 | #endif 128 | unsigned int hval; 129 | int lit; 130 | 131 | if (!in_len || !out_len) 132 | return 0; 133 | 134 | #if INIT_HTAB 135 | memset (htab, 0, sizeof (htab)); 136 | # if 0 137 | for (hslot = htab; hslot < htab + HSIZE; hslot++) 138 | *hslot++ = ip; 139 | # endif 140 | #endif 141 | 142 | lit = 0; op++; /* start run */ 143 | 144 | hval = FRST (ip); 145 | while (ip < in_end - 2) 146 | { 147 | hval = NEXT (hval, ip); 148 | hslot = htab + IDX (hval); 149 | ref = *hslot; *hslot = ip; 150 | 151 | if (1 152 | #if INIT_HTAB 153 | && ref < ip /* the next test will actually take care of this, but this is faster */ 154 | #endif 155 | && (off = ip - ref - 1) < MAX_OFF 156 | && ip + 4 < in_end 157 | && ref > (u8 *)in_data 158 | #if STRICT_ALIGN 159 | && ref[0] == ip[0] 160 | && ref[1] == ip[1] 161 | && ref[2] == ip[2] 162 | #else 163 | && *(u16 *)ref == *(u16 *)ip 164 | && ref[2] == ip[2] 165 | #endif 166 | ) 167 | { 168 | /* match found at *ref++ */ 169 | unsigned int len = 2; 170 | unsigned int maxlen = in_end - ip - len; 171 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 172 | 173 | op [- lit - 1] = lit - 1; /* stop run */ 174 | op -= !lit; /* undo run if length is zero */ 175 | 176 | if (expect_false (op + 3 + 1 >= out_end)) 177 | return 0; 178 | 179 | for (;;) 180 | { 181 | if (expect_true (maxlen > 16)) 182 | { 183 | len++; if (ref [len] != ip [len]) break; 184 | len++; if (ref [len] != ip [len]) break; 185 | len++; if (ref [len] != ip [len]) break; 186 | len++; if (ref [len] != ip [len]) break; 187 | 188 | len++; if (ref [len] != ip [len]) break; 189 | len++; if (ref [len] != ip [len]) break; 190 | len++; if (ref [len] != ip [len]) break; 191 | len++; if (ref [len] != ip [len]) break; 192 | 193 | len++; if (ref [len] != ip [len]) break; 194 | len++; if (ref [len] != ip [len]) break; 195 | len++; if (ref [len] != ip [len]) break; 196 | len++; if (ref [len] != ip [len]) break; 197 | 198 | len++; if (ref [len] != ip [len]) break; 199 | len++; if (ref [len] != ip [len]) break; 200 | len++; if (ref [len] != ip [len]) break; 201 | len++; if (ref [len] != ip [len]) break; 202 | } 203 | 204 | do 205 | len++; 206 | while (len < maxlen && ref[len] == ip[len]); 207 | 208 | break; 209 | } 210 | 211 | len -= 2; /* len is now #octets - 1 */ 212 | ip++; 213 | 214 | if (len < 7) 215 | { 216 | *op++ = (off >> 8) + (len << 5); 217 | } 218 | else 219 | { 220 | *op++ = (off >> 8) + ( 7 << 5); 221 | *op++ = len - 7; 222 | } 223 | 224 | *op++ = off; 225 | lit = 0; op++; /* start run */ 226 | 227 | ip += len + 1; 228 | 229 | if (expect_false (ip >= in_end - 2)) 230 | break; 231 | 232 | #if ULTRA_FAST || VERY_FAST 233 | --ip; 234 | # if VERY_FAST && !ULTRA_FAST 235 | --ip; 236 | # endif 237 | hval = FRST (ip); 238 | 239 | hval = NEXT (hval, ip); 240 | htab[IDX (hval)] = ip; 241 | ip++; 242 | 243 | # if VERY_FAST && !ULTRA_FAST 244 | hval = NEXT (hval, ip); 245 | htab[IDX (hval)] = ip; 246 | ip++; 247 | # endif 248 | #else 249 | ip -= len + 1; 250 | 251 | do 252 | { 253 | hval = NEXT (hval, ip); 254 | htab[IDX (hval)] = ip; 255 | ip++; 256 | } 257 | while (len--); 258 | #endif 259 | } 260 | else 261 | { 262 | /* one more literal byte we must copy */ 263 | if (expect_false (op >= out_end)) 264 | return 0; 265 | 266 | lit++; *op++ = *ip++; 267 | 268 | if (expect_false (lit == MAX_LIT)) 269 | { 270 | op [- lit - 1] = lit - 1; /* stop run */ 271 | lit = 0; op++; /* start run */ 272 | } 273 | } 274 | } 275 | 276 | if (op + 3 > out_end) /* at most 3 bytes can be missing here */ 277 | return 0; 278 | 279 | while (ip < in_end) 280 | { 281 | lit++; *op++ = *ip++; 282 | 283 | if (expect_false (lit == MAX_LIT)) 284 | { 285 | op [- lit - 1] = lit - 1; /* stop run */ 286 | lit = 0; op++; /* start run */ 287 | } 288 | } 289 | 290 | op [- lit - 1] = lit - 1; /* end run */ 291 | op -= !lit; /* undo run if length is zero */ 292 | 293 | return op - (u8 *)out_data; 294 | } 295 | 296 | -------------------------------------------------------------------------------- /quickcheck/lzf/lzf_d.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #include "lzfP.h" 38 | 39 | #if AVOID_ERRNO 40 | # define SET_ERRNO(n) 41 | #else 42 | # include 43 | # define SET_ERRNO(n) errno = (n) 44 | #endif 45 | 46 | /* 47 | #if (__i386 || __amd64) && __GNUC__ >= 3 48 | # define lzf_movsb(dst, src, len) \ 49 | asm ("rep movsb" \ 50 | : "=D" (dst), "=S" (src), "=c" (len) \ 51 | : "0" (dst), "1" (src), "2" (len)); 52 | #endif 53 | */ 54 | 55 | unsigned int 56 | lzf_decompress (const void *const in_data, unsigned int in_len, 57 | void *out_data, unsigned int out_len) 58 | { 59 | u8 const *ip = (const u8 *)in_data; 60 | u8 *op = (u8 *)out_data; 61 | u8 const *const in_end = ip + in_len; 62 | u8 *const out_end = op + out_len; 63 | 64 | do 65 | { 66 | unsigned int ctrl = *ip++; 67 | 68 | if (ctrl < (1 << 5)) /* literal run */ 69 | { 70 | ctrl++; 71 | 72 | if (op + ctrl > out_end) 73 | { 74 | SET_ERRNO (E2BIG); 75 | return 0; 76 | } 77 | 78 | #if CHECK_INPUT 79 | if (ip + ctrl > in_end) 80 | { 81 | SET_ERRNO (EINVAL); 82 | return 0; 83 | } 84 | #endif 85 | 86 | #ifdef lzf_movsb 87 | lzf_movsb (op, ip, ctrl); 88 | #else 89 | do 90 | *op++ = *ip++; 91 | while (--ctrl); 92 | #endif 93 | } 94 | else /* back reference */ 95 | { 96 | unsigned int len = ctrl >> 5; 97 | 98 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; 99 | 100 | #if CHECK_INPUT 101 | if (ip >= in_end) 102 | { 103 | SET_ERRNO (EINVAL); 104 | return 0; 105 | } 106 | #endif 107 | if (len == 7) 108 | { 109 | len += *ip++; 110 | #if CHECK_INPUT 111 | if (ip >= in_end) 112 | { 113 | SET_ERRNO (EINVAL); 114 | return 0; 115 | } 116 | #endif 117 | } 118 | 119 | ref -= *ip++; 120 | 121 | if (op + len + 2 > out_end) 122 | { 123 | SET_ERRNO (E2BIG); 124 | return 0; 125 | } 126 | 127 | if (ref < (u8 *)out_data) 128 | { 129 | SET_ERRNO (EINVAL); 130 | return 0; 131 | } 132 | 133 | #ifdef lzf_movsb 134 | len += 2; 135 | lzf_movsb (op, ref, len); 136 | #else 137 | *op++ = *ref++; 138 | *op++ = *ref++; 139 | 140 | do 141 | *op++ = *ref++; 142 | while (--len); 143 | #endif 144 | } 145 | } 146 | while (ip < in_end); 147 | 148 | return op - (u8 *)out_data; 149 | } 150 | 151 | -------------------------------------------------------------------------------- /quickcheck/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod sys { 2 | use libc::{c_uint,c_void}; 3 | use lzf::{LzfError, LzfResult}; 4 | use std::io::Error; 5 | 6 | extern { 7 | fn lzf_compress(in_data: *const c_void, in_len: c_uint, 8 | out_data: *const c_void, out_len: c_uint) -> c_uint; 9 | fn lzf_decompress(in_data: *const c_void, in_len: c_uint, 10 | out_data: *const c_void, out_len: c_uint) -> c_uint; 11 | } 12 | 13 | pub fn compress(data: &[u8]) -> LzfResult> { 14 | let data_len = data.len(); 15 | let mut out : Vec = Vec::with_capacity(data_len); 16 | 17 | let result = unsafe { lzf_compress(data.as_ptr() as *const c_void, data_len as c_uint, 18 | out.as_ptr() as *const c_void, data_len as c_uint) }; 19 | 20 | match result { 21 | 0 => Err(LzfError::NoCompressionPossible), 22 | _ => { 23 | unsafe { out.set_len(result as usize) }; 24 | Ok(out) 25 | } 26 | } 27 | } 28 | 29 | pub fn decompress(data: &[u8], out_len: usize) -> LzfResult> { 30 | let mut out : Vec = Vec::with_capacity(out_len); 31 | 32 | if data.len() == 0 { 33 | return Err(LzfError::DataCorrupted); 34 | } 35 | 36 | let result = unsafe { lzf_decompress(data.as_ptr() as *const c_void, data.len() as c_uint, 37 | out.as_ptr() as *const c_void, out_len as c_uint) }; 38 | match result { 39 | 0 => { 40 | match Error::last_os_error().raw_os_error() { 41 | Some(7) => Err(LzfError::BufferTooSmall), 42 | Some(22) => Err(LzfError::DataCorrupted), 43 | Some(e) => Err(LzfError::UnknownError(e)), 44 | None => Err(LzfError::UnknownError(0)), 45 | } 46 | }, 47 | _ => { 48 | unsafe { out.set_len(result as usize) }; 49 | Ok(out) 50 | } 51 | } 52 | } 53 | } 54 | 55 | #[cfg(test)] 56 | mod test { 57 | use super::sys; 58 | use lzf::LzfError; 59 | 60 | #[test] 61 | fn test_compress_skips_short() { 62 | match sys::compress("foo".as_bytes()) { 63 | Ok(_) => panic!("Compression did _something_, with is wrong for 'foo'"), 64 | Err(err) => assert_eq!(LzfError::NoCompressionPossible, err) 65 | } 66 | } 67 | 68 | #[test] 69 | fn test_compress_lorem() { 70 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."; 71 | 72 | match sys::compress(lorem.as_bytes()) { 73 | Ok(compressed) => { 74 | assert_eq!(272, compressed.len()) 75 | } 76 | Err(err) => panic!("Compression failed with error {:?}", err) 77 | } 78 | } 79 | 80 | #[test] 81 | fn test_compress_decompress_lorem_round() { 82 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."; 83 | 84 | let compressed = match sys::compress(lorem.as_bytes()) { 85 | Ok(c) => c, 86 | Err(err) => panic!("Compression failed with error {:?}", err) 87 | }; 88 | 89 | match sys::decompress(&compressed, lorem.len()) { 90 | Ok(decompressed) => { 91 | assert_eq!(lorem.len(), decompressed.len()); 92 | assert_eq!(lorem.as_bytes(), &decompressed[..]); 93 | }, 94 | Err(err) => panic!("Decompression failed with error {:?}", err) 95 | }; 96 | } 97 | 98 | #[test] 99 | fn test_decompress_fails_with_short_buffer() { 100 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."; 101 | 102 | let compressed = match sys::compress(lorem.as_bytes()) { 103 | Ok(c) => c, 104 | Err(err) => panic!("Compression failed with error {:?}", err) 105 | }; 106 | 107 | match sys::decompress(&compressed, 10) { 108 | Ok(_) => panic!("Decompression worked. That should not happen"), 109 | Err(err) => assert_eq!(LzfError::BufferTooSmall, err) 110 | } 111 | } 112 | 113 | #[test] 114 | fn test_decompress_fails_for_corrupted_data() { 115 | let lorem = "Lorem ipsum dolor sit amet"; 116 | 117 | match sys::decompress(lorem.as_bytes(), lorem.len()) { 118 | Ok(_) => panic!("Decompression worked. That should not happen"), 119 | Err(err) => assert_eq!(LzfError::DataCorrupted, err) 120 | } 121 | } 122 | 123 | #[test] 124 | fn test_empty() { 125 | assert_eq!(LzfError::DataCorrupted, sys::decompress(&[], 10).unwrap_err()); 126 | } 127 | } 128 | 129 | #[cfg(test)] 130 | mod quickcheck_test { 131 | use super::sys; 132 | use quickcheck::{quickcheck, TestResult}; 133 | use lzf::{self, LzfError}; 134 | 135 | fn roundtrip_native(data: Vec) -> TestResult { 136 | let compr = match sys::compress(&data) { 137 | Ok(compr) => compr, 138 | Err(LzfError::NoCompressionPossible) => return TestResult::discard(), 139 | Err(LzfError::DataCorrupted) => return TestResult::discard(), 140 | e @ _ => panic!("{:?}", e), 141 | }; 142 | let decompr = sys::decompress(&compr, data.len()).unwrap(); 143 | TestResult::from_bool(data == decompr) 144 | } 145 | 146 | #[test] 147 | fn qc_native_roundtrip() { 148 | quickcheck(roundtrip_native as fn(_) -> _); 149 | } 150 | 151 | fn compare_compress(data: Vec) -> TestResult { 152 | let rust_compr = lzf::compress(&data); 153 | let native_compr = sys::compress(&data); 154 | TestResult::from_bool(rust_compr == native_compr) 155 | } 156 | 157 | #[test] 158 | fn qc_native_matches_rust() { 159 | quickcheck(compare_compress as fn(_) -> _); 160 | } 161 | 162 | fn compare_decompress(data: Vec) -> TestResult { 163 | let rust_decompr = lzf::decompress(&data, data.len()*2); 164 | let native_decompr = sys::decompress(&data, data.len()*2); 165 | TestResult::from_bool(rust_decompr == native_decompr) 166 | } 167 | 168 | #[test] 169 | fn qc_native_decompress_matches_rust() { 170 | quickcheck(compare_decompress as fn(_) -> _); 171 | } 172 | 173 | fn native_compress_rust_decompress(data: Vec) -> TestResult { 174 | let compr = match sys::compress(&data) { 175 | Ok(compr) => compr, 176 | Err(LzfError::NoCompressionPossible) => return TestResult::discard(), 177 | Err(LzfError::DataCorrupted) => return TestResult::discard(), 178 | e @ _ => panic!("{:?}", e), 179 | }; 180 | 181 | let decompr = lzf::decompress(&compr, data.len()).unwrap(); 182 | TestResult::from_bool(data == decompr) 183 | } 184 | 185 | fn rust_compress_native_decompress(data: Vec) -> TestResult { 186 | let compr = match lzf::compress(&data) { 187 | Ok(compr) => compr, 188 | Err(LzfError::NoCompressionPossible) => return TestResult::discard(), 189 | Err(LzfError::DataCorrupted) => return TestResult::discard(), 190 | e @ _ => panic!("{:?}", e), 191 | }; 192 | 193 | let decompr = sys::decompress(&compr, data.len()).unwrap(); 194 | TestResult::from_bool(data == decompr) 195 | } 196 | 197 | #[test] 198 | fn qc_native_compress_rust_decompress() { 199 | quickcheck(native_compress_rust_decompress as fn(_) -> _); 200 | } 201 | 202 | #[test] 203 | fn qc_rust_compress_native_decompress() { 204 | quickcheck(rust_compress_native_decompress as fn(_) -> _); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /src/compress.rs: -------------------------------------------------------------------------------- 1 | use super::{LzfError, LzfResult}; 2 | use std::cmp; 3 | 4 | const HLOG: usize = 16; 5 | const HSIZE: u32 = 1 << HLOG; 6 | const MAX_OFF: usize = 1 << 13; 7 | const MAX_REF: usize = (1 << 8) + (1 << 3); 8 | const MAX_LIT: i32 = 1 << 5; 9 | 10 | fn first(p: &[u8], off: usize) -> u32 { 11 | ((p[off] as u32) << 8) | p[off + 1] as u32 12 | } 13 | 14 | fn next(v: u32, p: &[u8], off: usize) -> u32 { 15 | (v << 8) | p[off + 2] as u32 16 | } 17 | 18 | fn idx(h: u32) -> usize { 19 | let h = h as u64; 20 | ( 21 | // 8 = 3*8-HLOG, but HLOG is constant at 16 22 | (h.wrapping_shr(8).wrapping_sub(h * 5)) & (HSIZE - 1) as u64 23 | ) as usize 24 | } 25 | 26 | fn not(i: i32) -> i32 { 27 | if i == 0 { 28 | 1 29 | } else { 30 | 0 31 | } 32 | } 33 | 34 | /// Compress the given data, if possible. 35 | /// The return value will be set to the error if compression fails. 36 | /// 37 | /// The buffer is always set to the same size as the input buffer. 38 | /// If that is not enough to hold the lzf-compressed data, 39 | /// an error will be returned. 40 | /// 41 | /// Example: 42 | /// 43 | /// ```rust 44 | /// let data = "aaaaaaaaa"; 45 | /// let compressed = lzf::compress(data.as_bytes()).unwrap(); 46 | /// ``` 47 | pub fn compress(data: &[u8]) -> LzfResult> { 48 | let in_len = data.len(); 49 | let out_buf_len = in_len; 50 | let mut out = vec![0; out_buf_len]; 51 | 52 | let mut out_len: i32 = 1; /* start run by default */ 53 | 54 | let mut htab = vec![0; 1 << HLOG]; 55 | 56 | let mut current_offset = 0; 57 | 58 | if in_len < 2 { 59 | return Err(LzfError::NoCompressionPossible); 60 | } 61 | 62 | let mut lit: i32 = 0; 63 | 64 | let mut hval: u32; 65 | let mut ref_offset; 66 | 67 | hval = first(data, current_offset); 68 | 69 | while current_offset < in_len - 2 { 70 | hval = next(hval, data, current_offset); 71 | let hslot_idx = idx(hval); 72 | 73 | ref_offset = htab[hslot_idx]; 74 | htab[hslot_idx] = current_offset; 75 | 76 | let off = current_offset.wrapping_sub(ref_offset).wrapping_sub(1); 77 | if off < MAX_OFF 78 | && current_offset + 4 < in_len 79 | && ref_offset > 0 80 | && ref_offset < in_len - 2 81 | && data[ref_offset] == data[current_offset] 82 | && data[ref_offset + 1] == data[current_offset + 1] 83 | && data[ref_offset + 2] == data[current_offset + 2] 84 | { 85 | let mut len = 2; 86 | let maxlen = cmp::min(in_len - current_offset - len, MAX_REF); 87 | 88 | /* stop run */ 89 | out[(out_len - lit - 1) as usize] = (lit as u8).wrapping_sub(1); 90 | out_len -= not(lit); /* undo run if length is zero */ 91 | 92 | if out_len as i32 + 3 + 1 >= out_buf_len as i32 { 93 | return Err(LzfError::NoCompressionPossible); 94 | } 95 | 96 | len += 1; 97 | while len < maxlen && data[ref_offset + len] == data[current_offset + len] { 98 | len += 1; 99 | } 100 | 101 | len -= 2; /* len is now #octets - 1 */ 102 | current_offset += 1; 103 | 104 | if len < 7 { 105 | out[out_len as usize] = (off >> 8) as u8 + (len << 5) as u8; 106 | out_len += 1; 107 | } else { 108 | out[out_len as usize] = (off >> 8) as u8 + (7 << 5); 109 | out[out_len as usize + 1] = (len as u8).wrapping_sub(7); 110 | out_len += 2; 111 | } 112 | 113 | out[out_len as usize] = off as u8; 114 | out_len += 2; /* start run */ 115 | lit = 0; 116 | 117 | /* we add here, because we later substract from the total length */ 118 | current_offset += len - 1; 119 | 120 | if current_offset >= in_len { 121 | break; 122 | } 123 | 124 | hval = first(data, current_offset); 125 | 126 | hval = next(hval, data, current_offset); 127 | htab[idx(hval)] = current_offset; 128 | current_offset += 1; 129 | 130 | hval = next(hval, data, current_offset); 131 | htab[idx(hval)] = current_offset; 132 | current_offset += 1; 133 | } else { 134 | /* one more literal byte we must copy */ 135 | if out_len >= out_buf_len as i32 { 136 | return Err(LzfError::NoCompressionPossible); 137 | } 138 | 139 | lit += 1; 140 | out[out_len as usize] = data[current_offset]; 141 | out_len += 1; 142 | current_offset += 1; 143 | 144 | if lit == MAX_LIT { 145 | /* stop run */ 146 | out[(out_len - lit - 1) as usize] = (lit as u8).wrapping_sub(1); 147 | lit = 0; 148 | out_len += 1; /* start run */ 149 | } 150 | } 151 | } 152 | 153 | /* at most 3 bytes can be missing here */ 154 | if out_len + 3 > out_buf_len as i32 { 155 | return Err(LzfError::NoCompressionPossible); 156 | } 157 | 158 | while current_offset < in_len { 159 | lit += 1; 160 | out[out_len as usize] = data[current_offset]; 161 | out_len += 1; 162 | current_offset += 1; 163 | 164 | if lit == MAX_LIT { 165 | /* stop run */ 166 | out[(out_len - lit - 1) as usize] = (lit as u8).wrapping_sub(1); 167 | lit = 0; 168 | out_len += 1; /* start run */ 169 | } 170 | } 171 | 172 | /* end run */ 173 | out[(out_len - lit - 1) as usize] = (lit as u8).wrapping_sub(1); 174 | out_len -= not(lit); /* undo run if length is zero */ 175 | 176 | unsafe { out.set_len(out_len as usize) }; 177 | 178 | Ok(out) 179 | } 180 | 181 | #[test] 182 | fn test_compress_skips_short() { 183 | match compress("foo".as_bytes()) { 184 | Ok(_) => panic!("Compression did _something_, which is wrong for 'foo'"), 185 | Err(err) => assert_eq!(LzfError::NoCompressionPossible, err), 186 | } 187 | } 188 | 189 | #[test] 190 | fn test_compress_lorem() { 191 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod \ 192 | tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At \ 193 | vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, \ 194 | no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit \ 195 | amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut \ 196 | labore et dolore magna aliquyam erat, sed diam voluptua."; 197 | 198 | match compress(lorem.as_bytes()) { 199 | Ok(compressed) => { 200 | assert_eq!(272, compressed.len()) 201 | } 202 | Err(err) => panic!("Compression failed with error {:?}", err), 203 | } 204 | } 205 | 206 | #[test] 207 | fn test_compress_decompress_lorem_round() { 208 | use super::decompress; 209 | 210 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod \ 211 | tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At \ 212 | vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, \ 213 | no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit \ 214 | amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut \ 215 | labore et dolore magna aliquyam erat, sed diam voluptua."; 216 | 217 | let compressed = match compress(lorem.as_bytes()) { 218 | Ok(c) => c, 219 | Err(err) => panic!("Compression failed with error {:?}", err), 220 | }; 221 | 222 | match decompress(&compressed, lorem.len()) { 223 | Ok(decompressed) => { 224 | assert_eq!(lorem.len(), decompressed.len()); 225 | assert_eq!(lorem.as_bytes(), &decompressed[..]); 226 | } 227 | Err(err) => panic!("Decompression failed with error {:?}", err), 228 | }; 229 | } 230 | 231 | #[test] 232 | fn test_alice_wonderland_both() { 233 | let alice = "\r\n\r\n\r\n\r\n ALICE'S ADVENTURES IN WONDERLAND\r\n"; 234 | 235 | let compressed = match compress(alice.as_bytes()) { 236 | Ok(c) => c, 237 | Err(err) => panic!("Compression failed with error {:?}", err), 238 | }; 239 | 240 | let c_compressed = match super::compress(alice.as_bytes()) { 241 | Ok(c) => c, 242 | Err(err) => panic!("Compression failed with error {:?}", err), 243 | }; 244 | 245 | assert_eq!(&compressed[..], &c_compressed[..]); 246 | } 247 | 248 | #[test] 249 | fn quickcheck_found_bug() { 250 | let inp = vec![ 251 | 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 1, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 0, 5, 0, 252 | 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 1, 5, 0, 1, 6, 0, 1, 7, 0, 1, 8, 0, 253 | 1, 9, 0, 1, 10, 0, 0, 254 | ]; 255 | 256 | assert_eq!(LzfError::NoCompressionPossible, compress(&inp).unwrap_err()); 257 | } 258 | 259 | #[test] 260 | fn quickcheck_found_bug2() { 261 | let inp = vec![0]; 262 | 263 | assert_eq!(LzfError::NoCompressionPossible, compress(&inp).unwrap_err()); 264 | } 265 | -------------------------------------------------------------------------------- /src/decompress.rs: -------------------------------------------------------------------------------- 1 | use super::{LzfError, LzfResult}; 2 | 3 | /// Decompress the given data, if possible. 4 | /// An error will be returned if decompression fails. 5 | /// 6 | /// The length of the output buffer can be specified. 7 | /// If the output buffer is not large enough to hold the decompressed data, 8 | /// BufferTooSmall is returned. 9 | /// Otherwise the number of decompressed bytes 10 | /// (i.e. the original length of the data) is returned. 11 | /// 12 | /// If an error in the compressed data is detected, DataCorrupted is returned. 13 | /// 14 | /// Example: 15 | /// 16 | /// ```rust,no_run 17 | /// let data = "[your-compressed-data]"; 18 | /// let decompressed = lzf::decompress(data.as_bytes(), 10); 19 | /// ``` 20 | pub fn decompress(data: &[u8], out_len_should: usize) -> LzfResult> { 21 | let mut current_offset = 0; 22 | 23 | let in_len = data.len(); 24 | if in_len == 0 { 25 | return Err(LzfError::DataCorrupted); 26 | } 27 | 28 | // We have sanity checks to not exceed this capacity. 29 | let mut output = vec![0; out_len_should]; 30 | let mut out_len: usize = 0; 31 | 32 | while current_offset < in_len { 33 | let mut ctrl = data[current_offset] as usize; 34 | current_offset += 1; 35 | 36 | if ctrl < (1 << 5) { 37 | ctrl += 1; 38 | 39 | if out_len + ctrl > out_len_should { 40 | return Err(LzfError::BufferTooSmall); 41 | } 42 | 43 | if current_offset + ctrl > in_len { 44 | return Err(LzfError::DataCorrupted); 45 | } 46 | 47 | // We can simply memcpy everything from the input to the output 48 | output[out_len..(out_len + ctrl)] 49 | .copy_from_slice(&data[current_offset..(current_offset + ctrl)]); 50 | 51 | current_offset += ctrl; 52 | out_len += ctrl; 53 | } else { 54 | let mut len = ctrl >> 5; 55 | 56 | let mut ref_offset = (((ctrl & 0x1f) << 8) + 1) as i32; 57 | 58 | if current_offset >= in_len { 59 | return Err(LzfError::DataCorrupted); 60 | } 61 | 62 | if len == 7 { 63 | len += data[current_offset] as usize; 64 | current_offset += 1; 65 | 66 | if current_offset >= in_len { 67 | return Err(LzfError::DataCorrupted); 68 | } 69 | } 70 | 71 | ref_offset += data[current_offset] as i32; 72 | current_offset += 1; 73 | 74 | if out_len + len + 2 > out_len_should { 75 | return Err(LzfError::BufferTooSmall); 76 | } 77 | 78 | let mut ref_pos = (out_len as i32) - ref_offset; 79 | if ref_pos < 0 { 80 | return Err(LzfError::DataCorrupted); 81 | } 82 | 83 | let c = output[ref_pos as usize]; 84 | output[out_len] = c; 85 | out_len += 1; 86 | ref_pos += 1; 87 | 88 | let c = output[ref_pos as usize]; 89 | output[out_len] = c; 90 | out_len += 1; 91 | ref_pos += 1; 92 | 93 | while len > 0 { 94 | let c = output[ref_pos as usize]; 95 | output[out_len] = c; 96 | out_len += 1; 97 | ref_pos += 1; 98 | len -= 1; 99 | } 100 | } 101 | } 102 | 103 | // Set the real length now, user might have passed a bigger buffer in the first place. 104 | unsafe { output.set_len(out_len) }; 105 | 106 | Ok(output) 107 | } 108 | 109 | #[test] 110 | fn test_decompress_lorem() { 111 | use super::compress; 112 | 113 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod \ 114 | tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At \ 115 | vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, \ 116 | no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit \ 117 | amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut \ 118 | labore et dolore magna aliquyam erat, sed diam voluptua."; 119 | 120 | let compressed = compress(lorem.as_bytes()).unwrap(); 121 | 122 | let decompressed = decompress(&compressed[..], lorem.len()).unwrap(); 123 | assert_eq!(lorem.as_bytes(), &decompressed[..]); 124 | 125 | let decompressed = decompress(&compressed[..], 1000).unwrap(); 126 | assert_eq!(lorem.len(), decompressed.len()); 127 | } 128 | 129 | #[test] 130 | fn test_decompress_fails_with_short_buffer() { 131 | use super::compress; 132 | 133 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod \ 134 | tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At \ 135 | vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, \ 136 | no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit \ 137 | amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut \ 138 | labore et dolore magna aliquyam erat, sed diam voluptua."; 139 | 140 | let compressed = match compress(lorem.as_bytes()) { 141 | Ok(c) => c, 142 | Err(err) => panic!("Compression failed with error {:?}", err), 143 | }; 144 | 145 | match decompress(&compressed, 10) { 146 | Ok(_) => panic!("Decompression worked. That should not happen"), 147 | Err(err) => assert_eq!(LzfError::BufferTooSmall, err), 148 | } 149 | } 150 | 151 | #[test] 152 | fn test_decompress_fails_for_corrupted_data() { 153 | let lorem = "Lorem ipsum dolor sit amet"; 154 | 155 | match decompress(lorem.as_bytes(), lorem.len()) { 156 | Ok(_) => panic!("Decompression worked. That should not happen"), 157 | Err(err) => assert_eq!(LzfError::DataCorrupted, err), 158 | } 159 | } 160 | 161 | #[test] 162 | fn test_alice_wonderland() { 163 | use super::compress; 164 | 165 | let alice = "\r\n\r\n\r\n\r\n ALICE'S ADVENTURES IN WONDERLAND\r\n"; 166 | 167 | let compressed = match compress(alice.as_bytes()) { 168 | Ok(c) => c, 169 | Err(err) => panic!("Compression failed with error {:?}", err), 170 | }; 171 | 172 | match decompress(&compressed, alice.len()) { 173 | Ok(decompressed) => { 174 | assert_eq!(alice.len(), decompressed.len()); 175 | assert_eq!(alice.as_bytes(), &decompressed[..]); 176 | } 177 | Err(err) => panic!("Decompression failed with error {:?}", err), 178 | } 179 | } 180 | 181 | #[test] 182 | fn easily_compressible() { 183 | // RDB regression 184 | let data = vec![1, 97, 97, 224, 187, 0, 1, 97, 97]; 185 | let real_length = 200; 186 | 187 | let text = decompress(&data, real_length).unwrap(); 188 | assert_eq!(200, text.len()); 189 | assert_eq!(97, text[0]); 190 | assert_eq!(97, text[199]); 191 | } 192 | 193 | #[test] 194 | fn test_empty() { 195 | assert_eq!(LzfError::DataCorrupted, decompress(&[], 10).unwrap_err()); 196 | } 197 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! lzf is a very small data compression library. 2 | //! 3 | //! Originally written as [LibLZF](http://software.schmorp.de/pkg/liblzf.html) 4 | //! by Marc Lehmann in portable C. 5 | //! 6 | //! This Rust library is a rewrite of the original C code 7 | //! and fully compatible with compressed data from the C code (and vice versa). 8 | //! 9 | //! # Basic Operation 10 | //! 11 | //! ```rust 12 | //! # use lzf; 13 | //! let data = "aaaaaaaaa"; 14 | //! 15 | //! let compressed = lzf::compress(data.as_bytes()).unwrap(); 16 | //! 17 | //! let decompressed = lzf::decompress(&compressed, data.len()).unwrap(); 18 | //! ``` 19 | #![deny(missing_docs)] 20 | 21 | use std::fmt; 22 | 23 | mod compress; 24 | mod decompress; 25 | pub use compress::compress; 26 | pub use decompress::decompress; 27 | 28 | /// Errors that can occur during Compression or Decompression. 29 | #[derive(PartialEq, Eq, Clone, Debug, Copy)] 30 | pub enum LzfError { 31 | /// The provided buffer is too small to handle the uncompressed data 32 | BufferTooSmall, 33 | /// The given compressed data is corrupted 34 | DataCorrupted, 35 | /// The given data can't be compressed 36 | NoCompressionPossible, 37 | /// An unknown error occured 38 | UnknownError(i32), 39 | } 40 | 41 | impl fmt::Display for LzfError { 42 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 43 | match *self { 44 | LzfError::BufferTooSmall => { 45 | write!( 46 | f, 47 | "the given buffer is too small to handle the uncompressed data" 48 | ) 49 | } 50 | LzfError::DataCorrupted => { 51 | write!(f, "the given data is corrupted") 52 | } 53 | LzfError::NoCompressionPossible => { 54 | write!(f, "the input data cannot be compressed") 55 | } 56 | LzfError::UnknownError(err) => { 57 | write!(f, "unknown error, code {}", err) 58 | } 59 | } 60 | } 61 | } 62 | 63 | /// A Result providing the underlying data or a compression/decompression error 64 | pub type LzfResult = Result; 65 | 66 | #[test] 67 | fn test_compress_skips_short() { 68 | match compress("foo".as_bytes()) { 69 | Ok(_) => panic!("Compression did _something_, which is wrong for 'foo'"), 70 | Err(err) => assert_eq!(LzfError::NoCompressionPossible, err), 71 | } 72 | } 73 | 74 | #[test] 75 | fn test_compress_lorem() { 76 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod \ 77 | tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At \ 78 | vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, \ 79 | no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit \ 80 | amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut \ 81 | labore et dolore magna aliquyam erat, sed diam voluptua."; 82 | 83 | match compress(lorem.as_bytes()) { 84 | Ok(compressed) => { 85 | assert_eq!(272, compressed.len()) 86 | } 87 | Err(err) => panic!("Compression failed with error {:?}", err), 88 | } 89 | } 90 | 91 | #[test] 92 | fn test_compress_decompress_lorem_round() { 93 | let lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod \ 94 | tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At \ 95 | vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, \ 96 | no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit \ 97 | amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut \ 98 | labore et dolore magna aliquyam erat, sed diam voluptua."; 99 | 100 | let compressed = match compress(lorem.as_bytes()) { 101 | Ok(c) => c, 102 | Err(err) => panic!("Compression failed with error {:?}", err), 103 | }; 104 | 105 | match decompress(&compressed, lorem.len()) { 106 | Ok(decompressed) => { 107 | assert_eq!(lorem.len(), decompressed.len()); 108 | assert_eq!(lorem.as_bytes(), &decompressed[..]); 109 | } 110 | Err(err) => panic!("Decompression failed with error {:?}", err), 111 | }; 112 | } 113 | 114 | #[cfg(test)] 115 | mod quickcheck_test { 116 | use super::*; 117 | use quickcheck::{quickcheck, TestResult}; 118 | 119 | fn compress_decompress_round(data: Vec) -> TestResult { 120 | let compr = match compress(&data) { 121 | Ok(compr) => compr, 122 | Err(LzfError::NoCompressionPossible) => return TestResult::discard(), 123 | Err(LzfError::DataCorrupted) => return TestResult::discard(), 124 | e @ _ => panic!("{:?}", e), 125 | }; 126 | let decompr = decompress(&compr, data.len()).unwrap(); 127 | TestResult::from_bool(data == decompr) 128 | } 129 | 130 | #[test] 131 | fn qc_roundtrip() { 132 | quickcheck(compress_decompress_round as fn(_) -> _); 133 | } 134 | } 135 | --------------------------------------------------------------------------------