X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fio%2Fbyteio.rs;h=286a1177a55f40d63bff4e8972f3912a5ee57827;hb=1678d59a37c619e7a3a2604f4bd23bb10c1769f6;hp=966169cdefa00756767c79a500cd64ef70499064;hpb=5641dccfbf2a70d589cf094a0d4ed5a10f919f00;p=nihav.git diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index 966169c..286a117 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -65,6 +65,33 @@ macro_rules! peek_int { }) } +macro_rules! read_int_func { + ($s: ident, $inttype: ty, $size: expr, $which: ident) => { + pub fn $s(src: &[u8]) -> ByteIOResult<$inttype> { + if src.len() < $size { return Err(ByteIOError::ReadError); } + unsafe { + Ok((*(src.as_ptr() as *const $inttype)).$which()) + } + } + } +} + +read_int_func!(read_u16be, u16, 2, to_be); +read_int_func!(read_u16le, u16, 2, to_le); +read_int_func!(read_u32be, u32, 4, to_be); +read_int_func!(read_u32le, u32, 4, to_le); +read_int_func!(read_u64be, u64, 8, to_be); +read_int_func!(read_u64le, u64, 8, to_le); + +pub fn read_u24be(src: &[u8]) -> ByteIOResult { + if src.len() < 3 { return Err(ByteIOError::ReadError); } + Ok(((src[0] as u32) << 16) | ((src[1] as u32) << 8) | (src[2] as u32)) +} +pub fn read_u24le(src: &[u8]) -> ByteIOResult { + if src.len() < 3 { return Err(ByteIOError::ReadError); } + Ok(((src[2] as u32) << 16) | ((src[1] as u32) << 8) | (src[0] as u32)) +} + impl<'a> ByteReader<'a> { pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io: io } } @@ -599,7 +626,7 @@ mod test { assert_eq!(reader.read_u24le().unwrap(), 0x010101u32); assert_eq!(reader.read_u32le().unwrap(), 0x01010101u32); assert_eq!(reader.read_u64le().unwrap(), 0x0101010101010101u64); - let mut file = File::open("assets/MaoMacha.asx").unwrap(); + let mut file = File::open("assets/Misc/MaoMacha.asx").unwrap(); let mut fr = FileReader::new_read(&mut file); let mut br2 = ByteReader::new(&mut fr); assert_eq!(br2.read_byte().unwrap(), 0x30);