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 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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, uint64_t offset) {
51 return align(value - offset, align_on) + offset;
52}
53
54template<typename T>
55inline constexpr T round(T x) {
56 return static_cast<T>(round<uint64_t>(x));
57}
58
59
60template<>
61inline uint64_t round<uint64_t>(uint64_t x) {
62 //From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
63 x--;
64 x |= x >> 1; // handle 2 bit numbers
65 x |= x >> 2; // handle 4 bit numbers
66 x |= x >> 4; // handle 8 bit numbers
67 x |= x >> 8; // handle 16 bit numbers
68 x |= x >> 16; // handle 32 bit numbers
69 x |= x >> 32; // handle 64 bit numbers
70 x++;
71 return x;
72}
73
74
75constexpr size_t operator ""_KB(unsigned long long kbs)
76{
77 return 1024LLU * kbs;
78}
79
80constexpr size_t operator ""_MB(unsigned long long mbs)
81{
82 return 1024LLU * 1024LLU * mbs;
83}
84
85constexpr size_t operator ""_GB(unsigned long long gbs)
86{
87 return 1024LLU * 1024LLU * 1024LLU * gbs;
88}
89
91 uint64_t major = 0;
92 uint64_t minor = 0;
93 uint64_t patch = 0;
94 uint64_t id = 0;
95
96 LIEF_API std::string to_string() const;
97
98 friend LIEF_API
99 std::ostream& operator<<(std::ostream& os, const lief_version_t& version)
100 {
101 os << version.to_string();
102 return os;
103 }
104};
105
107LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);
108
110LIEF_API result<std::u16string> u8tou16(const std::string& string);
111
114
117
120
123
127LIEF_API result<std::string> demangle(const std::string& mangled);
128
141LIEF_API std::string dump(
142 const uint8_t* buffer, size_t size, const std::string& title = "",
143 const std::string& prefix = "", size_t limit = 0);
144
145inline std::string dump(span<const uint8_t> data, const std::string& title = "",
146 const std::string& prefix = "", size_t limit = 0)
147{
148 return dump(data.data(), data.size(), title, prefix, limit);
149}
150
151inline std::string dump(const std::vector<uint8_t>& data,
152 const std::string& title = "",
153 const std::string& prefix = "",
154 size_t limit = 0)
155{
156 return dump(data.data(), data.size(), title, prefix, limit);
157}
158}
159
160
161#endif
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
LIEF namespace.
Definition Abstract/Binary.hpp:40
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:61
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.
std::string u16tou8(const std::u16string &string, bool remove_null_char=false)
Convert a UTF-16 string to a UTF-8 one.
constexpr T round(T x)
Definition utils.hpp:55
Definition utils.hpp:90
uint64_t major
Definition utils.hpp:91
uint64_t minor
Definition utils.hpp:92
uint64_t patch
Definition utils.hpp:93
std::string to_string() const
friend std::ostream & operator<<(std::ostream &os, const lief_version_t &version)
Definition utils.hpp:99
#define LIEF_API
Definition visibility.h:41