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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use byteorder::ByteOrder;
use core::mem::{size_of, MaybeUninit};
use core::slice;
#[derive(Debug)]
pub struct CursorWrite<'a> {
data: &'a mut [u8],
pos: usize
}
impl<'a> CursorWrite<'a> {
pub fn new(data: &mut [u8]) -> CursorWrite<'_> {
CursorWrite {
data: data,
pos: 0
}
}
pub fn pos(&self) -> usize {
self.pos
}
pub fn skip_write(&mut self, bytelen: usize) -> &mut [u8] {
let ret = &mut self.data[self.pos..self.pos + bytelen];
self.pos += bytelen;
ret
}
pub fn write_u8<BO: ByteOrder>(&mut self, v: u8) {
self.data[self.pos] = v;
self.pos += 1;
}
pub fn write_u16<BO: ByteOrder>(&mut self, v: u16) {
BO::write_u16(&mut self.data[self.pos..], v);
self.pos += 2;
}
pub fn write_u32<BO: ByteOrder>(&mut self, v: u32) {
BO::write_u32(&mut self.data[self.pos..], v);
self.pos += 4;
}
pub fn write_u64<BO: ByteOrder>(&mut self, v: u64) {
BO::write_u64(&mut self.data[self.pos..], v);
self.pos += 8;
}
pub fn write(&mut self, v: &[u8]) {
self.data[self.pos..self.pos + v.len()].copy_from_slice(v);
self.pos += v.len();
}
pub fn write_raw<T: Copy>(&mut self, v: T) {
let ptr = &v;
let arr = unsafe {
slice::from_raw_parts(ptr as *const T as *const u8, size_of::<T>())
};
self.skip_write(size_of::<T>()).copy_from_slice(arr);
}
}
#[derive(Debug)]
pub struct CursorRead<'a> {
data: &'a [u8],
pos: ::core::cell::Cell<usize>
}
impl<'a> CursorRead<'a> {
pub fn new(data: &[u8]) -> CursorRead<'_> {
CursorRead {
data: data,
pos: 0.into()
}
}
pub fn pos(&self) -> usize {
self.pos.get()
}
pub fn read_u8<BO: ByteOrder>(&self) -> u8 {
let ret = self.data[self.pos.get()];
self.pos.set(self.pos.get() + 1);
ret
}
pub fn read_u16<BO: ByteOrder>(&self) -> u16 {
let ret = BO::read_u16(&self.data[self.pos.get()..]);
self.pos.set(self.pos.get() + 2);
ret
}
pub fn read_u32<BO: ByteOrder>(&self) -> u32 {
let ret = BO::read_u32(&self.data[self.pos.get()..]);
self.pos.set(self.pos.get() + 4);
ret
}
pub fn read_u64<BO: ByteOrder>(&self) -> u64 {
let ret = BO::read_u64(&self.data[self.pos.get()..]);
self.pos.set(self.pos.get() + 8);
ret
}
pub fn assert(&self, v: &[u8]) {
assert_eq!(&self.data[self.pos.get()..self.pos.get() + v.len()], v);
self.pos.set(self.pos.get() + v.len());
}
pub fn skip_read(&self, bytelen: usize) -> &[u8] {
let ret = &self.data[self.pos.get()..self.pos.get() + bytelen];
self.pos.set(self.pos.get() + bytelen);
ret
}
pub fn read_raw<T: Copy>(&self) -> T {
unsafe {
let mut v: MaybeUninit<T> = MaybeUninit::uninit();
core::ptr::copy(self.skip_read(size_of::<T>()).as_ptr(), v.as_mut_ptr() as *mut u8, size_of::<T>());
v.assume_init()
}
}
}