Created binary_loader

This commit is contained in:
2020-11-10 15:17:08 -07:00
parent 4650e84111
commit 097d6a973c
4 changed files with 117 additions and 0 deletions

9
binary_loader/Cargo.toml Normal file
View 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
View 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));
}
}

View File

@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
binary_loader = { path = "../binary_loader" }

View File

@@ -6,6 +6,27 @@ struct DBHeader {
record_count: u32,
}
impl DBHeader {
fn from(raw: [u8; 8]) -> DBHeader {
let mut reserved : u16 = 0x0000;
reserved = raw[2].into();
reserved <<= 8;
let temp : u16 = raw[3].into();
reserved |= temp;
let mut record_count : u32 = 0x00000000;
DBHeader {
magic: raw[0],
major_version: (raw[1] | 0xF0) >> 4,
minor_version: (raw[1] | 0x0F),
reserved: reserved,
record_count: record_count,
}
}
}
struct RecordHeader {
field_count: u8,
length: u8,
@@ -23,4 +44,11 @@ mod tests {
fn it_works() {
assert_eq!(2 + 2, 4);
}
#[test]
fn load_empty_db_header() {
let header = [0xDB, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let db_header = DBHeader::from(header);
assert_eq!(db_header.magic, 0xDB);
}
}