LCOV - code coverage report
Current view: top level - src/macho/commands - build_version.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 72.7 % 55 40
Test Date: 2024-10-27:00:00:00 Functions: 72.7 % 11 8

            Line data    Source code
       1              : use std::marker::PhantomData;
       2              : 
       3              : use lief_ffi as ffi;
       4              : 
       5              : use super::Command;
       6              : use crate::common::FromFFI;
       7              : 
       8              : /// Structure that represents the `LC_BUILD_VERSION` command
       9              : pub struct BuildVersion<'a> {
      10              :     ptr: cxx::UniquePtr<ffi::MachO_BuildVersion>,
      11              :     _owner: PhantomData<&'a ffi::MachO_Binary>,
      12              : }
      13              : 
      14              : #[allow(non_camel_case_types)]
      15          192 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
      16              : pub enum Platform {
      17              :     MACOS,
      18              :     IOS,
      19              :     TVOS,
      20              :     WATCHOS,
      21              :     BRIDGEOS,
      22              :     MAC_CATALYST,
      23              :     IOS_SIMULATOR,
      24              :     TVOS_SIMULATOR,
      25              :     WATCHOS_SIMULATOR,
      26              :     DRIVERKIT,
      27              :     VISIONOS,
      28              :     VISIONOS_SIMULATOR,
      29              :     FIRMWARE,
      30              :     SEPOS,
      31              :     ANY,
      32              :     UNKNOWN(u32),
      33              : }
      34              : 
      35              : impl From<u32> for Platform {
      36          192 :     fn from(value: u32) -> Self {
      37          192 :         match value {
      38           48 :             0x00000001 => Platform::MACOS,
      39           72 :             0x00000002 => Platform::IOS,
      40            0 :             0x00000003 => Platform::TVOS,
      41            0 :             0x00000004 => Platform::WATCHOS,
      42            0 :             0x00000005 => Platform::BRIDGEOS,
      43            0 :             0x00000006 => Platform::MAC_CATALYST,
      44            0 :             0x00000007 => Platform::IOS_SIMULATOR,
      45            0 :             0x00000008 => Platform::TVOS_SIMULATOR,
      46            0 :             0x00000009 => Platform::WATCHOS_SIMULATOR,
      47            0 :             0x0000000A => Platform::DRIVERKIT,
      48            0 :             0x0000000B => Platform::VISIONOS,
      49            0 :             0x0000000C => Platform::VISIONOS_SIMULATOR,
      50            0 :             0x0000000D => Platform::FIRMWARE,
      51            0 :             0x0000000E => Platform::SEPOS,
      52            0 :             0xFFFFFFFF => Platform::ANY,
      53           72 :             _ => Platform::UNKNOWN(value),
      54              :         }
      55          192 :     }
      56              : }
      57              : 
      58              : impl BuildVersion<'_> {
      59           80 :     pub fn sdk(&self) -> (u64, u64, u64) {
      60           80 :         let vec = Vec::from(self.ptr.sdk().as_slice());
      61           80 :         if vec.len() != 3 {
      62            0 :             return (0, 0, 0);
      63           80 :         }
      64           80 :         (vec[0], vec[1], vec[2])
      65           80 :     }
      66              : 
      67           80 :     pub fn minos(&self) -> (u64, u64, u64) {
      68           80 :         let vec = Vec::from(self.ptr.sdk().as_slice());
      69           80 :         if vec.len() != 3 {
      70            0 :             return (0, 0, 0);
      71           80 :         }
      72           80 :         (vec[0], vec[1], vec[2])
      73           80 :     }
      74              : 
      75           80 :     pub fn platform(&self) -> Platform {
      76           80 :         Platform::from(self.ptr.platform())
      77           80 :     }
      78              : }
      79              : 
      80              : impl std::fmt::Debug for BuildVersion<'_> {
      81           80 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      82           80 :         let base = self as &dyn Command;
      83           80 :         f.debug_struct("BuildVersion")
      84           80 :             .field("base", &base)
      85           80 :             .field("sdk", &self.sdk())
      86           80 :             .field("minos", &self.minos())
      87           80 :             .field("platform", &self.platform())
      88           80 :             .finish()
      89           80 :     }
      90              : }
      91              : 
      92              : impl FromFFI<ffi::MachO_BuildVersion> for BuildVersion<'_> {
      93           80 :     fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_BuildVersion>) -> Self {
      94           80 :         Self {
      95           80 :             ptr: cmd,
      96           80 :             _owner: PhantomData,
      97           80 :         }
      98           80 :     }
      99              : }
     100              : 
     101              : impl Command for BuildVersion<'_> {
     102          320 :     fn get_base(&self) -> &ffi::MachO_Command {
     103          320 :         self.ptr.as_ref().unwrap().as_ref()
     104          320 :     }
     105              : }
        

Generated by: LCOV version 2.1-1