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
use core::convert::TryInto;

use crate::pod::Bytes;

#[allow(unused)]
#[inline]
pub(crate) fn align(offset: usize, size: usize) -> usize {
    (offset + (size - 1)) & !(size - 1)
}

pub(crate) fn data_range(
    data: &[u8],
    data_address: u64,
    range_address: u64,
    size: u64,
) -> Option<&[u8]> {
    let offset = range_address.checked_sub(data_address)?;
    data.get(offset.try_into().ok()?..)?
        .get(..size.try_into().ok()?)
}

/// A table of zero-terminated strings.
///
/// This is used for most file formats.
#[derive(Debug, Default, Clone, Copy)]
pub struct StringTable<'data> {
    data: Bytes<'data>,
}

impl<'data> StringTable<'data> {
    /// Interpret the given data as a string table.
    pub fn new(data: &'data [u8]) -> Self {
        StringTable { data: Bytes(data) }
    }

    /// Return the string at the given offset.
    pub fn get(&self, offset: u32) -> Result<&'data [u8], ()> {
        self.data.read_string_at(offset as usize)
    }
}