1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::context::{
self,
Endianness::{Big, Little},
};
use crate::Bits;
use byteorder::{BigEndian, ByteOrder, LittleEndian};
pub fn p8(v: u8) -> Vec<u8> {
vec![v]
}
pub fn p16(v: u16) -> Vec<u8> {
let mut res: Vec<u8> = std::iter::repeat(0).take(2).collect();
match context::get_endianess() {
Big => BigEndian::write_u16(&mut res, v),
Little => LittleEndian::write_u16(&mut res, v),
};
res
}
pub fn p32(v: u32) -> Vec<u8> {
let mut res: Vec<u8> = std::iter::repeat(0).take(4).collect();
match context::get_endianess() {
Big => BigEndian::write_u32(&mut res, v),
Little => LittleEndian::write_u32(&mut res, v),
};
res
}
pub fn p64(v: u64) -> Vec<u8> {
let mut res: Vec<u8> = std::iter::repeat(0).take(8).collect();
match context::get_endianess() {
Big => BigEndian::write_u64(&mut res, v),
Little => LittleEndian::write_u64(&mut res, v),
};
res
}
pub fn u8(v: &[u8]) -> u8 {
v[0]
}
pub fn u16(v: &[u8]) -> u16 {
match context::get_endianess() {
Big => BigEndian::read_u16(v),
Little => LittleEndian::read_u16(v),
}
}
pub fn u32(v: &[u8]) -> u32 {
match context::get_endianess() {
Big => BigEndian::read_u32(v),
Little => LittleEndian::read_u32(v),
}
}
pub fn u64(v: &[u8]) -> u64 {
match context::get_endianess() {
Big => BigEndian::read_u64(v),
Little => LittleEndian::read_u64(v),
}
}
pub fn pack<T>(v: T) -> Option<Vec<u8>>
where
T: num_traits::ToPrimitive,
{
match context::get_bits() {
Bits::Eight => Some(p8(v.to_u8()?)),
Bits::Sixteen => Some(p16(v.to_u16()?)),
Bits::ThirtyTwo => Some(p32(v.to_u32()?)),
Bits::SixtyFour => Some(p64(v.to_u64()?)),
}
}
pub fn unpack(v: &[u8]) -> u64 {
match context::get_bits() {
Bits::Eight => u8(v) as u64,
Bits::Sixteen => u16(v) as u64,
Bits::ThirtyTwo => u32(v) as u64,
Bits::SixtyFour => u64(v),
}
}