diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/archive.rs | 42 | ||||
| -rw-r--r-- | src/datatypes.rs | 20 | ||||
| -rw-r--r-- | src/file.rs | 11 | ||||
| -rw-r--r-- | src/io.rs | 25 | ||||
| -rw-r--r-- | src/lib.rs | 11 | ||||
| -rw-r--r-- | src/result.rs | 63 | ||||
| -rw-r--r-- | src/utils.rs | 60 | ||||
| -rw-r--r-- | src/zip/archive.rs | 0 | ||||
| -rw-r--r-- | src/zip/datatypes.rs | 166 | ||||
| -rw-r--r-- | src/zip/file.rs | 124 | ||||
| -rw-r--r-- | src/zip/io.rs | 155 | ||||
| -rw-r--r-- | src/zip/mod.rs | 6 |
12 files changed, 0 insertions, 683 deletions
diff --git a/src/archive.rs b/src/archive.rs deleted file mode 100644 index ef74de9..0000000 --- a/src/archive.rs +++ /dev/null | |||
| @@ -1,42 +0,0 @@ | |||
| 1 | use crate::io::{ArchiveRead, ArchiveWrite}; | ||
| 2 | use crate::result::ArchiveResult; | ||
| 3 | use std::fs::File; | ||
| 4 | use std::path::Path; | ||
| 5 | |||
| 6 | pub struct Archive<IO> { | ||
| 7 | pub io: IO, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl<IO: ArchiveRead> Archive<IO> { | ||
| 11 | pub fn file_reader(path: impl AsRef<Path>) -> ArchiveResult<Self> | ||
| 12 | where | ||
| 13 | IO: ArchiveRead<Reader = File>, | ||
| 14 | { | ||
| 15 | Self::reader(File::open(path)?) | ||
| 16 | } | ||
| 17 | |||
| 18 | pub fn reader(reader: IO::Reader) -> ArchiveResult<Self> { | ||
| 19 | Ok(Self { | ||
| 20 | io: IO::new(reader)?, | ||
| 21 | }) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | impl<IO: ArchiveRead> Archive<IO> {} | ||
| 26 | |||
| 27 | impl<IO: ArchiveWrite> Archive<IO> { | ||
| 28 | pub fn file_writer(path: impl AsRef<Path>) -> ArchiveResult<Self> | ||
| 29 | where | ||
| 30 | IO: ArchiveWrite<Writer = File>, | ||
| 31 | { | ||
| 32 | Self::writer(File::create(path)?) | ||
| 33 | } | ||
| 34 | |||
| 35 | pub fn writer(writer: IO::Writer) -> ArchiveResult<Self> { | ||
| 36 | Ok(Self { | ||
| 37 | io: IO::new(writer)?, | ||
| 38 | }) | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | impl<IO: ArchiveWrite> Archive<IO> {} | ||
diff --git a/src/datatypes.rs b/src/datatypes.rs deleted file mode 100644 index 59c7552..0000000 --- a/src/datatypes.rs +++ /dev/null | |||
| @@ -1,20 +0,0 @@ | |||
| 1 | use crate::result::ArchiveResult; | ||
| 2 | use crate::utils::ReadHelper; | ||
| 3 | use std::io::{Read, Write}; | ||
| 4 | |||
| 5 | pub trait ArchiveDatatype<const SIZE: usize>: Sized { | ||
| 6 | const SIZE: usize = SIZE; | ||
| 7 | |||
| 8 | fn parse(buf: [u8; SIZE]) -> Self; | ||
| 9 | |||
| 10 | fn dump(&self) -> [u8; SIZE]; | ||
| 11 | |||
| 12 | fn read(mut reader: impl Read) -> ArchiveResult<Self> { | ||
| 13 | Ok(Self::parse(reader.read2buf::<SIZE>()?)) | ||
| 14 | } | ||
| 15 | |||
| 16 | fn write(&self, mut writer: impl Write) -> ArchiveResult<()> { | ||
| 17 | writer.write(&self.dump())?; | ||
| 18 | Ok(()) | ||
| 19 | } | ||
| 20 | } | ||
diff --git a/src/file.rs b/src/file.rs deleted file mode 100644 index 9fa2add..0000000 --- a/src/file.rs +++ /dev/null | |||
| @@ -1,11 +0,0 @@ | |||
| 1 | use std::io::{Read, Write}; | ||
| 2 | |||
| 3 | pub trait ArchiveFile { | ||
| 4 | type Info; | ||
| 5 | |||
| 6 | fn new(info: Self::Info) -> Self; | ||
| 7 | } | ||
| 8 | |||
| 9 | pub trait ArchiveFileRead: Read + ArchiveFile {} | ||
| 10 | |||
| 11 | pub trait ArchiveFileWrite: Write + ArchiveFile {} | ||
diff --git a/src/io.rs b/src/io.rs deleted file mode 100644 index 43cf1a4..0000000 --- a/src/io.rs +++ /dev/null | |||
| @@ -1,25 +0,0 @@ | |||
| 1 | use crate::file::{ArchiveFileRead, ArchiveFileWrite}; | ||
| 2 | use crate::result::ArchiveResult; | ||
| 3 | use std::io::{Read, Write}; | ||
| 4 | |||
| 5 | pub trait ArchiveRead: Sized { | ||
| 6 | type Reader: Read; | ||
| 7 | type FileInfo; | ||
| 8 | type FileReader: ArchiveFileRead<Info = Self::FileInfo>; | ||
| 9 | |||
| 10 | fn new(reader: Self::Reader) -> ArchiveResult<Self>; | ||
| 11 | |||
| 12 | fn files(&self) -> ArchiveResult<Vec<Self::FileInfo>>; | ||
| 13 | |||
| 14 | fn open_file(&self, name: &str) -> ArchiveResult<Self::FileReader>; | ||
| 15 | } | ||
| 16 | |||
| 17 | pub trait ArchiveWrite: Sized { | ||
| 18 | type Writer: Write; | ||
| 19 | type FileInfo; | ||
| 20 | type FileWriter: ArchiveFileWrite<Info = Self::FileInfo>; | ||
| 21 | |||
| 22 | fn new(write: Self::Writer) -> ArchiveResult<Self>; | ||
| 23 | |||
| 24 | fn create_file(&self, name: &str) -> ArchiveResult<Self::FileWriter>; | ||
| 25 | } | ||
| @@ -1,11 +0,0 @@ | |||
| 1 | pub mod zip; | ||
| 2 | |||
| 3 | mod result; | ||
| 4 | mod utils; | ||
| 5 | |||
| 6 | mod archive; | ||
| 7 | mod datatypes; | ||
| 8 | mod file; | ||
| 9 | mod io; | ||
| 10 | |||
| 11 | pub use archive::Archive; | ||
diff --git a/src/result.rs b/src/result.rs deleted file mode 100644 index 9f35920..0000000 --- a/src/result.rs +++ /dev/null | |||
| @@ -1,63 +0,0 @@ | |||
| 1 | use std::error::Error; | ||
| 2 | use std::fmt::{Debug, Display}; | ||
| 3 | use std::io; | ||
| 4 | |||
| 5 | pub type ArchiveResult<R> = Result<R, ArchiveError>; | ||
| 6 | |||
| 7 | #[derive(Debug)] | ||
| 8 | pub enum ArchiveError { | ||
| 9 | IO(io::Error), | ||
| 10 | BadArchive { reason: &'static str }, | ||
| 11 | IncorrectSignature { expected: u32, received: u32 }, | ||
| 12 | IncorrectString { location: &'static str }, | ||
| 13 | IncorrectDate { year: u16, month: u16, day: u16 }, | ||
| 14 | IncorrectTime { hour: u16, min: u16, sec: u16 }, | ||
| 15 | UnsupportedCompressionMethod { method: u16 }, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl From<io::Error> for ArchiveError { | ||
| 19 | fn from(value: io::Error) -> Self { | ||
| 20 | Self::IO(value) | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | impl Display for ArchiveError { | ||
| 25 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 26 | match self { | ||
| 27 | Self::IO(err) => write!(f, "{err}"), | ||
| 28 | Self::BadArchive { reason } => { | ||
| 29 | write!(f, "Bad archive because {reason}") | ||
| 30 | } | ||
| 31 | Self::IncorrectSignature { expected, received } => { | ||
| 32 | write!( | ||
| 33 | f, | ||
| 34 | "Wrong signature (expected: {expected}, received: {received})" | ||
| 35 | ) | ||
| 36 | } | ||
| 37 | Self::IncorrectString { location } => { | ||
| 38 | write!(f, "Incorrect string in {location}") | ||
| 39 | } | ||
| 40 | Self::IncorrectDate { year, month, day } => { | ||
| 41 | write!( | ||
| 42 | f, | ||
| 43 | "Incorrect date (year: {year}, month: {month}, day: {day})" | ||
| 44 | ) | ||
| 45 | } | ||
| 46 | Self::IncorrectTime { hour, min, sec } => { | ||
| 47 | write!(f, "Incorrect time (hour: {hour}, min: {min}, sec: {sec})") | ||
| 48 | } | ||
| 49 | Self::UnsupportedCompressionMethod { method } => { | ||
| 50 | write!(f, "Unsupported compression method (method: {method})") | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | impl Error for ArchiveError { | ||
| 57 | fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
| 58 | match self { | ||
| 59 | Self::IO(source) => Some(source), | ||
| 60 | _ => None, | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index 57ad1c9..0000000 --- a/src/utils.rs +++ /dev/null | |||
| @@ -1,60 +0,0 @@ | |||
| 1 | use std::io::{Read, Result as IOResult}; | ||
| 2 | |||
| 3 | macro_rules! archive_datatype { | ||
| 4 | { | ||
| 5 | $vis:vis struct $struct_name:ident { | ||
| 6 | $( | ||
| 7 | $($fix_field_name:ident: $fix_field_type:ty)? | ||
| 8 | $([const] $const_field_name:ident: $const_field_type:ty = $const_field_value:expr)? | ||
| 9 | , | ||
| 10 | )* | ||
| 11 | } | ||
| 12 | } | ||
| 13 | => { | ||
| 14 | #[derive(Debug)] | ||
| 15 | $vis struct $struct_name { $($(pub $fix_field_name: $fix_field_type,)?)* } | ||
| 16 | |||
| 17 | #[allow(unused)] | ||
| 18 | impl $struct_name { | ||
| 19 | $($(pub const $const_field_name: $const_field_type = $const_field_value;)?)* | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | impl ArchiveDatatype<{$($((<$fix_field_type>::BITS as usize / 8)+)?)* 0}> for $struct_name { | ||
| 24 | fn parse(buf: [u8; Self::SIZE]) -> Self { | ||
| 25 | let mut byte = 0; | ||
| 26 | |||
| 27 | $($( | ||
| 28 | byte += (<$fix_field_type>::BITS / 8) as usize; | ||
| 29 | let $fix_field_name = <$fix_field_type>::from_le_bytes(buf[byte - (<$fix_field_type>::BITS as usize / 8)..byte].try_into().unwrap()); | ||
| 30 | )?)* | ||
| 31 | |||
| 32 | Self { $($($fix_field_name,)?)* } | ||
| 33 | } | ||
| 34 | |||
| 35 | fn dump(&self) -> [u8; Self::SIZE] { | ||
| 36 | [$($(&self.$fix_field_name.to_le_bytes()[..],)?)*] | ||
| 37 | .concat() | ||
| 38 | .try_into() | ||
| 39 | .unwrap() | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | pub(crate) use archive_datatype; | ||
| 45 | |||
| 46 | pub trait ReadHelper: Read { | ||
| 47 | fn read2buf<const SIZE: usize>(&mut self) -> IOResult<[u8; SIZE]> { | ||
| 48 | let mut buf = [0; SIZE]; | ||
| 49 | self.read(&mut buf)?; | ||
| 50 | Ok(buf) | ||
| 51 | } | ||
| 52 | |||
| 53 | fn read2vec(&mut self, size: usize) -> IOResult<Vec<u8>> { | ||
| 54 | let mut buf = vec![0; size]; | ||
| 55 | self.read(&mut buf)?; | ||
| 56 | Ok(buf) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | impl<R: Read> ReadHelper for R {} | ||
diff --git a/src/zip/archive.rs b/src/zip/archive.rs deleted file mode 100644 index e69de29..0000000 --- a/src/zip/archive.rs +++ /dev/null | |||
diff --git a/src/zip/datatypes.rs b/src/zip/datatypes.rs deleted file mode 100644 index 5ce1045..0000000 --- a/src/zip/datatypes.rs +++ /dev/null | |||
| @@ -1,166 +0,0 @@ | |||
| 1 | pub use crate::datatypes::ArchiveDatatype; | ||
| 2 | use crate::result::{ArchiveError, ArchiveResult}; | ||
| 3 | use crate::utils::{archive_datatype, ReadHelper}; | ||
| 4 | use std::io::{Read, Seek, SeekFrom}; | ||
| 5 | |||
| 6 | archive_datatype! { | ||
| 7 | pub struct LocalFileHeader { | ||
| 8 | [const] SIGNATURE: u32 = 0x04034b50, | ||
| 9 | signature: u32, | ||
| 10 | version_needed: u16, | ||
| 11 | general_purpose_bit_flag: u16, | ||
| 12 | compression_method: u16, | ||
| 13 | last_mod_file_time: u16, | ||
| 14 | last_mod_file_date: u16, | ||
| 15 | crc32: u32, | ||
| 16 | compressed_size: u32, | ||
| 17 | uncompressed_size: u32, | ||
| 18 | file_name_length: u16, | ||
| 19 | extra_field_length: u16, | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | archive_datatype! { | ||
| 24 | pub struct DataDescriptor { | ||
| 25 | [const] SIGNATURE: u32 = 0x08074b50, | ||
| 26 | crc32: u32, | ||
| 27 | compressed_size: u32, | ||
| 28 | uncompressed_size: u32, | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | archive_datatype! { | ||
| 33 | pub struct ArchiveExtraDataRecord { | ||
| 34 | [const] SIGNATURE: u32 = 0x08064b50, | ||
| 35 | signature: u32, | ||
| 36 | extra_field_length: u32, | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | archive_datatype! { | ||
| 41 | pub struct CentralDirectoryRecord { | ||
| 42 | [const] SIGNATURE: u32 = 0x02014b50, | ||
| 43 | signature: u32, | ||
| 44 | version_made_by: u16, | ||
| 45 | version_needed: u16, | ||
| 46 | general_purpose_bit_flag: u16, | ||
| 47 | compression_method: u16, | ||
| 48 | last_mod_file_time: u16, | ||
| 49 | last_mod_file_date: u16, | ||
| 50 | crc32: u32, | ||
| 51 | compressed_size: u32, | ||
| 52 | uncompressed_size: u32, | ||
| 53 | file_name_length: u16, | ||
| 54 | extra_field_length: u16, | ||
| 55 | file_comment_length: u16, | ||
| 56 | disk_number: u16, | ||
| 57 | internal_file_attributes: u16, | ||
| 58 | external_file_attributes: u32, | ||
| 59 | header_offset: u32, | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | archive_datatype! { | ||
| 64 | pub struct DigitalSignature{ | ||
| 65 | [const] SIGNATURE: u32 = 0x05054b50, | ||
| 66 | signature: u32, | ||
| 67 | data_size: u16, | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | archive_datatype! { | ||
| 72 | pub struct Zip64EndOfCentralDirectoryRecord { | ||
| 73 | [const] SIGNATURE: u32 = 0x06064b50, | ||
| 74 | signature: u32, | ||
| 75 | size_of_zip64_eocd: u64, | ||
| 76 | version_made_by: u16, | ||
| 77 | version_needed: u16, | ||
| 78 | disk: u32, | ||
| 79 | disk_where_starts_cd: u32, | ||
| 80 | entries_in_cd_on_disk: u64, | ||
| 81 | entries_in_cd: u64, | ||
| 82 | size_of_cd_records: u64, | ||
| 83 | offset_of_cd_entries: u64, | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | impl Zip64EndOfCentralDirectoryRecord { | ||
| 88 | pub fn find( | ||
| 89 | mut reader: impl Read + Seek, | ||
| 90 | eocd_offset: u64, | ||
| 91 | ) -> ArchiveResult<Option<(u64, Self, Vec<u8>)>> { | ||
| 92 | let locator_offset = eocd_offset - Zip64EndOfCentralDirectoryLocator::SIZE as u64; | ||
| 93 | reader.seek(SeekFrom::Start(locator_offset))?; | ||
| 94 | |||
| 95 | let locator = Zip64EndOfCentralDirectoryLocator::read(&mut reader)?; | ||
| 96 | if locator.signature != Zip64EndOfCentralDirectoryLocator::SIGNATURE { | ||
| 97 | return Ok(None); | ||
| 98 | } | ||
| 99 | |||
| 100 | reader.seek(SeekFrom::Start(locator.offset_of_zip64_eocd))?; | ||
| 101 | let eocd64 = Self::read(&mut reader)?; | ||
| 102 | if eocd64.signature != Self::SIGNATURE { | ||
| 103 | return Err(ArchiveError::IncorrectSignature { | ||
| 104 | expected: Self::SIGNATURE, | ||
| 105 | received: eocd64.signature, | ||
| 106 | }); | ||
| 107 | } | ||
| 108 | |||
| 109 | let ext_data = reader.read2vec(eocd64.size_of_zip64_eocd as usize + 12 - Self::SIZE)?; | ||
| 110 | Ok(Some((locator.offset_of_zip64_eocd, eocd64, ext_data))) | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | archive_datatype! { | ||
| 115 | pub struct Zip64EndOfCentralDirectoryLocator { | ||
| 116 | [const] SIGNATURE: u32 = 0x07064b50, | ||
| 117 | signature: u32, | ||
| 118 | disk_where_starts_zip64_eocd: u32, | ||
| 119 | offset_of_zip64_eocd: u64, | ||
| 120 | total_disks_number: u32, | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | archive_datatype! { | ||
| 125 | pub struct EndOfCentralDirectoryRecord { | ||
| 126 | [const] SIGNATURE: u32 = 0x06054b50, | ||
| 127 | signature: u32, | ||
| 128 | disk: u16, | ||
| 129 | disk_where_starts_cd: u16, | ||
| 130 | entries_in_cd_on_disk: u16, | ||
| 131 | entries_in_cd: u16, | ||
| 132 | size_of_cd_records: u32, | ||
| 133 | offset_of_cd_entries: u32, | ||
| 134 | comment_length: u16, | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | impl EndOfCentralDirectoryRecord { | ||
| 139 | pub fn find(mut reader: impl Read + Seek) -> ArchiveResult<(u64, Self, String)> { | ||
| 140 | let file_size = reader.seek(SeekFrom::End(0))? as usize; | ||
| 141 | let limit: usize = (u16::MAX as usize + Self::SIZE).min(file_size); | ||
| 142 | let mut buf = vec![0; limit]; | ||
| 143 | |||
| 144 | reader.seek(SeekFrom::End(-(limit as i64)))?; | ||
| 145 | reader.read(&mut buf)?; | ||
| 146 | |||
| 147 | for byte in 0..limit - 4 { | ||
| 148 | if u32::from_le_bytes(buf[byte..byte + 4].try_into().unwrap()) == Self::SIGNATURE { | ||
| 149 | let eocd = Self::parse(buf[byte..byte + Self::SIZE].try_into().unwrap()); | ||
| 150 | |||
| 151 | let comment = String::from_utf8( | ||
| 152 | buf[byte + Self::SIZE..byte + Self::SIZE + eocd.comment_length as usize].into(), | ||
| 153 | ) | ||
| 154 | .map_err(|_| ArchiveError::IncorrectString { | ||
| 155 | location: "archive_comment", | ||
| 156 | })?; | ||
| 157 | |||
| 158 | return Ok(((file_size - limit + byte) as u64, eocd, comment)); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | Err(ArchiveError::BadArchive { | ||
| 163 | reason: "end of central directory record not found", | ||
| 164 | }) | ||
| 165 | } | ||
| 166 | } | ||
diff --git a/src/zip/file.rs b/src/zip/file.rs deleted file mode 100644 index 261390f..0000000 --- a/src/zip/file.rs +++ /dev/null | |||
| @@ -1,124 +0,0 @@ | |||
| 1 | use crate::file::{ArchiveFile, ArchiveFileRead, ArchiveFileWrite}; | ||
| 2 | use crate::result::{ArchiveError, ArchiveResult}; | ||
| 3 | use chrono::NaiveDateTime; | ||
| 4 | use std::io::{Read, Write}; | ||
| 5 | |||
| 6 | pub struct GeneralPurposeBitFlag {} | ||
| 7 | |||
| 8 | pub enum CompressionMethod { | ||
| 9 | Store, | ||
| 10 | Deflate, | ||
| 11 | Bzip2, | ||
| 12 | LZMA, | ||
| 13 | Zstandard, | ||
| 14 | } | ||
| 15 | |||
| 16 | impl TryFrom<u16> for CompressionMethod { | ||
| 17 | type Error = ArchiveError; | ||
| 18 | |||
| 19 | fn try_from(value: u16) -> ArchiveResult<Self> { | ||
| 20 | Ok(match value { | ||
| 21 | 0 => Self::Store, | ||
| 22 | 8 => Self::Deflate, | ||
| 23 | 12 => Self::Bzip2, | ||
| 24 | 14 => Self::LZMA, | ||
| 25 | 93 | 20 => Self::Zstandard, | ||
| 26 | _ => return Err(ArchiveError::UnsupportedCompressionMethod { method: value }), | ||
| 27 | }) | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | pub struct FileInfo { | ||
| 32 | number: u64, | ||
| 33 | version_made_by: u16, | ||
| 34 | version_needed: u16, | ||
| 35 | general_purpose_bit_flag: GeneralPurposeBitFlag, | ||
| 36 | compression_method: CompressionMethod, | ||
| 37 | file_modified_at: NaiveDateTime, | ||
| 38 | crc32: u32, | ||
| 39 | compressed_size: u64, | ||
| 40 | uncompressed_size: u64, | ||
| 41 | name: String, | ||
| 42 | comment: String, | ||
| 43 | header_offset: u64, | ||
| 44 | } | ||
| 45 | |||
| 46 | impl FileInfo { | ||
| 47 | pub fn new( | ||
| 48 | number: u64, | ||
| 49 | version_made_by: u16, | ||
| 50 | version_needed: u16, | ||
| 51 | general_purpose_bit_flag: GeneralPurposeBitFlag, | ||
| 52 | compression_method: CompressionMethod, | ||
| 53 | file_modified_at: NaiveDateTime, | ||
| 54 | crc32: u32, | ||
| 55 | compressed_size: u64, | ||
| 56 | uncompressed_size: u64, | ||
| 57 | name: String, | ||
| 58 | comment: String, | ||
| 59 | header_offset: u64, | ||
| 60 | ) -> Self { | ||
| 61 | Self { | ||
| 62 | number, | ||
| 63 | version_made_by, | ||
| 64 | version_needed, | ||
| 65 | general_purpose_bit_flag, | ||
| 66 | compression_method, | ||
| 67 | file_modified_at, | ||
| 68 | crc32, | ||
| 69 | compressed_size, | ||
| 70 | uncompressed_size, | ||
| 71 | name, | ||
| 72 | comment, | ||
| 73 | header_offset, | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | pub struct FileReader { | ||
| 79 | info: FileInfo, | ||
| 80 | } | ||
| 81 | |||
| 82 | impl ArchiveFile for FileReader { | ||
| 83 | type Info = FileInfo; | ||
| 84 | |||
| 85 | fn new(info: Self::Info) -> Self { | ||
| 86 | Self { info } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | impl Read for FileReader { | ||
| 91 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
| 92 | todo!() | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | impl ArchiveFileRead for FileReader {} | ||
| 97 | |||
| 98 | impl FileReader {} | ||
| 99 | |||
| 100 | pub struct FileWriter { | ||
| 101 | info: FileInfo, | ||
| 102 | } | ||
| 103 | |||
| 104 | impl ArchiveFile for FileWriter { | ||
| 105 | type Info = FileInfo; | ||
| 106 | |||
| 107 | fn new(info: Self::Info) -> Self { | ||
| 108 | Self { info } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | impl Write for FileWriter { | ||
| 113 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
| 114 | todo!() | ||
| 115 | } | ||
| 116 | |||
| 117 | fn flush(&mut self) -> std::io::Result<()> { | ||
| 118 | todo!() | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | impl ArchiveFileWrite for FileWriter {} | ||
| 123 | |||
| 124 | impl FileWriter {} | ||
diff --git a/src/zip/io.rs b/src/zip/io.rs deleted file mode 100644 index c41607f..0000000 --- a/src/zip/io.rs +++ /dev/null | |||
| @@ -1,155 +0,0 @@ | |||
| 1 | use super::datatypes::*; | ||
| 2 | use super::file::{FileInfo, FileReader, FileWriter}; | ||
| 3 | use crate::io::{ArchiveRead, ArchiveWrite}; | ||
| 4 | use crate::result::{ArchiveError, ArchiveResult}; | ||
| 5 | use crate::utils::ReadHelper; | ||
| 6 | use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; | ||
| 7 | use std::collections::HashMap; | ||
| 8 | use std::fs::File; | ||
| 9 | use std::io::Read; | ||
| 10 | use std::io::{Seek, SeekFrom, Write}; | ||
| 11 | |||
| 12 | pub struct Reader<R: Read + Seek = File> { | ||
| 13 | reader: R, | ||
| 14 | files: HashMap<String, FileInfo>, | ||
| 15 | comment: String, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl<R: Read + Seek> ArchiveRead for Reader<R> { | ||
| 19 | type Reader = R; | ||
| 20 | type FileInfo = FileInfo; | ||
| 21 | type FileReader = FileReader; | ||
| 22 | |||
| 23 | fn new(mut reader: Self::Reader) -> ArchiveResult<Self> { | ||
| 24 | let cd_size: u64; | ||
| 25 | let cd_offset: u64; | ||
| 26 | let cd_entries: u64; | ||
| 27 | |||
| 28 | let (eocd_offset, eocd, comment) = EndOfCentralDirectoryRecord::find(&mut reader)?; | ||
| 29 | if let Some((eocd64_offset, eocd64, eocd64_extensible)) = | ||
| 30 | Zip64EndOfCentralDirectoryRecord::find(&mut reader, eocd_offset)? | ||
| 31 | { | ||
| 32 | cd_size = eocd64.size_of_cd_records; | ||
| 33 | cd_offset = eocd64.offset_of_cd_entries; | ||
| 34 | cd_entries = eocd64.entries_in_cd; | ||
| 35 | } else { | ||
| 36 | cd_size = eocd.size_of_cd_records as u64; | ||
| 37 | cd_offset = eocd.offset_of_cd_entries as u64; | ||
| 38 | cd_entries = eocd.entries_in_cd as u64; | ||
| 39 | } | ||
| 40 | |||
| 41 | let mut buf = vec![0; cd_size as usize]; | ||
| 42 | reader.seek(SeekFrom::Start(cd_offset))?; | ||
| 43 | reader.read(&mut buf)?; | ||
| 44 | |||
| 45 | let mut buf: &[u8] = &buf; | ||
| 46 | let mut files = HashMap::with_capacity(cd_entries as usize); | ||
| 47 | |||
| 48 | for entry_number in 0..cd_entries { | ||
| 49 | let cd = CentralDirectoryRecord::read(&mut buf)?; | ||
| 50 | |||
| 51 | let file_name = String::from_utf8(buf.read2vec(cd.file_name_length as usize)?).or( | ||
| 52 | Err(ArchiveError::IncorrectString { | ||
| 53 | location: "file_name", | ||
| 54 | }), | ||
| 55 | )?; | ||
| 56 | let mut extra_field: &[u8] = &buf.read2vec(cd.extra_field_length as usize)?; | ||
| 57 | let file_comment = String::from_utf8(buf.read2vec(cd.file_comment_length as usize)?) | ||
| 58 | .or(Err(ArchiveError::IncorrectString { | ||
| 59 | location: "file_comment", | ||
| 60 | }))?; | ||
| 61 | |||
| 62 | let mut uncompressed_size: u64 = cd.uncompressed_size as u64; | ||
| 63 | let mut compressed_size: u64 = cd.compressed_size as u64; | ||
| 64 | let mut header_offset: u64 = cd.header_offset as u64; | ||
| 65 | |||
| 66 | while extra_field.len() > 0 { | ||
| 67 | let header = u16::from_le_bytes(extra_field.read2buf()?); | ||
| 68 | let size = u16::from_le_bytes(extra_field.read2buf()?); | ||
| 69 | let mut data: &[u8] = &extra_field.read2vec(size as usize)?; | ||
| 70 | |||
| 71 | match header { | ||
| 72 | 0x0001 => { | ||
| 73 | if uncompressed_size == 0xFFFFFFFF { | ||
| 74 | uncompressed_size = u64::from_le_bytes(data.read2buf()?); | ||
| 75 | } | ||
| 76 | if compressed_size == 0xFFFFFFFF { | ||
| 77 | compressed_size = u64::from_le_bytes(data.read2buf()?); | ||
| 78 | } | ||
| 79 | if header_offset == 0xFFFFFFFF { | ||
| 80 | header_offset = u64::from_le_bytes(data.read2buf()?); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | _ => {} | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | let year = ((cd.last_mod_file_date >> 9) & 0x7F) + 1980; | ||
| 88 | let month = (cd.last_mod_file_date >> 5) & 0xF; | ||
| 89 | let day = cd.last_mod_file_date & 0x1F; | ||
| 90 | |||
| 91 | let hour = (cd.last_mod_file_time >> 11) & 0x1F; | ||
| 92 | let min = (cd.last_mod_file_time >> 5) & 0x3F; | ||
| 93 | let sec = (cd.last_mod_file_time & 0x1F) * 2; | ||
| 94 | |||
| 95 | files.insert( | ||
| 96 | file_name.clone(), | ||
| 97 | FileInfo::new( | ||
| 98 | entry_number, | ||
| 99 | cd.version_made_by, | ||
| 100 | cd.version_needed, | ||
| 101 | super::file::GeneralPurposeBitFlag {}, | ||
| 102 | cd.compression_method.try_into()?, | ||
| 103 | NaiveDateTime::new( | ||
| 104 | NaiveDate::from_ymd_opt(year as i32, month as u32, day as u32) | ||
| 105 | .ok_or(ArchiveError::IncorrectDate { year, month, day })?, | ||
| 106 | NaiveTime::from_hms_opt(hour as u32, min as u32, sec as u32) | ||
| 107 | .ok_or(ArchiveError::IncorrectTime { hour, min, sec })?, | ||
| 108 | ), | ||
| 109 | cd.crc32, | ||
| 110 | compressed_size, | ||
| 111 | uncompressed_size, | ||
| 112 | file_name, | ||
| 113 | file_comment, | ||
| 114 | header_offset, | ||
| 115 | ), | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | Ok(Self { | ||
| 120 | reader, | ||
| 121 | files, | ||
| 122 | comment, | ||
| 123 | }) | ||
| 124 | } | ||
| 125 | |||
| 126 | fn files(&self) -> ArchiveResult<Vec<Self::FileInfo>> { | ||
| 127 | todo!() | ||
| 128 | } | ||
| 129 | |||
| 130 | fn open_file(&self, name: &str) -> ArchiveResult<Self::FileReader> { | ||
| 131 | todo!() | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | impl<R: Read + Seek> Reader<R> {} | ||
| 136 | |||
| 137 | pub struct Writer<W: Write = File> { | ||
| 138 | writer: W, | ||
| 139 | } | ||
| 140 | |||
| 141 | impl<W: Write + Seek> ArchiveWrite for Writer<W> { | ||
| 142 | type Writer = W; | ||
| 143 | type FileInfo = FileInfo; | ||
| 144 | type FileWriter = FileWriter; | ||
| 145 | |||
| 146 | fn new(writer: Self::Writer) -> ArchiveResult<Self> { | ||
| 147 | todo!() | ||
| 148 | } | ||
| 149 | |||
| 150 | fn create_file(&self, name: &str) -> ArchiveResult<Self::FileWriter> { | ||
| 151 | todo!() | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | impl<W: Write> Writer<W> {} | ||
diff --git a/src/zip/mod.rs b/src/zip/mod.rs deleted file mode 100644 index b668b6b..0000000 --- a/src/zip/mod.rs +++ /dev/null | |||
| @@ -1,6 +0,0 @@ | |||
| 1 | mod archive; | ||
| 2 | mod datatypes; | ||
| 3 | mod file; | ||
| 4 | mod io; | ||
| 5 | |||
| 6 | pub use io::{Reader, Writer}; | ||
