hao 0.0.19

Read, edit, create and write .net assemblies
Documentation
//! The metadata streams: the tables (`#~`) and the heaps their rows point into.
//!
//! [`MetadataStreams`] holds one of each. The heaps are:
//!
//! - [`StringsStream`] (`#Strings`): names, as null-terminated UTF-8.
//! - [`BlobStream`] (`#Blob`): signatures, constant values, attribute arguments.
//! - [`GuidStream`] (`#GUID`): GUIDs, such as the module's MVID.
//! - [`UserStringsStream`] (`#US`): string literals loaded by `ldstr`.

mod blob_stream;
pub mod tables_stream;
mod values_streams;
pub use blob_stream::*;
pub use values_streams::*;

use super::metadata_header::StreamHeader;
use crate::{
    error::{HaoError, Result},
    io::PEImage,
};
use tables_stream::TablesStreams;

/// Schema version of the tables stream (major, minor), usually 2.0. Some row
/// layouts depend on it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version(u8, u8);
impl Version {
    /// Makes a version from its major and minor numbers.
    pub fn new(major_version: u8, minor_version: u8) -> Self {
        Self(major_version, minor_version)
    }
}

/// The streams of a module's metadata.
#[derive(Debug, Clone)]
pub struct MetadataStreams<'a> {
    /// `#~` (or `#-`): the metadata tables.
    pub tables_stream: TablesStreams<'a>,
    /// `#Strings`: names, indexed by
    /// [`StringsStreamOffset`](tables_stream::StringsStreamOffset).
    pub strings_stream: StringsStream<'a>,
    /// `#GUID`: GUIDs, indexed by
    /// [`GuidStreamOffset`](tables_stream::GuidStreamOffset).
    pub guid_stream: GuidStream<'a>,
    /// `#Blob`: signatures and other binary values, indexed by
    /// [`BlobStreamOffset`](tables_stream::BlobStreamOffset).
    pub blob_stream: BlobStream<'a>,
    /// `#US`: string literals for `ldstr`. `None` if the module has none.
    pub user_strings_stream: Option<UserStringsStream<'a>>,
}

impl<'a> MetadataStreams<'a> {
    /// Finds each stream from its header. Stream offsets are relative to the
    /// metadata root, at RVA `metadata_virtual_address`.
    ///
    /// Errors if the tables, `#Strings`, `#GUID` or `#Blob` stream is missing.
    /// Unknown streams, such as `#Pdb`, are ignored.
    pub fn from_headers(
        pe_image: &PEImage<'a>,
        streams: &[StreamHeader],
        metadata_virtual_address: u32,
    ) -> Result<Self> {
        let mut strings_stream = None;
        let mut tables_stream = None;
        let mut blob_stream = None;
        let mut guid_stream = None;
        let mut user_strings_stream = None;

        for stream_header in streams {
            let stream_reader = pe_image.create_reader(
                metadata_virtual_address + stream_header.offset,
                Some(stream_header.stream_size as usize),
            )?;

            match stream_header.name.as_ref() {
                "#Strings" => strings_stream = Some(StringsStream::from_reader(stream_reader)?),
                "#US" => user_strings_stream = Some(UserStringsStream::from_reader(stream_reader)?),
                "#Blob" => blob_stream = Some(BlobStream::from_reader(stream_reader)?),
                "#GUID" => guid_stream = Some(GuidStream::from_reader(stream_reader)?),
                "#~" | "#-" => tables_stream = Some(TablesStreams::from_reader(stream_reader)?),
                "#Pdb" => continue,
                _ => {}
            }
        }

        Ok(Self {
            tables_stream: tables_stream
                .ok_or(HaoError::BadImageFormat("No .net tables stream (#~)"))?,
            strings_stream: strings_stream
                .ok_or(HaoError::BadImageFormat("No strings stream (#Strings)"))?,
            guid_stream: guid_stream.ok_or(HaoError::BadImageFormat("No guid stream (#GUID)"))?,
            blob_stream: blob_stream.ok_or(HaoError::BadImageFormat("No blob stream (#Blob)"))?,
            user_strings_stream,
        })
    }
}