Skip to main content

powerio_archive/
stream.rs

1//! Bounded payload reads shared by native format adapters.
2
3use std::io::Read;
4
5/// Why an entry's inflated stream was refused.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum DeclaredSizeFailure {
9    /// The reader or inflater failed, including a CRC mismatch.
10    Read(std::io::Error),
11    /// The declared size does not fit in memory on this platform.
12    Allocation,
13    /// The stream ended after this many bytes, before the declared size.
14    Short(u64),
15    /// The stream produced more bytes than declared or than the bound allows.
16    Long,
17}
18
19impl DeclaredSizeFailure {
20    fn from_read_error(error: std::io::Error) -> Self {
21        // zip 9.x rejects an inflater that produces more bytes than the
22        // central directory declared before our one-byte overrun probe runs.
23        // Preserve the reader's stable malformed-archive diagnostic instead
24        // of leaking the ZIP implementation's internal error text.
25        if error
26            .to_string()
27            .contains("larger than its declared uncompressed size")
28        {
29            Self::Long
30        } else {
31            Self::Read(error)
32        }
33    }
34}
35
36/// Inflate exactly the `size` bytes an entry declares, after the `prefix`
37/// already read from it. One more byte is requested to confirm that the
38/// stream ends there, which is also when the reader checks the CRC. A stream
39/// longer or shorter than declared is refused, so the declared size the
40/// ratio and budget checks used is the size produced, and the buffer is
41/// reserved once at that size.
42pub fn read_declared_size(
43    entry: &mut impl Read,
44    prefix: &[u8],
45    size: u64,
46) -> std::result::Result<Vec<u8>, DeclaredSizeFailure> {
47    if prefix.len() as u64 > size {
48        return Err(DeclaredSizeFailure::Long);
49    }
50    let capacity = usize::try_from(size).map_err(|_| DeclaredSizeFailure::Allocation)?;
51    let mut content = Vec::new();
52    content
53        .try_reserve_exact(capacity)
54        .map_err(|_| DeclaredSizeFailure::Allocation)?;
55    content.extend_from_slice(prefix);
56    entry
57        .by_ref()
58        .take(size - prefix.len() as u64)
59        .read_to_end(&mut content)
60        .map_err(DeclaredSizeFailure::from_read_error)?;
61    let produced = content.len() as u64;
62    if produced != size {
63        return Err(DeclaredSizeFailure::Short(produced));
64    }
65    if entry
66        .read(&mut [0_u8; 1])
67        .map_err(DeclaredSizeFailure::from_read_error)?
68        != 0
69    {
70        return Err(DeclaredSizeFailure::Long);
71    }
72    Ok(content)
73}
74
75/// Read through EOF under an actual expanded-byte cap, including a one-byte
76/// overrun probe. Does not trust a declared ZIP size.
77pub fn read_bounded(
78    reader: &mut impl Read,
79    max_bytes: u64,
80) -> Result<Vec<u8>, DeclaredSizeFailure> {
81    let mut bytes = Vec::new();
82    reader
83        .take(max_bytes.saturating_add(1))
84        .read_to_end(&mut bytes)
85        .map_err(DeclaredSizeFailure::Read)?;
86    if bytes.len() as u64 > max_bytes {
87        return Err(DeclaredSizeFailure::Long);
88    }
89    Ok(bytes)
90}