diff --git a/src/entry.rs b/src/entry.rs index 189b054..e1f7c5b 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,8 +1,7 @@ +use super::{Compression, ReadExt, Version}; use byteorder::{ReadBytesExt, LE}; use std::io; -use super::{Compression, ReadExt, Version}; - #[derive(Debug)] pub struct Block { pub offset: u64, @@ -72,8 +71,30 @@ impl Entry { version: super::Version, key: Option<&aes::Aes256Dec>, ) -> Result, super::Error> { - let buf = io::BufWriter::new(Vec::new()); - todo!("read the stuff"); + let mut buf = io::BufWriter::new(Vec::with_capacity(self.uncompressed as usize)); + reader.seek(io::SeekFrom::Start(self.offset))?; + let mut data = reader.read_len(match self.encrypted { + // add alignment (aes block size: 16) then zero out alignment bits + true => (self.compressed + 15) & !17, + false => self.compressed, + } as usize)?; + if self.encrypted { + if let Some(key) = key { + use aes::cipher::BlockDecrypt; + for block in data.chunks_mut(16) { + key.decrypt_block(aes::Block::from_mut_slice(block)) + } + data.truncate(self.compressed as usize); + } + } + use io::Write; + match self.compression { + Compression::None => buf.write_all(&data)?, + Compression::Zlib => todo!(), + Compression::Gzip => todo!(), + Compression::Oodle => todo!(), + } + buf.flush()?; Ok(buf.into_inner()?) } } diff --git a/src/footer.rs b/src/footer.rs index 3798cd5..7f6c54c 100644 --- a/src/footer.rs +++ b/src/footer.rs @@ -1,8 +1,6 @@ -use std::str::FromStr; - -use byteorder::{ReadBytesExt, LE}; - use super::{Compression, ReadExt, Version}; +use byteorder::{ReadBytesExt, LE}; +use std::str::FromStr; #[derive(Debug)] pub struct Footer { diff --git a/src/pak.rs b/src/pak.rs index ab766ae..d879e51 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -1,6 +1,5 @@ -use std::io; - use super::Version; +use std::io; #[derive(Debug)] pub struct Pak {