pub struct Strtab<'a> { /* private fields */ }
Expand description
A common string table format which is indexed by byte offsets (and not
member index). Constructed using parse
with your choice of delimiter. Please be careful.
Implementations§
Source§impl<'a> Strtab<'a>
impl<'a> Strtab<'a>
Sourcepub fn new(bytes: &'a [u8], delim: u8) -> Self
pub fn new(bytes: &'a [u8], delim: u8) -> Self
Creates a Strtab
with bytes
as the backing string table, using delim
as the delimiter between entries.
NB: this does not preparse the string table, which can have non-optimal access patterns. See https://github.com/m4b/goblin/pull/275#issue-660364025
Sourcepub fn from_slice_unparsed(
bytes: &'a [u8],
offset: usize,
len: usize,
delim: u8,
) -> Self
pub fn from_slice_unparsed( bytes: &'a [u8], offset: usize, len: usize, delim: u8, ) -> Self
Creates a Strtab
directly without bounds check and without parsing it.
This is potentially unsafe and should only be used if feature = "alloc"
is disabled.
Sourcepub fn get_unsafe(&self, offset: usize) -> Option<&'a str>
pub fn get_unsafe(&self, offset: usize) -> Option<&'a str>
Gets a str reference from the backing bytes starting at byte offset
.
If the index is out of bounds, None
is returned. Panics if bytes are invalid UTF-8.
Use this method if the Strtab
was created using from_slice_unparsed()
.
Sourcepub fn parse(
bytes: &'a [u8],
offset: usize,
len: usize,
delim: u8,
) -> Result<Self>
pub fn parse( bytes: &'a [u8], offset: usize, len: usize, delim: u8, ) -> Result<Self>
Parses a Strtab
from bytes
at offset
with len
size as the backing string table, using delim
as the delimiter.
Errors if bytes are invalid UTF-8.
Requires feature = "alloc"
Sourcepub fn new_preparsed(bytes: &'a [u8], delim: u8) -> Result<Self>
pub fn new_preparsed(bytes: &'a [u8], delim: u8) -> Result<Self>
Parses a Strtab
with bytes
as the backing string table, using delim
as the delimiter between entries.
Requires feature = "alloc"
Sourcepub fn to_vec(&self) -> Result<Vec<&'a str>>
pub fn to_vec(&self) -> Result<Vec<&'a str>>
Converts the string table to a vector of parsed strings.
Note: This method is used to check the parsed contents of strtab
.
If you want to get the correct contents of strtab
as Vec
, use the following example.
§Examples
use goblin::error::Error;
pub fn show_shdr_strtab(bytes: &[u8]) -> Result<(), Error> {
let elf = goblin::elf::Elf::parse(&bytes)?;
for section in elf.section_headers {
println!("{}", elf.shdr_strtab.get_at(section.sh_name).unwrap_or(""));
}
Ok(())
}
Requires feature = "alloc"
Sourcepub fn get_at(&self, offset: usize) -> Option<&'a str>
pub fn get_at(&self, offset: usize) -> Option<&'a str>
Safely gets a str reference from the parsed table starting at byte offset
.
If the index is out of bounds, None
is returned.
Requires feature = "alloc"