From 3c6dffa5ccfc72804a76f2090738721b90b10ce8 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Tue, 25 Jun 2024 23:23:19 +0900 Subject: Add bit flag logic --- src/zip/file.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'src/zip/file.rs') diff --git a/src/zip/file.rs b/src/zip/file.rs index f735d65..b4f2d7c 100644 --- a/src/zip/file.rs +++ b/src/zip/file.rs @@ -28,8 +28,100 @@ impl CompressionMethod { } } +pub struct BitFlag { + pub 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, pub crc: u32, pub compressed_size: u64, @@ -42,6 +134,7 @@ pub struct ZipFile { impl ZipFile { pub fn new( compression_method: CompressionMethod, + bit_flag: BitFlag, datetime: DateTime, crc: u32, compressed_size: u64, @@ -52,6 +145,7 @@ impl ZipFile { ) -> Self { Self { compression_method, + bit_flag, datetime, crc, compressed_size, -- cgit v1.2.3