LIEF: Library to Instrument Executable Formats Version 0.16.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
36
37template<typename T>
38inline constexpr T round(T x) {
39 return static_cast<T>(round<uint64_t>(x));
40}
41
42
43template<>
44inline uint64_t round<uint64_t>(uint64_t x) {
45 //From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
46 x--;
47 x |= x >> 1; // handle 2 bit numbers
48 x |= x >> 2; // handle 4 bit numbers
49 x |= x >> 4; // handle 8 bit numbers
50 x |= x >> 8; // handle 16 bit numbers
51 x |= x >> 16; // handle 32 bit numbers
52 x |= x >> 32; // handle 64 bit numbers
53 x++;
54 return x;
55}
56
57
58constexpr size_t operator ""_KB(unsigned long long kbs)
59{
60 return 1024LLU * kbs;
61}
62
63constexpr size_t operator ""_MB(unsigned long long mbs)
64{
65 return 1024LLU * 1024LLU * mbs;
66}
67
68constexpr size_t operator ""_GB(unsigned long long gbs)
69{
70 return 1024LLU * 1024LLU * 1024LLU * gbs;
71}
72LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);
75LIEF_API result<std::u16string> u8tou16(const std::string& string);
81LIEF_API result<std::string> demangle(const std::string& mangled);
86}
87
88
89#endif
LIEF namespace.
Definition Abstract/Binary.hpp:36
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:25
uint64_t round< uint64_t >(uint64_t x)
Definition utils.hpp:44
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:38
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