diff --git a/src/bele.h b/src/bele.h index 36d9ae60..91078257 100644 --- a/src/bele.h +++ b/src/bele.h @@ -143,27 +143,27 @@ inline void set_le64(void *p, upx_uint64_t v) { set_ne64(p, ne64_to_le64(v)); } **************************************************************************/ inline unsigned get_be24(const void *p) { - const unsigned char *b = ACC_CCAST(const unsigned char *, p); + const byte *b = ACC_CCAST(const byte *, p); return (b[0] << 16) | (b[1] << 8) | (b[2] << 0); } inline unsigned get_le24(const void *p) { - const unsigned char *b = ACC_CCAST(const unsigned char *, p); + const byte *b = ACC_CCAST(const byte *, p); return (b[0] << 0) | (b[1] << 8) | (b[2] << 16); } inline void set_be24(void *p, unsigned v) { - unsigned char *b = ACC_PCAST(unsigned char *, p); - b[0] = ACC_ICONV(unsigned char, (v >> 16) & 0xff); - b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff); - b[2] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); + byte *b = ACC_PCAST(byte *, p); + b[0] = ACC_ICONV(byte, (v >> 16) & 0xff); + b[1] = ACC_ICONV(byte, (v >> 8) & 0xff); + b[2] = ACC_ICONV(byte, (v >> 0) & 0xff); } inline void set_le24(void *p, unsigned v) { - unsigned char *b = ACC_PCAST(unsigned char *, p); - b[0] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); - b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff); - b[2] = ACC_ICONV(unsigned char, (v >> 16) & 0xff); + byte *b = ACC_PCAST(byte *, p); + b[0] = ACC_ICONV(byte, (v >> 0) & 0xff); + b[1] = ACC_ICONV(byte, (v >> 8) & 0xff); + b[2] = ACC_ICONV(byte, (v >> 16) & 0xff); } inline unsigned get_le26(const void *p) { return get_le32(p) & 0x03ffffff; } @@ -250,7 +250,7 @@ inline upx_int64_t get_le64_signed(const void *p) { struct alignas(1) BE16 { typedef unsigned integral_conversion_type; // automatic conversion to unsigned - unsigned char d[2]; + byte d[2]; BE16 &operator=(unsigned v) { set_be16(d, v); @@ -300,7 +300,7 @@ struct alignas(1) BE16 { struct alignas(1) BE32 { typedef unsigned integral_conversion_type; // automatic conversion to unsigned - unsigned char d[4]; + byte d[4]; BE32 &operator=(unsigned v) { set_be32(d, v); @@ -350,7 +350,7 @@ struct alignas(1) BE32 { struct alignas(1) BE64 { typedef upx_uint64_t integral_conversion_type; // automatic conversion to upx_uint64_t - unsigned char d[8]; + byte d[8]; BE64 &operator=(upx_uint64_t v) { set_be64(d, v); @@ -400,7 +400,7 @@ struct alignas(1) BE64 { struct alignas(1) LE16 { typedef unsigned integral_conversion_type; // automatic conversion to unsigned - unsigned char d[2]; + byte d[2]; LE16 &operator=(unsigned v) { set_le16(d, v); @@ -450,7 +450,7 @@ struct alignas(1) LE16 { struct alignas(1) LE32 { typedef unsigned integral_conversion_type; // automatic conversion to unsigned - unsigned char d[4]; + byte d[4]; LE32 &operator=(unsigned v) { set_le32(d, v); @@ -500,7 +500,7 @@ struct alignas(1) LE32 { struct alignas(1) LE64 { typedef upx_uint64_t integral_conversion_type; // automatic conversion to upx_uint64_t - unsigned char d[8]; + byte d[8]; LE64 &operator=(upx_uint64_t v) { set_le64(d, v); diff --git a/src/check/dt_check.cpp b/src/check/dt_check.cpp index 7e0d1ae4..04b7751f 100644 --- a/src/check/dt_check.cpp +++ b/src/check/dt_check.cpp @@ -301,10 +301,10 @@ void upx_compiler_sanity_check(void) { assert(TestBELE::test()); assert(TestBELE::test()); { - alignas(16) static const unsigned char dd[32] = { + alignas(16) static const byte dd[32] = { 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, 0, 0, 0, 0, 0}; - const unsigned char *d; + const byte *d; const N_BELE_RTP::AbstractPolicy *bele; d = dd + 7; assert(upx_adler32(d, 4) == 0x09f003f7); diff --git a/src/conf.h b/src/conf.h index b5578db8..f3ed0cda 100644 --- a/src/conf.h +++ b/src/conf.h @@ -186,8 +186,9 @@ typedef acc_int64_t upx_int64_t; typedef acc_uint64_t upx_uint64_t; typedef acc_uintptr_t upx_uintptr_t; -typedef unsigned char upx_byte; -#define upx_bytep upx_byte * +typedef unsigned char byte; +#define upx_byte byte +#define upx_bytep byte * // protect against integer overflows and malicious header fields // see C 11 standard, Annex K @@ -834,6 +835,7 @@ void *membuffer_get_void_ptr(MemBuffer &mb); unsigned membuffer_get_size(MemBuffer &mb); // xspan +#include "util/raw_bytes.h" #include "util/xspan.h" // util/dt_check.cpp diff --git a/src/filter.cpp b/src/filter.cpp index b9e318e7..fdacb73d 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -33,7 +33,7 @@ // util **************************************************************************/ -static void initFilter(Filter *f, upx_byte *buf, unsigned buf_len) { +static void initFilter(Filter *f, byte *buf, unsigned buf_len) { f->buf = buf; f->buf_len = buf_len; // clear output parameters @@ -45,7 +45,7 @@ static void initFilter(Filter *f, upx_byte *buf, unsigned buf_len) { **************************************************************************/ const FilterImpl::FilterEntry *FilterImpl::getFilter(int id) { - static unsigned char filter_map[256]; + static upx_uint8_t filter_map[256]; static upx_std_once_flag init_done; upx_std_call_once(init_done, []() noexcept { @@ -56,7 +56,7 @@ const FilterImpl::FilterEntry *FilterImpl::getFilter(int id) { int filter_id = filters[i].id; assert(filter_id >= 0 && filter_id <= 255); assert(filter_map[filter_id] == 0xff); - filter_map[filter_id] = (unsigned char) i; + filter_map[filter_id] = (upx_uint8_t) i; } }); @@ -102,8 +102,8 @@ void Filter::init(int id_, unsigned addvalue_) { this->n_mru = 0; } -bool Filter::filter(SPAN_0(upx_byte) xbuf, unsigned buf_len_) { - upx_byte *const buf_ = raw_bytes(xbuf, buf_len_); +bool Filter::filter(SPAN_0(byte) xbuf, unsigned buf_len_) { + byte *const buf_ = raw_bytes(xbuf, buf_len_); initFilter(this, buf_, buf_len_); const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id); @@ -134,8 +134,8 @@ bool Filter::filter(SPAN_0(upx_byte) xbuf, unsigned buf_len_) { return false; } -void Filter::unfilter(SPAN_0(upx_byte) xbuf, unsigned buf_len_, bool verify_checksum) { - upx_byte *const buf_ = raw_bytes(xbuf, buf_len_); +void Filter::unfilter(SPAN_0(byte) xbuf, unsigned buf_len_, bool verify_checksum) { + byte *const buf_ = raw_bytes(xbuf, buf_len_); initFilter(this, buf_, buf_len_); const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id); @@ -179,11 +179,11 @@ void Filter::verifyUnfilter() { unfilter(this->buf, this->buf_len, true); } -bool Filter::scan(SPAN_0(const upx_byte) xbuf, unsigned buf_len_) { - const upx_byte *const buf_ = raw_bytes(xbuf, buf_len_); +bool Filter::scan(SPAN_0(const byte) xbuf, unsigned buf_len_) { + const byte *const buf_ = raw_bytes(xbuf, 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 *const b = const_cast(buf_); + byte *const b = const_cast(buf_); initFilter(this, b, buf_len_); const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id); diff --git a/src/filter.h b/src/filter.h index 0593544c..2496b715 100644 --- a/src/filter.h +++ b/src/filter.h @@ -26,8 +26,6 @@ */ #pragma once -#ifndef UPX_FILTER_H__ -#define UPX_FILTER_H__ 1 /************************************************************************* // A filter is a reversible operation that modifies a given @@ -54,10 +52,10 @@ public: } void init(int id = 0, unsigned addvalue = 0); - bool filter(SPAN_0(upx_byte) buf, unsigned buf_len); - void unfilter(SPAN_0(upx_byte) buf, unsigned buf_len, bool verify_checksum = false); + bool filter(SPAN_0(byte) buf, unsigned buf_len); + void unfilter(SPAN_0(byte) buf, unsigned buf_len, bool verify_checksum = false); void verifyUnfilter(); - bool scan(SPAN_0(const upx_byte) buf, unsigned buf_len); + bool scan(SPAN_0(const byte) buf, unsigned buf_len); static bool isValidFilter(int filter_id); static bool isValidFilter(int filter_id, const int *allowed_filters); @@ -65,7 +63,7 @@ public: public: // Will be set by each call to filter()/unfilter(). // Read-only afterwards. - upx_byte *buf = nullptr; + byte *buf = nullptr; unsigned buf_len = 0; // Checksum of the buffer before applying the filter @@ -77,7 +75,7 @@ public: const int *preferred_ctos = nullptr; // Input/output parameters used by various filters - unsigned char cto; // call trick offset + byte cto; // call trick offset // Output used by various filters. Read only. unsigned calls; @@ -123,6 +121,4 @@ private: static const int n_filters; // number of filters[] }; -#endif /* already included */ - /* vim:set ts=4 sw=4 et: */ diff --git a/src/filter/ct.h b/src/filter/ct.h index 8c58ab91..0a8be429 100644 --- a/src/filter/ct.h +++ b/src/filter/ct.h @@ -32,8 +32,8 @@ **************************************************************************/ #define CT16(f, cond, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 3; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 3; \ do { \ if (cond) \ { \ @@ -211,8 +211,8 @@ static int s_ct16_e8e9_bswap_be(Filter *f) **************************************************************************/ #define CT32(f, cond, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 5; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 5; \ do { \ if (cond) \ { \ @@ -388,8 +388,8 @@ static int s_ct32_e8e9_bswap_be(Filter *f) **************************************************************************/ #define CT24ARM_LE(f, cond, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 4; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 4; \ do { \ if (cond) \ { \ @@ -424,8 +424,8 @@ static int s_ct24arm_le(Filter *f) #undef CT24ARM_LE #define CT24ARM_BE(f, cond, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 4; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 4; \ do { \ if (cond) \ { \ @@ -464,8 +464,8 @@ static int s_ct24arm_be(Filter *f) **************************************************************************/ #define CT26ARM_LE(f, cond, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 4; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 4; \ do { \ if (cond) \ { \ diff --git a/src/filter/cto.h b/src/filter/cto.h index cf6f6081..eb217495 100644 --- a/src/filter/cto.h +++ b/src/filter/cto.h @@ -35,10 +35,10 @@ static int F(Filter *f) { #ifdef U // filter - upx_byte *b = f->buf; + byte *b = f->buf; #else // scan - const upx_byte *b = f->buf; + const byte *b = f->buf; #endif const unsigned addvalue = f->addvalue; const unsigned size = f->buf_len; @@ -141,7 +141,7 @@ static int F(Filter *f) #ifdef U static int U(Filter *f) { - upx_byte *b = f->buf; + byte *b = f->buf; const unsigned size5 = f->buf_len - 5; const unsigned addvalue = f->addvalue; const unsigned cto = (unsigned)f->cto << 24; diff --git a/src/filter/ctoj.h b/src/filter/ctoj.h index b659abf3..0b9b4455 100644 --- a/src/filter/ctoj.h +++ b/src/filter/ctoj.h @@ -39,10 +39,10 @@ static int F(Filter *f) { #ifdef U // filter - upx_byte *b = f->buf; + byte *b = f->buf; #else // scan - const upx_byte *b = f->buf; + const byte *b = f->buf; #endif const unsigned addvalue = f->addvalue; const unsigned size = f->buf_len; @@ -140,7 +140,7 @@ static int F(Filter *f) #ifdef U static int U(Filter *f) { - upx_byte *b = f->buf; + byte *b = f->buf; const unsigned size5 = f->buf_len - 5; const unsigned addvalue = f->addvalue; const unsigned cto = (unsigned)f->cto << 24; diff --git a/src/filter/ctojr.h b/src/filter/ctojr.h index 21f87369..b1bfa6bb 100644 --- a/src/filter/ctojr.h +++ b/src/filter/ctojr.h @@ -98,10 +98,10 @@ static int F(Filter *f) { #ifdef U // filter - upx_byte *const b = f->buf; + byte *const b = f->buf; #else // scan - const upx_byte *b = f->buf; + const byte *b = f->buf; #endif const unsigned size = f->buf_len; @@ -268,7 +268,7 @@ static int U(Filter *f) { unsigned ic, jc; - upx_byte *const b = f->buf; + byte *const b = f->buf; const unsigned size5 = f->buf_len - 5; const unsigned cto = (unsigned)f->cto << 24; unsigned lastcall = 0; diff --git a/src/filter/ctok.h b/src/filter/ctok.h index e8a84cfc..bb228688 100644 --- a/src/filter/ctok.h +++ b/src/filter/ctok.h @@ -39,10 +39,10 @@ static int F(Filter *f) { #ifdef U // filter - upx_byte *b = f->buf; + byte *b = f->buf; #else // scan - const upx_byte *b = f->buf; + const byte *b = f->buf; #endif const unsigned addvalue = f->addvalue; const unsigned size = f->buf_len; @@ -141,7 +141,7 @@ static int F(Filter *f) #ifdef U static int U(Filter *f) { - upx_byte *b = f->buf; + byte *b = f->buf; const unsigned size5 = f->buf_len - 5; const unsigned addvalue = f->addvalue; const unsigned cto = (unsigned)f->cto << 24; diff --git a/src/filter/ctsw.h b/src/filter/ctsw.h index 111441e9..f3775ed7 100644 --- a/src/filter/ctsw.h +++ b/src/filter/ctsw.h @@ -32,8 +32,8 @@ **************************************************************************/ #define CTSW16(f, cond1, cond2, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 3; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 3; \ do { \ if (cond1) \ { \ @@ -102,8 +102,8 @@ static int s_ctsw16_e9_e8(Filter *f) **************************************************************************/ #define CTSW32(f, cond1, cond2, addvalue, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 5; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 5; \ do { \ if (cond1) \ { \ diff --git a/src/filter/getcto.h b/src/filter/getcto.h index 8bd7cd72..34881656 100644 --- a/src/filter/getcto.h +++ b/src/filter/getcto.h @@ -31,7 +31,7 @@ // **************************************************************************/ -static int getcto(Filter *f, const unsigned char *buf, const int n=256) +static int getcto(Filter *f, const byte *buf, const int n=256) { int ic = n; @@ -63,7 +63,7 @@ static int getcto(Filter *f, const unsigned char *buf, const int n=256) //throwCantPack("call trick problem"); return -1; - f->cto = (unsigned char) ic; + f->cto = (byte) ic; return ic; } diff --git a/src/filter/ppcbxx.h b/src/filter/ppcbxx.h index bd694350..3f1f5f8f 100644 --- a/src/filter/ppcbxx.h +++ b/src/filter/ppcbxx.h @@ -41,11 +41,11 @@ static int F(Filter *f) { #ifdef U // filter - upx_byte *b = f->buf; + byte *b = f->buf; const unsigned addvalue = f->addvalue; #else // scan - const upx_byte *b = f->buf; + const byte *b = f->buf; #endif const unsigned size = umin(f->buf_len, 0u - (~0u<<(32 - (6+ W_CTO)))); const unsigned size4 = size -4; @@ -125,7 +125,7 @@ static int F(Filter *f) #ifdef U static int U(Filter *f) { - upx_byte *b = f->buf; + byte *b = f->buf; const unsigned size4 = umin(f->buf_len - 4, 0u - (~0u<<(32 - (6+ W_CTO)))); const unsigned addvalue = f->addvalue; diff --git a/src/filter/sub.hh b/src/filter/sub.hh index ffa5408f..cf1641ce 100644 --- a/src/filter/sub.hh +++ b/src/filter/sub.hh @@ -32,7 +32,7 @@ **************************************************************************/ #define SUB(f, N, T, get, set) \ - upx_byte *b = f->buf; \ + byte *b = f->buf; \ unsigned l = f->buf_len / sizeof(T); \ int i; \ T d[N]; \ @@ -54,7 +54,7 @@ #define ADD(f, N, T, get, set) \ - upx_byte *b = f->buf; \ + byte *b = f->buf; \ unsigned l = f->buf_len / sizeof(T); \ int i; \ T d[N]; \ diff --git a/src/filter/sw.h b/src/filter/sw.h index 4b901982..6c704aa1 100644 --- a/src/filter/sw.h +++ b/src/filter/sw.h @@ -32,8 +32,8 @@ **************************************************************************/ #define SW16(f, cond, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 3; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 3; \ do { \ if (cond) \ { \ @@ -110,8 +110,8 @@ static int s_sw16_e8e9(Filter *f) **************************************************************************/ #define SW32(f, cond, get, set) \ - upx_byte *b = f->buf; \ - upx_byte *b_end = b + f->buf_len - 5; \ + byte *b = f->buf; \ + byte *b_end = b + f->buf_len - 5; \ do { \ if (cond) \ { \ diff --git a/src/lefile.cpp b/src/lefile.cpp index 5bf727c2..c0c0c276 100644 --- a/src/lefile.cpp +++ b/src/lefile.cpp @@ -89,7 +89,7 @@ void LeFile::writePageMap() { void LeFile::readResidentNames() { sores_names = ih.entry_table_offset - ih.resident_names_offset; - ires_names = New(upx_byte, sores_names); + ires_names = New(byte, sores_names); fif->seek(le_offset + ih.resident_names_offset, SEEK_SET); fif->readx(ires_names, sores_names); } @@ -102,7 +102,7 @@ void LeFile::writeResidentNames() { void LeFile::readEntryTable() { soentries = ih.fixup_page_table_offset - ih.entry_table_offset; fif->seek(le_offset + ih.entry_table_offset, SEEK_SET); - ientries = New(upx_byte, soentries); + ientries = New(byte, soentries); fif->readx(ientries, soentries); } @@ -125,7 +125,7 @@ void LeFile::writeFixupPageTable() { void LeFile::readFixups() { sofixups = get_le32(ifpage_table + pages) - get_le32(ifpage_table); - ifixups = New(upx_byte, sofixups); + ifixups = New(byte, sofixups); fif->seek(le_offset + ih.fixup_record_table_offset, SEEK_SET); fif->readx(ifixups, sofixups); } @@ -171,7 +171,7 @@ void LeFile::writeImage() { void LeFile::readNonResidentNames() { if (ih.non_resident_name_table_length) { sononres_names = ih.non_resident_name_table_length; - inonres_names = New(upx_byte, sononres_names); + inonres_names = New(byte, sononres_names); fif->seek(exe_offset + ih.non_resident_name_table_offset, SEEK_SET); fif->readx(inonres_names, sononres_names); } @@ -184,7 +184,7 @@ void LeFile::writeNonResidentNames() { bool LeFile::readFileHeader() { #define H(x) get_le16(header + 2 * (x)) - upx_byte header[0x40]; + byte header[0x40]; le_offset = exe_offset = 0; int ic; @@ -262,7 +262,7 @@ void LeFile::countFixups(unsigned *counts) const { // counts[objects] - # of selector fixups // counts[objects+1] - # of self-relative fixups - const upx_byte *fix = ifixups; + const byte *fix = ifixups; const unsigned sfixups = get_le32(ifpage_table + pages); unsigned ll; diff --git a/src/lefile.h b/src/lefile.h index aa84cb6c..e42f2535 100644 --- a/src/lefile.h +++ b/src/lefile.h @@ -127,10 +127,10 @@ protected: }; struct alignas(1) le_pagemap_entry_t { - unsigned char h; - unsigned char m; - unsigned char l; - unsigned char type; // 0x00-legal;0x40-iterated;0x80-invalid;0xC0-zeroed + byte h; + byte m; + byte l; + byte type; // 0x00-legal;0x40-iterated;0x80-invalid;0xC0-zeroed }; virtual void readObjectTable(); @@ -199,18 +199,18 @@ protected: unsigned *ofpage_table = nullptr; le_pagemap_entry_t *ipm_entries = nullptr; le_pagemap_entry_t *opm_entries = nullptr; - upx_byte *ires_names = nullptr; - upx_byte *ores_names = nullptr; - upx_byte *ifixups = nullptr; - upx_byte *ofixups = nullptr; - upx_byte *inonres_names = nullptr; - upx_byte *ononres_names = nullptr; + byte *ires_names = nullptr; + byte *ores_names = nullptr; + byte *ifixups = nullptr; + byte *ofixups = nullptr; + byte *inonres_names = nullptr; + byte *ononres_names = nullptr; MemBuffer mb_iimage; - SPAN_0(upx_byte) iimage = nullptr; + SPAN_0(byte) iimage = nullptr; MemBuffer mb_oimage; - SPAN_0(upx_byte) oimage = nullptr; - upx_byte *ientries = nullptr; - upx_byte *oentries = nullptr; + SPAN_0(byte) oimage = nullptr; + byte *ientries = nullptr; + byte *oentries = nullptr; unsigned soobject_table; unsigned sofpage_table; diff --git a/src/linker.cpp b/src/linker.cpp index 4d1aec6e..28bf3d21 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -122,7 +122,7 @@ ElfLinker::~ElfLinker() { } void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) { - const upx_byte *pdata = (const upx_byte *) pdata_v; + const byte *pdata = (const byte *) pdata_v; if (plen >= 16 && memcmp(pdata, "UPX#", 4) == 0) { // decompress pre-compressed stub-loader int method; @@ -142,7 +142,7 @@ void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) { } assert((unsigned) plen < u_len); inputlen = u_len; - input = new upx_byte[inputlen + 1]; + input = New(byte, inputlen + 1); unsigned new_len = u_len; int r = upx_decompress(pdata, c_len, input, &new_len, method, nullptr); if (r == UPX_E_OUT_OF_MEMORY) @@ -151,7 +151,7 @@ void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) { throwBadLoader(); } else { inputlen = plen; - input = new upx_byte[inputlen + 1]; + input = New(byte, inputlen + 1); if (inputlen) memcpy(input, pdata, inputlen); } @@ -159,7 +159,7 @@ void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) { output_capacity = (inputlen ? (inputlen + pxtra) : 0x4000); assert(output_capacity <= (1 << 16)); // LE16 l_info.l_size - output = new upx_byte[output_capacity]; + output = New(byte, output_capacity); outputlen = 0; NO_printf("\nElfLinker::init %d @%p\n", output_capacity, output); @@ -456,7 +456,7 @@ int ElfLinker::getSectionSize(const char *sname) const { return section->size; } -upx_byte *ElfLinker::getLoader(int *llen) const { +byte *ElfLinker::getLoader(int *llen) const { if (llen) *llen = outputlen; return output; @@ -482,7 +482,7 @@ void ElfLinker::relocate() { else { value = rel->value->section->offset + rel->value->offset + rel->add; } - upx_byte *location = rel->section->output + rel->offset; + byte *location = rel->section->output + rel->offset; NO_printf("%-28s %-28s %-10s %#16llx %#16llx\n", rel->section->name, rel->value->name, rel->type, (long long) value, (long long) value - rel->section->offset - rel->offset); @@ -553,7 +553,7 @@ void ElfLinker::alignWithByte(unsigned len, unsigned char b) { outputlen += len; } -void ElfLinker::relocate1(const Relocation *rel, upx_byte *, upx_uint64_t, const char *) { +void ElfLinker::relocate1(const Relocation *rel, byte *, upx_uint64_t, const char *) { internal_error("unknown relocation type '%s\n'", rel->type); } @@ -564,7 +564,7 @@ void ElfLinker::relocate1(const Relocation *rel, upx_byte *, upx_uint64_t, const **************************************************************************/ #if 0 // FIXME -static void check8(const Relocation *rel, const upx_byte *location, int v, int d) +static void check8(const Relocation *rel, const byte *location, int v, int d) { if (v < -128 || v > 127) internal_error("value out of range (%d) in reloc %s:%x\n", @@ -575,7 +575,7 @@ static void check8(const Relocation *rel, const upx_byte *location, int v, int d } #endif -void ElfLinkerAMD64::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerAMD64::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (strncmp(type, "R_X86_64_", 9)) return super::relocate1(rel, location, value, type); @@ -608,7 +608,7 @@ void ElfLinkerAMD64::relocate1(const Relocation *rel, upx_byte *location, upx_ui super::relocate1(rel, location, value, type); } -void ElfLinkerArmBE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerArmBE::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (!strcmp(type, "R_ARM_PC24") || !strcmp(type, "R_ARM_CALL") || !strcmp(type, "R_ARM_JUMP24")) { @@ -633,7 +633,7 @@ void ElfLinkerArmBE::relocate1(const Relocation *rel, upx_byte *location, upx_ui super::relocate1(rel, location, value, type); } -void ElfLinkerArmLE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerArmLE::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (!strcmp(type, "R_ARM_PC24") || !strcmp(type, "R_ARM_CALL") || !strcmp(type, "R_ARM_JUMP24")) { @@ -658,7 +658,7 @@ void ElfLinkerArmLE::relocate1(const Relocation *rel, upx_byte *location, upx_ui super::relocate1(rel, location, value, type); } -void ElfLinkerArm64LE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerArm64LE::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (strncmp(type, "R_AARCH64_", 10)) return super::relocate1(rel, location, value, type); @@ -706,7 +706,7 @@ void ElfLinkerM68k::alignCode(unsigned len) { outputlen += len; } -void ElfLinkerM68k::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerM68k::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (strncmp(type, "R_68K_", 6)) return super::relocate1(rel, location, value, type); @@ -726,7 +726,7 @@ void ElfLinkerM68k::relocate1(const Relocation *rel, upx_byte *location, upx_uin super::relocate1(rel, location, value, type); } -void ElfLinkerMipsBE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerMipsBE::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { #define MIPS_HI(a) (((a) >> 16) + (((a) &0x8000) >> 15)) #define MIPS_LO(a) ((a) &0xffff) @@ -753,7 +753,7 @@ void ElfLinkerMipsBE::relocate1(const Relocation *rel, upx_byte *location, upx_u #undef MIPS_PC26 } -void ElfLinkerMipsLE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerMipsLE::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { #define MIPS_HI(a) (((a) >> 16) + (((a) &0x8000) >> 15)) #define MIPS_LO(a) ((a) &0xffff) @@ -780,7 +780,7 @@ void ElfLinkerMipsLE::relocate1(const Relocation *rel, upx_byte *location, upx_u #undef MIPS_PC26 } -void ElfLinkerPpc32::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerPpc32::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (strncmp(type, "R_PPC_", 6)) return super::relocate1(rel, location, value, type); @@ -813,7 +813,7 @@ void ElfLinkerPpc32::relocate1(const Relocation *rel, upx_byte *location, upx_ui super::relocate1(rel, location, value, type); } -void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerPpc64le::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (!strcmp(type, "R_PPC64_ADDR64")) { set_le64(location, get_le64(location) + value); @@ -856,7 +856,7 @@ void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, upx_ super::relocate1(rel, location, value, type); } -void ElfLinkerPpc64::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerPpc64::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (!strcmp(type, "R_PPC64_ADDR32")) { set_be32(location, get_be32(location) + value); @@ -886,7 +886,7 @@ void ElfLinkerPpc64::relocate1(const Relocation *rel, upx_byte *location, upx_ui super::relocate1(rel, location, value, type); } -void ElfLinkerX86::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, +void ElfLinkerX86::relocate1(const Relocation *rel, byte *location, upx_uint64_t value, const char *type) { if (strncmp(type, "R_386_", 6)) return super::relocate1(rel, location, value, type); diff --git a/src/linker.h b/src/linker.h index beb2fa16..70981213 100644 --- a/src/linker.h +++ b/src/linker.h @@ -42,9 +42,9 @@ protected: struct Symbol; struct Relocation; - upx_byte *input = nullptr; + byte *input = nullptr; int inputlen = 0; - upx_byte *output = nullptr; + byte *output = nullptr; int outputlen = 0; unsigned output_capacity = 0; @@ -91,7 +91,7 @@ public: Section *addSection(const char *sname, const void *sdata, int slen, unsigned p2align); int getSection(const char *sname, int *slen = nullptr) const; int getSectionSize(const char *sname) const; - upx_byte *getLoader(int *llen = nullptr) const; + byte *getLoader(int *llen = nullptr) const; void defineSymbol(const char *name, upx_uint64_t value); upx_uint64_t getSymbolOffset(const char *) const; @@ -111,7 +111,7 @@ public: protected: void relocate(); - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type); // target endianness abstraction @@ -126,7 +126,7 @@ protected: struct ElfLinker::Section : private noncopyable { char *name = nullptr; void *input = nullptr; - upx_byte *output = nullptr; + byte *output = nullptr; unsigned size = 0; upx_uint64_t offset = 0; unsigned p2align = 0; // log2 @@ -164,7 +164,7 @@ class ElfLinkerAMD64 : public ElfLinker { protected: virtual void alignCode(unsigned len) override { alignWithByte(len, 0x90); } - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -173,7 +173,7 @@ class ElfLinkerARM64 final : public ElfLinker { protected: virtual void alignCode(unsigned len) override { alignWithByte(len, 0x90); } - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -184,7 +184,7 @@ public: ElfLinkerArmBE() { bele = &N_BELE_RTP::be_policy; } protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -192,7 +192,7 @@ class ElfLinkerArmLE final : public ElfLinker { typedef ElfLinker super; protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -200,7 +200,7 @@ class ElfLinkerArm64LE final : public ElfLinker { typedef ElfLinker super; protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -212,7 +212,7 @@ public: protected: virtual void alignCode(unsigned len) override; - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -223,7 +223,7 @@ public: ElfLinkerMipsBE() { bele = &N_BELE_RTP::be_policy; } protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -231,7 +231,7 @@ class ElfLinkerMipsLE final : public ElfLinker { typedef ElfLinker super; protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -242,7 +242,7 @@ public: ElfLinkerPpc32() { bele = &N_BELE_RTP::be_policy; } protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -250,7 +250,7 @@ class ElfLinkerPpc64le final : public ElfLinker { typedef ElfLinker super; protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -261,7 +261,7 @@ public: ElfLinkerPpc64() { bele = &N_BELE_RTP::be_policy; } protected: - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; @@ -270,7 +270,7 @@ class ElfLinkerX86 final : public ElfLinker { protected: virtual void alignCode(unsigned len) override { alignWithByte(len, 0x90); } - virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + virtual void relocate1(const Relocation *, byte *location, upx_uint64_t value, const char *type) override; }; diff --git a/src/p_com.cpp b/src/p_com.cpp index d60b1e35..232d0f7e 100644 --- a/src/p_com.cpp +++ b/src/p_com.cpp @@ -59,7 +59,7 @@ const int *PackCom::getFilters() const { **************************************************************************/ bool PackCom::canPack() { - unsigned char buf[128]; + byte buf[128]; fi->readx(buf, sizeof(buf)); if (memcmp(buf, "MZ", 2) == 0 || memcmp(buf, "ZM", 2) == 0) // .exe @@ -123,7 +123,7 @@ void PackCom::buildLoader(const Filter *ft) { } } -void PackCom::patchLoader(OutputFile *fo, upx_byte *loader, int lsize, unsigned calls) { +void PackCom::patchLoader(OutputFile *fo, byte *loader, int lsize, unsigned calls) { const int e_len = getLoaderSectionStart("COMCUTPO"); const int d_len = lsize - e_len; assert(e_len > 0 && e_len < 128); diff --git a/src/p_com.h b/src/p_com.h index ae604d9a..03f265be 100644 --- a/src/p_com.h +++ b/src/p_com.h @@ -57,7 +57,7 @@ protected: // dos/sys will override these: virtual unsigned getCallTrickOffset() const { return 0x100; } virtual void buildLoader(const Filter *ft) override; - virtual void patchLoader(OutputFile *fo, upx_byte *, int, unsigned); + virtual void patchLoader(OutputFile *fo, byte *, int, unsigned); }; #endif /* already included */ diff --git a/src/p_djgpp2.cpp b/src/p_djgpp2.cpp index ebf94fce..e0ab86c5 100644 --- a/src/p_djgpp2.cpp +++ b/src/p_djgpp2.cpp @@ -66,7 +66,7 @@ const int *PackDjgpp2::getFilters() const { return filters; } -unsigned PackDjgpp2::findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, unsigned range, +unsigned PackDjgpp2::findOverlapOverhead(const byte *buf, const byte *tbuf, unsigned range, unsigned upper_limit) const { unsigned o = super::findOverlapOverhead(buf, tbuf, range, upper_limit); o = (o + 0x3ff) & ~0x1ff; @@ -106,7 +106,7 @@ void PackDjgpp2::handleStub(OutputFile *fo) { } static bool is_dlm(InputFile *fi, unsigned coff_offset) { - unsigned char buf[4]; + byte buf[4]; unsigned off; try { @@ -125,7 +125,7 @@ static bool is_dlm(InputFile *fi, unsigned coff_offset) { } static void handle_allegropak(InputFile *fi, OutputFile *fo) { - unsigned char b[8]; + byte b[8]; int pfsize = 0; try { @@ -150,8 +150,8 @@ static void handle_allegropak(InputFile *fi, OutputFile *fo) { } int PackDjgpp2::readFileHeader() { - unsigned char hdr[0x1c]; - unsigned char magic[8]; + byte hdr[0x1c]; + byte magic[8]; fi->seek(0, SEEK_SET); fi->readx(hdr, sizeof(hdr)); @@ -390,11 +390,11 @@ void PackDjgpp2::unpack(OutputFile *fo) { if (ph.filter) { Filter ft(ph.level); ft.init(ph.filter, addvalue); - ft.cto = (unsigned char) ph.filter_cto; + ft.cto = (byte) ph.filter_cto; if (ph.version < 11) { - unsigned char ctobuf[4]; + byte ctobuf[4]; fi->readx(ctobuf, 4); - ft.cto = (unsigned char) (get_le32(ctobuf) >> 24); + ft.cto = (byte) (get_le32(ctobuf) >> 24); } ft.unfilter(obuf, ph.u_len - data->size); } diff --git a/src/p_djgpp2.h b/src/p_djgpp2.h index f03f1feb..30f850c1 100644 --- a/src/p_djgpp2.h +++ b/src/p_djgpp2.h @@ -57,8 +57,7 @@ protected: void handleStub(OutputFile *fo); int readFileHeader(); - virtual unsigned findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, - unsigned range = 0, + virtual unsigned findOverlapOverhead(const byte *buf, const byte *tbuf, unsigned range = 0, unsigned upper_limit = ~0u) const override; virtual void buildLoader(const Filter *ft) override; virtual Linker *newLinker() const override; diff --git a/src/p_exe.cpp b/src/p_exe.cpp index b005e44e..36bab65e 100644 --- a/src/p_exe.cpp +++ b/src/p_exe.cpp @@ -258,12 +258,12 @@ bool PackExe::canPack() { // **************************************************************************/ -static unsigned optimize_relocs(upx_byte *b, const unsigned size, const upx_byte *relocs, - const unsigned nrelocs, upx_byte *crel, bool *has_9a) { +static unsigned optimize_relocs(byte *b, const unsigned size, const byte *relocs, + const unsigned nrelocs, byte *crel, bool *has_9a) { if (opt->exact) throwCantPackExact(); - upx_byte *const crel_save = crel; + byte *const crel_save = crel; unsigned i; unsigned seg_high = 0; #if 0 @@ -351,7 +351,7 @@ static unsigned optimize_relocs(upx_byte *b, const unsigned size, const upx_byte t -= 254; ones++; } - *crel++ = (unsigned char) t; + *crel++ = (byte) t; di = offs; } } while (i < nrelocs); @@ -390,9 +390,9 @@ void PackExe::pack(OutputFile *fo) { // relocations has_9a = false; - upx_byte *w = ibuf + ih_imagesize; + byte *w = ibuf + ih_imagesize; if (ih.relocs) { - upx_byte *wr = w + RSFCRI; + byte *wr = w + RSFCRI; fi->seek(ih.relocoffs, SEEK_SET); fi->readx(wr, 4 * ih.relocs); @@ -408,7 +408,7 @@ void PackExe::pack(OutputFile *fo) { if (relocsize > MAXRELOCS) throwCantPack("too many relocations"); #if 0 - upx_byte out[9*relocsize/8+1024]; + byte out[9*relocsize/8+1024]; unsigned in_len = relocsize; unsigned out_len = 0; ucl_nrv2b_99_compress(w, in_len, out, &out_len, nullptr, 9, nullptr, nullptr); @@ -467,7 +467,7 @@ void PackExe::pack(OutputFile *fo) { oh.max = ic < 0xffff && ih.max != 0xffff ? ic : 0xffff; // set extra info - unsigned char extra_info[9]; + byte extra_info[9]; unsigned eisize = 0; if (oh.ss != ih.ss) { set_le16(extra_info + eisize, ih.ss); @@ -489,7 +489,7 @@ void PackExe::pack(OutputFile *fo) { eisize += 2; flag |= MAXMEM; } - extra_info[eisize++] = (unsigned char) flag; + extra_info[eisize++] = (byte) flag; if (M_IS_NRV2B(ph.method) || M_IS_NRV2D(ph.method) || M_IS_NRV2E(ph.method)) linker->defineSymbol("bx_magic", 0x7FFF + 0x10 * ((packedsize & 15) + 1)); @@ -608,13 +608,13 @@ void PackExe::unpack(OutputFile *fo) { unsigned imagesize = ih_imagesize; imagesize--; - const unsigned char flag = ibuf[imagesize]; + const byte flag = ibuf[imagesize]; unsigned relocn = 0; - SPAN_S_VAR(upx_byte, relocs, obuf + ph.u_len, obuf); + SPAN_S_VAR(byte, relocs, obuf + ph.u_len, obuf); MemBuffer mb_wrkmem; - SPAN_0_VAR(upx_byte, wrkmem, nullptr); + SPAN_0_VAR(byte, wrkmem, nullptr); if (!(flag & NORELOC)) { relocs -= get_le16(obuf + (ph.u_len - 2)); ph.u_len -= 2; @@ -623,7 +623,7 @@ void PackExe::unpack(OutputFile *fo) { wrkmem = mb_wrkmem; // => now a SPAN_S unsigned es = 0, ones = get_le16(relocs); const unsigned seghi = get_le16(relocs + 2); - SPAN_S_VAR(const upx_byte, p, relocs + 4); + SPAN_S_VAR(const byte, p, relocs + 4); while (ones) { unsigned di = get_le16(p); @@ -637,7 +637,7 @@ void PackExe::unpack(OutputFile *fo) { } dorel = true; if (*p == 0) { - SPAN_S_VAR(const upx_byte, q, obuf); + SPAN_S_VAR(const byte, q, obuf); for (q = obuf + (es * 16 + di); !(*q == 0x9a && get_le16(q + 3) <= seghi); q++) { } diff --git a/src/p_ps1.cpp b/src/p_ps1.cpp index a89792f7..2bbfcecc 100644 --- a/src/p_ps1.cpp +++ b/src/p_ps1.cpp @@ -126,10 +126,10 @@ bool PackPs1::readBkupHeader() { fi->readx(&bh, sizeof(bh)); if (bh.ih_csum != upx_adler32(&bh, SZ_IH_BKUP)) { - unsigned char buf[sizeof(bh)]; + byte buf[sizeof(bh)]; fi->seek(sizeof(ps1_exe_t), SEEK_SET); fi->readx(buf, sizeof(bh)); - if (!getBkupHeader(buf, (unsigned char *) &bh)) + if (!getBkupHeader(buf, (byte *) &bh)) return false; } return true; @@ -144,11 +144,11 @@ bool PackPs1::readBkupHeader() { ACC_BLOCK_END #define ADLER16(a) (((a) >> 16) ^ ((a) &0xffff)) -void PackPs1::putBkupHeader(const unsigned char *src, unsigned char *dst, unsigned *len) { +void PackPs1::putBkupHeader(const byte *src, byte *dst, unsigned *len) { unsigned sz_cbh = MemBuffer::getSizeForCompression(SZ_IH_BKUP); if (src && dst) { - unsigned char *cpr_bh = New(unsigned char, sz_cbh); + ByteArray(cpr_bh, sz_cbh); memset(cpr_bh, 0, sizeof(bh)); ps1_exe_chb_t *p = (ps1_exe_chb_t *) cpr_bh; @@ -161,7 +161,6 @@ void PackPs1::putBkupHeader(const unsigned char *src, unsigned char *dst, unsign *len = ALIGN_UP(sz_cbh + (unsigned) sizeof(ps1_exe_chb_t) - 1, 4u); p->ih_csum = ADLER16(upx_adler32(&ih.epc, SZ_IH_BKUP)); memcpy(dst, cpr_bh, SZ_IH_BKUP); - delete[] cpr_bh; } else throwInternalError("header compression failed"); } @@ -170,15 +169,15 @@ void PackPs1::putBkupHeader(const unsigned char *src, unsigned char *dst, unsign #define ADLER16_LO(a, b) (((a) >> 16) ^ (b)) #define RE_ADLER16(a, b) (ADLER16_HI(a, b) | ADLER16_LO(a, b)) -bool PackPs1::getBkupHeader(unsigned char *p, unsigned char *dst) { +bool PackPs1::getBkupHeader(byte *p, byte *dst) { ps1_exe_chb_t *src = (ps1_exe_chb_t *) p; if (src && (src->id == '1' && src->len < SZ_IH_BKUP) && dst) { - unsigned char *unc_bh = New(unsigned char, MemBuffer::getSizeForDecompression(SZ_IH_BKUP)); + ByteArray(unc_bh, MemBuffer::getSizeForDecompression(SZ_IH_BKUP)); unsigned sz_bh = SZ_IH_BKUP; - int r = upx_decompress((const unsigned char *) &src->ih_bkup, src->len, unc_bh, &sz_bh, - M_NRV2E_8, nullptr); + int r = upx_decompress((const byte *) &src->ih_bkup, src->len, unc_bh, &sz_bh, M_NRV2E_8, + nullptr); if (r == UPX_E_OUT_OF_MEMORY) throwOutOfMemoryException(); if (r != UPX_E_OK || sz_bh != SZ_IH_BKUP) @@ -188,7 +187,6 @@ bool PackPs1::getBkupHeader(unsigned char *p, unsigned char *dst) { if (ad != RE_ADLER16(ad, ch)) throwInternalError("backup header damaged"); memcpy(dst, unc_bh, SZ_IH_BKUP); - delete[] unc_bh; } else return false; return true; @@ -223,7 +221,7 @@ bool PackPs1::checkFileHeader() { **************************************************************************/ bool PackPs1::canPack() { - unsigned char buf[PS_HDR_SIZE - sizeof(ps1_exe_t)]; + byte buf[PS_HDR_SIZE - sizeof(ps1_exe_t)]; if (!readFileHeader()) return false; @@ -295,7 +293,7 @@ void PackPs1::buildLoader(const Filter *) { } else { if (M_IS_LZMA(ph.method) && buildPart2) { sz_lcpr = MemBuffer::getSizeForCompression(sz_lunc); - unsigned char *cprLoader = New(unsigned char, sz_lcpr); + byte *cprLoader = New(byte, sz_lcpr); // FIXME: does this leak? => should put into class int r = upx_compress(getLoader(), sz_lunc, cprLoader, &sz_lcpr, nullptr, M_NRV2B_8, 10, nullptr, nullptr); if (r != UPX_E_OK || sz_lcpr >= sz_lunc) @@ -303,13 +301,12 @@ void PackPs1::buildLoader(const Filter *) { initLoader(stub_mipsel_r3000_ps1, sizeof(stub_mipsel_r3000_ps1), isCon || !M_IS_LZMA(ph.method) ? 0 : 1); linker->addSection("lzma.exec", cprLoader, sz_lcpr, 0); - delete[] cprLoader; } else initLoader(stub_mipsel_r3000_ps1, sizeof(stub_mipsel_r3000_ps1)); pad_code = ALIGN_GAP((ph.c_len + (isCon ? sz_lcpr : 0)), 4u); assert(pad_code < 4); - static const unsigned char pad_buffer[4] = {0, 0, 0, 0}; + static const byte pad_buffer[4] = {0, 0, 0, 0}; linker->addSection("pad.code", pad_buffer, pad_code, 0); if (isCon) { @@ -363,7 +360,7 @@ void PackPs1::buildLoader(const Filter *) { #define BSS_CHK_LIMIT (18) bool PackPs1::findBssSection() { - unsigned char reg; + byte reg; const LE32 *const p1 = ACC_CCAST(const LE32 *, ibuf + (ih.epc - ih.tx_ptr)); if ((ih.epc - ih.tx_ptr + (BSS_CHK_LIMIT * 4)) > fdata_size) @@ -413,7 +410,7 @@ bool PackPs1::findBssSection() { void PackPs1::pack(OutputFile *fo) { ibuf.alloc(fdata_size); obuf.allocForCompression(fdata_size); - const upx_byte *p_scan = ibuf + fdata_size; + const byte *p_scan = ibuf + fdata_size; // read file fi->seek(PS_HDR_SIZE, SEEK_SET); @@ -472,7 +469,7 @@ void PackPs1::pack(OutputFile *fo) { memcpy(&oh, &ih, sizeof(ih)); unsigned sz_cbh; - putBkupHeader((const unsigned char *) &ih.epc, (unsigned char *) &bh, &sz_cbh); + putBkupHeader((const byte *) &ih.epc, (byte *) &bh, &sz_cbh); if (ih.is_ptr < (EXE_BS | (PS_RAM_SIZE - PS_STACK_SIZE))) oh.is_ptr = (EXE_BS | (PS_RAM_SIZE - 16)); @@ -529,7 +526,7 @@ void PackPs1::pack(OutputFile *fo) { } ibuf.clear(0, fdata_size); - upx_bytep paddata = ibuf; + byte *paddata = ibuf; if (M_IS_LZMA(ph.method)) { linker->defineSymbol("lzma_init_off", lzma_init); @@ -593,7 +590,7 @@ void PackPs1::pack(OutputFile *fo) { printf("%-13s: bbs end : %08X bytes\n", getName(), (unsigned int) bss_end); printf("%-13s: eof in mem IF : %08X bytes\n", getName(), (unsigned int) ih.tx_ptr + ih.tx_len); printf("%-13s: eof in mem OF : %08X bytes\n", getName(), (unsigned int) oh.tx_ptr + oh.tx_len); - unsigned char i = 0; + byte i = 0; if (isCon) { if (foundBss) i = 1; } else { i = 2; if (M_IS_LZMA(ph.method)) { if (!foundBss) i = 3; else i = 4; } } const char *loader_method[] = { "con/stack", "con/bss", "cdb", "cdb/stack", "cdb/bss" }; diff --git a/src/p_ps1.h b/src/p_ps1.h index 23033c94..d002fcc2 100644 --- a/src/p_ps1.h +++ b/src/p_ps1.h @@ -56,8 +56,8 @@ public: virtual int canUnpack() override; protected: - void putBkupHeader(const unsigned char *src, unsigned char *dst, unsigned *len); - bool getBkupHeader(unsigned char *src, unsigned char *dst); + void putBkupHeader(const byte *src, byte *dst, unsigned *len); + bool getBkupHeader(byte *src, byte *dst); bool readBkupHeader(); virtual void buildLoader(const Filter *ft) override; bool findBssSection(); @@ -99,10 +99,10 @@ protected: }; struct alignas(1) ps1_exe_chb_t { - unsigned char id; - unsigned char len; + byte id; + byte len; LE16 ih_csum; - unsigned char ih_bkup; + byte ih_bkup; }; struct alignas(1) bss_nfo { diff --git a/src/p_sys.cpp b/src/p_sys.cpp index 3d49d84a..2ff40c68 100644 --- a/src/p_sys.cpp +++ b/src/p_sys.cpp @@ -41,7 +41,7 @@ static const CLANG_FORMAT_DUMMY_STATEMENT **************************************************************************/ bool PackSys::canPack() { - unsigned char buf[128]; + byte buf[128]; fi->readx(buf, sizeof(buf)); if (memcmp(buf, "\xff\xff\xff\xff", 4) != 0) @@ -83,7 +83,7 @@ void PackSys::buildLoader(const Filter *ft) { // clang-format on } -void PackSys::patchLoader(OutputFile *fo, upx_byte *loader, int lsize, unsigned calls) { +void PackSys::patchLoader(OutputFile *fo, byte *loader, int lsize, unsigned calls) { const int e_len = getLoaderSectionStart("SYSCUTPO"); const int d_len = lsize - e_len; assert(e_len > 0 && e_len < 128); diff --git a/src/p_sys.h b/src/p_sys.h index e68e120b..95e7a586 100644 --- a/src/p_sys.h +++ b/src/p_sys.h @@ -48,7 +48,7 @@ public: protected: // dos/com overrides virtual unsigned getCallTrickOffset() const override { return 0; } virtual void buildLoader(const Filter *ft) override; - virtual void patchLoader(OutputFile *fo, upx_byte *, int, unsigned) override; + virtual void patchLoader(OutputFile *fo, byte *, int, unsigned) override; }; #endif /* already included */ diff --git a/src/p_tmt.cpp b/src/p_tmt.cpp index 675a0461..28834643 100644 --- a/src/p_tmt.cpp +++ b/src/p_tmt.cpp @@ -56,7 +56,7 @@ const int *PackTmt::getFilters() const { return filters; } -unsigned PackTmt::findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, unsigned range, +unsigned PackTmt::findOverlapOverhead(const byte *buf, const byte *tbuf, unsigned range, unsigned upper_limit) const { // make sure the decompressor will be paragraph aligned unsigned o = super::findOverlapOverhead(buf, tbuf, range, upper_limit); @@ -89,7 +89,7 @@ void PackTmt::buildLoader(const Filter *ft) { int PackTmt::readFileHeader() { #define H(x) get_le16(h + 2 * (x)) #define H4(x) get_le32(h + (x)) - unsigned char h[0x40]; + byte h[0x40]; int ic; unsigned exe_offset = 0; adam_offset = 0; @@ -181,7 +181,7 @@ void PackTmt::pack(OutputFile *fo) { throwCantPack("file is already compressed with another packer"); MemBuffer mb_relocs(rsize); - SPAN_S_VAR(upx_byte, relocs, mb_relocs); + SPAN_S_VAR(byte, relocs, mb_relocs); fi->readx(relocs, rsize); const unsigned overlay = file_size - fi->tell(); @@ -191,10 +191,9 @@ void PackTmt::pack(OutputFile *fo) { set_le32(relocs + ic * 4, get_le32(relocs + ic * 4) - 4); MemBuffer mb_orelocs(4 * relocnum + 8192); // relocations + extra_info - SPAN_S_VAR(upx_byte, orelocs, mb_orelocs); + SPAN_S_VAR(byte, orelocs, mb_orelocs); unsigned orelocsize = optimizeReloc(relocnum, relocs, orelocs, ibuf, usize, 32, true, &big_relocs); - relocs.destroy(); // done mb_relocs.dealloc(); // done // extra_info orelocs[orelocsize++] = 0; // why is this needed - historical oversight ??? @@ -203,7 +202,6 @@ void PackTmt::pack(OutputFile *fo) { set_le32(orelocs + orelocsize, orelocsize + 4); // save orelocsize orelocsize += 4; memcpy(raw_index_bytes(ibuf, usize, orelocsize), orelocs, orelocsize); - orelocs.destroy(); // done mb_orelocs.dealloc(); // done // prepare packheader @@ -249,7 +247,7 @@ void PackTmt::pack(OutputFile *fo) { fo->write(loader, e_len); fo->write(obuf, ph.c_len); fo->write(loader + lsize - d_len, d_len); // decompressor - unsigned char rel_entry[4]; + byte rel_entry[4]; set_le32(rel_entry, 5 + s_point); fo->write(rel_entry, sizeof(rel_entry)); @@ -292,18 +290,18 @@ void PackTmt::unpack(OutputFile *fo) { if (ph.filter) { Filter ft(ph.level); ft.init(ph.filter, 0); - ft.cto = (unsigned char) ph.filter_cto; + ft.cto = (byte) ph.filter_cto; if (ph.version < 11) - ft.cto = (unsigned char) (get_le32(obuf + ph.u_len - 12) >> 24); + ft.cto = (byte) (get_le32(obuf + ph.u_len - 12) >> 24); ft.unfilter(obuf, osize); } // decode relocations - SPAN_S_VAR(const upx_byte, orelocs, raw_index_bytes(obuf, osize, orelocsize), orelocsize); - SPAN_S_VAR(upx_byte, reloc_image, raw_index_bytes(obuf, 0, osize), osize); + SPAN_S_VAR(const byte, orelocs, raw_index_bytes(obuf, osize, orelocsize), orelocsize); + SPAN_S_VAR(byte, reloc_image, raw_index_bytes(obuf, 0, osize), osize); MemBuffer mb_relocs; const unsigned relocnum = unoptimizeReloc(orelocs, mb_relocs, reloc_image, osize, 32, true); - SPAN_S_VAR(upx_byte, relocs, mb_relocs); + SPAN_S_VAR(byte, relocs, mb_relocs); for (unsigned ic = 0; ic < relocnum; ic++) set_le32(relocs + ic * 4, get_le32(relocs + ic * 4) + 4); diff --git a/src/p_tmt.h b/src/p_tmt.h index 941f3a03..e38118a7 100644 --- a/src/p_tmt.h +++ b/src/p_tmt.h @@ -56,8 +56,7 @@ public: protected: int readFileHeader(); - virtual unsigned findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, - unsigned range = 0, + virtual unsigned findOverlapOverhead(const byte *buf, const byte *tbuf, unsigned range = 0, unsigned upper_limit = ~0u) const override; virtual void buildLoader(const Filter *ft) override; virtual Linker *newLinker() const override; diff --git a/src/p_tos.cpp b/src/p_tos.cpp index 3da7eaa2..77a3d8c2 100644 --- a/src/p_tos.cpp +++ b/src/p_tos.cpp @@ -298,7 +298,7 @@ bool PackTos::checkFileHeader() { **************************************************************************/ // Check relocation for errors to make sure our loader can handle it. -static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, unsigned *nrelocs, +static int check_relocs(const byte *relocs, unsigned rsize, unsigned isize, unsigned *nrelocs, unsigned *relocsize, unsigned *overlay) { unsigned fixup = get_be32(relocs); unsigned last_fixup = fixup; @@ -345,7 +345,7 @@ bool PackTos::canPack() { if (!readFileHeader()) return false; - unsigned char buf[768]; + byte buf[768]; fi->readx(buf, sizeof(buf)); checkAlreadyPacked(buf, sizeof(buf)); diff --git a/src/p_wcle.cpp b/src/p_wcle.cpp index 1e0e160f..a81bdb7c 100644 --- a/src/p_wcle.cpp +++ b/src/p_wcle.cpp @@ -127,7 +127,7 @@ bool PackWcle::canPack() { void PackWcle::encodeEntryTable() { unsigned count, object, n; - upx_byte *p = ientries; + byte *p = ientries; n = 0; while (*p) { count = *p; @@ -208,8 +208,8 @@ void PackWcle::encodeObjectTable() { void PackWcle::encodePageMap() { opm_entries = New(le_pagemap_entry_t, sopm_entries = opages); for (unsigned ic = 0; ic < sopm_entries; ic++) { - opm_entries[ic].l = (unsigned char) (ic + 1); - opm_entries[ic].m = (unsigned char) ((ic + 1) >> 8); + opm_entries[ic].l = (byte) (ic + 1); + opm_entries[ic].m = (byte) ((ic + 1) >> 8); opm_entries[ic].h = 0; opm_entries[ic].type = 0; } @@ -224,7 +224,7 @@ void PackWcle::encodeFixupPageTable() { void PackWcle::encodeFixups() { sofixups = 1 * 7 + FIXUP_EXTRA; - ofixups = New(upx_byte, sofixups); + ofixups = New(byte, sofixups); memset(ofixups, 0, sofixups); ofixups[0] = 7; set_le16(ofixups + 2, (LE_STUB_EDI + neweip) & (mps - 1)); @@ -251,12 +251,12 @@ void PackWcle::preprocessFixups() { ByteArray(srf, counts[objects + 0] + 1); ByteArray(slf, counts[objects + 1] + 1); - SPAN_S_VAR(upx_byte, relocs, mb_relocs); - SPAN_S_VAR(upx_byte, selector_fixups, srf_membuf); - SPAN_S_VAR(upx_byte, selfrel_fixups, slf_membuf); + SPAN_S_VAR(byte, relocs, mb_relocs); + SPAN_S_VAR(byte, selector_fixups, srf_membuf); + SPAN_S_VAR(byte, selfrel_fixups, slf_membuf); unsigned relocnum = 0; - upx_byte *fix = ifixups; + byte *fix = ifixups; for (ic = jc = 0; ic < pages; ic++) { while (ptr_udiff_bytes(fix, ifixups) < get_le32(ifpage_table + (ic + 1))) { const int fixp2 = get_le16_signed(fix + 2); @@ -351,9 +351,9 @@ void PackWcle::preprocessFixups() { if (sofixups < 4 * relocnum + 8192) { delete[] ifixups; sofixups = 4 * relocnum + 8192; - ifixups = New(upx_byte, sofixups); + ifixups = New(byte, sofixups); } - SPAN_S_VAR(upx_byte, orelocs, ifixups, sofixups); + SPAN_S_VAR(byte, orelocs, ifixups, sofixups); fix = ifixups + optimizeReloc(relocnum, relocs, orelocs, iimage, soimage, 32, true, &big_relocs); has_extra_code = ptr_udiff_bytes(selector_fixups, srf) != 0; @@ -431,7 +431,7 @@ void PackWcle::pack(OutputFile *fo) { const unsigned text_vaddr = IOT(ih.init_cs_object - 1, my_base_address); // attach some useful data at the end of preprocessed fixups - ifixups[sofixups++] = (unsigned char) (ih.automatic_data_object & 0xff); + ifixups[sofixups++] = (byte) (ih.automatic_data_object & 0xff); unsigned ic = objects * sizeof(*iobject_table); memcpy(ifixups + sofixups, iobject_desc, ic); iobject_desc.dealloc(); @@ -442,7 +442,7 @@ void PackWcle::pack(OutputFile *fo) { set_le32(ifixups + sofixups + 4, ih.init_eip_offset + text_vaddr); // real entry point set_le32(ifixups + sofixups + 8, mem_size(mps, pages)); // virtual address of unpacked relocations - ifixups[sofixups + 12] = (unsigned char) (unsigned) objects; + ifixups[sofixups + 12] = (byte) (unsigned) objects; sofixups += 13; // prepare filter @@ -525,7 +525,7 @@ void PackWcle::decodeFixups() { mb_iimage.dealloc(); iimage = nullptr; - SPAN_S_VAR(const upx_byte, p, oimage + soimage); + SPAN_S_VAR(const byte, p, oimage + soimage); MemBuffer mb_relocs; unsigned const fixupn = unoptimizeReloc(p, mb_relocs, oimage, soimage, 32, true); @@ -544,10 +544,10 @@ void PackWcle::decodeFixups() { mb_relocs.dealloc(); // done // selector fixups then self-relative fixups - SPAN_S_VAR(const upx_byte, selector_fixups, p); + SPAN_S_VAR(const byte, selector_fixups, p); // Find selfrel_fixups by skipping over selector_fixups. - SPAN_S_VAR(const upx_byte, q, selector_fixups); + SPAN_S_VAR(const byte, q, selector_fixups); // The code is a subroutine that ends in RET (0xC3). while (*q != 0xC3) { // Defend against tampered selector_fixups; see PackWcle::preprocessFixups(). @@ -557,14 +557,14 @@ void PackWcle::decodeFixups() { // and where byte [+1] also can be '\xDA' or '\xDB'. if (0x8C != q[0] || 0x66 != q[2] || 0x89 != q[3]) { // Unexpected; tampering? // Try to recover by looking for the RET. - upx_byte const *q2 = (upx_byte const *) memchr(q, 0xC3, 9); + const byte *q2 = (const byte *) memchr(q, 0xC3, 9); if (q2) { // Assume recovery q = q2; break; } } // Guard against run-away. - static unsigned char const blank[9] = {0}; + static byte const blank[9] = {0}; // catastrophic worst case or no-good early warning if (ptr_diff_bytes(oimage + ph.u_len - sizeof(blank), raw_bytes(q, 0)) < 0 || !memcmp(blank, q, sizeof(blank))) { @@ -576,11 +576,11 @@ void PackWcle::decodeFixups() { q += 9; } unsigned selectlen = ptr_udiff_bytes(q, selector_fixups) / 9; - SPAN_S_VAR(const upx_byte, selfrel_fixups, q + 1); // Skip the 0xC3 + SPAN_S_VAR(const byte, selfrel_fixups, q + 1); // Skip the 0xC3 const unsigned fbytes = fixupn * 9 + 1000 + selectlen * 5; - ofixups = New(upx_byte, fbytes); - SPAN_S_VAR(upx_byte, fp, ofixups, fbytes, ofixups); + ofixups = New(byte, fbytes); + SPAN_S_VAR(byte, fp, ofixups, fbytes, ofixups); for (ic = 1, jc = 0; ic <= opages; ic++) { // self relative fixups @@ -592,9 +592,9 @@ void PackWcle::decodeFixups() { r += o; o = soobject_table; virt2rela(oobject_table, &o, &r); - fp[4] = (unsigned char) o; + fp[4] = (byte) o; set_le32(fp + 5, r); - fp[1] = (unsigned char) (r > 0xFFFF ? 0x10 : 0); + fp[1] = (byte) (r > 0xFFFF ? 0x10 : 0); fp += fp[1] ? 9 : 7; selfrel_fixups += 4; dputc('r', stdout); @@ -605,7 +605,7 @@ void PackWcle::decodeFixups() { fp[1] = 0; set_le16(fp + 2, r & (mps - 1)); unsigned x = selector_fixups[1] > 0xD0 ? oh.init_ss_object : oh.init_cs_object; - fp[4] = (unsigned char) x; + fp[4] = (byte) x; fp += 5; selector_fixups += 9; selectlen--; @@ -618,13 +618,13 @@ void PackWcle::decodeFixups() { { r = get_le32(oimage + get_le32(wrkmem + 4 * (jc - 2))); fp[0] = 7; - fp[1] = (unsigned char) (r > 0xFFFF ? 0x10 : 0); + fp[1] = (byte) (r > 0xFFFF ? 0x10 : 0); set_le16(fp + 2, get_le32(wrkmem + 4 * (jc - 2)) | ~3); set_le32(fp + 5, r); o = soobject_table; r = get_le32(wrkmem + 4 * (jc - 1)); virt2rela(oobject_table, &o, &r); - fp[4] = (unsigned char) o; + fp[4] = (byte) o; fp += fp[1] ? 9 : 7; dputc('0', stdout); } @@ -633,9 +633,9 @@ void PackWcle::decodeFixups() { virt2rela(oobject_table, &o, &r); r = get_le32(oimage + get_le32(wrkmem + 4 * jc)); fp[0] = 7; - fp[1] = (unsigned char) (r > 0xFFFF ? 0x10 : 0); + fp[1] = (byte) (r > 0xFFFF ? 0x10 : 0); set_le16(fp + 2, get_le32(wrkmem + 4 * jc) & (mps - 1)); - fp[4] = (unsigned char) o; + fp[4] = (byte) o; set_le32(fp + 5, r); fp += fp[1] ? 9 : 7; jc += 2; @@ -697,7 +697,7 @@ void PackWcle::decodeImage() { void PackWcle::decodeEntryTable() { unsigned count, object, n, r; - SPAN_S_VAR(upx_byte, p, ientries, soentries); + SPAN_S_VAR(byte, p, ientries, soentries); n = 0; while (*p) { count = *p; @@ -776,9 +776,9 @@ void PackWcle::unpack(OutputFile *fo) { Filter ft(ph.level); ft.init(ph.filter, text_vaddr); - ft.cto = (unsigned char) ph.filter_cto; + ft.cto = (byte) ph.filter_cto; if (ph.version < 11) - ft.cto = (unsigned char) (get_le32(oimage + ph.u_len - 9) >> 24); + ft.cto = (byte) (get_le32(oimage + ph.u_len - 9) >> 24); ft.unfilter(raw_bytes(oimage + text_vaddr, text_size), text_size); } diff --git a/src/p_wince_arm.cpp b/src/p_wince_arm.cpp index e12f0296..f4d97eff 100644 --- a/src/p_wince_arm.cpp +++ b/src/p_wince_arm.cpp @@ -122,7 +122,7 @@ bool PackWinCeArm::canPack() { } void PackWinCeArm::buildLoader(const Filter *ft) { - const unsigned char *loader = use_thumb_stub ? stub_arm_v4t_wince_pe : stub_arm_v4a_wince_pe; + const byte *loader = use_thumb_stub ? stub_arm_v4t_wince_pe : stub_arm_v4a_wince_pe; unsigned size = use_thumb_stub ? sizeof(stub_arm_v4t_wince_pe) : sizeof(stub_arm_v4a_wince_pe); // prepare loader diff --git a/src/packer.cpp b/src/packer.cpp index 0c31de36..f6a8f55e 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -180,7 +180,7 @@ int forced_method(int method) // extract the forced method // compress - wrap call to low-level upx_compress() **************************************************************************/ -bool Packer::compress(SPAN_P(upx_byte) i_ptr, unsigned i_len, SPAN_P(upx_byte) o_ptr, +bool Packer::compress(SPAN_P(byte) i_ptr, unsigned i_len, SPAN_P(byte) o_ptr, const upx_compress_config_t *cconf_parm) { ph.u_len = i_len; ph.c_len = 0; @@ -331,8 +331,8 @@ bool Packer::checkFinalCompressionRatio(const OutputFile *fo) const { // decompress **************************************************************************/ -void ph_decompress(PackHeader &ph, SPAN_P(const upx_byte) in, SPAN_P(upx_byte) out, - bool verify_checksum, Filter *ft) { +void ph_decompress(PackHeader &ph, SPAN_P(const byte) in, SPAN_P(byte) out, bool verify_checksum, + Filter *ft) { unsigned adler; // verify checksum of compressed data @@ -364,8 +364,7 @@ void ph_decompress(PackHeader &ph, SPAN_P(const upx_byte) in, SPAN_P(upx_byte) o } } -void Packer::decompress(SPAN_P(const upx_byte) in, SPAN_P(upx_byte) out, bool verify_checksum, - Filter *ft) { +void Packer::decompress(SPAN_P(const byte) in, SPAN_P(byte) out, bool verify_checksum, Filter *ft) { ph_decompress(ph, in, out, verify_checksum, ft); } @@ -373,8 +372,8 @@ void Packer::decompress(SPAN_P(const upx_byte) in, SPAN_P(upx_byte) out, bool ve // overlapping decompression **************************************************************************/ -static bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_bytep buf, - const upx_bytep tbuf, unsigned overlap_overhead) { +static bool ph_testOverlappingDecompression(const PackHeader &ph, const byte *buf, const byte *tbuf, + unsigned overlap_overhead) { if (ph.c_len >= ph.u_len) return false; @@ -400,7 +399,7 @@ static bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_byte return (r == UPX_E_OK && new_len == ph.u_len); } -bool Packer::testOverlappingDecompression(const upx_bytep buf, const upx_bytep tbuf, +bool Packer::testOverlappingDecompression(const byte *buf, const byte *tbuf, unsigned overlap_overhead) const { return ph_testOverlappingDecompression(ph, buf, tbuf, overlap_overhead); } @@ -433,7 +432,7 @@ void Packer::verifyOverlappingDecompression(Filter *ft) { obuf.checkState(); } -void Packer::verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft) { +void Packer::verifyOverlappingDecompression(byte *o_ptr, unsigned o_size, Filter *ft) { assert(ph.c_len < ph.u_len); assert((int) ph.overlap_overhead > 0); if (ph_skipVerify(ph)) @@ -455,7 +454,7 @@ void Packer::verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Fi // - you can enforce an upper_limit (so that we can fail early) **************************************************************************/ -unsigned Packer::findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, unsigned range, +unsigned Packer::findOverlapOverhead(const byte *buf, const byte *tbuf, unsigned range, unsigned upper_limit) const { assert((int) range >= 0); @@ -635,15 +634,15 @@ int Packer::patchPackHeader(void *b, int blen) { int boff = find_le32(b, blen, UPX_MAGIC_LE32); checkPatch(b, blen, boff, size); - auto bb = (upx_byte *) b; - ph.putPackHeader(SPAN_S_MAKE(upx_byte, bb + boff, blen, bb)); + auto bb = (byte *) b; + ph.putPackHeader(SPAN_S_MAKE(byte, bb + boff, blen, bb)); return boff; } bool Packer::getPackHeader(const void *b, int blen, bool allow_incompressible) { - auto bb = (const upx_byte *) b; - if (!ph.decodePackHeaderFromBuf(SPAN_S_MAKE(const upx_byte, bb, blen), blen)) + auto bb = (const byte *) b; + if (!ph.decodePackHeaderFromBuf(SPAN_S_MAKE(const byte, bb, blen), blen)) return false; if (ph.version > getVersion()) @@ -695,7 +694,7 @@ void Packer::checkAlreadyPacked(const void *b, int blen) { // is a real PackHeader, e.g. // // PackHeader tmp; - // if (!tmp.decodePackHeaderFromBuf((unsigned char *)b + boff, blen - boff)) + // if (!tmp.decodePackHeaderFromBuf((byte *)b + boff, blen - boff)) // return; // // This also would require that the buffer in 'b' holds @@ -738,7 +737,7 @@ int Packer::patch_be16(void *b, int blen, unsigned old, unsigned new_) { int boff = find_be16(b, blen, old); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_be16(p, new_); return boff; @@ -748,7 +747,7 @@ int Packer::patch_be16(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 2); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_be16(p, new_); return boff; @@ -758,7 +757,7 @@ int Packer::patch_be32(void *b, int blen, unsigned old, unsigned new_) { int boff = find_be32(b, blen, old); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_be32(p, new_); return boff; @@ -768,7 +767,7 @@ int Packer::patch_be32(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 4); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_be32(p, new_); return boff; @@ -778,7 +777,7 @@ int Packer::patch_le16(void *b, int blen, unsigned old, unsigned new_) { int boff = find_le16(b, blen, old); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_le16(p, new_); return boff; @@ -788,7 +787,7 @@ int Packer::patch_le16(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 2); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_le16(p, new_); return boff; @@ -798,7 +797,7 @@ int Packer::patch_le32(void *b, int blen, unsigned old, unsigned new_) { int boff = find_le32(b, blen, old); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_le32(p, new_); return boff; @@ -808,7 +807,7 @@ int Packer::patch_le32(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 4); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *) b + boff; + byte *p = (byte *) b + boff; set_le32(p, new_); return boff; @@ -910,9 +909,9 @@ void Packer::addLoaderVA(const char *s, ...) { va_end(ap); } -upx_byte *Packer::getLoader() const { +byte *Packer::getLoader() const { int size = -1; - upx_byte *oloader = linker->getLoader(&size); + byte *oloader = linker->getLoader(&size); if (oloader == nullptr || size <= 0) throwBadLoader(); return oloader; @@ -920,7 +919,7 @@ upx_byte *Packer::getLoader() const { int Packer::getLoaderSize() const { int size = -1; - upx_byte *oloader = linker->getLoader(&size); + byte *oloader = linker->getLoader(&size); if (oloader == nullptr || size <= 0) throwBadLoader(); return size; @@ -962,7 +961,7 @@ void Packer::relocateLoader() { int lsize = -1; int loff = getLoaderSectionStart("UPX1HEAD", &lsize); assert(lsize == ph.getPackHeaderSize()); - unsigned char *p = getLoader() + loff; + byte *p = getLoader() + loff; assert(get_le32(p) == UPX_MAGIC_LE32); //patchPackHeader(p, lsize); ph.putPackHeader(p); @@ -1093,12 +1092,12 @@ done: return nfilters; } -void Packer::compressWithFilters(upx_bytep i_ptr, - unsigned const i_len, // written and restored by filters - upx_bytep const o_ptr, // where to put compressed output - upx_bytep f_ptr, +void Packer::compressWithFilters(byte *i_ptr, + unsigned const i_len, // written and restored by filters + byte *const o_ptr, // where to put compressed output + byte *f_ptr, unsigned const f_len, // subset of [*i_ptr, +i_len) - upx_bytep const hdr_ptr, unsigned const hdr_len, + byte *const hdr_ptr, unsigned const hdr_len, Filter *const parm_ft, // updated unsigned const overlap_range, upx_compress_config_t const *const cconf, @@ -1150,7 +1149,7 @@ void Packer::compressWithFilters(upx_bytep i_ptr, } // Working buffer for compressed data. Don't waste memory and allocate as needed. - upx_bytep o_tmp = o_ptr; + byte *o_tmp = o_ptr; MemBuffer o_tmp_buf; // compress using all methods/filters @@ -1305,15 +1304,15 @@ void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range, void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range, upx_compress_config_t const *cconf, int filter_strategy, unsigned filter_off, unsigned ibuf_off, unsigned obuf_off, - upx_bytep const hdr_ptr, unsigned hdr_len, + byte *const hdr_ptr, unsigned hdr_len, bool inhibit_compression_check) { ibuf.checkState(); obuf.checkState(); - upx_bytep i_ptr = ibuf + ibuf_off; + byte *i_ptr = ibuf + ibuf_off; unsigned i_len = ph.u_len; - upx_bytep o_ptr = obuf + obuf_off; - upx_bytep f_ptr = ibuf + filter_off; + byte *o_ptr = obuf + obuf_off; + byte *f_ptr = ibuf + filter_off; unsigned f_len = ft->buf_len ? ft->buf_len : i_len; assert(f_ptr + f_len <= i_ptr + i_len); diff --git a/src/packer.h b/src/packer.h index 5e61fdbc..af7e4053 100644 --- a/src/packer.h +++ b/src/packer.h @@ -48,8 +48,8 @@ class PackHeader final { // these are strictly private to friend Packer PackHeader(); - void putPackHeader(SPAN_S(upx_byte) p); - bool decodePackHeaderFromBuf(SPAN_S(const upx_byte) b, int blen); + void putPackHeader(SPAN_S(byte) p); + bool decodePackHeaderFromBuf(SPAN_S(const byte) b, int blen); public: int getPackHeaderSize() const; @@ -94,9 +94,9 @@ public: }; bool ph_skipVerify(const PackHeader &ph); -void ph_decompress(PackHeader &ph, SPAN_P(const upx_byte) in, SPAN_P(upx_byte) out, - bool verify_checksum, Filter *ft); -bool ph_testOverlappingDecompression(const PackHeader &ph, SPAN_P(const upx_byte) buf, +void ph_decompress(PackHeader &ph, SPAN_P(const byte) in, SPAN_P(byte) out, bool verify_checksum, + Filter *ft); +bool ph_testOverlappingDecompression(const PackHeader &ph, SPAN_P(const byte) buf, unsigned overlap_overhead); /************************************************************************* @@ -161,9 +161,9 @@ public: protected: // main compression drivers - bool compress(SPAN_P(upx_byte) i_ptr, unsigned i_len, SPAN_P(upx_byte) o_ptr, + bool compress(SPAN_P(byte) i_ptr, unsigned i_len, SPAN_P(byte) o_ptr, const upx_compress_config_t *cconf = nullptr); - void decompress(SPAN_P(const upx_byte) in, SPAN_P(upx_byte) out, bool verify_checksum = true, + void decompress(SPAN_P(const byte) in, SPAN_P(byte) out, bool verify_checksum = true, Filter *ft = nullptr); virtual bool checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const; virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const; @@ -176,27 +176,27 @@ protected: void compressWithFilters(Filter *ft, const unsigned overlap_range, const upx_compress_config_t *cconf, int filter_strategy, unsigned filter_buf_off, unsigned compress_ibuf_off, - unsigned compress_obuf_off, upx_bytep const hdr_ptr, unsigned hdr_len, + unsigned compress_obuf_off, byte *const hdr_ptr, unsigned hdr_len, bool inhibit_compression_check = false); // real compression driver - void compressWithFilters(upx_bytep i_ptr, unsigned i_len, // written and restored by filters - upx_bytep o_ptr, upx_bytep f_ptr, + void compressWithFilters(byte *i_ptr, unsigned i_len, // written and restored by filters + byte *o_ptr, byte *f_ptr, unsigned f_len, // subset of [*i_ptr, +i_len) - upx_bytep const hdr_ptr, unsigned hdr_len, + byte *const hdr_ptr, unsigned hdr_len, Filter *parm_ft, // updated unsigned overlap_range, upx_compress_config_t const *cconf, int filter_strategy, bool inhibit_compression_check = false); // util for verifying overlapping decompresion // non-destructive test - virtual bool testOverlappingDecompression(const upx_bytep buf, const upx_bytep tbuf, + virtual bool testOverlappingDecompression(const byte *buf, const byte *tbuf, unsigned overlap_overhead) const; // non-destructive find - virtual unsigned findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, - unsigned range = 0, unsigned upper_limit = ~0u) const; + virtual unsigned findOverlapOverhead(const byte *buf, const byte *tbuf, unsigned range = 0, + unsigned upper_limit = ~0u) const; // destructive decompress + verify void verifyOverlappingDecompression(Filter *ft = nullptr); - void verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft = nullptr); + void verifyOverlappingDecompression(byte *o_ptr, unsigned o_size, Filter *ft = nullptr); // packheader handling virtual int patchPackHeader(void *b, int blen); @@ -209,7 +209,7 @@ protected: virtual Linker *newLinker() const = 0; virtual void relocateLoader(); // loader util for linker - virtual upx_byte *getLoader() const; + virtual byte *getLoader() const; virtual int getLoaderSize() const; virtual void initLoader(const void *pdata, int plen, int small = -1, int pextra = 0); #define C const char * @@ -247,7 +247,7 @@ protected: // filter handling [see packer_f.cpp] virtual bool isValidFilter(int filter_id) const; - virtual void optimizeFilter(Filter *, const upx_byte *, unsigned) const {} + virtual void optimizeFilter(Filter *, const byte *, unsigned) const {} virtual void addFilter32(int filter_id); virtual void defineFilterSymbols(const Filter *ft); @@ -271,12 +271,11 @@ protected: void checkPatch(void *b, int blen, int boff, int size); // relocation util - static unsigned optimizeReloc(unsigned relocnum, SPAN_P(upx_byte) relocs, SPAN_S(upx_byte) out, - SPAN_P(upx_byte) image, unsigned image_size, int bits, bool bswap, + static unsigned optimizeReloc(unsigned relocnum, SPAN_P(byte) relocs, SPAN_S(byte) out, + SPAN_P(byte) image, unsigned image_size, int bits, bool bswap, int *big); - static unsigned unoptimizeReloc(SPAN_S(const upx_byte) & in, MemBuffer &out, - SPAN_P(upx_byte) image, unsigned image_size, int bits, - bool bswap); + static unsigned unoptimizeReloc(SPAN_S(const byte) & in, MemBuffer &out, SPAN_P(byte) image, + unsigned image_size, int bits, bool bswap); // Target Endianness abstraction unsigned get_te16(const void *p) const { return bele->get16(p); } diff --git a/src/packer_r.cpp b/src/packer_r.cpp index c3ef383a..fc608e1a 100644 --- a/src/packer_r.cpp +++ b/src/packer_r.cpp @@ -33,12 +33,12 @@ // returns number of bytes written to |out| **************************************************************************/ -unsigned Packer::optimizeReloc(unsigned relocnum, SPAN_P(upx_byte) relocs, SPAN_S(upx_byte) out, - SPAN_P(upx_byte) image, unsigned image_size, int bits, bool bswap, +unsigned Packer::optimizeReloc(unsigned relocnum, SPAN_P(byte) relocs, SPAN_S(byte) out, + SPAN_P(byte) image, unsigned image_size, int bits, bool bswap, int *big) { assert(bits == 32 || bits == 64); mem_size_assert(1, image_size); - SPAN_P_VAR(upx_byte, fix, out); + SPAN_P_VAR(byte, fix, out); *big = 0; if (opt->exact) @@ -55,11 +55,11 @@ unsigned Packer::optimizeReloc(unsigned relocnum, SPAN_P(upx_byte) relocs, SPAN_ else if ((int) delta < 4) throwCantPack("overlapping fixups"); else if (delta < 0xf0) - *fix++ = (unsigned char) delta; + *fix++ = (byte) delta; else if (delta < 0x100000) { - *fix++ = (unsigned char) (0xf0 + (delta >> 16)); - *fix++ = (unsigned char) delta; - *fix++ = (unsigned char) (delta >> 8); + *fix++ = (byte) (0xf0 + (delta >> 16)); + *fix++ = (byte) delta; + *fix++ = (byte) (delta >> 8); } else { *big = 1; *fix++ = 0xf0; @@ -88,12 +88,11 @@ unsigned Packer::optimizeReloc(unsigned relocnum, SPAN_P(upx_byte) relocs, SPAN_ // allocates |out| and returns number of relocs written to |out| **************************************************************************/ -unsigned Packer::unoptimizeReloc(SPAN_S(const upx_byte) & in, MemBuffer &out, - SPAN_P(upx_byte) image, unsigned image_size, int bits, - bool bswap) { +unsigned Packer::unoptimizeReloc(SPAN_S(const byte) & in, MemBuffer &out, SPAN_P(byte) image, + unsigned image_size, int bits, bool bswap) { assert(bits == 32 || bits == 64); mem_size_assert(1, image_size); - SPAN_S_VAR(const upx_byte, fix, in); + SPAN_S_VAR(const byte, fix, in); // count unsigned relocnum = 0; diff --git a/src/packhead.cpp b/src/packhead.cpp index 75ed32b5..5f631387 100644 --- a/src/packhead.cpp +++ b/src/packhead.cpp @@ -41,7 +41,7 @@ PackHeader::PackHeader() : version(-1), format(-1) {} // simple checksum for the header itself (since version 10) **************************************************************************/ -static unsigned char get_packheader_checksum(SPAN_S(const upx_byte) buf, int len) { +static byte get_packheader_checksum(SPAN_S(const byte) buf, int len) { assert(len >= 4); assert(get_le32(buf) == UPX_MAGIC_LE32); // printf("1 %d\n", len); @@ -52,7 +52,7 @@ static unsigned char get_packheader_checksum(SPAN_S(const upx_byte) buf, int len c += *buf++; c %= 251; // printf("2 %d\n", c); - return (unsigned char) c; + return (byte) c; } /************************************************************************* @@ -92,7 +92,7 @@ int PackHeader::getPackHeaderSize() const { // see stub/header.ash **************************************************************************/ -void PackHeader::putPackHeader(SPAN_S(upx_byte) p) { +void PackHeader::putPackHeader(SPAN_S(byte) p) { // NOTE: It is the caller's responsbility to ensure the buffer p has // sufficient space for the header. assert(get_le32(p) == UPX_MAGIC_LE32); @@ -111,14 +111,14 @@ void PackHeader::putPackHeader(SPAN_S(upx_byte) p) { old_chksum = get_packheader_checksum(p, size - 1); set_le16(p + 16, u_len); set_le16(p + 18, c_len); - p[20] = (unsigned char) filter; + p[20] = (byte) filter; } else if (format == UPX_F_DOS_EXE) { size = 27; old_chksum = get_packheader_checksum(p, size - 1); set_le24(p + 16, u_len); set_le24(p + 19, c_len); set_le24(p + 22, u_file_size); - p[25] = (unsigned char) filter; + p[25] = (byte) filter; } else if (format == UPX_F_DOS_EXEH) { throwInternalError("invalid format"); } else { @@ -127,10 +127,10 @@ void PackHeader::putPackHeader(SPAN_S(upx_byte) p) { set_le32(p + 16, u_len); set_le32(p + 20, c_len); set_le32(p + 24, u_file_size); - p[28] = (unsigned char) filter; - p[29] = (unsigned char) filter_cto; + p[28] = (byte) filter; + p[29] = (byte) filter_cto; assert(n_mru == 0 || (n_mru >= 2 && n_mru <= 256)); - p[30] = (unsigned char) (n_mru ? n_mru - 1 : 0); + p[30] = (byte) (n_mru ? n_mru - 1 : 0); } set_le32(p + 8, u_adler); set_le32(p + 12, c_adler); @@ -142,16 +142,16 @@ void PackHeader::putPackHeader(SPAN_S(upx_byte) p) { set_be32(p + 16, u_adler); set_be32(p + 20, c_adler); set_be32(p + 24, u_file_size); - p[28] = (unsigned char) filter; - p[29] = (unsigned char) filter_cto; + p[28] = (byte) filter; + p[29] = (byte) filter_cto; assert(n_mru == 0 || (n_mru >= 2 && n_mru <= 256)); - p[30] = (unsigned char) (n_mru ? n_mru - 1 : 0); + p[30] = (byte) (n_mru ? n_mru - 1 : 0); } - p[4] = (unsigned char) version; - p[5] = (unsigned char) format; - p[6] = (unsigned char) method; - p[7] = (unsigned char) level; + p[4] = (byte) version; + p[5] = (byte) format; + p[6] = (byte) method; + p[7] = (byte) level; // header_checksum assert(size == getPackHeaderSize()); @@ -170,7 +170,7 @@ void PackHeader::putPackHeader(SPAN_S(upx_byte) p) { // **************************************************************************/ -bool PackHeader::decodePackHeaderFromBuf(SPAN_S(const upx_byte) buf, int blen) { +bool PackHeader::decodePackHeaderFromBuf(SPAN_S(const byte) buf, int blen) { int boff = find_le32(raw_bytes(buf, blen), blen, UPX_MAGIC_LE32); if (boff < 0) return false; @@ -178,7 +178,7 @@ bool PackHeader::decodePackHeaderFromBuf(SPAN_S(const upx_byte) buf, int blen) { if (blen < 20) throwCantUnpack("header corrupted 1"); - SPAN_S_VAR(const upx_byte, const p, buf + boff); + SPAN_S_VAR(const byte, const p, buf + boff); version = p[4]; format = p[5]; @@ -192,9 +192,7 @@ bool PackHeader::decodePackHeaderFromBuf(SPAN_S(const upx_byte) buf, int blen) { } if (!((format >= 1 && format <= UPX_F_W64PE_ARM64EC) || (format >= 129 && format <= UPX_F_DYLIB_PPC64))) { - char msg[24]; - snprintf(msg, sizeof(msg), "unknown format %d", format); - throwCantUnpack(msg); + throwCantUnpack("unknown format %d", format); } // diff --git a/src/packmast.h b/src/packmast.h index 12b20eae..a2012359 100644 --- a/src/packmast.h +++ b/src/packmast.h @@ -26,8 +26,6 @@ */ #pragma once -#ifndef UPX_PACKMASTER_H__ -#define UPX_PACKMASTER_H__ 1 class Packer; class InputFile; @@ -63,6 +61,4 @@ private: options_t *saved_opt = nullptr; }; -#endif /* already included */ - /* vim:set ts=4 sw=4 et: */ diff --git a/src/pefile.cpp b/src/pefile.cpp index da1f1f2c..31f1df00 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -256,7 +256,7 @@ void PeFile::Reloc::newRelocPos(void *p) { rel1 = (LE16 *) ((char *) p + sizeof(reloc)); } -PeFile::Reloc::Reloc(upx_byte *s, unsigned si) : start(s), size(si), rel(nullptr), rel1(nullptr) { +PeFile::Reloc::Reloc(byte *s, unsigned si) : start(s), size(si), rel(nullptr), rel1(nullptr) { COMPILE_TIME_ASSERT(sizeof(reloc) == 8) COMPILE_TIME_ASSERT_ALIGNED1(reloc) memset(counts, 0, sizeof(counts)); @@ -266,7 +266,7 @@ PeFile::Reloc::Reloc(upx_byte *s, unsigned si) : start(s), size(si), rel(nullptr } PeFile::Reloc::Reloc(unsigned relocnum) : start(nullptr), size(0), rel(nullptr), rel1(nullptr) { - start = new upx_byte[mem_size(4, relocnum, 8192)]; // => oxrelocs + start = new byte[mem_size(4, relocnum, 8192)]; // => oxrelocs counts[0] = 0; } @@ -290,7 +290,7 @@ void PeFile::Reloc::add(unsigned pos, unsigned type) { set_le32(start + 1024 + 4 * counts[0]++, (pos << 4) + type); } -void PeFile::Reloc::finish(upx_byte *&p, unsigned &siz) { +void PeFile::Reloc::finish(byte *&p, unsigned &siz) { unsigned prev = 0xffffffff; set_le32(start + 1024 + 4 * counts[0]++, 0xf0000000); qsort(start + 1024, counts[0], 4, le32_compare); @@ -390,8 +390,8 @@ void PeFile32::processRelocs() // pass1 ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); mb_orelocs.alloc(mem_size(4, relocnum, 8192)); // 8192 - safety orelocs = mb_orelocs; // => orelocs now is a SPAN_S - sorelocs = optimizeReloc(xcounts[3], (upx_byte *) fix[3], orelocs, ibuf + rvamin, - ibufgood - rvamin, 32, true, &big_relocs); + sorelocs = optimizeReloc(xcounts[3], (byte *) fix[3], orelocs, ibuf + rvamin, ibufgood - rvamin, + 32, true, &big_relocs); delete[] fix[3]; // Malware that hides behind UPX often has PE header info that is @@ -489,7 +489,7 @@ void PeFile64::processRelocs() // pass1 ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); mb_orelocs.alloc(mem_size(4, relocnum, 8192)); // 8192 - safety orelocs = mb_orelocs; // => orelocs now is a SPAN_S - sorelocs = optimizeReloc(xcounts[10], (upx_byte *) fix[10], orelocs, ibuf + rvamin, + sorelocs = optimizeReloc(xcounts[10], (byte *) fix[10], orelocs, ibuf + rvamin, ibufgood - rvamin, 64, true, &big_relocs); for (ic = 15; ic; ic--) @@ -682,7 +682,7 @@ public: template void add(const C *dll, unsigned ordinal) { - ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "unsigned char" + ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "byte" assert(ordinal < 0x10000); char ord[1 + 5 + 1]; upx_safe_snprintf(ord, sizeof(ord), "%c%05u", ordinal_id, ordinal); @@ -691,8 +691,8 @@ public: template void add(const C1 *dll, const C2 *proc) { - ACC_COMPILE_TIME_ASSERT(sizeof(C1) == 1) // "char" or "unsigned char" - ACC_COMPILE_TIME_ASSERT(sizeof(C2) == 1) // "char" or "unsigned char" + ACC_COMPILE_TIME_ASSERT(sizeof(C1) == 1) // "char" or "byte" + ACC_COMPILE_TIME_ASSERT(sizeof(C2) == 1) // "char" or "byte" assert(proc); add((const char *) dll, (const char *) proc, 0); } @@ -702,7 +702,7 @@ public: int osize = 4 + 2 * nsections; // upper limit for alignments for (unsigned ic = 0; ic < nsections; ic++) osize += sections[ic]->size; - output = New(upx_byte, output_capacity = osize); + output = New(byte, output_capacity = osize); outputlen = 0; // sort the sections by name before adding them all @@ -727,8 +727,8 @@ public: template upx_uint64_t getAddress(const C1 *dll, const C2 *proc) const { - ACC_COMPILE_TIME_ASSERT(sizeof(C1) == 1) // "char" or "unsigned char" - ACC_COMPILE_TIME_ASSERT(sizeof(C2) == 1) // "char" or "unsigned char" + ACC_COMPILE_TIME_ASSERT(sizeof(C1) == 1) // "char" or "byte" + ACC_COMPILE_TIME_ASSERT(sizeof(C2) == 1) // "char" or "byte" const Section *s = getThunk((const char *) dll, (const char *) proc, thunk_separator_first); if (s == nullptr && (s = getThunk((const char *) dll, (const char *) proc, thunk_separator)) == nullptr) @@ -738,7 +738,7 @@ public: template upx_uint64_t getAddress(const C *dll, unsigned ordinal) const { - ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "unsigned char" + ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "byte" assert(ordinal > 0 && ordinal < 0x10000); char ord[1 + 5 + 1]; upx_safe_snprintf(ord, sizeof(ord), "%c%05u", ordinal_id, ordinal); @@ -751,14 +751,14 @@ public: template upx_uint64_t getAddress(const C *dll) const { - ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "unsigned char" + ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "byte" tstr sdll(name_for_dll((const char *) dll, dll_name_id)); return findSection(sdll, true)->offset; } template upx_uint64_t hasDll(const C *dll) const { - ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "unsigned char" + ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "byte" tstr sdll(name_for_dll((const char *) dll, dll_name_id)); return findSection(sdll, false) != nullptr; } @@ -814,8 +814,8 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1 } struct udll { - const upx_byte *name; - const upx_byte *shname; + const byte *name; + const byte *shname; unsigned ordinal; unsigned iat; LEXX *lookupt; @@ -878,7 +878,7 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1 dlls[ic].ordinal = *tarr & 0xffff; } else // it's an import by name { - IPTR_VAR(const upx_byte, const name, ibuf + *tarr + 2); + IPTR_VAR(const byte, const name, ibuf + *tarr + 2); unsigned len = strlen(name); soimport += len + 1; if (dlls[ic].shname == nullptr || len < strlen(dlls[ic].shname)) @@ -925,7 +925,7 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1 Interval names(ibuf), iats(ibuf), lookups(ibuf); // create the preprocessed data - SPAN_S_VAR(upx_byte, ppi, oimport); // preprocessed imports + SPAN_S_VAR(byte, ppi, oimport); // preprocessed imports for (ic = 0; ic < dllnum; ic++) { LEXX *tarr = idlls[ic]->lookupt; set_le32(ppi, ilinker->getAddress(idlls[ic]->name)); @@ -1309,7 +1309,7 @@ void PeFile::processTls2(Reloc *rel, const Interval *iv, unsigned newaddr, SPAN_S_VAR(tls, const tlsp, mb_otls); // now the relocation entries in the tls data area for (ic = 0; ic < iv->ivnum; ic += 4) { - SPAN_S_VAR(upx_byte, pp, + SPAN_S_VAR(byte, pp, otls + (iv->ivarr[ic].start - (tlsp->datastart - imagebase) + sizeof(tls))); LEXX *const p = (LEXX *) raw_bytes(pp, sizeof(LEXX)); cb_value_t kc = *p; @@ -1331,7 +1331,7 @@ void PeFile::processTls2(Reloc *rel, const Interval *iv, unsigned newaddr, if (use_tls_callbacks) { // set handler offset - SPAN_S_VAR(upx_byte, pp, otls); + SPAN_S_VAR(byte, pp, otls); pp = otls + (sotls - 2 * cb_size); *(LEXX *) raw_bytes(pp, sizeof(LEXX)) = tls_handler_offset + imagebase; pp = otls + (sotls - 1 * cb_size); @@ -1351,7 +1351,7 @@ void PeFile::processLoadConf(Interval *iv) // pass 1 return; const unsigned lcaddr = IDADDR(PEDIR_LOADCONF); - const upx_byte *const loadconf = ibuf.subref("bad loadconf %#x", lcaddr, 4); + const byte *const loadconf = ibuf.subref("bad loadconf %#x", lcaddr, 4); soloadconf = get_le32(loadconf); if (soloadconf == 0) return; @@ -1372,7 +1372,7 @@ void PeFile::processLoadConf(Interval *iv) // pass 1 } mb_oloadconf.alloc(soloadconf); - oloadconf = (upx_byte *) mb_oloadconf.getVoidPtr(); + oloadconf = (byte *) mb_oloadconf.getVoidPtr(); memcpy(oloadconf, loadconf, soloadconf); } @@ -1416,7 +1416,7 @@ struct alignas(1) PeFile::Resource::res_data { struct PeFile::Resource::upx_rnode { unsigned id; - upx_byte *name; + byte *name; upx_rnode *parent; }; @@ -1432,13 +1432,12 @@ struct PeFile::Resource::upx_rleaf : public PeFile::Resource::upx_rnode { res_data data; }; -PeFile::Resource::Resource(const upx_byte *ibufstart_, const upx_byte *ibufend_) : root(nullptr) { +PeFile::Resource::Resource(const byte *ibufstart_, const byte *ibufend_) : root(nullptr) { ibufstart = ibufstart_; ibufend = ibufend_; } -PeFile::Resource::Resource(const upx_byte *p, const upx_byte *ibufstart_, - const upx_byte *ibufend_) { +PeFile::Resource::Resource(const byte *p, const byte *ibufstart_, const byte *ibufend_) { ibufstart = ibufstart_; ibufend = ibufend_; newstart = nullptr; @@ -1462,7 +1461,7 @@ bool PeFile::Resource::next() { unsigned PeFile::Resource::itype() const { return current->parent->parent->id; } -const upx_byte *PeFile::Resource::ntype() const { return current->parent->parent->name; } +const byte *PeFile::Resource::ntype() const { return current->parent->parent->name; } unsigned PeFile::Resource::size() const { return ALIGN_UP(current->data.size, 4u); } @@ -1474,14 +1473,14 @@ void PeFile::Resource::dump() const { dump(root, 0); } unsigned PeFile::Resource::iname() const { return current->parent->id; } -const upx_byte *PeFile::Resource::nname() const { return current->parent->name; } +const byte *PeFile::Resource::nname() const { return current->parent->name; } /* unsigned ilang() const {return current->id;} - const upx_byte *nlang() const {return current->name;} + const byte *nlang() const {return current->name;} */ -void PeFile::Resource::init(const upx_byte *res) { +void PeFile::Resource::init(const byte *res) { COMPILE_TIME_ASSERT(sizeof(res_dir_entry) == 8) COMPILE_TIME_ASSERT(sizeof(res_dir) == 16 + 8) COMPILE_TIME_ASSERT(sizeof(res_data) == 16) @@ -1553,11 +1552,11 @@ PeFile::Resource::upx_rnode *PeFile::Resource::convert(const void *rnode, upx_rn branch->children[ic] = child; child->id = rde->tnl; if (child->id & 0x80000000) { - const upx_byte *p = start + (child->id & 0x7fffffff); + const byte *p = start + (child->id & 0x7fffffff); ibufcheck(p, 2); const unsigned len = 2 + 2 * get_le16(p); ibufcheck(p, len); - child->name = New(upx_byte, len); + child->name = New(byte, len); memcpy(child->name, p, len); // copy unicode string ssize += len; // size of unicode strings } @@ -1593,7 +1592,7 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos, unsigned &sp be->tnl = branch->children[ic]->id; be->child = bpos + ((level < 2) ? 0x80000000 : 0); - const upx_byte *p; + const byte *p; if ((p = branch->children[ic]->name) != nullptr) { be->tnl = spos + 0x80000000; if (spos + get_le16(p) * 2 + 2 > dirsize()) @@ -1606,12 +1605,12 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos, unsigned &sp } } -upx_byte *PeFile::Resource::build() { +byte *PeFile::Resource::build() { mb_start.dealloc(); newstart = nullptr; if (dirsize()) { mb_start.alloc(dirsize()); - newstart = static_cast(mb_start.getVoidPtr()); + newstart = static_cast(mb_start.getVoidPtr()); unsigned bpos = 0, spos = dsize; build(root, bpos, spos, 0); @@ -1643,7 +1642,7 @@ void PeFile::Resource::destroy(upx_rnode *node, unsigned level) { } } -static void lame_print_unicode(const upx_byte *p) { +static void lame_print_unicode(const byte *p) { for (unsigned ic = 0; ic < get_le16(p); ic++) printf("%c", (char) p[ic * 2 + 2]); } @@ -1665,7 +1664,7 @@ void PeFile::Resource::dump(const upx_rnode *node, unsigned level) const { dump(branch->children[ic], level + 1); } -void PeFile::Resource::clear(upx_byte *node, unsigned level, Interval *iv) { +void PeFile::Resource::clear(byte *node, unsigned level, Interval *iv) { if (level == 3) iv->add(node, sizeof(res_data)); else { @@ -1679,7 +1678,7 @@ void PeFile::Resource::clear(upx_byte *node, unsigned level, Interval *iv) { } bool PeFile::Resource::clear() { - newstart = const_cast(start); + newstart = const_cast(start); Interval iv(newstart); clear(newstart, 0, &iv); iv.flatten(); @@ -1699,25 +1698,25 @@ void PeFile::processResources(Resource *res, unsigned newaddr) { if (res->newoffs()) res->newoffs() += newaddr; if (res->dirsize()) { - upx_byte *p = res->build(); + byte *p = res->build(); memcpy(oresources, p, res->dirsize()); } } -static bool match(unsigned itype, const unsigned char *ntype, unsigned iname, - const unsigned char *nname, const char *keep) { +static bool match(unsigned itype, const byte *ntype, unsigned iname, const byte *nname, + const char *keep) { // format of string keep: type1[/name1],type2[/name2], .... // typex and namex can be string or number // hopefully resource names do not have '/' or ',' characters inside struct helper { - static bool match(unsigned num, const unsigned char *unistr, const char *mkeep) { + static bool match(unsigned num, const byte *unistr, const char *mkeep) { if (!unistr) return (unsigned) atoi(mkeep) == num; unsigned ic; for (ic = 0; ic < get_le16(unistr); ic++) - if (unistr[2 + ic * 2] != (unsigned char) mkeep[ic]) + if (unistr[2 + ic * 2] != (byte) mkeep[ic]) return false; return mkeep[ic] == 0 || mkeep[ic] == ',' || mkeep[ic] == '/'; } @@ -1770,7 +1769,7 @@ void PeFile::processResources(Resource *res) { mb_oresources.alloc(soresources); mb_oresources.clear(); oresources = mb_oresources; // => SPAN_S - SPAN_S_VAR(upx_byte, ores, oresources + res->dirsize()); + SPAN_S_VAR(byte, ores, oresources + res->dirsize()); char *keep_icons = nullptr; // icon ids in the first icon group unsigned iconsin1stdir = 0; @@ -2130,7 +2129,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask, // EFI build tools already clear DOS stub // and small file alignment benefits from extra space - unsigned char stub[0x40]; + byte stub[0x40]; memset(stub, 0, sizeof(stub)); set_le16(stub, 'M' + 'Z' * 256); set_le32(stub + sizeof(stub) - sizeof(LE32), sizeof(stub)); @@ -2162,7 +2161,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask, Resource res(ibuf, ibuf + ibuf.getSize()); Interval tlsiv(ibuf); Interval loadconfiv(ibuf); - Export xport((char *) (unsigned char *) ibuf); + Export xport((char *) (byte *) ibuf); const unsigned dllstrings = processImports(); processTls(&tlsiv); // call before processRelocs!! @@ -2205,7 +2204,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask, // some extra_info data for uncompression support unsigned s = 0; - upx_byte *const p1 = ibuf.subref("bad ph.u_len %#x", ph.u_len, sizeof(ih)); + byte *const p1 = ibuf.subref("bad ph.u_len %#x", ph.u_len, sizeof(ih)); memcpy(p1 + s, &ih, sizeof(ih)); s += sizeof(ih); memcpy(p1 + s, isection, ih.objects * sizeof(*isection)); @@ -2217,7 +2216,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask, } if (sorelocs) { set_le32(p1 + s, crelocs); - p1[s + 4] = (unsigned char) (big_relocs & 6); + p1[s + 4] = (byte) (big_relocs & 6); s += 5; } if (soresources) { @@ -2536,7 +2535,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask, // unpack **************************************************************************/ -void PeFile::rebuildRelocs(SPAN_S(upx_byte) & extra_info, unsigned bits, unsigned flags, +void PeFile::rebuildRelocs(SPAN_S(byte) & extra_info, unsigned bits, unsigned flags, upx_uint64_t imagebase) { assert(bits == 32 || bits == 64); if (!ODADDR(PEDIR_RELOC) || !ODSIZE(PEDIR_RELOC) || (flags & RELOCS_STRIPPED)) @@ -2549,10 +2548,10 @@ void PeFile::rebuildRelocs(SPAN_S(upx_byte) & extra_info, unsigned bits, unsigne } const unsigned orig_crelocs = mem_size(1, get_le32(extra_info)); - const upx_byte big = extra_info[4]; + const byte big = extra_info[4]; extra_info += 5; - SPAN_S_VAR(const upx_byte, rdata, obuf + orig_crelocs, obuf); + SPAN_S_VAR(const byte, rdata, obuf + orig_crelocs, obuf); MemBuffer mb_wrkmem; unsigned relocnum = unoptimizeReloc(rdata, mb_wrkmem, obuf, orig_crelocs, bits, true); unsigned r16 = 0; @@ -2573,12 +2572,12 @@ void PeFile::rebuildRelocs(SPAN_S(upx_byte) & extra_info, unsigned bits, unsigne if ((big & 6) == 6) while (*++q) rel.add(*q + rvamin, 1); - // rdata = (upx_byte *) raw_bytes(q, 0); // ??? + // rdata = (byte *) raw_bytes(q, 0); // ??? } - SPAN_S_VAR(upx_byte, const wrkmem, mb_wrkmem); + SPAN_S_VAR(byte, const wrkmem, mb_wrkmem); for (unsigned ic = 0; ic < relocnum; ic++) { - OPTR_VAR(upx_byte, const p, obuf + get_le32(wrkmem + 4 * ic)); + OPTR_VAR(byte, const p, obuf + get_le32(wrkmem + 4 * ic)); if (bits == 32) set_le32(p, get_le32(p) + imagebase + rvamin); else @@ -2600,7 +2599,7 @@ void PeFile::rebuildExports() { return; // nothing to do opt->win32_pe.compress_exports = 0; - Export xport((char *) (unsigned char *) ibuf - isection[2].vaddr); + Export xport((char *) (byte *) ibuf - isection[2].vaddr); processExports(&xport); processExports(&xport, ODADDR(PEDIR_EXPORT)); omemcpy(obuf + (ODADDR(PEDIR_EXPORT) - rvamin), oexport, soexport); @@ -2610,7 +2609,7 @@ void PeFile::rebuildTls() { // this is an easy one : just do nothing ;-) } -void PeFile::rebuildResources(SPAN_S(upx_byte) & extra_info, unsigned lastvaddr) { +void PeFile::rebuildResources(SPAN_S(byte) & extra_info, unsigned lastvaddr) { if (ODSIZE(PEDIR_RESOURCE) == 0 || IDSIZE(PEDIR_RESOURCE) == 0) return; @@ -2623,7 +2622,7 @@ void PeFile::rebuildResources(SPAN_S(upx_byte) & extra_info, unsigned lastvaddr) throwCantUnpack("corrupted PE header"); // TODO: introduce WildPtr for "virtual pointer" pointing before a buffer - const upx_byte *r = ibuf.raw_bytes(0) - lastvaddr; + const byte *r = ibuf.raw_bytes(0) - lastvaddr; Resource res(r + vaddr, ibuf, ibuf + ibuf.getSize()); while (res.next()) if (res.offs() > vaddr) { @@ -2637,7 +2636,7 @@ void PeFile::rebuildResources(SPAN_S(upx_byte) & extra_info, unsigned lastvaddr) } } if (res.dirsize()) { - upx_byte *p = res.build(); + byte *p = res.build(); OCHECK(obuf + (ODADDR(PEDIR_RESOURCE) - rvamin), 16); // write back when the original is zeroed if (get_le32(obuf + (ODADDR(PEDIR_RESOURCE) - rvamin + 12)) == 0) @@ -2646,21 +2645,21 @@ void PeFile::rebuildResources(SPAN_S(upx_byte) & extra_info, unsigned lastvaddr) } template -void PeFile::rebuildImports(SPAN_S(upx_byte) & extra_info, ord_mask_t ord_mask, bool set_oft) { +void PeFile::rebuildImports(SPAN_S(byte) & extra_info, ord_mask_t ord_mask, bool set_oft) { if (ODADDR(PEDIR_IMPORT) == 0 || ODSIZE(PEDIR_IMPORT) <= sizeof(import_desc)) return; - OPTR_VAR(const upx_byte, const imdata, obuf + mem_size(1, get_le32(extra_info))); + OPTR_VAR(const byte, const imdata, obuf + mem_size(1, get_le32(extra_info))); const unsigned inamespos = mem_size(1, get_le32(extra_info + 4)); extra_info += 8; unsigned sdllnames = 0; - IPTR_VAR_OFFSET(const upx_byte, const import, IDADDR(PEDIR_IMPORT) - isection[2].vaddr); - OPTR_VAR(const upx_byte, p, raw_bytes(imdata, 4)); + IPTR_VAR_OFFSET(const byte, const import, IDADDR(PEDIR_IMPORT) - isection[2].vaddr); + OPTR_VAR(const byte, p, raw_bytes(imdata, 4)); for (; get_le32(p) != 0; ++p) { - const upx_byte *dname = raw_bytes(import + mem_size(1, get_le32(p)), 1); + const byte *dname = raw_bytes(import + mem_size(1, get_le32(p)), 1); const unsigned dlen = strlen(dname); ICHECK(dname, dlen + 1); @@ -2676,24 +2675,24 @@ void PeFile::rebuildImports(SPAN_S(upx_byte) & extra_info, ord_mask_t ord_mask, sdllnames = ALIGN_UP(sdllnames, 2u); // TODO: introduce WildPtr for "virtual pointer" pointing before a buffer - upx_byte *const Obuf = obuf.raw_bytes(0) - rvamin; + byte *const Obuf = obuf.raw_bytes(0) - rvamin; #if 0 import_desc * const im0 = (import_desc*) (Obuf + ODADDR(PEDIR_IMPORT)); import_desc *im = im0; - upx_byte *dllnames = Obuf + inamespos; - upx_byte *importednames = dllnames + sdllnames; - upx_byte * const importednames_start = importednames; + byte *dllnames = Obuf + inamespos; + byte *importednames = dllnames + sdllnames; + byte * const importednames_start = importednames; #else SPAN_S_VAR(import_desc, const im0, (import_desc *) (Obuf + ODADDR(PEDIR_IMPORT)), obuf); SPAN_S_VAR(import_desc, im, im0); - SPAN_0_VAR(upx_byte, dllnames, inamespos ? Obuf + inamespos : nullptr, obuf); - SPAN_0_VAR(upx_byte, importednames, inamespos ? dllnames + sdllnames : nullptr); - SPAN_0_VAR(upx_byte, const importednames_start, importednames); + SPAN_0_VAR(byte, dllnames, inamespos ? Obuf + inamespos : nullptr, obuf); + SPAN_0_VAR(byte, importednames, inamespos ? dllnames + sdllnames : nullptr); + SPAN_0_VAR(byte, const importednames_start, importednames); #endif for (p = imdata; get_le32(p) != 0; ++p) { // restore the name of the dll - const upx_byte *dname = raw_bytes(import + get_le32(p), 1); + const byte *dname = raw_bytes(import + get_le32(p), 1); const unsigned dlen = strlen(dname); ICHECK(dname, dlen + 1); @@ -2768,9 +2767,9 @@ void PeFile::unpack0(OutputFile *fo, const ht &ih, ht &oh, ord_mask_t ord_mask, decompress(ibuf, obuf); unsigned skip = get_le32(obuf + (ph.u_len - 4)); unsigned take = sizeof(oh); - SPAN_S_VAR(upx_byte, extra_info, obuf); + SPAN_S_VAR(byte, extra_info, obuf); extra_info = obuf.subref("bad extra_info offset %#x", skip, take); - // upx_byte * const eistart = raw_bytes(extra_info, 0); + // byte * const eistart = raw_bytes(extra_info, 0); memcpy(&oh, extra_info, take); extra_info += take; @@ -2799,7 +2798,7 @@ void PeFile::unpack0(OutputFile *fo, const ht &ih, ht &oh, ord_mask_t ord_mask, if (ph.filter) { Filter ft(ph.level); ft.init(ph.filter, oh.codebase - rvamin); - ft.cto = (unsigned char) ph.filter_cto; + ft.cto = (byte) ph.filter_cto; OCHECK(obuf + (oh.codebase - rvamin), oh.codesize); ft.unfilter(obuf + (oh.codebase - rvamin), oh.codesize); } @@ -2890,7 +2889,7 @@ int PeFile::canUnpack0(unsigned max_sections, unsigned objs, unsigned ih_entry, if (!is_packed && !found_ph) return -1; if (is_packed && ih_entry < isection[2].vaddr) { - unsigned char buf[256]; + byte buf[256]; bool x = false; memset(buf, 0, sizeof(buf)); @@ -2899,7 +2898,7 @@ int PeFile::canUnpack0(unsigned max_sections, unsigned objs, unsigned ih_entry, fi->read(buf, sizeof(buf)); // FIXME this is for x86 - static const unsigned char magic[] = "\x8b\x1e\x83\xee\xfc\x11\xdb"; + static const byte magic[] = "\x8b\x1e\x83\xee\xfc\x11\xdb"; // mov ebx, [esi]; sub esi, -4; adc ebx,ebx int offset = find(buf, sizeof(buf), magic, 7); diff --git a/src/pefile.h b/src/pefile.h index e8174a65..22c51735 100644 --- a/src/pefile.h +++ b/src/pefile.h @@ -92,13 +92,13 @@ protected: unsigned processImports0(ord_mask_t ord_mask); template - void rebuildImports(SPAN_S(upx_byte) & extra_info, ord_mask_t ord_mask, bool set_oft); + void rebuildImports(SPAN_S(byte) & extra_info, ord_mask_t ord_mask, bool set_oft); virtual unsigned processImports() = 0; virtual void processImports2(unsigned, unsigned); MemBuffer mb_oimport; - SPAN_0(upx_byte) oimport = nullptr; + SPAN_0(byte) oimport = nullptr; unsigned soimport; - upx_byte *oimpdlls; + byte *oimpdlls; unsigned soimpdlls; ImportLinker *ilinker; virtual const char *kernelDll() const { return "KERNEL32.DLL"; } @@ -108,25 +108,25 @@ protected: virtual void processRelocs() = 0; void processRelocs(Reloc *); - void rebuildRelocs(SPAN_S(upx_byte) &, unsigned bits, unsigned flags, upx_uint64_t imagebase); + void rebuildRelocs(SPAN_S(byte) &, unsigned bits, unsigned flags, upx_uint64_t imagebase); MemBuffer mb_orelocs; - SPAN_0(upx_byte) orelocs = nullptr; + SPAN_0(byte) orelocs = nullptr; unsigned sorelocs; - upx_byte *oxrelocs = nullptr; + byte *oxrelocs = nullptr; unsigned soxrelocs; void processExports(Export *); void processExports(Export *, unsigned); void rebuildExports(); MemBuffer mb_oexport; - SPAN_0(upx_byte) oexport = nullptr; + SPAN_0(byte) oexport = nullptr; unsigned soexport; void processResources(Resource *); void processResources(Resource *, unsigned); - void rebuildResources(SPAN_S(upx_byte) &, unsigned); + void rebuildResources(SPAN_S(byte) &, unsigned); MemBuffer mb_oresources; - SPAN_0(upx_byte) oresources = nullptr; + SPAN_0(byte) oresources = nullptr; unsigned soresources; template @@ -142,7 +142,7 @@ protected: void rebuildTls(); MemBuffer mb_otls; - SPAN_0(upx_byte) otls = nullptr; + SPAN_0(byte) otls = nullptr; unsigned sotls; unsigned tlsindex; unsigned tlscb_ptr; @@ -152,7 +152,7 @@ protected: void processLoadConf(Reloc *, const Interval *, unsigned); void processLoadConf(Interval *); MemBuffer mb_oloadconf; - upx_byte *oloadconf = nullptr; + byte *oloadconf = nullptr; unsigned soloadconf; unsigned stripDebug(unsigned); @@ -353,7 +353,7 @@ protected: }; class Reloc : private noncopyable { - upx_byte *start; + byte *start; unsigned size; void newRelocPos(void *p); @@ -364,14 +364,14 @@ protected: unsigned counts[16]; public: - Reloc(upx_byte *, unsigned); + Reloc(byte *, unsigned); Reloc(unsigned relocnum); // bool next(unsigned &pos, unsigned &type); const unsigned *getcounts() const { return counts; } // void add(unsigned pos, unsigned type); - void finish(upx_byte *&p, unsigned &size); + void finish(byte *&p, unsigned &size); }; class Resource : private noncopyable { @@ -383,50 +383,50 @@ protected: struct upx_rleaf; MemBuffer mb_start; - const upx_byte *start; - upx_byte *newstart; + const byte *start; + byte *newstart; upx_rnode *root; upx_rleaf *head; upx_rleaf *current; unsigned dsize; unsigned ssize; - const upx_byte *ibufstart; - const upx_byte *ibufend; + const byte *ibufstart; + const byte *ibufend; void check(const res_dir *, unsigned); upx_rnode *convert(const void *, upx_rnode *, unsigned); void build(const upx_rnode *, unsigned &, unsigned &, unsigned); - void clear(upx_byte *, unsigned, Interval *); + void clear(byte *, unsigned, Interval *); void dump(const upx_rnode *, unsigned) const; void destroy(upx_rnode *urd, unsigned level); void ibufcheck(const void *m, unsigned size); public: - Resource(const upx_byte *ibufstart, const upx_byte *ibufen); - Resource(const upx_byte *p, const upx_byte *ibufstart, const upx_byte *ibufend); + Resource(const byte *ibufstart, const byte *ibufen); + Resource(const byte *p, const byte *ibufstart, const byte *ibufend); ~Resource(); - void init(const upx_byte *); + void init(const byte *); unsigned dirsize() const; bool next(); unsigned itype() const; - const upx_byte *ntype() const; + const byte *ntype() const; unsigned size() const; unsigned offs() const; unsigned &newoffs(); - upx_byte *build(); + byte *build(); bool clear(); void dump() const; unsigned iname() const; - const upx_byte *nname() const; + const byte *nname() const; /* unsigned ilang() const {return current->id;} - const upx_byte *nlang() const {return current->name;} + const byte *nlang() const {return current->name;} */ }; diff --git a/src/stub/src/arch/i386/macros.S b/src/stub/src/arch/i386/macros.S index 419936c4..039a01ce 100644 --- a/src/stub/src/arch/i386/macros.S +++ b/src/stub/src/arch/i386/macros.S @@ -428,10 +428,10 @@ section LXUNF000 // enter at +0 for decompression; +2 for unfiltering jmps decompr0 section LXUNF002 // 2+ address of decompress subroutine - // unfilter(upx_byte *, length, cto8) + // unfilter(byte *, length, cto8) lxunfilter: pop edx // return address - pop eax // upx_byte *, same as addvalue + pop eax // byte *, same as addvalue pop ecx // length xchg eax, edi // edi= pointer; eax= saved_edi pusha // save C-convention ebx, ebp, esi, edi; also eax, edx diff --git a/src/ui.h b/src/ui.h index 5cb26352..1eaa8e6a 100644 --- a/src/ui.h +++ b/src/ui.h @@ -26,8 +26,6 @@ */ #pragma once -#ifndef UPX_UI_H__ -#define UPX_UI_H__ 1 class OutputFile; class Packer; @@ -108,6 +106,4 @@ protected: static unsigned update_fu_len; }; -#endif /* already included */ - /* vim:set ts=4 sw=4 et: */ diff --git a/src/util/membuffer.cpp b/src/util/membuffer.cpp index 7513e8d2..4c9d4dda 100644 --- a/src/util/membuffer.cpp +++ b/src/util/membuffer.cpp @@ -181,7 +181,7 @@ void MemBuffer::checkState() const { throwInternalError("block not allocated"); assert(size_in_bytes > 0); if (use_simple_mcheck()) { - const unsigned char *p = (const unsigned char *) ptr; + const byte *p = (const byte *) ptr; if (get_ne32(p - 4) != MAGIC1(p)) throwInternalError("memory clobbered before allocated block 1"); if (get_ne32(p - 8) != size_in_bytes) @@ -201,7 +201,7 @@ void MemBuffer::alloc(upx_uint64_t bytes) { size_t malloc_bytes = mem_size(1, bytes); if (use_simple_mcheck()) malloc_bytes += 32; - unsigned char *p = (unsigned char *) ::malloc(malloc_bytes); + byte *p = (byte *) ::malloc(malloc_bytes); NO_printf("MemBuffer::alloc %llu: %p\n", bytes, p); if (!p) throwOutOfMemoryException(); @@ -234,7 +234,7 @@ void MemBuffer::dealloc() { stats.global_dealloc_counter += 1; stats.global_total_active_bytes -= size_in_bytes; if (use_simple_mcheck()) { - unsigned char *p = (unsigned char *) ptr; + byte *p = (byte *) ptr; // clear magic constants set_ne32(p - 8, 0); set_ne32(p - 4, 0); @@ -278,7 +278,7 @@ TEST_CASE("MemBuffer") { CHECK_THROWS(mb.subref("", 1, 64)); CHECK_THROWS(mb.subref("", 64, 1)); if (use_simple_mcheck()) { - unsigned char *p = raw_bytes(mb, 0); + byte *p = raw_bytes(mb, 0); unsigned magic1 = get_ne32(p - 4); set_ne32(p - 4, magic1 ^ 1); CHECK_THROWS(mb.checkState()); diff --git a/src/util/membuffer.h b/src/util/membuffer.h index 049c95a5..e57b0c5f 100644 --- a/src/util/membuffer.h +++ b/src/util/membuffer.h @@ -80,9 +80,9 @@ public: // raw access } }; -class MemBuffer final : public MemBufferBase { +class MemBuffer final : public MemBufferBase { public: - MemBuffer() : MemBufferBase() {} + MemBuffer() : MemBufferBase() {} explicit MemBuffer(upx_uint64_t bytes); ~MemBuffer(); diff --git a/src/util/raw_bytes.h b/src/util/raw_bytes.h new file mode 100644 index 00000000..f6e7fdde --- /dev/null +++ b/src/util/raw_bytes.h @@ -0,0 +1,81 @@ +/* xspan -- a minimally invasive checked memory smart pointer + + This file is part of the UPX executable compressor. + + Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer + All Rights Reserved. + + UPX and the UCL library are free software; you can redistribute them + and/or modify them under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, Inc., + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Markus F.X.J. Oberhumer + + */ + +#pragma once + +/************************************************************************* +// raw_bytes() - get underlying memory from checked buffers/pointers. +// This is overloaded by various utility classes like MemBuffer and XSpan. +// +// Note that the pointer type is retained, the "_bytes" hints size_in_bytes +**************************************************************************/ + +// default: for any regular pointer, raw_bytes() is just the pointer itself +template +inline + typename std::enable_if::value && !std_is_bounded_array::value, T>::type + raw_bytes(T ptr, size_t size_in_bytes) { + if (size_in_bytes > 0) { + if very_unlikely (ptr == nullptr) + throwCantPack("raw_bytes unexpected NULL ptr"); + if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0)) + throwCantPack("raw_bytes valgrind-check-mem"); + } + return ptr; +} + +// default: for any regular pointer, raw_index_bytes() is just "pointer + index" +// NOTE: index == number of elements, *NOT* size in bytes! +template +inline + typename std::enable_if::value && !std_is_bounded_array::value, T>::type + raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) { + typedef typename std::remove_pointer::type element_type; + if very_unlikely (ptr == nullptr) + throwCantPack("raw_index_bytes unexpected NULL ptr"); + size_in_bytes = mem_size(sizeof(element_type), index, size_in_bytes); // assert size + if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0)) + throwCantPack("raw_index_bytes valgrind-check-mem"); + UNUSED(size_in_bytes); + return ptr + index; +} + +// same for bounded arrays +template +inline T *raw_bytes(T (&a)[N], size_t size_in_bytes) { + typedef T element_type; + if very_unlikely (size_in_bytes > mem_size(sizeof(element_type), N)) + throwCantPack("raw_bytes out of range"); + return a; +} + +template +inline T *raw_index_bytes(T (&a)[N], size_t index, size_t size_in_bytes) { + typedef T element_type; + return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index; +} + +/* vim:set ts=4 sw=4 et: */ diff --git a/src/util/util.cpp b/src/util/util.cpp index f842b9a6..46fdd91c 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -261,8 +261,8 @@ int find(const void *buf, int blen, const void *what, int wlen) { if (buf == nullptr || blen <= 0 || what == nullptr || wlen <= 0) return -1; - const unsigned char *b = (const unsigned char *) buf; - unsigned char first_byte = *(const unsigned char *) what; + const byte *b = (const byte *) buf; + byte first_byte = *(const byte *) what; blen -= wlen; for (int i = 0; i <= blen; i++, b++) @@ -273,44 +273,44 @@ int find(const void *buf, int blen, const void *what, int wlen) { } int find_be16(const void *b, int blen, unsigned what) { - unsigned char w[2]; + byte w[2]; set_be16(w, what); return find(b, blen, w, 2); } int find_be32(const void *b, int blen, unsigned what) { - unsigned char w[4]; + byte w[4]; set_be32(w, what); return find(b, blen, w, 4); } int find_be64(const void *b, int blen, upx_uint64_t what) { - unsigned char w[8]; + byte w[8]; set_be64(w, what); return find(b, blen, w, 8); } int find_le16(const void *b, int blen, unsigned what) { - unsigned char w[2]; + byte w[2]; set_le16(w, what); return find(b, blen, w, 2); } int find_le32(const void *b, int blen, unsigned what) { - unsigned char w[4]; + byte w[4]; set_le32(w, what); return find(b, blen, w, 4); } int find_le64(const void *b, int blen, upx_uint64_t what) { - unsigned char w[8]; + byte w[8]; set_le64(w, what); return find(b, blen, w, 8); } TEST_CASE("find") { CHECK(find(nullptr, -1, nullptr, -1) == -1); - static const unsigned char b[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + static const byte b[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; CHECK(find(b, 16, b, 0) == -1); for (int i = 0; i < 16; i++) { CHECK(find(b, 16, b + i, 1) == i); @@ -330,7 +330,7 @@ TEST_CASE("find") { } int mem_replace(void *buf, int blen, const void *what, int wlen, const void *replacement) { - unsigned char *b = (unsigned char *) buf; + byte *b = (byte *) buf; int boff = 0; int n = 0; diff --git a/src/util/xspan.cpp b/src/util/xspan.cpp index 1fad5aa1..fb105a9d 100644 --- a/src/util/xspan.cpp +++ b/src/util/xspan.cpp @@ -46,28 +46,28 @@ static XSpanStats xspan_stats; // HINT: set env-var "UPX_DEBUG_DOCTEST_DISABLE=1" for improved debugging experience noinline void xspan_fail_nullptr() { xspan_stats.fail_nullptr += 1; - throwCantUnpack("xspan unexpected NULL pointer; take care!"); + throwCantPack("xspan unexpected NULL pointer; take care!"); } noinline void xspan_fail_nullbase() { xspan_stats.fail_nullbase += 1; - throwCantUnpack("xspan unexpected NULL base; take care!"); + throwCantPack("xspan unexpected NULL base; take care!"); } noinline void xspan_fail_not_same_base() { xspan_stats.fail_not_same_base += 1; - throwInternalError("xspan unexpected base pointer; take care!"); + throwCantPack("xspan unexpected base pointer; take care!"); } noinline void xspan_fail_range_nullptr() { xspan_stats.fail_range_nullptr += 1; - throwCantUnpack("xspan_check_range: unexpected NULL pointer; take care!"); + throwCantPack("xspan_check_range: unexpected NULL pointer; take care!"); } noinline void xspan_fail_range_nullbase() { xspan_stats.fail_range_nullbase += 1; - throwCantUnpack("xspan_check_range: unexpected NULL base; take care!"); + throwCantPack("xspan_check_range: unexpected NULL base; take care!"); } noinline void xspan_fail_range_range() { xspan_stats.fail_range_range += 1; - throwCantUnpack("xspan_check_range: pointer out of range; take care!"); + throwCantPack("xspan_check_range: pointer out of range; take care!"); } void xspan_check_range(const void *p, const void *base, ptrdiff_t size_in_bytes) { diff --git a/src/util/xspan.h b/src/util/xspan.h index 6cfef65b..fff8cb91 100644 --- a/src/util/xspan.h +++ b/src/util/xspan.h @@ -159,56 +159,4 @@ inline R *xspan_make_helper__(R * /*dummy*/, MemBuffer &first) { #define SPAN_S_VAR XSPAN_S_VAR #endif -/************************************************************************* -// raw_bytes() - get underlying memory from checked buffers/pointers. -// This is overloaded by various utility classes like MemBuffer and XSpan. -// -// Note that the pointer type is retained, the "_bytes" hints size_in_bytes -**************************************************************************/ - -// default: for any regular pointer, raw_bytes() is just the pointer itself -template -inline - typename std::enable_if::value && !std_is_bounded_array::value, T>::type - raw_bytes(T ptr, size_t size_in_bytes) { - if (size_in_bytes > 0) { - if very_unlikely (ptr == nullptr) - throwInternalError("raw_bytes unexpected NULL ptr"); - if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0)) - throwInternalError("raw_bytes valgrind-check-mem"); - } - return ptr; -} - -// default: for any regular pointer, raw_index_bytes() is just "pointer + index" -// NOTE: index == number of elements, *NOT* size in bytes! -template -inline - typename std::enable_if::value && !std_is_bounded_array::value, T>::type - raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) { - typedef typename std::remove_pointer::type element_type; - if very_unlikely (ptr == nullptr) - throwInternalError("raw_index_bytes unexpected NULL ptr"); - size_in_bytes = mem_size(sizeof(element_type), index, size_in_bytes); // assert size - if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0)) - throwInternalError("raw_index_bytes valgrind-check-mem"); - UNUSED(size_in_bytes); - return ptr + index; -} - -// same for bounded arrays -template -inline T *raw_bytes(T (&a)[N], size_t size_in_bytes) { - typedef T element_type; - if very_unlikely (size_in_bytes > mem_size(sizeof(element_type), N)) - throwInternalError("raw_bytes out of range"); - return a; -} - -template -inline T *raw_index_bytes(T (&a)[N], size_t index, size_t size_in_bytes) { - typedef T element_type; - return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index; -} - /* vim:set ts=4 sw=4 et: */ diff --git a/src/util/xspan_impl_common.h b/src/util/xspan_impl_common.h index fb171ca9..10c11ff9 100644 --- a/src/util/xspan_impl_common.h +++ b/src/util/xspan_impl_common.h @@ -94,9 +94,15 @@ forceinline pointer ensureBase() const { } public: -inline ~CSelf() {} -void destroy() { +inline ~CSelf() { +#if DEBUG + invalidate(); +#endif +} +noinline void invalidate() { assertInvariants(); + ptr = (pointer) (acc_uintptr_t) 16; // point to non-null invalid address + // ptr = (pointer) (void *) &ptr; // point to self base = ptr; size_in_bytes = 0; assertInvariants(); diff --git a/src/util/xspan_impl_ptr.h b/src/util/xspan_impl_ptr.h index 2825da98..c4df6b6b 100644 --- a/src/util/xspan_impl_ptr.h +++ b/src/util/xspan_impl_ptr.h @@ -62,7 +62,17 @@ public: operator pointer() const { return ptr; } #endif - inline ~CSelf() {} + inline ~CSelf() { +#if DEBUG + invalidate(); +#endif + } + noinline void invalidate() { + assertInvariants(); + ptr = (pointer) (acc_uintptr_t) 16; // point to non-null invalid address + // ptr = (pointer) (void *) &ptr; // point to self + assertInvariants(); + } inline CSelf() { assertInvariants(); } // constructors from pointers