Skip to main content

zip/extra_fields/
mod.rs

1//! Types for extra fields
2
3use core::fmt::Display;
4
5mod aex_encryption;
6mod extended_timestamp;
7mod ntfs;
8mod zip64_extended_information;
9mod zipinfo_utf8;
10
11pub(crate) use aex_encryption::AexEncryption;
12pub(crate) use zip64_extended_information::Zip64ExtendedInformation;
13
14// re-export
15pub use extended_timestamp::ExtendedTimestamp;
16pub use ntfs::Ntfs;
17pub use zipinfo_utf8::UnicodeExtraField;
18
19/// marker trait to denote the place where this extra field has been stored
20pub trait ExtraFieldVersion {}
21
22/// use this to mark extra fields specified in a local header
23
24#[derive(Debug, Clone)]
25pub struct LocalHeaderVersion;
26
27/// use this to mark extra fields specified in the central header
28
29#[derive(Debug, Clone)]
30pub struct CentralHeaderVersion;
31
32impl ExtraFieldVersion for LocalHeaderVersion {}
33impl ExtraFieldVersion for CentralHeaderVersion {}
34
35/// contains one extra field
36#[derive(Debug, Clone)]
37pub enum ExtraField {
38    /// NTFS extra field
39    Ntfs(Ntfs),
40
41    /// extended timestamp, as described in <https://libzip.org/specifications/extrafld.txt>
42    ExtendedTimestamp(ExtendedTimestamp),
43}
44
45/// Extra field used in this crate
46#[repr(u16)]
47#[derive(Debug, Copy, Clone, PartialEq, Eq)]
48pub(crate) enum UsedExtraField {
49    /// ZIP64 extended information extra field
50    Zip64ExtendedInfo = 0x0001,
51    /// NTFS
52    Ntfs = 0x000a,
53    /// extended timestamp
54    /// from <https://libzip.org/specifications/extrafld.txt>
55    ExtendedTimestamp = 0x5455,
56    /// Info-ZIP Unicode Comment Extra Field
57    UnicodeComment = 0x6375,
58    /// Info-ZIP Unicode Path Extra Field
59    UnicodePath = 0x7075,
60    /// AE-x encryption structure
61    AeXEncryption = 0x9901,
62    /// Data Stream Alignment (Apache Commons-Compress)
63    DataStreamAlignment = 0xa11e,
64}
65
66impl UsedExtraField {
67    pub const fn to_le_bytes(self) -> [u8; 2] {
68        let field_u16 = self.as_u16();
69        field_u16.to_le_bytes()
70    }
71
72    pub const fn as_u16(self) -> u16 {
73        self as u16
74    }
75}
76
77impl From<UsedExtraField> for u16 {
78    fn from(value: UsedExtraField) -> Self {
79        value.as_u16()
80    }
81}
82
83impl Display for UsedExtraField {
84    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
85        write!(f, "0x{:04X}", *self as u16)
86    }
87}
88
89macro_rules! extra_field_match {
90    ($x:expr, $( $variant:path ),+ $(,)?) => {
91        match $x {
92            $(
93                v if v == $variant as u16 => Ok($variant),
94            )+
95            _ => Err(()),
96        }
97    };
98}
99
100impl TryFrom<u16> for UsedExtraField {
101    type Error = ();
102
103    fn try_from(value: u16) -> Result<Self, Self::Error> {
104        extra_field_match!(
105            value,
106            UsedExtraField::Zip64ExtendedInfo,
107            UsedExtraField::Ntfs,
108            UsedExtraField::ExtendedTimestamp,
109            UsedExtraField::UnicodeComment,
110            UsedExtraField::UnicodePath,
111            UsedExtraField::DataStreamAlignment,
112            UsedExtraField::AeXEncryption,
113        )
114    }
115}
116
117/// Known Extra fields (PKWARE and Third party) mappings, sorted
118pub const EXTRA_FIELD_MAPPING: [u16; 59] = [
119    UsedExtraField::Zip64ExtendedInfo.as_u16(),
120    0x0007, // AV Info
121    0x0008, // Reserved for extended language encoding data (PFS)
122    0x0009, // OS/2
123    UsedExtraField::Ntfs.as_u16(),
124    0x000c, // OpenVMS
125    0x000d, // UNIX
126    0x000e, // Reserved for file stream and fork descriptors
127    0x000f, // Patch Descriptor
128    0x0014, // PKCS#7 Store for X.509 Certificates
129    0x0015, // X.509 Certificate ID and Signature for individual file
130    0x0016, // X.509 Certificate ID for Central Directory
131    0x0017, // Strong Encryption Header
132    0x0018, // Record Management Controls
133    0x0019, // PKCS#7 Encryption Recipient Certificate List
134    0x0020, // Reserved for Timestamp record
135    0x0021, // Policy Decryption Key Record
136    0x0022, // Smartcrypt Key Provider Record
137    0x0023, // Smartcrypt Policy Key Data Record
138    0x0065, // IBM S/390 (Z390), AS/400 (I400) attributes - uncompressed
139    0x0066, // Reserved for IBM S/390 (Z390), AS/400 (I400) attributes - compressed
140    // Third party mappings commonly used
141    0x07c8, // Macintosh
142    0x1986, // Pixar USD header ID
143    0x2605, // ZipIt Macintosh
144    0x2705, // ZipIt Macintosh 1.3.5+
145    0x2805, // ZipIt Macintosh 1.3.5+
146    0x334d, // Info-ZIP Macintosh
147    0x4154, // Tandem
148    0x4341, // Acorn/SparkFS
149    0x4453, // Windows NT security descriptor (binary ACL)
150    0x4690, // POSZIP 4690 (reserved)
151    0x4704, // VM/CMS
152    0x470f, // MVS
153    0x4854, // THEOS (old?)
154    0x4b46, // FWKCS MD5
155    0x4c41, // OS/2 access control list (text ACL)
156    0x4d49, // Info-ZIP OpenVMS
157    0x4d63, // Macintosh Smartzip (??)
158    0x4f4c, // Xceed original location extra field
159    0x5356, // AOS/VS (ACL)
160    UsedExtraField::ExtendedTimestamp.as_u16(),
161    0x554e, // Xceed unicode extra field
162    0x5855, // Info-ZIP UNIX (original, also OS/2, NT, etc)
163    UsedExtraField::UnicodeComment.as_u16(),
164    0x6542, // BeOS/BeBox
165    0x6854, // THEOS
166    UsedExtraField::UnicodePath.as_u16(),
167    0x7441, // AtheOS/Syllable
168    0x756e, // ASi UNIX
169    0x7855, // Info-ZIP UNIX (new)
170    0x7875, // Info-ZIP UNIX (newer UID/GID)
171    UsedExtraField::AeXEncryption.as_u16(),
172    0x9902, // unknown
173    UsedExtraField::DataStreamAlignment.as_u16(),
174    0xa220, // Microsoft Open Packaging Growth Hint
175    0xcafe, // Java JAR file Extra Field Header ID
176    0xd935, // Android ZIP Alignment Extra Field
177    0xe57a, // Korean ZIP code page info
178    0xfd4a, // SMS/QDOS
179];