Skip to content

Commit

Permalink
feat: Added decompressing support
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Dec 24, 2023
1 parent 9066891 commit e248915
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 63 deletions.
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
lib/external/** linguist-vendored
lib/external/** linguist-vendored

dist/*.sh eol=lf
dist/**/*Dockerfile eol=lf
2 changes: 1 addition & 1 deletion dist/Arch/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dist/DEBIAN/control.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Description: ImHex Hex Editor
A Hex Editor for Reverse Engineers, Programmers and
Expand Down
1 change: 1 addition & 0 deletions dist/ImHex-9999.ebuild
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ RDEPEND="${DEPEND}
dev-cpp/nlohmann_json
dbus
xdg-desktop-portal
app-arch/libarchive
"
BDEPEND="${DEPEND}"
27 changes: 14 additions & 13 deletions dist/get_deps_archlinux.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion dist/get_deps_debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ apt install -y \
libdbus-1-dev \
libcurl4-gnutls-dev \
libgtk-3-dev \
ninja-build
ninja-build \
libarchive-dev
3 changes: 2 additions & 1 deletion dist/get_deps_fedora.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dnf install -y \
glfw-devel \
lld \
mbedtls-devel \
gtk3-devel
gtk3-devel \
libarchive-devel
3 changes: 2 additions & 1 deletion dist/get_deps_msys2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions dist/macOS/arm64.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion dist/msys2/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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=()
Expand Down
43 changes: 2 additions & 41 deletions dist/net.werwolv.ImHex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions dist/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/external/libromfs
2 changes: 1 addition & 1 deletion lib/libimhex/source/api/plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions plugins/decompress/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
59 changes: 59 additions & 0 deletions plugins/decompress/source/content/pl_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <hex.hpp>

#include <hex/api/content_registry.hpp>
#include <pl/core/evaluator.hpp>
#include <pl/patterns/pattern.hpp>

#include <archive.h>
#include <archive_entry.h>

#include <wolv/utils/guards.hpp>

#include <vector>
#include <optional>

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<Token::Literal> {
const auto inputPattern = params[0].toPattern();
auto &section = evaluator->getSection(params[1].toUnsigned());

std::vector<u8> 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;
});
}

}
21 changes: 21 additions & 0 deletions plugins/decompress/source/plugin_decompress.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <hex/plugin.hpp>

#include <hex/api/content_registry.hpp>
#include <hex/helpers/logger.hpp>

#include <romfs/romfs.hpp>

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();
}

0 comments on commit e248915

Please sign in to comment.