-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement unpack iro subcommand (#1)
- Loading branch information
1 parent
51a5b05
commit f3ad7da
Showing
8 changed files
with
367 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::path::PathBuf; | ||
|
||
use thiserror::Error; | ||
|
||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Io(#[from] ::std::io::Error), | ||
#[error(transparent)] | ||
StripPrefix(#[from] ::std::path::StripPrefixError), | ||
#[error("{0} is not a directory")] | ||
NotDir(PathBuf), | ||
#[error("output path already exists: {0}")] | ||
OutputPathExists(PathBuf), | ||
#[error("{0} has invalid unicode")] | ||
InvalidUnicode(PathBuf), | ||
#[error("could not find default name from {0}")] | ||
CannotDetectDefaultName(PathBuf), | ||
#[error("parsing error due to invalid iro flags {0}")] | ||
InvalidIroFlags(i32), | ||
// #[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), | ||
#[error("invalid utf16 {0}")] | ||
InvalidUtf16(String), | ||
#[error("parten file path does not exists: {0}")] | ||
ParentPathDoesNotExist(PathBuf), | ||
} | ||
|
||
impl From<nom::Err<nom::error::Error<&[u8]>>> for Error { | ||
fn from(err: nom::Err<nom::error::Error<&[u8]>>) -> Self { | ||
Self::CannotParseBinary(err.map_input(|input| input.into())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use nom::{ | ||
bytes::complete::{tag, take}, | ||
number::complete::{le_i32, le_u16, le_u32, le_u64}, | ||
}; | ||
|
||
use crate::{ | ||
iro_entry::{FileFlags, IroEntry}, | ||
iro_header::{IroFlags, IroHeader, IroVersion, IRO_SIG}, | ||
Error, | ||
}; | ||
|
||
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, 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), | ||
)) | ||
} | ||
|
||
/// Parse IroEntry without considering length of entire block | ||
pub fn parse_iro_entry_v2(bytes: &[u8]) -> Result<(&[u8], IroEntry), Error> { | ||
let (bytes, filepath_len) = le_u16(bytes)?; | ||
let (bytes, filepath) = take(filepath_len)(bytes)?; | ||
let (bytes, file_flags) = le_i32(bytes)?; | ||
let (bytes, offset) = le_u64(bytes)?; | ||
let (bytes, data_len) = le_u32(bytes)?; | ||
|
||
Ok(( | ||
bytes, | ||
IroEntry::new( | ||
filepath.to_vec(), | ||
FileFlags::try_from(file_flags)?, | ||
offset, | ||
data_len, | ||
), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.