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
use crate::{
    error::{Error, UnsupportedError},
    parse::{Method, MsdosTimestamp, Version, ZipBytes, ZipString},
};

use winnow::{
    binary::{le_u16, le_u32, le_u64, le_u8},
    combinator::opt,
    error::{ContextError, ErrMode, ErrorKind, FromExternalError},
    seq,
    token::tag,
    PResult, Parser, Partial,
};

#[derive(Debug)]
/// 4.3.7 Local file header
pub struct LocalFileHeaderRecord {
    /// version needed to extract
    pub reader_version: Version,

    /// general purpose bit flag
    pub flags: u16,

    /// compression method
    pub method: Method,

    /// last mod file datetime
    pub modified: MsdosTimestamp,

    /// crc-32
    pub crc32: u32,

    /// compressed size
    pub compressed_size: u32,

    /// uncompressed size
    pub uncompressed_size: u32,

    /// file name
    pub name: ZipString,

    /// extra field
    pub extra: ZipBytes,

    /// method-specific fields
    pub method_specific: MethodSpecific,
}

#[derive(Debug)]
/// Method-specific properties following the local file header
pub enum MethodSpecific {
    /// No method-specific properties
    None,

    /// LZMA properties
    Lzma(LzmaProperties),
}

impl LocalFileHeaderRecord {
    /// The signature for a local file header
    pub const SIGNATURE: &'static str = "PK\x03\x04";

    /// Parser for the local file header
    pub fn parser(i: &mut Partial<&'_ [u8]>) -> PResult<Self> {
        let _ = tag(Self::SIGNATURE).parse_next(i)?;

        let reader_version = Version::parser.parse_next(i)?;
        let flags = le_u16.parse_next(i)?;
        let method = le_u16.parse_next(i).map(Method::from)?;
        let modified = MsdosTimestamp::parser.parse_next(i)?;
        let crc32 = le_u32.parse_next(i)?;
        let compressed_size = le_u32.parse_next(i)?;
        let uncompressed_size = le_u32.parse_next(i)?;

        let name_len = le_u16.parse_next(i)?;
        let extra_len = le_u16.parse_next(i)?;

        let name = ZipString::parser(name_len).parse_next(i)?;
        let extra = ZipBytes::parser(extra_len).parse_next(i)?;

        let method_specific = match method {
            Method::Lzma => {
                let lzma_properties = LzmaProperties::parser.parse_next(i)?;
                if let Err(e) = lzma_properties.error_if_unsupported() {
                    return Err(ErrMode::Cut(ContextError::from_external_error(
                        i,
                        ErrorKind::Verify,
                        e,
                    )));
                }
                MethodSpecific::Lzma(lzma_properties)
            }
            _ => MethodSpecific::None,
        };

        Ok(Self {
            reader_version,
            flags,
            method,
            modified,
            crc32,
            compressed_size,
            uncompressed_size,
            name,
            extra,
            method_specific,
        })
    }

    /// Check for the presence of the bit flag that indicates a data descriptor
    /// is present after the file data.
    pub fn has_data_descriptor(&self) -> bool {
        // 4.3.9.1 This descriptor MUST exist if bit 3 of the general
        // purpose bit flag is set (see below).
        self.flags & 0b1000 != 0
    }
}

/// 4.3.9  Data descriptor:
#[derive(Debug)]
pub struct DataDescriptorRecord {
    /// CRC32 checksum
    pub crc32: u32,
    /// Compressed size
    pub compressed_size: u64,
    /// Uncompressed size
    pub uncompressed_size: u64,
}

impl DataDescriptorRecord {
    const SIGNATURE: &'static str = "PK\x07\x08";

    /// Create a parser for the data descriptor record.
    pub fn mk_parser(is_zip64: bool) -> impl FnMut(&mut Partial<&'_ [u8]>) -> PResult<Self> {
        move |i| {
            // From appnote.txt:
            //
            // 4.3.9.3 Although not originally assigned a signature, the value
            // 0x08074b50 has commonly been adopted as a signature value for the
            // data descriptor record.  Implementers SHOULD be aware that ZIP files
            // MAY be encountered with or without this signature marking data
            // descriptors and SHOULD account for either case when reading ZIP files
            // to ensure compatibility.
            let _ = opt(tag(Self::SIGNATURE)).parse_next(i)?;

            if is_zip64 {
                seq! {Self {
                    crc32: le_u32,
                    compressed_size: le_u64,
                    uncompressed_size: le_u64,
                }}
                .parse_next(i)
            } else {
                seq! {Self {
                    crc32: le_u32,
                    compressed_size: le_u32.map(|x| x as u64),
                    uncompressed_size: le_u32.map(|x| x as u64),
                }}
                .parse_next(i)
            }
        }
    }
}

/// 5.8.5 LZMA Properties header
#[derive(Debug)]
pub struct LzmaProperties {
    /// major version
    pub major: u8,
    /// minor version
    pub minor: u8,
    /// properties size
    pub properties_size: u16,
}

impl LzmaProperties {
    /// Parser for the LZMA properties header.
    pub fn parser(i: &mut Partial<&'_ [u8]>) -> PResult<Self> {
        // Note: the actual properties (5 bytes, contains dictionary size,
        // and various other settings) is not actually read, because lzma-rs
        // reads those properties itself.

        seq! {Self {
            major: le_u8,
            minor: le_u8,
            properties_size: le_u16,
        }}
        .parse_next(i)
    }

    /// Check if the LZMA version is supported.
    pub fn error_if_unsupported(&self) -> Result<(), Error> {
        if (self.major, self.minor) != (2, 0) {
            return Err(Error::Unsupported(
                UnsupportedError::LzmaVersionUnsupported {
                    minor: self.minor,
                    major: self.major,
                },
            ));
        }

        const LZMA_PROPERTIES_SIZE: u16 = 5;
        if self.properties_size != LZMA_PROPERTIES_SIZE {
            return Err(Error::Unsupported(
                UnsupportedError::LzmaPropertiesHeaderWrongSize {
                    expected: 5,
                    actual: self.properties_size,
                },
            ));
        }

        Ok(())
    }
}