diff --git a/src/ext/io.rs b/src/ext/io.rs index 3aafd31..f95104c 100644 --- a/src/ext/io.rs +++ b/src/ext/io.rs @@ -15,13 +15,13 @@ pub trait Peek { fn peek_exact(&mut self, buf: &mut [u8]) -> Result<()>; /// Peeks data from the reader at a specific offset into the provided buffer. /// Returns the number of bytes read. - fn peek_at(&mut self, offset: usize, buf: &mut [u8]) -> Result; + fn peek_at(&mut self, offset: u64, buf: &mut [u8]) -> Result; /// Peeks data from the reader at a specific offset into the provided buffer. /// Returns an error if the buffer is not filled completely. - fn peek_exact_at(&mut self, offset: usize, buf: &mut [u8]) -> Result<()>; + fn peek_exact_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<()>; /// Peeks data from the reader at a specific offset into a vector. /// Returns the vector containing the data read. - fn peek_at_vec(&mut self, offset: usize, len: usize) -> Result> { + fn peek_at_vec(&mut self, offset: u64, len: usize) -> Result> { let mut buf = vec![0u8; len]; let bytes_read = self.peek_at(offset, &mut buf)?; if bytes_read < len { @@ -31,7 +31,7 @@ pub trait Peek { } /// Peeks data from the reader at a specific offset into a vector. /// Returns an error if the buffer is not filled completely. - fn peek_exact_at_vec(&mut self, offset: usize, len: usize) -> Result> { + fn peek_exact_at_vec(&mut self, offset: u64, len: usize) -> Result> { let mut buf = vec![0u8; len]; self.peek_exact_at(offset, &mut buf)?; Ok(buf) @@ -146,109 +146,109 @@ pub trait Peek { Ok(i128::from_be_bytes(buf)) } /// Peeks a [u8] at a specific offset from the reader. - fn peek_u8_at(&mut self, offset: usize) -> Result { + fn peek_u8_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 1]; self.peek_exact_at(offset, &mut buf)?; Ok(buf[0]) } /// Peeks a [u16] at a specific offset from the reader in little-endian order. - fn peek_u16_at(&mut self, offset: usize) -> Result { + fn peek_u16_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.peek_exact_at(offset, &mut buf)?; Ok(u16::from_le_bytes(buf)) } /// Peeks a [u16] at a specific offset from the reader in big-endian order. - fn peek_u16_be_at(&mut self, offset: usize) -> Result { + fn peek_u16_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.peek_exact_at(offset, &mut buf)?; Ok(u16::from_be_bytes(buf)) } /// Peeks a [u32] at a specific offset from the reader in little-endian order. - fn peek_u32_at(&mut self, offset: usize) -> Result { + fn peek_u32_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.peek_exact_at(offset, &mut buf)?; Ok(u32::from_le_bytes(buf)) } /// Peeks a [u32] at a specific offset from the reader in big-endian order. - fn peek_u32_be_at(&mut self, offset: usize) -> Result { + fn peek_u32_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.peek_exact_at(offset, &mut buf)?; Ok(u32::from_be_bytes(buf)) } /// Peeks a [u64] at a specific offset from the reader in little-endian order. - fn peek_u64_at(&mut self, offset: usize) -> Result { + fn peek_u64_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.peek_exact_at(offset, &mut buf)?; Ok(u64::from_le_bytes(buf)) } /// Peeks a [u64] at a specific offset from the reader in big-endian order. - fn peek_u64_be_at(&mut self, offset: usize) -> Result { + fn peek_u64_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.peek_exact_at(offset, &mut buf)?; Ok(u64::from_be_bytes(buf)) } /// Peeks a [u128] at a specific offset from the reader in little-endian order. - fn peek_u128_at(&mut self, offset: usize) -> Result { + fn peek_u128_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.peek_exact_at(offset, &mut buf)?; Ok(u128::from_le_bytes(buf)) } /// Peeks a [u128] at a specific offset from the reader in big-endian order. - fn peek_u128_be_at(&mut self, offset: usize) -> Result { + fn peek_u128_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.peek_exact_at(offset, &mut buf)?; Ok(u128::from_be_bytes(buf)) } /// Peeks an [i8] at a specific offset from the reader. - fn peek_i8_at(&mut self, offset: usize) -> Result { + fn peek_i8_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 1]; self.peek_exact_at(offset, &mut buf)?; Ok(i8::from_le_bytes(buf)) } /// Peeks an [i16] at a specific offset from the reader in little-endian order. - fn peek_i16_at(&mut self, offset: usize) -> Result { + fn peek_i16_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.peek_exact_at(offset, &mut buf)?; Ok(i16::from_le_bytes(buf)) } /// Peeks an [i16] at a specific offset from the reader in big-endian order. - fn peek_i16_be_at(&mut self, offset: usize) -> Result { + fn peek_i16_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.peek_exact_at(offset, &mut buf)?; Ok(i16::from_be_bytes(buf)) } /// Peeks an [i32] at a specific offset from the reader in little-endian order. - fn peek_i32_at(&mut self, offset: usize) -> Result { + fn peek_i32_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.peek_exact_at(offset, &mut buf)?; Ok(i32::from_le_bytes(buf)) } /// Peeks an [i32] at a specific offset from the reader in big-endian order. - fn peek_i32_be_at(&mut self, offset: usize) -> Result { + fn peek_i32_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.peek_exact_at(offset, &mut buf)?; Ok(i32::from_be_bytes(buf)) } /// Peeks an [i64] at a specific offset from the reader in little-endian order. - fn peek_i64_at(&mut self, offset: usize) -> Result { + fn peek_i64_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.peek_exact_at(offset, &mut buf)?; Ok(i64::from_le_bytes(buf)) } /// Peeks an [i64] at a specific offset from the reader in big-endian order. - fn peek_i64_be_at(&mut self, offset: usize) -> Result { + fn peek_i64_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.peek_exact_at(offset, &mut buf)?; Ok(i64::from_be_bytes(buf)) } /// Peeks an [i128] at a specific offset from the reader in little-endian order. - fn peek_i128_at(&mut self, offset: usize) -> Result { + fn peek_i128_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.peek_exact_at(offset, &mut buf)?; Ok(i128::from_le_bytes(buf)) } /// Peeks an [i128] at a specific offset from the reader in big-endian order. - fn peek_i128_be_at(&mut self, offset: usize) -> Result { + fn peek_i128_be_at(&mut self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.peek_exact_at(offset, &mut buf)?; Ok(i128::from_be_bytes(buf)) @@ -257,7 +257,7 @@ pub trait Peek { /// Peeks a C-style string (null-terminated) from the reader. fn peek_cstring(&mut self) -> Result; /// Peeks a C-style string (null-terminated) from the reader at a specific offset. - fn peek_cstring_at(&mut self, offset: usize) -> Result; + fn peek_cstring_at(&mut self, offset: u64) -> Result; /// Reads a struct from the reader. /// The struct must implement the `StructUnpack` trait. @@ -299,7 +299,7 @@ pub trait Peek { Ok(()) } /// Peeks data at a specific offset and checks if it matches the provided data. - fn peek_and_equal_at(&mut self, offset: usize, data: &[u8]) -> Result<()> { + fn peek_and_equal_at(&mut self, offset: u64, data: &[u8]) -> Result<()> { let mut buf = vec![0u8; data.len()]; self.peek_exact_at(offset, &mut buf)?; if buf != data { @@ -327,17 +327,17 @@ impl Peek for T { Ok(()) } - fn peek_at(&mut self, offset: usize, buf: &mut [u8]) -> Result { + fn peek_at(&mut self, offset: u64, buf: &mut [u8]) -> Result { let current_pos = self.stream_position()?; - self.seek(SeekFrom::Start(offset as u64))?; + self.seek(SeekFrom::Start(offset))?; let bytes_read = self.read(buf)?; self.seek(SeekFrom::Start(current_pos))?; Ok(bytes_read) } - fn peek_exact_at(&mut self, offset: usize, buf: &mut [u8]) -> Result<()> { + fn peek_exact_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<()> { let current_pos = self.stream_position()?; - self.seek(SeekFrom::Start(offset as u64))?; + self.seek(SeekFrom::Start(offset))?; self.read_exact(buf)?; self.seek(SeekFrom::Start(current_pos))?; Ok(()) @@ -358,7 +358,7 @@ impl Peek for T { CString::new(buf).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } - fn peek_cstring_at(&mut self, offset: usize) -> Result { + fn peek_cstring_at(&mut self, offset: u64) -> Result { let current_pos = self.stream_position()?; let mut buf = Vec::new(); self.seek(SeekFrom::Start(offset as u64))?; @@ -399,10 +399,10 @@ pub trait CPeek { } /// Peeks data from the reader at a specific offset into the provided buffer. /// Returns the number of bytes read. - fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result; + fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result; /// Peeks data from the reader at a specific offset into the provided buffer. /// Returns an error if the buffer is not filled completely. - fn cpeek_exact_at(&self, offset: usize, buf: &mut [u8]) -> Result<()> { + fn cpeek_exact_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { let bytes_read = self.cpeek_at(offset, buf)?; if bytes_read < buf.len() { return Err(std::io::Error::new( @@ -414,7 +414,7 @@ pub trait CPeek { } /// Peeks data from the reader at a specific offset into a vector. /// Returns the vector containing the data read. - fn cpeek_at_vec(&self, offset: usize, len: usize) -> Result> { + fn cpeek_at_vec(&self, offset: u64, len: usize) -> Result> { let mut buf = vec![0u8; len]; let bytes_read = self.cpeek_at(offset, &mut buf)?; if bytes_read < len { @@ -424,7 +424,7 @@ pub trait CPeek { } /// Peeks data from the reader at a specific offset into a vector. /// Returns an error if the buffer is not filled completely. - fn cpeek_exact_at_vec(&self, offset: usize, len: usize) -> Result> { + fn cpeek_exact_at_vec(&self, offset: u64, len: usize) -> Result> { let mut buf = vec![0u8; len]; self.cpeek_exact_at(offset, &mut buf)?; Ok(buf) @@ -539,109 +539,109 @@ pub trait CPeek { Ok(i128::from_be_bytes(buf)) } /// Peeks a [u8] at a specific offset from the reader. - fn cpeek_u8_at(&self, offset: usize) -> Result { + fn cpeek_u8_at(&self, offset: u64) -> Result { let mut buf = [0u8; 1]; self.cpeek_exact_at(offset, &mut buf)?; Ok(buf[0]) } /// Peeks a [u16] at a specific offset from the reader in little-endian order. - fn cpeek_u16_at(&self, offset: usize) -> Result { + fn cpeek_u16_at(&self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u16::from_le_bytes(buf)) } /// Peeks a [u16] at a specific offset from the reader in big-endian order. - fn cpeek_u16_be_at(&self, offset: usize) -> Result { + fn cpeek_u16_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u16::from_be_bytes(buf)) } /// Peeks a [u32] at a specific offset from the reader in little-endian order. - fn cpeek_u32_at(&self, offset: usize) -> Result { + fn cpeek_u32_at(&self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u32::from_le_bytes(buf)) } /// Peeks a [u32] at a specific offset from the reader in big-endian order. - fn cpeek_u32_be_at(&self, offset: usize) -> Result { + fn cpeek_u32_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u32::from_be_bytes(buf)) } /// Peeks a [u64] at a specific offset from the reader in little-endian order. - fn cpeek_u64_at(&self, offset: usize) -> Result { + fn cpeek_u64_at(&self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u64::from_le_bytes(buf)) } /// Peeks a [u64] at a specific offset from the reader in big-endian order. - fn cpeek_u64_be_at(&self, offset: usize) -> Result { + fn cpeek_u64_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u64::from_be_bytes(buf)) } /// Peeks a [u128] at a specific offset from the reader in little-endian order. - fn cpeek_u128_at(&self, offset: usize) -> Result { + fn cpeek_u128_at(&self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u128::from_le_bytes(buf)) } /// Peeks a [u128] at a specific offset from the reader in big-endian order. - fn cpeek_u128_be_at(&self, offset: usize) -> Result { + fn cpeek_u128_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.cpeek_exact_at(offset, &mut buf)?; Ok(u128::from_be_bytes(buf)) } /// Peeks an [i8] at a specific offset from the reader. - fn cpeek_i8_at(&self, offset: usize) -> Result { + fn cpeek_i8_at(&self, offset: u64) -> Result { let mut buf = [0u8; 1]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i8::from_le_bytes(buf)) } /// Peeks an [i16] at a specific offset from the reader in little-endian order. - fn cpeek_i16_at(&self, offset: usize) -> Result { + fn cpeek_i16_at(&self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i16::from_le_bytes(buf)) } /// Peeks an [i16] at a specific offset from the reader in big-endian order. - fn cpeek_i16_be_at(&self, offset: usize) -> Result { + fn cpeek_i16_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 2]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i16::from_be_bytes(buf)) } /// Peeks an [i32] at a specific offset from the reader in little-endian order. - fn cpeek_i32_at(&self, offset: usize) -> Result { + fn cpeek_i32_at(&self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i32::from_le_bytes(buf)) } /// Peeks an [i32] at a specific offset from the reader in big-endian order. - fn cpeek_i32_be_at(&self, offset: usize) -> Result { + fn cpeek_i32_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 4]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i32::from_be_bytes(buf)) } /// Peeks an [i64] at a specific offset from the reader in little-endian order. - fn cpeek_i64_at(&self, offset: usize) -> Result { + fn cpeek_i64_at(&self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i64::from_le_bytes(buf)) } /// Peeks an [i64] at a specific offset from the reader in big-endian order. - fn cpeek_i64_be_at(&self, offset: usize) -> Result { + fn cpeek_i64_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 8]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i64::from_be_bytes(buf)) } /// Peeks an [i128] at a specific offset from the reader in little-endian order. - fn cpeek_i128_at(&self, offset: usize) -> Result { + fn cpeek_i128_at(&self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i128::from_le_bytes(buf)) } /// Peeks an [i128] at a specific offset from the reader in big-endian order. - fn cpeek_i128_be_at(&self, offset: usize) -> Result { + fn cpeek_i128_be_at(&self, offset: u64) -> Result { let mut buf = [0u8; 16]; self.cpeek_exact_at(offset, &mut buf)?; Ok(i128::from_be_bytes(buf)) @@ -651,13 +651,13 @@ pub trait CPeek { fn cpeek_cstring(&self) -> Result; /// Peeks a C-style string (null-terminated) from the reader at a specific offset. - fn cpeek_cstring_at(&self, offset: usize) -> Result { + fn cpeek_cstring_at(&self, offset: u64) -> Result { let mut buf = Vec::new(); let mut byte = [0u8; 1]; self.cpeek_at(offset, &mut byte)?; while byte[0] != 0 { buf.push(byte[0]); - self.cpeek_at(offset + buf.len(), &mut byte)?; + self.cpeek_at(offset + buf.len() as u64, &mut byte)?; } CString::new(buf).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } @@ -675,7 +675,7 @@ pub trait CPeek { Ok(()) } /// Peeks data at a specific offset and checks if it matches the provided data. - fn cpeek_and_equal_at(&self, offset: usize, data: &[u8]) -> Result<()> { + fn cpeek_and_equal_at(&self, offset: u64, data: &[u8]) -> Result<()> { let mut buf = vec![0u8; data.len()]; self.cpeek_exact_at(offset, &mut buf)?; if buf != data { @@ -696,7 +696,7 @@ impl CPeek for Mutex { lock.peek(buf) } - fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result { + fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result { let mut lock = self.lock().map_err(|_| { std::io::Error::new(std::io::ErrorKind::Other, "Failed to lock the mutex") })?; @@ -1291,7 +1291,7 @@ impl CPeek for MemReader { self.to_ref().cpeek(buf) } - fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result { + fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result { self.to_ref().cpeek_at(offset, buf) } @@ -1367,8 +1367,9 @@ impl<'a> CPeek for MemReaderRef<'a> { Ok(bytes_to_read) } - fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result { + fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result { let len = self.data.len(); + let offset = offset as usize; if offset >= len { return Ok(0); } @@ -1500,7 +1501,7 @@ impl CPeek for MemWriter { self.to_ref().cpeek(buf) } - fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result { + fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result { self.to_ref().cpeek_at(offset, buf) } @@ -1738,9 +1739,7 @@ impl Result, O: Fn(u64) -> R "Original offset is out of bounds for u32 address patching", )); } - self.input.seek(SeekFrom::Start(original_offset))?; - let original_address = self.input.read_u32()?; - self.input.seek(SeekFrom::Start(input_pos))?; + let original_address = self.input.peek_u32_at(original_offset)?; let new_offset = self.map_offset(original_offset)?; let offset = (self.address_to_offset)(original_address as u64)?; let offset = self.map_offset(offset)?; diff --git a/src/scripts/bgi/bp.rs b/src/scripts/bgi/bp.rs index 1653e26..fc46ad8 100644 --- a/src/scripts/bgi/bp.rs +++ b/src/scripts/bgi/bp.rs @@ -129,7 +129,7 @@ impl Script for BGIBpScript { for i in self.strings.iter() { let text_address = i.offset_pos + i.text_offset as usize - 1; // println!("offset: {}, text address: {}, text_offset: {}", i.offset_pos, text_address, i.text_offset); - let str = self.data.cpeek_cstring_at(text_address)?; + let str = self.data.cpeek_cstring_at(text_address as u64)?; let str = decode_to_string(self.encoding, str.as_bytes(), true)?; messages.push(Message { name: None, @@ -157,7 +157,7 @@ impl Script for BGIBpScript { let text_address = i.offset_pos + i.text_offset as usize - 1; let old_str_len = self .data - .cpeek_cstring_at(text_address)? + .cpeek_cstring_at(text_address as u64)? .as_bytes_with_nul() .len(); let mut str = mes.message; diff --git a/src/scripts/bgi/parser.rs b/src/scripts/bgi/parser.rs index 925b7a0..2a17820 100644 --- a/src/scripts/bgi/parser.rs +++ b/src/scripts/bgi/parser.rs @@ -451,13 +451,13 @@ impl<'a> V1Parser<'a> { pub fn is_empty_string(&self, address: usize) -> Result { let start = self.offset + address; - let data = self.buf.cpeek_u8_at(start)?; + let data = self.buf.cpeek_u8_at(start as u64)?; Ok(data == 0) } pub fn read_string_at_address(&mut self, address: usize) -> Result { let start = self.offset + address; - let buf = self.buf.peek_cstring_at(start)?; + let buf = self.buf.peek_cstring_at(start as u64)?; // Sometimes string has private use area characters, so we disable strict checking Ok(decode_to_string(self.encoding, buf.as_bytes(), false)?) } diff --git a/src/scripts/bgi/script.rs b/src/scripts/bgi/script.rs index 4cd1853..69cad85 100644 --- a/src/scripts/bgi/script.rs +++ b/src/scripts/bgi/script.rs @@ -129,7 +129,7 @@ impl BGIScript { fn read_string(&self, offset: usize) -> Result { let start = self.offset + offset; - let string_data = self.data.cpeek_cstring_at(start)?; + let string_data = self.data.cpeek_cstring_at(start as u64)?; // sometimes string has private use area characters, so we disable strict checking let string = decode_to_string(self.encoding, string_data.as_bytes(), false)?; Ok(string) @@ -319,7 +319,7 @@ impl Script for BGIScript { } let old_str_len = self .data - .cpeek_cstring_at(bgi_str_old_offset)? + .cpeek_cstring_at(bgi_str_old_offset as u64)? .as_bytes_with_nul() .len(); let nmess = encode_string(encoding, &nmes, false)?; @@ -382,7 +382,7 @@ impl Script for BGIScript { } let old_str_len = self .data - .cpeek_cstring_at(curs.address + self.offset)? + .cpeek_cstring_at((curs.address + self.offset) as u64)? .as_bytes_with_nul() .len(); let nmes = match curs.typ { diff --git a/src/scripts/cat_system/cst.rs b/src/scripts/cat_system/cst.rs index 4f571f5..33409f6 100644 --- a/src/scripts/cat_system/cst.rs +++ b/src/scripts/cat_system/cst.rs @@ -151,8 +151,7 @@ impl CstScript { let string_count = (strings_offset - string_address_offset) / 4; let mut strings = Vec::with_capacity(string_count as usize); for i in 0..string_count { - let offset = file.cpeek_u32_at(string_address_offset as usize + i as usize * 4)? - as usize + let offset = file.cpeek_u32_at(string_address_offset as u64 + i as u64 * 4)? as usize + strings_offset as usize; file.pos = offset; let start_marker = file.read_u8()?; diff --git a/src/scripts/cat_system/image/hg3.rs b/src/scripts/cat_system/image/hg3.rs index 928f6e4..8e1fff8 100644 --- a/src/scripts/cat_system/image/hg3.rs +++ b/src/scripts/cat_system/image/hg3.rs @@ -90,22 +90,22 @@ impl Hg3Image { } let mut offset = 0xC; let mut entries = Vec::new(); - let len = reader.data.len(); + let len = reader.data.len() as u64; while offset + 0x14 < len && reader.cpeek_and_equal_at(offset + 8, b"stdinfo").is_ok() { let mut section_size = reader.cpeek_u32_at(offset)?; if section_size == 0 { - section_size = (len - offset as usize) as u32; + section_size = (len - offset) as u32; } let stdinfo_size = reader.cpeek_u32_at(offset + 0x10)?; if reader - .cpeek_and_equal_at(offset + 8 + stdinfo_size as usize, b"img") + .cpeek_and_equal_at(offset + 8 + stdinfo_size as u64, b"img") .is_ok() { - reader.pos = offset + 16; + reader.pos = (offset + 16) as usize; let entry = Hg3Entry::unpack(&mut reader, false, Encoding::Cp932)?; - entries.push((entry, offset + 8, section_size as usize - 8)); + entries.push((entry, (offset + 8) as usize, section_size as usize - 8)); } - offset += section_size as usize; + offset += section_size as u64; } if entries.is_empty() { return Err(anyhow::anyhow!("No valid entries found in HG-3 image")); diff --git a/src/scripts/circus/archive/dat.rs b/src/scripts/circus/archive/dat.rs index ff997d1..1f70178 100644 --- a/src/scripts/circus/archive/dat.rs +++ b/src/scripts/circus/archive/dat.rs @@ -229,12 +229,12 @@ impl DatArchive { let index_size = (name_len + 4) * count as usize; count -= 1; let mut entries = Vec::with_capacity(count as usize); - let mut next_offset = reader.peek_u32_at(4 + name_len)?; + let mut next_offset = reader.peek_u32_at(4 + name_len as u64)?; if (next_offset as usize) < index_size + 4 { return Err(anyhow::anyhow!("Invalid next_offset")); } - let first_size = reader.peek_u32_at(name_len)?; - let second_offset = reader.peek_u32_at(8 + name_len * 2)?; + let first_size = reader.peek_u32_at(name_len as u64)?; + let second_offset = reader.peek_u32_at(8 + name_len as u64 * 2)?; if second_offset - next_offset == first_size { return Err(anyhow::anyhow!("Invalid second_offset")); } @@ -248,7 +248,7 @@ impl DatArchive { if i + 1 == count { next_offset = file_len as u32; } else { - next_offset = reader.peek_u32_at((name_len + 4) * (i as usize + 2))?; + next_offset = reader.peek_u32_at((name_len as u64 + 4) * (i as u64 + 2))?; } if next_offset < offset { return Err(anyhow::anyhow!("Invalid offset in DAT archive")); @@ -348,12 +348,12 @@ fn is_this_format_name_len(buf: &[u8], name_len: usize) -> Result { if mcount == 0 { return Err(anyhow::anyhow!("No entries found in DAT archive")); } - let mut next_offset = reader.cpeek_u32_at(4 + name_len)?; + let mut next_offset = reader.cpeek_u32_at(4 + name_len as u64)?; if (next_offset as usize) < index_size + 4 { return Err(anyhow::anyhow!("Invalid next_offset in DAT archive")); } - let first_size = reader.cpeek_u32_at(name_len)?; - let second_offset = reader.cpeek_u32_at(8 + name_len * 2)?; + let first_size = reader.cpeek_u32_at(name_len as u64)?; + let second_offset = reader.cpeek_u32_at(8 + name_len as u64 * 2)?; if second_offset - next_offset == first_size { return Err(anyhow::anyhow!("Invalid second_offset in DAT archive")); } @@ -362,7 +362,7 @@ fn is_this_format_name_len(buf: &[u8], name_len: usize) -> Result { if i + 1 == mcount { break; } else { - next_offset = reader.cpeek_u32_at((name_len + 4) * (i + 2))?; + next_offset = reader.cpeek_u32_at((name_len as u64 + 4) * (i as u64 + 2))?; } if next_offset < offset { return Err(anyhow::anyhow!("Invalid offset in DAT archive")); diff --git a/src/scripts/circus/audio/pcm.rs b/src/scripts/circus/audio/pcm.rs index 75b8d38..b08bb60 100644 --- a/src/scripts/circus/audio/pcm.rs +++ b/src/scripts/circus/audio/pcm.rs @@ -552,14 +552,14 @@ impl PcmDecoder { let mut v1 = a1; let mut v5 = 1; for _ in 0..0x1000 { - self.unk_6a16c8[v5] = self.encoded.cpeek_u8_at(v1)?; + self.unk_6a16c8[v5] = self.encoded.cpeek_u8_at(v1 as u64)?; v1 += 1; v5 += 2; } v5 = 0; for _ in 0..0x800 { - let v7 = self.encoded.cpeek_u8_at(v1 + 0x800)?; - let v8 = self.encoded.cpeek_u8_at(v1)?; + let v7 = self.encoded.cpeek_u8_at(v1 as u64 + 0x800)?; + let v8 = self.encoded.cpeek_u8_at(v1 as u64)?; self.unk_6a16c8[v5] = (v7 >> 4) | (v8 & 0xF0); self.unk_6a16c8[v5 + 2] = (v8 << 4) | (v7 & 0x0F); v5 += 4; diff --git a/src/scripts/escude/archive.rs b/src/scripts/escude/archive.rs index df3cb50..5d6663a 100644 --- a/src/scripts/escude/archive.rs +++ b/src/scripts/escude/archive.rs @@ -249,11 +249,11 @@ impl Script for EscudeBinArchi let entry = &self.entries[index]; let name = self .reader - .cpeek_cstring_at(entry.name_offset as usize + self.file_count as usize * 12 + 0x14)?; + .cpeek_cstring_at(entry.name_offset as u64 + self.file_count as u64 * 12 + 0x14)?; let name = decode_to_string(self.archive_encoding, name.as_bytes(), true)?; let mut data = self .reader - .cpeek_at_vec(entry.data_offset as usize, entry.length as usize)?; + .cpeek_at_vec(entry.data_offset as u64, entry.length as usize)?; if data.starts_with(b"acp") { let mut decoder = match super::lzw::LZWDecoder::new(&data) { Ok(decoder) => decoder, @@ -285,7 +285,7 @@ impl<'a, T: Iterator, R: Read + Seek> Iterator Some(entry) => entry, None => return None, }; - let name_offset = entry.name_offset as usize + self.file_count as usize * 12 + 0x14; + let name_offset = entry.name_offset as u64 + self.file_count as u64 * 12 + 0x14; let name = match self.reader.cpeek_cstring_at(name_offset) { Ok(name) => name, Err(e) => return Some(Err(e.into())), diff --git a/src/scripts/yaneurao/itufuru/archive.rs b/src/scripts/yaneurao/itufuru/archive.rs index 07544d1..566e015 100644 --- a/src/scripts/yaneurao/itufuru/archive.rs +++ b/src/scripts/yaneurao/itufuru/archive.rs @@ -257,9 +257,9 @@ impl Script for ItufuruArchive )); } let entry = &self.files[index]; - let file_offset = entry.offset as usize; + let file_offset = entry.offset as u64; match self.reader.cpeek_exact_at_vec( - file_offset + self.first_file_offset as usize, + file_offset + self.first_file_offset as u64, entry.size as usize, ) { Ok(data) => { diff --git a/src/scripts/yaneurao/itufuru/script.rs b/src/scripts/yaneurao/itufuru/script.rs index b8ca3de..25be5f0 100644 --- a/src/scripts/yaneurao/itufuru/script.rs +++ b/src/scripts/yaneurao/itufuru/script.rs @@ -131,7 +131,7 @@ impl Script for ItufuruScript { let mut messages = Vec::new(); for i in self.strings.iter() { let str_pos = i.len_pos + 2; // Skip the length bytes - let s = self.data.cpeek_cstring_at(str_pos)?; + let s = self.data.cpeek_cstring_at(str_pos as u64)?; let decoded = decode_to_string(self.encoding, s.as_bytes(), true)?; messages.push(Message { name: None,