Skip to main content

zip/format/
flags.rs

1//! Flags of zip
2
3/// Zip flags
4/// Stored as Little endian
5#[rustfmt::skip]
6#[repr(u16)]
7#[derive(Debug, Copy, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum ZipFlags {
10    /// If set, indicates that the file is encrypted.
11    Encrypted                   = 0b0000_0000_0000_0001,
12    /// Compression setting
13    #[allow(unused)]
14    CompressionSetting          = 0b0000_0000_0000_0010,
15    /// Compression setting 2
16    #[allow(unused)]
17    CompressionSetting2         = 0b0000_0000_0000_0100,
18    /// If this bit is set, the fields crc-32, compressed size and uncompressed size are set to zero in the  local header.
19    /// The correct values are put in the data descriptor immediately following the compressed data.
20    UsingDataDescriptor         = 0b0000_0000_0000_1000,
21    /// Reserved for use with method 8, for enhanced deflating.
22    #[allow(unused)]
23    ReservedEnhancedDeflating   = 0b0000_0000_0001_0000,
24    /// If this bit is set, this indicates that the file is compressed patched data.
25    #[allow(unused)]
26    CompressedPatchedData       = 0b0000_0000_0010_0000,
27    /// Strong encryption.
28    /// If this bit is set, you MUST set the version needed to extract value to at least 50 and you MUST also set bit 0.
29    /// If AES encryption is used, the version needed to extract value MUST be at least 51.
30    #[allow(unused)]
31    StrongEncryption            = 0b0000_0000_0100_0000,
32    // bit 7 Currently unused   = 0b0000_0000_1000_0000;
33    // bit 8 Currently unused   = 0b0000_0001_0000_0000;
34    // bit 9 Currently unused   = 0b0000_0010_0000_0000;
35    // bit 10 Currently unused  = 0b0000_0100_0000_0000;
36
37    /// Language encoding flag (EFS).
38    /// If this bit is set, the filename and comment fields for this file MUST be encoded using UTF-8.
39    LanguageEncoding            = 0b0000_1000_0000_0000,
40    /// Reserved by PKWARE for enhanced compression.
41    #[allow(unused)]
42    ReservedEnhancedCompression = 0b0001_0000_0000_0000,
43    /// Set when encrypting the Central Directory to indicate selected data values in the Local Header are masked to hide their actual values.
44    #[allow(unused)]
45    Masked                      = 0b0010_0000_0000_0000,
46    /// Reserved by PKWARE for alternate streams.
47    #[allow(unused)]
48    ReservedAlternateStream     = 0b0100_0000_0000_0000,
49    /// Reserved by PKWARE.
50    #[allow(unused)]
51    Reserved                    = 0b1000_0000_0000_0000,
52}
53
54impl ZipFlags {
55    #[inline]
56    pub(crate) fn matching(flags: u16, matching_flag: Self) -> bool {
57        flags & u16::from(matching_flag) != 0
58    }
59
60    #[inline]
61    pub(crate) const fn as_u16(self) -> u16 {
62        self as u16
63    }
64}
65
66impl From<ZipFlags> for u16 {
67    fn from(value: ZipFlags) -> u16 {
68        value.as_u16()
69    }
70}
71
72/// Zip flags
73#[repr(transparent)]
74#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
75pub struct ZipFileFlags(pub(crate) u16);
76
77impl ZipFileFlags {
78    /// Check if flags are matching a specific `ZipFlags`
79    #[inline]
80    #[must_use]
81    pub fn matching(&self, matching_flag: ZipFlags) -> bool {
82        ZipFlags::matching(self.0, matching_flag)
83    }
84
85    /// Check if the encrypted flag is set
86    #[inline]
87    #[must_use]
88    pub fn is_encrypted(&self) -> bool {
89        self.matching(ZipFlags::Encrypted)
90    }
91
92    /// Check if the data descriptor flag is set
93    #[inline]
94    #[must_use]
95    pub fn is_using_data_descriptor(&self) -> bool {
96        self.matching(ZipFlags::UsingDataDescriptor)
97    }
98
99    /// Get the inner value
100    #[inline]
101    #[must_use]
102    pub const fn as_u16(self) -> u16 {
103        self.0
104    }
105}
106
107impl From<u16> for ZipFileFlags {
108    fn from(value: u16) -> Self {
109        ZipFileFlags(value)
110    }
111}
112
113impl core::ops::BitOrAssign<u16> for ZipFileFlags {
114    fn bitor_assign(&mut self, rhs: u16) {
115        self.0 |= rhs;
116    }
117}