LIEF: Library to Instrument Executable Formats Version 0.17.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
50template<typename T>
51inline constexpr T round(T x) {
52 return static_cast<T>(round<uint64_t>(x));
53}
54
55
56template<>
57inline uint64_t round<uint64_t>(uint64_t x) {
58 //From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
59 x--;
60 x |= x >> 1; // handle 2 bit numbers
61 x |= x >> 2; // handle 4 bit numbers
62 x |= x >> 4; // handle 8 bit numbers
63 x |= x >> 8; // handle 16 bit numbers
64 x |= x >> 16; // handle 32 bit numbers
65 x |= x >> 32; // handle 64 bit numbers
66 x++;
67 return x;
68}
69
70
71constexpr size_t operator ""_KB(unsigned long long kbs)
72{
73 return 1024LLU * kbs;
74}
75
76constexpr size_t operator ""_MB(unsigned long long mbs)
77{
78 return 1024LLU * 1024LLU * mbs;
79}
80
81constexpr size_t operator ""_GB(unsigned long long gbs)
82{
83 return 1024LLU * 1024LLU * 1024LLU * gbs;
84}
85
87 uint64_t major = 0;
88 uint64_t minor = 0;
89 uint64_t patch = 0;
90 uint64_t id = 0;
91
92 std::string to_string() const;
93
94 friend LIEF_API
95 std::ostream& operator<<(std::ostream& os, const lief_version_t& version)
96 {
97 os << version.to_string();
98 return os;
99 }
100};
101LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);
104LIEF_API result<std::u16string> u8tou16(const std::string& string);
107LIEF_API bool is_extended();
110LIEF_API std::string extended_version_info();
116LIEF_API result<std::string> demangle(const std::string& mangled);
121LIEF_API std::string dump(
135 const uint8_t* buffer, size_t size, const std::string& title = "",
136 const std::string& prefix = "", size_t limit = 0);
137
138inline std::string dump(span<const uint8_t> data, const std::string& title = "",
139 const std::string& prefix = "", size_t limit = 0)
140{
141 return dump(data.data(), data.size(), title, prefix, limit);
142}
143
144inline std::string dump(const std::vector<uint8_t>& data,
145 const std::string& title = "",
146 const std::string& prefix = "",
147 size_t limit = 0)
148{
149 return dump(data.data(), data.size(), title, prefix, limit);
150}
151}
152
153
154#endif
LIEF namespace.
Definition Abstract/Binary.hpp:39
std::string extended_version_info()
Details about the extended version.
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:28
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.
uint64_t round< uint64_t >(uint64_t x)
Definition utils.hpp:57
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:51
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
Definition utils.hpp:86
uint64_t major
Definition utils.hpp:87
uint64_t minor
Definition utils.hpp:88
uint64_t patch
Definition utils.hpp:89
std::string to_string() const
friend std::ostream & operator<<(std::ostream &os, const lief_version_t &version)
Definition utils.hpp:95
#define LIEF_API
Definition visibility.h:41