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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use std::cmp;

use oval::Buffer;
use tracing::trace;
use winnow::{
    error::ErrMode,
    stream::{AsBytes, Offset},
    Parser, Partial,
};

mod store_dec;

#[cfg(feature = "deflate")]
mod deflate_dec;

#[cfg(feature = "deflate64")]
mod deflate64_dec;

#[cfg(feature = "bzip2")]
mod bzip2_dec;

#[cfg(feature = "lzma")]
mod lzma_dec;

#[cfg(feature = "zstd")]
mod zstd_dec;

use crate::{
    error::{Error, FormatError, UnsupportedError},
    parse::{DataDescriptorRecord, Entry, LocalFileHeader, Method},
};

use super::FsmResult;

struct EntryReadMetrics {
    uncompressed_size: u64,
    crc32: u32,
}

#[derive(Default)]
enum State {
    ReadLocalHeader,

    ReadData {
        /// Whether the entry has a data descriptor
        has_data_descriptor: bool,

        /// Whether the entry is zip64 (because its compressed size or uncompressed size is u32::MAX)
        is_zip64: bool,

        /// Amount of bytes we've fed to the decompressor
        compressed_bytes: u64,

        /// Amount of bytes the decompressor has produced
        uncompressed_bytes: u64,

        /// CRC32 hash of the decompressed data
        hasher: crc32fast::Hasher,

        /// The decompression method we're using
        decompressor: AnyDecompressor,
    },

    ReadDataDescriptor {
        /// Whether the entry is zip64 (because its compressed size or uncompressed size is u32::MAX)
        is_zip64: bool,

        /// Size we've decompressed + crc32 hash we've computed
        metrics: EntryReadMetrics,
    },

    Validate {
        /// Size we've decompressed + crc32 hash we've computed
        metrics: EntryReadMetrics,

        /// The data descriptor for this entry, if any
        descriptor: Option<DataDescriptorRecord>,
    },

    #[default]
    Transition,
}

/// A state machine that can parse a zip entry
pub struct EntryFsm {
    state: State,
    entry: Option<Entry>,
    buffer: Buffer,
}

impl EntryFsm {
    /// Create a new state machine for decompressing a zip entry
    pub fn new(entry: Option<Entry>, buffer: Option<Buffer>) -> Self {
        const BUF_CAPACITY: usize = 256 * 1024;

        Self {
            state: State::ReadLocalHeader,
            entry,
            buffer: match buffer {
                Some(buffer) => {
                    assert!(buffer.capacity() >= BUF_CAPACITY, "buffer too small");
                    buffer
                }
                None => Buffer::with_capacity(BUF_CAPACITY),
            },
        }
    }

    /// If this returns true, the caller should read data from into
    /// [Self::space] — without forgetting to call [Self::fill] with the number
    /// of bytes written.
    pub fn wants_read(&self) -> bool {
        match self.state {
            State::ReadLocalHeader => true,
            State::ReadData { .. } => {
                // we want to read if we have space
                self.buffer.available_space() > 0
            }
            State::ReadDataDescriptor { .. } => true,
            State::Validate { .. } => false,
            State::Transition => unreachable!(),
        }
    }

    /// Like `process`, but only processes the header. If this returns
    /// `Ok(None)`, the caller should read more data and call this function
    /// again.
    pub fn process_till_header(&mut self) -> Result<Option<&Entry>, Error> {
        match &self.state {
            State::ReadLocalHeader => {
                self.internal_process_local_header()?;
            }
            _ => {
                // already good
            }
        }

        // this will be non-nil if we've parsed the local header, otherwise,
        Ok(self.entry.as_ref())
    }

