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
192 lines (161 loc) · 4.87 KB
/
Copy pathutils.cpp
File metadata and controls
192 lines (161 loc) · 4.87 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* 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 <algorithm>
#include <iterator>
#include <string>
#include <spdlog/fmt/fmt.h>
#include "LIEF/utils.hpp"
#include "LIEF/errors.hpp"
#include "LIEF/version.h"
#include "third-party/utfcpp.hpp"
#include "LIEF/config.h"
#include "logging.hpp"
#include "messages.hpp"
#include "internal_utils.hpp"
namespace LIEF {
template <typename octet_iterator>
result<uint32_t> next(octet_iterator& it, octet_iterator end) {
using namespace utf8;
utfchar32_t cp = 0;
internal::utf_error err_code = internal::validate_next(it, end, cp);
switch (err_code) {
case internal::UTF8_OK :
break;
case internal::NOT_ENOUGH_ROOM :
return make_error_code(lief_errors::data_too_large);
case internal::INVALID_LEAD :
case internal::INCOMPLETE_SEQUENCE :
case internal::OVERLONG_SEQUENCE :
return make_error_code(lief_errors::read_error);
case internal::INVALID_CODE_POINT :
return make_error_code(lief_errors::read_error);
}
return cp;
}
std::string u16tou8(const std::u16string& string, bool remove_null_char) {
std::string name;
std::u16string clean_string;
std::copy_if(std::begin(string), std::end(string),
std::back_inserter(clean_string),
utf8::internal::is_code_point_valid);
utf8::unchecked::utf16to8(std::begin(clean_string), std::end(clean_string),
std::back_inserter(name));
if (remove_null_char) {
return std::string{name.c_str()};
}
return name;
}
result<std::u16string> u8tou16(const std::string& string) {
std::u16string name;
auto start = string.begin();
auto end = string.end();
auto res = std::back_inserter(name);
while (start < end) {
auto cp = next(start, end);
if (!cp) {
return make_error_code(lief_errors::conversion_error);
}
uint32_t cp_val = *cp;
if (cp_val > 0xffff) {
*res++ = static_cast<uint16_t>((cp_val >> 10) + utf8::internal::LEAD_OFFSET);
*res++ = static_cast<uint16_t>((cp_val & 0x3ff) + utf8::internal::TRAIL_SURROGATE_MIN);
} else {
*res++ = static_cast<uint16_t>(cp_val);
}
}
return name;
}
inline std::string pretty_hex(char c) {
if (is_printable(c)) {
return std::string("") + c;
}
return ".";
}
std::string dump(const uint8_t* buffer, size_t size, const std::string& title,
const std::string& prefix, size_t limit)
{
if (size < 2) {
return "";
}
std::string out;
std::string banner;
if (!title.empty()) {
banner = prefix + "+" + std::string(22 * 3, '-') + "---+" + "\n" + prefix + "| ";
banner += title;
if (title.size() < 68) {
banner += std::string(68 - title.size(), ' ');
}
banner += "|\n";
}
out = std::string(22 * 3, '-') + "\n";
std::string lhs, rhs;
if (limit > 0) {
size = std::min<size_t>(size, limit);
}
for (size_t i = 0; i < size; ++i) {
if (i == 0) {
out = prefix + "+" + std::string(22 * 3, '-') + "---+" + "\n" + prefix + "| ";
}
if (i > 0 && i % 16 == 0) {
out += "\n" + prefix + "| ";
}
rhs += pretty_hex((char)(buffer[i]));
out += fmt::format("{:02x} ", buffer[i]);
if (i % 16 == 15 || i == (size - 1)) {
if (i == (size - 1)) {
out += std::string(((16 - ((size - 1) % 16) - 1)) * 3, ' ');
rhs += std::string(((16 - ((size - 1) % 16) - 1)) * 1, ' ');
}
out += " | ";
out += rhs + " |";
rhs = "";
}
}
out += std::string("\n") + prefix + '+' + std::string(22 * 3, '-') + "---+";
return banner + out;
}
bool is_extended() {
return lief_extended;
}
std::string lief_version_t::to_string() const {
if (id == 0) {
return fmt::format("{}.{}.{}", major, minor, patch);
}
return fmt::format("{}.{}.{}.{}", major, minor, patch, id);
}
lief_version_t version() {
if (lief_extended) {
return extended_version();
}
return {LIEF_VERSION_MAJOR, LIEF_VERSION_MINOR, LIEF_VERSION_PATCH, 0};
}
#if !defined(LIEF_EXTENDED)
result<std::string> demangle(const std::string&/*mangled*/) {
logging::needs_lief_extended();
return make_error_code(lief_errors::require_extended_version);
}
#endif
#if !defined(LIEF_EXTENDED)
std::string extended_version_info() {
return "";
}
#endif
#if !defined(LIEF_EXTENDED)
lief_version_t extended_version() {
return {};
}
#endif
} // namespace LIEF