Compare commits

...

3 Commits

Author SHA1 Message Date
ae6c6e371b added load_u16 2020-11-10 15:22:26 -07:00
097d6a973c Created binary_loader 2020-11-10 15:17:08 -07:00
4650e84111 created db crate 2020-11-10 10:56:31 -07:00
5 changed files with 167 additions and 0 deletions

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]
db = { path = "db" }

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]

93
binary_loader/src/lib.rs Normal file
View File

@@ -0,0 +1,93 @@
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_u16(raw: [u16; 2]) -> u16 {
let mut value: u16 = 0;
let temp: u16 = raw[0].into();
value = temp << 8;
let temp: u16 = raw[1].into();
value |= temp;
value
}
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));
}
#[test]
fn test_load_u16() {
assert_eq!(0x3423, load_u16([0x34, 0x23]));
}
}

10
db/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "db"
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]
binary_loader = { path = "../binary_loader" }

54
db/src/lib.rs Normal file
View File

@@ -0,0 +1,54 @@
struct DBHeader {
magic: u8,
major_version: u8, // actually u4 in data
minor_version: u8, // actually u4 in data
reserved: u16,
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,
crc: u16,
}
struct Field {
field_type: u8,
value: String,
}
#[cfg(test)]
mod tests {
#[test]
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);
}
}