Update offset in Peek/CPeek trait from usize to u64

This commit is contained in:
2025-08-13 11:18:00 +08:00
parent b9ee9d1e42
commit 270a2d9fdf
11 changed files with 91 additions and 93 deletions

View File

@@ -15,13 +15,13 @@ pub trait Peek {
fn peek_exact(&mut self, buf: &mut [u8]) -> Result<()>; fn peek_exact(&mut self, buf: &mut [u8]) -> Result<()>;
/// Peeks data from the reader at a specific offset into the provided buffer. /// Peeks data from the reader at a specific offset into the provided buffer.
/// Returns the number of bytes read. /// Returns the number of bytes read.
fn peek_at(&mut self, offset: usize, buf: &mut [u8]) -> Result<usize>; fn peek_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize>;
/// Peeks data from the reader at a specific offset into the provided buffer. /// Peeks data from the reader at a specific offset into the provided buffer.
/// Returns an error if the buffer is not filled completely. /// 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. /// Peeks data from the reader at a specific offset into a vector.
/// Returns the vector containing the data read. /// Returns the vector containing the data read.
fn peek_at_vec(&mut self, offset: usize, len: usize) -> Result<Vec<u8>> { fn peek_at_vec(&mut self, offset: u64, len: usize) -> Result<Vec<u8>> {
let mut buf = vec![0u8; len]; let mut buf = vec![0u8; len];
let bytes_read = self.peek_at(offset, &mut buf)?; let bytes_read = self.peek_at(offset, &mut buf)?;
if bytes_read < len { if bytes_read < len {
@@ -31,7 +31,7 @@ pub trait Peek {
} }
/// Peeks data from the reader at a specific offset into a vector. /// Peeks data from the reader at a specific offset into a vector.
/// Returns an error if the buffer is not filled completely. /// Returns an error if the buffer is not filled completely.
fn peek_exact_at_vec(&mut self, offset: usize, len: usize) -> Result<Vec<u8>> { fn peek_exact_at_vec(&mut self, offset: u64, len: usize) -> Result<Vec<u8>> {
let mut buf = vec![0u8; len]; let mut buf = vec![0u8; len];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(buf) Ok(buf)
@@ -146,109 +146,109 @@ pub trait Peek {
Ok(i128::from_be_bytes(buf)) Ok(i128::from_be_bytes(buf))
} }
/// Peeks a [u8] at a specific offset from the reader. /// Peeks a [u8] at a specific offset from the reader.
fn peek_u8_at(&mut self, offset: usize) -> Result<u8> { fn peek_u8_at(&mut self, offset: u64) -> Result<u8> {
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(buf[0]) Ok(buf[0])
} }
/// Peeks a [u16] at a specific offset from the reader in little-endian order. /// Peeks a [u16] at a specific offset from the reader in little-endian order.
fn peek_u16_at(&mut self, offset: usize) -> Result<u16> { fn peek_u16_at(&mut self, offset: u64) -> Result<u16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u16::from_le_bytes(buf)) Ok(u16::from_le_bytes(buf))
} }
/// Peeks a [u16] at a specific offset from the reader in big-endian order. /// Peeks a [u16] at a specific offset from the reader in big-endian order.
fn peek_u16_be_at(&mut self, offset: usize) -> Result<u16> { fn peek_u16_be_at(&mut self, offset: u64) -> Result<u16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u16::from_be_bytes(buf)) Ok(u16::from_be_bytes(buf))
} }
/// Peeks a [u32] at a specific offset from the reader in little-endian order. /// Peeks a [u32] at a specific offset from the reader in little-endian order.
fn peek_u32_at(&mut self, offset: usize) -> Result<u32> { fn peek_u32_at(&mut self, offset: u64) -> Result<u32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u32::from_le_bytes(buf)) Ok(u32::from_le_bytes(buf))
} }
/// Peeks a [u32] at a specific offset from the reader in big-endian order. /// Peeks a [u32] at a specific offset from the reader in big-endian order.
fn peek_u32_be_at(&mut self, offset: usize) -> Result<u32> { fn peek_u32_be_at(&mut self, offset: u64) -> Result<u32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u32::from_be_bytes(buf)) Ok(u32::from_be_bytes(buf))
} }
/// Peeks a [u64] at a specific offset from the reader in little-endian order. /// Peeks a [u64] at a specific offset from the reader in little-endian order.
fn peek_u64_at(&mut self, offset: usize) -> Result<u64> { fn peek_u64_at(&mut self, offset: u64) -> Result<u64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u64::from_le_bytes(buf)) Ok(u64::from_le_bytes(buf))
} }
/// Peeks a [u64] at a specific offset from the reader in big-endian order. /// Peeks a [u64] at a specific offset from the reader in big-endian order.
fn peek_u64_be_at(&mut self, offset: usize) -> Result<u64> { fn peek_u64_be_at(&mut self, offset: u64) -> Result<u64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u64::from_be_bytes(buf)) Ok(u64::from_be_bytes(buf))
} }
/// Peeks a [u128] at a specific offset from the reader in little-endian order. /// Peeks a [u128] at a specific offset from the reader in little-endian order.
fn peek_u128_at(&mut self, offset: usize) -> Result<u128> { fn peek_u128_at(&mut self, offset: u64) -> Result<u128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u128::from_le_bytes(buf)) Ok(u128::from_le_bytes(buf))
} }
/// Peeks a [u128] at a specific offset from the reader in big-endian order. /// Peeks a [u128] at a specific offset from the reader in big-endian order.
fn peek_u128_be_at(&mut self, offset: usize) -> Result<u128> { fn peek_u128_be_at(&mut self, offset: u64) -> Result<u128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(u128::from_be_bytes(buf)) Ok(u128::from_be_bytes(buf))
} }
/// Peeks an [i8] at a specific offset from the reader. /// Peeks an [i8] at a specific offset from the reader.
fn peek_i8_at(&mut self, offset: usize) -> Result<i8> { fn peek_i8_at(&mut self, offset: u64) -> Result<i8> {
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i8::from_le_bytes(buf)) Ok(i8::from_le_bytes(buf))
} }
/// Peeks an [i16] at a specific offset from the reader in little-endian order. /// Peeks an [i16] at a specific offset from the reader in little-endian order.
fn peek_i16_at(&mut self, offset: usize) -> Result<i16> { fn peek_i16_at(&mut self, offset: u64) -> Result<i16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i16::from_le_bytes(buf)) Ok(i16::from_le_bytes(buf))
} }
/// Peeks an [i16] at a specific offset from the reader in big-endian order. /// Peeks an [i16] at a specific offset from the reader in big-endian order.
fn peek_i16_be_at(&mut self, offset: usize) -> Result<i16> { fn peek_i16_be_at(&mut self, offset: u64) -> Result<i16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i16::from_be_bytes(buf)) Ok(i16::from_be_bytes(buf))
} }
/// Peeks an [i32] at a specific offset from the reader in little-endian order. /// Peeks an [i32] at a specific offset from the reader in little-endian order.
fn peek_i32_at(&mut self, offset: usize) -> Result<i32> { fn peek_i32_at(&mut self, offset: u64) -> Result<i32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i32::from_le_bytes(buf)) Ok(i32::from_le_bytes(buf))
} }
/// Peeks an [i32] at a specific offset from the reader in big-endian order. /// Peeks an [i32] at a specific offset from the reader in big-endian order.
fn peek_i32_be_at(&mut self, offset: usize) -> Result<i32> { fn peek_i32_be_at(&mut self, offset: u64) -> Result<i32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i32::from_be_bytes(buf)) Ok(i32::from_be_bytes(buf))
} }
/// Peeks an [i64] at a specific offset from the reader in little-endian order. /// Peeks an [i64] at a specific offset from the reader in little-endian order.
fn peek_i64_at(&mut self, offset: usize) -> Result<i64> { fn peek_i64_at(&mut self, offset: u64) -> Result<i64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i64::from_le_bytes(buf)) Ok(i64::from_le_bytes(buf))
} }
/// Peeks an [i64] at a specific offset from the reader in big-endian order. /// Peeks an [i64] at a specific offset from the reader in big-endian order.
fn peek_i64_be_at(&mut self, offset: usize) -> Result<i64> { fn peek_i64_be_at(&mut self, offset: u64) -> Result<i64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i64::from_be_bytes(buf)) Ok(i64::from_be_bytes(buf))
} }
/// Peeks an [i128] at a specific offset from the reader in little-endian order. /// Peeks an [i128] at a specific offset from the reader in little-endian order.
fn peek_i128_at(&mut self, offset: usize) -> Result<i128> { fn peek_i128_at(&mut self, offset: u64) -> Result<i128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i128::from_le_bytes(buf)) Ok(i128::from_le_bytes(buf))
} }
/// Peeks an [i128] at a specific offset from the reader in big-endian order. /// Peeks an [i128] at a specific offset from the reader in big-endian order.
fn peek_i128_be_at(&mut self, offset: usize) -> Result<i128> { fn peek_i128_be_at(&mut self, offset: u64) -> Result<i128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
Ok(i128::from_be_bytes(buf)) Ok(i128::from_be_bytes(buf))
@@ -257,7 +257,7 @@ pub trait Peek {
/// Peeks a C-style string (null-terminated) from the reader. /// Peeks a C-style string (null-terminated) from the reader.
fn peek_cstring(&mut self) -> Result<CString>; fn peek_cstring(&mut self) -> Result<CString>;
/// Peeks a C-style string (null-terminated) from the reader at a specific offset. /// Peeks a C-style string (null-terminated) from the reader at a specific offset.
fn peek_cstring_at(&mut self, offset: usize) -> Result<CString>; fn peek_cstring_at(&mut self, offset: u64) -> Result<CString>;
/// Reads a struct from the reader. /// Reads a struct from the reader.
/// The struct must implement the `StructUnpack` trait. /// The struct must implement the `StructUnpack` trait.
@@ -299,7 +299,7 @@ pub trait Peek {
Ok(()) Ok(())
} }
/// Peeks data at a specific offset and checks if it matches the provided data. /// 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()]; let mut buf = vec![0u8; data.len()];
self.peek_exact_at(offset, &mut buf)?; self.peek_exact_at(offset, &mut buf)?;
if buf != data { if buf != data {
@@ -327,17 +327,17 @@ impl<T: Read + Seek> Peek for T {
Ok(()) Ok(())
} }
fn peek_at(&mut self, offset: usize, buf: &mut [u8]) -> Result<usize> { fn peek_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize> {
let current_pos = self.stream_position()?; let current_pos = self.stream_position()?;
self.seek(SeekFrom::Start(offset as u64))?; self.seek(SeekFrom::Start(offset))?;
let bytes_read = self.read(buf)?; let bytes_read = self.read(buf)?;
self.seek(SeekFrom::Start(current_pos))?; self.seek(SeekFrom::Start(current_pos))?;
Ok(bytes_read) 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()?; let current_pos = self.stream_position()?;
self.seek(SeekFrom::Start(offset as u64))?; self.seek(SeekFrom::Start(offset))?;
self.read_exact(buf)?; self.read_exact(buf)?;
self.seek(SeekFrom::Start(current_pos))?; self.seek(SeekFrom::Start(current_pos))?;
Ok(()) Ok(())
@@ -358,7 +358,7 @@ impl<T: Read + Seek> Peek for T {
CString::new(buf).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) CString::new(buf).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
} }
fn peek_cstring_at(&mut self, offset: usize) -> Result<CString> { fn peek_cstring_at(&mut self, offset: u64) -> Result<CString> {
let current_pos = self.stream_position()?; let current_pos = self.stream_position()?;
let mut buf = Vec::new(); let mut buf = Vec::new();
self.seek(SeekFrom::Start(offset as u64))?; 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. /// Peeks data from the reader at a specific offset into the provided buffer.
/// Returns the number of bytes read. /// Returns the number of bytes read.
fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize>; fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize>;
/// Peeks data from the reader at a specific offset into the provided buffer. /// Peeks data from the reader at a specific offset into the provided buffer.
/// Returns an error if the buffer is not filled completely. /// 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)?; let bytes_read = self.cpeek_at(offset, buf)?;
if bytes_read < buf.len() { if bytes_read < buf.len() {
return Err(std::io::Error::new( 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. /// Peeks data from the reader at a specific offset into a vector.
/// Returns the vector containing the data read. /// Returns the vector containing the data read.
fn cpeek_at_vec(&self, offset: usize, len: usize) -> Result<Vec<u8>> { fn cpeek_at_vec(&self, offset: u64, len: usize) -> Result<Vec<u8>> {
let mut buf = vec![0u8; len]; let mut buf = vec![0u8; len];
let bytes_read = self.cpeek_at(offset, &mut buf)?; let bytes_read = self.cpeek_at(offset, &mut buf)?;
if bytes_read < len { if bytes_read < len {
@@ -424,7 +424,7 @@ pub trait CPeek {
} }
/// Peeks data from the reader at a specific offset into a vector. /// Peeks data from the reader at a specific offset into a vector.
/// Returns an error if the buffer is not filled completely. /// Returns an error if the buffer is not filled completely.
fn cpeek_exact_at_vec(&self, offset: usize, len: usize) -> Result<Vec<u8>> { fn cpeek_exact_at_vec(&self, offset: u64, len: usize) -> Result<Vec<u8>> {
let mut buf = vec![0u8; len]; let mut buf = vec![0u8; len];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(buf) Ok(buf)
@@ -539,109 +539,109 @@ pub trait CPeek {
Ok(i128::from_be_bytes(buf)) Ok(i128::from_be_bytes(buf))
} }
/// Peeks a [u8] at a specific offset from the reader. /// Peeks a [u8] at a specific offset from the reader.
fn cpeek_u8_at(&self, offset: usize) -> Result<u8> { fn cpeek_u8_at(&self, offset: u64) -> Result<u8> {
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(buf[0]) Ok(buf[0])
} }
/// Peeks a [u16] at a specific offset from the reader in little-endian order. /// Peeks a [u16] at a specific offset from the reader in little-endian order.
fn cpeek_u16_at(&self, offset: usize) -> Result<u16> { fn cpeek_u16_at(&self, offset: u64) -> Result<u16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u16::from_le_bytes(buf)) Ok(u16::from_le_bytes(buf))
} }
/// Peeks a [u16] at a specific offset from the reader in big-endian order. /// Peeks a [u16] at a specific offset from the reader in big-endian order.
fn cpeek_u16_be_at(&self, offset: usize) -> Result<u16> { fn cpeek_u16_be_at(&self, offset: u64) -> Result<u16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u16::from_be_bytes(buf)) Ok(u16::from_be_bytes(buf))
} }
/// Peeks a [u32] at a specific offset from the reader in little-endian order. /// Peeks a [u32] at a specific offset from the reader in little-endian order.
fn cpeek_u32_at(&self, offset: usize) -> Result<u32> { fn cpeek_u32_at(&self, offset: u64) -> Result<u32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u32::from_le_bytes(buf)) Ok(u32::from_le_bytes(buf))
} }
/// Peeks a [u32] at a specific offset from the reader in big-endian order. /// Peeks a [u32] at a specific offset from the reader in big-endian order.
fn cpeek_u32_be_at(&self, offset: usize) -> Result<u32> { fn cpeek_u32_be_at(&self, offset: u64) -> Result<u32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u32::from_be_bytes(buf)) Ok(u32::from_be_bytes(buf))
} }
/// Peeks a [u64] at a specific offset from the reader in little-endian order. /// Peeks a [u64] at a specific offset from the reader in little-endian order.
fn cpeek_u64_at(&self, offset: usize) -> Result<u64> { fn cpeek_u64_at(&self, offset: u64) -> Result<u64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u64::from_le_bytes(buf)) Ok(u64::from_le_bytes(buf))
} }
/// Peeks a [u64] at a specific offset from the reader in big-endian order. /// Peeks a [u64] at a specific offset from the reader in big-endian order.
fn cpeek_u64_be_at(&self, offset: usize) -> Result<u64> { fn cpeek_u64_be_at(&self, offset: u64) -> Result<u64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u64::from_be_bytes(buf)) Ok(u64::from_be_bytes(buf))
} }
/// Peeks a [u128] at a specific offset from the reader in little-endian order. /// Peeks a [u128] at a specific offset from the reader in little-endian order.
fn cpeek_u128_at(&self, offset: usize) -> Result<u128> { fn cpeek_u128_at(&self, offset: u64) -> Result<u128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u128::from_le_bytes(buf)) Ok(u128::from_le_bytes(buf))
} }
/// Peeks a [u128] at a specific offset from the reader in big-endian order. /// Peeks a [u128] at a specific offset from the reader in big-endian order.
fn cpeek_u128_be_at(&self, offset: usize) -> Result<u128> { fn cpeek_u128_be_at(&self, offset: u64) -> Result<u128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(u128::from_be_bytes(buf)) Ok(u128::from_be_bytes(buf))
} }
/// Peeks an [i8] at a specific offset from the reader. /// Peeks an [i8] at a specific offset from the reader.
fn cpeek_i8_at(&self, offset: usize) -> Result<i8> { fn cpeek_i8_at(&self, offset: u64) -> Result<i8> {
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i8::from_le_bytes(buf)) Ok(i8::from_le_bytes(buf))
} }
/// Peeks an [i16] at a specific offset from the reader in little-endian order. /// Peeks an [i16] at a specific offset from the reader in little-endian order.
fn cpeek_i16_at(&self, offset: usize) -> Result<i16> { fn cpeek_i16_at(&self, offset: u64) -> Result<i16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i16::from_le_bytes(buf)) Ok(i16::from_le_bytes(buf))
} }
/// Peeks an [i16] at a specific offset from the reader in big-endian order. /// Peeks an [i16] at a specific offset from the reader in big-endian order.
fn cpeek_i16_be_at(&self, offset: usize) -> Result<i16> { fn cpeek_i16_be_at(&self, offset: u64) -> Result<i16> {
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i16::from_be_bytes(buf)) Ok(i16::from_be_bytes(buf))
} }
/// Peeks an [i32] at a specific offset from the reader in little-endian order. /// Peeks an [i32] at a specific offset from the reader in little-endian order.
fn cpeek_i32_at(&self, offset: usize) -> Result<i32> { fn cpeek_i32_at(&self, offset: u64) -> Result<i32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i32::from_le_bytes(buf)) Ok(i32::from_le_bytes(buf))
} }
/// Peeks an [i32] at a specific offset from the reader in big-endian order. /// Peeks an [i32] at a specific offset from the reader in big-endian order.
fn cpeek_i32_be_at(&self, offset: usize) -> Result<i32> { fn cpeek_i32_be_at(&self, offset: u64) -> Result<i32> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i32::from_be_bytes(buf)) Ok(i32::from_be_bytes(buf))
} }
/// Peeks an [i64] at a specific offset from the reader in little-endian order. /// Peeks an [i64] at a specific offset from the reader in little-endian order.
fn cpeek_i64_at(&self, offset: usize) -> Result<i64> { fn cpeek_i64_at(&self, offset: u64) -> Result<i64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i64::from_le_bytes(buf)) Ok(i64::from_le_bytes(buf))
} }
/// Peeks an [i64] at a specific offset from the reader in big-endian order. /// Peeks an [i64] at a specific offset from the reader in big-endian order.
fn cpeek_i64_be_at(&self, offset: usize) -> Result<i64> { fn cpeek_i64_be_at(&self, offset: u64) -> Result<i64> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i64::from_be_bytes(buf)) Ok(i64::from_be_bytes(buf))
} }
/// Peeks an [i128] at a specific offset from the reader in little-endian order. /// Peeks an [i128] at a specific offset from the reader in little-endian order.
fn cpeek_i128_at(&self, offset: usize) -> Result<i128> { fn cpeek_i128_at(&self, offset: u64) -> Result<i128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i128::from_le_bytes(buf)) Ok(i128::from_le_bytes(buf))
} }
/// Peeks an [i128] at a specific offset from the reader in big-endian order. /// Peeks an [i128] at a specific offset from the reader in big-endian order.
fn cpeek_i128_be_at(&self, offset: usize) -> Result<i128> { fn cpeek_i128_be_at(&self, offset: u64) -> Result<i128> {
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
Ok(i128::from_be_bytes(buf)) Ok(i128::from_be_bytes(buf))
@@ -651,13 +651,13 @@ pub trait CPeek {
fn cpeek_cstring(&self) -> Result<CString>; fn cpeek_cstring(&self) -> Result<CString>;
/// Peeks a C-style string (null-terminated) from the reader at a specific offset. /// Peeks a C-style string (null-terminated) from the reader at a specific offset.
fn cpeek_cstring_at(&self, offset: usize) -> Result<CString> { fn cpeek_cstring_at(&self, offset: u64) -> Result<CString> {
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut byte = [0u8; 1]; let mut byte = [0u8; 1];
self.cpeek_at(offset, &mut byte)?; self.cpeek_at(offset, &mut byte)?;
while byte[0] != 0 { while byte[0] != 0 {
buf.push(byte[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)) CString::new(buf).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
} }
@@ -675,7 +675,7 @@ pub trait CPeek {
Ok(()) Ok(())
} }
/// Peeks data at a specific offset and checks if it matches the provided data. /// 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()]; let mut buf = vec![0u8; data.len()];
self.cpeek_exact_at(offset, &mut buf)?; self.cpeek_exact_at(offset, &mut buf)?;
if buf != data { if buf != data {
@@ -696,7 +696,7 @@ impl<T: Peek> CPeek for Mutex<T> {
lock.peek(buf) lock.peek(buf)
} }
fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> { fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
let mut lock = self.lock().map_err(|_| { let mut lock = self.lock().map_err(|_| {
std::io::Error::new(std::io::ErrorKind::Other, "Failed to lock the mutex") 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) self.to_ref().cpeek(buf)
} }
fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> { fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
self.to_ref().cpeek_at(offset, buf) self.to_ref().cpeek_at(offset, buf)
} }
@@ -1367,8 +1367,9 @@ impl<'a> CPeek for MemReaderRef<'a> {
Ok(bytes_to_read) Ok(bytes_to_read)
} }
fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> { fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
let len = self.data.len(); let len = self.data.len();
let offset = offset as usize;
if offset >= len { if offset >= len {
return Ok(0); return Ok(0);
} }
@@ -1500,7 +1501,7 @@ impl CPeek for MemWriter {
self.to_ref().cpeek(buf) self.to_ref().cpeek(buf)
} }
fn cpeek_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> { fn cpeek_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
self.to_ref().cpeek_at(offset, buf) self.to_ref().cpeek_at(offset, buf)
} }
@@ -1738,9 +1739,7 @@ impl<R: Read + Seek, W: Write + Seek, A: Fn(u64) -> Result<u64>, O: Fn(u64) -> R
"Original offset is out of bounds for u32 address patching", "Original offset is out of bounds for u32 address patching",
)); ));
} }
self.input.seek(SeekFrom::Start(original_offset))?; let original_address = self.input.peek_u32_at(original_offset)?;
let original_address = self.input.read_u32()?;
self.input.seek(SeekFrom::Start(input_pos))?;
let new_offset = self.map_offset(original_offset)?; let new_offset = self.map_offset(original_offset)?;
let offset = (self.address_to_offset)(original_address as u64)?; let offset = (self.address_to_offset)(original_address as u64)?;
let offset = self.map_offset(offset)?; let offset = self.map_offset(offset)?;

