LIEF: Library to Instrument Executable Formats
Version 1.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
lief-install
x86_64
static
include
LIEF
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 <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
27
namespace
LIEF
{
28
inline
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
39
inline
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
50
inline
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
template
<
typename
T>
56
inline
constexpr
T
round
(T x) {
57
return
static_cast<
T
>
(
round<uint64_t>
(x));
58
}
59
60
61
template
<>
62
inline
uint64_t
round<uint64_t>
(uint64_t x) {
63
// From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
64
x--;
65
x |= x >> 1;
// handle 2 bit numbers
66
x |= x >> 2;
// handle 4 bit numbers
67
x |= x >> 4;
// handle 8 bit numbers
68
x |= x >> 8;
// handle 16 bit numbers
69
x |= x >> 16;
// handle 32 bit numbers
70
x |= x >> 32;
// handle 64 bit numbers
71
x++;
72
return
x;
73
}
74
75
76
constexpr
size_t
operator
""
_KB(
unsigned
long
long
kbs) {
77
return
1024LLU * kbs;
78
}
79
80
constexpr
size_t
operator
""
_MB(
unsigned
long
long
mbs) {
81
return
1024LLU * 1024LLU * mbs;
82
}
83
84
constexpr
size_t
operator
""
_GB(
unsigned
long
long
gbs) {
85
return
1024LLU * 1024LLU * 1024LLU * gbs;
86
}
87
88
struct
lief_version_t
{
89
uint64_t
major
= 0;
90
uint64_t
minor
= 0;
91
uint64_t
patch
= 0;
92
uint64_t
id
= 0;
93
94
LIEF_API
std::string
to_string
()
const
;
95
96
friend
LIEF_API
std::ostream&
operator<<
(std::ostream& os,
97
const
lief_version_t
&
version
) {
98
os <<
version
.to_string();
99
return
os;
100
}
101
};
102
103
LIEF_API
std::string
u16tou8
(
const
char16_t
* buffer,
size_t
size,
104
bool
remove_null_char =
false
);
105
107
inline
std::string
u16tou8
(
const
std::u16string&
string
,
108
bool
remove_null_char =
false
) {
109
return
u16tou8
(
string
.data(),
string
.size(), remove_null_char);
110
}
111
113
LIEF_API
result<std::u16string>
u8tou16
(
const
std::string&
string
);
114
116
LIEF_API
bool
is_extended
();
117
119
LIEF_API
std::string
extended_version_info
();
120
122
LIEF_API
lief_version_t
extended_version
();
123
125
LIEF_API
lief_version_t
version
();
126
130
LIEF_API
result<std::string>
demangle
(
const
std::string& mangled);
131
144
LIEF_API
std::string
dump
(
const
uint8_t* buffer,
size_t
size,
145
const
std::string& title =
""
,
146
const
std::string& prefix =
""
,
size_t
limit = 0);
147
148
inline
std::string
dump
(
span<const uint8_t>
data,
const
std::string& title =
""
,
149
const
std::string& prefix =
""
,
size_t
limit = 0) {
150
return
dump
(data.data(), data.size(), title, prefix, limit);
151
}
152
153
inline
std::string
dump
(
const
std::vector<uint8_t>& data,
154
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
}
159
160
161
#endif
LIEF::result
Wrapper that contains an Object (T) or an error.
Definition
errors.hpp:77
errors.hpp
LIEF
LIEF namespace.
Definition
Abstract/Binary.hpp:40
LIEF::extended_version_info
std::string extended_version_info()
Details about the extended version.
LIEF::align
uint64_t align(uint64_t value, uint64_t align_on)
Definition
utils.hpp:28
LIEF::align_with_offset
uint64_t align_with_offset(uint64_t value, uint64_t align_on, uint64_t offset)
Definition
utils.hpp:50
LIEF::span
tcb::span< ElementType, Extent > span
Definition
span.hpp:22
LIEF::align_down
uint64_t align_down(uint64_t value, uint64_t align_on)
Definition
utils.hpp:39
LIEF::dump
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
lief_version_t version()
Return the current version.
LIEF::round< uint64_t >
uint64_t round< uint64_t >(uint64_t x)
Definition
utils.hpp:62
LIEF::u16tou8
std::string u16tou8(const char16_t *buffer, size_t size, bool remove_null_char=false)
LIEF::demangle
result< std::string > demangle(const std::string &mangled)
Demangle the given input.
LIEF::u8tou16
result< std::u16string > u8tou16(const std::string &string)
Convert a UTF-8 string to a UTF-16 one.
LIEF::is_extended
bool is_extended()
Whether this version of LIEF includes extended features.
LIEF::extended_version
lief_version_t extended_version()
Return the extended version.
LIEF::round
constexpr T round(T x)
Definition
utils.hpp:56
span.hpp
LIEF::lief_version_t
Definition
utils.hpp:88
LIEF::lief_version_t::major
uint64_t major
Definition
utils.hpp:89
LIEF::lief_version_t::minor
uint64_t minor
Definition
utils.hpp:90
LIEF::lief_version_t::patch
uint64_t patch
Definition
utils.hpp:91
LIEF::lief_version_t::to_string
std::string to_string() const
LIEF::lief_version_t::operator<<
friend std::ostream & operator<<(std::ostream &os, const lief_version_t &version)
Definition
utils.hpp:96
visibility.h
LIEF_API
#define LIEF_API
Definition
visibility.h:45
Generated by
1.17.0