Skip to content

Commit

Permalink
Factor out compression index size version check
Browse files Browse the repository at this point in the history
  • Loading branch information
trumank committed Aug 20, 2024
1 parent 55d419d commit 78c4725
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions repak/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ fn align(offset: u64) -> u64 {
(offset + 15) & !15
}

fn compression_index_size(version: Version) -> CompressionIndexSize {
match version {
Version::V8A => CompressionIndexSize::U8,
_ => CompressionIndexSize::U32,
}
}

enum CompressionIndexSize {
U8,
U32,
}

#[derive(Debug)]
pub(crate) struct Entry {
pub offset: u64,
Expand Down Expand Up @@ -65,9 +77,9 @@ impl Entry {
size += 8; // offset
size += 8; // compressed
size += 8; // uncompressed
size += match version != Version::V8A {
true => 4, // 32 bit compression
false => 1, // 8 bit compression
size += match compression_index_size(version) {
CompressionIndexSize::U8 => 1, // 8 bit compression
CompressionIndexSize::U32 => 4, // 32 bit compression
};
size += match version.version_major() == VersionMajor::Initial {
true => 8, // timestamp
Expand Down Expand Up @@ -223,10 +235,9 @@ impl Entry {
let offset = reader.read_u64::<LE>()?;
let compressed = reader.read_u64::<LE>()?;
let uncompressed = reader.read_u64::<LE>()?;
let compression = match if version == Version::V8A {
reader.read_u8()? as u32
} else {
reader.read_u32::<LE>()?
let compression = match match compression_index_size(version) {
CompressionIndexSize::U8 => reader.read_u8()? as u32,
CompressionIndexSize::U32 => reader.read_u32::<LE>()?,
} {
0 => None,
n => Some(n - 1),
Expand Down Expand Up @@ -267,9 +278,9 @@ impl Entry {
writer.write_u64::<LE>(self.compressed)?;
writer.write_u64::<LE>(self.uncompressed)?;
let compression = self.compression_slot.map_or(0, |n| n + 1);
match version {
Version::V8A => writer.write_u8(compression.try_into().unwrap())?,
_ => writer.write_u32::<LE>(compression)?,
match compression_index_size(version) {
CompressionIndexSize::U8 => writer.write_u8(compression.try_into().unwrap())?,
CompressionIndexSize::U32 => writer.write_u32::<LE>(compression)?,
}

if version.version_major() == VersionMajor::Initial {
Expand Down

0 comments on commit 78c4725

Please sign in to comment.