2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-29 15:09:36 +01:00
|
|
|
#include <cstdio>
|
2020-11-11 00:13:09 +01:00
|
|
|
#include <codecvt>
|
2021-03-01 08:56:49 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
|
|
|
#include <imgui_internal.h>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-12-10 16:10:34 +01:00
|
|
|
#include <windows.h>
|
2022-01-24 20:53:17 +01:00
|
|
|
#elif defined(OS_LINUX)
|
2021-12-13 22:58:23 +01:00
|
|
|
#include <unistd.h>
|
2022-01-24 20:53:17 +01:00
|
|
|
#elif defined(OS_MACOS)
|
2022-06-25 12:19:59 +02:00
|
|
|
#include <hex/helpers/utils_macos.hpp>
|
|
|
|
#include <unistd.h>
|
2021-12-10 16:10:34 +01:00
|
|
|
#endif
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-09-30 12:49:28 +02:00
|
|
|
float operator""_scaled(long double value) {
|
2022-02-01 18:09:40 +01:00
|
|
|
return value * ImHexApi::System::getGlobalScale();
|
2022-01-11 23:48:18 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:49:28 +02:00
|
|
|
float operator""_scaled(unsigned long long value) {
|
2022-02-01 18:09:40 +01:00
|
|
|
return value * ImHexApi::System::getGlobalScale();
|
2022-01-11 23:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImVec2 scaled(const ImVec2 &vector) {
|
2022-02-01 18:09:40 +01:00
|
|
|
return vector * ImHexApi::System::getGlobalScale();
|
2022-01-11 23:48:18 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
std::string to_string(u128 value) {
|
|
|
|
char data[45] = { 0 };
|
|
|
|
|
|
|
|
u8 index = sizeof(data) - 2;
|
|
|
|
while (value != 0 && index != 0) {
|
|
|
|
data[index] = '0' + value % 10;
|
|
|
|
value /= 10;
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
2022-03-04 20:52:39 +01:00
|
|
|
return { data + index + 1 };
|
2021-01-27 00:00:20 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
std::string to_string(i128 value) {
|
2021-01-27 00:00:20 +01:00
|
|
|
char data[45] = { 0 };
|
|
|
|
|
|
|
|
u128 unsignedValue = value < 0 ? -value : value;
|
|
|
|
|
|
|
|
u8 index = sizeof(data) - 2;
|
|
|
|
while (unsignedValue != 0 && index != 0) {
|
|
|
|
data[index] = '0' + unsignedValue % 10;
|
|
|
|
unsignedValue /= 10;
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
data[index] = '-';
|
2022-03-04 20:52:39 +01:00
|
|
|
return { data + index };
|
2021-01-27 00:00:20 +01:00
|
|
|
} else
|
2022-03-04 20:52:39 +01:00
|
|
|
return { data + index + 1 };
|
2021-01-27 00:00:20 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
std::string toByteString(u64 bytes) {
|
|
|
|
double value = bytes;
|
|
|
|
u8 unitIndex = 0;
|
|
|
|
|
|
|
|
while (value > 1024) {
|
|
|
|
value /= 1024;
|
|
|
|
unitIndex++;
|
|
|
|
|
|
|
|
if (unitIndex == 6)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:29:20 +02:00
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if (unitIndex == 0)
|
|
|
|
result = hex::format("{0:}", value);
|
|
|
|
else
|
|
|
|
result = hex::format("{0:.2f}", value);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
switch (unitIndex) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case 0:
|
2022-06-30 11:29:20 +02:00
|
|
|
result += ((value == 1) ? " Byte" : " Bytes");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2022-06-30 11:43:40 +02:00
|
|
|
result += " kiB";
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2022-06-30 11:43:40 +02:00
|
|
|
result += " MiB";
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2022-06-30 11:43:40 +02:00
|
|
|
result += " GiB";
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2022-06-30 11:43:40 +02:00
|
|
|
result += " TiB";
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case 5:
|
2022-06-30 11:43:40 +02:00
|
|
|
result += " PiB";
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case 6:
|
2022-06-30 11:43:40 +02:00
|
|
|
result += " EiB";
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = "A lot!";
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:04:05 +01:00
|
|
|
std::string makeStringPrintable(const std::string &string) {
|
|
|
|
std::string result;
|
|
|
|
for (char c : string) {
|
|
|
|
if (std::isprint(c))
|
|
|
|
result += c;
|
|
|
|
else
|
|
|
|
result += hex::format("\\x{0:02X}", u8(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:12:46 +02:00
|
|
|
std::string makePrintable(u8 c) {
|
2020-11-27 09:09:48 +01:00
|
|
|
switch (c) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case 0:
|
|
|
|
return "NUL";
|
|
|
|
case 1:
|
|
|
|
return "SOH";
|
|
|
|
case 2:
|
|
|
|
return "STX";
|
|
|
|
case 3:
|
|
|
|
return "ETX";
|
|
|
|
case 4:
|
|
|
|
return "EOT";
|
|
|
|
case 5:
|
|
|
|
return "ENQ";
|
|
|
|
case 6:
|
|
|
|
return "ACK";
|
|
|
|
case 7:
|
|
|
|
return "BEL";
|
|
|
|
case 8:
|
|
|
|
return "BS";
|
|
|
|
case 9:
|
|
|
|
return "TAB";
|
|
|
|
case 10:
|
|
|
|
return "LF";
|
|
|
|
case 11:
|
|
|
|
return "VT";
|
|
|
|
case 12:
|
|
|
|
return "FF";
|
|
|
|
case 13:
|
|
|
|
return "CR";
|
|
|
|
case 14:
|
|
|
|
return "SO";
|
|
|
|
case 15:
|
|
|
|
return "SI";
|
|
|
|
case 16:
|
|
|
|
return "DLE";
|
|
|
|
case 17:
|
|
|
|
return "DC1";
|
|
|
|
case 18:
|
|
|
|
return "DC2";
|
|
|
|
case 19:
|
|
|
|
return "DC3";
|
|
|
|
case 20:
|
|
|
|
return "DC4";
|
|
|
|
case 21:
|
|
|
|
return "NAK";
|
|
|
|
case 22:
|
|
|
|
return "SYN";
|
|
|
|
case 23:
|
|
|
|
return "ETB";
|
|
|
|
case 24:
|
|
|
|
return "CAN";
|
|
|
|
case 25:
|
|
|
|
return "EM";
|
|
|
|
case 26:
|
|
|
|
return "SUB";
|
|
|
|
case 27:
|
|
|
|
return "ESC";
|
|
|
|
case 28:
|
|
|
|
return "FS";
|
|
|
|
case 29:
|
|
|
|
return "GS";
|
|
|
|
case 30:
|
|
|
|
return "RS";
|
|
|
|
case 31:
|
|
|
|
return "US";
|
|
|
|
case 32:
|
|
|
|
return "Space";
|
|
|
|
case 127:
|
|
|
|
return "DEL";
|
|
|
|
case 128 ... 255:
|
|
|
|
return " ";
|
|
|
|
default:
|
|
|
|
return std::string() + static_cast<char>(c);
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
std::vector<std::string> splitString(const std::string &string, const std::string &delimiter) {
|
2022-05-28 16:18:55 +02:00
|
|
|
size_t start = 0, end = 0;
|
2021-01-27 00:00:20 +01:00
|
|
|
std::string token;
|
|
|
|
std::vector<std::string> res;
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
while ((end = string.find(delimiter, start)) != std::string::npos) {
|
2022-05-28 16:18:55 +02:00
|
|
|
size_t size = end - start;
|
|
|
|
if (start + size > string.length())
|
|
|
|
break;
|
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
token = string.substr(start, end - start);
|
|
|
|
start = end + delimiter.length();
|
|
|
|
res.push_back(token);
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:49:37 +02:00
|
|
|
res.emplace_back(string.substr(start));
|
2021-01-27 00:00:20 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
std::string combineStrings(const std::vector<std::string> &strings, const std::string &delimiter) {
|
2021-04-12 20:49:37 +02:00
|
|
|
std::string result;
|
|
|
|
for (const auto &string : strings) {
|
|
|
|
result += string;
|
|
|
|
result += delimiter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.substr(0, result.length() - delimiter.length());
|
|
|
|
}
|
|
|
|
|
2022-10-21 10:03:37 +02:00
|
|
|
std::string replaceStrings(std::string string, const std::string &search, const std::string &replace) {
|
|
|
|
if (search.empty())
|
|
|
|
return string;
|
|
|
|
|
|
|
|
std::size_t pos;
|
|
|
|
while ((pos = string.find(search)) != std::string::npos)
|
|
|
|
string.replace(pos, search.size(), replace);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
std::string toEngineeringString(double value) {
|
2022-09-12 21:28:02 +02:00
|
|
|
constexpr static std::array Suffixes = { "a", "f", "p", "n", "u", "m", "", "k", "M", "G", "T", "P", "E" };
|
2021-01-27 00:00:20 +01:00
|
|
|
|
2021-02-17 14:47:25 +01:00
|
|
|
int8_t suffixIndex = 6;
|
2021-01-27 00:00:20 +01:00
|
|
|
|
2021-02-17 14:47:25 +01:00
|
|
|
while (suffixIndex != 0 && suffixIndex != 12 && (value >= 1000 || value < 1) && value != 0) {
|
2021-01-27 00:00:20 +01:00
|
|
|
if (value >= 1000) {
|
|
|
|
value /= 1000;
|
2021-02-17 14:47:25 +01:00
|
|
|
suffixIndex++;
|
2021-01-27 00:00:20 +01:00
|
|
|
} else if (value < 1) {
|
|
|
|
value *= 1000;
|
2021-02-17 14:47:25 +01:00
|
|
|
suffixIndex--;
|
2021-01-27 00:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 14:47:25 +01:00
|
|
|
return std::to_string(value).substr(0, 5) + Suffixes[suffixIndex];
|
2021-01-27 00:00:20 +01:00
|
|
|
}
|
|
|
|
|
2021-08-22 21:43:31 +02:00
|
|
|
void runCommand(const std::string &command) {
|
2021-01-27 00:00:20 +01:00
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
auto result = system(hex::format("start {0}", command).c_str());
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
auto result = system(hex::format("open {0}", command).c_str());
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
auto result = system(hex::format("xdg-open {0}", command).c_str());
|
|
|
|
#endif
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(result);
|
2021-08-22 21:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void openWebpage(std::string url) {
|
|
|
|
|
2021-09-10 21:42:35 +02:00
|
|
|
if (url.find("://") == std::string::npos)
|
2021-08-22 21:43:31 +02:00
|
|
|
url = "https://" + url;
|
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
ShellExecute(nullptr, "open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
openWebpageMacos(url.c_str());
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
auto result = system(hex::format("xdg-open {0}", url).c_str());
|
|
|
|
hex::unused(result);
|
|
|
|
#else
|
|
|
|
#warning "Unknown OS, can't open webpages"
|
|
|
|
#endif
|
2021-01-27 00:00:20 +01:00
|
|
|
}
|
|
|
|
|
2022-08-03 10:19:34 +02:00
|
|
|
std::optional<u8> hexCharToValue(char c) {
|
|
|
|
if (std::isdigit(c))
|
|
|
|
return c - '0';
|
|
|
|
else if (std::isxdigit(c))
|
|
|
|
return std::toupper(c) - 'A' + 0x0A;
|
|
|
|
else
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
std::string encodeByteString(const std::vector<u8> &bytes) {
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
for (u8 byte : bytes) {
|
|
|
|
if (std::isprint(byte) && byte != '\\')
|
|
|
|
result += char(byte);
|
|
|
|
else {
|
|
|
|
switch (byte) {
|
|
|
|
case '\\':
|
|
|
|
result += "\\";
|
|
|
|
break;
|
|
|
|
case '\a':
|
|
|
|
result += "\\a";
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
result += "\\b";
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
result += "\\f";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
result += "\\n";
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
result += "\\r";
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
result += "\\t";
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
result += "\\v";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result += hex::format("\\x{:02X}", byte);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> decodeByteString(const std::string &string) {
|
|
|
|
u32 offset = 0;
|
|
|
|
std::vector<u8> result;
|
|
|
|
|
|
|
|
while (offset < string.length()) {
|
|
|
|
auto c = [&] { return string[offset]; };
|
|
|
|
|
|
|
|
if (c() == '\\') {
|
2022-09-13 14:10:52 +02:00
|
|
|
if ((offset + 2) > string.length()) return {};
|
2022-02-17 14:43:04 +01:00
|
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
char escapeChar = c();
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
switch (escapeChar) {
|
|
|
|
case 'a':
|
|
|
|
result.push_back('\a');
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
result.push_back('\b');
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
result.push_back('\f');
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
result.push_back('\n');
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
result.push_back('\r');
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
result.push_back('\t');
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
result.push_back('\v');
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
result.push_back('\\');
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
{
|
|
|
|
u8 byte = 0x00;
|
|
|
|
if ((offset + 1) >= string.length()) return {};
|
|
|
|
|
|
|
|
for (u8 i = 0; i < 2; i++) {
|
|
|
|
byte <<= 4;
|
2022-08-03 10:19:34 +02:00
|
|
|
if (auto hexValue = hexCharToValue(c()); hexValue.has_value())
|
|
|
|
byte |= hexValue.value();
|
2022-02-17 14:43:04 +01:00
|
|
|
else
|
|
|
|
return {};
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back(byte);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push_back(c());
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-01 00:23:45 +02:00
|
|
|
float float16ToFloat32(u16 float16) {
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 sign = float16 >> 15;
|
2021-09-01 00:23:45 +02:00
|
|
|
u32 exponent = (float16 >> 10) & 0x1F;
|
|
|
|
u32 mantissa = float16 & 0x3FF;
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
u32 result = 0x00;
|
2021-09-01 00:23:45 +02:00
|
|
|
|
|
|
|
if (exponent == 0) {
|
|
|
|
if (mantissa == 0) {
|
|
|
|
// +- Zero
|
|
|
|
result = sign << 31;
|
|
|
|
} else {
|
|
|
|
// Subnormal value
|
|
|
|
exponent = 0x7F - 14;
|
|
|
|
|
|
|
|
while ((mantissa & (1 << 10)) == 0) {
|
|
|
|
exponent--;
|
|
|
|
mantissa <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mantissa &= 0x3FF;
|
|
|
|
result = (sign << 31) | (exponent << 23) | (mantissa << 13);
|
|
|
|
}
|
|
|
|
} else if (exponent == 0x1F) {
|
|
|
|
// +-Inf or +-NaN
|
|
|
|
result = (sign << 31) | (0xFF << 23) | (mantissa << 13);
|
|
|
|
} else {
|
|
|
|
// Normal value
|
|
|
|
result = (sign << 31) | ((exponent + (0x7F - 15)) << 23) | (mantissa << 13);
|
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
float floatResult = 0;
|
|
|
|
std::memcpy(&floatResult, &result, sizeof(float));
|
|
|
|
|
|
|
|
return floatResult;
|
2021-09-01 00:23:45 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 22:58:23 +01:00
|
|
|
bool isProcessElevated() {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
bool elevated = false;
|
2022-02-01 22:09:44 +01:00
|
|
|
HANDLE token = INVALID_HANDLE_VALUE;
|
2021-12-13 22:58:23 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) {
|
|
|
|
TOKEN_ELEVATION elevation;
|
|
|
|
DWORD elevationSize = sizeof(TOKEN_ELEVATION);
|
2021-12-13 22:58:23 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (::GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &elevationSize))
|
|
|
|
elevated = elevation.TokenIsElevated;
|
|
|
|
}
|
2021-12-13 22:58:23 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (token != INVALID_HANDLE_VALUE)
|
|
|
|
::CloseHandle(token);
|
2021-12-13 22:58:23 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return elevated;
|
2021-12-13 22:58:23 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#elif defined(OS_LINUX) || defined(OS_MACOS)
|
2022-03-27 00:01:28 +01:00
|
|
|
return getuid() == 0 || getuid() != geteuid();
|
2022-01-24 20:53:17 +01:00
|
|
|
#endif
|
2021-12-13 22:58:23 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:20:14 +01:00
|
|
|
std::optional<std::string> getEnvironmentVariable(const std::string &env) {
|
|
|
|
auto value = std::getenv(env.c_str());
|
|
|
|
|
|
|
|
if (value == nullptr)
|
|
|
|
return std::nullopt;
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|