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
use endianity::{Endianity, EndianBuf};
use parser::{parse_null_terminated_string, Error, Result};
use std::ffi;
use std::marker::PhantomData;
use Section;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DebugStrOffset(pub usize);
#[derive(Debug, Clone, Copy)]
pub struct DebugStr<'input, Endian>
where Endian: Endianity
{
debug_str_section: EndianBuf<'input, Endian>,
}
impl<'input, Endian> DebugStr<'input, Endian>
where Endian: Endianity
{
pub fn new(debug_str_section: &'input [u8]) -> DebugStr<'input, Endian> {
DebugStr { debug_str_section: EndianBuf(debug_str_section, PhantomData) }
}
pub fn get_str(&self, offset: DebugStrOffset) -> Result<&'input ffi::CStr> {
if self.debug_str_section.len() < offset.0 {
return Err(Error::UnexpectedEof);
}
let buf = self.debug_str_section.range_from(offset.0..);
let result = parse_null_terminated_string(buf.0);
result.map(|(_, cstr)| cstr)
}
}
impl<'input, Endian> Section<'input> for DebugStr<'input, Endian>
where Endian: Endianity
{
fn section_name() -> &'static str {
".debug_str"
}
}
impl<'input, Endian> From<&'input [u8]> for DebugStr<'input, Endian>
where Endian: Endianity
{
fn from(v: &'input [u8]) -> Self {
Self::new(v)
}
}