use std::path::PathBuf;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum BinparseError {
#[error("Failed to read file '{path}': {source}")]
FileRead {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("Not a valid ELF file: {0}")]
NotElf(String),
#[error("Failed to parse binary: {0}")]
ParseError(String),
#[error("Unsupported binary format: {0}")]
UnsupportedFormat(String),
#[error("File is too small to be a valid binary ({size} bytes)")]
FileTooSmall { size: usize },
#[error("{kind} not found: {name}")]
NotFound { kind: &'static str, name: String },
#[error("Failed to read string from string table at offset {offset}")]
StringTableError { offset: usize },
}
impl From<goblin::error::Error> for BinparseError {
fn from(err: goblin::error::Error) -> Self {
match err {
goblin::error::Error::Malformed(msg) => BinparseError::ParseError(msg.to_string()),
goblin::error::Error::BadMagic(magic) => {
BinparseError::NotElf(format!("Bad magic number: 0x{:08X}", magic))
}
goblin::error::Error::Scroll(e) => BinparseError::ParseError(e.to_string()),
goblin::error::Error::IO(e) => BinparseError::ParseError(e.to_string()),
goblin::error::Error::BufferTooShort(needed, _) => {
BinparseError::ParseError(format!("Buffer too short, needed {} bytes", needed))
}
_ => BinparseError::ParseError(err.to_string()),
}
}
}
pub type Result<T> = std::result::Result<T, BinparseError>;