Skip to content

Commit

Permalink
chore: refactor and add iro flags validation
Browse files Browse the repository at this point in the history
returning error if iro is of type patch
  • Loading branch information
tangtang95 committed May 19, 2024
1 parent 418fdd6 commit 6dec4c4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::PathBuf;

use thiserror::Error;

use crate::iro_header::{IroFlags, IroVersion};


#[derive(Error, Debug)]
pub enum Error {
Expand All @@ -19,15 +21,20 @@ pub enum Error {
CannotDetectDefaultName(PathBuf),
#[error("parsing error due to invalid iro flags {0}")]
InvalidIroFlags(i32),
// #[error("failed to parse binary data")]
#[error(transparent)]
#[error("parsing error due to invalid iro version {0}")]
InvalidIroVersion(i32),
#[error("failed to parse binary data")]
CannotParseBinary(nom::Err<::nom::error::Error<Vec<u8>>>),
#[error("parsing error due to invalid file flags {0}")]
InvalidFileFlags(i32),
#[error("invalid utf16 {0}")]
InvalidUtf16(String),
#[error("parten file path does not exists: {0}")]
#[error("parent file path does not exists: {0}")]
ParentPathDoesNotExist(PathBuf),
#[error("unsupporter iro version {0}")]
UnsupportedIroVersion(IroVersion),
#[error("unsupporter iro flags {0}")]
UnsupportedIroFlags(IroFlags),
}

impl From<nom::Err<nom::error::Error<&[u8]>>> for Error {
Expand Down
19 changes: 17 additions & 2 deletions src/iro_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ pub struct IroHeader {
pub num_files: u32,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[allow(dead_code)]
pub enum IroFlags {
None = 0,
Patch = 1,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[allow(dead_code)]
pub enum IroVersion {
Zero = 0x10000,
One = 0x10001,
Two = 0x10002,
}

Expand Down Expand Up @@ -75,7 +76,21 @@ impl Display for IroVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IroVersion::Zero => f.write_str("0x10000"),
IroVersion::One => f.write_str("0x10001"),
IroVersion::Two => f.write_str("0x10002"),
}
}
}

impl TryFrom<i32> for IroVersion {
type Error = Error;

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0x10000 => Ok(IroVersion::Zero),
0x10001 => Ok(IroVersion::One),
0x10002 => Ok(IroVersion::Two),
_ => Err(Error::InvalidIroVersion(value)),
}
}
}
4 changes: 2 additions & 2 deletions src/iro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use crate::{

pub fn parse_iro_header_v2(bytes: &[u8]) -> Result<(&[u8], IroHeader), Error> {
let (bytes, _) = tag(&IRO_SIG.to_le_bytes())(bytes)?;
let (bytes, _) = tag((IroVersion::Two as i32).to_le_bytes())(bytes)?;
let (bytes, version) = le_i32(bytes)?;
let (bytes, flags) = le_i32(bytes)?;
let (bytes, _) = tag(16i32.to_le_bytes())(bytes)?;
let (bytes, num_files) = le_u32(bytes)?;

Ok((
bytes,
IroHeader::new(IroVersion::Two, IroFlags::try_from(flags)?, 16, num_files),
IroHeader::new(IroVersion::try_from(version)?, IroFlags::try_from(flags)?, 16, num_files),
))
}

Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ fn unpack_archive(iro_path: PathBuf, output_path: Option<PathBuf>) -> Result<Pat
println!("- number of files: {}", iro_header.num_files);
println!();

// IRO validation
if iro_header.version != IroVersion::Two {
return Err(Error::UnsupportedIroVersion(iro_header.version));
}
if iro_header.flags != IroFlags::None {
return Err(Error::UnsupportedIroFlags(iro_header.flags));
}

let mut iro_entries: Vec<IroEntry> = Vec::new();
for _ in 0..iro_header.num_files {
let mut entry_len_bytes = [0u8; 2];
Expand Down

0 comments on commit 6dec4c4

Please sign in to comment.