Skip to content

Commit

Permalink
Merge pull request #26 from eZioPan/negative-byte-offset
Browse files Browse the repository at this point in the history
allow negative byte offset
  • Loading branch information
Dirbaio authored Feb 23, 2024
2 parents 2222feb + a258685 commit 3875f77
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/transform/modify_byte_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,53 @@ use crate::ir::*;

#[derive(Debug, Serialize, Deserialize)]
pub struct ModifyByteOffset {
pub block: String,
pub add_offset: u32,
pub blocks: String,
pub exclude_items: Option<String>,
pub add_offset: i32,
pub strict: Option<bool>, // if this value is false, bypass overflowed/underflowed modification
}

impl ModifyByteOffset {
pub fn run(&self, ir: &mut IR) -> anyhow::Result<()> {
let path_re = make_regex(&self.block)?;
let path_re = make_regex(&self.blocks)?;
let ex_re = if let Some(exclude_items) = &self.exclude_items {
make_regex(exclude_items)?
} else {
make_regex("")?
};

let strict = self.strict.unwrap_or_default();

let mut err_names = Vec::new();

for id in match_all(ir.blocks.keys().cloned(), &path_re) {
let b = ir.blocks.get_mut(&id).unwrap();
for i in &mut b.items {
i.byte_offset += self.add_offset;
if ex_re.is_match(&i.name) {
continue;
}

match i.byte_offset.checked_add_signed(self.add_offset) {
Some(new_offset) => i.byte_offset = new_offset,
None if strict => err_names.push((id.clone(), i.name.clone())),
None => (),
};
}
}

if !err_names.is_empty() {
let mut err_msg = String::new();

for e_name in err_names {
err_msg.push_str(&format!(
"Block: {} Item: {}: byte_offset out of range after modify\n",
e_name.0, e_name.1
));
}

panic!("{err_msg}")
}

Ok(())
}
}

0 comments on commit 3875f77

Please sign in to comment.