aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-core/src
diff options
context:
space:
mode:
authorKostya Shishkov <kostya.shishkov@gmail.com>2019-02-15 15:04:46 +0100
committerKostya Shishkov <kostya.shishkov@gmail.com>2019-02-15 15:04:46 +0100
commit95058bc1b14392a24f3ab760ee34b494d61318d8 (patch)
tree3f4d0026b9cb4c00be510e9c0437f8a809507530 /nihav-core/src
parentb70cc0062fd4d791ee20f23439a3b08a04921835 (diff)
downloadnihav-95058bc1b14392a24f3ab760ee34b494d61318d8.tar.gz
byteio: make standalone read_uXX{be,le} functions
Diffstat (limited to 'nihav-core/src')
-rw-r--r--nihav-core/src/io/byteio.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs
index 966169c..880e89b 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<u32> {
+ 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<u32> {
+ 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 } }