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 <string>
19#include <vector>
20
21#include "LIEF/visibility.h"
22#include "LIEF/span.hpp"
23
24#include "LIEF/errors.hpp"
25
26namespace LIEF {
27inline uint64_t align(uint64_t value, uint64_t align_on) {
28 if (align_on == 0) {
29 return value;
30 }
31 const auto r = value % align_on;
32 if (r > 0) {
33 return value + (align_on - r);
34 }
35 return value;
36}
37
38inline uint64_t align_down(uint64_t value, uint64_t align_on) {
39 if (align_on == 0) {
40 return value;
41 }
42 const auto r = value % align_on;
43 if (r > 0) {
44 return value - r;
45 }
46 return value;
47}
48
49template<typename T>
50inline constexpr T round(T x) {
51 return static_cast<T>(round<uint64_t>(x));
52}
53
54
55template<>
56inline uint64_t round<uint64_t>(uint64_t x) {
57 //From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
58 x--;
59 x |= x >> 1; // handle 2 bit numbers
60 x |= x >> 2; // handle 4 bit numbers
61 x |= x >> 4; // handle 8 bit numbers
62 x |= x >> 8; // handle 16 bit numbers
63 x |= x >> 16; // handle 32 bit numbers
64 x |= x >> 32; // handle 64 bit numbers
65 x++;
66 return x;
67}
68
69
70constexpr size_t operator ""_KB(unsigned long long kbs)
71{
72 return 1024LLU * kbs;
73}
74
75constexpr size_t operator ""_MB(unsigned long long mbs)
76{
77 return 1024LLU * 1024LLU * mbs;
78}
79
80constexpr size_t operator ""_GB(unsigned long long gbs)
81{
82 return 1024LLU * 1024LLU * 1024LLU * gbs;
83}
84LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);
87LIEF_API result<std::u16string> u8tou16(const std::string& string);
96LIEF_API result<std::string> demangle(const std::string& mangled);
101LIEF_API std::string dump(
115 const uint8_t* buffer, size_t size, const std::string& title = "",
116 const std::string& prefix = "", size_t limit = 0);
117
118inline std::string dump(span<const uint8_t> data, const std::string& title = "",
119 const std::string& prefix = "", size_t limit = 0)
120{
121 return dump(data.data(), data.size(), title, prefix, limit);
122}
123
124inline std::string dump(const std::vector<uint8_t>& data,
125 const std::string& title = "",
126 const std::string& prefix = "",
127 size_t limit = 0)
128{
129 return dump(data.data(), data.size(), title, prefix, limit);
130}
131}
132
133
134#endif
LIEF namespace.
Definition Abstract/Binary.hpp:36
std::string extended_version_info()
Details about the extended version.
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:27
tcb::span< ElementType, Extent > span
Definition span.hpp:22
uint64_t align_down(uint64_t value, uint64_t align_on)
Definition utils.hpp:38
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:56
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.
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:50
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
#define LIEF_API
Definition visibility.h:41