-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use core::{mem, ptr}; | ||
|
||
macro_rules! read_single { | ||
($src:expr, $size:expr, $ty:ty, $which:ident) => ({ | ||
assert!($size == mem::size_of::<$ty>()); | ||
assert!($size == $src.len()); | ||
unsafe { | ||
let mut tmp: $ty = mem::uninitialized(); | ||
let p = &mut tmp as *mut _ as *mut u8; | ||
ptr::copy_nonoverlapping($src.as_ptr(), p, $size); | ||
tmp.$which() | ||
} | ||
}); | ||
} | ||
|
||
/// Read the value of a vector of bytes as a u32 value in little-endian format. | ||
#[inline] | ||
pub fn read_u32_le(src: &[u8]) -> u32 { | ||
read_single!(src, 4, u32, to_le) | ||
} | ||
|
||
/// Read the value of a vector of bytes as a u32 value in big-endian format. | ||
#[inline] | ||
pub fn read_u32_be(src: &[u8]) -> u32 { | ||
read_single!(src, 4, u32, to_be) | ||
} | ||
|
||
/// Read the value of a vector of bytes as a u64 value in little-endian format. | ||
#[inline] | ||
pub fn read_u64_le(src: &[u8]) -> u64 { | ||
read_single!(src, 8, u64, to_le) | ||
} | ||
|
||
/// Read the value of a vector of bytes as a u64 value in big-endian format. | ||
#[inline] | ||
pub fn read_u64_be(src: &[u8]) -> u64 { | ||
read_single!(src, 8, u64, to_be) | ||
} |
Oops, something went wrong.