forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal_utils.cpp
More file actions
99 lines (88 loc) · 2.62 KB
/
Copy pathinternal_utils.cpp
File metadata and controls
99 lines (88 loc) · 2.62 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
/* 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 "internal_utils.hpp"
#include <ctime>
#include <chrono>
namespace LIEF {
std::string printable_string(const std::string& str) {
std::string out;
out.reserve(str.size());
for (char c : str) {
if (is_printable(c)) {
out += c;
}
}
return out;
}
template<class T>
std::string hex_dump_impl(T data, const std::string& sep) {
std::vector<std::string> hexdigits;
hexdigits.reserve(data.size());
std::transform(data.begin(), data.end(), std::back_inserter(hexdigits),
[] (uint8_t x) { return fmt::format("{:02x}", x); });
return fmt::to_string(fmt::join(hexdigits, sep));
}
std::string hex_dump(const std::vector<uint8_t>& data, const std::string& sep) {
return hex_dump_impl(data, sep);
}
std::string hex_dump(span<const uint8_t> data, const std::string& sep) {
return hex_dump_impl(data, sep);
}
inline std::string pretty_hex(char c) {
if (is_printable(c)) {
return std::string("") + c;
}
return ".";
}
std::vector<std::string> split(const std::string& input, char c = '\n') {
// Not really efficient but does not aim to
std::stringstream strm(input);
std::vector<std::string> out;
std::string element;
while (std::getline(strm, element, c)) {
out.push_back(element);
}
return out;
}
std::string indent(const std::string& input, size_t level) {
if (input.empty()) {
return "";
}
std::vector<std::string> lines = split(input);
std::string output;
output.reserve(input.size());
for (const std::string& line : lines) {
if (line.empty()) {
output += '\n';
continue;
}
output += std::string(level, ' ') + line + '\n';
}
if (output.empty()) {
output.pop_back();
}
return output;
}
std::string ts_to_str(uint64_t timestamp) {
using namespace fmt;
using namespace std::chrono;
system_clock::time_point tp = system_clock::time_point(std::chrono::seconds(timestamp));
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::string ts = std::ctime(&t);
ts.resize(ts.size() - 1);
return ts;
}
}