LIEF: Library to Instrument Executable Formats Version 0.15.1
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 <vector>
19#include <string>
20
21#include "LIEF/span.hpp"
22#include "LIEF/types.hpp"
23#include "LIEF/visibility.h"
24
25#include "LIEF/errors.hpp"
26
27
28namespace LIEF {
29inline uint64_t align(uint64_t value, uint64_t align_on) {
30 if (align_on == 0) {
31 return value;
32 }
33 const auto r = value % align_on;
34 if (r > 0) {
35 return value + (align_on - r);
36 }
37 return value;
38}
39
40
41template<typename T>
42inline constexpr T round(T x) {
43 return static_cast<T>(round<uint64_t>(x));
44}
45
46
47template<>
48inline uint64_t round<uint64_t>(uint64_t x) {
49 //From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
50 x--;
51 x |= x >> 1; // handle 2 bit numbers
52 x |= x >> 2; // handle 4 bit numbers
53 x |= x >> 4; // handle 8 bit numbers
54 x |= x >> 8; // handle 16 bit numbers
55 x |= x >> 16; // handle 32 bit numbers
56 x |= x >> 32; // handle 64 bit numbers
57 x++;
58 return x;
59}
60
61
62constexpr size_t operator ""_KB(unsigned long long kbs)
63{
64 return 1024LLU * kbs;
65}
66
67constexpr size_t operator ""_MB(unsigned long long mbs)
68{
69 return 1024LLU * 1024LLU * mbs;
70}
71
72constexpr size_t operator ""_GB(unsigned long long gbs)
73{
74 return 1024LLU * 1024LLU * 1024LLU * gbs;
75}
76
77
79LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);
80
82LIEF_API result<std::u16string> u8tou16(const std::string& string);
83
84LIEF_API std::string hex_str(uint8_t c);
85
86LIEF_API std::string hex_dump(const std::vector<uint8_t>& data,
87 const std::string& sep = ":");
88
90 const std::string& sep = ":");
91
93LIEF_API bool is_hex_number(const std::string& nb);
94
97}
98
99namespace LIEF {
100namespace LEB128 {
101std::vector<uint8_t> uencode(uint64_t value);
102}
103}
104
105#endif
std::vector< uint8_t > uencode(uint64_t value)
LIEF namespace.
Definition Abstract/Binary.hpp:32
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:29
tcb::span< ElementType, Extent > span
Definition span.hpp:22
uint64_t round< uint64_t >(uint64_t x)
Definition utils.hpp:48
std::string hex_str(uint8_t c)
std::string hex_dump(const std::vector< uint8_t > &data, const std::string &sep=":")
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:42
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74
bool is_hex_number(const std::string &nb)
Check if the given number is a hex-like string.
#define LIEF_API
Definition visibility.h:41