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
#[cfg(not(feature = "std"))]
#[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))]
use alloc::string::String;
#[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))]
use core::str;
pub(crate) struct DataReader<'a> {
data: &'a [u8],
index: usize,
}
impl<'a> DataReader<'a> {
pub(crate) fn new(data: &'a [u8]) -> Self {
Self { data, index: 0 }
}
#[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))]
pub(crate) fn index(&self) -> usize {
self.index
}
#[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))]
pub(crate) fn set_index(&mut self, index: usize) {
self.index = index
}
pub(crate) fn can_read(&self) -> bool {
self.index < self.data.len()
}
pub(crate) fn read_u8(&mut self) -> usize {
let b = self.data[self.index] as usize;
self.index += 1;
b
}
pub(crate) fn read_compressed_u32(&mut self) -> u32 {
let mut result = 0;
let mut shift = 0;
loop {
if shift >= 32 {
panic!();
}
let b = self.read_u8() as u32;
if (b & 0x80) == 0 {
return result | (b << shift);
}
result |= (b & 0x7F) << shift;
shift += 7;
}
}
#[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))]
pub(crate) fn read_ascii_string(&mut self) -> String {
let len = self.read_u8();
let s = str::from_utf8(&self.data[self.index..self.index + len]).unwrap();
self.index += len;
String::from(s)
}
}