add search code
This commit is contained in:
parent
178f600ecb
commit
cbc1b33934
84
src/main.rs
84
src/main.rs
|
@ -95,7 +95,8 @@ fn verify_code(codes : &Vec<(u32, u32)>) -> Result<(), String> {
|
||||||
if search_bytes == 0 {
|
if search_bytes == 0 {
|
||||||
return Err(format!("wrong code at line {}: search length should not be 0", line));
|
return Err(format!("wrong code at line {}: search length should not be 0", line));
|
||||||
}
|
}
|
||||||
let code_len = ((search_bytes - 1) / 4 + 1) * 2;
|
let mut code_len = (search_bytes - 1) / 4 + 2;
|
||||||
|
code_len += code_len % 2;
|
||||||
if pos + code_len > max_len {
|
if pos + code_len > max_len {
|
||||||
return Err(format!("wrong code at line {}: unexpected end", line));
|
return Err(format!("wrong code at line {}: unexpected end", line));
|
||||||
}
|
}
|
||||||
|
@ -137,7 +138,7 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
let mut pos = 0usize;
|
let mut pos = 0usize;
|
||||||
let max_code_len = codes.len();
|
let max_code_len = codes.len();
|
||||||
let max_data_len = data.len();
|
let max_data_len = data.len();
|
||||||
let mut search_addr : Option<(bool, u32)> = None;
|
let mut search_addr : Option<usize> = None;
|
||||||
let mut code_len = 0;
|
let mut code_len = 0;
|
||||||
loop {
|
loop {
|
||||||
pos += code_len;
|
pos += code_len;
|
||||||
|
@ -151,14 +152,14 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
0x0 => {
|
0x0 => {
|
||||||
code_len = 2;
|
code_len = 2;
|
||||||
let val = codes[pos + 1].1 as u8;
|
let val = codes[pos + 1].1 as u8;
|
||||||
let addr = if let Some(saddr) = search_addr.take() {
|
let addr = if (first_code & 0xf000000) == 0x8000000 {
|
||||||
if saddr.0 == false {
|
if search_addr == None {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
(codes[pos].1 & 0xffffff) + saddr.1
|
(first_code & 0xffffff) as usize + search_addr.unwrap()
|
||||||
} else {
|
} else {
|
||||||
codes[pos].1 & 0xfffffff
|
(first_code & 0xfffffff) as usize
|
||||||
} as usize;
|
};
|
||||||
if addr >= max_data_len {
|
if addr >= max_data_len {
|
||||||
return Err(format!("apply code error at line {}: address overflow", line));
|
return Err(format!("apply code error at line {}: address overflow", line));
|
||||||
}
|
}
|
||||||
|
@ -167,14 +168,14 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
0x1 => {
|
0x1 => {
|
||||||
code_len = 2;
|
code_len = 2;
|
||||||
let val = codes[pos + 1].1 as u16;
|
let val = codes[pos + 1].1 as u16;
|
||||||
let addr = if let Some(saddr) = search_addr.take() {
|
let addr = if (first_code & 0xf000000) == 0x8000000 {
|
||||||
if saddr.0 == false {
|
if search_addr == None {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
(codes[pos].1 & 0xffffff) + saddr.1
|
(first_code & 0xffffff) as usize + search_addr.unwrap()
|
||||||
} else {
|
} else {
|
||||||
codes[pos].1 & 0xfffffff
|
(first_code & 0xfffffff) as usize
|
||||||
} as usize;
|
};
|
||||||
if addr + 1 >= max_data_len {
|
if addr + 1 >= max_data_len {
|
||||||
return Err(format!("apply code error at line {}: address overflow", line));
|
return Err(format!("apply code error at line {}: address overflow", line));
|
||||||
}
|
}
|
||||||
|
@ -184,14 +185,14 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
0x2 => {
|
0x2 => {
|
||||||
code_len = 2;
|
code_len = 2;
|
||||||
let val = codes[pos + 1].1;
|
let val = codes[pos + 1].1;
|
||||||
let addr = if let Some(saddr) = search_addr.take() {
|
let addr = if (first_code & 0xf000000) == 0x8000000 {
|
||||||
if saddr.0 == false {
|
if search_addr == None {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
(codes[pos].1 & 0xffffff) + saddr.1
|
(first_code & 0xffffff) as usize + search_addr.unwrap()
|
||||||
} else {
|
} else {
|
||||||
codes[pos].1 & 0xfffffff
|
(first_code & 0xfffffff) as usize
|
||||||
} as usize;
|
};
|
||||||
if addr + 3 >= max_data_len {
|
if addr + 3 >= max_data_len {
|
||||||
return Err(format!("apply code error at line {}: address overflow", line));
|
return Err(format!("apply code error at line {}: address overflow", line));
|
||||||
}
|
}
|
||||||
|
@ -203,24 +204,17 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
0x4 => {
|
0x4 => {
|
||||||
code_len = 4;
|
code_len = 4;
|
||||||
let mut val = codes[pos + 1].1;
|
let mut val = codes[pos + 1].1;
|
||||||
let mut addr = if let Some(saddr) = search_addr.take() {
|
let mut addr = (first_code & 0xffffff) as usize;
|
||||||
if saddr.0 == false {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
(codes[pos].1 & 0xffffff) + saddr.1
|
|
||||||
} else {
|
|
||||||
codes[pos].1 & 0xfffffff
|
|
||||||
} as usize;
|
|
||||||
let mut count = (codes[pos + 2].1 & 0xfff0000) >> 16;
|
let mut count = (codes[pos + 2].1 & 0xfff0000) >> 16;
|
||||||
let addr_diff = codes[pos + 2].1 & 0xffff;
|
let addr_diff = codes[pos + 2].1 & 0xffff;
|
||||||
let val_diff = codes[pos + 3].1;
|
let val_diff = codes[pos + 3].1;
|
||||||
while count != 0 {
|
while count != 0 {
|
||||||
if codes[pos].1 & 0xf000000 == 0x1000000 {
|
if first_code & 0xf000000 == 0x1000000 {
|
||||||
if addr >= max_data_len {
|
if addr >= max_data_len {
|
||||||
return Err(format!("apply code error at line {}: address overflow", line));
|
return Err(format!("apply code error at line {}: address overflow", line));
|
||||||
}
|
}
|
||||||
data[addr] = (val & 0xff) as u8;
|
data[addr] = (val & 0xff) as u8;
|
||||||
} else if codes[pos].1 & 0xf000000 == 0x2000000 {
|
} else if first_code & 0xf000000 == 0x2000000 {
|
||||||
if addr + 1 >= max_data_len {
|
if addr + 1 >= max_data_len {
|
||||||
return Err(format!("apply code error at line {}: address overflow", line));
|
return Err(format!("apply code error at line {}: address overflow", line));
|
||||||
}
|
}
|
||||||
|
@ -244,17 +238,19 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
return Err(format!("apply code error at line {}: not supported", line));
|
return Err(format!("apply code error at line {}: not supported", line));
|
||||||
}
|
}
|
||||||
0x8 => {
|
0x8 => {
|
||||||
// let count = (codes[pos].1 & 0xfff0000) >> 16;
|
let count = (codes[pos].1 & 0xfff0000) >> 16;
|
||||||
// let search_bytes = (first_code & 0xffff) as usize;
|
let search_bytes = (first_code & 0xffff) as usize;
|
||||||
// code_len = ((search_bytes - 1) / 4 + 1) * 2;
|
code_len = (search_bytes - 1) / 4 + 2;
|
||||||
// let mut bytes = 0;
|
code_len += code_len % 2;
|
||||||
// let mut search = Vec::with_capacity(search_bytes);
|
let mut bytes = 0;
|
||||||
// while bytes < search_bytes {
|
let mut search = Vec::with_capacity(search_bytes);
|
||||||
// let offset = pos + 1 + (bytes / 4);
|
while bytes < search_bytes {
|
||||||
// let shift = (3 - (bytes % 4)) * 8;
|
let offset = pos + 1 + (bytes / 4);
|
||||||
// search.push(((codes[offset].1 >> shift) & 0xff) as u8);
|
let shift = (3 - (bytes % 4)) * 8;
|
||||||
// bytes += 1;
|
search.push(((codes[offset].1 >> shift) & 0xff) as u8);
|
||||||
// }
|
bytes += 1;
|
||||||
|
}
|
||||||
|
search_addr = search_data(count, &data, &search);
|
||||||
}
|
}
|
||||||
0xa => {
|
0xa => {
|
||||||
let mut addr = (codes[pos].1 & 0xfffffff) as usize;
|
let mut addr = (codes[pos].1 & 0xfffffff) as usize;
|
||||||
|
@ -280,3 +276,15 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
|
||||||
write_file.write(data.as_slice()).map_err(fn_map_err)?;
|
write_file.write(data.as_slice()).map_err(fn_map_err)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_data(mut count : u32, data : &Vec<u8>, search : &Vec<u8>) -> Option<usize> {
|
||||||
|
for (position, window) in data.windows(search.len()).enumerate() {
|
||||||
|
if window == search {
|
||||||
|
count -= 1;
|
||||||
|
if count == 0 {
|
||||||
|
return Some(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue