LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_UTILS_HEADER
17#define LIEF_UTILS_HEADER
18#include <string_view>
19#include <ostream>
20#include <string>
21#include <vector>
22
23#include "LIEF/span.hpp"
24#include "LIEF/visibility.h"
25
26#include "LIEF/errors.hpp"
27
28namespace LIEF {
29inline uint64_t align(uint64_t value, uint64_t align_on) {
30 if (align_on == 0) {
31 return value;
32 }
33 const auto r = value % align_on;
34 if (r > 0) {
35 return value + (align_on - r);
36 }
37 return value;
38}
39
40inline uint64_t align_down(uint64_t value, uint64_t align_on) {
41 if (align_on == 0) {
42 return value;
43 }
44 const auto r = value % align_on;
45 if (r > 0) {
46 return value - r;
47 }
48 return value;
49}
50
51inline uint64_t align_with_offset(uint64_t value, uint64_t align_on,
52 uint64_t offset) {
53 return align(value - offset, align_on) + offset;
54}
55
56// Align up with alignment being a power of 2
57template<class T>
58constexpr T align_up(T value, T alignment) noexcept {
59 return (value + (alignment - 1)) & ~(alignment - 1);
60}
61
62template<typename T>
63inline constexpr T round(T x) {
64 return static_cast<T>(round<uint64_t>(x));
65}
66
67
68template<>
69inline uint64_t round<uint64_t>(uint64_t x) {
70 // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
71 x--;
72 x |= x >> 1; // handle 2 bit numbers
73 x |= x >> 2; // handle 4 bit numbers
74 x |= x >> 4; // handle 8 bit numbers
75 x |= x >> 8; // handle 16 bit numbers
76 x |= x >> 16; // handle 32 bit numbers
77 x |= x >> 32; // handle 64 bit numbers
78 x++;
79 return x;
80}
81
82
83constexpr size_t operator""_KB(unsigned long long kbs) {
84 return 1024LLU * kbs;
85}
86
87constexpr size_t operator""_MB(unsigned long long mbs) {
88 return 1024LLU * 1024LLU * mbs;
89}
90
91constexpr size_t operator""_GB(unsigned long long gbs) {
92 return 1024LLU * 1024LLU * 1024LLU * gbs;
93}
94
96 uint64_t major = 0;
97 uint64_t minor = 0;
98 uint64_t patch = 0;
99 uint64_t id = 0;
100
101 LIEF_API std::string to_string() const;
102
103 friend LIEF_API std::ostream& operator<<(std::ostream& os,
104 const lief_version_t& version) {
105 os << version.to_string();
106 return os;
107 }
108};
109
110LIEF_API std::string u16tou8(const char16_t* buffer, size_t size,
111 bool remove_null_char = false);
112
114inline std::string u16tou8(const std::u16string& string,
115 bool remove_null_char = false) {
116 return u16tou8(string.data(), string.size(), remove_null_char);
117}
118
120LIEF_API result<std::u16string> u8tou16(const std::string& string);
121
124
127
130
133
137LIEF_API result<std::string> demangle(std::string_view mangled);
138
151LIEF_API std::string dump(const uint8_t* buffer, size_t size,
152 const std::string& title = "",
153 const std::string& prefix = "", size_t limit = 0);
154
155inline std::string dump(span<const uint8_t> data, const std::string& title = "",
156 const std::string& prefix = "", size_t limit = 0) {
157 return dump(data.data(), data.size(), title, prefix, limit);
158}
159
160inline std::string dump(const std::vector<uint8_t>& data,
161 const std::string& title = "",
162 const std::string& prefix = "", size_t limit = 0) {
163 return dump(data.data(), data.size(), title, prefix, limit);
164}
165}
166
167
168#endif
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
LIEF namespace.
Definition Abstract/Binary.hpp:41
constexpr T align_up(T value, T alignment) noexcept
Definition utils.hpp:58
result< std::string > demangle(std::string_view mangled)
Demangle the given input.
std::string extended_version_info()
Details about the extended version.
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:29
uint64_t align_with_offset(uint64_t value, uint64_t align_on, uint64_t offset)
Definition utils.hpp:51
tcb::span< ElementType, Extent > span
Definition span.hpp:22
uint64_t align_down(uint64_t value, uint64_t align_on)
Definition utils.hpp:40
std::string dump(const uint8_t *buffer, size_t size, const std::string &title="", const std::string &prefix="", size_t limit=0)
Hexdump the provided buffer.
lief_version_t version()
Return the current version.
uint64_t round< uint64_t >(uint64_t x)
Definition utils.hpp:69
std::string u16tou8(const char16_t *buffer, size_t size, bool remove_null_char=false)
result< std::u16string > u8tou16(const std::string &string)
Convert a UTF-8 string to a UTF-16 one.
bool is_extended()
Whether this version of LIEF includes extended features.
lief_version_t extended_version()
Return the extended version.
constexpr T round(T x)
Definition utils.hpp:63
Definition utils.hpp:95
uint64_t major
Definition utils.hpp:96
uint64_t minor
Definition utils.hpp:97
uint64_t patch
Definition utils.hpp:98
std::string to_string() const
friend std::ostream & operator<<(std::ostream &os, const lief_version_t &version)
Definition utils.hpp:103
#define LIEF_API
Definition visibility.h:45