From 6c41a4c9c849917fac9b980a8c58f9418575fe8c Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Wed, 18 Jan 2023 09:28:12 +0100 Subject: [PATCH] src: move compress files into a subdir; clang-format some more files; NFC --- .github/workflows/ci.yml | 3 +- CMakeLists.txt | 12 ++- Makefile | 2 +- src/Makefile | 6 +- ...dtx_rangeless.cpp => dtx_rangeless_fn.cpp} | 0 src/{ => compress}/compress.cpp | 89 +++++++------------ src/{ => compress}/compress.h | 34 +++++-- src/{ => compress}/compress_lzma.cpp | 20 ++--- src/{ => compress}/compress_ucl.cpp | 11 ++- src/{ => compress}/compress_zlib.cpp | 15 ++-- src/{ => compress}/compress_zstd.cpp | 6 +- src/conf.h | 16 ++-- src/filter.cpp | 76 ++++++---------- src/filter.h | 46 +++++----- src/{filteri.cpp => filter/filter_impl.cpp} | 52 +++++------ src/help.cpp | 2 +- src/main.cpp | 5 +- 17 files changed, 183 insertions(+), 212 deletions(-) rename src/check/{dtx_rangeless.cpp => dtx_rangeless_fn.cpp} (100%) rename src/{ => compress}/compress.cpp (71%) rename src/{ => compress}/compress.h (85%) rename src/{ => compress}/compress_lzma.cpp (97%) rename src/{ => compress}/compress_ucl.cpp (98%) rename src/{ => compress}/compress_zlib.cpp (97%) rename src/{ => compress}/compress_zstd.cpp (99%) rename src/{filteri.cpp => filter/filter_impl.cpp} (93%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b0a8fbd..7d43c596 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -293,7 +293,8 @@ jobs: set s=%H%\src cat .GITREV.txt set /p GITREV=<.GITREV.txt - cl -std:c++17 -Zc:__cplusplus -EHsc -J -O2 -W4 -WX -DUPX_VERSION_GITREV="""%GITREV%""" -DWITH_ZSTD %DEFS% -I%H%\vendor -I%H%\vendor\boost-pfr\include -Feupx.exe %s%\*.cpp %s%\check\*.cpp %s%\util\*.cpp %BDIR%\ucl\ucl.lib %BDIR%\zlib\zlib.lib %BDIR%\zstd\zstd.lib /link setargv.obj + set sources=%s%\*.cpp %s%\check\*.cpp %s%\compress\*.cpp %s%\filter\*.cpp %s%\util\*.cpp + cl -std:c++17 -Zc:__cplusplus -EHsc -J -O2 -W4 -WX -DUPX_VERSION_GITREV="""%GITREV%""" -DWITH_ZSTD %DEFS% -I%H%\vendor -I%H%\vendor\boost-pfr\include -Feupx.exe %sources% %BDIR%\ucl\ucl.lib %BDIR%\zlib\zlib.lib %BDIR%\zstd\zstd.lib /link setargv.obj - name: 'Make artifact' shell: bash run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ebc7c57..a2148605 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,7 +121,7 @@ add_library(upx_vendor_zstd STATIC ${zstd_SOURCES}) set_property(TARGET upx_vendor_zstd PROPERTY C_STANDARD 11) endif() -file(GLOB upx_SOURCES "src/*.cpp" "src/check/*.cpp" "src/util/*.cpp") +file(GLOB upx_SOURCES "src/*.cpp" "src/[cfu]*/*.cpp") list(SORT upx_SOURCES) add_executable(upx ${upx_SOURCES}) #target_compile_features(upx PRIVATE cxx_std_17) @@ -250,6 +250,8 @@ endif() # "ninja test" #*********************************************************************** +if(NOT UPX_CONFIG_CMAKE_DISABLE_TEST) + include(CTest) if(NOT CMAKE_CROSSCOMPILING) add_test(NAME upx-version COMMAND upx --version) @@ -279,12 +281,16 @@ if(NOT CMAKE_CROSSCOMPILING AND NOT UPX_CONFIG_DISABLE_SELF_PACK_TEST) upx_add_test(upx-run-packed ./upx-packed${exe} --version-short) endif() +endif() # UPX_CONFIG_CMAKE_DISABLE_TEST + #*********************************************************************** # "cmake --install ." # "make install" # "ninja install" #*********************************************************************** +if(NOT UPX_CONFIG_CMAKE_DISABLE_INSTALL) + # installation prefix and directories if(NOT CMAKE_INSTALL_PREFIX) #message(FATAL_ERROR "ERROR: CMAKE_INSTALL_PREFIX is not defined.") @@ -302,6 +308,8 @@ if(CMAKE_INSTALL_PREFIX AND DEFINED CMAKE_INSTALL_FULL_BINDIR) install(FILES doc/upx.1 DESTINATION "${CMAKE_INSTALL_FULL_MANDIR}/man1") endif() +endif() # UPX_CONFIG_CMAKE_DISABLE_INSTALL + #*********************************************************************** # finally print some info about the build configuration #*********************************************************************** @@ -317,11 +325,13 @@ function(print_var) endif() endforeach() endfunction() +if(NOT UPX_CONFIG_CMAKE_DISABLE_PRINT_INFO) print_var(CMAKE_HOST_SYSTEM_NAME CMAKE_HOST_SYSTEM_VERSION) print_var(CMAKE_SYSTEM_NAME CMAKE_SYSTEM_VERSION CMAKE_CROSSCOMPILING) print_var(CMAKE_C_COMPILER_ID CMAKE_C_COMPILER_VERSION CMAKE_C_COMPILER_ARCHITECTURE_ID CMAKE_C_PLATFORM_ID CMAKE_C_COMPILER_ABI) print_var(CMAKE_CXX_COMPILER_ID CMAKE_CXX_COMPILER_VERSION CMAKE_CXX_COMPILER_ARCHITECTURE_ID CMAKE_CXX_PLATFORM_ID CMAKE_CXX_COMPILER_ABI) print_var(CMAKE_INSTALL_PREFIX CMAKE_CONFIGURATION_TYPES CMAKE_BUILD_TYPE) +endif() # UPX_CONFIG_CMAKE_DISABLE_PRINT_INFO if (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release)$") message(WARNING "WARNING: unsupported CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}; please use \"Debug\" or \"Release\"") endif() diff --git a/Makefile b/Makefile index 46246791..1e5ecf01 100644 --- a/Makefile +++ b/Makefile @@ -136,5 +136,5 @@ build/extra/cross-windows-mingw64/%: UPX_CMAKE_CONFIG_FLAGS += -DUPX_CONFIG_DISA SUBMODULES = boost-pfr doctest lzma-sdk rangeless ucl valgrind zlib zstd -dummy := $(foreach m,$(SUBMODULES),$(if $(wildcard vendor/$(m)/[CL]*),,\ +dummy := $(foreach m,$(SUBMODULES),$(if $(wildcard vendor/$m/[CL]*),$m,\ $(error ERROR: missing git submodule $m; run 'git submodule update --init'))) diff --git a/src/Makefile b/src/Makefile index 79d4e06d..2139ec11 100644 --- a/src/Makefile +++ b/src/Makefile @@ -75,11 +75,11 @@ endif # automatically format some C++ source code files ifeq ($(shell uname),Linux) # Markus loves clang-format, but John hates it; find a compromise -CLANG_FORMAT_EXCLUDE_FILES += conf.h miniacc.h version.h -CLANG_FORMAT_EXCLUDE_FILES += compress.cpp compress.h filter.cpp filter.h filteri.cpp help.cpp +CLANG_FORMAT_EXCLUDE_FILES += conf.h miniacc.h version.h help.cpp CLANG_FORMAT_EXCLUDE_FILES += p_elf.h p_elf_enum.h p_lx_% p_mach% p_unix% p_vmli% CLANG_FORMAT_EXCLUDE_FILES += p_w32pe.cpp p_w64pep.cpp packer_c.cpp packer_f.cpp pefile% -CLANG_FORMAT_FILES := $(sort $(wildcard *.[ch]* ../maint/src/*.[ch]* check/*.[ch]* util/*.[ch]*)) +CLANG_FORMAT_EXCLUDE_FILES += compress/compress.h filter/filter_impl.cpp +CLANG_FORMAT_FILES := $(sort $(wildcard *.[ch]* ../maint/src/*.[ch]* [cu]*/*.[ch]*)) CLANG_FORMAT_FILES := $(filter-out $(CLANG_FORMAT_EXCLUDE_FILES),$(CLANG_FORMAT_FILES)) clang-format: PHONY $(CLANG_FORMAT_FILES) @echo "running upx-clang-format" diff --git a/src/check/dtx_rangeless.cpp b/src/check/dtx_rangeless_fn.cpp similarity index 100% rename from src/check/dtx_rangeless.cpp rename to src/check/dtx_rangeless_fn.cpp diff --git a/src/compress.cpp b/src/compress/compress.cpp similarity index 71% rename from src/compress.cpp rename to src/compress/compress.cpp index 1c7f02bf..e64b12d2 100644 --- a/src/compress.cpp +++ b/src/compress/compress.cpp @@ -3,7 +3,6 @@ This file is part of the UPX executable compressor. Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer - Copyright (C) 1996-2023 Laszlo Molnar All Rights Reserved. UPX and the UCL library are free software; you can redistribute them @@ -21,22 +20,19 @@ If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - Markus F.X.J. Oberhumer Laszlo Molnar - + Markus F.X.J. Oberhumer + */ - -#include "conf.h" +#include "../conf.h" #include "compress.h" -#include "util/membuffer.h" - +#include "../util/membuffer.h" /************************************************************************* // **************************************************************************/ -unsigned upx_adler32(const void *buf, unsigned len, unsigned adler) -{ +unsigned upx_adler32(const void *buf, unsigned len, unsigned adler) { if (len == 0) return adler; assert(buf != nullptr); @@ -47,8 +43,7 @@ unsigned upx_adler32(const void *buf, unsigned len, unsigned adler) #endif } - -#if 0 /* UNUSED */ +#if 0 // UNUSED unsigned upx_crc32(const void *buf, unsigned len, unsigned crc) { if (len == 0) @@ -60,24 +55,20 @@ unsigned upx_crc32(const void *buf, unsigned len, unsigned crc) return upx_zlib_crc32(buf, len, crc); #endif } -#endif /* UNUSED */ - +#endif // UNUSED /************************************************************************* // **************************************************************************/ -int upx_compress( const upx_bytep src, unsigned src_len, - upx_bytep dst, unsigned* dst_len, - upx_callback_p cb, - int method, int level, - const upx_compress_config_t *cconf, - upx_compress_result_t *cresult ) -{ +int upx_compress(const upx_bytep src, unsigned src_len, upx_bytep dst, unsigned *dst_len, + upx_callback_p cb, int method, int level, const upx_compress_config_t *cconf, + upx_compress_result_t *cresult) { int r = UPX_E_ERROR; upx_compress_result_t cresult_buffer; - assert(method > 0); assert(level > 0); + assert(method > 0); + assert(level > 0); #if 1 // set available bytes in dst @@ -94,65 +85,57 @@ int upx_compress( const upx_bytep src, unsigned src_len, cresult = &cresult_buffer; memset(cresult, 0, sizeof(*cresult)); #if 1 - // debug - cresult->method = method; - cresult->level = level; - cresult->u_len = src_len; - cresult->c_len = 0; + // debugging aid + cresult->debug.method = method; + cresult->debug.level = level; + cresult->debug.u_len = src_len; + cresult->debug.c_len = 0; #endif - if __acc_cte(0) { + if (__acc_cte(false)) { } #if (WITH_LZMA) else if (M_IS_LZMA(method)) - r = upx_lzma_compress(src, src_len, dst, dst_len, - cb, method, level, cconf, cresult); + r = upx_lzma_compress(src, src_len, dst, dst_len, cb, method, level, cconf, cresult); #endif #if (WITH_NRV) else if ((M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method)) && !opt->prefer_ucl) - r = upx_nrv_compress(src, src_len, dst, dst_len, - cb, method, level, cconf, cresult); + r = upx_nrv_compress(src, src_len, dst, dst_len, cb, method, level, cconf, cresult); #endif #if (WITH_UCL) else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method)) - r = upx_ucl_compress(src, src_len, dst, dst_len, - cb, method, level, cconf, cresult); + r = upx_ucl_compress(src, src_len, dst, dst_len, cb, method, level, cconf, cresult); #endif #if (WITH_ZSTD) else if (M_IS_ZSTD(method)) - r = upx_zstd_compress(src, src_len, dst, dst_len, - cb, method, level, cconf, cresult); + r = upx_zstd_compress(src, src_len, dst, dst_len, cb, method, level, cconf, cresult); #endif else { throwInternalError("unknown compression method"); } #if 1 - // debug - cresult->c_len = *dst_len; + // debugging aid + cresult->debug.c_len = *dst_len; #endif return r; } - /************************************************************************* // **************************************************************************/ -int upx_decompress(const upx_bytep src, unsigned src_len, - upx_bytep dst, unsigned* dst_len, - int method, - const upx_compress_result_t *cresult ) -{ +int upx_decompress(const upx_bytep src, unsigned src_len, upx_bytep dst, unsigned *dst_len, + int method, const upx_compress_result_t *cresult) { int r = UPX_E_ERROR; assert(*dst_len > 0); assert(src_len < *dst_len); // must be compressed - if (cresult && cresult->method == 0) + if (cresult && cresult->debug.method == 0) cresult = nullptr; - if __acc_cte(0) { + if (__acc_cte(false)) { } #if (WITH_LZMA) else if (M_IS_LZMA(method)) @@ -181,29 +164,23 @@ int upx_decompress(const upx_bytep src, unsigned src_len, return r; } - /************************************************************************* // **************************************************************************/ -int upx_test_overlap( const upx_bytep buf, - const upx_bytep tbuf, - unsigned src_off, unsigned src_len, - unsigned* dst_len, - int method, - const upx_compress_result_t *cresult ) -{ +int upx_test_overlap(const upx_bytep buf, const upx_bytep tbuf, unsigned src_off, unsigned src_len, + unsigned *dst_len, int method, const upx_compress_result_t *cresult) { int r = UPX_E_ERROR; - if (cresult && cresult->method == 0) + if (cresult && cresult->debug.method == 0) cresult = nullptr; assert(*dst_len > 0); assert(src_len < *dst_len); // must be compressed unsigned overlap_overhead = src_off + src_len - *dst_len; - assert((int)overlap_overhead > 0); + assert((int) overlap_overhead > 0); - if __acc_cte(0) { + if (__acc_cte(false)) { } #if (WITH_LZMA) else if (M_IS_LZMA(method)) diff --git a/src/compress.h b/src/compress/compress.h similarity index 85% rename from src/compress.h rename to src/compress/compress.h index f2c1c519..5e891c40 100644 --- a/src/compress.h +++ b/src/compress/compress.h @@ -3,7 +3,6 @@ This file is part of the UPX executable compressor. Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer - Copyright (C) 1996-2023 Laszlo Molnar All Rights Reserved. UPX and the UCL library are free software; you can redistribute them @@ -21,19 +20,40 @@ If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - Markus F.X.J. Oberhumer Laszlo Molnar - + Markus F.X.J. Oberhumer + */ - -#ifndef __UPX_COMPRESS_H -#define __UPX_COMPRESS_H 1 - +#pragma once +#ifndef UPX_COMPRESS_H__ +#define UPX_COMPRESS_H__ 1 /************************************************************************* // **************************************************************************/ +#if (WITH_BZIP2) +int upx_bzip2_init(void); +const char *upx_bzip2_version_string(void); +int upx_bzip2_compress ( const upx_bytep src, unsigned src_len, + upx_bytep dst, unsigned* dst_len, + upx_callback_p cb, + int method, int level, + const upx_compress_config_t *cconf, + upx_compress_result_t *cresult ); +int upx_bzip2_decompress ( const upx_bytep src, unsigned src_len, + upx_bytep dst, unsigned* dst_len, + int method, + const upx_compress_result_t *cresult ); +int upx_bzip2_test_overlap ( const upx_bytep buf, + const upx_bytep tbuf, + unsigned src_off, unsigned src_len, + unsigned* dst_len, + int method, + const upx_compress_result_t *cresult ); +#endif + + #if (WITH_LZMA) int upx_lzma_init(void); const char *upx_lzma_version_string(void); diff --git a/src/compress_lzma.cpp b/src/compress/compress_lzma.cpp similarity index 97% rename from src/compress_lzma.cpp rename to src/compress/compress_lzma.cpp index 0f40357b..9d8f4de5 100644 --- a/src/compress_lzma.cpp +++ b/src/compress/compress_lzma.cpp @@ -3,7 +3,6 @@ This file is part of the UPX executable compressor. Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer - Copyright (C) 1996-2023 Laszlo Molnar All Rights Reserved. UPX and the UCL library are free software; you can redistribute them @@ -21,13 +20,13 @@ If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - Markus F.X.J. Oberhumer Laszlo Molnar - + Markus F.X.J. Oberhumer + */ -#include "conf.h" +#include "../conf.h" #include "compress.h" -#include "util/membuffer.h" +#include "../util/membuffer.h" #if (ACC_CC_CLANG) #pragma clang diagnostic ignored "-Wshadow" @@ -453,7 +452,7 @@ int upx_lzma_decompress(const upx_bytep src, unsigned src_len, upx_bytep dst, un src_len -= 2; if (cresult) { - assert(cresult->method == method); + assert(cresult->debug.method == method); assert(cresult->result_lzma.pos_bits == (unsigned) s.Properties.pb); assert(cresult->result_lzma.lit_pos_bits == (unsigned) s.Properties.lp); assert(cresult->result_lzma.lit_context_bits == (unsigned) s.Properties.lc); @@ -518,14 +517,7 @@ int upx_lzma_test_overlap(const upx_bytep buf, const upx_bytep tbuf, unsigned sr int upx_lzma_init(void) { return 0; } -const char *upx_lzma_version_string(void) { -#if (WITH_LZMA == 0x443) - return "4.43"; -#else -#error "unknown WITH_LZMA version" - return nullptr; -#endif -} +const char *upx_lzma_version_string(void) { return "4.43"; } /************************************************************************* // doctest checks diff --git a/src/compress_ucl.cpp b/src/compress/compress_ucl.cpp similarity index 98% rename from src/compress_ucl.cpp rename to src/compress/compress_ucl.cpp index 58a1158b..4bf942ba 100644 --- a/src/compress_ucl.cpp +++ b/src/compress/compress_ucl.cpp @@ -3,7 +3,6 @@ This file is part of the UPX executable compressor. Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer - Copyright (C) 1996-2023 Laszlo Molnar All Rights Reserved. UPX and the UCL library are free software; you can redistribute them @@ -21,11 +20,11 @@ If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - Markus F.X.J. Oberhumer Laszlo Molnar - + Markus F.X.J. Oberhumer + */ -#include "conf.h" +#include "../conf.h" #include "compress.h" /************************************************************************* @@ -261,7 +260,7 @@ unsigned upx_ucl_adler32(const void *buf, unsigned len, unsigned adler) { return ucl_adler32(adler, (const ucl_bytep) buf, len); } -#if 0 /* UNUSED */ +#if 0 // UNUSED unsigned upx_ucl_crc32(const void *buf, unsigned len, unsigned crc) { return ucl_crc32(crc, (const ucl_bytep) buf, len); } @@ -273,7 +272,7 @@ unsigned upx_ucl_crc32(const void *buf, unsigned len, unsigned crc) { #if DEBUG && !defined(DOCTEST_CONFIG_DISABLE) && 1 -#include "util/membuffer.h" +#include "../util/membuffer.h" static bool check_ucl(const int method, const unsigned expected_c_len) { const unsigned u_len = 16384; diff --git a/src/compress_zlib.cpp b/src/compress/compress_zlib.cpp similarity index 97% rename from src/compress_zlib.cpp rename to src/compress/compress_zlib.cpp index 994cc926..4ab44649 100644 --- a/src/compress_zlib.cpp +++ b/src/compress/compress_zlib.cpp @@ -3,7 +3,6 @@ This file is part of the UPX executable compressor. Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer - Copyright (C) 1996-2023 Laszlo Molnar All Rights Reserved. UPX and the UCL library are free software; you can redistribute them @@ -21,13 +20,13 @@ If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - Markus F.X.J. Oberhumer Laszlo Molnar - + Markus F.X.J. Oberhumer + */ -#include "conf.h" +#include "../conf.h" #include "compress.h" -#include "util/membuffer.h" +#include "../util/membuffer.h" #include #include @@ -224,13 +223,13 @@ int upx_zlib_init(void) { const char *upx_zlib_version_string(void) { return zlibVersion(); } -#if 0 /* UNUSED */ +#if 0 // UNUSED unsigned upx_zlib_adler32(const void *buf, unsigned len, unsigned adler) { return adler32(adler, (const Bytef *) buf, len); } #endif -#if 0 /* UNUSED */ +#if 0 // UNUSED unsigned upx_zlib_crc32(const void *buf, unsigned len, unsigned crc) { return crc32(crc, (const Bytef *) buf, len); } @@ -242,8 +241,6 @@ unsigned upx_zlib_crc32(const void *buf, unsigned len, unsigned crc) { #if DEBUG && !defined(DOCTEST_CONFIG_DISABLE) && 1 -#include "util/membuffer.h" - static bool check_zlib(const int method, const int level, const unsigned expected_c_len) { const unsigned u_len = 16384; const unsigned c_extra = 4096; diff --git a/src/compress_zstd.cpp b/src/compress/compress_zstd.cpp similarity index 99% rename from src/compress_zstd.cpp rename to src/compress/compress_zstd.cpp index 367eef8f..249fe0a7 100644 --- a/src/compress_zstd.cpp +++ b/src/compress/compress_zstd.cpp @@ -24,13 +24,13 @@ */ -#include "conf.h" +#include "../conf.h" void zstd_compress_config_t::reset() { mem_clear(this, sizeof(*this)); } #if WITH_ZSTD #include "compress.h" -#include "util/membuffer.h" +#include "../util/membuffer.h" #include #include #include @@ -161,8 +161,6 @@ const char *upx_zstd_version_string(void) { return ZSTD_VERSION_STRING; } #if DEBUG && !defined(DOCTEST_CONFIG_DISABLE) && 1 -#include "util/membuffer.h" - static bool check_zstd(const int method, const int level, const unsigned expected_c_len) { const unsigned u_len = 16384; const unsigned c_extra = 4096; diff --git a/src/conf.h b/src/conf.h index 6e904121..f005b807 100644 --- a/src/conf.h +++ b/src/conf.h @@ -583,7 +583,7 @@ constexpr bool string_ge(const char *a, const char *b) { // compression - setup and callback_t **************************************************************************/ -#define WITH_LZMA 0x443 +#define WITH_LZMA 1 #define WITH_UCL 1 #define WITH_ZLIB 1 #if (WITH_UCL) @@ -755,9 +755,11 @@ struct zstd_compress_result_t struct upx_compress_result_t { - // debug - int method, level; - unsigned u_len, c_len; + // debugging aid + struct { + int method, level; + unsigned u_len, c_len; + } debug; lzma_compress_result_t result_lzma; ucl_compress_result_t result_ucl; @@ -765,7 +767,7 @@ struct upx_compress_result_t zstd_compress_result_t result_zstd; void reset() { - memset(this, 0, sizeof(*this)); + memset(&this->debug, 0, sizeof(this->debug)); result_lzma.reset(); result_ucl.reset(); result_zlib.reset(); result_zstd.reset(); } }; @@ -831,9 +833,9 @@ void show_license(); void show_usage(); void show_version(bool one_line=false); -// compress.cpp +// compress/compress.cpp unsigned upx_adler32(const void *buf, unsigned len, unsigned adler=1); -unsigned upx_crc32(const void *buf, unsigned len, unsigned crc=0); +unsigned upx_crc32 (const void *buf, unsigned len, unsigned crc=0); int upx_compress ( const upx_bytep src, unsigned src_len, upx_bytep dst, unsigned* dst_len, diff --git a/src/filter.cpp b/src/filter.cpp index 070c61cc..af1da56a 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -25,42 +25,34 @@ */ - #include "conf.h" #include "filter.h" #include "file.h" - /************************************************************************* // util **************************************************************************/ -static //inline -void initFilter(Filter *f, upx_byte *buf, unsigned buf_len) -{ +static void initFilter(Filter *f, upx_byte *buf, unsigned buf_len) { f->buf = buf; f->buf_len = buf_len; // clear output parameters f->calls = f->wrongcalls = f->noncalls = f->firstcall = f->lastcall = 0; } - /************************************************************************* // get a FilterEntry **************************************************************************/ -const FilterImp::FilterEntry *FilterImp::getFilter(int id) -{ +const FilterImpl::FilterEntry *FilterImpl::getFilter(int id) { static bool done = false; static unsigned char filter_map[256]; - if (!done) - { + if (!done) { // init the filter_map[] - assert(n_filters <= 254); // as 0xff means "empty slot" + assert(n_filters <= 254); // as 0xff means "empty slot" memset(filter_map, 0xff, sizeof(filter_map)); - for (int i = 0; i < n_filters; i++) - { + for (int i = 0; i < n_filters; i++) { int filter_id = filters[i].id; assert(filter_id >= 0 && filter_id <= 255); assert(filter_map[filter_id] == 0xff); @@ -72,21 +64,18 @@ const FilterImp::FilterEntry *FilterImp::getFilter(int id) if (id < 0 || id > 255) return nullptr; unsigned index = filter_map[id]; - if (index == 0xff) // empty slot + if (index == 0xff) // empty slot return nullptr; assert(filters[index].id == id); return &filters[index]; } - -bool Filter::isValidFilter(int filter_id) -{ - const FilterImp::FilterEntry * const fe = FilterImp::getFilter(filter_id); +bool Filter::isValidFilter(int filter_id) { + const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(filter_id); return fe != nullptr; } -bool Filter::isValidFilter(int filter_id, const int *allowed_filters) -{ +bool Filter::isValidFilter(int filter_id, const int *allowed_filters) { if (!isValidFilter(filter_id)) return false; if (filter_id == 0) @@ -99,13 +88,11 @@ bool Filter::isValidFilter(int filter_id, const int *allowed_filters) return false; } - /************************************************************************* // high level API **************************************************************************/ -void Filter::init(int id_, unsigned addvalue_) -{ +void Filter::init(int id_, unsigned addvalue_) { this->id = id_; initFilter(this, nullptr, 0); // clear input parameters @@ -116,12 +103,10 @@ void Filter::init(int id_, unsigned addvalue_) this->n_mru = 0; } - -bool Filter::filter(upx_byte *buf_, unsigned buf_len_) -{ +bool Filter::filter(upx_byte *buf_, unsigned buf_len_) { initFilter(this, buf_, buf_len_); - const FilterImp::FilterEntry * const fe = FilterImp::getFilter(id); + const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id); if (fe == nullptr) throwInternalError("filter-1"); if (fe->id == 0) @@ -138,10 +123,10 @@ bool Filter::filter(upx_byte *buf_, unsigned buf_len_) if (clevel != 1) this->adler = upx_adler32(this->buf, this->buf_len); - //printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len); - //OutputFile::dump("filter.dat", buf, buf_len); + NO_printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len); + // OutputFile::dump("filter.dat", buf, buf_len); int r = (*fe->do_filter)(this); - //printf("filter: %02x %d\n", fe->id, r); + NO_printf("filter: %02x %d\n", fe->id, r); if (r > 0) throwFilterException(); if (r == 0) @@ -149,12 +134,10 @@ bool Filter::filter(upx_byte *buf_, unsigned buf_len_) return false; } - -void Filter::unfilter(upx_byte *buf_, unsigned buf_len_, bool verify_checksum) -{ +void Filter::unfilter(upx_byte *buf_, unsigned buf_len_, bool verify_checksum) { initFilter(this, buf_, buf_len_); - const FilterImp::FilterEntry * const fe = FilterImp::getFilter(id); + const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id); if (fe == nullptr) throwInternalError("unfilter-1"); if (fe->id == 0) @@ -166,24 +149,21 @@ void Filter::unfilter(upx_byte *buf_, unsigned buf_len_, bool verify_checksum) if (!fe->do_unfilter) throwInternalError("unfilter-2"); - //printf("unfilter: %02x %p %d\n", this->id, this->buf, this->buf_len); + NO_printf("unfilter: %02x %p %d\n", this->id, this->buf, this->buf_len); int r = (*fe->do_unfilter)(this); - //printf("unfilter: %02x %d\n", fe->id, r); + NO_printf("unfilter: %02x %d\n", fe->id, r); if (r != 0) throwInternalError("unfilter-3"); - //OutputFile::dump("unfilter.dat", buf, buf_len); + // OutputFile::dump("unfilter.dat", buf, buf_len); // verify checksum - if (verify_checksum && clevel != 1) - { + if (verify_checksum && clevel != 1) { if (this->adler != upx_adler32(this->buf, this->buf_len)) throwInternalError("unfilter-4"); } } - -void Filter::verifyUnfilter() -{ +void Filter::verifyUnfilter() { // Note: // This verify is just because of complete paranoia that there // could be a hidden bug in the filter implementation, and @@ -193,20 +173,18 @@ void Filter::verifyUnfilter() // See also: // Packer::verifyOverlappingDecompression() - //printf("verifyUnfilter: %02x %p %d\n", this->id, this->buf, this->buf_len); + NO_printf("verifyUnfilter: %02x %p %d\n", this->id, this->buf, this->buf_len); if (clevel != 1) unfilter(this->buf, this->buf_len, true); } - -bool Filter::scan(const upx_byte *buf_, unsigned buf_len_) -{ +bool Filter::scan(const upx_byte *buf_, unsigned buf_len_) { // Note: must use const_cast here. This is fine as the scan // implementations (fe->do_scan) actually don't change the buffer. upx_byte *b = const_cast(buf_); initFilter(this, b, buf_len_); - const FilterImp::FilterEntry * const fe = FilterImp::getFilter(id); + const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id); if (fe == nullptr) throwInternalError("scan-1"); if (fe->id == 0) @@ -218,9 +196,9 @@ bool Filter::scan(const upx_byte *buf_, unsigned buf_len_) if (!fe->do_scan) throwInternalError("scan-2"); - //printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len); + NO_printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len); int r = (*fe->do_scan)(this); - //printf("filter: %02x %d\n", fe->id, r); + NO_printf("filter: %02x %d\n", fe->id, r); if (r > 0) throwFilterException(); if (r == 0) diff --git a/src/filter.h b/src/filter.h index af49194a..d0cc5c2a 100644 --- a/src/filter.h +++ b/src/filter.h @@ -25,13 +25,9 @@ */ - -#ifndef __UPX_FILTER_H -#define __UPX_FILTER_H 1 - -class Filter; -class FilterImp; - +#pragma once +#ifndef UPX_FILTER_H__ +#define UPX_FILTER_H__ 1 /************************************************************************* // A filter is a reversible operation that modifies a given @@ -50,14 +46,16 @@ class FilterImp; // to absolute addresses so that the buffer compresses better. **************************************************************************/ -class Filter -{ +class Filter { public: - Filter(int level) { clevel = level; init(); } - void init(int id=0, unsigned addvalue=0); + Filter(int level) { + clevel = level; + init(); + } + void init(int id = 0, unsigned addvalue = 0); bool filter(upx_byte *buf, unsigned buf_len); - void unfilter(upx_byte *buf, unsigned buf_len, bool verify_checksum=false); + void unfilter(upx_byte *buf, unsigned buf_len, bool verify_checksum = false); void verifyUnfilter(); bool scan(const upx_byte *buf, unsigned buf_len); @@ -79,7 +77,7 @@ public: const int *preferred_ctos = nullptr; // Input/output parameters used by various filters - unsigned char cto; // call trick offset + unsigned char cto; // call trick offset // Output used by various filters. Read only. unsigned calls; @@ -87,16 +85,15 @@ public: unsigned wrongcalls; unsigned firstcall; unsigned lastcall; - unsigned n_mru; // ctojr only + unsigned n_mru; // ctojr only // Read only. int id; private: - int clevel; // compression level + int clevel; // compression level }; - /************************************************************************* // We don't want a full OO interface here because of // certain implementation speed reasons. @@ -104,19 +101,17 @@ private: // This class is private to Filter - don't look. **************************************************************************/ -class FilterImp -{ +class FilterImpl { friend class Filter; private: - struct FilterEntry - { - int id; // 0 .. 255 + struct FilterEntry { + int id; // 0 .. 255 unsigned min_buf_len; unsigned max_buf_len; - int (*do_filter)(Filter *); // filter a buffer - int (*do_unfilter)(Filter *); // unfilter a buffer - int (*do_scan)(Filter *); // scan a buffer + int (*do_filter)(Filter *); // filter a buffer + int (*do_unfilter)(Filter *); // unfilter a buffer + int (*do_scan)(Filter *); // scan a buffer }; // get a specific filter entry @@ -125,10 +120,9 @@ private: private: // strictly private filter database static const FilterEntry filters[]; - static const int n_filters; // number of filters[] + static const int n_filters; // number of filters[] }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/filteri.cpp b/src/filter/filter_impl.cpp similarity index 93% rename from src/filteri.cpp rename to src/filter/filter_impl.cpp index 5b5c6d17..b91b618d 100644 --- a/src/filteri.cpp +++ b/src/filter/filter_impl.cpp @@ -1,4 +1,4 @@ -/* filteri.cpp -- filter implementation (low-level) +/* filter_impl.cpp -- filter implementation (low-level) This file is part of the UPX executable compressor. @@ -26,8 +26,8 @@ */ -#include "conf.h" -#include "filter.h" +#include "../conf.h" +#include "../filter.h" static unsigned umin(unsigned const a, unsigned const b) @@ -45,20 +45,20 @@ umin(unsigned const a, unsigned const b) // util **************************************************************************/ -#include "filter/getcto.h" +#include "getcto.h" /************************************************************************* // simple filters: calltrick / swaptrick / delta / ... **************************************************************************/ -#include "filter/ct.h" -#include "filter/sw.h" -#include "filter/ctsw.h" +#include "ct.h" +#include "sw.h" +#include "ctsw.h" -#include "filter/sub8.h" -#include "filter/sub16.h" -#include "filter/sub32.h" +#include "sub8.h" +#include "sub16.h" +#include "sub32.h" /************************************************************************* @@ -68,25 +68,25 @@ umin(unsigned const a, unsigned const b) #define COND(b,x) (b[x] == 0xe8) #define F f_cto32_e8_bswap_le #define U u_cto32_e8_bswap_le -#include "filter/cto.h" +#include "cto.h" #define F s_cto32_e8_bswap_le -#include "filter/cto.h" +#include "cto.h" #undef COND #define COND(b,x) (b[x] == 0xe9) #define F f_cto32_e9_bswap_le #define U u_cto32_e9_bswap_le -#include "filter/cto.h" +#include "cto.h" #define F s_cto32_e9_bswap_le -#include "filter/cto.h" +#include "cto.h" #undef COND #define COND(b,x) (b[x] == 0xe8 || b[x] == 0xe9) #define F f_cto32_e8e9_bswap_le #define U u_cto32_e8e9_bswap_le -#include "filter/cto.h" +#include "cto.h" #define F s_cto32_e8e9_bswap_le -#include "filter/cto.h" +#include "cto.h" #undef COND @@ -97,9 +97,9 @@ umin(unsigned const a, unsigned const b) #define COND(b,x,lastcall) (b[x] == 0xe8 || b[x] == 0xe9) #define F f_ctoj32_e8e9_bswap_le #define U u_ctoj32_e8e9_bswap_le -#include "filter/ctoj.h" +#include "ctoj.h" #define F s_ctoj32_e8e9_bswap_le -#include "filter/ctoj.h" +#include "ctoj.h" #undef COND @@ -112,9 +112,9 @@ umin(unsigned const a, unsigned const b) #define COND(b,x,lc,id) (COND1(b,x) || ((9<=(0xf&(id))) && COND2(b,x,lc))) #define F f_ctok32_e8e9_bswap_le #define U u_ctok32_e8e9_bswap_le -#include "filter/ctok.h" +#include "ctok.h" #define F s_ctok32_e8e9_bswap_le -#include "filter/ctok.h" +#include "ctok.h" #undef COND #undef COND2 #undef COND1 @@ -138,9 +138,9 @@ umin(unsigned const a, unsigned const b) #define F f_ctojr32_e8e9_bswap_le #define U u_ctojr32_e8e9_bswap_le -#include "filter/ctojr.h" +#include "ctojr.h" #define F s_ctojr32_e8e9_bswap_le -#include "filter/ctojr.h" +#include "ctojr.h" #undef CONDU #undef CONDF @@ -158,9 +158,9 @@ umin(unsigned const a, unsigned const b) #define COND(b,x) (18==(get_be32(b+x)>>26)) #define F f_ppcbxx #define U u_ppcbxx -#include "filter/ppcbxx.h" +#include "ppcbxx.h" #define F s_ppcbxx -#include "filter/ppcbxx.h" +#include "ppcbxx.h" #undef COND @@ -168,7 +168,7 @@ umin(unsigned const a, unsigned const b) // database for use in class Filter **************************************************************************/ -const FilterImp::FilterEntry FilterImp::filters[] = { +const FilterImpl::FilterEntry FilterImpl::filters[] = { // no filter { 0x00, 0, 0, nullptr, nullptr, nullptr }, @@ -261,6 +261,6 @@ const FilterImp::FilterEntry FilterImp::filters[] = { { 0xd0, 8, 0, f_ppcbxx, u_ppcbxx, s_ppcbxx }, }; -const int FilterImp::n_filters = TABLESIZE(filters); +const int FilterImpl::n_filters = TABLESIZE(filters); /* vim:set ts=4 sw=4 et: */ diff --git a/src/help.cpp b/src/help.cpp index 5e85fbbd..7a09968e 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -27,9 +27,9 @@ #include "conf.h" -#include "compress.h" #include "packmast.h" #include "packer.h" +#include "compress/compress.h" // upx_ucl_version_string() /************************************************************************* diff --git a/src/main.cpp b/src/main.cpp index 9f907966..319652a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,10 +28,10 @@ // main entry, mostly boring stuff; see work.cpp for actual action #include "conf.h" -#include "compress.h" #include "file.h" #include "packer.h" #include "p_elf.h" +#include "compress/compress.h" // upx_ucl_init() /************************************************************************* // options @@ -1207,6 +1207,9 @@ int upx_main(int argc, char *argv[]) { set_term(stderr); +#if (WITH_BZIP2) + assert(upx_bzip2_init() == 0); +#endif assert(upx_lzma_init() == 0); #if (WITH_NRV) assert(upx_nrv_init() == 0);