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_driver.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_driver.rs')
| -rw-r--r-- | src/zip/file_driver.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/zip/file_driver.rs b/src/zip/file_driver.rs new file mode 100644 index 0000000..47b4242 --- /dev/null +++ b/src/zip/file_driver.rs | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | use crate::driver::FileDriver; | ||
| 2 | use crate::zip::{ZipError, ZipFileInfo, ZipResult}; | ||
| 3 | use std::io::{ | ||
| 4 | Error as IoError, ErrorKind as IoErrorKind, Read, Result as IoResult, Seek, SeekFrom, Take, | ||
| 5 | Write, | ||
| 6 | }; | ||
| 7 | |||
| 8 | pub struct ZipFile<'d, Io> { | ||
| 9 | io: &'d mut Io, | ||
| 10 | info: &'d ZipFileInfo, | ||
| 11 | |||
| 12 | bounds: (u64, u64), | ||
| 13 | cursor: u64, | ||
| 14 | } | ||
| 15 | |||
| 16 | impl<'d, Io> FileDriver for ZipFile<'d, Io> { | ||
| 17 | type Io = Io; | ||
| 18 | type FileInfo = ZipFileInfo; | ||
| 19 | |||
| 20 | fn info(&self) -> &Self::FileInfo { | ||
| 21 | self.info | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | impl<'d, Io: Read + Seek> ZipFile<'d, Io> { | ||
| 26 | pub fn new(io: &'d mut Io, info: &'d ZipFileInfo) -> ZipResult<Self> { | ||
| 27 | io.seek(SeekFrom::Start(info.header_pointer))?; | ||
| 28 | let buf = { | ||
| 29 | let mut buf = [0; 30]; | ||
| 30 | io.read(&mut buf)?; | ||
| 31 | buf | ||
| 32 | }; | ||
| 33 | if u32::from_le_bytes(buf[..4].try_into().unwrap()) != 0x04034b50 { | ||
| 34 | return Err(ZipError::InvalidFileHeaderSignature.into()); | ||
| 35 | } | ||
| 36 | let data_pointer = info.header_pointer | ||
| 37 | + 30 | ||
| 38 | + u16::from_le_bytes(buf[26..28].try_into().unwrap()) as u64 | ||
| 39 | + u16::from_le_bytes(buf[28..30].try_into().unwrap()) as u64; | ||
| 40 | io.seek(SeekFrom::Start(data_pointer))?; | ||
| 41 | |||
| 42 | Ok(Self { | ||
| 43 | io, | ||
| 44 | info, | ||
| 45 | |||
| 46 | bounds: (data_pointer, data_pointer + info.compressed_size), | ||
| 47 | cursor: data_pointer, | ||
| 48 | }) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<'d, Io: Read> Read for ZipFile<'d, Io> { | ||
| 53 | fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> { | ||
| 54 | let upper = buf.len().min((self.bounds.1 - self.cursor) as usize); | ||
| 55 | self.cursor += upper as u64; | ||
| 56 | self.io.read(&mut buf[..upper]) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | impl<'d, Io: Write> Write for ZipFile<'d, Io> { | ||
| 61 | fn write(&mut self, buf: &[u8]) -> IoResult<usize> { | ||
| 62 | todo!() | ||
| 63 | } | ||
| 64 | |||
| 65 | fn flush(&mut self) -> IoResult<()> { | ||
| 66 | todo!() | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | impl<'d, Io: Seek> Seek for ZipFile<'d, Io> { | ||
| 71 | fn seek(&mut self, pos: SeekFrom) -> IoResult<u64> { | ||
| 72 | self.cursor = match pos { | ||
| 73 | SeekFrom::Start(offset) => self.bounds.0 + offset, | ||
| 74 | SeekFrom::End(offset) => { | ||
| 75 | let cursor = self.bounds.1.saturating_add_signed(offset); | ||
| 76 | if cursor < self.bounds.0 { | ||
| 77 | return Err(IoError::new( | ||
| 78 | IoErrorKind::InvalidInput, | ||
| 79 | ZipError::NegativeFileOffset, | ||
| 80 | )); | ||
| 81 | } | ||
| 82 | cursor | ||
| 83 | } | ||
| 84 | SeekFrom::Current(offset) => { | ||
| 85 | let cursor = self.cursor.saturating_add_signed(offset); | ||
| 86 | if cursor < self.bounds.0 { | ||
| 87 | return Err(IoError::new( | ||
| 88 | IoErrorKind::InvalidInput, | ||
| 89 | ZipError::NegativeFileOffset, | ||
| 90 | )); | ||
| 91 | } | ||
| 92 | cursor | ||
| 93 | } | ||
| 94 | } | ||
| 95 | .min(self.bounds.1); | ||
| 96 | |||
| 97 | Ok(self.io.seek(SeekFrom::Start(self.cursor))? - self.bounds.0) | ||
| 98 | } | ||
| 99 | } | ||
