cpclib_crunchers/
lib.rs

1#![feature(vec_into_raw_parts)]
2
3use lz49::lz49_encode_legacy;
4use lzsa::{LzsaMinMatch, LzsaVersion};
5#[cfg(not(target_arch = "wasm32"))]
6use shrinkler::ShrinklerConfiguration;
7
8#[cfg(not(target_arch = "wasm32"))]
9pub mod apultra;
10#[cfg(not(target_arch = "wasm32"))]
11pub mod exomizer;
12#[cfg(not(target_arch = "wasm32"))]
13pub mod lz4;
14pub mod lz48;
15pub mod lz49;
16#[cfg(not(target_arch = "wasm32"))]
17pub mod zx0;
18
19#[cfg(not(target_arch = "wasm32"))]
20pub mod shrinkler;
21
22pub mod lzsa;
23
24pub enum CompressMethod {
25    #[cfg(not(target_arch = "wasm32"))]
26    Apultra,
27    #[cfg(not(target_arch = "wasm32"))]
28    Exomizer,
29    #[cfg(not(target_arch = "wasm32"))]
30    Lz4,
31    Lz48,
32    Lz49,
33    Lzsa(LzsaVersion, Option<LzsaMinMatch>),
34    #[cfg(not(target_arch = "wasm32"))]
35    Shrinkler(ShrinklerConfiguration),
36    #[cfg(not(target_arch = "wasm32"))]
37    Upkr,
38    #[cfg(not(target_arch = "wasm32"))]
39    Zx0
40}
41
42impl CompressMethod {
43    pub fn compress(&self, data: &[u8]) -> Result<Vec<u8>, ()> {
44        match self {
45            #[cfg(not(target_arch = "wasm32"))]
46            CompressMethod::Apultra => Ok(apultra::compress(data)),
47            #[cfg(not(target_arch = "wasm32"))]
48            CompressMethod::Exomizer => Ok(exomizer::compress(data)),
49            #[cfg(not(target_arch = "wasm32"))]
50            CompressMethod::Lz4 => Ok(lz4::compress(data)),
51            CompressMethod::Lz48 => Ok(lz48::lz48_encode_legacy(data)),
52            CompressMethod::Lz49 => Ok(lz49_encode_legacy(data)),
53            CompressMethod::Lzsa(version, minmatch) => lzsa::compress(data, *version, *minmatch),
54            #[cfg(not(target_arch = "wasm32"))]
55            CompressMethod::Shrinkler(conf) => Ok(conf.compress(data)),
56            #[cfg(not(target_arch = "wasm32"))]
57            CompressMethod::Upkr => {
58                let mut config = upkr::Config::default();
59                config.use_bitstream = true;
60                config.bitstream_is_big_endian = true;
61                config.invert_bit_encoding = true;
62                config.simplified_prob_update = true;
63                let level = 9;
64                Ok(upkr::pack(data, level, &config, None))
65            },
66            #[cfg(not(target_arch = "wasm32"))]
67            CompressMethod::Zx0 => Ok(zx0::compress(data))
68        }
69    }
70}