aboutsummaryrefslogtreecommitdiff
path: root/src/zip/file/info.rs
blob: 599dcc398119d71e3e24ae1783ecb2266f03fbb5 (plain)
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
use crate::driver::ArchiveFileInfo;
use crate::zip::{ZipError, ZipResult};
use chrono::{DateTime, Local};

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum CompressionMethod {
    Store,
    Deflate,
    BZip2,
    Lzma,
    Zstd,
    Xz,
    Unsupported(u16),
}

impl CompressionMethod {
    pub(crate) fn from_struct_id(id: u16) -> ZipResult<Self> {
        Ok(match id {
            0 => Self::Store,
            8 => Self::Deflate,
            12 => Self::BZip2,
            14 => Self::Lzma,
            93 => Self::Zstd,
            95 => Self::Xz,
            1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => Self::Unsupported(id),
            21..=92 | 100.. => return Err(ZipError::InvalidCompressionMethod(id).into()),
        })
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum EncryptionMethod {
    None,
    Weak(u8),
    Unsupported,
}

impl EncryptionMethod {
    pub(crate) fn from_bif_flag(bit_flag: BitFlag, crc: u32, dos_time: u16) -> EncryptionMethod {
        match (bit_flag.is_encrypted(), bit_flag.is_strong_encryption()) {
            (false, false) => EncryptionMethod::None,
            (true, false) => EncryptionMethod::Weak(if bit_flag.is_has_data_descriptor() {
                (dos_time >> 8) as u8 // Info-ZIP modification
            } else {
                (crc >> 24) as u8
            }),
            (true, true) => EncryptionMethod::Unsupported,
            _ => panic!("impossible"),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct BitFlag {
    flag: u16,
}

pub mod bit {
    #[derive(Debug, PartialEq, Eq, Clone)]
    pub enum DeflateMode {
        Normal,
        Maximum,
        Fast,
        SuperFast,
    }
}

macro_rules! get_set_bit_flag {
    {$($get:ident $set:ident $bit:expr)+} => {
        $(
            pub fn $get(&self) -> bool {
                self.get_bit($bit)
            }

            pub fn $set(&mut self, enable: bool) {
                self.set_bit($bit, enable);
            }
        )*
    };
}

impl BitFlag {
    pub fn new(flag: u16) -> Self {
        Self { flag }
    }

    #[inline]
    fn get_bit(&self, bit: u32) -> bool {
        (self.flag & 2u16.pow(bit)) > 0
    }

    #[inline]
    fn set_bit(&mut self, bit: u32, enable: bool) {
        if enable {
            self.flag |= 2u16.pow(bit);
        } else {
            self.flag &= !2u16.pow(bit);
        }
    }

    pub fn deflate_mode(&self) -> bit::DeflateMode {
        match self.flag & 6 {
            0 => bit::DeflateMode::Normal,
            2 => bit::DeflateMode::Maximum,
            4 => bit::DeflateMode::Fast,
            6 => bit::DeflateMode::SuperFast,
            _ => panic!("impossible"),
        }
    }

    pub fn set_deflate_mode(&mut self, mode: bit::DeflateMode) {
        match mode {
            bit::DeflateMode::Normal => {
                self.set_bit(1, false);
                self.set_bit(2, false);
            }
            bit::DeflateMode::Maximum => {
                self.set_bit(1, true);
                self.set_bit(2, false);
            }
            bit::DeflateMode::Fast => {
                self.set_bit(1, false);
                self.set_bit(2, true);
            }
            bit::DeflateMode::SuperFast => {
                self.set_bit(1, true);
                self.set_bit(2, true);
            }
        }
    }

    get_set_bit_flag! {
        is_encrypted           set_encrypted           0
        is_imploding_8k        set_imploding_8k        1
        is_imploding_3sf_trees set_imploding_3sf_trees 2
        is_lzma_has_eos_marker set_lzma_has_eos_marker 1
        is_has_data_descriptor set_has_data_descriptor 3
        is_patched_data        set_patched_data        5
        is_strong_encryption   set_strong_encryption   6
        is_utf8                set_utf8                11
        is_cd_encryption       set_cd_encryption       13
    }
}

#[derive(Debug, Clone)]
pub struct ZipFileInfo {
    pub compression_method: CompressionMethod,
    pub encryption_method: EncryptionMethod,
    pub bit_flag: BitFlag,
    pub mtime: DateTime<Local>,
    pub atime: Option<DateTime<Local>>,
    pub ctime: Option<DateTime<Local>>,
    pub crc: u32,
    pub compressed_size: u64,
    pub size: u64,
    pub header_pointer: u64,
    pub name: String,
    pub comment: String,
}

impl ZipFileInfo {
    pub fn new(
        compression_method: CompressionMethod,
        encryption_method: EncryptionMethod,
        bit_flag: BitFlag,
        mtime: DateTime<Local>,
        atime: Option<DateTime<Local>>,
        ctime: Option<DateTime<Local>>,
        crc: u32,
        compressed_size: u64,
        size: u64,
        header_pointer: u64,
        name: String,
        comment: String,
    ) -> Self {
        Self {
            compression_method,
            encryption_method,
            bit_flag,
            mtime,
            atime,
            ctime,
            crc,
            compressed_size,
            size,
            header_pointer,
            name,
            comment,
        }
    }

    pub fn is_dir(&self) -> bool {
        self.name.ends_with("/")
    }
}

impl ArchiveFileInfo for ZipFileInfo {}