    fn internal_process_local_header(&mut self) -> Result<bool, Error> {
        assert!(
            matches!(self.state, State::ReadLocalHeader),
            "internal_process_local_header called in wrong state",
        );

        let mut input = Partial::new(self.buffer.data());
        match LocalFileHeader::parser.parse_next(&mut input) {
            Ok(header) => {
                let consumed = input.as_bytes().offset_from(&self.buffer.data());
                tracing::trace!(local_file_header = ?header, consumed, "parsed local file header");
                let decompressor = AnyDecompressor::new(
                    header.method,
                    self.entry.as_ref().map(|entry| entry.uncompressed_size),
                )?;

                if self.entry.is_none() {
                    self.entry = Some(header.as_entry()?);
                }

                self.state = State::ReadData {
                    is_zip64: header.compressed_size == u32::MAX
                        || header.uncompressed_size == u32::MAX,
                    has_data_descriptor: header.has_data_descriptor(),
                    compressed_bytes: 0,
                    uncompressed_bytes: 0,
                    hasher: crc32fast::Hasher::new(),
                    decompressor,
                };
                self.buffer.consume(consumed);
                Ok(true)
            }
            Err(ErrMode::Incomplete(_)) => Ok(false),
            Err(_e) => Err(Error::Format(FormatError::InvalidLocalHeader)),
        }
    }

