Created binary_loader
This commit is contained in:
9
binary_loader/Cargo.toml
Normal file
9
binary_loader/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "binary_loader"
|
||||
version = "0.1.0"
|
||||
authors = ["Jon Janzen <jon@endofeternity.ca>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
79
binary_loader/src/lib.rs
Normal file
79
binary_loader/src/lib.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
pub fn load_high_u4(raw: u8) -> u8 {
|
||||
(raw & 0xF0) >> 4
|
||||
}
|
||||
|
||||
pub fn load_low_u4(raw: u8) -> u8 {
|
||||
raw & 0x0F
|
||||
}
|
||||
|
||||
pub fn load_u8(raw: u8) -> u8 {
|
||||
raw
|
||||
}
|
||||
|
||||
pub fn load_u32(raw: [u8; 4]) -> u32 {
|
||||
let mut value: u32 = 0;
|
||||
let temp: u32 = raw[0].into();
|
||||
value = temp << 24;
|
||||
let temp: u32 = raw[1].into();
|
||||
value |= temp << 16;
|
||||
let temp: u32 = raw[2].into();
|
||||
value |= temp << 8;
|
||||
let temp: u32 = raw[3].into();
|
||||
value |= temp;
|
||||
value
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn load_0() {
|
||||
assert_eq!(0, load_u32([0, 0, 0, 0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_1() {
|
||||
assert_eq!(1, load_u32([0, 0, 0, 1]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_big() {
|
||||
assert_eq!(0x01000000, load_u32([1, 0, 0, 0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_low_u4() {
|
||||
assert_eq!(0x07, load_low_u4(0x07));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_low_u4_with_high_data() {
|
||||
assert_eq!(0x03, load_low_u4(0x13));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_low_u4_with_only_high_data() {
|
||||
assert_eq!(0x00, load_low_u4(0x30));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_high_u4() {
|
||||
assert_eq!(0x07, load_high_u4(0x70));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_high_u4_with_low_data() {
|
||||
assert_eq!(0x01, load_high_u4(0x13));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_high_u4_with_only_low_data() {
|
||||
assert_eq!(0x00, load_high_u4(0x03));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_u8() {
|
||||
assert_eq!(0x43, load_u8(0x43));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user