datex_core/parser/
header.rs1use crate::{utils::{buffers::{read_u8, read_u16, read_slice, read_u32, read_u64}}, global::dxb_block::{DXBHeader, RoutingInfo, HeaderFlags, DXBBlockType}, datex_values::Endpoint};
2
3
4pub fn has_dxb_magic_number(dxb:&[u8]) -> bool {
6 dxb.len() >= 2 && dxb[0] == 0x01 && dxb[1] == 0x64
7}
8
9pub fn parse_dxb_header<'a>(dxb:&'a [u8]) -> (DXBHeader, &'a [u8]) {
10 if !has_dxb_magic_number(dxb) {
12 panic!("Invalid DXB header format - missing magic number");
13 }
14 if dxb.len() < 28 {
16 panic!("Invalid DXB header format - too short");
17 }
18
19 let index = &mut 2;
20
21 let version = read_u8(dxb, index);
23 let size = read_u16(dxb, index);
24 let ttl = read_u8(dxb, index);
25 let priority = read_u8(dxb, index);
26 let signed_encrypted = read_u8(dxb, index);
27 let signed = signed_encrypted == 1 || signed_encrypted == 2; let encrypted = signed_encrypted == 2 || signed_encrypted == 3; let sender = get_dxb_header_sender(dxb, index);
30 let _receivers = get_dxb_header_receivers(dxb, index);
31
32 let scope_id = read_u32(dxb, index);
34 let block_index = read_u16(dxb, index);
35 let block_increment = read_u16(dxb, index);
36 let block_type = DXBBlockType::try_from(read_u8(dxb, index)).expect("Invalid DXB block type");
37 let _flags = read_u8(dxb, index); let timestamp = read_u64(dxb, index);
39
40
41 let header = DXBHeader {
42 version,
43 size,
44 signed,
45 encrypted,
46
47 scope_id,
48 block_index,
49 block_increment,
50 block_type,
51 timestamp,
52
53 flags: HeaderFlags {allow_execute:true,end_of_scope:true,device_type:0},
54 routing: RoutingInfo {ttl, sender, priority}
55 };
56
57 return (header, dxb.clone());
58}
59
60
61fn get_dxb_header_sender(dxb:&[u8], index: &mut usize) -> Option<Endpoint> {
62
63 if read_u8(dxb, index) == std::u8::MAX {
64 return None;
65 }
66 else {
67 *index -= 1;
68 return Some(Endpoint::new_from_binary(&read_slice(dxb, index, 21)))
69 }
70}
71
72
73fn get_dxb_header_receivers(dxb:&[u8], index: &mut usize) -> Option<Endpoint> {
74 if read_u16(dxb, index) == std::u16::MAX {
75 return None;
76 }
77 else {
78 return None;
80 }
81}