repak/src/footer.rs

62 lines
2.3 KiB
Rust
Raw Normal View History

use std::str::FromStr;
2023-01-03 10:33:42 +00:00
use byteorder::{ReadBytesExt, LE};
use super::{Compression, ReadExt, Version};
pub struct Footer {
pub encryption_guid: Option<[u8; 20]>,
pub encrypted: Option<bool>,
pub magic: u32,
pub version: Version,
pub offset: u64,
pub size: u64,
pub hash: [u8; 20],
pub frozen: Option<bool>,
pub compression: Option<Vec<Compression>>,
}
impl Footer {
pub fn new<R: std::io::Read>(reader: &mut R, version: &Version) -> Result<Self, super::Error> {
2023-01-03 10:33:42 +00:00
let footer = Footer {
encryption_guid: (version >= &Version::EncryptionKeyGuid)
.then_some(reader.read_guid()?),
encrypted: (version >= &Version::CompressionEncryption).then_some(reader.read_bool()?),
magic: reader.read_u32::<LE>()?,
version: Version::from_repr(reader.read_u32::<LE>()?).unwrap_or_default(),
offset: reader.read_u64::<LE>()?,
size: reader.read_u64::<LE>()?,
hash: reader.read_guid()?,
frozen: (version == &Version::FrozenIndex).then_some(reader.read_bool()?),
compression: (version >= &Version::FNameBasedCompression).then_some({
let mut compression =
Vec::with_capacity(match version == &Version::FNameBasedCompression {
true => 4,
false => 5,
2023-01-03 10:33:42 +00:00
});
for _ in 0..compression.capacity() {
compression.push(
Compression::from_str(
&reader
.read_len(32)?
.iter()
// filter out whitespace and convert to char
.filter_map(|&ch| (ch != 0).then_some(ch as char))
.collect::<String>(),
)
.unwrap_or_default(),
)
2023-01-03 10:33:42 +00:00
}
compression
}),
};
if super::MAGIC != footer.magic {
2023-01-05 20:20:05 +00:00
return Err(super::Error::MagicMismatch(footer.magic));
2023-01-03 10:33:42 +00:00
}
if version != &footer.version {
2023-01-05 20:20:05 +00:00
return Err(super::Error::VersionMismatch(*version, footer.version));
2023-01-03 10:33:42 +00:00
}
Ok(footer)
}
}