forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
169 lines (140 loc) · 4.57 KB
/
Copy pathutils.cpp
File metadata and controls
169 lines (140 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <vector>
#include "LIEF/MachO/utils.hpp"
#include "LIEF/MachO/DyldInfo.hpp"
#include "LIEF/MachO/SegmentCommand.hpp"
#include "Object.tcc"
#include "MachO/Structures.hpp"
#include "LIEF/BinaryStream/FileStream.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
#include "logging.hpp"
namespace LIEF {
namespace MachO {
inline result<MACHO_TYPES> magic_from_stream(BinaryStream& stream,
bool keep_offset = false) {
ScopedStream scoped(stream);
if (!keep_offset) {
scoped->setpos(0);
}
if (auto magic_res = scoped->read<uint32_t>()) {
return static_cast<MACHO_TYPES>(*magic_res);
}
return make_error_code(lief_errors::read_error);
}
bool is_macho(BinaryStream& stream) {
if (auto magic_res = magic_from_stream(stream)) {
const MACHO_TYPES magic = *magic_res;
return (magic == MACHO_TYPES::MAGIC ||
magic == MACHO_TYPES::CIGAM ||
magic == MACHO_TYPES::MAGIC_64 ||
magic == MACHO_TYPES::CIGAM_64 ||
magic == MACHO_TYPES::MAGIC_FAT ||
magic == MACHO_TYPES::CIGAM_FAT ||
magic == MACHO_TYPES::NEURAL_MODEL);
}
return false;
}
bool is_macho(const std::string& file) {
if (auto stream = FileStream::from_file(file)) {
return is_macho(*stream);
}
return false;
}
bool is_macho(const std::vector<uint8_t>& raw) {
if (auto stream = SpanStream::from_vector(raw)) {
return is_macho(*stream);
}
return false;
}
bool is_fat(const std::string& file) {
if (auto stream = FileStream::from_file(file)) {
if (auto magic_res = magic_from_stream(*stream)) {
const MACHO_TYPES magic = *magic_res;
return magic == MACHO_TYPES::MAGIC_FAT ||
magic == MACHO_TYPES::CIGAM_FAT;
}
}
return false;
}
bool is_64(BinaryStream& stream) {
ScopedStream scoped(stream, 0);
if (auto magic_res = magic_from_stream(*scoped)) {
const MACHO_TYPES magic = *magic_res;
return magic == MACHO_TYPES::MAGIC_64 ||
magic == MACHO_TYPES::CIGAM_64;
}
return false;
}
bool is_64(const std::string& file) {
if (auto stream = FileStream::from_file(file)) {
return is_64(*stream);
}
return false;
}
template<class MACHO_T>
void foreach_segment_impl(BinaryStream& stream, const segment_callback_t cbk) {
using header_t = typename MACHO_T::header;
using segment_command_t = typename MACHO_T::segment_command;
auto res_hdr = stream.read<header_t>();
if (!res_hdr) {
return;
}
const auto& hdr = *res_hdr;
for (size_t i = 0; i < hdr.ncmds; ++i) {
const auto raw_cmd = stream.peek<details::load_command>();
if (!raw_cmd) {
break;
}
const auto cmd = LoadCommand::TYPE(raw_cmd->cmd);
const bool is_segment = cmd == LoadCommand::TYPE::SEGMENT ||
cmd == LoadCommand::TYPE::SEGMENT_64;
if (is_segment) {
auto res_segment = stream.peek<segment_command_t>();
if (!res_segment) {
break;
}
std::string segname(res_segment->segname, 16);
cbk(segname, res_segment->fileoff, res_segment->filesize,
res_segment->vmaddr, res_segment->vmsize);
}
stream.increment_pos(raw_cmd->cmdsize);
}
}
void foreach_segment(BinaryStream& stream, const segment_callback_t cbk) {
ScopedStream scoped(stream);
auto magic_res = magic_from_stream(*scoped, /*keep_offset=*/true);
if (!magic_res) {
return;
}
const MACHO_TYPES magic = *magic_res;
if (magic == MACHO_TYPES::MAGIC_FAT || magic == MACHO_TYPES::CIGAM_FAT) {
LIEF_WARN("Can't get the file size of a FAT Macho-O");
return;
}
const bool is64 = magic == MACHO_TYPES::MAGIC_64 ||
magic == MACHO_TYPES::CIGAM_64;
const bool is32 = magic == MACHO_TYPES::MAGIC ||
magic == MACHO_TYPES::CIGAM;
if (!is64 && !is32) {
return;
}
return is64 ? foreach_segment_impl<details::MachO64>(*scoped, cbk) :
foreach_segment_impl<details::MachO32>(*scoped, cbk);
}
}
}