From e2489151f3004ae2f3b66b5940c163567ceec427 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 24 Dec 2023 13:14:51 +0100 Subject: [PATCH] feat: Added decompressing support --- .gitattributes | 5 +- dist/Arch/PKGBUILD | 2 +- dist/DEBIAN/control.in | 2 +- dist/ImHex-9999.ebuild | 1 + dist/get_deps_archlinux.sh | 27 +++++---- dist/get_deps_debian.sh | 3 +- dist/get_deps_fedora.sh | 3 +- dist/get_deps_msys2.sh | 3 +- dist/macOS/arm64.Dockerfile | 1 + dist/msys2/PKGBUILD | 3 +- dist/net.werwolv.ImHex.yaml | 43 +------------- dist/web/Dockerfile | 1 + lib/external/libromfs | 2 +- lib/libimhex/source/api/plugin_manager.cpp | 2 +- plugins/decompress/CMakeLists.txt | 19 ++++++ .../source/content/pl_functions.cpp | 59 +++++++++++++++++++ .../decompress/source/plugin_decompress.cpp | 21 +++++++ 17 files changed, 134 insertions(+), 63 deletions(-) create mode 100644 plugins/decompress/CMakeLists.txt create mode 100644 plugins/decompress/source/content/pl_functions.cpp create mode 100644 plugins/decompress/source/plugin_decompress.cpp diff --git a/.gitattributes b/.gitattributes index 7d00e59fd..9ffb78875 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,4 @@ -lib/external/** linguist-vendored \ No newline at end of file +lib/external/** linguist-vendored + +dist/*.sh eol=lf +dist/**/*Dockerfile eol=lf \ No newline at end of file diff --git a/dist/Arch/PKGBUILD b/dist/Arch/PKGBUILD index 93f3ef28c..d7ec649fb 100644 --- a/dist/Arch/PKGBUILD +++ b/dist/Arch/PKGBUILD @@ -8,7 +8,7 @@ pkgdesc="A Hex Editor for Reverse Engineers, Programmers and people who value th arch=("x86_64") url="https://github.com/WerWolv/ImHex" license=('GPL2') -depends=(glfw mbedtls freetype2 libglvnd dbus gtk3 curl fmt yara nlohmann-json) +depends=(glfw mbedtls freetype2 libglvnd dbus gtk3 curl fmt yara nlohmann-json libarchive) makedepends=(git) provides=(imhex) conflicts=(imhex) diff --git a/dist/DEBIAN/control.in b/dist/DEBIAN/control.in index 263e7ed53..3c1c162f8 100644 --- a/dist/DEBIAN/control.in +++ b/dist/DEBIAN/control.in @@ -4,7 +4,7 @@ Section: editors Priority: optional Architecture: amd64 License: GNU GPL-2 -Depends: libglfw3 | libglfw3-wayland, libmagic1, libmbedtls14, libfreetype6, libopengl0, libdbus-1-3, xdg-desktop-portal +Depends: libglfw3 | libglfw3-wayland, libmagic1, libmbedtls14, libfreetype6, libopengl0, libdbus-1-3, xdg-desktop-portal, libarchive Maintainer: WerWolv Description: ImHex Hex Editor A Hex Editor for Reverse Engineers, Programmers and diff --git a/dist/ImHex-9999.ebuild b/dist/ImHex-9999.ebuild index ec50ce3c3..c5092593e 100644 --- a/dist/ImHex-9999.ebuild +++ b/dist/ImHex-9999.ebuild @@ -24,5 +24,6 @@ RDEPEND="${DEPEND} dev-cpp/nlohmann_json dbus xdg-desktop-portal + app-arch/libarchive " BDEPEND="${DEPEND}" diff --git a/dist/get_deps_archlinux.sh b/dist/get_deps_archlinux.sh index dbd14b73b..58dcbc796 100755 --- a/dist/get_deps_archlinux.sh +++ b/dist/get_deps_archlinux.sh @@ -1,17 +1,18 @@ #!/usr/bin/env sh pacman -S $@ --needed \ - cmake \ - gcc \ - lld \ - glfw \ - file \ - mbedtls \ - freetype2 \ - dbus \ - gtk3 \ - curl \ - fmt \ - yara \ + cmake \ + gcc \ + lld \ + glfw \ + file \ + mbedtls \ + freetype2 \ + dbus \ + gtk3 \ + curl \ + fmt \ + yara \ nlohmann-json \ - ninja + ninja \ + libarchive diff --git a/dist/get_deps_debian.sh b/dist/get_deps_debian.sh index 0686eed8d..46b432988 100755 --- a/dist/get_deps_debian.sh +++ b/dist/get_deps_debian.sh @@ -22,4 +22,5 @@ apt install -y \ libdbus-1-dev \ libcurl4-gnutls-dev \ libgtk-3-dev \ - ninja-build + ninja-build \ + libarchive-dev diff --git a/dist/get_deps_fedora.sh b/dist/get_deps_fedora.sh index 86012b45f..a90e119e6 100755 --- a/dist/get_deps_fedora.sh +++ b/dist/get_deps_fedora.sh @@ -12,4 +12,5 @@ dnf install -y \ glfw-devel \ lld \ mbedtls-devel \ - gtk3-devel \ No newline at end of file + gtk3-devel \ + libarchive-devel \ No newline at end of file diff --git a/dist/get_deps_msys2.sh b/dist/get_deps_msys2.sh index beeb907d7..cc75295eb 100755 --- a/dist/get_deps_msys2.sh +++ b/dist/get_deps_msys2.sh @@ -12,4 +12,5 @@ pacman -S --needed --noconfirm \ mingw-w64-x86_64-freetype \ mingw-w64-x86_64-dlfcn \ mingw-w64-x86_64-ninja \ - mingw-w64-x86_64-capstone + mingw-w64-x86_64-capstone \ + mingw-w64-x86_64-libarchive diff --git a/dist/macOS/arm64.Dockerfile b/dist/macOS/arm64.Dockerfile index e3d4ce2e2..a3ee8900b 100644 --- a/dist/macOS/arm64.Dockerfile +++ b/dist/macOS/arm64.Dockerfile @@ -52,6 +52,7 @@ vcpkg install --triplet=arm-osx-mytriplet curl vcpkg install --triplet=arm-osx-mytriplet mbedtls vcpkg install --triplet=arm-osx-mytriplet freetype vcpkg install --triplet=arm-osx-mytriplet josuttis-jthread +vcpkg install --triplet=arm-osx-mytriplet libarchive EOF ## Install glfw3 dep diff --git a/dist/msys2/PKGBUILD b/dist/msys2/PKGBUILD index 6037693ad..9fae7ef6d 100644 --- a/dist/msys2/PKGBUILD +++ b/dist/msys2/PKGBUILD @@ -16,7 +16,8 @@ makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-file" "${MINGW_PACKAGE_PREFIX}-mbedtls" "${MINGW_PACKAGE_PREFIX}-polly" - "${MINGW_PACKAGE_PREFIX}-freetype") + "${MINGW_PACKAGE_PREFIX}-freetype" + "${MINGW_PACKAGE_PREFIX}-libarchive") source=() sha256sums=() diff --git a/dist/net.werwolv.ImHex.yaml b/dist/net.werwolv.ImHex.yaml index 28f01188f..925c5f5bf 100644 --- a/dist/net.werwolv.ImHex.yaml +++ b/dist/net.werwolv.ImHex.yaml @@ -12,49 +12,10 @@ finish-args: - --device=all modules: - - name: libiconv - sources: - - type: archive - url: https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz - sha256: e6a1b1b589654277ee790cce3734f07876ac4ccfaecbee8afa0b649cf529cc04 - - - name: glfw - buildsystem: cmake-ninja - builddir: true - config-opts: - - -DCMAKE_BUILD_TYPE=RelWithDebInfo - - -DBUILD_SHARED_LIBS:BOOL=ON - sources: - - type: archive - url: https://github.com/glfw/glfw/releases/download/3.3.2/glfw-3.3.2.zip - sha256: 08a33a512f29d7dbf78eab39bd7858576adcc95228c9efe8e4bc5f0f3261efc7 - cleanup: - - /include - - /lib/pkgconfig - - - name: mbedtls - buildsystem: cmake-ninja - config-opts: - - -DCMAKE_C_FLAGS=-fPIC - sources: - - type: archive - url: https://github.com/ARMmbed/mbedtls/archive/refs/tags/v2.27.0.tar.gz - sha256: 2a07856e541f0e5f6eaee4f78018c52f25bd244ed76f9020dea54a8b02cac6ea - - - name: nlohmann-json - buildsystem: cmake-ninja - builddir: true - config-opts: - - -DCMAKE_BUILD_TYPE=RelWithDebInfo - - -DBUILD_TESTING=OFF - - sources: - - type: archive - url: https://github.com/nlohmann/json/archive/v3.9.1.tar.gz - sha256: 4cf0df69731494668bdd6460ed8cb269b68de9c19ad8c27abc24cd72605b2d5b - - name: imhex buildsystem: cmake + config-opts: + - -DCMAKE_BUILD_TYPE=RelWithDebInfo sources: - type: git diff --git a/dist/web/Dockerfile b/dist/web/Dockerfile index 6d7545d53..d9ef8dfbd 100644 --- a/dist/web/Dockerfile +++ b/dist/web/Dockerfile @@ -37,6 +37,7 @@ mkdir -p $VCPKG_DEFAULT_BINARY_CACHE /vcpkg/vcpkg install --triplet=wasm32-emscripten libmagic /vcpkg/vcpkg install --triplet=wasm32-emscripten freetype /vcpkg/vcpkg install --triplet=wasm32-emscripten mbedtls +/vcpkg/vcpkg install --triplet=wasm32-emscripten libarchive EOF # Build ImHex diff --git a/lib/external/libromfs b/lib/external/libromfs index 45382a6a3..0a72f7bb3 160000 --- a/lib/external/libromfs +++ b/lib/external/libromfs @@ -1 +1 @@ -Subproject commit 45382a6a37a4cccbecc0f7fb6e6c5f607b50ffab +Subproject commit 0a72f7bb33d87c4fb2709b6b94bf83518ab0022d diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index b1f81b2ae..86dae1b5e 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -174,7 +174,7 @@ namespace hex { } bool Plugin::isValid() const { - return m_handle != 0; + return m_handle != 0 || m_functions.initializeLibraryFunction != nullptr || m_functions.initializePluginFunction != nullptr; } bool Plugin::isLoaded() const { diff --git a/plugins/decompress/CMakeLists.txt b/plugins/decompress/CMakeLists.txt new file mode 100644 index 000000000..e7f37b8c7 --- /dev/null +++ b/plugins/decompress/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.16) + +include(ImHexPlugin) + +find_package(LibArchive 3.6 REQUIRED) + +add_imhex_plugin( + NAME + decompress + SOURCES + source/plugin_decompress.cpp + + source/content/pl_functions.cpp + INCLUDES + include + LIBRARIES + ui + LibArchive::LibArchive +) diff --git a/plugins/decompress/source/content/pl_functions.cpp b/plugins/decompress/source/content/pl_functions.cpp new file mode 100644 index 000000000..9d13974ea --- /dev/null +++ b/plugins/decompress/source/content/pl_functions.cpp @@ -0,0 +1,59 @@ +#include + +#include +#include +#include + +#include +#include + +#include + +#include +#include + +namespace hex::plugin::decompress { + + void registerPatternLanguageFunctions() { + using namespace pl::core; + using FunctionParameterCount = pl::api::FunctionParameterCount; + + const pl::api::Namespace nsHexDec = { "builtin", "hex", "dec" }; + + /* decompress() */ + ContentRegistry::PatternLanguage::addFunction(nsHexDec, "decompress", FunctionParameterCount::exactly(2), [](Evaluator *evaluator, auto params) -> std::optional { + const auto inputPattern = params[0].toPattern(); + auto §ion = evaluator->getSection(params[1].toUnsigned()); + + std::vector compressedData; + compressedData.resize(inputPattern->getSize()); + evaluator->readData(inputPattern->getOffset(), compressedData.data(), compressedData.size(), inputPattern->getSection()); + + auto inArchive = archive_read_new(); + ON_SCOPE_EXIT { + archive_read_close(inArchive); + archive_read_free(inArchive); + }; + + archive_read_support_filter_all(inArchive); + archive_read_support_format_raw(inArchive); + + archive_read_open_memory(inArchive, compressedData.data(), compressedData.size()); + + archive_entry *entry = nullptr; + while (archive_read_next_header(inArchive, &entry) == ARCHIVE_OK) { + const void *block = nullptr; + size_t size = 0x00; + i64 offset = 0x00; + + while (archive_read_data_block(inArchive, &block, &size, &offset) == ARCHIVE_OK) { + section.resize(section.size() + size); + std::memcpy(section.data(), block, size); + } + } + + return std::nullopt; + }); + } + +} \ No newline at end of file diff --git a/plugins/decompress/source/plugin_decompress.cpp b/plugins/decompress/source/plugin_decompress.cpp new file mode 100644 index 000000000..17a992605 --- /dev/null +++ b/plugins/decompress/source/plugin_decompress.cpp @@ -0,0 +1,21 @@ +#include + +#include +#include + +#include + +namespace hex::plugin::decompress { + + void registerPatternLanguageFunctions(); + +} + +using namespace hex; +using namespace hex::plugin::decompress; + +IMHEX_PLUGIN_SETUP("Decompressing", "WerWolv", "Support for decompressing data") { + hex::log::debug("Using romfs: '{}'", romfs::name()); + + registerPatternLanguageFunctions(); +}