Move U32Table definition over to its one usage in hash.rs

This commit is contained in:
Christopher Cole 2022-11-13 13:58:48 -08:00
parent 910a46ef9d
commit a8c1125fa5
No known key found for this signature in database
GPG Key ID: 0AC856975983E9DB
2 changed files with 25 additions and 23 deletions

View File

@ -3,10 +3,28 @@ use core::mem::size_of;
use crate::endian::EndianParse;
use crate::file::Class;
use crate::parse::{ParseAt, ParseError, ParsingTable, ReadBytesExt, U32Table};
use crate::parse::{ParseAt, ParseError, ParsingTable, ReadBytesExt};
use crate::string_table::StringTable;
use crate::symbol::{Symbol, SymbolTable};
impl ParseAt for u32 {
fn parse_at<E: EndianParse>(
endian: E,
_class: Class,
offset: &mut usize,
data: &[u8],
) -> Result<Self, ParseError> {
endian.parse_u32_at(offset, data)
}
#[inline]
fn size_for(_class: Class) -> usize {
core::mem::size_of::<u32>()
}
}
type U32Table<'data, E> = ParsingTable<'data, E, u32>;
/// Header at the start of SysV Hash Table sections of type [SHT_HASH](crate::abi::SHT_HASH).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SysVHashHeader {
@ -87,7 +105,7 @@ impl<'data, E: EndianParse> SysVHashTable<'data, E> {
strtab: &StringTable<'data>,
) -> Result<Option<(usize, Symbol)>, ParseError> {
// empty hash tables don't have any entries. This avoids a divde by zero in the modulus calculation
if self.buckets.len() == 0 {
if self.buckets.is_empty() {
return Ok(None);
}
@ -239,7 +257,7 @@ impl<'data, E: EndianParse> GnuHashTable<'data, E> {
strtab: &StringTable<'data>,
) -> Result<Option<(usize, Symbol)>, ParseError> {
// empty hash tables don't have any entries. This avoids a divde by zero in the modulus calculation
if self.buckets.len() == 0 {
if self.buckets.is_empty() {
return Ok(None);
}

View File

@ -298,24 +298,6 @@ impl<'data, E: EndianParse, P: ParseAt> IntoIterator for ParsingTable<'data, E,
}
}
impl ParseAt for u32 {
fn parse_at<E: EndianParse>(
endian: E,
_class: Class,
offset: &mut usize,
data: &[u8],
) -> Result<Self, ParseError> {
endian.parse_u32_at(offset, data)
}
#[inline]
fn size_for(_class: Class) -> usize {
core::mem::size_of::<u32>()
}
}
pub type U32Table<'data, E> = ParsingTable<'data, E, u32>;
// Simple convenience extension trait to wrap get() with .ok_or(SliceReadError)
pub(crate) trait ReadBytesExt<'data> {
fn get_bytes(self, range: Range<usize>) -> Result<&'data [u8], ParseError>;
@ -331,7 +313,7 @@ impl<'data> ReadBytesExt<'data> for &'data [u8] {
}
#[cfg(test)]
pub fn test_parse_for<E: EndianParse, P: ParseAt + core::fmt::Debug + PartialEq>(
pub(crate) fn test_parse_for<E: EndianParse, P: ParseAt + core::fmt::Debug + PartialEq>(
endian: E,
class: Class,
expected: P,
@ -350,7 +332,7 @@ pub fn test_parse_for<E: EndianParse, P: ParseAt + core::fmt::Debug + PartialEq>
}
#[cfg(test)]
pub fn test_parse_fuzz_too_short<E: EndianParse, P: ParseAt + core::fmt::Debug>(
pub(crate) fn test_parse_fuzz_too_short<E: EndianParse, P: ParseAt + core::fmt::Debug>(
endian: E,
class: Class,
) {
@ -396,6 +378,8 @@ mod parsing_table_tests {
use super::*;
type U32Table<'data, E> = ParsingTable<'data, E, u32>;
#[test]
fn test_u32_validate_entsize() {
assert!(matches!(u32::validate_entsize(Class::ELF32, 4), Ok(4)));