LCOV - code coverage report
Current view: top level - src/macho/commands - dylib.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 39.3 % 112 44
Test Date: 2026-04-12:00:00:00 Functions: 53.3 % 15 8

            Line data    Source code
       1              : use super::Command;
       2              : use lief_ffi as ffi;
       3              : 
       4              : use crate::common::FromFFI;
       5              : use crate::declare_iterator;
       6              : use std::marker::PhantomData;
       7              : 
       8              : /// Structure which represents a library dependency
       9              : pub struct Dylib<'a> {
      10              :     ptr: cxx::UniquePtr<ffi::MachO_Dylib>,
      11              :     _owner: PhantomData<&'a ffi::MachO_Binary>,
      12              : }
      13              : 
      14              : impl Dylib<'_> {
      15              :     /// Library name
      16        91338 :     pub fn name(&self) -> String {
      17        91338 :         self.ptr.name().to_string()
      18        91338 :     }
      19              : 
      20            0 :     pub fn set_name(&mut self, name: &str) {
      21            0 :         self.ptr.pin_mut().set_name(name.to_string());
      22            0 :     }
      23              : 
      24              :     /// Create a new ID_DYLIB command
      25            0 :     pub fn id_dylib(name: &str, timestamp: u32, current_version: u32, compat_version: u32) -> Self {
      26            0 :         Self::from_ffi(ffi::MachO_Dylib::id_dylib(
      27            0 :             name.to_string(),
      28            0 :             timestamp,
      29            0 :             current_version,
      30            0 :             compat_version,
      31            0 :         ))
      32            0 :     }
      33              : 
      34              :     /// Create a new LOAD_DYLIB command
      35            0 :     pub fn load_dylib(
      36            0 :         name: &str,
      37            0 :         timestamp: u32,
      38            0 :         current_version: u32,
      39            0 :         compat_version: u32,
      40            0 :     ) -> Self {
      41            0 :         Self::from_ffi(ffi::MachO_Dylib::load_dylib(
      42            0 :             name.to_string(),
      43            0 :             timestamp,
      44            0 :             current_version,
      45            0 :             compat_version,
      46            0 :         ))
      47            0 :     }
      48              : 
      49              :     /// Create a new REEXPORT_DYLIB command
      50            0 :     pub fn reexport_dylib(
      51            0 :         name: &str,
      52            0 :         timestamp: u32,
      53            0 :         current_version: u32,
      54            0 :         compat_version: u32,
      55            0 :     ) -> Self {
      56            0 :         Self::from_ffi(ffi::MachO_Dylib::reexport_dylib(
      57            0 :             name.to_string(),
      58            0 :             timestamp,
      59            0 :             current_version,
      60            0 :             compat_version,
      61            0 :         ))
      62            0 :     }
      63              : 
      64              :     /// Create a new WEAK_DYLIB command
      65            0 :     pub fn weak_dylib(
      66            0 :         name: &str,
      67            0 :         timestamp: u32,
      68            0 :         current_version: u32,
      69            0 :         compat_version: u32,
      70            0 :     ) -> Self {
      71            0 :         Self::from_ffi(ffi::MachO_Dylib::weak_dylib(
      72            0 :             name.to_string(),
      73            0 :             timestamp,
      74            0 :             current_version,
      75            0 :             compat_version,
      76            0 :         ))
      77            0 :     }
      78              : 
      79              :     /// Create a new LAZY_LOAD_DYLIB command
      80            0 :     pub fn lazy_load_dylib(
      81            0 :         name: &str,
      82            0 :         timestamp: u32,
      83            0 :         current_version: u32,
      84            0 :         compat_version: u32,
      85            0 :     ) -> Self {
      86            0 :         Self::from_ffi(ffi::MachO_Dylib::lazy_load_dylib(
      87            0 :             name.to_string(),
      88            0 :             timestamp,
      89            0 :             current_version,
      90            0 :             compat_version,
      91            0 :         ))
      92            0 :     }
      93              : 
      94              :     /// Original string offset of the name
      95            0 :     pub fn name_offset(&self) -> u32 {
      96            0 :         self.ptr.name_offset()
      97            0 :     }
      98              : 
      99              :     /// Date and Time when the shared library was built
     100        91338 :     pub fn timestamp(&self) -> u32 {
     101        91338 :         self.ptr.timestamp()
     102        91338 :     }
     103              : 
     104              :     /// Current version of the shared library
     105        91338 :     pub fn current_version(&self) -> (u64, u64, u64) {
     106        91338 :         let vec = Vec::from(self.ptr.current_version().as_slice());
     107        91338 :         if vec.len() != 3 {
     108            0 :             return (0, 0, 0);
     109        91338 :         }
     110        91338 :         (vec[0], vec[1], vec[2])
     111        91338 :     }
     112              :     /// Compatibility version of the shared library
     113        91338 :     pub fn compatibility_version(&self) -> (u64, u64, u64) {
     114        91338 :         let vec = Vec::from(self.ptr.compatibility_version().as_slice());
     115        91338 :         if vec.len() != 3 {
     116            0 :             return (0, 0, 0);
     117        91338 :         }
     118        91338 :         (vec[0], vec[1], vec[2])
     119        91338 :     }
     120              : }
     121              : 
     122              : impl std::fmt::Debug for Dylib<'_> {
     123        91338 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     124        91338 :         let base = self as &dyn Command;
     125        91338 :         f.debug_struct("Dylib")
     126        91338 :             .field("base", &base)
     127        91338 :             .field("name", &self.name())
     128        91338 :             .field("timestamp", &self.timestamp())
     129        91338 :             .field("current_version", &self.current_version())
     130        91338 :             .field("compatibility_version", &self.compatibility_version())
     131        91338 :             .finish()
     132        91338 :     }
     133              : }
     134              : 
     135              : impl FromFFI<ffi::MachO_Dylib> for Dylib<'_> {
     136        91364 :     fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_Dylib>) -> Self {
     137        91364 :         Self {
     138        91364 :             ptr: cmd,
     139        91364 :             _owner: PhantomData,
     140        91364 :         }
     141        91364 :     }
     142              : }
     143              : 
     144              : impl Command for Dylib<'_> {
     145       365352 :     fn get_base(&self) -> &ffi::MachO_Command {
     146       365352 :         self.ptr.as_ref().unwrap().as_ref()
     147       365352 :     }
     148              : }
     149              : 
     150         1287 : declare_iterator!(
     151         1287 :     Libraries,
     152         1287 :     Dylib<'a>,
     153         1287 :     ffi::MachO_Dylib,
     154         1287 :     ffi::MachO_Binary,
     155         1287 :     ffi::MachO_Binary_it_libraries
     156         1287 : );
        

Generated by: LCOV version 2.1-1