Struct metagoblin::strtab::Strtab

source ·
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>

source

pub fn new(bytes: &'a [u8], delim: u8) -> Strtab<'a>

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

source

pub fn len(&self) -> usize

Returns the length of this Strtab in bytes

source

pub fn from_slice_unparsed( bytes: &'a [u8], offset: usize, len: usize, delim: u8 ) -> Strtab<'a>

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.

source

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().

source

pub fn parse( bytes: &'a [u8], offset: usize, len: usize, delim: u8 ) -> Result<Strtab<'a>, Error>

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"

source

pub fn new_preparsed(bytes: &'a [u8], delim: u8) -> Result<Strtab<'a>, Error>

Parses a Strtab with bytes as the backing string table, using delim as the delimiter between entries.

Requires feature = "alloc"

source

pub fn to_vec(&self) -> Result<Vec<&'a str>, Error>

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"

source

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"

source

pub unsafe fn from_raw(ptr: *const u8, len: usize, delim: u8) -> Strtab<'a>

👎Deprecated since 0.4.2: Use from_slice_unparsed() instead

Construct a strtab from a ptr, and a size, using delim as the delimiter

§Safety

This function creates a Strtab directly from a raw pointer and size

source

pub fn get(&self, offset: usize) -> Option<Result<&'a str, Error>>

👎Deprecated since 0.4.2: Bad performance, use get_at() instead

Parses a str reference from the parsed table starting at byte offset.

If the index is out of bounds, None is returned. Requires feature = "alloc"

Trait Implementations§

source§

impl<'a> Debug for Strtab<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<'a> Default for Strtab<'a>

source§

fn default() -> Strtab<'a>

Returns the “default value” for a type. Read more
source§

impl<'a> Index<usize> for Strtab<'a>

source§

fn index(&self, offset: usize) -> &<Strtab<'a> as Index<usize>>::Output

Gets str reference at starting at byte offset. NB: this will panic if the underlying bytes are not valid utf8, or the offset is invalid

§

type Output = str

The returned type after indexing.

Auto Trait Implementations§

§

impl<'a> Freeze for Strtab<'a>

§

impl<'a> RefUnwindSafe for Strtab<'a>

§

impl<'a> Send for Strtab<'a>

§

impl<'a> Sync for Strtab<'a>

§

impl<'a> Unpin for Strtab<'a>

§

impl<'a> UnwindSafe for Strtab<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.