diff --git a/include/helpers/utils.hpp b/include/helpers/utils.hpp deleted file mode 100644 index 616766da0..000000000 --- a/include/helpers/utils.hpp +++ /dev/null @@ -1,168 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include -#include -#include - -#ifdef __MINGW32__ -#include - -#else -#include -#endif - -#include "lang/token.hpp" - -#ifdef __APPLE__ - #define off64_t off_t - #define fopen64 fopen - #define fseeko64 fseek - #define ftello64 ftell -#endif - -#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION <= 12000 -#if __has_include() -// Make sure we break when derived_from is implemented in libc++. Then we can fix a compatibility version above -#include -#endif -// libcxx 12 still doesn't have derived_from implemented, as a result we need to define it ourself using clang built-ins. -// [concept.derived] (patch from https://reviews.llvm.org/D74292) -namespace hex { -template - concept derived_from = - __is_base_of(_Bp, _Dp) && __is_convertible_to(const volatile _Dp*, const volatile _Bp*); -} -#else -// Assume supported -#include -namespace hex { - using std::derived_from; -} -#endif - -namespace hex { - - template - inline std::string format(const char *format, Args ... args) { - ssize_t size = snprintf( nullptr, 0, format, args ... ); - - if (size <= 0) - return ""; - - std::vector buffer(size + 1, 0x00); - snprintf(buffer.data(), size + 1, format, args ...); - - return std::string(buffer.data(), buffer.data() + size); - } - - [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const u64 &value) { - u64 mask = (std::numeric_limits::max() >> (63 - (from - to))) << to; - return (value & mask) >> to; - } - - [[nodiscard]] constexpr inline u64 signExtend(u64 value, u8 currWidth, u8 targetWidth) { - u64 mask = 1LLU << (currWidth - 1); - return (((value ^ mask) - mask) << (64 - targetWidth)) >> (64 - targetWidth); - } - - [[nodiscard]] constexpr inline bool isUnsigned(const lang::Token::TypeToken::Type type) { - return (static_cast(type) & 0x0F) == 0x00; - } - - [[nodiscard]] constexpr inline bool isSigned(const lang::Token::TypeToken::Type type) { - return (static_cast(type) & 0x0F) == 0x01; - } - - [[nodiscard]] constexpr inline bool isFloatingPoint(const lang::Token::TypeToken::Type type) { - return (static_cast(type) & 0x0F) == 0x02; - } - - [[nodiscard]] constexpr inline u32 getTypeSize(const lang::Token::TypeToken::Type type) { - return static_cast(type) >> 4; - } - - std::string toByteString(u64 bytes); - std::string makePrintable(char c); - - template - struct always_false : std::false_type {}; - - template - constexpr T changeEndianess(T value, std::endian endian) { - if (endian == std::endian::native) - return value; - - if constexpr (sizeof(T) == 1) - return value; - else if constexpr (sizeof(T) == 2) - return __builtin_bswap16(value); - else if constexpr (sizeof(T) == 4) - return __builtin_bswap32(value); - else if constexpr (sizeof(T) == 8) - return __builtin_bswap64(value); - else - static_assert(always_false::value, "Invalid type provided!"); - } - - template - constexpr T changeEndianess(T value, size_t size, std::endian endian) { - if (endian == std::endian::native) - return value; - - if (size == 1) - return value; - else if (size == 2) - return __builtin_bswap16(value); - else if (size == 4) - return __builtin_bswap32(value); - else if (size == 8) - return __builtin_bswap64(value); - else - throw std::invalid_argument("Invalid value size!"); - } - - template< class T > - constexpr T bit_width(T x) noexcept { - return std::numeric_limits::digits - std::countl_zero(x); - } - - template - constexpr T bit_ceil(T x) noexcept { - if (x <= 1u) - return T(1); - - return T(1) << bit_width(T(x - 1)); - } - - std::vector readFile(std::string_view path); - - class ScopeExit { - public: - ScopeExit(std::function func) : m_func(func) {} - ~ScopeExit() { if (this->m_func != nullptr) this->m_func(); } - - void release() { - this->m_func = nullptr; - } - - private: - std::function m_func; - }; - - struct Region { - u64 address; - size_t size; - }; - - struct Bookmark { - Region region; - - std::vector name; - std::vector comment; - }; -} \ No newline at end of file diff --git a/include/lang/token.hpp b/include/lang/token.hpp index 44aa2b2a3..e0a27af5c 100644 --- a/include/lang/token.hpp +++ b/include/lang/token.hpp @@ -67,5 +67,21 @@ namespace hex::lang { } typeToken; u32 lineNumber; + + [[nodiscard]] constexpr static inline bool isUnsigned(const TypeToken::Type type) { + return (static_cast(type) & 0x0F) == 0x00; + } + + [[nodiscard]] constexpr static inline bool isSigned(const TypeToken::Type type) { + return (static_cast(type) & 0x0F) == 0x01; + } + + [[nodiscard]] constexpr static inline bool isFloatingPoint(const TypeToken::Type type) { + return (static_cast(type) & 0x0F) == 0x02; + } + + [[nodiscard]] constexpr static inline u32 getTypeSize(const TypeToken::Type type) { + return static_cast(type) >> 4; + } }; } \ No newline at end of file diff --git a/plugins/libimhex/include/helpers/utils.hpp b/plugins/libimhex/include/helpers/utils.hpp index 05a7a1054..d37b18611 100644 --- a/plugins/libimhex/include/helpers/utils.hpp +++ b/plugins/libimhex/include/helpers/utils.hpp @@ -2,23 +2,11 @@ #include -#define TOKEN_CONCAT_IMPL(x, y) x ## y -#define TOKEN_CONCAT(x, y) TOKEN_CONCAT_IMPL(x, y) - -#ifdef __APPLE__ - #define off64_t off_t - #define fopen64 fopen - #define fseeko64 fseek - #define ftello64 ftell -#endif - #include #include #include #include #include -#include -#include #include #ifdef __MINGW32__ @@ -28,6 +16,33 @@ #include #endif +#ifdef __APPLE__ +#define off64_t off_t + #define fopen64 fopen + #define fseeko64 fseek + #define ftello64 ftell +#endif + +#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION <= 12000 +#if __has_include() +// Make sure we break when derived_from is implemented in libc++. Then we can fix a compatibility version above +#include +#endif +// libcxx 12 still doesn't have derived_from implemented, as a result we need to define it ourself using clang built-ins. +// [concept.derived] (patch from https://reviews.llvm.org/D74292) +namespace hex { +template + concept derived_from = + __is_base_of(_Bp, _Dp) && __is_convertible_to(const volatile _Dp*, const volatile _Bp*); +} +#else +// Assume supported +#include +namespace hex { + using std::derived_from; +} +#endif + namespace hex { template @@ -43,8 +58,8 @@ namespace hex { return std::string(buffer.data(), buffer.data() + size); } - [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) { - std::remove_cvref_t mask = (std::numeric_limits>::max() >> (((sizeof(value) * 8) - 1) - (from - to))) << to; + [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const u64 &value) { + u64 mask = (std::numeric_limits::max() >> (63 - (from - to))) << to; return (value & mask) >> to; } @@ -73,7 +88,7 @@ namespace hex { else if constexpr (sizeof(T) == 8) return __builtin_bswap64(value); else - static_assert(always_false::value, "Invalid type provided!"); + static_assert(always_false::value, "Invalid type provided!"); } template @@ -108,18 +123,16 @@ namespace hex { std::vector readFile(std::string_view path); - #define SCOPE_EXIT(func) ScopeExit TOKEN_CONCAT(scopeGuard, __COUNTER__)([&] { func }) class ScopeExit { public: - ScopeExit(std::function func) : m_func(std::move(func)) {} - ~ScopeExit() { if (!this->m_released) this->m_func(); } + ScopeExit(std::function func) : m_func(func) {} + ~ScopeExit() { if (this->m_func != nullptr) this->m_func(); } void release() { - this->m_released = true; + this->m_func = nullptr; } private: - bool m_released = false; std::function m_func; }; diff --git a/plugins/libimhex/include/hex.hpp b/plugins/libimhex/include/hex.hpp index c527edbde..824fede55 100644 --- a/plugins/libimhex/include/hex.hpp +++ b/plugins/libimhex/include/hex.hpp @@ -23,52 +23,4 @@ extern char **mainArgv; #define MAGIC_PATH_SEPARATOR ";" #else #define MAGIC_PATH_SEPARATOR ":" -#endif - -template<> -struct std::is_integral : public std::true_type { }; -template<> -struct std::is_integral : public std::true_type { }; -template<> -struct std::is_signed : public std::true_type { }; - -#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION <= 12000 -#if __has_include() -// Make sure we break when derived_from is implemented in libc++. Then we can fix a compatibility version above -#include -#endif -// libcxx 12 still doesn't have many default concepts implemented, as a result we need to define it ourself using clang built-ins. -// [concept.derived] (patch from https://reviews.llvm.org/D74292) -namespace hex { -template -concept derived_from = - __is_base_of(_Bp, _Dp) && __is_convertible_to(const volatile _Dp*, const volatile _Bp*); -} - -// [concepts.arithmetic] (patch from https://reviews.llvm.org/D88131) -namespace hex { -template -concept integral = __is_integral(_Tp); - -template -concept signed_integral = integral<_Tp> && __is_signed(_Tp); - -template -concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>; - -template -concept floating_point = __is_floating_point(_Tp); -} -#else -// Assume supported -#include - -namespace hex { - using std::derived_from; - - using std::integral; - using std::signed_integral; - using std::unsigned_integral; - using std::floating_point; -} #endif \ No newline at end of file diff --git a/plugins/wintools/.idea/misc.xml b/plugins/wintools/.idea/misc.xml new file mode 100644 index 000000000..79b3c9483 --- /dev/null +++ b/plugins/wintools/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/plugins/wintools/.idea/vcs.xml b/plugins/wintools/.idea/vcs.xml new file mode 100644 index 000000000..b2bdec2d7 --- /dev/null +++ b/plugins/wintools/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/plugins/wintools/.idea/workspace.xml b/plugins/wintools/.idea/workspace.xml new file mode 100644 index 000000000..94a1b8d90 --- /dev/null +++ b/plugins/wintools/.idea/workspace.xml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1608738610011 + + + + + + \ No newline at end of file diff --git a/source/lang/evaluator.cpp b/source/lang/evaluator.cpp index 2c0f8ccdd..aeac9a380 100644 --- a/source/lang/evaluator.cpp +++ b/source/lang/evaluator.cpp @@ -1,5 +1,7 @@ #include "lang/evaluator.hpp" +#include "lang/token.hpp" + #include #include @@ -204,7 +206,7 @@ namespace hex::lang { return { nullptr, 0 }; } - size_t size = getTypeSize(enumType->getUnderlyingType()); + size_t size = Token::getTypeSize(enumType->getUnderlyingType()); return { new PatternDataEnum(offset, size, varDeclNode->getVariableName(), enumType->getName(), enumType->getValues(), varDeclNode->getEndianess().value_or(this->m_defaultDataEndianess)), size }; } @@ -322,22 +324,22 @@ namespace hex::lang { type = currType->getAssignedType(); } - size_t typeSize = getTypeSize(type); + size_t typeSize = Token::getTypeSize(type); size_t arraySize = varDeclNode->getArraySize(); - if (isSigned(type)) { + if (Token::isSigned(type)) { if (typeSize == 1 && arraySize == 1) return { new PatternDataCharacter(offset, typeSize, varDeclNode->getVariableName(), varDeclNode->getEndianess().value_or(this->m_defaultDataEndianess)), 1 }; else if (arraySize > 1) return createArrayPattern(varDeclNode, offset); else return { new PatternDataSigned(offset, typeSize, varDeclNode->getVariableName(), varDeclNode->getEndianess().value_or(this->m_defaultDataEndianess)), typeSize * arraySize }; - } else if (isUnsigned(varDeclNode->getVariableType())) { + } else if (Token::isUnsigned(varDeclNode->getVariableType())) { if (arraySize > 1) return createArrayPattern(varDeclNode, offset); else return { new PatternDataUnsigned(offset, typeSize, varDeclNode->getVariableName(), varDeclNode->getEndianess().value_or(this->m_defaultDataEndianess)), typeSize * arraySize }; - } else if (isFloatingPoint(varDeclNode->getVariableType())) { + } else if (Token::isFloatingPoint(varDeclNode->getVariableType())) { if (arraySize > 1) return createArrayPattern(varDeclNode, offset); else diff --git a/source/lang/parser.cpp b/source/lang/parser.cpp index 3d906ef5e..bec534a42 100644 --- a/source/lang/parser.cpp +++ b/source/lang/parser.cpp @@ -1,6 +1,7 @@ #include "lang/parser.hpp" #include "helpers/utils.hpp" +#include "lang/token.hpp" #include @@ -63,7 +64,7 @@ namespace hex::lang { ASTNode* Parser::parseBuiltinPointerVariableDecl(TokenIter &curr, bool hasEndianDef) { auto pointerType = curr[-2].typeToken.type; - if (!isUnsigned(pointerType)) { + if (!Token::isUnsigned(pointerType)) { this->m_error = { curr->lineNumber, "Pointer size needs to be a unsigned type" }; return nullptr; } @@ -88,16 +89,16 @@ namespace hex::lang { endianess = std::endian::big; else return nullptr; - return new ASTNodeVariableDecl(curr[-7].lineNumber, curr[-6].typeToken.type, curr[-4].identifierToken.identifier, "", { }, 1, { }, getTypeSize(pointerType), endianess); + return new ASTNodeVariableDecl(curr[-7].lineNumber, curr[-6].typeToken.type, curr[-4].identifierToken.identifier, "", { }, 1, { }, Token::getTypeSize(pointerType), endianess); } else - return new ASTNodeVariableDecl(curr[-6].lineNumber, curr[-6].typeToken.type, curr[-4].identifierToken.identifier, "", { }, 1, { }, getTypeSize(pointerType)); + return new ASTNodeVariableDecl(curr[-6].lineNumber, curr[-6].typeToken.type, curr[-4].identifierToken.identifier, "", { }, 1, { }, Token::getTypeSize(pointerType)); } ASTNode* Parser::parseCustomTypePointerVariableDecl(TokenIter &curr, bool hasEndianDef) { auto pointerType = curr[-2].typeToken.type; - if (!isUnsigned(pointerType)) { + if (!Token::isUnsigned(pointerType)) { this->m_error = { curr->lineNumber, "Pointer size needs to be a unsigned type" }; return nullptr; } @@ -124,10 +125,10 @@ namespace hex::lang { return nullptr; } - return new ASTNodeVariableDecl(curr[-7].lineNumber,Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 1, { }, getTypeSize(pointerType), endianess); + return new ASTNodeVariableDecl(curr[-7].lineNumber,Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 1, { }, Token::getTypeSize(pointerType), endianess); } else - return new ASTNodeVariableDecl(curr[-6].lineNumber, Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 1, { }, getTypeSize(pointerType)); + return new ASTNodeVariableDecl(curr[-6].lineNumber, Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 1, { }, Token::getTypeSize(pointerType)); } ASTNode* Parser::parseBuiltinArrayDecl(TokenIter &curr, bool hasEndianDef) { @@ -369,7 +370,7 @@ namespace hex::lang { return nullptr; } - if (!isUnsigned(underlyingType)) { + if (!Token::isUnsigned(underlyingType)) { this->m_error = { curr[-3].lineNumber, "Underlying type needs to be an unsigned type" }; return nullptr; }