src: clang-format various packers

This commit is contained in:
Markus F.X.J. Oberhumer
2022-11-26 17:45:25 +01:00
parent 93c6496ba8
commit 9f5d67e27c
13 changed files with 863 additions and 1275 deletions
+44 -84
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com>
*/
#include "conf.h"
#include "file.h"
#include "filter.h"
@@ -33,49 +32,42 @@
#include "p_com.h"
#include "linker.h"
static const
static const CLANG_FORMAT_DUMMY_STATEMENT
#include "stub/i086-dos16.com.h"
//#define TESTING 1
/*************************************************************************
//
**************************************************************************/
const int *PackCom::getCompressionMethods(int method, int level) const
{
static const int m_nrv2b[] = { M_NRV2B_LE16, M_END };
const int *PackCom::getCompressionMethods(int method, int level) const {
static const int m_nrv2b[] = {M_NRV2B_LE16, M_END};
#if 0
static const int m_nrv2d[] = { M_NRV2D_LE16, M_END };
#endif
UNUSED(method); UNUSED(level);
UNUSED(method);
UNUSED(level);
return m_nrv2b;
}
const int *PackCom::getFilters() const
{
static const int filters[] = {
0x06, 0x03, 0x04, 0x01, 0x05, 0x02,
FT_END };
const int *PackCom::getFilters() const {
static const int filters[] = {0x06, 0x03, 0x04, 0x01, 0x05, 0x02, FT_END};
return filters;
}
/*************************************************************************
//
**************************************************************************/
bool PackCom::canPack()
{
bool PackCom::canPack() {
unsigned char buf[128];
fi->readx(buf, sizeof(buf));
if (memcmp(buf,"MZ",2) == 0 || memcmp(buf,"ZM",2) == 0 // .exe
|| memcmp (buf,"\xff\xff\xff\xff",4) == 0) // .sys
if (memcmp(buf, "MZ", 2) == 0 || memcmp(buf, "ZM", 2) == 0 // .exe
|| memcmp(buf, "\xff\xff\xff\xff", 4) == 0) // .sys
return false;
if (!fn_has_ext(fi->getName(),"com"))
if (!fn_has_ext(fi->getName(), "com"))
return false;
checkAlreadyPacked(buf, sizeof(buf));
if (file_size < 1024)
@@ -85,15 +77,11 @@ bool PackCom::canPack()
return true;
}
/*************************************************************************
//
**************************************************************************/
void PackCom::patchLoader(OutputFile *fo,
upx_byte *loader, int lsize,
unsigned calls)
{
void PackCom::patchLoader(OutputFile *fo, upx_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);
@@ -118,11 +106,11 @@ void PackCom::patchLoader(OutputFile *fo,
loader = getLoader();
// some day we could use the relocation stuff for patchPackHeader too
patchPackHeader(loader,e_len);
patchPackHeader(loader, e_len);
// write loader + compressed file
fo->write(loader,e_len); // entry
fo->write(obuf,ph.c_len);
fo->write(loader+e_len,d_len); // decompressor
fo->write(loader, e_len); // entry
fo->write(obuf, ph.c_len);
fo->write(loader + e_len, d_len); // decompressor
#if 0
printf("%-13s: entry : %8ld bytes\n", getName(), (long) e_len);
printf("%-13s: compressed : %8ld bytes\n", getName(), (long) ph.c_len);
@@ -130,65 +118,45 @@ void PackCom::patchLoader(OutputFile *fo,
#endif
}
void PackCom::buildLoader(const Filter *ft)
{
void PackCom::buildLoader(const Filter *ft) {
initLoader(stub_i086_dos16_com, sizeof(stub_i086_dos16_com));
addLoader("COMMAIN1",
ph.first_offset_found == 1 ? "COMSBBBP" : "",
"COMPSHDI",
ft->id ? "COMCALLT" : "",
"COMMAIN2,UPX1HEAD,COMCUTPO,NRV2B160",
ft->id ? "NRVDDONE" : "NRVDRETU",
"NRVDECO1",
ph.max_offset_found <= 0xd00 ? "NRVLED00" : "NRVGTD00",
"NRVDECO2",
nullptr
);
if (ft->id)
{
addLoader("COMMAIN1", ph.first_offset_found == 1 ? "COMSBBBP" : "", "COMPSHDI",
ft->id ? "COMCALLT" : "", "COMMAIN2,UPX1HEAD,COMCUTPO,NRV2B160",
ft->id ? "NRVDDONE" : "NRVDRETU", "NRVDECO1",
ph.max_offset_found <= 0xd00 ? "NRVLED00" : "NRVGTD00", "NRVDECO2", nullptr);
if (ft->id) {
assert(ft->calls > 0);
addFilter16(ft->id);
}
}
void PackCom::addFilter16(int filter_id)
{
void PackCom::addFilter16(int filter_id) {
assert(filter_id > 0);
assert(isValidFilter(filter_id));
if (filter_id % 3 == 0)
addLoader("CALLTR16",
filter_id < 4 ? "CT16SUB0" : "",
filter_id < 4 ? "" : (opt->cpu == opt->CPU_8086 ? "CT16I086" : "CT16I286,CT16SUB0"),
"CALLTRI2",
getFormat() == UPX_F_DOS_COM ? "CORETURN" : "",
nullptr
);
addLoader("CALLTR16", filter_id < 4 ? "CT16SUB0" : "",
filter_id < 4 ? ""
: (opt->cpu == opt->CPU_8086 ? "CT16I086" : "CT16I286,CT16SUB0"),
"CALLTRI2", getFormat() == UPX_F_DOS_COM ? "CORETURN" : "", nullptr);
else
addLoader(filter_id%3 == 1 ? "CT16E800" : "CT16E900",
"CALLTRI5",
getFormat() == UPX_F_DOS_COM ? "CT16JEND" : "CT16JUL2",
filter_id < 4 ? "CT16SUB1" : "",
filter_id < 4 ? "" : (opt->cpu == opt->CPU_8086 ? "CT16I087" : "CT16I287,CT16SUB1"),
"CALLTRI6",
nullptr
);
addLoader(
filter_id % 3 == 1 ? "CT16E800" : "CT16E900", "CALLTRI5",
getFormat() == UPX_F_DOS_COM ? "CT16JEND" : "CT16JUL2", filter_id < 4 ? "CT16SUB1" : "",
filter_id < 4 ? "" : (opt->cpu == opt->CPU_8086 ? "CT16I087" : "CT16I287,CT16SUB1"),
"CALLTRI6", nullptr);
}
/*************************************************************************
//
**************************************************************************/
void PackCom::pack(OutputFile *fo)
{
void PackCom::pack(OutputFile *fo) {
// read file
ibuf.alloc(file_size);
obuf.allocForCompression(file_size);
fi->seek(0,SEEK_SET);
fi->readx(ibuf,file_size);
fi->seek(0, SEEK_SET);
fi->readx(ibuf, file_size);
// prepare packheader
ph.u_len = file_size;
@@ -201,7 +169,7 @@ void PackCom::pack(OutputFile *fo)
const int lsize = getLoaderSize();
MemBuffer loader(lsize);
memcpy(loader,getLoader(),lsize);
memcpy(loader, getLoader(), lsize);
const unsigned calls = ft.id % 3 ? ft.lastcall - 2 * ft.calls : ft.calls;
patchLoader(fo, loader, lsize, calls);
@@ -214,13 +182,11 @@ void PackCom::pack(OutputFile *fo)
throwNotCompressible();
}
/*************************************************************************
//
**************************************************************************/
int PackCom::canUnpack()
{
int PackCom::canUnpack() {
if (!readPackHeader(128))
return false;
if (file_size_u <= ph.c_len)
@@ -228,19 +194,17 @@ int PackCom::canUnpack()
return true;
}
/*************************************************************************
//
**************************************************************************/
void PackCom::unpack(OutputFile *fo)
{
void PackCom::unpack(OutputFile *fo) {
ibuf.alloc(file_size);
obuf.allocForDecompression(ph.u_len);
// read whole file
fi->seek(0,SEEK_SET);
fi->readx(ibuf,file_size);
fi->seek(0, SEEK_SET);
fi->readx(ibuf, file_size);
// get compressed data offset
unsigned e_len = ph.buf_offset + ph.getPackHeaderSize();
@@ -248,22 +212,18 @@ void PackCom::unpack(OutputFile *fo)
throwCantUnpack("file damaged");
// decompress
decompress(ibuf+e_len,obuf);
decompress(ibuf + e_len, obuf);
// unfilter
Filter ft(ph.level);
ft.init(ph.filter, getCallTrickOffset());
ft.unfilter(obuf,ph.u_len);
ft.unfilter(obuf, ph.u_len);
// write decompressed file
if (fo)
fo->write(obuf,ph.u_len);
fo->write(obuf, ph.u_len);
}
Linker* PackCom::newLinker() const
{
return new ElfLinkerX86();
}
Linker *PackCom::newLinker() const { return new ElfLinkerX86(); }
/* vim:set ts=4 sw=4 et: */