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
163
164
165
166
167
168
use core::fmt;
use core::ops::Index;
use core::slice;
use core::str;
use scroll::{ctx, Pread};
if_alloc! {
use crate::error;
use alloc::vec::Vec;
}
pub struct Strtab<'a> {
bytes: &'a [u8],
delim: ctx::StrCtx,
}
#[inline(always)]
fn get_str(offset: usize, bytes: &[u8], delim: ctx::StrCtx) -> scroll::Result<&str> {
bytes.pread_with::<&str>(offset, delim)
}
impl<'a> Strtab<'a> {
pub fn new(bytes: &'a [u8], delim: u8) -> Self {
Strtab {
delim: ctx::StrCtx::Delimiter(delim),
bytes,
}
}
pub unsafe fn from_raw(ptr: *const u8, size: usize, delim: u8) -> Strtab<'a> {
Strtab {
delim: ctx::StrCtx::Delimiter(delim),
bytes: slice::from_raw_parts(ptr, size),
}
}
#[cfg(feature = "alloc")]
pub fn parse(
bytes: &'a [u8],
offset: usize,
len: usize,
delim: u8,
) -> error::Result<Strtab<'a>> {
let (end, overflow) = offset.overflowing_add(len);
if overflow || end > bytes.len() {
return Err(error::Error::Malformed(format!(
"Strtable size ({}) + offset ({}) is out of bounds for {} #bytes. Overflowed: {}",
len,
offset,
bytes.len(),
overflow
)));
}
Ok(Strtab {
bytes: &bytes[offset..end],
delim: ctx::StrCtx::Delimiter(delim),
})
}
#[cfg(feature = "alloc")]
pub fn to_vec(&self) -> error::Result<Vec<&'a str>> {
let len = self.bytes.len();
let mut strings = Vec::with_capacity(len);
let mut i = 0;
while i < len {
let string = self.get(i).unwrap()?;
i = i + string.len() + 1;
strings.push(string);
}
Ok(strings)
}
#[cfg(feature = "alloc")]
pub fn get(&self, offset: usize) -> Option<error::Result<&'a str>> {
if offset >= self.bytes.len() {
None
} else {
Some(get_str(offset, self.bytes, self.delim).map_err(core::convert::Into::into))
}
}
pub fn get_unsafe(&self, offset: usize) -> Option<&'a str> {
if offset >= self.bytes.len() {
None
} else {
Some(get_str(offset, self.bytes, self.delim).unwrap())
}
}
}
impl<'a> fmt::Debug for Strtab<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Strtab")
.field("delim", &self.delim)
.field("bytes", &str::from_utf8(self.bytes))
.finish()
}
}
impl<'a> Default for Strtab<'a> {
fn default() -> Strtab<'a> {
Strtab {
bytes: &[],
delim: ctx::StrCtx::default(),
}
}
}
impl<'a> Index<usize> for Strtab<'a> {
type Output = str;
#[inline(always)]
fn index(&self, offset: usize) -> &Self::Output {
get_str(offset, self.bytes, self.delim).unwrap()
}
}
#[test]
fn as_vec_no_final_null() {
let bytes = b"\0printf\0memmove\0busta";
let strtab = unsafe { Strtab::from_raw(bytes.as_ptr(), bytes.len(), 0x0) };
let vec = strtab.to_vec().unwrap();
assert_eq!(vec.len(), 4);
assert_eq!(vec, vec!["", "printf", "memmove", "busta"]);
}
#[test]
fn as_vec_no_first_null_no_final_null() {
let bytes = b"printf\0memmove\0busta";
let strtab = unsafe { Strtab::from_raw(bytes.as_ptr(), bytes.len(), 0x0) };
let vec = strtab.to_vec().unwrap();
assert_eq!(vec.len(), 3);
assert_eq!(vec, vec!["printf", "memmove", "busta"]);
}
#[test]
fn to_vec_final_null() {
let bytes = b"\0printf\0memmove\0busta\0";
let strtab = unsafe { Strtab::from_raw(bytes.as_ptr(), bytes.len(), 0x0) };
let vec = strtab.to_vec().unwrap();
assert_eq!(vec.len(), 4);
assert_eq!(vec, vec!["", "printf", "memmove", "busta"]);
}
#[test]
fn to_vec_newline_delim() {
let bytes = b"\nprintf\nmemmove\nbusta\n";
let strtab = unsafe { Strtab::from_raw(bytes.as_ptr(), bytes.len(), b'\n') };
let vec = strtab.to_vec().unwrap();
assert_eq!(vec.len(), 4);
assert_eq!(vec, vec!["", "printf", "memmove", "busta"]);
}