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 79848 : pub fn name(&self) -> String {
17 79848 : self.ptr.name().to_string()
18 79848 : }
19 :
20 : /// Original string offset of the name
21 0 : pub fn name_offset(&self) -> u32 {
22 0 : self.ptr.name_offset()
23 0 : }
24 :
25 : /// Date and Time when the shared library was built
26 79848 : pub fn timestamp(&self) -> u32 {
27 79848 : self.ptr.timestamp()
28 79848 : }
29 :
30 : /// Current version of the shared library
31 79848 : pub fn current_version(&self) -> (u64, u64, u64) {
32 79848 : let vec = Vec::from(self.ptr.current_version().as_slice());
33 79848 : if vec.len() != 3 {
34 0 : return (0, 0, 0);
35 79848 : }
36 79848 : (vec[0], vec[1], vec[2])
37 79848 : }
38 : /// Compatibility version of the shared library
39 79848 : pub fn compatibility_version(&self) -> (u64, u64, u64) {
40 79848 : let vec = Vec::from(self.ptr.compatibility_version().as_slice());
41 79848 : if vec.len() != 3 {
42 0 : return (0, 0, 0);
43 79848 : }
44 79848 : (vec[0], vec[1], vec[2])
45 79848 : }
46 : }
47 :
48 : impl std::fmt::Debug for Dylib<'_> {
49 79848 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 79848 : let base = self as &dyn Command;
51 79848 : f.debug_struct("Dylib")
52 79848 : .field("base", &base)
53 79848 : .field("name", &self.name())
54 79848 : .field("timestamp", &self.timestamp())
55 79848 : .field("current_version", &self.current_version())
56 79848 : .field("compatibility_version", &self.compatibility_version())
57 79848 : .finish()
58 79848 : }
59 : }
60 :
61 : impl FromFFI<ffi::MachO_Dylib> for Dylib<'_> {
62 79872 : fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_Dylib>) -> Self {
63 79872 : Self {
64 79872 : ptr: cmd,
65 79872 : _owner: PhantomData,
66 79872 : }
67 79872 : }
68 : }
69 :
70 : impl Command for Dylib<'_> {
71 319392 : fn get_base(&self) -> &ffi::MachO_Command {
72 319392 : self.ptr.as_ref().unwrap().as_ref()
73 319392 : }
74 : }
75 :
76 1092 : declare_iterator!(
77 1092 : Libraries,
78 1092 : Dylib<'a>,
79 1092 : ffi::MachO_Dylib,
80 1092 : ffi::MachO_Binary,
81 1092 : ffi::MachO_Binary_it_libraries
82 1092 : );
|