    /// Process the input and write the output to the given buffer
    ///
    /// This function will return `FsmResult::Continue` if it needs more input
    /// to continue, or if it needs more space to write to. It will return
    /// `FsmResult::Done` when all the input has been decompressed and all
    /// the output has been written.
    ///
    /// Also, after writing all the output, process will read the data
    /// descriptor (if any), and make sur the CRC32 hash and the uncompressed
    /// size match the expected values.
    pub fn process(
        mut self,
        out: &mut [u8],
    ) -> Result<FsmResult<(Self, DecompressOutcome), Buffer>, Error> {
        tracing::trace!(
            state = match &self.state {
                State::ReadLocalHeader => "ReadLocalHeader",
                State::ReadData { .. } => "ReadData",
                State::ReadDataDescriptor { .. } => "ReadDataDescriptor",
                State::Validate { .. } => "Validate",
                State::Transition => "Transition",
            },
            "process"
        );

        use State as S;
        'process_state: loop {
            return match &mut self.state {
                S::ReadLocalHeader => {
                    if self.internal_process_local_header()? {
                        // the local header was completed, let's keep going
                        continue 'process_state;
                    } else {
                        // no buffer were touched, the local header wasn't complete
                        let outcome = DecompressOutcome {
                            bytes_read: 0,
                            bytes_written: 0,
                        };
                        Ok(FsmResult::Continue((self, outcome)))
                    }
                }
                S::ReadData {
                    compressed_bytes,
                    uncompressed_bytes,
                    hasher,
                    decompressor,
                    ..
                } => {
                    let in_buf = self.buffer.data();
                    let entry = self.entry.as_ref().unwrap();

                    // do we have more input to feed to the decompressor?
                    // if so, don't give it an empty read
                    if in_buf.is_empty() && *compressed_bytes < entry.compressed_size {
                        return Ok(FsmResult::Continue((self, Default::default())));
                    }

                    // don't feed the decompressor bytes beyond the entry's compressed size
                    let in_buf_max_len = cmp::min(
                        in_buf.len(),
                        entry.compressed_size as usize - *compressed_bytes as usize,
                    );
                    let in_buf = &in_buf[..in_buf_max_len];
                    let bytes_fed_this_turn = in_buf.len();

                    let fed_bytes_after_this = *compressed_bytes + in_buf.len() as u64;
                    let has_more_input = if fed_bytes_after_this == entry.compressed_size as _ {
                        HasMoreInput::No
                    } else {
                        HasMoreInput::Yes
                    };

                    trace!(
                        compressed_bytes = *compressed_bytes,
                        uncompressed_bytes = *uncompressed_bytes,
                        fed_bytes_after_this,
                        in_buf_len = in_buf.len(),
                        ?has_more_input,
                        "decompressing"
                    );

                    let outcome = decompressor.decompress(in_buf, out, has_more_input)?;
                    self.buffer.consume(outcome.bytes_read);
                    *compressed_bytes += outcome.bytes_read as u64;
                    trace!(
                        compressed_bytes = *compressed_bytes,
                        uncompressed_bytes = *uncompressed_bytes,
                        entry_compressed_size = %entry.compressed_size,
                        ?outcome,
                        "decompressed"
                    );

                    if outcome.bytes_written == 0 && *compressed_bytes == entry.compressed_size {
                        trace!("eof and no bytes written, we're done");

                        // we're done, let's read the data descriptor (if there's one)
                        transition!(self.state => (S::ReadData {  has_data_descriptor, is_zip64, uncompressed_bytes, hasher, .. }) {
                            let metrics = EntryReadMetrics {
                                uncompressed_size: uncompressed_bytes,
                                crc32: hasher.finalize(),
                            };

                            if has_data_descriptor {
                                trace!("transitioning to ReadDataDescriptor");
                                S::ReadDataDescriptor { metrics, is_zip64 }
                            } else {
                                trace!("transitioning to Validate");
                                S::Validate { metrics, descriptor: None }
                            }
                        });
                        return self.process(out);
                    } else if outcome.bytes_written == 0 && outcome.bytes_read == 0 {
                        if bytes_fed_this_turn == 0 {
                            return Err(Error::IO(std::io::Error::new(
                                std::io::ErrorKind::UnexpectedEof,
                                "decompressor made no progress: this is probably an rc-zip bug",
                            )));
                        } else {
                            // ok fine, continue
                        }
                    }

                    // write the decompressed data to the hasher
                    hasher.update(&out[..outcome.bytes_written]);
                    // update the number of bytes we've decompressed
                    *uncompressed_bytes += outcome.bytes_written as u64;

                    trace!(
                        compressed_bytes = *compressed_bytes,
                        uncompressed_bytes = *uncompressed_bytes,
                        "updated hasher"
                    );

                    Ok(FsmResult::Continue((self, outcome)))
                }
                S::ReadDataDescriptor { is_zip64, .. } => {
                    let mut input = Partial::new(self.buffer.data());

                    match DataDescriptorRecord::mk_parser(*is_zip64).parse_next(&mut input) {
                        Ok(descriptor) => {
                            self.buffer
                                .consume(input.as_bytes().offset_from(&self.buffer.data()));
                            trace!("data descriptor = {:#?}", descriptor);
                            transition!(self.state => (S::ReadDataDescriptor { metrics, .. }) {
                                S::Validate { metrics, descriptor: Some(descriptor) }
                            });
                            self.process(out)
                        }
                        Err(ErrMode::Incomplete(_)) => {
                            Ok(FsmResult::Continue((self, Default::default())))
                        }
                        Err(_e) => Err(Error::Format(FormatError::InvalidDataDescriptor)),
                    }
                }
                S::Validate {
                    metrics,
                    descriptor,
                } => {
                    let entry = self.entry.as_ref().unwrap();

                    let expected_crc32 = if entry.crc32 != 0 {
                        entry.crc32
                    } else if let Some(descriptor) = descriptor.as_ref() {
                        descriptor.crc32
                    } else {
                        0
                    };

                    if entry.uncompressed_size != metrics.uncompressed_size {
                        return Err(Error::Format(FormatError::WrongSize {
                            expected: entry.uncompressed_size,
                            actual: metrics.uncompressed_size,
                        }));
                    }

                    if expected_crc32 != 0 && expected_crc32 != metrics.crc32 {
                        return Err(Error::Format(FormatError::WrongChecksum {
                            expected: expected_crc32,
                            actual: metrics.crc32,
                        }));
                    }

                    Ok(FsmResult::Done(self.buffer))
                }
                S::Transition => {
                    unreachable!("the state machine should never be in the transition state")
                }
            };
        }
    }

    /// Returns a mutable slice with all the available space to write to.
    ///
    /// After writing to this, call [Self::fill] with the number of bytes written.
    #[inline]
    pub fn space(&mut self) -> &mut [u8] {
        if self.buffer.available_space() == 0 {
            self.buffer.shift();
        }
        self.buffer.space()
    }

    /// After having written data to [Self::space], call this to indicate how
    /// many bytes were written.
    #[inline]
    pub fn fill(&mut self, count: usize) -> usize {
        self.buffer.fill(count)
    }
}

