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 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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
20#include "LIEF/visibility.h"
21
22#include "LIEF/errors.hpp"
23
24namespace LIEF {
25inline uint64_t align(uint64_t value, uint64_t align_on) {
26 if (align_on == 0) {
27 return value;
28 }
29 const auto r = value % align_on;
30 if (r > 0) {
31 return value + (align_on - r);
32 }
33 return value;
34}
35
36inline uint64_t align_down(uint64_t value, uint64_t align_on) {
37 if (align_on == 0) {
38 return value;
39 }
40 const auto r = value % align_on;
41 if (r > 0) {
42 return value - r;
43 }
44 return value;
45}
46
47template<typename T>
48inline constexpr T round(T x) {
49 return static_cast<T>(round<uint64_t>(x));
50}
51
52
53template<>
54inline uint64_t round<uint64_t>(uint64_t x) {
55 //From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
56 x--;
57 x |= x >> 1; // handle 2 bit numbers
58 x |= x >> 2; // handle 4 bit numbers
59 x |= x >> 4; // handle 8 bit numbers
60 x |= x >> 8; // handle 16 bit numbers
61 x |= x >> 16; // handle 32 bit numbers
62 x |= x >> 32; // handle 64 bit numbers
63 x++;
64 return x;
65}
66
67
68constexpr size_t operator ""_KB(unsigned long long kbs)
69{
70 return 1024LLU * kbs;
71}
72
73constexpr size_t operator ""_MB(unsigned long long mbs)
74{
75 return 1024LLU * 1024LLU * mbs;
76}
77
78constexpr size_t operator ""_GB(unsigned long long gbs)
79{
80 return 1024LLU * 1024LLU * 1024LLU * gbs;
81}
82LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);
85LIEF_API result<std::u16string> u8tou16(const std::string& string);
94LIEF_API result<std::string> demangle(const std::string& mangled);
99}
100
101
102#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:25
uint64_t align_down(uint64_t value, uint64_t align_on)
Definition utils.hpp:36
uint64_t round< uint64_t >(uint64_t x)
Definition utils.hpp:54
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:48
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