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
use std::str::Utf8Error;
use llvm_constants::{IrBlockId, StrtabCode};
use llvm_support::StrtabRef;
use thiserror::Error;
use crate::block::{BlockMapError, IrBlock};
use crate::map::MapCtx;
use crate::unroll::UnrolledBlock;
#[derive(Debug, Error)]
pub enum StrtabError {
#[error("requested range in string table is invalid")]
BadRange,
#[error("could not decode range into a UTF-8 string: {0}")]
BadString(#[from] Utf8Error),
}
#[derive(Clone, Debug)]
pub struct Strtab(Vec<u8>);
impl AsRef<[u8]> for Strtab {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl IrBlock for Strtab {
const BLOCK_ID: IrBlockId = IrBlockId::Strtab;
fn try_map_inner(block: &UnrolledBlock, _ctx: &mut MapCtx) -> Result<Self, BlockMapError> {
let strtab = {
let strtab = block.one_record(StrtabCode::Blob as u64)?;
strtab.try_blob(0)?
};
Ok(Self(strtab))
}
}
impl Strtab {
pub fn get(&self, sref: &StrtabRef) -> Option<&str> {
self.try_get(sref).ok()
}
pub fn try_get(&self, sref: &StrtabRef) -> Result<&str, StrtabError> {
let inner = self.as_ref();
if sref.size == 0 || sref.offset >= inner.len() || sref.offset + sref.size > inner.len() {
return Err(StrtabError::BadRange);
}
Ok(std::str::from_utf8(
&inner[sref.offset..sref.offset + sref.size],
)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sref(tup: (usize, usize)) -> StrtabRef {
tup.into()
}
#[test]
fn test_strtab() {
let inner = "this is a string table";
let strtab = Strtab(inner.into());
assert_eq!(strtab.get(&sref((0, 4))).unwrap(), "this");
assert_eq!(strtab.get(&sref((0, 7))).unwrap(), "this is");
assert_eq!(strtab.get(&sref((8, 14))).unwrap(), "a string table");
assert_eq!(
strtab.get(&sref((0, inner.len()))).unwrap(),
"this is a string table"
);
assert!(strtab.get(&sref((inner.len(), 0))).is_none());
assert!(strtab.get(&sref((0, inner.len() + 1))).is_none());
assert!(strtab.get(&sref((0, 0))).is_none());
}
}