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