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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use std::io;
use std::io::{Read, Write};
use std::mem::transmute_copy;
use std::process::{Command, Stdio};
use std::str::FromStr;
use std::thread::spawn;

use bytesize::ByteSize;

use crate::errors::Result;
use crate::{Compression, Error};

#[derive(Copy, Clone)]
pub enum Level {
    Best,
    Numeric(u32),
}

impl Level {
    pub fn to_numeric(&self, compression: Compression) -> u32 {
        match self {
            Level::Best => compression.best_level(),
            Level::Numeric(n) => *n,
        }
    }
}

impl FromStr for Level {
    type Err = ();

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.parse::<u32>() {
            Ok(level) => Ok(Self::Numeric(level)),
            Err(_) => {
                if s == "best" {
                    Ok(Self::Best)
                } else {
                    Err(())
                }
            }
        }
    }
}

pub fn create_compressor(method: Compression, level: Level) -> Box<dyn Compress> {
    let level = level.to_numeric(method);

    match method {
        Compression::Gzip => Box::new(GzipCompressor::new(level)),
        Compression::Xz => Box::new(XzCompressor::new(level)),
        Compression::Zstd => Box::new(ZstdCompressor::new(level)),
        Compression::Bzip2 => Box::new(Bzip2Compressor::new(level)),
        Compression::None => Box::new(NoCompressor::new()),
        Compression::Brotli => Box::new(BrotliCompressor::new(level)),
        Compression::Bzip3 => Box::new(Bzip3Compressor::new(Bzip3Compressor::level_to_block_size(
            level,
        ))),
        Compression::External => {
            unreachable!("Invalid argument")
        }
    }
}

pub fn create_decompressor(method: Compression) -> Box<dyn Decompress> {
    match method {
        Compression::Gzip => Box::new(GzipDecompressor),
        Compression::Xz => Box::new(XzDecompressor),
        Compression::Zstd => Box::new(ZstdDecompressor),
        Compression::Bzip2 => Box::new(Bzip2Decompressor),
        Compression::None => Box::new(NoDecompressor),
        Compression::Brotli => Box::new(BrotliDecompressor),
        Compression::Bzip3 => Box::new(Bzip3Decompressor),
        Compression::External => {
            unreachable!("Invalid argument")
        }
    }
}

pub trait Compress {
    /// Returns the size after compression
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64>;
}

pub trait Decompress {
    /// Returns size of uncompressed content
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64>;
}

pub struct GzipCompressor {
    level: flate2::Compression,
}

impl Compress for GzipCompressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut encoder = flate2::read::GzEncoder::new(from, self.level);
        Ok(io::copy(&mut encoder, to)?)
    }
}

impl GzipCompressor {
    pub fn new(level: u32) -> Self {
        Self {
            level: flate2::Compression::new(level),
        }
    }
}

impl Default for GzipCompressor {
    fn default() -> Self {
        GzipCompressor::new(flate2::Compression::default().level())
    }
}

#[derive(Default)]
pub struct NoCompressor;

impl Compress for NoCompressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Ok(io::copy(from, to)?)
    }
}

impl NoCompressor {
    pub fn new() -> NoCompressor {
        NoCompressor
    }
}

pub struct XzCompressor {
    level: u32,
}

impl XzCompressor {
    pub fn new(level: u32) -> XzCompressor {
        Self { level }
    }
}

impl Compress for XzCompressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut encoder = xz2::read::XzEncoder::new(from, self.level);
        Ok(io::copy(&mut encoder, to)?)
    }
}

pub struct ZstdCompressor {
    level: u32,
}

impl ZstdCompressor {
    pub fn new(level: u32) -> ZstdCompressor {
        Self { level }
    }
}

impl Compress for ZstdCompressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut encoder = zstd::stream::read::Encoder::new(from, self.level as i32)?;
        Ok(io::copy(&mut encoder, to)?)
    }
}

pub struct Bzip2Compressor {
    level: u32,
}

impl Bzip2Compressor {
    pub fn new(level: u32) -> Self {
        Self { level }
    }
}

impl Compress for Bzip2Compressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut encoder = bzip2::read::BzEncoder::new(from, bzip2::Compression::new(self.level));
        Ok(io::copy(&mut encoder, to)?)
    }
}

struct BrotliCompressor {
    quality: u32,
}

impl BrotliCompressor {
    pub fn new(quality: u32) -> BrotliCompressor {
        Self { quality }
    }
}

