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
//! `zip-core::structs` contains common zip records/structs enum, according to the standart file <https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT>
//!
//! They are optimized for Rust usage

pub struct VersionMadeBy {
    pub compatibility_information: CompatibleFileAttributeInformation,
    pub version: ZipVersion,
}

/// Part of version made by
///
/// see [4.4.2.2](https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT)
pub enum CompatibleFileAttributeInformation {
    /// MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)
    MsDosAndOs2,
    Amiga,
    OpenVms,
    Unix,
    VmCms,
    AtariSt,
    Os2,
    Macintosh,
    ZSystem,
    CpM,
    WindowsNtfs,
    Mvs,
    Vse,
    AcornRisc,
    VFat,
    AlternateMvs,
    BeOs,
    Tandem,
    Os400,
    OsX,
}

impl TryFrom<u8> for CompatibleFileAttributeInformation {
    type Error = u8;

    /// in case of an unknown number, we return the number itself
    /// Note: an unknown number might be a incompatiblity in the zip or just
    /// that this crate hasn't been updated to the latest APPNOTE.TXT
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use CompatibleFileAttributeInformation::*;
        match value {
            0 => Ok(MsDosAndOs2),
            1 => Ok(Amiga),
            2 => Ok(OpenVms),
            3 => Ok(Unix),
            4 => Ok(VmCms),
            5 => Ok(AtariSt),
            6 => Ok(Os2),
            7 => Ok(Macintosh),
            8 => Ok(ZSystem),
            9 => Ok(CpM),
            10 => Ok(WindowsNtfs),
            11 => Ok(Mvs),
            12 => Ok(Vse),
            13 => Ok(AcornRisc),
            14 => Ok(VFat),
            15 => Ok(AlternateMvs),
            16 => Ok(BeOs),
            17 => Ok(Tandem),
            18 => Ok(Os400),
            19 => Ok(OsX),
            other => Err(other),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct ZipVersion {
    pub major_version: u8,
    pub minor_version: u8,
}

/// stored in u8 in 4.4.2.2
impl From<u8> for ZipVersion {
    fn from(value: u8) -> Self {
        let major_version = value / 10;
        let minor_version = value.rem_euclid(10);
        Self {
            major_version,
            minor_version,
        }
    }
}

/// stored in u18 in 4.4.3.2
impl From<u16> for ZipVersion {
    fn from(value: u16) -> Self {
        let version = value.to_le_bytes();
        Self {
            major_version: version[0],
            minor_version: version[1],
        }
    }
}

impl ZipVersion {
    pub fn new(major_version: u8, minor_version: u8) -> Self {
        Self {
            major_version,
            minor_version,
        }
    }
}

/// see [4.4.3.2](https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT)
pub enum MinimumFeatureVersion {
    Default,
    VolumeLabel,
    Folder,
    CompressedUsingDeflate,
    EncryptedUsingPkWare,
    CompressedUsingDeflate64,
    CompressedUsingPkwareDclImplode,
    PatchDataSet,
    UsesZip64FormatExtentions,
    CompressedUsingBzip2,
    EncryptedUsingDes,
    EncryptedUsing3Des,
    EncryptedUsingOriginalRc2,
    EncryptedUsingRc4,
    EncryptedUsingAes,
    EncryptedUsingCorrectedRc2,
    EncryptedUsingCorrectedRc2_64,
    EncryptedUsingNonOaepKeyWrapping,
    CentralDirectoryEncryption,
    CompressedUsingLzma,
    CompressedUsingPpmd,
    EncryptedUsingBlowfish,
    EncryptedUsingTwofish,
}

impl From<MinimumFeatureVersion> for ZipVersion {
    fn from(value: MinimumFeatureVersion) -> Self {
        use MinimumFeatureVersion::*;
        match value {
            Default => ZipVersion::new(1, 0),
            VolumeLabel => ZipVersion::new(1, 1),
            Folder => ZipVersion::new(2, 0),
            CompressedUsingDeflate => ZipVersion::new(2, 0),
            EncryptedUsingPkWare => ZipVersion::new(2, 0),
            CompressedUsingDeflate64 => ZipVersion::new(2, 1),
            CompressedUsingPkwareDclImplode => ZipVersion::new(2, 5),
            PatchDataSet => ZipVersion::new(2, 7),
            UsesZip64FormatExtentions => ZipVersion::new(4, 5),
            CompressedUsingBzip2 => ZipVersion::new(4, 7),
            EncryptedUsingDes => ZipVersion::new(5, 0),
            EncryptedUsing3Des => ZipVersion::new(5, 0),
            EncryptedUsingOriginalRc2 => ZipVersion::new(5, 0),
            EncryptedUsingRc4 => ZipVersion::new(5, 0),
            EncryptedUsingAes => ZipVersion::new(5, 1),
            EncryptedUsingCorrectedRc2 => ZipVersion::new(5, 1),
            EncryptedUsingCorrectedRc2_64 => ZipVersion::new(5, 2),
            EncryptedUsingNonOaepKeyWrapping => ZipVersion::new(6, 1),
            CentralDirectoryEncryption => ZipVersion::new(6, 2),
            CompressedUsingLzma => ZipVersion::new(6, 3),
            CompressedUsingPpmd => ZipVersion::new(6, 3),
            EncryptedUsingBlowfish => ZipVersion::new(6, 3),
            EncryptedUsingTwofish => ZipVersion::new(6, 3),
        }
    }
}

/// see [4.4.5](https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT)
pub enum CompressionMethod {
    Stored,
    Shrunk,
    ReducedWithFactor1,
    ReducedWithFactor2,
    ReducedWithFactor3,
    ReducedWithFactor4,
    Imploded,
    Deflated,
    EnhancedDeflatingUsingDeflate64,
    PkWareDataCompressionLIbraryImploding,
    Bzip2,
    Lzma,
    IbmZOsCmpsc,
    /// Ibm Terse new, for old see PkWareDataCompressionLIbraryImploding
    IbmTerse,
    IbmLz77z,
    Zstdandart,
    Mp3,
    Xz,
    Jpeg,
    WavPack,
    PpmdV1R1,
    Aex,
}

impl CompressionMethod {
    pub fn is_deprecated(&self) -> bool {
        use CompressionMethod::*;
        matches!(
            self,
            Shrunk | ReducedWithFactor1 | ReducedWithFactor2 | ReducedWithFactor3 | ReducedWithFactor4 | Imploded
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn order_zip_version() {
        let v1_0 = ZipVersion::new(1, 0);
        let v1_1 = ZipVersion::new(1, 1);
        let v2_0 = ZipVersion::new(2, 0);
        let v2_1 = ZipVersion::new(2, 1);
        assert_eq!(v1_0, v1_0);
        assert_ne!(v1_0, v1_1);
        assert_ne!(v1_1, v2_1);

        assert!(v1_1 > v1_0);
        assert!(v2_0 > v1_0);
        assert!(v2_0 > v1_1);
        assert!(v2_1 > v2_0);
        assert!(v2_0 <= v2_0);
        assert!(v2_0 < v2_1);
        assert!(v1_1 < v2_1);
        assert!(v1_0 < v2_1);
        assert_eq!(v1_0.max(v1_1), v1_1);
    }
}