2020-12-27 14:12:53 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/concepts.hpp>
|
2022-01-16 01:51:31 +01:00
|
|
|
#include <hex/helpers/paths.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <array>
|
2021-10-09 23:07:58 +02:00
|
|
|
#include <bit>
|
2021-01-07 01:58:56 +01:00
|
|
|
#include <cstring>
|
2021-09-09 12:58:44 +02:00
|
|
|
#include <cctype>
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <functional>
|
2021-09-16 22:23:51 +02:00
|
|
|
#include <limits>
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2021-01-02 20:49:55 +01:00
|
|
|
#include <type_traits>
|
2021-12-10 11:55:27 +01:00
|
|
|
#include <variant>
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
#include <nfd.hpp>
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define TOKEN_CONCAT_IMPL(x, y) x##y
|
|
|
|
#define TOKEN_CONCAT(x, y) TOKEN_CONCAT_IMPL(x, y)
|
2021-03-31 22:54:43 +02:00
|
|
|
#define ANONYMOUS_VARIABLE(prefix) TOKEN_CONCAT(prefix, __COUNTER__)
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2022-01-11 23:48:18 +01:00
|
|
|
struct ImVec2;
|
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-01-11 23:48:18 +01:00
|
|
|
long double operator""_scaled(long double value);
|
|
|
|
long double operator""_scaled(unsigned long long value);
|
|
|
|
ImVec2 scaled(const ImVec2 &vector);
|
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
std::string to_string(u128 value);
|
2022-01-22 22:37:52 +01:00
|
|
|
std::string to_string(i128 value);
|
2021-01-21 14:53:44 +01:00
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
std::string toByteString(u64 bytes);
|
2021-08-30 19:12:46 +02:00
|
|
|
std::string makePrintable(u8 c);
|
2021-01-21 14:53:44 +01:00
|
|
|
|
2021-08-22 21:43:31 +02:00
|
|
|
void runCommand(const std::string &command);
|
|
|
|
void openWebpage(std::string url);
|
2021-01-21 14:53:44 +01:00
|
|
|
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
[[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) {
|
2021-01-27 00:00:20 +01:00
|
|
|
using ValueType = std::remove_cvref_t<decltype(value)>;
|
|
|
|
ValueType mask = (std::numeric_limits<ValueType>::max() >> (((sizeof(value) * 8) - 1) - (from - to))) << to;
|
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
return (value & mask) >> to;
|
|
|
|
}
|
2021-04-14 09:03:41 +02:00
|
|
|
|
2021-09-12 14:28:13 +02:00
|
|
|
[[nodiscard]] inline u64 extract(u32 from, u32 to, const std::vector<u8> &bytes) {
|
|
|
|
u8 index = 0;
|
2022-01-24 20:53:17 +01:00
|
|
|
while (from > 32 && to > 32) {
|
2021-09-12 14:28:13 +02:00
|
|
|
if (from - 8 < 0 || to - 8 < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
from -= 8;
|
|
|
|
to -= 8;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 value = 0;
|
|
|
|
std::memcpy(&value, &bytes[index], std::min(sizeof(value), bytes.size() - index));
|
|
|
|
u64 mask = (std::numeric_limits<u64>::max() >> (64 - (from + 1)));
|
|
|
|
|
|
|
|
return (value & mask) >> to;
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
constexpr inline i128 signExtend(size_t numBits, i128 value) {
|
|
|
|
i128 mask = 1U << (numBits - 1);
|
2021-09-21 21:29:18 +02:00
|
|
|
return (value ^ mask) - mask;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<class... Ts>
|
|
|
|
struct overloaded : Ts... { using Ts::operator()...; };
|
|
|
|
template<class... Ts>
|
|
|
|
overloaded(Ts...) -> overloaded<Ts...>;
|
2021-06-17 23:13:58 +02:00
|
|
|
|
2021-10-20 10:04:52 +02:00
|
|
|
template<size_t Size>
|
|
|
|
struct SizeTypeImpl { };
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<1> { using Type = u8; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<2> { using Type = u16; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<4> { using Type = u32; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<8> { using Type = u64; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<16> { using Type = u128; };
|
2021-10-20 10:04:52 +02:00
|
|
|
|
|
|
|
template<size_t Size>
|
|
|
|
using SizeType = typename SizeTypeImpl<Size>::Type;
|
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
template<typename T>
|
2021-10-20 10:04:52 +02:00
|
|
|
constexpr T changeEndianess(const T &value, std::endian endian) {
|
2020-12-27 14:12:53 +01:00
|
|
|
if (endian == std::endian::native)
|
|
|
|
return value;
|
|
|
|
|
2021-10-20 10:04:52 +02:00
|
|
|
constexpr auto Size = sizeof(T);
|
|
|
|
|
|
|
|
SizeType<Size> unswapped;
|
|
|
|
std::memcpy(&unswapped, &value, Size);
|
|
|
|
|
|
|
|
SizeType<Size> swapped;
|
|
|
|
|
|
|
|
if constexpr (!std::has_single_bit(Size) || Size > 16)
|
2022-01-24 20:53:17 +01:00
|
|
|
static_assert(always_false<T>::value, "Invalid type provided!");
|
2021-10-20 10:04:52 +02:00
|
|
|
|
|
|
|
switch (Size) {
|
2022-01-24 20:53:17 +01:00
|
|
|
case 1:
|
|
|
|
swapped = unswapped;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
swapped = __builtin_bswap16(unswapped);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
swapped = __builtin_bswap32(unswapped);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
swapped = __builtin_bswap64(unswapped);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
swapped = (u128(__builtin_bswap64(unswapped & 0xFFFF'FFFF'FFFF'FFFF)) << 64) | __builtin_bswap64(u128(unswapped) >> 64);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
2021-10-20 10:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
T result;
|
|
|
|
std::memcpy(&result, &swapped, Size);
|
|
|
|
|
|
|
|
return result;
|
2020-12-27 14:12:53 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] constexpr u128 bitmask(u8 bits) {
|
2022-01-12 11:59:08 +01:00
|
|
|
return u128(-1) >> (128 - bits);
|
2022-01-12 08:59:14 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
template<typename T>
|
|
|
|
constexpr T changeEndianess(T value, size_t size, std::endian endian) {
|
|
|
|
if (endian == std::endian::native)
|
|
|
|
return value;
|
|
|
|
|
2021-10-20 10:04:52 +02:00
|
|
|
u128 unswapped = 0;
|
|
|
|
std::memcpy(&unswapped, &value, size);
|
|
|
|
|
|
|
|
u128 swapped;
|
|
|
|
|
|
|
|
switch (size) {
|
2022-01-24 20:53:17 +01:00
|
|
|
case 1:
|
|
|
|
swapped = unswapped;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
swapped = __builtin_bswap16(unswapped);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
swapped = __builtin_bswap32(unswapped);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
swapped = __builtin_bswap64(unswapped);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
swapped = (u128(__builtin_bswap64(unswapped & 0xFFFF'FFFF'FFFF'FFFF)) << 64) | __builtin_bswap64(u128(unswapped) >> 64);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
2021-10-20 10:04:52 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 10:35:26 +02:00
|
|
|
T result = 0;
|
2021-10-20 10:04:52 +02:00
|
|
|
std::memcpy(&result, &swapped, size);
|
|
|
|
|
|
|
|
return result;
|
2020-12-27 14:12:53 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<class T>
|
2020-12-27 14:12:53 +01:00
|
|
|
constexpr T bit_width(T x) noexcept {
|
|
|
|
return std::numeric_limits<T>::digits - std::countl_zero(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr T bit_ceil(T x) noexcept {
|
|
|
|
if (x <= 1u)
|
|
|
|
return T(1);
|
|
|
|
|
|
|
|
return T(1) << bit_width(T(x - 1));
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
std::vector<std::string> splitString(const std::string &string, const std::string &delimiter);
|
|
|
|
std::string combineStrings(const std::vector<std::string> &strings, const std::string &delimiter = "");
|
2021-01-20 22:54:46 +01:00
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
std::string toEngineeringString(double value);
|
2021-01-11 00:27:00 +01:00
|
|
|
|
2021-01-31 01:42:29 +01:00
|
|
|
template<typename T>
|
|
|
|
std::vector<u8> toBytes(T value) {
|
|
|
|
std::vector<u8> bytes(sizeof(T));
|
|
|
|
std::memcpy(bytes.data(), &value, sizeof(T));
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
inline std::vector<u8> parseByteString(const std::string &string) {
|
2021-02-14 01:11:55 +01:00
|
|
|
auto byteString = std::string(string);
|
|
|
|
byteString.erase(std::remove(byteString.begin(), byteString.end(), ' '), byteString.end());
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if ((byteString.length() % 2) != 0) return {};
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
std::vector<u8> result;
|
|
|
|
for (u32 i = 0; i < byteString.length(); i += 2) {
|
|
|
|
if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1]))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:38:28 +02:00
|
|
|
inline std::string toBinaryString(hex::unsigned_integral auto number) {
|
2021-02-07 22:57:34 +01:00
|
|
|
if (number == 0) return "0";
|
|
|
|
|
|
|
|
std::string result;
|
2022-01-22 22:37:52 +01:00
|
|
|
for (i16 bit = hex::bit_width(number) - 1; bit >= 0; bit--)
|
2021-02-07 22:57:34 +01:00
|
|
|
result += (number & (0b1 << bit)) == 0 ? '0' : '1';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-02-14 01:11:55 +01:00
|
|
|
inline void trimLeft(std::string &s) {
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
|
2022-01-24 20:53:17 +01:00
|
|
|
return !std::isspace(ch) && ch >= 0x20;
|
|
|
|
}));
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
inline void trimRight(std::string &s) {
|
2021-02-14 01:11:55 +01:00
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
|
2022-01-24 20:53:17 +01:00
|
|
|
return !std::isspace(ch) && ch >= 0x20;
|
|
|
|
}).base(),
|
|
|
|
s.end());
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void trim(std::string &s) {
|
|
|
|
trimLeft(s);
|
|
|
|
trimRight(s);
|
|
|
|
}
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
enum class DialogMode {
|
|
|
|
Open,
|
|
|
|
Save,
|
|
|
|
Folder
|
|
|
|
};
|
|
|
|
|
2022-01-16 01:51:31 +01:00
|
|
|
void openFileBrowser(const std::string &title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(fs::path)> &callback, const std::string &defaultPath = {});
|
2021-08-28 00:45:59 +02:00
|
|
|
|
2021-09-01 00:23:45 +02:00
|
|
|
float float16ToFloat32(u16 float16);
|
|
|
|
|
2021-09-09 12:58:44 +02:00
|
|
|
inline bool equalsIgnoreCase(const std::string &left, const std::string &right) {
|
|
|
|
return std::equal(left.begin(), left.end(), right.begin(), right.end(), [](char a, char b) {
|
|
|
|
return tolower(a) == tolower(b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool containsIgnoreCase(const std::string &a, const std::string &b) {
|
2022-01-24 20:53:17 +01:00
|
|
|
auto iter = std::search(a.begin(), a.end(), b.begin(), b.end(), [](char ch1, char ch2) {
|
2021-09-09 12:58:44 +02:00
|
|
|
return std::toupper(ch1) == std::toupper(ch2);
|
|
|
|
});
|
|
|
|
|
|
|
|
return iter != a.end();
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename T, typename... VariantTypes>
|
2021-12-10 11:55:27 +01:00
|
|
|
T get_or(const std::variant<VariantTypes...> &variant, T alt) {
|
|
|
|
const T *value = std::get_if<T>(&variant);
|
|
|
|
if (value == nullptr)
|
|
|
|
return alt;
|
|
|
|
else
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:58:23 +01:00
|
|
|
bool isProcessElevated();
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
namespace scope_guard {
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define SCOPE_GUARD ::hex::scope_guard::ScopeGuardOnExit() + [&]()
|
|
|
|
#define ON_SCOPE_EXIT auto ANONYMOUS_VARIABLE(SCOPE_EXIT_) = SCOPE_GUARD
|
2021-08-28 00:45:59 +02:00
|
|
|
|
|
|
|
template<class F>
|
|
|
|
class ScopeGuard {
|
|
|
|
private:
|
|
|
|
F m_func;
|
|
|
|
bool m_active;
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
public:
|
|
|
|
constexpr ScopeGuard(F func) : m_func(std::move(func)), m_active(true) { }
|
2022-01-24 20:53:17 +01:00
|
|
|
~ScopeGuard() {
|
|
|
|
if (this->m_active) { this->m_func(); }
|
|
|
|
}
|
2021-08-28 00:45:59 +02:00
|
|
|
void release() { this->m_active = false; }
|
|
|
|
|
|
|
|
ScopeGuard(ScopeGuard &&other) noexcept : m_func(std::move(other.m_func)), m_active(other.m_active) {
|
|
|
|
other.cancel();
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
ScopeGuard &operator=(ScopeGuard &&) = delete;
|
2021-08-28 00:45:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class ScopeGuardOnExit { };
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename F>
|
|
|
|
constexpr ScopeGuard<F> operator+(ScopeGuardOnExit, F &&f) {
|
2021-08-28 00:45:59 +02:00
|
|
|
return ScopeGuard<F>(std::forward<F>(f));
|
2020-12-27 14:12:53 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace first_time_exec {
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define FIRST_TIME static auto ANONYMOUS_VARIABLE(FIRST_TIME_) = ::hex::first_time_exec::FirstTimeExecutor() + [&]()
|
2020-12-27 14:12:53 +01:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
template<class F>
|
|
|
|
class FirstTimeExecute {
|
|
|
|
public:
|
|
|
|
constexpr FirstTimeExecute(F func) { func(); }
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
FirstTimeExecute &operator=(FirstTimeExecute &&) = delete;
|
2021-08-28 00:45:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class FirstTimeExecutor { };
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename F>
|
|
|
|
constexpr FirstTimeExecute<F> operator+(FirstTimeExecutor, F &&f) {
|
2021-08-28 00:45:59 +02:00
|
|
|
return FirstTimeExecute<F>(std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace final_cleanup {
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define FINAL_CLEANUP static auto ANONYMOUS_VARIABLE(ON_EXIT_) = ::hex::final_cleanup::FinalCleanupExecutor() + [&]()
|
2021-08-28 00:45:59 +02:00
|
|
|
|
|
|
|
template<class F>
|
|
|
|
class FinalCleanupExecute {
|
|
|
|
F m_func;
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
public:
|
|
|
|
constexpr FinalCleanupExecute(F func) : m_func(func) { }
|
|
|
|
constexpr ~FinalCleanupExecute() { this->m_func(); }
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
FinalCleanupExecute &operator=(FinalCleanupExecute &&) = delete;
|
2021-08-28 00:45:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class FinalCleanupExecutor { };
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename F>
|
|
|
|
constexpr FinalCleanupExecute<F> operator+(FinalCleanupExecutor, F &&f) {
|
2021-08-28 00:45:59 +02:00
|
|
|
return FinalCleanupExecute<F>(std::forward<F>(f));
|
|
|
|
}
|
2021-03-31 22:54:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 01:11:28 +02:00
|
|
|
}
|