Arion 1.0.2-alpha
A high-performance C++ framework for emulating executable binaries.
 
Loading...
Searching...
No Matches
convert_utils.hpp
Go to the documentation of this file.
1#ifndef ARION_CONVERT_UTILS_HPP
2#define ARION_CONVERT_UTILS_HPP
3
4#include <algorithm>
6#include <arpa/inet.h>
7#include <cxxabi.h>
8#include <iomanip>
9#include <memory>
10#include <netinet/in.h>
11#include <sstream>
12#include <string>
13
14namespace arion
15{
16
22std::string inline str_to_uppercase(const std::string &input)
23{
24 std::string result = input;
25 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
26 return result;
27}
28
34std::string inline str_to_lowercase(const std::string &input)
35{
36 std::string result = input;
37 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
38 return result;
39}
40
50template <typename T> inline std::string int_to_hex(T value, uint8_t padding = 0, bool prefix = true)
51{
52 static_assert(std::is_integral<T>::value, "T must be an integral type.");
53
54 std::stringstream stream;
55 if (prefix)
56 stream << "0x";
57 if (padding)
58 stream << std::setfill('0') << std::setw(padding);
59 stream << std::hex << +value;
60 return stream.str();
61}
62
68std::string inline prot_flags_to_str(PROT_FLAGS flags)
69{
70 const std::string generic_flags = "rwx";
71 std::stringstream ss;
72
73 for (uint8_t bit_i = 0; bit_i < 3; bit_i++)
74 {
75 if ((flags >> (2 - bit_i)) & 1)
76 ss << generic_flags[bit_i];
77 else
78 ss << "-";
79 }
80
81 return ss.str();
82}
83
89std::string inline demangle_cxx_symbol(std::string mangled_name)
90{
91 int status = 0;
92 std::unique_ptr<char, decltype(&std::free)> demangled(
93 abi::__cxa_demangle(mangled_name.c_str(), nullptr, nullptr, &status), &std::free);
94 return (!status) ? demangled.get() : mangled_name;
95}
96
102std::string inline strip_str(const std::string &str)
103{
104 auto start = std::find_if_not(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); });
105
106 auto end = std::find_if_not(str.rbegin(), str.rend(), [](unsigned char c) { return std::isspace(c); }).base();
107
108 return (start < end ? std::string(start, end) : std::string());
109}
110
116std::string inline ipv4_addr_to_string(in_addr_t addr)
117{
118 char buf[INET_ADDRSTRLEN];
119 if (!inet_ntop(AF_INET, &addr, buf, INET_ADDRSTRLEN))
120 return std::string("");
121 return std::string(buf);
122}
123
129std::string inline ipv6_addr_to_string(const uint8_t addr[16])
130{
131 char buf[INET6_ADDRSTRLEN];
132 if (!inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN))
133 return std::string("");
134 return std::string(buf);
135}
136
142std::string inline name_from_path(const std::string &path)
143{
144 size_t pos = path.find_last_of('/');
145 if (pos != std::string::npos)
146 {
147 return path.substr(pos + 1);
148 }
149 return path;
150}
151
152}; // namespace arion
153
154#endif // ARION_CONVERT_UTILS_HPP
Definition arch_x86-64.hpp:11
std::string strip_str(const std::string &str)
Definition convert_utils.hpp:102
std::string ipv6_addr_to_string(const uint8_t addr[16])
Definition convert_utils.hpp:129
std::string ipv4_addr_to_string(in_addr_t addr)
Definition convert_utils.hpp:116
std::string name_from_path(const std::string &path)
Definition convert_utils.hpp:142
std::string int_to_hex(T value, uint8_t padding=0, bool prefix=true)
Definition convert_utils.hpp:50
std::string str_to_lowercase(const std::string &input)
Definition convert_utils.hpp:34
std::string demangle_cxx_symbol(std::string mangled_name)
Definition convert_utils.hpp:89
uint8_t PROT_FLAGS
Identifies memory protection rights.
Definition global_defs.hpp:38
std::string str_to_uppercase(const std::string &input)
Definition convert_utils.hpp:22
std::string prot_flags_to_str(PROT_FLAGS flags)
Definition convert_utils.hpp:68