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
use std::io::Write;

use byteorder::WriteBytesExt;

use super::{range_enc::RangeEncoder, CountingWriter, LZMA2Options};

use super::encoder::{LZMAEncoder, LZMAEncoderModes};
/// Compresses into the legacy .lzma file format or into a raw LZMA stream
///
/// # Examples
/// ```
/// use std::io::Write;
/// use lzma_rust::{LZMA2Options, LZMAWriter};
/// let s = b"Hello, world!";
/// let mut out = Vec::new();
/// let mut options = LZMA2Options::with_preset(6);
/// options.dict_size = LZMA2Options::DICT_SIZE_DEFAULT;

/// let mut w = LZMAWriter::new_no_header(CountingWriter::new(&mut out), &options, false).unwrap();
/// w.write_all(&s).unwrap();
/// w.write(&[]).unwrap();
///
/// ```
///
pub struct LZMAWriter<W: Write> {
    rc: RangeEncoder<CountingWriter<W>>,
    lzma: LZMAEncoder,
    use_end_marker: bool,
    finished: bool,
    current_uncompressed_size: u64,
    expected_uncompressed_size: Option<u64>,
    props: u8,
    mode: LZMAEncoderModes,
}

impl<W: Write> LZMAWriter<W> {
    pub fn new(
        mut out: CountingWriter<W>,
        options: &LZMA2Options,
        use_header: bool,
        use_end_marker: bool,
        expected_uncompressed_size: Option<u64>,
    ) -> Result<LZMAWriter<W>, std::io::Error> {
        let (mut lzma, mode) = LZMAEncoder::new(
            options.mode,
            options.lc,
            options.lp,
            options.pb,
            options.mf,
            options.depth_limit,
            options.dict_size,
            options.nice_len as usize,
        );
        if let Some(preset_dict) = &options.preset_dict {
            if use_header {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Unsupported,
                    "Header is not supported with preset dict",
                ));
            }
            lzma.lz.set_preset_dict(options.dict_size, preset_dict);
        }

        let props = options.get_props();
        if use_header {
            out.write_u8(props as _)?;
            let mut dict_size = options.dict_size;
            for _i in 0..4 {
                out.write_u8((dict_size & 0xFF) as u8)?;
                dict_size >>= 8;
            }
            let expected_compressed_size = expected_uncompressed_size.unwrap_or(u64::MAX);
            for i in 0..8 {
                out.write_u8(((expected_compressed_size >> (i * 8)) & 0xFF) as u8)?;
            }
        }

        let rc = RangeEncoder::new(out);
        Ok(LZMAWriter {
            rc,
            lzma,
            use_end_marker,
            finished: false,
            current_uncompressed_size: 0,
            expected_uncompressed_size,
            props,
            mode,
        })
    }

    #[inline]
    pub fn new_use_header(
        out: CountingWriter<W>,
        options: &LZMA2Options,
        input_size: Option<u64>,
    ) -> Result<Self, std::io::Error> {
        Self::new(out, options, true, input_size.is_none(), input_size)
    }

    #[inline]
    pub fn new_no_header(
        out: CountingWriter<W>,
        options: &LZMA2Options,
        use_end_marker: bool,
    ) -> Result<Self, std::io::Error> {
        Self::new(out, options, false, use_end_marker, None)
    }

    #[inline]
    pub fn props(&self) -> u8 {
        self.props
    }

    #[inline]
    pub fn get_uncompressed_size(&self) -> u64 {
        self.current_uncompressed_size
    }

    pub fn finish(&mut self) -> std::io::Result<()> {
        if !self.finished {
            if let Some(exp) = self.expected_uncompressed_size {
                if exp != self.current_uncompressed_size {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Expected compressed size does not match actual compressed size",
                    ));
                }
            }
            self.lzma.lz.set_finishing();
            self.lzma.encode_for_lzma1(&mut self.rc, &mut self.mode)?;
            if self.use_end_marker {
                self.lzma.encode_lzma1_end_marker(&mut self.rc)?;
            }
            self.rc.finish()?;
            self.finished = true;
        }
        Ok(())
    }
}

impl<W: Write> Write for LZMAWriter<W> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if self.finished {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Already finished",
            ));
        }
        if buf.len() == 0 {
            self.finish()?;
            self.rc.inner().write(buf)?;
            return Ok(0);
        }
        if let Some(exp) = self.expected_uncompressed_size {
            if exp < self.current_uncompressed_size + buf.len() as u64 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "Expected compressed size does not match actual compressed size",
                ));
            }
        }
        self.current_uncompressed_size += buf.len() as u64;
        let mut len = buf.len();
        let mut off = 0;
        while len > 0 {
            let used = self.lzma.lz.fill_window(&buf[off..]);
            off += used;
            len -= used;
            self.lzma.encode_for_lzma1(&mut self.rc, &mut self.mode)?;
        }

        Ok(off)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}