rc_zip/parse/
version.rs

1use num_enum::{FromPrimitive, IntoPrimitive};
2use ownable::{IntoOwned, ToOwned};
3use std::fmt;
4use winnow::{binary::le_u8, seq, PResult, Parser, Partial};
5
6/// A zip version (either created by, or required when reading an archive).
7///
8/// Versions determine which features are supported by a tool, and
9/// which features are required when reading a file.
10///
11/// For more information, see the [.ZIP Application Note](https://support.pkware.com/display/PKZIP/APPNOTE), section 4.4.2.
12#[derive(Clone, Copy, ToOwned, IntoOwned, PartialEq, Eq, Hash)]
13pub struct Version {
14    /// The host system on which
15    pub host_system: HostSystem,
16
17    /// Integer version, e.g. 45 for Zip version 4.5
18    /// See APPNOTE, section 4.4.2.1
19    pub version: u8,
20}
21
22impl fmt::Debug for Version {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        write!(f, "{:?} v{}", self.host_system, self.version)
25    }
26}
27
28impl Version {
29    /// Parse a version from a byte slice
30    pub fn parser(i: &mut Partial<&'_ [u8]>) -> PResult<Self> {
31        seq! {Self {
32            version: le_u8,
33            host_system: le_u8.map(HostSystem::from),
34        }}
35        .parse_next(i)
36    }
37}
38
39/// System on which an archive was created, as encoded into a version u16.
40///
41/// See APPNOTE, section 4.4.2.2
42#[derive(
43    Debug, Clone, Copy, IntoPrimitive, FromPrimitive, ToOwned, IntoOwned, PartialEq, Eq, Hash,
44)]
45#[repr(u8)]
46pub enum HostSystem {
47    /// MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)
48    MsDos = 0,
49
50    /// Amiga
51    Amiga = 1,
52
53    /// OpenVMS
54    OpenVms = 2,
55
56    /// UNIX
57    Unix = 3,
58
59    /// VM/CMS
60    VmCms = 4,
61
62    /// Atari ST
63    AtariSt = 5,
64
65    /// OS/2 H.P.F.S
66    Os2Hpfs = 6,
67
68    /// Macintosh (see `Osx`)
69    Macintosh = 7,
70
71    /// Z-System
72    ZSystem = 8,
73
74    /// CP/M
75    CpM = 9,
76
77    /// Windows NTFS
78    WindowsNtfs = 10,
79
80    /// MVS (OS/390 - Z/OS)
81    Mvs = 11,
82
83    /// VSE
84    Vse = 12,
85
86    /// Acorn Risc
87    AcornRisc = 13,
88
89    /// VFAT
90    Vfat = 14,
91
92    /// alternate MVS
93    AlternateMvs = 15,
94
95    /// BeOS
96    BeOs = 16,
97
98    /// Tandem
99    Tandem = 17,
100
101    /// OS/400
102    Os400 = 18,
103
104    /// OS X (Darwin)
105    Osx = 19,
106
107    /// Unknown host system
108    ///
109    /// Values 20 through 255 are currently unused, as of
110    /// APPNOTE.TXT 6.3.10
111    #[num_enum(catch_all)]
112    Unknown(u8),
113}