LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomasliefuthp
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 <ostream>
19#include <string>
20#include <vector>
21
22#include "LIEF/visibility.h"
23#include "LIEF/span.hpp"
24
25#include "LIEF/errors.hpp"
26
27namespace LIEF {
28inline uint64_t align(uint64_t value, uint64_t align_on) {
29 if (align_on == 0) {
30 return value;
31 }
32 const auto r = value % align_on;
33 if (r > 0) {
34 return value + (align_on - r);
35 }
36 return value;
37}
38
39inline uint64_t align_down(uint64_t value, uint64_t align_on) {
40 if (align_on == 0) {
41 return value;
42 }
43 const auto r = value % align_on;
44 if (r > 0) {
45 return value - r;
46 }
47 return value;
48}
49
50inline uint64_t align_with_offset(uint64_t value, uint64_t align_on,
51 uint64_t offset) {
52 return align(value - offset, align_on) + offset;
53}
54
55// Align up with alignment being a power of 2
56template<class T>
57constexpr T align_up(T value, T alignment) noexcept {
58 return (value + (alignment - 1)) & ~(alignment - 1);
59}
60
61template<typename T>
62inline constexpr T round(T x) {
63 return static_cast<T>(round<uint64_t>(x));
64}
65
66
67template<>
68inline uint64_t round<uint64_t>(uint64_t x) {
69 // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
70 x--;
71 x |= x >> 1; // handle 2 bit numbers
72 x |= x >> 2; // handle 4 bit numbers
73 x |= x >> 4; // handle 8 bit numbers
74 x |= x >> 8; // handle 16 bit numbers
75 x |= x >> 16; // handle 32 bit numbers
76 x |= x >> 32; // handle 64 bit numbers
77 x++;
78 return x;
79}
80
81
82constexpr size_t operator""_KB(unsigned long long kbs) {
83 return 1024LLU * kbs;
84}
85
86constexpr size_t operator""_MB(unsigned long long mbs) {
87 return 1024LLU * 1024LLU * mbs;
88}
89
90constexpr size_t operator""_GB(unsigned long long gbs) {
91 return 1024LLU * 1024LLU * 1024LLU * gbs;
92}
93
95 uint64_t major = 0;
96 uint64_t minor = 0;
97 uint64_t patch = 0;
98 uint64_t id = 0;
99
100 LIEF_API std::string to_string() const;
101
102 friend LIEF_API std::ostream& operator<<(std::ostream& os,
103 const lief_version_t& version) {
104 os << version.to_string();
105 return os;
106 }
107};
108
109LIEF_API std::string u16tou8(const char16_t* buffer, size_t size,
110 bool remove_null_char = false);
111
113inline std::string u16tou8(const std::u16string& string,
114 bool remove_null_char = false) {
115 return u16tou8(string.data(), string.size(), remove_null_char);
116}
117
119LIEF_API result<std::u16string> u8tou16(const std::string& string);
120
123
126
129
132
136LIEF_API result<std::string> demangle(const std::string& mangled);
137
150LIEF_API std::string dump(const uint8_t* buffer, size_t size,
151 const std::string& title = "",
152 const std::string& prefix = "", size_t limit = 0);
153
154inline std::string dump(span<const uint8_t> data, const std::string& title = "",
155 const std::string& prefix = "", size_t limit = 0) {
156 return dump(data.data(), data.size(), title, prefix, limit);
157}
158
159inline std::string dump(const std::vector<uint8_t>& data,
160 const std::string& title = "",
161 const std::string& prefix = "", size_t limit = 0) {
162 return dump(data.data(), data.size(), title, prefix, limit);
163}
164}
165
166
167#endif
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
LIEF namespace.
Definition Abstract/Binary.hpp:41
constexpr T align_up(T value, T alignment) noexcept
Definition utils.hpp:57
std::string extended_version_info()
Details about the extended version.
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:28
uint64_t align_with_offset(uint64_t value, uint64_t align_on, uint64_t offset)
Definition utils.hpp:50
tcb::span< ElementType, Extent > span
Definition span.hpp:22
uint64_t align_down(uint64_t value, uint64_t align_on)
Definition utils.hpp:39
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:68
std::string u16tou8(const char16_t *buffer, size_t size, bool remove_null_char=false)
result< std::string > demangle(const std::string &mangled)
Demangle the given input.
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:62
Definition utils.hpp:94
uint64_t major
Definition utils.hpp:95
uint64_t minor
Definition utils.hpp:96
uint64_t patch
Definition utils.hpp:97
std::string to_string() const
friend std::ostream & operator<<(std::ostream &os, const lief_version_t &version)
Definition utils.hpp:102
#define LIEF_API
Definition visibility.h:45