aboutsummaryrefslogtreecommitdiff
path: root/src/zip/file.rs
blob: 5b0723f5f484d2c04f0f7933b722b2314928243f (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
use crate::driver::ArchiveFile;
use crate::zip::{ZipError, ZipResult};
use chrono::{DateTime, Local};

pub enum CompressionMethod {
    Store,
    Deflate,
    BZIP2,
    LZMA,
    ZStd,
    XZ,
}

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

pub struct BitFlag {
    flag: u16,
}

pub mod bit {
    #[derive(Debug, PartialEq, Eq)]
    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
    }
}

pub struct ZipFile {
    pub compression_method: CompressionMethod,
    pub bit_flag: BitFlag,
    pub datetime: DateTime<Local>,
    pub crc: u32,
    pub compressed_size: u64,
    pub size: u64,
    pub header_pointer: u64,
    pub name: String,
    pub comment: String,
}

impl ZipFile {
    pub fn new(
        compression_method: CompressionMethod,
        bit_flag: BitFlag,
        datetime: DateTime<Local>,
        crc: u32,
        compressed_size: u64,
        size: u64,
        header_pointer: u64,
        name: String,
        comment: String,
    ) -> Self {
        Self {
            compression_method,
            bit_flag,
            datetime,
            crc,
            compressed_size,
            size,
            header_pointer,
            name,
            comment,
        }
    }
}

impl ArchiveFile for ZipFile {}