Skip to content

Commit

Permalink
fix: use exact reads rather than buf reader
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtang95 committed May 18, 2024
1 parent 979e447 commit 881a89d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub enum Error {
CannotDetectDefaultName(PathBuf),
#[error("parsing error due to invalid iro flags {0}")]
InvalidIroFlags(i32),
#[error("failed to parse binary data")]
// #[error("failed to parse binary data")]
#[error(transparent)]
CannotParseBinary(nom::Err<::nom::error::Error<Vec<u8>>>),
#[error("parsing error due to invalid file flags {0}")]
InvalidFileFlags(i32),
Expand Down
6 changes: 3 additions & 3 deletions src/iro_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nom::{
bytes::streaming::{tag, take},
number::streaming::{le_i32, le_u16, le_u32, le_u64},
bytes::complete::{tag, take},
number::complete::{le_i32, le_u16, le_u32, le_u64},
};

use crate::{
Expand All @@ -22,8 +22,8 @@ pub fn parse_iro_header_v2(bytes: &[u8]) -> Result<(&[u8], IroHeader), Error> {
))
}

/// Parse IroEntry without considering length of entire block
pub fn parse_iro_entry_v2(bytes: &[u8]) -> Result<(&[u8], IroEntry), Error> {
let (bytes, _) = le_u16(bytes)?;
let (bytes, filepath_len) = le_u16(bytes)?;
let (bytes, filepath) = take(filepath_len)(bytes)?;
let (bytes, file_flags) = le_i32(bytes)?;
Expand Down
24 changes: 14 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,10 @@ fn unpack_archive(iro_path: PathBuf, output_path: Option<PathBuf>) -> Result<Pat
return Err(Error::OutputPathExists(output_path));
}

let iro_file = std::fs::File::open(&iro_path)?;
let mut buf_reader = BufReader::new(&iro_file);
let bytes = buf_reader.fill_buf()?;
let (rem_bytes, iro_header) = parse_iro_header_v2(bytes)?;
let consumed_bytes_len = bytes.len() - rem_bytes.len();
buf_reader.consume(consumed_bytes_len);
let mut iro_file = std::fs::File::open(&iro_path)?;
let mut iro_header_bytes = [0u8; 20];
iro_file.read_exact(&mut iro_header_bytes)?;
let (_, iro_header) = parse_iro_header_v2(&iro_header_bytes)?;

println!("IRO metadata");
println!("- version: {}", iro_header.version);
Expand All @@ -197,12 +195,18 @@ fn unpack_archive(iro_path: PathBuf, output_path: Option<PathBuf>) -> Result<Pat

let mut iro_entries: Vec<IroEntry> = Vec::new();
for _ in 0..iro_header.num_files {
let bytes = buf_reader.fill_buf()?;
let (rem_bytes, iro_entry) = parse_iro_entry_v2(bytes)?;
let mut entry_len_bytes = [0u8; 2];
iro_file.read_exact(&mut entry_len_bytes)?;
let entry_len = u16::from_le_bytes(entry_len_bytes);
println!("{}", entry_len);

let mut entry_bytes = vec![0u8; entry_len as usize - 2];
iro_file.read_exact(entry_bytes.as_mut())?;
println!("{:?}", entry_bytes);

let (_, iro_entry) = parse_iro_entry_v2(&entry_bytes)?;

iro_entries.push(iro_entry);
let consumed_bytes_len = bytes.len() - rem_bytes.len();
buf_reader.consume(consumed_bytes_len);
}

for iro_entry in iro_entries {
Expand Down

0 comments on commit 881a89d

Please sign in to comment.