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: 2026-04-12: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         7592 : #[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              : /// Data directory's types
      41              : impl From<u32> for Type {
      42         7592 :     fn from(value: u32) -> Self {
      43         7592 :         match value {
      44          169 :             0x00000000 => Type::EXPORT_TABLE,
      45         2613 :             0x00000001 => Type::IMPORT_TABLE,
      46          169 :             0x00000002 => Type::RESOURCE_TABLE,
      47          169 :             0x00000003 => Type::EXCEPTION_TABLE,
      48          169 :             0x00000004 => Type::CERTIFICATE_TABLE,
      49          169 :             0x00000005 => Type::BASE_RELOCATION_TABLE,
      50          169 :             0x00000006 => Type::DEBUG_DIR,
      51          169 :             0x00000007 => Type::ARCHITECTURE,
      52          169 :             0x00000008 => Type::GLOBAL_PTR,
      53          169 :             0x00000009 => Type::TLS_TABLE,
      54          169 :             0x0000000a => Type::LOAD_CONFIG_TABLE,
      55          169 :             0x0000000b => Type::BOUND_IMPORT,
      56         2613 :             0x0000000c => Type::IAT,
      57          169 :             0x0000000d => Type::DELAY_IMPORT_DESCRIPTOR,
      58          169 :             0x0000000e => Type::CLR_RUNTIME_HEADER,
      59          169 :             0x0000000f => Type::RESERVED,
      60            0 :             _ => Type::UNKNOWN(value),
      61              :         }
      62         7592 :     }
      63              : }
      64              : impl From<Type> for u32 {
      65          182 :     fn from(ty: Type) -> Self {
      66          182 :         match ty {
      67            0 :             Type::EXPORT_TABLE => 0x00000000,
      68           13 :             Type::IMPORT_TABLE => 0x00000001,
      69          169 :             Type::RESOURCE_TABLE => 0x00000002,
      70            0 :             Type::EXCEPTION_TABLE => 0x00000003,
      71            0 :             Type::CERTIFICATE_TABLE => 0x00000004,
      72            0 :             Type::BASE_RELOCATION_TABLE => 0x00000005,
      73            0 :             Type::DEBUG_DIR => 0x00000006,
      74            0 :             Type::ARCHITECTURE => 0x00000007,
      75            0 :             Type::GLOBAL_PTR => 0x00000008,
      76            0 :             Type::TLS_TABLE => 0x00000009,
      77            0 :             Type::LOAD_CONFIG_TABLE => 0x0000000a,
      78            0 :             Type::BOUND_IMPORT => 0x0000000b,
      79            0 :             Type::IAT => 0x0000000c,
      80            0 :             Type::DELAY_IMPORT_DESCRIPTOR => 0x0000000d,
      81            0 :             Type::CLR_RUNTIME_HEADER => 0x0000000e,
      82            0 :             Type::RESERVED => 0x0000000f,
      83            0 :             Type::UNKNOWN(_) => 0x0000000f, // RESERVED
      84              :         }
      85          182 :     }
      86              : }
      87              : 
      88              : impl std::fmt::Debug for DataDirectory<'_> {
      89         7592 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      90         7592 :         f.debug_struct("DataDirectory")
      91         7592 :             .field("type", &self.get_type())
      92         7592 :             .field("rva", &self.rva())
      93         7592 :             .field("size", &self.size())
      94         7592 :             .field("section", &self.section())
      95         7592 :             .finish()
      96         7592 :     }
      97              : }
      98              : 
      99              : impl FromFFI<ffi::PE_DataDirectory> for DataDirectory<'_> {
     100         7774 :     fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_DataDirectory>) -> Self {
     101         7774 :         DataDirectory {
     102         7774 :             ptr,
     103         7774 :             _owner: PhantomData,
     104         7774 :         }
     105         7774 :     }
     106              : }
     107              : 
     108              : impl DataDirectory<'_> {
     109              :     /// The **relative** virtual address where the data referenced by this data directory are
     110              :     /// located.
     111         7722 :     pub fn rva(&self) -> u32 {
     112         7722 :         self.ptr.RVA()
     113         7722 :     }
     114              :     /// The size of the data referenced by this data directory
     115         7592 :     pub fn size(&self) -> u32 {
     116         7592 :         self.ptr.size()
     117         7592 :     }
     118              :     /// The type of the data directory which is defined by its index.
     119         7592 :     pub fn get_type(&self) -> Type {
     120         7592 :         Type::from(self.ptr.get_type())
     121         7592 :     }
     122              :     /// The (optional) section in which the data associated with the data directory
     123              :     /// are located.
     124         7592 :     pub fn section(&self) -> Option<Section<'_>> {
     125         7592 :         into_optional(self.ptr.section())
     126         7592 :     }
     127              : 
     128              :     /// Content bytes wraped with this data directory
     129          299 :     pub fn content(&self) -> &[u8] {
     130          299 :         to_slice!(self.ptr.content());
     131          299 :     }
     132              : }
     133              : 
     134         2704 : declare_iterator!(
     135         2704 :     DataDirectories,
     136         2704 :     DataDirectory<'a>,
     137         2704 :     ffi::PE_DataDirectory,
     138         2704 :     ffi::PE_Binary,
     139         2704 :     ffi::PE_Binary_it_data_directories
     140         2704 : );
        

Generated by: LCOV version 2.1-1