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 864 : #[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 : MACOS_EXCLAVE_CORE,
32 : MACOS_EXCLAVE_KIT,
33 : IOS_EXCLAVE_CORE,
34 : IOS_EXCLAVE_KIT,
35 : TVOS_EXCLAVE_CORE,
36 : TVOS_EXCLAVE_KIT,
37 : WATCHOS_EXCLAVE_CORE,
38 : WATCHOS_EXCLAVE_KIT,
39 : VISIONOS_EXCLAVE_CORE,
40 : VISIONOS_EXCLAVE_KIT,
41 : ANY,
42 : UNKNOWN(u32),
43 : }
44 :
45 : impl From<u32> for Platform {
46 864 : fn from(value: u32) -> Self {
47 864 : match value {
48 648 : 0x00000001 => Platform::MACOS,
49 108 : 0x00000002 => Platform::IOS,
50 0 : 0x00000003 => Platform::TVOS,
51 0 : 0x00000004 => Platform::WATCHOS,
52 0 : 0x00000005 => Platform::BRIDGEOS,
53 0 : 0x00000006 => Platform::MAC_CATALYST,
54 0 : 0x00000007 => Platform::IOS_SIMULATOR,
55 0 : 0x00000008 => Platform::TVOS_SIMULATOR,
56 0 : 0x00000009 => Platform::WATCHOS_SIMULATOR,
57 0 : 0x0000000A => Platform::DRIVERKIT,
58 0 : 0x0000000B => Platform::VISIONOS,
59 0 : 0x0000000C => Platform::VISIONOS_SIMULATOR,
60 0 : 0x0000000D => Platform::FIRMWARE,
61 0 : 0x0000000E => Platform::SEPOS,
62 0 : 0x0000000F => Platform::MACOS_EXCLAVE_CORE,
63 0 : 0x00000010 => Platform::MACOS_EXCLAVE_KIT,
64 0 : 0x00000011 => Platform::IOS_EXCLAVE_CORE,
65 0 : 0x00000012 => Platform::IOS_EXCLAVE_KIT,
66 0 : 0x00000013 => Platform::TVOS_EXCLAVE_CORE,
67 0 : 0x00000014 => Platform::TVOS_EXCLAVE_KIT,
68 0 : 0x00000015 => Platform::WATCHOS_EXCLAVE_CORE,
69 0 : 0x00000016 => Platform::WATCHOS_EXCLAVE_KIT,
70 0 : 0x00000017 => Platform::VISIONOS_EXCLAVE_CORE,
71 0 : 0x00000018 => Platform::VISIONOS_EXCLAVE_KIT,
72 0 : 0xFFFFFFFF => Platform::ANY,
73 108 : _ => Platform::UNKNOWN(value),
74 : }
75 864 : }
76 : }
77 :
78 : impl BuildVersion<'_> {
79 504 : pub fn sdk(&self) -> (u64, u64, u64) {
80 504 : let vec = Vec::from(self.ptr.sdk().as_slice());
81 504 : if vec.len() != 3 {
82 0 : return (0, 0, 0);
83 504 : }
84 504 : (vec[0], vec[1], vec[2])
85 504 : }
86 :
87 504 : pub fn minos(&self) -> (u64, u64, u64) {
88 504 : let vec = Vec::from(self.ptr.sdk().as_slice());
89 504 : if vec.len() != 3 {
90 0 : return (0, 0, 0);
91 504 : }
92 504 : (vec[0], vec[1], vec[2])
93 504 : }
94 :
95 504 : pub fn platform(&self) -> Platform {
96 504 : Platform::from(self.ptr.platform())
97 504 : }
98 : }
99 :
100 : impl std::fmt::Debug for BuildVersion<'_> {
101 504 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 504 : let base = self as &dyn Command;
103 504 : f.debug_struct("BuildVersion")
104 504 : .field("base", &base)
105 504 : .field("sdk", &self.sdk())
106 504 : .field("minos", &self.minos())
107 504 : .field("platform", &self.platform())
108 504 : .finish()
109 504 : }
110 : }
111 :
112 : impl FromFFI<ffi::MachO_BuildVersion> for BuildVersion<'_> {
113 504 : fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_BuildVersion>) -> Self {
114 504 : Self {
115 504 : ptr: cmd,
116 504 : _owner: PhantomData,
117 504 : }
118 504 : }
119 : }
120 :
121 : impl Command for BuildVersion<'_> {
122 2016 : fn get_base(&self) -> &ffi::MachO_Command {
123 2016 : self.ptr.as_ref().unwrap().as_ref()
124 2016 : }
125 : }
|