src: move compress files into a subdir; clang-format some more files; NFC
This commit is contained in:
+27
-49
@@ -25,42 +25,34 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#include "conf.h"
|
||||
#include "filter.h"
|
||||
#include "file.h"
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// util
|
||||
**************************************************************************/
|
||||
|
||||
static //inline
|
||||
void initFilter(Filter *f, upx_byte *buf, unsigned buf_len)
|
||||
{
|
||||
static void initFilter(Filter *f, upx_byte *buf, unsigned buf_len) {
|
||||
f->buf = buf;
|
||||
f->buf_len = buf_len;
|
||||
// clear output parameters
|
||||
f->calls = f->wrongcalls = f->noncalls = f->firstcall = f->lastcall = 0;
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// get a FilterEntry
|
||||
**************************************************************************/
|
||||
|
||||
const FilterImp::FilterEntry *FilterImp::getFilter(int id)
|
||||
{
|
||||
const FilterImpl::FilterEntry *FilterImpl::getFilter(int id) {
|
||||
static bool done = false;
|
||||
static unsigned char filter_map[256];
|
||||
|
||||
if (!done)
|
||||
{
|
||||
if (!done) {
|
||||
// init the filter_map[]
|
||||
assert(n_filters <= 254); // as 0xff means "empty slot"
|
||||
assert(n_filters <= 254); // as 0xff means "empty slot"
|
||||
memset(filter_map, 0xff, sizeof(filter_map));
|
||||
for (int i = 0; i < n_filters; i++)
|
||||
{
|
||||
for (int i = 0; i < n_filters; i++) {
|
||||
int filter_id = filters[i].id;
|
||||
assert(filter_id >= 0 && filter_id <= 255);
|
||||
assert(filter_map[filter_id] == 0xff);
|
||||
@@ -72,21 +64,18 @@ const FilterImp::FilterEntry *FilterImp::getFilter(int id)
|
||||
if (id < 0 || id > 255)
|
||||
return nullptr;
|
||||
unsigned index = filter_map[id];
|
||||
if (index == 0xff) // empty slot
|
||||
if (index == 0xff) // empty slot
|
||||
return nullptr;
|
||||
assert(filters[index].id == id);
|
||||
return &filters[index];
|
||||
}
|
||||
|
||||
|
||||
bool Filter::isValidFilter(int filter_id)
|
||||
{
|
||||
const FilterImp::FilterEntry * const fe = FilterImp::getFilter(filter_id);
|
||||
bool Filter::isValidFilter(int filter_id) {
|
||||
const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(filter_id);
|
||||
return fe != nullptr;
|
||||
}
|
||||
|
||||
bool Filter::isValidFilter(int filter_id, const int *allowed_filters)
|
||||
{
|
||||
bool Filter::isValidFilter(int filter_id, const int *allowed_filters) {
|
||||
if (!isValidFilter(filter_id))
|
||||
return false;
|
||||
if (filter_id == 0)
|
||||
@@ -99,13 +88,11 @@ bool Filter::isValidFilter(int filter_id, const int *allowed_filters)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// high level API
|
||||
**************************************************************************/
|
||||
|
||||
void Filter::init(int id_, unsigned addvalue_)
|
||||
{
|
||||
void Filter::init(int id_, unsigned addvalue_) {
|
||||
this->id = id_;
|
||||
initFilter(this, nullptr, 0);
|
||||
// clear input parameters
|
||||
@@ -116,12 +103,10 @@ void Filter::init(int id_, unsigned addvalue_)
|
||||
this->n_mru = 0;
|
||||
}
|
||||
|
||||
|
||||
bool Filter::filter(upx_byte *buf_, unsigned buf_len_)
|
||||
{
|
||||
bool Filter::filter(upx_byte *buf_, unsigned buf_len_) {
|
||||
initFilter(this, buf_, buf_len_);
|
||||
|
||||
const FilterImp::FilterEntry * const fe = FilterImp::getFilter(id);
|
||||
const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id);
|
||||
if (fe == nullptr)
|
||||
throwInternalError("filter-1");
|
||||
if (fe->id == 0)
|
||||
@@ -138,10 +123,10 @@ bool Filter::filter(upx_byte *buf_, unsigned buf_len_)
|
||||
if (clevel != 1)
|
||||
this->adler = upx_adler32(this->buf, this->buf_len);
|
||||
|
||||
//printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
//OutputFile::dump("filter.dat", buf, buf_len);
|
||||
NO_printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
// OutputFile::dump("filter.dat", buf, buf_len);
|
||||
int r = (*fe->do_filter)(this);
|
||||
//printf("filter: %02x %d\n", fe->id, r);
|
||||
NO_printf("filter: %02x %d\n", fe->id, r);
|
||||
if (r > 0)
|
||||
throwFilterException();
|
||||
if (r == 0)
|
||||
@@ -149,12 +134,10 @@ bool Filter::filter(upx_byte *buf_, unsigned buf_len_)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Filter::unfilter(upx_byte *buf_, unsigned buf_len_, bool verify_checksum)
|
||||
{
|
||||
void Filter::unfilter(upx_byte *buf_, unsigned buf_len_, bool verify_checksum) {
|
||||
initFilter(this, buf_, buf_len_);
|
||||
|
||||
const FilterImp::FilterEntry * const fe = FilterImp::getFilter(id);
|
||||
const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id);
|
||||
if (fe == nullptr)
|
||||
throwInternalError("unfilter-1");
|
||||
if (fe->id == 0)
|
||||
@@ -166,24 +149,21 @@ void Filter::unfilter(upx_byte *buf_, unsigned buf_len_, bool verify_checksum)
|
||||
if (!fe->do_unfilter)
|
||||
throwInternalError("unfilter-2");
|
||||
|
||||
//printf("unfilter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
NO_printf("unfilter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
int r = (*fe->do_unfilter)(this);
|
||||
//printf("unfilter: %02x %d\n", fe->id, r);
|
||||
NO_printf("unfilter: %02x %d\n", fe->id, r);
|
||||
if (r != 0)
|
||||
throwInternalError("unfilter-3");
|
||||
//OutputFile::dump("unfilter.dat", buf, buf_len);
|
||||
// OutputFile::dump("unfilter.dat", buf, buf_len);
|
||||
|
||||
// verify checksum
|
||||
if (verify_checksum && clevel != 1)
|
||||
{
|
||||
if (verify_checksum && clevel != 1) {
|
||||
if (this->adler != upx_adler32(this->buf, this->buf_len))
|
||||
throwInternalError("unfilter-4");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Filter::verifyUnfilter()
|
||||
{
|
||||
void Filter::verifyUnfilter() {
|
||||
// Note:
|
||||
// This verify is just because of complete paranoia that there
|
||||
// could be a hidden bug in the filter implementation, and
|
||||
@@ -193,20 +173,18 @@ void Filter::verifyUnfilter()
|
||||
// See also:
|
||||
// Packer::verifyOverlappingDecompression()
|
||||
|
||||
//printf("verifyUnfilter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
NO_printf("verifyUnfilter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
if (clevel != 1)
|
||||
unfilter(this->buf, this->buf_len, true);
|
||||
}
|
||||
|
||||
|
||||
bool Filter::scan(const upx_byte *buf_, unsigned buf_len_)
|
||||
{
|
||||
bool Filter::scan(const upx_byte *buf_, unsigned buf_len_) {
|
||||
// Note: must use const_cast here. This is fine as the scan
|
||||
// implementations (fe->do_scan) actually don't change the buffer.
|
||||
upx_byte *b = const_cast<upx_byte *>(buf_);
|
||||
initFilter(this, b, buf_len_);
|
||||
|
||||
const FilterImp::FilterEntry * const fe = FilterImp::getFilter(id);
|
||||
const FilterImpl::FilterEntry *const fe = FilterImpl::getFilter(id);
|
||||
if (fe == nullptr)
|
||||
throwInternalError("scan-1");
|
||||
if (fe->id == 0)
|
||||
@@ -218,9 +196,9 @@ bool Filter::scan(const upx_byte *buf_, unsigned buf_len_)
|
||||
if (!fe->do_scan)
|
||||
throwInternalError("scan-2");
|
||||
|
||||
//printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
NO_printf("filter: %02x %p %d\n", this->id, this->buf, this->buf_len);
|
||||
int r = (*fe->do_scan)(this);
|
||||
//printf("filter: %02x %d\n", fe->id, r);
|
||||
NO_printf("filter: %02x %d\n", fe->id, r);
|
||||
if (r > 0)
|
||||
throwFilterException();
|
||||
if (r == 0)
|
||||
|
||||
Reference in New Issue
Block a user