impl Compress for BrotliCompressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut reader = brotli::CompressorReader::new(from, 4096, self.quality, 21);
        Ok(io::copy(&mut reader, to)?)
    }
}

struct Bzip3Compressor {
    block_size: usize,
}

impl Compress for Bzip3Compressor {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut encoder = match bzip3::read::Bz3Encoder::new(from, self.block_size) {
            Ok(d) => d,
            Err(e) => return Err(Error::CompressorError(format!("{}", e))),
        };
        Ok(io::copy(&mut encoder, to)?)
    }
}

impl Bzip3Compressor {
    pub fn new(block_size: usize) -> Self {
        Self { block_size }
    }

    /// Level ranging from 1 to 9 -> block size ranging from 65kiB to 511MiB
    /// block size will be coerced within its valid range when level is outside 1..=9
    fn level_to_block_size(level: u32) -> usize {
        let low = ByteSize::kib(65).0 as usize;
        let up = ByteSize::mib(511).0 as usize;
        let part = (up - low) / 9;

        match level {
            l @ 2..=8 => low + l as usize * part,
            u32::MIN..=1 => low,
            9..=u32::MAX => up,
        }
    }
}

pub struct GzipDecompressor;
pub struct XzDecompressor;
pub struct ZstdDecompressor;
pub struct Bzip2Decompressor;
pub struct NoDecompressor;
pub struct BrotliDecompressor;
pub struct Bzip3Decompressor;

impl Decompress for GzipDecompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Ok(io::copy(&mut flate2::read::GzDecoder::new(from), to)?)
    }
}

impl Decompress for XzDecompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Ok(io::copy(&mut xz2::read::XzDecoder::new(from), to)?)
    }
}

impl Decompress for ZstdDecompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Ok(io::copy(&mut zstd::stream::read::Decoder::new(from)?, to)?)
    }
}

impl Decompress for Bzip2Decompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Ok(io::copy(&mut bzip2::read::BzDecoder::new(from), to)?)
    }
}

impl Decompress for NoDecompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Ok(io::copy(from, to)?)
    }
}

pub struct ExternalFilter<'a> {
    cmd: &'a Vec<String>,
}

impl<'a> ExternalFilter<'a> {
    pub fn new(cmd: &'a Vec<String>) -> Self {
        Self { cmd }
    }

    // TODO: clippy lint warning
    #[allow(clippy::needless_pass_by_ref_mut)]
    fn process_filter(args: &Vec<String>, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let cmd = args;
        let mut command = Command::new(&cmd[0]);
        if cmd.len() > 1 {
            command.args(&cmd[1..]);
        }
        command
            .stdout(Stdio::piped())
            .stdin(Stdio::piped())
            .stderr(Stdio::inherit());
        let mut child = command.spawn().unwrap();
        let stdin = child.stdin.take().unwrap();
        let mut stdout = child.stdout.take().unwrap();

        // TODO: optimize performance (spawning threads frequently)
        // it's really a trouble and hard using unix `poll` here (can't retrieve raw file descriptors)

        // child thread: `from` -> `stdin`
        let from_addr = &from as *const &mut dyn Read as usize;
        let thread = spawn(move || unsafe {
            let mut stdin = stdin;
            let from: &mut dyn Read = transmute_copy(&*(from_addr as *const &mut dyn Read));
            let result = io::copy(from, &mut stdin);
            drop(stdin);
            result
        });

        // main thread: `stdout` -> `to`
        let read_size = io::copy(&mut stdout, to)?;
        let status = child.wait().unwrap();
        if !status.success() {
            return Err(Error::FilterNonZeroExit(status.code().unwrap()));
        }

        thread.join().unwrap()?;
        Ok(read_size)
    }
}

impl<'a> Compress for ExternalFilter<'a> {
    fn compress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Self::process_filter(self.cmd, from, to)
    }
}

impl<'a> Decompress for ExternalFilter<'a> {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        Self::process_filter(self.cmd, from, to)
    }
}

impl Decompress for BrotliDecompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut reader = brotli::Decompressor::new(from, 4096);
        Ok(io::copy(&mut reader, to)?)
    }
}

impl Decompress for Bzip3Decompressor {
    fn decompress_to(&self, from: &mut dyn Read, to: &mut dyn Write) -> Result<u64> {
        let mut decoder = match bzip3::read::Bz3Decoder::new(from) {
            Ok(d) => d,
            Err(e) => return Err(Error::DecompressorError(format!("{}", e))),
        };
        Ok(io::copy(&mut decoder, to)?)
    }
}