src: move compress files into a subdir; clang-format some more files; NFC

This commit is contained in:
Markus F.X.J. Oberhumer 2023-01-18 09:28:12 +01:00
parent 334be287fc
commit 6c41a4c9c8
17 changed files with 183 additions and 212 deletions

View File

@ -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: |

View File

@ -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()

View File

@ -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')))

View File

@ -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"

View File

@ -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@oberhumer.com> <ezerotven+github@gmail.com>
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
*/
#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))

View File

@ -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@oberhumer.com> <ezerotven+github@gmail.com>
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
*/
#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);

View File

@ -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@oberhumer.com> <ezerotven+github@gmail.com>
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
*/
#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

View File

@ -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@oberhumer.com> <ezerotven+github@gmail.com>
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
*/
#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;

View File

@ -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@oberhumer.com> <ezerotven+github@gmail.com>
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
*/
#include "conf.h"
#include "../conf.h"
#include "compress.h"
#include "util/membuffer.h"
#include "../util/membuffer.h"
#include <zlib/zlib.h>
#include <zlib/deflate.h>
@ -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;

View File

@ -24,13 +24,13 @@
<markus@oberhumer.com>
*/
#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 <zstd/lib/zstd.h>
#include <zstd/lib/zstd_errors.h>
#include <zstd/lib/compress/hist.h>
@ -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;

View File

@ -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,

View File

@ -25,42 +25,34 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com>
*/
#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<upx_byte *>(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)

View File

@ -25,13 +25,9 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com>
*/
#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: */

View File

@ -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: */

View File

@ -27,9 +27,9 @@
#include "conf.h"
#include "compress.h"
#include "packmast.h"
#include "packer.h"
#include "compress/compress.h" // upx_ucl_version_string()
/*************************************************************************

View File

@ -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);