diff options
| author | Igor Tolmachev <me@igorek.dev> | 2024-06-27 16:15:00 +0900 |
|---|---|---|
| committer | Igor Tolmachev <me@igorek.dev> | 2024-06-29 21:18:46 +0900 |
| commit | 51694e1f0b2730915e0a57ec6d8de503cf06ef9a (patch) | |
| tree | 165f3adbc29f96814b57aeba394fb4045af7c5f0 /src/zip/file.rs | |
| parent | a867677218c1d55dadfcac1ca5b8cd32a78a3c28 (diff) | |
| download | archivator-51694e1f0b2730915e0a57ec6d8de503cf06ef9a.tar.gz archivator-51694e1f0b2730915e0a57ec6d8de503cf06ef9a.zip | |
Create file driver and implement file reader
Diffstat (limited to 'src/zip/file.rs')
| -rw-r--r-- | src/zip/file.rs | 160 |
1 files changed, 0 insertions, 160 deletions
diff --git a/src/zip/file.rs b/src/zip/file.rs deleted file mode 100644 index 5b0723f..0000000 --- a/src/zip/file.rs +++ /dev/null | |||
| @@ -1,160 +0,0 @@ | |||
| 1 | use crate::driver::ArchiveFile; | ||
| 2 | use crate::zip::{ZipError, ZipResult}; | ||
| 3 | use chrono::{DateTime, Local}; | ||
| 4 | |||
| 5 | pub enum CompressionMethod { | ||
| 6 | Store, | ||
| 7 | Deflate, | ||
| 8 | BZIP2, | ||
| 9 | LZMA, | ||
| 10 | ZStd, | ||
| 11 | XZ, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl CompressionMethod { | ||
| 15 | pub(crate) fn from_struct_id(id: u16) -> ZipResult<Self> { | ||
| 16 | match id { | ||
| 17 | 0 => Ok(Self::Store), | ||
| 18 | 8 => Ok(Self::Deflate), | ||
| 19 | 12 => Ok(Self::BZIP2), | ||
| 20 | 14 => Ok(Self::LZMA), | ||
| 21 | 93 => Ok(Self::ZStd), | ||
| 22 | 95 => Ok(Self::XZ), | ||
| 23 | 1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => { | ||
| 24 | Err(ZipError::UnsupportedCompressionMethod.into()) | ||
| 25 | } | ||
| 26 | 21..=92 | 100.. => Err(ZipError::InvalidCompressionMethod.into()), | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | pub struct BitFlag { | ||
| 32 | flag: u16, | ||
| 33 | } | ||
| 34 | |||
| 35 | pub mod bit { | ||
| 36 | #[derive(Debug, PartialEq, Eq)] | ||
| 37 | pub enum DeflateMode { | ||
| 38 | Normal, | ||
| 39 | Maximum, | ||
| 40 | Fast, | ||
| 41 | SuperFast, | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | macro_rules! get_set_bit_flag { | ||
| 46 | {$($get:ident $set:ident $bit:expr)+} => { | ||
| 47 | $( | ||
| 48 | pub fn $get(&self) -> bool { | ||
| 49 | self.get_bit($bit) | ||
| 50 | } | ||
| 51 | |||
| 52 | pub fn $set(&mut self, enable: bool) { | ||
| 53 | self.set_bit($bit, enable); | ||
| 54 | } | ||
| 55 | )* | ||
| 56 | }; | ||
| 57 | } | ||
| 58 | |||
| 59 | impl BitFlag { | ||
| 60 | pub fn new(flag: u16) -> Self { | ||
| 61 | Self { flag } | ||
| 62 | } | ||
| 63 | |||
| 64 | #[inline] | ||
| 65 | fn get_bit(&self, bit: u32) -> bool { | ||
| 66 | (self.flag & 2u16.pow(bit)) > 0 | ||
| 67 | } | ||
| 68 | |||
| 69 | #[inline] | ||
| 70 | fn set_bit(&mut self, bit: u32, enable: bool) { | ||
| 71 | if enable { | ||
| 72 | self.flag |= 2u16.pow(bit); | ||
| 73 | } else { | ||
| 74 | self.flag &= !2u16.pow(bit); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | pub fn deflate_mode(&self) -> bit::DeflateMode { | ||
| 79 | match self.flag & 6 { | ||
| 80 | 0 => bit::DeflateMode::Normal, | ||
| 81 | 2 => bit::DeflateMode::Maximum, | ||
| 82 | 4 => bit::DeflateMode::Fast, | ||
| 83 | 6 => bit::DeflateMode::SuperFast, | ||
| 84 | _ => panic!("impossible"), | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | pub fn set_deflate_mode(&mut self, mode: bit::DeflateMode) { | ||
| 89 | match mode { | ||
| 90 | bit::DeflateMode::Normal => { | ||
| 91 | self.set_bit(1, false); | ||
| 92 | self.set_bit(2, false); | ||
| 93 | } | ||
| 94 | bit::DeflateMode::Maximum => { | ||
| 95 | self.set_bit(1, true); | ||
| 96 | self.set_bit(2, false); | ||
| 97 | } | ||
| 98 | bit::DeflateMode::Fast => { | ||
| 99 | self.set_bit(1, false); | ||
| 100 | self.set_bit(2, true); | ||
| 101 | } | ||
| 102 | bit::DeflateMode::SuperFast => { | ||
| 103 | self.set_bit(1, true); | ||
| 104 | self.set_bit(2, true); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | get_set_bit_flag! { | ||
| 110 | is_encrypted set_encrypted 0 | ||
| 111 | is_imploding_8k set_imploding_8k 1 | ||
| 112 | is_imploding_3sf_trees set_imploding_3sf_trees 2 | ||
| 113 | is_lzma_has_eos_marker set_lzma_has_eos_marker 1 | ||
| 114 | is_has_data_descriptor set_has_data_descriptor 3 | ||
| 115 | is_patched_data set_patched_data 5 | ||
| 116 | is_strong_encryption set_strong_encryption 6 | ||
| 117 | is_utf8 set_utf8 11 | ||
| 118 | is_cd_encryption set_cd_encryption 13 | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | pub struct ZipFile { | ||
| 123 | pub compression_method: CompressionMethod, | ||
| 124 | pub bit_flag: BitFlag, | ||
| 125 | pub datetime: DateTime<Local>, | ||
| 126 | pub crc: u32, | ||
| 127 | pub compressed_size: u64, | ||
| 128 | pub size: u64, | ||
| 129 | pub header_pointer: u64, | ||
| 130 | pub name: String, | ||
| 131 | pub comment: String, | ||
| 132 | } | ||
| 133 | |||
| 134 | impl ZipFile { | ||
| 135 | pub fn new( | ||
| 136 | compression_method: CompressionMethod, | ||
| 137 | bit_flag: BitFlag, | ||
| 138 | datetime: DateTime<Local>, | ||
| 139 | crc: u32, | ||
| 140 | compressed_size: u64, | ||
| 141 | size: u64, | ||
| 142 | header_pointer: u64, | ||
| 143 | name: String, | ||
| 144 | comment: String, | ||
| 145 | ) -> Self { | ||
| 146 | Self { | ||
| 147 | compression_method, | ||
| 148 | bit_flag, | ||
| 149 | datetime, | ||
| 150 | crc, | ||
| 151 | compressed_size, | ||
| 152 | size, | ||
| 153 | header_pointer, | ||
| 154 | name, | ||
| 155 | comment, | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | impl ArchiveFile for ZipFile {} | ||
