Skip to main content

pe_assembler/types/nt/
mod.rs

1use byteorder::{LittleEndian, ReadBytesExt};
2use gaia_types::GaiaError;
3use serde::{Deserialize, Serialize};
4use std::io::{Read, Seek, SeekFrom};
5
6/// NT header structure
7///
8/// Contains the main signature and basic information for a PE file, identifying it as a valid PE file.
9/// The signature field must be 0x00004550 ("PE\0\0").
10#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
11pub struct NtHeader {
12    /// PE signature, must be 0x00004550 ("PE\0\0")
13    pub signature: u32,
14}
15
16impl NtHeader {
17    /// Read NT header from binary reader
18    ///
19    /// # Arguments
20    /// * `reader` - Binary reader
21    ///
22    /// # Returns
23    /// Returns NT header structure or error
24    pub fn read<R>(mut reader: R) -> Result<Self, GaiaError>
25    where
26        R: Read,
27    {
28        let signature = reader.read_u32::<LittleEndian>()?;
29        Ok(NtHeader { signature })
30    }
31
32    pub fn read_at<R, E>(mut reader: R, offset: u64) -> Result<Self, GaiaError>
33    where
34        R: Read + Seek,
35    {
36        reader.seek(SeekFrom::Start(offset))?;
37        Self::read(reader)
38    }
39}
40
41impl Default for NtHeader {
42    fn default() -> Self {
43        Self {
44            // "PE\0\0"
45            signature: 0x00004550,
46        }
47    }
48}