enum AnyDecompressor {
    Store(store_dec::StoreDec),
    #[cfg(feature = "deflate")]
    Deflate(Box<deflate_dec::DeflateDec>),
    #[cfg(feature = "deflate64")]
    Deflate64(Box<deflate64_dec::Deflate64Dec>),
    #[cfg(feature = "bzip2")]
    Bzip2(bzip2_dec::Bzip2Dec),
    #[cfg(feature = "lzma")]
    Lzma(Box<lzma_dec::LzmaDec>),
    #[cfg(feature = "zstd")]
    Zstd(zstd_dec::ZstdDec),
}

#[derive(Default, Debug)]
pub struct DecompressOutcome {
    /// Number of bytes read from input
    pub bytes_read: usize,

    /// Number of bytes written to output
    pub bytes_written: usize,
}

/// Returns whether there's more input to be fed to the decompressor
#[derive(Debug)]
pub enum HasMoreInput {
    Yes,
    No,
}

trait Decompressor {
    fn decompress(
        &mut self,
        in_buf: &[u8],
        out: &mut [u8],
        has_more_input: HasMoreInput,
    ) -> Result<DecompressOutcome, Error>;
}

impl AnyDecompressor {
    fn new(method: Method, #[allow(unused)] uncompressed_size: Option<u64>) -> Result<Self, Error> {
        let dec = match method {
            Method::Store => Self::Store(Default::default()),

            #[cfg(feature = "deflate")]
            Method::Deflate => Self::Deflate(Default::default()),
            #[cfg(not(feature = "deflate"))]
            Method::Deflate => {
                let err = Error::Unsupported(UnsupportedError::MethodNotEnabled(method));
                return Err(err);
            }

            #[cfg(feature = "deflate64")]
            Method::Deflate64 => Self::Deflate64(Default::default()),
            #[cfg(not(feature = "deflate64"))]
            Method::Deflate64 => {
                let err = Error::Unsupported(UnsupportedError::MethodNotEnabled(method));
                return Err(err);
            }

            #[cfg(feature = "bzip2")]
            Method::Bzip2 => Self::Bzip2(Default::default()),
            #[cfg(not(feature = "bzip2"))]
            Method::Bzip2 => {
                let err = Error::Unsupported(UnsupportedError::MethodNotEnabled(method));
                return Err(err);
            }

            #[cfg(feature = "lzma")]
            Method::Lzma => Self::Lzma(Box::new(lzma_dec::LzmaDec::new(uncompressed_size))),
            #[cfg(not(feature = "lzma"))]
            Method::Lzma => {
                let err = Error::Unsupported(UnsupportedError::MethodNotEnabled(method));
                return Err(err);
            }

            #[cfg(feature = "zstd")]
            Method::Zstd => Self::Zstd(zstd_dec::ZstdDec::new()?),
            #[cfg(not(feature = "zstd"))]
            Method::Zstd => {
                let err = Error::Unsupported(UnsupportedError::MethodNotEnabled(method));
                return Err(err);
            }

            _ => {
                let err = Error::Unsupported(UnsupportedError::MethodNotSupported(method));
                return Err(err);
            }
        };
        Ok(dec)
    }
}

impl Decompressor for AnyDecompressor {
    #[inline]
    fn decompress(
        &mut self,
        in_buf: &[u8],
        out: &mut [u8],
        has_more_input: HasMoreInput,
    ) -> Result<DecompressOutcome, Error> {
        // forward to the appropriate decompressor
        match self {
            Self::Store(dec) => dec.decompress(in_buf, out, has_more_input),
            #[cfg(feature = "deflate")]
            Self::Deflate(dec) => dec.decompress(in_buf, out, has_more_input),
            #[cfg(feature = "deflate64")]
            Self::Deflate64(dec) => dec.decompress(in_buf, out, has_more_input),
            #[cfg(feature = "bzip2")]
            Self::Bzip2(dec) => dec.decompress(in_buf, out, has_more_input),
            #[cfg(feature = "lzma")]
            Self::Lzma(dec) => dec.decompress(in_buf, out, has_more_input),
            #[cfg(feature = "zstd")]
            Self::Zstd(dec) => dec.decompress(in_buf, out, has_more_input),
        }
    }
}