From 10e759f1f1dd9264e8505d856abedc7586804ee1 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Fri, 11 Aug 2023 13:32:53 +0200 Subject: [PATCH] src: minor cleanups --- .github/workflows/ci.yml | 4 ++-- .github/workflows/weekly-ci-zigcc.yml | 4 ++-- src/packer.cpp | 12 +++++------- src/packer.h | 12 ++++++------ src/packhead.cpp | 15 ++++++++++----- src/packhead.h | 16 ++++++++++------ src/stub/src/include/header.S | 18 +++++++++++++----- src/util/xspan_impl_ptr.h | 4 ++-- 8 files changed, 50 insertions(+), 35 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f447b742..dbfe3142 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,8 +12,8 @@ env: CMAKE_REQUIRED_QUIET: OFF DEBIAN_FRONTEND: noninteractive UPX_CMAKE_BUILD_FLAGS: --verbose - # 2023-08-09 - ZIG_DIST_VERSION: 0.12.0-dev.25+36c57c3ba + # 2023-08-10 + ZIG_DIST_VERSION: 0.12.0-dev.47+0461a64a9 jobs: job-rebuild-and-verify-stubs: diff --git a/.github/workflows/weekly-ci-zigcc.yml b/.github/workflows/weekly-ci-zigcc.yml index c3cb1438..264dc8f4 100644 --- a/.github/workflows/weekly-ci-zigcc.yml +++ b/.github/workflows/weekly-ci-zigcc.yml @@ -8,8 +8,8 @@ on: env: CMAKE_REQUIRED_QUIET: OFF DEBIAN_FRONTEND: noninteractive - # 2023-08-09 - ZIG_DIST_VERSION: 0.12.0-dev.25+36c57c3ba + # 2023-08-10 + ZIG_DIST_VERSION: 0.12.0-dev.47+0461a64a9 jobs: job-linux-zigcc: # uses cmake + make diff --git a/src/packer.cpp b/src/packer.cpp index 31f30fee..b80cc2bb 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -36,14 +36,13 @@ // **************************************************************************/ -Packer::Packer(InputFile *f) : PackerBase(f) { - if (fi != nullptr) - file_size = fi->st_size(); - mem_size_assert(1, file_size_u); - uip = new UiPacker(this); - mem_clear(&ph); +PackerBase::PackerBase(InputFile *f) : fi(f), file_size(f ? f->st.st_size : 0) { + ph.reset(); + mem_size_assert(1, file_size); } +Packer::Packer(InputFile *f) : PackerBase(f) { uip = new UiPacker(this); } + Packer::~Packer() noexcept { // owner owner_delete(uip); @@ -51,7 +50,6 @@ Packer::~Packer() noexcept { assert_noexcept(linker == nullptr); // references bele = nullptr; - fi = nullptr; } // for PackMaster diff --git a/src/packer.h b/src/packer.h index d5464018..3b4461e3 100644 --- a/src/packer.h +++ b/src/packer.h @@ -44,7 +44,7 @@ class Filter; class PackerBase { friend class UiPacker; protected: - explicit PackerBase(InputFile *f) noexcept : fi(f) {} + explicit PackerBase(InputFile *f); public: virtual ~PackerBase() noexcept {} // getVersion() enables detecting forward incompatibility of unpack() @@ -75,12 +75,12 @@ public: virtual void doFileInfo() = 0; protected: - InputFile *fi = nullptr; - union { // unnamed union - upx_int64_t file_size = 0; // must get set by constructor - upx_uint64_t file_size_u; // explicitly unsigned + InputFile *const fi; // reference + union { // unnamed union + const upx_int64_t file_size; // must get set by constructor + const upx_uint64_t file_size_u; // explicitly unsigned }; - PackHeader ph = PackHeader{}; // must be filled by canUnpack() + PackHeader ph; // must be filled by canUnpack() }; /************************************************************************* diff --git a/src/packhead.cpp b/src/packhead.cpp index 5d05be10..42c2d882 100644 --- a/src/packhead.cpp +++ b/src/packhead.cpp @@ -36,23 +36,28 @@ // least to detect older versions, so this is a little bit messy. **************************************************************************/ -PackHeader::PackHeader() noexcept : version(-1), format(-1) {} +PackHeader::PackHeader() noexcept { reset(); } + +void PackHeader::reset() noexcept { + mem_clear(this); + version = -1; + format = -1; + compress_result.reset(); +} /************************************************************************* -// very simple checksum for the header itself (since version 10) +// extremely simple checksum for the header itself (since version 10) **************************************************************************/ static byte get_packheader_checksum(SPAN_S(const byte) buf, int blen) { assert(blen >= 4); assert(get_le32(buf) == UPX_MAGIC_LE32); - // printf("1 %d\n", blen); buf += 4; blen -= 4; unsigned c = 0; while (blen-- > 0) c += *buf++; c %= 251; - // printf("2 %d\n", c); return (byte) c; } @@ -98,7 +103,7 @@ void PackHeader::putPackHeader(SPAN_S(byte) p) const { // sufficient space for the header. assert(get_le32(p) == UPX_MAGIC_LE32); if (get_le32(p + 4) != UPX_MAGIC2_LE32) { - // fprintf(stderr, "MAGIC2_LE32: %x %x\n", get_le32(p+4), UPX_MAGIC2_LE32); + NO_fprintf(stderr, "MAGIC2_LE32: %x %x\n", get_le32(p + 4), UPX_MAGIC2_LE32); throwBadLoader(); } diff --git a/src/packhead.h b/src/packhead.h index 1b213f24..491d8508 100644 --- a/src/packhead.h +++ b/src/packhead.h @@ -34,21 +34,22 @@ class Filter; // also see stub/src/include/header.S **************************************************************************/ -class PackHeader final { -public: +struct PackHeader final { explicit PackHeader() noexcept; ~PackHeader() noexcept = default; + void reset() noexcept; + void putPackHeader(SPAN_S(byte) p) const; bool decodePackHeaderFromBuf(SPAN_S(const byte) b, int blen); int getPackHeaderSize() const; - // fields stored in compressed file => see header.S + // fields stored in compressed file => see stub/src/include/header.S // enum { magic = UPX_MAGIC_LE32 }; int version; - int format; // executable format - int method; // compresison method + int format; // executable format UPX_F_xxx + int method; // compresison method M_xxx int level; // compresison level 1..10 unsigned u_len; unsigned c_len; @@ -57,7 +58,10 @@ public: unsigned u_file_size; int filter; int filter_cto; - int n_mru; // FIXME: rename to filter_misc + union { + int filter_misc; // generic name + int n_mru; // specific name for filter ctojr + }; int header_checksum; // support fields for verifying decompression diff --git a/src/stub/src/include/header.S b/src/stub/src/include/header.S index 0603e3fd..bf82962c 100644 --- a/src/stub/src/include/header.S +++ b/src/stub/src/include/header.S @@ -27,32 +27,40 @@ ; */ +// see PackHeader::putPackHeader() section UPX1HEAD .byte 85,80,88,33 // 0 UPX_MAGIC_LE32 - .byte 161,216,208,213 // UPX_MAGIC2_LE32 +#if 0 + .byte 161,216,208,213 // 4 UPX_MAGIC2_LE32 +#else + .byte 161 // 4 version + .byte 216 // 5 format + .byte 208 // 6 method + .byte 213 // 7 level +#endif .long 0 // 8 uncompressed adler32 .long 0 // 12 compressed adler32 #ifdef COM .short 0 // 16 uncompressed len .short 0 // 18 compressed len - .byte 0 // 20 filter + .byte 0 // 20 filter id .byte 45 // 21 header checksum #elif defined(EXE) .byte 0,0,0 // 16 uncompressed len .byte 0,0,0 // 19 compressed len .byte 0,0,0 // 22 original file size - .byte 0 // 25 filter + .byte 0 // 25 filter id .byte 45 // 26 header checksum #else .long 0 // 16 uncompressed len .long 0 // 20 compressed len .long 0 // 24 original file size .byte 0 // 28 filter id - .byte 0 // 29 filter cto - .byte 0 // unused + .byte 0 // 29 filter_cto + .byte 0 // 30 filter_misc / n_mru .byte 45 // 31 header checksum #endif diff --git a/src/util/xspan_impl_ptr.h b/src/util/xspan_impl_ptr.h index 8144d678..af35b0f3 100644 --- a/src/util/xspan_impl_ptr.h +++ b/src/util/xspan_impl_ptr.h @@ -109,7 +109,7 @@ public: // assignment Self &operator=(const Self &other) { return assign(other); } - // FIXME: this is not called !! + // FIXME: this is not called?? template XSPAN_REQUIRES_CONVERTIBLE_R(Self &) operator=(U *other) { @@ -117,7 +117,7 @@ public: return assign(Self(other)); } - // FIXME: this is not called !! + // FIXME: this is not called?? template XSPAN_REQUIRES_CONVERTIBLE_R(Self &) operator=(const CSelf &other) {