Skip to content

Commit

Permalink
fix: buffer alloc and measure header size
Browse files Browse the repository at this point in the history
  • Loading branch information
Crauzer committed Nov 12, 2024
1 parent 13865c6 commit 461fe49
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
1 change: 1 addition & 0 deletions crates/league-modpkg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;

mod chunk;
mod error;
mod pkg_utils;
mod read;

#[derive(Debug, PartialEq)]
Expand Down
3 changes: 3 additions & 0 deletions crates/league-modpkg/src/pkg_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
impl Modpkg {
pub fn get
}
69 changes: 46 additions & 23 deletions crates/league-modpkg/src/read.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{error::ModpkgError, Modpkg, ModpkgChunk, ModpkgMetadata};
use byteorder::{ReadBytesExt as _, LE};
use io_ext::measure;
use std::{
collections::{hash_map::Entry, HashMap},
io::{BufReader, Read, Seek, SeekFrom},
Expand All @@ -9,37 +10,59 @@ impl Modpkg {
pub const MAGIC: u64 = u64::from_le_bytes(*b"_modpkg_");

pub fn read(reader: &mut BufReader<impl Read + Seek>) -> Result<Self, ModpkgError> {
let magic = reader.read_u64::<LE>()?;
if magic != Self::MAGIC {
return Err(ModpkgError::InvalidMagic(magic));
}
let (
real_header_size,
(
header_size,
metadata_size,
signature_size,
chunk_paths_size,
wad_paths_size,
layers_size,
chunk_count,
),
) = measure(reader, |reader| {
let magic = reader.read_u64::<LE>()?;
if magic != Self::MAGIC {
return Err(ModpkgError::InvalidMagic(magic));
}

let version = reader.read_u32::<LE>()?;
if version != 1 {
return Err(ModpkgError::InvalidVersion(version));
}
let version = reader.read_u32::<LE>()?;
if version != 1 {
return Err(ModpkgError::InvalidVersion(version));
}

let header_size = reader.read_u32::<LE>()?;
let metadata_size = reader.read_u32::<LE>()? as usize;
let signature_size = reader.read_u32::<LE>()? as usize;
let chunk_paths_size = reader.read_u32::<LE>()? as usize;
let wad_paths_size = reader.read_u32::<LE>()? as usize;
let layers_size = reader.read_u32::<LE>()? as usize;
let chunk_count = reader.read_u32::<LE>()?;
let header_size = reader.read_u32::<LE>()?;
let metadata_size = reader.read_u32::<LE>()? as usize;
let signature_size = reader.read_u32::<LE>()? as usize;
let chunk_paths_size = reader.read_u32::<LE>()? as usize;
let wad_paths_size = reader.read_u32::<LE>()? as usize;
let layers_size = reader.read_u32::<LE>()? as usize;
let chunk_count = reader.read_u32::<LE>()?;

let current_pos = reader.seek(SeekFrom::Current(0))?;
if header_size != current_pos as u32 {
Ok((
header_size,
metadata_size,
signature_size,
chunk_paths_size,
wad_paths_size,
layers_size,
chunk_count,
))
})?;

if header_size != real_header_size as u32 {
return Err(ModpkgError::InvalidHeaderSize {
header_size,
actual_size: current_pos,
actual_size: real_header_size,
});
}

let mut metadata = Vec::with_capacity(metadata_size);
let mut signature = Vec::with_capacity(signature_size);
let mut chunk_paths = Vec::with_capacity(chunk_paths_size);
let mut wad_paths = Vec::with_capacity(wad_paths_size);
let mut layers = Vec::with_capacity(layers_size);
let mut metadata = vec![0; metadata_size];
let mut signature = vec![0; signature_size];
let mut chunk_paths = vec![0; chunk_paths_size];
let mut wad_paths = vec![0; wad_paths_size];
let mut layers = vec![0; layers_size];

reader.read_exact(&mut metadata)?;
reader.read_exact(&mut signature)?;
Expand Down

0 comments on commit 461fe49

Please sign in to comment.