hao 0.0.19

Read, edit, create and write .net assemblies
Documentation
//! The [`HaoError`] type returned by every fallible operation, and its [`Result`] alias.

use thiserror::Error;

/// The result type returned throughout `hao`.
pub type Result<T> = std::result::Result<T, HaoError>;

/// Everything that can go wrong reading, editing or writing a module.
///
/// Loading is strict, so most variants describe a malformed image: which part
/// of the file was bad and, where known, where.
#[derive(Error, Debug)]
pub enum HaoError {
    /// Reading or writing the file on disk failed, in [`Module::from_path`](crate::Module::from_path)
    /// or [`Module::write_to_path`](crate::Module::write_to_path).
    #[error("An IO error has occurred. {0:?}")]
    IoError(std::io::Error),
    /// The bytes are not a valid PE image, or its optional header is missing.
    #[error("failed to parse PE file")]
    BadPeFormat,
    /// The PE image could not be re-parsed or written back while writing a module.
    #[error("PE error: {0:?}")]
    PeError(pewter::error::PewterError),
    /// The PE image has no CLI header, so it is native code rather than a .NET assembly.
    #[error("file is not a .net binary")]
    NotDotNetBinary,
    /// The CLI header or metadata is malformed; the message says what was wrong.
    #[error("file is badly formatted. {0}")]
    BadImageFormat(&'static str),
    /// A relative virtual address points outside every section of the image.
    #[error("RVA {0} is too large for this image")]
    BadRva(u32),
    /// A read ran past the end of its buffer; holds how many bytes were missing.
    #[error("Not enough data to complete read of {0} bytes")]
    NotEnoughDataLeft(usize),
    /// A metadata table column has an impossible size. Currently never returned.
    #[error("Invalid column size: {0}")]
    InvalidColumnSize(i32),
    /// A coded index (e.g. `TypeDefOrRef`) has a tag that names no table; holds the
    /// raw value and the coded index kind.
    #[error("Invalid value ({0}) for coded token \"{1}\".")]
    InvalidCodedToken(u32, &'static str),
    /// A coded index points past the end of its table. Currently never returned.
    #[error("Invalid offset ({0}) for coded token \"{1}\".")]
    InvalidCodedTokenOffset(u32, &'static str),
    /// An offset into a metadata heap (`#Strings`, `#Blob`, `#GUID`, `#US`) is out of range.
    #[error("Invalid index ({0}) for stream \"{1}\".")]
    InvalidStreamIndex(&'static str, usize),
    /// A `#Strings` heap entry is not valid UTF-8; holds its offset and raw bytes.
    #[error("Invalid UTF8 string at position {0} in #Strings stream. ({1:?})")]
    InvalidUTF8String(usize, Vec<u8>),
    /// A signature starts with a calling convention byte `hao` does not recognise.
    #[error("Invalid signature calling convention. {0}.")]
    InvalidSignatureCallingConvention(u8),
    /// A signature contains an unknown or misplaced `ELEMENT_TYPE_*` byte.
    #[error("Invalid signature element type ({0:#x}) at position {1}.")]
    InvalidSignatureElementType(u8, usize),
    /// A signature blob is malformed in some other way (truncated, a null token, ...).
    #[error("Invalid signature: {0} (at position {1}).")]
    InvalidSignature(&'static str, usize),
    /// A method body header, exception clause or operand is malformed.
    #[error("Invalid method body: {0} (at position {1}).")]
    InvalidMethodBody(&'static str, usize),
    /// The IL stream contains a byte sequence that is not an opcode.
    #[error("Invalid opcode {0:#x} at position {1}.")]
    InvalidOpCode(u16, usize),
    /// A metadata token in IL or a signature names a table it can't, or a row that doesn't exist.
    #[error("Invalid metadata token {0:#010x}.")]
    InvalidToken(u32),
    /// A custom attribute blob could not be decoded or encoded.
    #[error("Invalid custom attribute: {0}")]
    InvalidCustomAttribute(String),
    /// A value is too big for its encoding, e.g. an integer over the compressed-integer limit.
    #[error("Value too large to encode as a {0}.")]
    ValueTooLarge(&'static str),
    /// A module could not be written: e.g. a branch to a missing label, or an entry
    /// that belongs to another module.
    #[error("Could not write module: {0}")]
    WriteError(String),
    /// Something couldn't be imported into a module (a type, method or
    /// field from another module, say).
    #[error("Could not import: {0}")]
    Import(String),
    /// A lookup found nothing: a type, member or assembly.
    #[error("{0}")]
    NotFound(String),
    /// A signature is the wrong kind for its entry. Currently never returned.
    #[error("The provided signature does not match what is expected for the entry.")]
    InvalidSignatureForEntry(&'static str),
    /// A signature nests types too deeply (more than 64 levels), which usually means
    /// it is malformed or cyclic.
    #[error("Recursion limit reached.")]
    RecursionLimitReached,
    /// A table row refers to a row of another table that doesn't exist; holds the
    /// table name and index.
    #[error("Invalid reference to entry in {0} table at index {1}.")]
    InvalidEntryReference(&'static str, usize),
    /// Unspecified failure. Currently never returned.
    #[error("unknown error")]
    Unknown,
}