Compare commits
3 Commits
ae6c6e371b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 73bf2c538c | |||
| 00638fca6e | |||
| 1c1721b861 |
@@ -1,28 +1,26 @@
|
|||||||
pub fn load_high_u4(raw: u8) -> u8 {
|
pub fn load_high_u4(raw: &u8) -> u8 {
|
||||||
(raw & 0xF0) >> 4
|
(raw & 0xF0) >> 4
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_low_u4(raw: u8) -> u8 {
|
pub fn load_low_u4(raw: &u8) -> u8 {
|
||||||
raw & 0x0F
|
raw & 0x0F
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_u8(raw: u8) -> u8 {
|
pub fn load_u8(raw: &u8) -> u8 {
|
||||||
raw
|
raw & 0xFF
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_u16(raw: [u16; 2]) -> u16 {
|
pub fn load_u16(raw: &[u8]) -> u16 {
|
||||||
let mut value: u16 = 0;
|
|
||||||
let temp: u16 = raw[0].into();
|
let temp: u16 = raw[0].into();
|
||||||
value = temp << 8;
|
let mut value: u16 = temp << 8;
|
||||||
let temp: u16 = raw[1].into();
|
let temp: u16 = raw[1].into();
|
||||||
value |= temp;
|
value |= temp;
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_u32(raw: [u8; 4]) -> u32 {
|
pub fn load_u32(raw: &[u8]) -> u32 {
|
||||||
let mut value: u32 = 0;
|
|
||||||
let temp: u32 = raw[0].into();
|
let temp: u32 = raw[0].into();
|
||||||
value = temp << 24;
|
let mut value: u32 = temp << 24;
|
||||||
let temp: u32 = raw[1].into();
|
let temp: u32 = raw[1].into();
|
||||||
value |= temp << 16;
|
value |= temp << 16;
|
||||||
let temp: u32 = raw[2].into();
|
let temp: u32 = raw[2].into();
|
||||||
@@ -38,56 +36,59 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_0() {
|
fn load_0() {
|
||||||
assert_eq!(0, load_u32([0, 0, 0, 0]));
|
assert_eq!(0, load_u32(&[0, 0, 0, 0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_1() {
|
fn load_1() {
|
||||||
assert_eq!(1, load_u32([0, 0, 0, 1]));
|
assert_eq!(1, load_u32(&[0, 0, 0, 1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_big() {
|
fn load_big() {
|
||||||
assert_eq!(0x01000000, load_u32([1, 0, 0, 0]));
|
let my_arr = [1, 0, 0, 2, 1, 1];
|
||||||
|
let slice = &my_arr[0..4];
|
||||||
|
assert_eq!(0x01000002, load_u32(&slice));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_load_low_u4() {
|
fn test_load_low_u4() {
|
||||||
assert_eq!(0x07, load_low_u4(0x07));
|
assert_eq!(0x07, load_low_u4(&0x07));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_low_u4_with_high_data() {
|
fn load_low_u4_with_high_data() {
|
||||||
assert_eq!(0x03, load_low_u4(0x13));
|
assert_eq!(0x03, load_low_u4(&0x13));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_low_u4_with_only_high_data() {
|
fn load_low_u4_with_only_high_data() {
|
||||||
assert_eq!(0x00, load_low_u4(0x30));
|
assert_eq!(0x00, load_low_u4(&0x30));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_load_high_u4() {
|
fn test_load_high_u4() {
|
||||||
assert_eq!(0x07, load_high_u4(0x70));
|
assert_eq!(0x07, load_high_u4(&0x70));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_high_u4_with_low_data() {
|
fn load_high_u4_with_low_data() {
|
||||||
assert_eq!(0x01, load_high_u4(0x13));
|
assert_eq!(0x01, load_high_u4(&0x13));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_high_u4_with_only_low_data() {
|
fn load_high_u4_with_only_low_data() {
|
||||||
assert_eq!(0x00, load_high_u4(0x03));
|
assert_eq!(0x00, load_high_u4(&0x03));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_load_u8() {
|
fn test_load_u8() {
|
||||||
assert_eq!(0x43, load_u8(0x43));
|
assert_eq!(0x43, load_u8(&0x43));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_load_u16() {
|
fn test_load_u16() {
|
||||||
assert_eq!(0x3423, load_u16([0x34, 0x23]));
|
let my_arr = [0x34, 0x23];
|
||||||
|
assert_eq!(0x3423, load_u16(&my_arr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use binary_loader;
|
||||||
|
|
||||||
struct DBHeader {
|
struct DBHeader {
|
||||||
magic: u8,
|
magic: u8,
|
||||||
major_version: u8, // actually u4 in data
|
major_version: u8, // actually u4 in data
|
||||||
@@ -7,22 +9,13 @@ struct DBHeader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DBHeader {
|
impl DBHeader {
|
||||||
fn from(raw: [u8; 8]) -> DBHeader {
|
fn from(raw: &[u8]) -> 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 {
|
DBHeader {
|
||||||
magic: raw[0],
|
magic: binary_loader::load_u8(&raw[0]),
|
||||||
major_version: (raw[1] | 0xF0) >> 4,
|
major_version: binary_loader::load_high_u4(&raw[1]),
|
||||||
minor_version: (raw[1] | 0x0F),
|
minor_version: binary_loader::load_low_u4(&raw[1]),
|
||||||
reserved: reserved,
|
reserved: binary_loader::load_u16(&raw[2..4]),
|
||||||
record_count: record_count,
|
record_count: binary_loader::load_u32(&raw[4..8]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,13 +26,41 @@ struct RecordHeader {
|
|||||||
crc: u16,
|
crc: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RecordHeader {
|
||||||
|
fn from(raw: &[u8]) -> RecordHeader {
|
||||||
|
RecordHeader {
|
||||||
|
field_count: binary_loader::load_u8(&raw[0]),
|
||||||
|
length: binary_loader::load_u8(&raw[1]),
|
||||||
|
crc: binary_loader::load_u16(&raw[2..4]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct Field {
|
struct Field {
|
||||||
field_type: u8,
|
field_type: u8,
|
||||||
value: String,
|
length: u16,
|
||||||
|
value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Field {
|
||||||
|
fn from(raw: &[u8]) -> Field {
|
||||||
|
let length = binary_loader::load_u16(&raw[1..3]);
|
||||||
|
let mut lsize: usize = length.into();
|
||||||
|
lsize += 3;
|
||||||
|
let value = String::from_utf8_lossy(&raw[3..lsize]).to_string();
|
||||||
|
println!("{}", value);
|
||||||
|
println!("{}", lsize);
|
||||||
|
Field {
|
||||||
|
field_type: binary_loader::load_u8(&raw[0]),
|
||||||
|
length: length,
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn it_works() {
|
||||||
assert_eq!(2 + 2, 4);
|
assert_eq!(2 + 2, 4);
|
||||||
@@ -48,7 +69,30 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn load_empty_db_header() {
|
fn load_empty_db_header() {
|
||||||
let header = [0xDB, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
let header = [0xDB, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
let db_header = DBHeader::from(header);
|
let db_header = DBHeader::from(&header);
|
||||||
assert_eq!(db_header.magic, 0xDB);
|
assert_eq!(db_header.magic, 0xDB);
|
||||||
|
assert_eq!(db_header.major_version, 0);
|
||||||
|
assert_eq!(db_header.minor_version, 1);
|
||||||
|
assert_eq!(db_header.reserved, 0);
|
||||||
|
assert_eq!(db_header.record_count, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn load_record_header() {
|
||||||
|
let header: [u8; 4] = [0x3, 0x45, 0x12, 0x34];
|
||||||
|
let record_header = RecordHeader::from(&header);
|
||||||
|
assert_eq!(record_header.field_count, 3);
|
||||||
|
assert_eq!(record_header.length, 0x45);
|
||||||
|
assert_eq!(record_header.crc, 0x1234);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn load_field() {
|
||||||
|
let field: [u8; 6] = [0x01, 0x00, 0x03, 0x4a, 0x6f, 0x6e];
|
||||||
|
let field = Field::from(&field);
|
||||||
|
assert_eq!(0x01, field.field_type);
|
||||||
|
assert_eq!(3, field.length);
|
||||||
|
assert_eq!(String::from("Jon"), field.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user