diff --git a/examples/subcommands/mod.rs b/examples/subcommands/mod.rs index 5748bf9..1c134e7 100644 --- a/examples/subcommands/mod.rs +++ b/examples/subcommands/mod.rs @@ -21,5 +21,8 @@ fn load_pak( }) .transpose()?; - unpak::PakReader::new_any( std::io::BufReader::new(std::fs::OpenOptions::new().read(true).open(&path)?), key) + unpak::PakReader::new_any( + std::io::BufReader::new(std::fs::OpenOptions::new().read(true).open(&path)?), + key, + ) } diff --git a/src/entry.rs b/src/entry.rs index 7150ef8..966c193 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -46,7 +46,7 @@ impl Entry { size += 8; // compressed size += 8; // uncompressed size += match version != Version::V8A { - true => 4, // 32 bit compression + true => 4, // 32 bit compression false => 1, // 8 bit compression }; size += match version.version_major() == VersionMajor::Initial { @@ -71,7 +71,11 @@ impl Entry { let offset = reader.read_u64::()?; let compressed = reader.read_u64::()?; let uncompressed = reader.read_u64::()?; - let compression = match if version == Version::V8A { reader.read_u8()? as u32 } else { reader.read_u32::()? } { + let compression = match if version == Version::V8A { + reader.read_u8()? as u32 + } else { + reader.read_u32::()? + } { 0x01 | 0x10 | 0x20 => Compression::Zlib, _ => Compression::None, }; @@ -91,8 +95,10 @@ impl Entry { true => Some(reader.read_array(Block::new)?), false => None, }, - encrypted: version.version_major() >= VersionMajor::CompressionEncryption && reader.read_bool()?, - block_uncompressed: match version.version_major() >= VersionMajor::CompressionEncryption { + encrypted: version.version_major() >= VersionMajor::CompressionEncryption + && reader.read_bool()?, + block_uncompressed: match version.version_major() >= VersionMajor::CompressionEncryption + { true => Some(reader.read_u32::()?), false => None, }, @@ -116,7 +122,7 @@ impl Entry { if block_uncompressed == 0x3f { block_uncompressed = reader.read_u32::()?; } else { - block_uncompressed = block_uncompressed << 11; + block_uncompressed <<= 11; } let mut var_int = |bit: u32| -> Result<_, super::Error> { @@ -134,7 +140,7 @@ impl Entry { _ => var_int(29)?, }; - block_uncompressed = if compression_block_count <= 0 { + block_uncompressed = if compression_block_count == 0 { 0 } else if uncompressed < block_uncompressed.into() { uncompressed.try_into().unwrap() @@ -157,7 +163,6 @@ impl Entry { let mut index = offset_base; Some( (0..compression_block_count) - .into_iter() .map(|_| { let mut block_size = reader.read_u32::()? as u64; let block = Block { @@ -220,7 +225,9 @@ impl Entry { for block in blocks { io::copy( &mut <$decompressor>::new( - &data[match version.version_major() >= VersionMajor::RelativeChunkOffsets { + &data[match version.version_major() + >= VersionMajor::RelativeChunkOffsets + { true => { (block.start - (data_offset - self.offset)) as usize ..(block.end - (data_offset - self.offset)) as usize diff --git a/src/footer.rs b/src/footer.rs index e498e5d..e30d3c1 100644 --- a/src/footer.rs +++ b/src/footer.rs @@ -23,10 +23,12 @@ impl Footer { true => Some(reader.read_u128::()?), false => None, }, - encrypted: version.version_major() >= VersionMajor::IndexEncryption && reader.read_bool()?, + encrypted: version.version_major() >= VersionMajor::IndexEncryption + && reader.read_bool()?, magic: reader.read_u32::()?, version, - version_major: VersionMajor::from_repr(reader.read_u32::()?).unwrap_or(version.version_major()), + version_major: VersionMajor::from_repr(reader.read_u32::()?) + .unwrap_or(version.version_major()), index_offset: reader.read_u64::()?, index_size: reader.read_u64::()?, hash: reader.read_guid()?, diff --git a/src/lib.rs b/src/lib.rs index 8b3ea74..4dc9374 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,12 +70,12 @@ impl Version { size += 1; } if self >= Version::V8A { - // compression names: [[u8; 32]; 5] + // compression names: [[u8; 32]; 4] size += 32 * 4; } if self >= Version::V8B { - // compression names: [[u8; 32]; 5] - size += 32 * 1; + // additional compression name + size += 32; } size } diff --git a/src/pak.rs b/src/pak.rs index c19939e..885273f 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -57,21 +57,11 @@ fn decrypt(key: &Option, bytes: &mut [u8]) -> Result<(), super:: } impl PakReader { - pub fn new_any( - mut reader: R, - key: Option, - ) -> Result { + pub fn new_any(mut reader: R, key: Option) -> Result { for ver in Version::iter() { - match PakReader::new( - &mut reader, - ver, - key.clone(), - ) { + match PakReader::new(&mut reader, ver, key.clone()) { Ok(pak) => { - return Ok(PakReader { - pak, - reader, - }); + return Ok(PakReader { pak, reader }); } _ => continue, } @@ -172,7 +162,7 @@ impl PakReader { // concat directory with file name to match IndexV1 but should provide a more direct access method let path = format!( "{}{}", - dir_name.strip_prefix("/").unwrap_or(dir_name), + dir_name.strip_prefix('/').unwrap_or(dir_name), file_name ); entries_by_path.insert(path, entry); @@ -224,13 +214,19 @@ impl PakReader { pub fn read(&mut self, path: &str, writer: &mut W) -> Result<(), super::Error> { match self.pak.index.entries().get(path) { - Some(entry) => entry.read(&mut self.reader, self.pak.version, self.pak.key.as_ref(), writer), + Some(entry) => entry.read( + &mut self.reader, + self.pak.version, + self.pak.key.as_ref(), + writer, + ), None => Err(super::Error::Other("no file found at given path")), } } pub fn files(&self) -> std::vec::IntoIter { - self.pak.index + self.pak + .index .entries() .keys() .cloned()