LCOV - code coverage report
Current view: top level - src/pe - data_directory.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 79.2 % 77 61
Test Date: 2025-06-24:00:00:00 Functions: 91.7 % 12 11

            Line data    Source code
       1              : //! PE Data Directory module
       2              : 
       3              : use std::marker::PhantomData;
       4              : 
       5              : use crate::common::into_optional;
       6              : use crate::common::FromFFI;
       7              : use crate::declare_iterator;
       8              : use crate::pe::Section;
       9              : use crate::to_slice;
      10              : 
      11              : use lief_ffi as ffi;
      12              : 
      13              : pub struct DataDirectory<'a> {
      14              :     ptr: cxx::UniquePtr<ffi::PE_DataDirectory>,
      15              :     _owner: PhantomData<&'a ffi::PE_Binary>,
      16              : }
      17              : 
      18              : #[allow(non_camel_case_types)]
      19         7008 : #[derive(Debug, Copy, Clone)]
      20              : pub enum Type {
      21              :     EXPORT_TABLE,
      22              :     IMPORT_TABLE,
      23              :     RESOURCE_TABLE,
      24              :     EXCEPTION_TABLE,
      25              :     CERTIFICATE_TABLE,
      26              :     BASE_RELOCATION_TABLE,
      27              :     DEBUG_DIR,
      28              :     ARCHITECTURE,
      29              :     GLOBAL_PTR,
      30              :     TLS_TABLE,
      31              :     LOAD_CONFIG_TABLE,
      32              :     BOUND_IMPORT,
      33              :     IAT,
      34              :     DELAY_IMPORT_DESCRIPTOR,
      35              :     CLR_RUNTIME_HEADER,
      36              :     RESERVED,
      37              :     UNKNOWN(u32),
      38              : }
      39              : 
      40              : 
      41              : /// Data directory's types
      42              : impl From<u32> for Type {
      43         7008 :     fn from(value: u32) -> Self {
      44         7008 :         match value {
      45          156 :             0x00000000 => Type::EXPORT_TABLE,
      46         2412 :             0x00000001 => Type::IMPORT_TABLE,
      47          156 :             0x00000002 => Type::RESOURCE_TABLE,
      48          156 :             0x00000003 => Type::EXCEPTION_TABLE,
      49          156 :             0x00000004 => Type::CERTIFICATE_TABLE,
      50          156 :             0x00000005 => Type::BASE_RELOCATION_TABLE,
      51          156 :             0x00000006 => Type::DEBUG_DIR,
      52          156 :             0x00000007 => Type::ARCHITECTURE,
      53          156 :             0x00000008 => Type::GLOBAL_PTR,
      54          156 :             0x00000009 => Type::TLS_TABLE,
      55          156 :             0x0000000a => Type::LOAD_CONFIG_TABLE,
      56          156 :             0x0000000b => Type::BOUND_IMPORT,
      57         2412 :             0x0000000c => Type::IAT,
      58          156 :             0x0000000d => Type::DELAY_IMPORT_DESCRIPTOR,
      59          156 :             0x0000000e => Type::CLR_RUNTIME_HEADER,
      60          156 :             0x0000000f => Type::RESERVED,
      61            0 :             _ => Type::UNKNOWN(value),
      62              : 
      63              :         }
      64         7008 :     }
      65              : }
      66              : impl From<Type> for u32 {
      67          168 :     fn from(ty: Type) -> Self {
      68          168 :         match ty {
      69            0 :             Type::EXPORT_TABLE => 0x00000000,
      70           12 :             Type::IMPORT_TABLE => 0x00000001,
      71          156 :             Type::RESOURCE_TABLE => 0x00000002,
      72            0 :             Type::EXCEPTION_TABLE => 0x00000003,
      73            0 :             Type::CERTIFICATE_TABLE => 0x00000004,
      74            0 :             Type::BASE_RELOCATION_TABLE => 0x00000005,
      75            0 :             Type::DEBUG_DIR => 0x00000006,
      76            0 :             Type::ARCHITECTURE => 0x00000007,
      77            0 :             Type::GLOBAL_PTR => 0x00000008,
      78            0 :             Type::TLS_TABLE => 0x00000009,
      79            0 :             Type::LOAD_CONFIG_TABLE => 0x0000000a,
      80            0 :             Type::BOUND_IMPORT => 0x0000000b,
      81            0 :             Type::IAT => 0x0000000c,
      82            0 :             Type::DELAY_IMPORT_DESCRIPTOR => 0x0000000d,
      83            0 :             Type::CLR_RUNTIME_HEADER => 0x0000000e,
      84            0 :             Type::RESERVED => 0x0000000f,
      85            0 :             Type::UNKNOWN(_) => 0x0000000f, // RESERVED
      86              : 
      87              :         }
      88          168 :     }
      89              : }
      90              : 
      91              : impl std::fmt::Debug for DataDirectory<'_> {
      92         7008 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      93         7008 :         f.debug_struct("DataDirectory")
      94         7008 :             .field("type", &self.get_type())
      95         7008 :             .field("rva", &self.rva())
      96         7008 :             .field("size", &self.size())
      97         7008 :             .field("section", &self.section())
      98         7008 :             .finish()
      99         7008 :     }
     100              : }
     101              : 
     102              : impl FromFFI<ffi::PE_DataDirectory> for DataDirectory<'_> {
     103         7176 :     fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_DataDirectory>) -> Self {
     104         7176 :         DataDirectory {
     105         7176 :             ptr,
     106         7176 :             _owner: PhantomData,
     107         7176 :         }
     108         7176 :     }
     109              : }
     110              : 
     111              : impl DataDirectory<'_> {
     112              :     /// The **relative** virtual address where the data referenced by this data directory are
     113              :     /// located.
     114         7128 :     pub fn rva(&self) -> u32 {
     115         7128 :         self.ptr.RVA()
     116         7128 :     }
     117              :     /// The size of the data referenced by this data directory
     118         7008 :     pub fn size(&self) -> u32 {
     119         7008 :         self.ptr.size()
     120         7008 :     }
     121              :     /// The type of the data directory which is defined by its index.
     122         7008 :     pub fn get_type(&self) -> Type {
     123         7008 :         Type::from(self.ptr.get_type())
     124         7008 :     }
     125              :     /// The (optional) section in which the data associated with the data directory
     126              :     /// are located.
     127         7008 :     pub fn section(&self) -> Option<Section> {
     128         7008 :         into_optional(self.ptr.section())
     129         7008 :     }
     130              : 
     131              :     /// Content bytes wraped with this data directory
     132          276 :     pub fn content(&self) -> &[u8] {
     133          276 :         to_slice!(self.ptr.content());
     134          276 :     }
     135              : }
     136              : 
     137         2496 : declare_iterator!(
     138         2496 :     DataDirectories,
     139         2496 :     DataDirectory<'a>,
     140         2496 :     ffi::PE_DataDirectory,
     141         2496 :     ffi::PE_Binary,
     142         2496 :     ffi::PE_Binary_it_data_directories
     143         2496 : );
        

Generated by: LCOV version 2.1-1