use crate::error::{Error, Result};
pub const ELF_MAGIC: [u8; 4] = [0x7f, b'E', b'L', b'F'];
pub const EI_NIDENT: usize = 16;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ElfClass {
Elf32,
Elf64,
}
impl ElfClass {
pub const fn from_byte(value: u8) -> Result<Self> {
match value {
1 => Ok(Self::Elf32),
2 => Ok(Self::Elf64),
other => Err(Error::invalid_class(other)),
}
}
#[must_use]
pub const fn to_byte(self) -> u8 {
match self {
Self::Elf32 => 1,
Self::Elf64 => 2,
}
}
#[must_use]
pub const fn is_64bit(self) -> bool {
matches!(self, Self::Elf64)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Endian {
Little,
Big,
}
impl Endian {
pub const fn from_byte(value: u8) -> Result<Self> {
match value {
1 => Ok(Self::Little),
2 => Ok(Self::Big),
other => Err(Error::invalid_data_encoding(other)),
}
}
#[must_use]
pub const fn to_byte(self) -> u8 {
match self {
Self::Little => 1,
Self::Big => 2,
}
}
#[must_use]
pub const fn u16(self, bytes: [u8; 2]) -> u16 {
match self {
Self::Little => u16::from_le_bytes(bytes),
Self::Big => u16::from_be_bytes(bytes),
}
}
#[must_use]
pub const fn u32(self, bytes: [u8; 4]) -> u32 {
match self {
Self::Little => u32::from_le_bytes(bytes),
Self::Big => u32::from_be_bytes(bytes),
}
}
#[must_use]
pub const fn u64(self, bytes: [u8; 8]) -> u64 {
match self {
Self::Little => u64::from_le_bytes(bytes),
Self::Big => u64::from_be_bytes(bytes),
}
}
#[must_use]
pub const fn i32(self, bytes: [u8; 4]) -> i32 {
match self {
Self::Little => i32::from_le_bytes(bytes),
Self::Big => i32::from_be_bytes(bytes),
}
}
#[must_use]
pub const fn i64(self, bytes: [u8; 8]) -> i64 {
match self {
Self::Little => i64::from_le_bytes(bytes),
Self::Big => i64::from_be_bytes(bytes),
}
}
#[must_use]
pub const fn u16_bytes(self, value: u16) -> [u8; 2] {
match self {
Self::Little => value.to_le_bytes(),
Self::Big => value.to_be_bytes(),
}
}
#[must_use]
pub const fn u32_bytes(self, value: u32) -> [u8; 4] {
match self {
Self::Little => value.to_le_bytes(),
Self::Big => value.to_be_bytes(),
}
}
#[must_use]
pub const fn u64_bytes(self, value: u64) -> [u8; 8] {
match self {
Self::Little => value.to_le_bytes(),
Self::Big => value.to_be_bytes(),
}
}
#[must_use]
pub const fn i64_bytes(self, value: i64) -> [u8; 8] {
match self {
Self::Little => value.to_le_bytes(),
Self::Big => value.to_be_bytes(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OsAbi(pub u8);
impl OsAbi {
pub const SYSV: Self = Self(0);
pub const HPUX: Self = Self(1);
pub const NETBSD: Self = Self(2);
pub const GNU: Self = Self(3);
pub const SOLARIS: Self = Self(6);
pub const FREEBSD: Self = Self(9);
#[must_use]
pub const fn value(self) -> u8 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ElfIdent {
pub class: ElfClass,
pub data: Endian,
pub version: u8,
pub os_abi: OsAbi,
pub abi_version: u8,
}
impl ElfIdent {
pub fn parse(bytes: &[u8; EI_NIDENT]) -> Result<Self> {
if bytes[0..4] != ELF_MAGIC {
return Err(Error::invalid_magic());
}
Ok(Self {
class: ElfClass::from_byte(bytes[4])?,
data: Endian::from_byte(bytes[5])?,
version: bytes[6],
os_abi: OsAbi(bytes[7]),
abi_version: bytes[8],
})
}
#[must_use]
pub fn to_bytes(&self) -> [u8; EI_NIDENT] {
let mut bytes = [0u8; EI_NIDENT];
bytes[0..4].copy_from_slice(&ELF_MAGIC);
bytes[4] = self.class.to_byte();
bytes[5] = self.data.to_byte();
bytes[6] = self.version;
bytes[7] = self.os_abi.0;
bytes[8] = self.abi_version;
bytes
}
}