View File

@@ -129,7 +129,7 @@ impl Script for BGIBpScript {
for i in self.strings.iter() { for i in self.strings.iter() {
let text_address = i.offset_pos + i.text_offset as usize - 1; 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); // 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)?; let str = decode_to_string(self.encoding, str.as_bytes(), true)?;
messages.push(Message { messages.push(Message {
name: None, name: None,
@@ -157,7 +157,7 @@ impl Script for BGIBpScript {
let text_address = i.offset_pos + i.text_offset as usize - 1; let text_address = i.offset_pos + i.text_offset as usize - 1;
let old_str_len = self let old_str_len = self
.data .data
.cpeek_cstring_at(text_address)? .cpeek_cstring_at(text_address as u64)?
.as_bytes_with_nul() .as_bytes_with_nul()
.len(); .len();
let mut str = mes.message; let mut str = mes.message;

View File

@@ -451,13 +451,13 @@ impl<'a> V1Parser<'a> {
pub fn is_empty_string(&self, address: usize) -> Result<bool> { pub fn is_empty_string(&self, address: usize) -> Result<bool> {
let start = self.offset + address; 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) Ok(data == 0)
} }
pub fn read_string_at_address(&mut self, address: usize) -> Result<String> { pub fn read_string_at_address(&mut self, address: usize) -> Result<String> {
let start = self.offset + address; 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 // Sometimes string has private use area characters, so we disable strict checking
Ok(decode_to_string(self.encoding, buf.as_bytes(), false)?) Ok(decode_to_string(self.encoding, buf.as_bytes(), false)?)
} }

View File

@@ -129,7 +129,7 @@ impl BGIScript {
fn read_string(&self, offset: usize) -> Result<String> { fn read_string(&self, offset: usize) -> Result<String> {
let start = self.offset + offset; 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 // sometimes string has private use area characters, so we disable strict checking
let string = decode_to_string(self.encoding, string_data.as_bytes(), false)?; let string = decode_to_string(self.encoding, string_data.as_bytes(), false)?;
Ok(string) Ok(string)
@@ -319,7 +319,7 @@ impl Script for BGIScript {
} }
let old_str_len = self let old_str_len = self
.data .data
.cpeek_cstring_at(bgi_str_old_offset)? .cpeek_cstring_at(bgi_str_old_offset as u64)?
.as_bytes_with_nul() .as_bytes_with_nul()
.len(); .len();
let nmess = encode_string(encoding, &nmes, false)?; let nmess = encode_string(encoding, &nmes, false)?;
@@ -382,7 +382,7 @@ impl Script for BGIScript {
} }
let old_str_len = self let old_str_len = self
.data .data
.cpeek_cstring_at(curs.address + self.offset)? .cpeek_cstring_at((curs.address + self.offset) as u64)?
.as_bytes_with_nul() .as_bytes_with_nul()
.len(); .len();
let nmes = match curs.typ { let nmes = match curs.typ {

View File

@@ -151,8 +151,7 @@ impl CstScript {
let string_count = (strings_offset - string_address_offset) / 4; let string_count = (strings_offset - string_address_offset) / 4;
let mut strings = Vec::with_capacity(string_count as usize); let mut strings = Vec::with_capacity(string_count as usize);
for i in 0..string_count { for i in 0..string_count {
let offset = file.cpeek_u32_at(string_address_offset as usize + i as usize * 4)? let offset = file.cpeek_u32_at(string_address_offset as u64 + i as u64 * 4)? as usize
as usize
+ strings_offset as usize; + strings_offset as usize;
file.pos = offset; file.pos = offset;
let start_marker = file.read_u8()?; let start_marker = file.read_u8()?;

View File

@@ -90,22 +90,22 @@ impl Hg3Image {
} }
let mut offset = 0xC; let mut offset = 0xC;
let mut entries = Vec::new(); 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() { while offset + 0x14 < len && reader.cpeek_and_equal_at(offset + 8, b"stdinfo").is_ok() {
let mut section_size = reader.cpeek_u32_at(offset)?; let mut section_size = reader.cpeek_u32_at(offset)?;
if section_size == 0 { 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)?; let stdinfo_size = reader.cpeek_u32_at(offset + 0x10)?;
if reader 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() .is_ok()
{ {
reader.pos = offset + 16; reader.pos = (offset + 16) as usize;
let entry = Hg3Entry::unpack(&mut reader, false, Encoding::Cp932)?; 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() { if entries.is_empty() {
return Err(anyhow::anyhow!("No valid entries found in HG-3 image")); return Err(anyhow::anyhow!("No valid entries found in HG-3 image"));

View File

@@ -229,12 +229,12 @@ impl<T: Read + Seek + std::fmt::Debug> DatArchive<T> {
let index_size = (name_len + 4) * count as usize; let index_size = (name_len + 4) * count as usize;
count -= 1; count -= 1;
let mut entries = Vec::with_capacity(count as usize); 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 { if (next_offset as usize) < index_size + 4 {
return Err(anyhow::anyhow!("Invalid next_offset")); return Err(anyhow::anyhow!("Invalid next_offset"));
} }
let first_size = reader.peek_u32_at(name_len)?; let first_size = reader.peek_u32_at(name_len as u64)?;
let second_offset = reader.peek_u32_at(8 + name_len * 2)?; let second_offset = reader.peek_u32_at(8 + name_len as u64 * 2)?;
if second_offset - next_offset == first_size { if second_offset - next_offset == first_size {
return Err(anyhow::anyhow!("Invalid second_offset")); return Err(anyhow::anyhow!("Invalid second_offset"));
} }
@@ -248,7 +248,7 @@ impl<T: Read + Seek + std::fmt::Debug> DatArchive<T> {
if i + 1 == count { if i + 1 == count {
next_offset = file_len as u32; next_offset = file_len as u32;
} else { } 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 { if next_offset < offset {
return Err(anyhow::anyhow!("Invalid offset in DAT archive")); 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<u8> {
if mcount == 0 { if mcount == 0 {
return Err(anyhow::anyhow!("No entries found in DAT archive")); 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 { if (next_offset as usize) < index_size + 4 {
return Err(anyhow::anyhow!("Invalid next_offset in DAT archive")); return Err(anyhow::anyhow!("Invalid next_offset in DAT archive"));
} }
let first_size = reader.cpeek_u32_at(name_len)?; let first_size = reader.cpeek_u32_at(name_len as u64)?;
let second_offset = reader.cpeek_u32_at(8 + name_len * 2)?; let second_offset = reader.cpeek_u32_at(8 + name_len as u64 * 2)?;
if second_offset - next_offset == first_size { if second_offset - next_offset == first_size {
return Err(anyhow::anyhow!("Invalid second_offset in DAT archive")); 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<u8> {
if i + 1 == mcount { if i + 1 == mcount {
break; break;
} else { } 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 { if next_offset < offset {
return Err(anyhow::anyhow!("Invalid offset in DAT archive")); return Err(anyhow::anyhow!("Invalid offset in DAT archive"));

View File

@@ -552,14 +552,14 @@ impl PcmDecoder {
let mut v1 = a1; let mut v1 = a1;
let mut v5 = 1; let mut v5 = 1;
for _ in 0..0x1000 { 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; v1 += 1;
v5 += 2; v5 += 2;
} }
v5 = 0; v5 = 0;
for _ in 0..0x800 { for _ in 0..0x800 {
let v7 = self.encoded.cpeek_u8_at(v1 + 0x800)?; let v7 = self.encoded.cpeek_u8_at(v1 as u64 + 0x800)?;
let v8 = self.encoded.cpeek_u8_at(v1)?; let v8 = self.encoded.cpeek_u8_at(v1 as u64)?;
self.unk_6a16c8[v5] = (v7 >> 4) | (v8 & 0xF0); self.unk_6a16c8[v5] = (v7 >> 4) | (v8 & 0xF0);
self.unk_6a16c8[v5 + 2] = (v8 << 4) | (v7 & 0x0F); self.unk_6a16c8[v5 + 2] = (v8 << 4) | (v7 & 0x0F);
v5 += 4; v5 += 4;

View File

@@ -249,11 +249,11 @@ impl<T: Read + Seek + std::fmt::Debug + std::any::Any> Script for EscudeBinArchi
let entry = &self.entries[index]; let entry = &self.entries[index];
let name = self let name = self
.reader .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 name = decode_to_string(self.archive_encoding, name.as_bytes(), true)?;
let mut data = self let mut data = self
.reader .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") { if data.starts_with(b"acp") {
let mut decoder = match super::lzw::LZWDecoder::new(&data) { let mut decoder = match super::lzw::LZWDecoder::new(&data) {
Ok(decoder) => decoder, Ok(decoder) => decoder,
@@ -285,7 +285,7 @@ impl<'a, T: Iterator<Item = &'a BinEntry>, R: Read + Seek> Iterator
Some(entry) => entry, Some(entry) => entry,
None => return None, 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) { let name = match self.reader.cpeek_cstring_at(name_offset) {
Ok(name) => name, Ok(name) => name,
Err(e) => return Some(Err(e.into())), Err(e) => return Some(Err(e.into())),

View File

@@ -257,9 +257,9 @@ impl<T: Read + Seek + std::fmt::Debug + std::any::Any> Script for ItufuruArchive
)); ));
} }
let entry = &self.files[index]; 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( 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, entry.size as usize,
) { ) {
Ok(data) => { Ok(data) => {

View File

@@ -131,7 +131,7 @@ impl Script for ItufuruScript {
let mut messages = Vec::new(); let mut messages = Vec::new();
for i in self.strings.iter() { for i in self.strings.iter() {
let str_pos = i.len_pos + 2; // Skip the length bytes 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)?; let decoded = decode_to_string(self.encoding, s.as_bytes(), true)?;
messages.push(Message { messages.push(Message {
name: None, name: None,