clang-format files.

This commit is contained in:
Markus F.X.J. Oberhumer 2017-07-22 12:47:45 +02:00
parent 6de021d8d8
commit 33dc947c21
5 changed files with 170 additions and 264 deletions

View File

@ -163,11 +163,11 @@ endif
# automatically format some C++ source code files # automatically format some C++ source code files
ifeq ($(shell uname),Linux) ifeq ($(shell uname),Linux)
CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp packmast.cpp CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp packmast.cpp packmast.h
CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h
CLANG_FORMAT_FILES += snprintf.cpp CLANG_FORMAT_FILES += snprintf.cpp
CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h
CLANG_FORMAT_FILES += ui.cpp ui.h util.h CLANG_FORMAT_FILES += ui.cpp ui.h util.cpp util.h work.cpp
clang-format: clang-format:
$(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES)) $(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES))
.PHONY: clang-format .PHONY: clang-format

View File

@ -833,7 +833,7 @@ void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, upx_
} }
void ElfLinkerPpc64::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, void ElfLinkerPpc64::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value,
const char *type) { const char *type) {
if (!strcmp(type, "R_PPC64_ADDR32")) { if (!strcmp(type, "R_PPC64_ADDR32")) {
set_be32(location, get_be32(location) + value); set_be32(location, get_be32(location) + value);
return; return;
@ -855,9 +855,9 @@ void ElfLinkerPpc64::relocate1(const Relocation *rel, upx_byte *location, upx_ui
value -= rel->section->offset + rel->offset; value -= rel->section->offset + rel->offset;
if (strncmp(type, "14", 2) == 0) // for "14" and "14S" if (strncmp(type, "14", 2) == 0) // for "14" and "14S"
set_be16(2+ location, get_be16(2+ location) + value); set_be16(2 + location, get_be16(2 + location) + value);
else if (strncmp(type, "24", 2) == 0) // for "24" and "24S" else if (strncmp(type, "24", 2) == 0) // for "24" and "24S"
set_be24(1+ location, get_be24(1+ location) + value); set_be24(1 + location, get_be24(1 + location) + value);
else else
super::relocate1(rel, location, value, type); super::relocate1(rel, location, value, type);
} }

View File

@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_PACKMASTER_H #ifndef __UPX_PACKMASTER_H
#define __UPX_PACKMASTER_H 1 #define __UPX_PACKMASTER_H 1
@ -33,15 +32,13 @@ class Packer;
class InputFile; class InputFile;
class OutputFile; class OutputFile;
/************************************************************************* /*************************************************************************
// interface for work.cpp // interface for work.cpp
**************************************************************************/ **************************************************************************/
class PackMaster class PackMaster {
{
public: public:
PackMaster(InputFile *f, options_t *o=NULL); PackMaster(InputFile *f, options_t *o = NULL);
virtual ~PackMaster(); virtual ~PackMaster();
void pack(OutputFile *fo); void pack(OutputFile *fo);
@ -50,8 +47,8 @@ public:
void list(); void list();
void fileInfo(); void fileInfo();
typedef Packer* (*visit_func_t)(Packer *p, void *user); typedef Packer *(*visit_func_t)(Packer *p, void *user);
static Packer* visitAllPackers(visit_func_t, InputFile *f, const options_t *, void *user); static Packer *visitAllPackers(visit_func_t, InputFile *f, const options_t *, void *user);
private: private:
InputFile *fi; InputFile *fi;
@ -65,7 +62,6 @@ private:
options_t *saved_opt; options_t *saved_opt;
}; };
#endif /* already included */ #endif /* already included */
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */

View File

@ -25,15 +25,14 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#include "conf.h" #include "conf.h"
#include "util.h" #include "util.h"
#if (ACC_CC_MSC && (_MSC_VER >= 1000 && _MSC_VER < 1200)) #if (ACC_CC_MSC && (_MSC_VER >= 1000 && _MSC_VER < 1200))
/* avoid -W4 warnings in <conio.h> */ /* avoid -W4 warnings in <conio.h> */
# pragma warning(disable: 4032) #pragma warning(disable : 4032)
/* avoid -W4 warnings in <windows.h> */ /* avoid -W4 warnings in <windows.h> */
# pragma warning(disable: 4201 4214 4514) #pragma warning(disable : 4201 4214 4514)
#endif #endif
#define ACC_WANT_ACC_INCI_H 1 #define ACC_WANT_ACC_INCI_H 1
#include "miniacc.h" #include "miniacc.h"
@ -45,7 +44,6 @@
#undef HAVE_MKDIR #undef HAVE_MKDIR
#include "miniacc.h" #include "miniacc.h"
/************************************************************************* /*************************************************************************
// assert sane memory buffer sizes to protect against integer overflows // assert sane memory buffer sizes to protect against integer overflows
// and malicious header fields // and malicious header fields
@ -53,50 +51,57 @@
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_MEM == UPX_RSIZE_MAX) ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_MEM == UPX_RSIZE_MAX)
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_STR <= UPX_RSIZE_MAX / 256) ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_STR <= UPX_RSIZE_MAX / 256)
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * UPX_RSIZE_MAX * 9 / 8 + 16*1024*1024 < INT_MAX) ACC_COMPILE_TIME_ASSERT_HEADER(2ull * UPX_RSIZE_MAX * 9 / 8 + 16 * 1024 * 1024 < INT_MAX)
upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1, upx_uint64_t extra2) upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1,
{ upx_uint64_t extra2) {
assert(element_size > 0); assert(element_size > 0);
if (element_size > UPX_RSIZE_MAX) throwCantPack("mem_size 1; take care"); if (element_size > UPX_RSIZE_MAX)
if (n > UPX_RSIZE_MAX) throwCantPack("mem_size 2; take care"); throwCantPack("mem_size 1; take care");
if (extra1 > UPX_RSIZE_MAX) throwCantPack("mem_size 3; take care"); if (n > UPX_RSIZE_MAX)
if (extra2 > UPX_RSIZE_MAX) throwCantPack("mem_size 4; take care"); throwCantPack("mem_size 2; take care");
if (extra1 > UPX_RSIZE_MAX)
throwCantPack("mem_size 3; take care");
if (extra2 > UPX_RSIZE_MAX)
throwCantPack("mem_size 4; take care");
upx_uint64_t bytes = element_size * n + extra1 + extra2; // cannot overflow upx_uint64_t bytes = element_size * n + extra1 + extra2; // cannot overflow
if (bytes > UPX_RSIZE_MAX) throwCantPack("mem_size 5; take care"); if (bytes > UPX_RSIZE_MAX)
throwCantPack("mem_size 5; take care");
return ACC_ICONV(upx_rsize_t, bytes); return ACC_ICONV(upx_rsize_t, bytes);
} }
upx_rsize_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n) upx_rsize_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n) {
{
mem_size_assert(element_size, n); mem_size_assert(element_size, n);
return ACC_ICONV(upx_rsize_t, n); // return n return ACC_ICONV(upx_rsize_t, n); // return n
} }
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1, upx_uint64_t extra2) bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1,
{ upx_uint64_t extra2) {
assert(element_size > 0); assert(element_size > 0);
if (element_size > UPX_RSIZE_MAX) return false; if (element_size > UPX_RSIZE_MAX)
if (n > UPX_RSIZE_MAX) return false; return false;
if (extra1 > UPX_RSIZE_MAX) return false; if (n > UPX_RSIZE_MAX)
if (extra2 > UPX_RSIZE_MAX) return false; return false;
if (extra1 > UPX_RSIZE_MAX)
return false;
if (extra2 > UPX_RSIZE_MAX)
return false;
upx_uint64_t bytes = element_size * n + extra1 + extra2; // cannot overflow upx_uint64_t bytes = element_size * n + extra1 + extra2; // cannot overflow
if (bytes > UPX_RSIZE_MAX) return false; if (bytes > UPX_RSIZE_MAX)
return false;
return true; return true;
} }
bool mem_size_valid_bytes(upx_uint64_t bytes) bool mem_size_valid_bytes(upx_uint64_t bytes) {
{ if (bytes > UPX_RSIZE_MAX)
if (bytes > UPX_RSIZE_MAX) return false; return false;
return true; return true;
} }
int ptr_diff(const void *p1, const void *p2) {
int ptr_diff(const void *p1, const void *p2)
{
assert(p1 != NULL); assert(p1 != NULL);
assert(p2 != NULL); assert(p2 != NULL);
ptrdiff_t d = (const char *)p1 - (const char *)p2; ptrdiff_t d = (const char *) p1 - (const char *) p2;
if (p1 >= p2) if (p1 >= p2)
assert(mem_size_valid_bytes(d)); assert(mem_size_valid_bytes(d));
else else
@ -104,14 +109,12 @@ int ptr_diff(const void *p1, const void *p2)
return ACC_ICONV(int, d); return ACC_ICONV(int, d);
} }
unsigned ptr_udiff(const void *p1, const void *p2) unsigned ptr_udiff(const void *p1, const void *p2) {
{
int d = ptr_diff(p1, p2); int d = ptr_diff(p1, p2);
assert(d >= 0); assert(d >= 0);
return ACC_ICONV(unsigned, d); return ACC_ICONV(unsigned, d);
} }
/************************************************************************* /*************************************************************************
// bele.h // bele.h
**************************************************************************/ **************************************************************************/
@ -126,137 +129,117 @@ const BEPolicy be_policy;
const LEPolicy le_policy; const LEPolicy le_policy;
} }
/************************************************************************* /*************************************************************************
// qsort() util // qsort() util
**************************************************************************/ **************************************************************************/
int __acc_cdecl_qsort be16_compare(const void *e1, const void *e2) int __acc_cdecl_qsort be16_compare(const void *e1, const void *e2) {
{
const unsigned d1 = get_be16(e1); const unsigned d1 = get_be16(e1);
const unsigned d2 = get_be16(e2); const unsigned d2 = get_be16(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be24_compare(const void *e1, const void *e2) int __acc_cdecl_qsort be24_compare(const void *e1, const void *e2) {
{
const unsigned d1 = get_be24(e1); const unsigned d1 = get_be24(e1);
const unsigned d2 = get_be24(e2); const unsigned d2 = get_be24(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be32_compare(const void *e1, const void *e2) int __acc_cdecl_qsort be32_compare(const void *e1, const void *e2) {
{
const unsigned d1 = get_be32(e1); const unsigned d1 = get_be32(e1);
const unsigned d2 = get_be32(e2); const unsigned d2 = get_be32(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be64_compare(const void *e1, const void *e2) int __acc_cdecl_qsort be64_compare(const void *e1, const void *e2) {
{
const upx_uint64_t d1 = get_be64(e1); const upx_uint64_t d1 = get_be64(e1);
const upx_uint64_t d2 = get_be64(e2); const upx_uint64_t d2 = get_be64(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le16_compare(const void *e1, const void *e2) int __acc_cdecl_qsort le16_compare(const void *e1, const void *e2) {
{
const unsigned d1 = get_le16(e1); const unsigned d1 = get_le16(e1);
const unsigned d2 = get_le16(e2); const unsigned d2 = get_le16(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le24_compare(const void *e1, const void *e2) int __acc_cdecl_qsort le24_compare(const void *e1, const void *e2) {
{
const unsigned d1 = get_le24(e1); const unsigned d1 = get_le24(e1);
const unsigned d2 = get_le24(e2); const unsigned d2 = get_le24(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le32_compare(const void *e1, const void *e2) int __acc_cdecl_qsort le32_compare(const void *e1, const void *e2) {
{
const unsigned d1 = get_le32(e1); const unsigned d1 = get_le32(e1);
const unsigned d2 = get_le32(e2); const unsigned d2 = get_le32(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le64_compare(const void *e1, const void *e2) int __acc_cdecl_qsort le64_compare(const void *e1, const void *e2) {
{
const upx_uint64_t d1 = get_le64(e1); const upx_uint64_t d1 = get_le64(e1);
const upx_uint64_t d2 = get_le64(e2); const upx_uint64_t d2 = get_le64(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be16_compare_signed(const void *e1, const void *e2) {
int __acc_cdecl_qsort be16_compare_signed(const void *e1, const void *e2)
{
const int d1 = get_be16_signed(e1); const int d1 = get_be16_signed(e1);
const int d2 = get_be16_signed(e2); const int d2 = get_be16_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be24_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort be24_compare_signed(const void *e1, const void *e2) {
{
const int d1 = get_be24_signed(e1); const int d1 = get_be24_signed(e1);
const int d2 = get_be24_signed(e2); const int d2 = get_be24_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be32_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort be32_compare_signed(const void *e1, const void *e2) {
{
const int d1 = get_be32_signed(e1); const int d1 = get_be32_signed(e1);
const int d2 = get_be32_signed(e2); const int d2 = get_be32_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort be64_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort be64_compare_signed(const void *e1, const void *e2) {
{
const upx_int64_t d1 = get_be64_signed(e1); const upx_int64_t d1 = get_be64_signed(e1);
const upx_int64_t d2 = get_be64_signed(e2); const upx_int64_t d2 = get_be64_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le16_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort le16_compare_signed(const void *e1, const void *e2) {
{
const int d1 = get_le16_signed(e1); const int d1 = get_le16_signed(e1);
const int d2 = get_le16_signed(e2); const int d2 = get_le16_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le24_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort le24_compare_signed(const void *e1, const void *e2) {
{
const int d1 = get_le24_signed(e1); const int d1 = get_le24_signed(e1);
const int d2 = get_le24_signed(e2); const int d2 = get_le24_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le32_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort le32_compare_signed(const void *e1, const void *e2) {
{
const int d1 = get_le32_signed(e1); const int d1 = get_le32_signed(e1);
const int d2 = get_le32_signed(e2); const int d2 = get_le32_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
int __acc_cdecl_qsort le64_compare_signed(const void *e1, const void *e2) int __acc_cdecl_qsort le64_compare_signed(const void *e1, const void *e2) {
{
const upx_int64_t d1 = get_le64_signed(e1); const upx_int64_t d1 = get_le64_signed(e1);
const upx_int64_t d2 = get_le64_signed(e2); const upx_int64_t d2 = get_le64_signed(e2);
return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
} }
/************************************************************************* /*************************************************************************
// find util // find util
**************************************************************************/ **************************************************************************/
int find(const void *b, int blen, const void *what, int wlen) int find(const void *b, int blen, const void *what, int wlen) {
{
if (b == NULL || blen <= 0 || what == NULL || wlen <= 0) if (b == NULL || blen <= 0 || what == NULL || wlen <= 0)
return -1; return -1;
int i; int i;
const unsigned char *base = (const unsigned char *) b; const unsigned char *base = (const unsigned char *) b;
unsigned char firstc = * (const unsigned char *) what; unsigned char firstc = *(const unsigned char *) what;
blen -= wlen; blen -= wlen;
for (i = 0; i <= blen; i++, base++) for (i = 0; i <= blen; i++, base++)
@ -266,63 +249,48 @@ int find(const void *b, int blen, const void *what, int wlen)
return -1; return -1;
} }
int find_be16(const void *b, int blen, unsigned what) {
int find_be16(const void *b, int blen, unsigned what)
{
unsigned char w[2]; unsigned char w[2];
set_be16(w, what); set_be16(w, what);
return find(b, blen, w, 2); return find(b, blen, w, 2);
} }
int find_be32(const void *b, int blen, unsigned what) {
int find_be32(const void *b, int blen, unsigned what)
{
unsigned char w[4]; unsigned char w[4];
set_be32(w, what); set_be32(w, what);
return find(b, blen, w, 4); return find(b, blen, w, 4);
} }
int find_be64(const void *b, int blen, upx_uint64_t what) {
int find_be64(const void *b, int blen, upx_uint64_t what)
{
unsigned char w[8]; unsigned char w[8];
set_be64(w, what); set_be64(w, what);
return find(b, blen, w, 8); return find(b, blen, w, 8);
} }
int find_le16(const void *b, int blen, unsigned what) {
int find_le16(const void *b, int blen, unsigned what)
{
unsigned char w[2]; unsigned char w[2];
set_le16(w, what); set_le16(w, what);
return find(b, blen, w, 2); return find(b, blen, w, 2);
} }
int find_le32(const void *b, int blen, unsigned what) {
int find_le32(const void *b, int blen, unsigned what)
{
unsigned char w[4]; unsigned char w[4];
set_le32(w, what); set_le32(w, what);
return find(b, blen, w, 4); return find(b, blen, w, 4);
} }
int find_le64(const void *b, int blen, upx_uint64_t what) {
int find_le64(const void *b, int blen, upx_uint64_t what)
{
unsigned char w[8]; unsigned char w[8];
set_le64(w, what); set_le64(w, what);
return find(b, blen, w, 8); return find(b, blen, w, 8);
} }
int mem_replace(void *bb, int blen, const void *what, int wlen, const void *r) {
int mem_replace(void *bb, int blen, const void *what, int wlen, const void *r)
{
unsigned char *b = (unsigned char *) bb; unsigned char *b = (unsigned char *) bb;
int boff = 0; int boff = 0;
int n = 0; int n = 0;
while (blen - boff >= wlen) while (blen - boff >= wlen) {
{
int off = find(b + boff, blen - boff, what, wlen); int off = find(b + boff, blen - boff, what, wlen);
if (off < 0) if (off < 0)
break; break;
@ -334,32 +302,30 @@ int mem_replace(void *bb, int blen, const void *what, int wlen, const void *r)
return n; return n;
} }
/************************************************************************* /*************************************************************************
// fn - FileName util // fn - FileName util
**************************************************************************/ **************************************************************************/
#if (ACC_OS_CYGWIN || ACC_OS_DOS16 || ACC_OS_DOS32 || ACC_OS_EMX || ACC_OS_OS2 || ACC_OS_OS16 || ACC_OS_TOS || ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64) #if (ACC_OS_CYGWIN || ACC_OS_DOS16 || ACC_OS_DOS32 || ACC_OS_EMX || ACC_OS_OS2 || ACC_OS_OS16 || \
ACC_OS_TOS || ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64)
static const char dir_sep[] = "/\\"; static const char dir_sep[] = "/\\";
#define fn_is_drive(s) (s[0] && s[1] == ':') #define fn_is_drive(s) (s[0] && s[1] == ':')
#define fn_is_sep(c) (strchr(dir_sep,c) != NULL) #define fn_is_sep(c) (strchr(dir_sep, c) != NULL)
#define fn_skip_drive(s) (fn_is_drive(s) ? (s) + 2 : (s)) #define fn_skip_drive(s) (fn_is_drive(s) ? (s) + 2 : (s))
#define fn_tolower(c) (tolower(((unsigned char)(c)))) #define fn_tolower(c) (tolower(((unsigned char) (c))))
#else #else
//static const char dir_sep[] = "/"; // static const char dir_sep[] = "/";
#define fn_is_drive(s) (0) #define fn_is_drive(s) (0)
#define fn_is_sep(c) ((c) == '/') #define fn_is_sep(c) ((c) == '/')
#define fn_skip_drive(s) (s) #define fn_skip_drive(s) (s)
#define fn_tolower(c) (c) #define fn_tolower(c) (c)
#endif #endif
char *fn_basename(const char *name) {
char *fn_basename(const char *name)
{
const char *n, *nn; const char *n, *nn;
name = fn_skip_drive(name); name = fn_skip_drive(name);
@ -369,9 +335,7 @@ char *fn_basename(const char *name)
return ACC_UNCONST_CAST(char *, n); return ACC_UNCONST_CAST(char *, n);
} }
bool fn_has_ext(const char *name, const char *ext, bool ignore_case) {
bool fn_has_ext(const char *name, const char *ext, bool ignore_case)
{
const char *n, *e; const char *n, *e;
name = fn_basename(name); name = fn_basename(name);
@ -379,44 +343,37 @@ bool fn_has_ext(const char *name, const char *ext, bool ignore_case)
if (*n == '.') if (*n == '.')
e = n; e = n;
if (ignore_case) if (ignore_case)
return (strcasecmp(ext,e+1) == 0); return (strcasecmp(ext, e + 1) == 0);
else else
return (fn_strcmp(ext,e+1) == 0); return (fn_strcmp(ext, e + 1) == 0);
} }
char *fn_strlwr(char *n) {
char *fn_strlwr(char *n)
{
char *p; char *p;
for (p = n; *p; p++) for (p = n; *p; p++)
*p = (char) fn_tolower(*p); *p = (char) fn_tolower(*p);
return n; return n;
} }
int fn_strcmp(const char *n1, const char *n2) {
int fn_strcmp(const char *n1, const char *n2) for (;;) {
{ if (*n1 != *n2) {
for (;;)
{
if (*n1 != *n2)
{
int c = fn_tolower(*n1) - fn_tolower(*n2); int c = fn_tolower(*n1) - fn_tolower(*n2);
if (c) if (c)
return c; return c;
} }
if (*n1 == 0) if (*n1 == 0)
return 0; return 0;
n1++; n2++; n1++;
n2++;
} }
} }
/************************************************************************* /*************************************************************************
// misc. // misc.
**************************************************************************/ **************************************************************************/
bool set_method_name(char *buf, size_t size, int method, int level) bool set_method_name(char *buf, size_t size, int method, int level) {
{
bool r = true; bool r = true;
const char *alg; const char *alg;
if (M_IS_NRV2B(method)) if (M_IS_NRV2B(method))
@ -427,8 +384,7 @@ bool set_method_name(char *buf, size_t size, int method, int level)
alg = "NRV2E"; alg = "NRV2E";
else if (M_IS_LZMA(method)) else if (M_IS_LZMA(method))
alg = "LZMA"; alg = "LZMA";
else else {
{
alg = "???"; alg = "???";
r = false; r = false;
} }
@ -439,41 +395,36 @@ bool set_method_name(char *buf, size_t size, int method, int level)
return r; return r;
} }
void center_string(char *buf, size_t size, const char *s) {
void center_string(char *buf, size_t size, const char *s)
{
size_t l1 = size - 1; size_t l1 = size - 1;
size_t l2 = strlen(s); size_t l2 = strlen(s);
assert(size > 0); assert(size > 0);
assert(l2 < size); assert(l2 < size);
memset(buf, ' ', l1); memset(buf, ' ', l1);
memcpy(buf+(l1-l2)/2, s, l2); memcpy(buf + (l1 - l2) / 2, s, l2);
buf[l1] = 0; buf[l1] = 0;
} }
bool file_exists(const char *name) {
bool file_exists(const char *name)
{
int fd, r; int fd, r;
struct stat st; struct stat st;
/* return true if we can open it */ /* return true if we can open it */
fd = open(name, O_RDONLY | O_BINARY, 0); fd = open(name, O_RDONLY | O_BINARY, 0);
if (fd >= 0) if (fd >= 0) {
{
(void) close(fd); (void) close(fd);
return true; return true;
} }
/* return true if we can stat it */ /* return true if we can stat it */
//memset(&st, 0, sizeof(st)); // memset(&st, 0, sizeof(st));
r = stat(name, &st); r = stat(name, &st);
if (r != -1) if (r != -1)
return true; return true;
/* return true if we can lstat it */ /* return true if we can lstat it */
#if (HAVE_LSTAT) #if (HAVE_LSTAT)
//memset(&st, 0, sizeof(st)); // memset(&st, 0, sizeof(st));
r = lstat(name, &st); r = lstat(name, &st);
if (r != -1) if (r != -1)
return true; return true;
@ -482,10 +433,8 @@ bool file_exists(const char *name)
return false; return false;
} }
bool maketempname(char *ofilename, size_t size, const char *ifilename, const char *ext,
bool maketempname(char *ofilename, size_t size, bool force) {
const char *ifilename, const char *ext, bool force)
{
char *ofext = NULL, *ofname; char *ofext = NULL, *ofname;
int ofile; int ofile;
@ -493,8 +442,7 @@ bool maketempname(char *ofilename, size_t size,
return false; return false;
strcpy(ofilename, ifilename); strcpy(ofilename, ifilename);
for (ofname = fn_basename(ofilename); *ofname; ofname++) for (ofname = fn_basename(ofilename); *ofname; ofname++) {
{
if (*ofname == '.') if (*ofname == '.')
ofext = ofname; ofext = ofname;
} }
@ -502,8 +450,7 @@ bool maketempname(char *ofilename, size_t size,
ofext = ofilename + strlen(ofilename); ofext = ofilename + strlen(ofilename);
strcpy(ofext, ext); strcpy(ofext, ext);
for (ofile = 0; ofile < 1000; ofile++) for (ofile = 0; ofile < 1000; ofile++) {
{
assert(strlen(ofilename) < size); assert(strlen(ofilename) < size);
if (!file_exists(ofilename)) if (!file_exists(ofilename))
return true; return true;
@ -516,10 +463,7 @@ bool maketempname(char *ofilename, size_t size,
return false; return false;
} }
bool makebakname(char *ofilename, size_t size, const char *ifilename, bool force) {
bool makebakname(char *ofilename, size_t size,
const char *ifilename, bool force)
{
char *ofext = NULL, *ofname; char *ofext = NULL, *ofname;
int ofile; int ofile;
@ -527,23 +471,19 @@ bool makebakname(char *ofilename, size_t size,
return false; return false;
strcpy(ofilename, ifilename); strcpy(ofilename, ifilename);
for (ofname = fn_basename(ofilename); *ofname; ofname++) for (ofname = fn_basename(ofilename); *ofname; ofname++) {
{
if (*ofname == '.') if (*ofname == '.')
ofext = ofname; ofext = ofname;
} }
if (ofext == NULL) if (ofext == NULL) {
{
ofext = ofilename + strlen(ofilename); ofext = ofilename + strlen(ofilename);
strcpy(ofext, ".~"); strcpy(ofext, ".~");
} } else if (strlen(ofext) < 1 + 3)
else if (strlen(ofext) < 1 + 3)
strcat(ofilename, "~"); strcat(ofilename, "~");
else else
ofext[strlen(ofext)-1] = '~'; ofext[strlen(ofext) - 1] = '~';
for (ofile = 0; ofile < 1000; ofile++) for (ofile = 0; ofile < 1000; ofile++) {
{
assert(strlen(ofilename) < size); assert(strlen(ofilename) < size);
if (!file_exists(ofilename)) if (!file_exists(ofilename))
return true; return true;
@ -556,26 +496,23 @@ bool makebakname(char *ofilename, size_t size,
return false; return false;
} }
/************************************************************************* /*************************************************************************
// return compression ratio, where 100% == 1000*1000 == 1e6 // return compression ratio, where 100% == 1000*1000 == 1e6
**************************************************************************/ **************************************************************************/
unsigned get_ratio(upx_uint64_t u_len, upx_uint64_t c_len) unsigned get_ratio(upx_uint64_t u_len, upx_uint64_t c_len) {
{
const unsigned n = 1000 * 1000; const unsigned n = 1000 * 1000;
if (u_len == 0) if (u_len == 0)
return c_len == 0 ? 0 : n; return c_len == 0 ? 0 : n;
upx_uint64_t x = c_len * n; upx_uint64_t x = c_len * n;
assert(x / n == c_len); assert(x / n == c_len);
x /= u_len; x /= u_len;
x += 50; // rounding x += 50; // rounding
if (x >= 10 * n) // >= "1000%" if (x >= 10 * n) // >= "1000%"
x = 10 * n - 1; x = 10 * n - 1;
return ACC_ICONV(unsigned, x); return ACC_ICONV(unsigned, x);
} }
/************************************************************************* /*************************************************************************
// Don't link these functions from libc ==> save xxx bytes // Don't link these functions from libc ==> save xxx bytes
**************************************************************************/ **************************************************************************/
@ -584,19 +521,21 @@ extern "C" {
// FIXME - quick hack for arm-wince-gcc-3.4 (Debian pocketpc-*.deb packages) // FIXME - quick hack for arm-wince-gcc-3.4 (Debian pocketpc-*.deb packages)
#if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(__CEGCC__) && !defined(_WIN32) #if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(__CEGCC__) && !defined(_WIN32)
int dup(int fd) { UNUSED(fd); return -1; } int dup(int fd) {
UNUSED(fd);
return -1;
}
#endif #endif
#if (ACC_OS_DOS32) && defined(__DJGPP__) #if (ACC_OS_DOS32) && defined(__DJGPP__)
//int _is_executable(const char *, int, const char *) { return 0; } // int _is_executable(const char *, int, const char *) { return 0; }
// FIXME: something wants to link in ctime.o // FIXME: something wants to link in ctime.o
//time_t mktime(struct tm *) { return 0; } // time_t mktime(struct tm *) { return 0; }
//time_t time(time_t *t) { if (t) *t = 0; return 0; } // time_t time(time_t *t) { if (t) *t = 0; return 0; }
#endif #endif
} // extern "C" } // extern "C"
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */

View File

@ -25,42 +25,38 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#include "conf.h" #include "conf.h"
#include "file.h" #include "file.h"
#include "packmast.h" #include "packmast.h"
#include "packer.h" #include "packer.h"
#include "ui.h" #include "ui.h"
#if (ACC_OS_DOS32) && defined(__DJGPP__) #if (ACC_OS_DOS32) && defined(__DJGPP__)
# define USE_FTIME 1 #define USE_FTIME 1
#elif (ACC_OS_WIN32 && ACC_CC_MWERKS) && defined(__MSL__) #elif (ACC_OS_WIN32 && ACC_CC_MWERKS) && defined(__MSL__)
# include <utime.h> #include <utime.h>
# define USE_UTIME 1 #define USE_UTIME 1
#elif ((ACC_OS_WIN32 || ACC_OS_WIN64) && (ACC_CC_INTELC || ACC_CC_MSC)) #elif ((ACC_OS_WIN32 || ACC_OS_WIN64) && (ACC_CC_INTELC || ACC_CC_MSC))
# define USE__FUTIME 1 #define USE__FUTIME 1
#elif (HAVE_UTIME) #elif (HAVE_UTIME)
# define USE_UTIME 1 #define USE_UTIME 1
#endif #endif
#if !defined(SH_DENYRW) #if !defined(SH_DENYRW)
# define SH_DENYRW (-1) #define SH_DENYRW (-1)
#endif #endif
#if !defined(SH_DENYWR) #if !defined(SH_DENYWR)
# define SH_DENYWR (-1) #define SH_DENYWR (-1)
#endif #endif
// ignore errors in some cases and silence __attribute__((__warn_unused_result__)) // ignore errors in some cases and silence __attribute__((__warn_unused_result__))
#define IGNORE_ERROR(var) ACC_UNUSED(var) #define IGNORE_ERROR(var) ACC_UNUSED(var)
/************************************************************************* /*************************************************************************
// process one file // process one file
**************************************************************************/ **************************************************************************/
void do_one_file(const char *iname, char *oname) void do_one_file(const char *iname, char *oname) {
{
int r; int r;
struct stat st; struct stat st;
memset(&st, 0, sizeof(st)); memset(&st, 0, sizeof(st));
@ -69,8 +65,7 @@ void do_one_file(const char *iname, char *oname)
#else #else
r = stat(iname, &st); r = stat(iname, &st);
#endif #endif
if (r != 0) if (r != 0) {
{
if (errno == ENOENT) if (errno == ENOENT)
throw FileNotFoundException(iname, errno); throw FileNotFoundException(iname, errno);
else else
@ -89,8 +84,7 @@ void do_one_file(const char *iname, char *oname)
throwIOException("file is too small -- skipped"); throwIOException("file is too small -- skipped");
if (!mem_size_valid_bytes(st.st_size)) if (!mem_size_valid_bytes(st.st_size))
throwIOException("file is too large -- skipped"); throwIOException("file is too large -- skipped");
if ((st.st_mode & S_IWUSR) == 0) if ((st.st_mode & S_IWUSR) == 0) {
{
bool skip = true; bool skip = true;
if (opt->output_name) if (opt->output_name)
skip = false; skip = false;
@ -109,8 +103,7 @@ void do_one_file(const char *iname, char *oname)
#if (USE_FTIME) #if (USE_FTIME)
struct ftime fi_ftime; struct ftime fi_ftime;
memset(&fi_ftime, 0, sizeof(fi_ftime)); memset(&fi_ftime, 0, sizeof(fi_ftime));
if (opt->preserve_timestamp) if (opt->preserve_timestamp) {
{
if (getftime(fi.getFd(), &fi_ftime) != 0) if (getftime(fi.getFd(), &fi_ftime) != 0)
throwIOException("cannot determine file timestamp"); throwIOException("cannot determine file timestamp");
} }
@ -118,25 +111,19 @@ void do_one_file(const char *iname, char *oname)
// open output file // open output file
OutputFile fo; OutputFile fo;
if (opt->cmd == CMD_COMPRESS || opt->cmd == CMD_DECOMPRESS) if (opt->cmd == CMD_COMPRESS || opt->cmd == CMD_DECOMPRESS) {
{ if (opt->to_stdout) {
if (opt->to_stdout)
{
if (!fo.openStdout(1, opt->force ? true : false)) if (!fo.openStdout(1, opt->force ? true : false))
throwIOException("data not written to a terminal; Use '-f' to force."); throwIOException("data not written to a terminal; Use '-f' to force.");
} } else {
else char tname[ACC_FN_PATH_MAX + 1];
{
char tname[ACC_FN_PATH_MAX+1];
if (opt->output_name) if (opt->output_name)
strcpy(tname,opt->output_name); strcpy(tname, opt->output_name);
else else {
{
if (!maketempname(tname, sizeof(tname), iname, ".upx")) if (!maketempname(tname, sizeof(tname), iname, ".upx"))
throwIOException("could not create a temporary file name"); throwIOException("could not create a temporary file name");
} }
if (opt->force >= 2) if (opt->force >= 2) {
{
#if (HAVE_CHMOD) #if (HAVE_CHMOD)
r = chmod(tname, 0777); r = chmod(tname, 0777);
IGNORE_ERROR(r); IGNORE_ERROR(r);
@ -155,13 +142,13 @@ void do_one_file(const char *iname, char *oname)
shmode = O_DENYRW; shmode = O_DENYRW;
#endif #endif
// cannot rely on open() because of umask // cannot rely on open() because of umask
//int omode = st.st_mode | 0600; // int omode = st.st_mode | 0600;
int omode = 0600; int omode = 0600;
if (!opt->preserve_mode) if (!opt->preserve_mode)
omode = 0666; omode = 0666;
fo.sopen(tname,flags,shmode,omode); fo.sopen(tname, flags, shmode, omode);
// open succeeded - now set oname[] // open succeeded - now set oname[]
strcpy(oname,tname); strcpy(oname, tname);
} }
} }
@ -181,8 +168,7 @@ void do_one_file(const char *iname, char *oname)
throwInternalError("invalid command"); throwInternalError("invalid command");
// copy time stamp // copy time stamp
if (opt->preserve_timestamp && oname[0] && fo.isOpen()) if (opt->preserve_timestamp && oname[0] && fo.isOpen()) {
{
#if (USE_FTIME) #if (USE_FTIME)
r = setftime(fo.getFd(), &fi_ftime); r = setftime(fo.getFd(), &fi_ftime);
IGNORE_ERROR(r); IGNORE_ERROR(r);
@ -200,38 +186,32 @@ void do_one_file(const char *iname, char *oname)
fi.closex(); fi.closex();
// rename or delete files // rename or delete files
if (oname[0] && !opt->output_name) if (oname[0] && !opt->output_name) {
{
// FIXME: .exe or .cof etc. // FIXME: .exe or .cof etc.
if (!opt->backup) if (!opt->backup) {
{
#if (HAVE_CHMOD) #if (HAVE_CHMOD)
r = chmod(iname, 0777); r = chmod(iname, 0777);
IGNORE_ERROR(r); IGNORE_ERROR(r);
#endif #endif
File::unlink(iname); File::unlink(iname);
} } else {
else
{
// make backup // make backup
char bakname[ACC_FN_PATH_MAX+1]; char bakname[ACC_FN_PATH_MAX + 1];
if (!makebakname(bakname, sizeof(bakname), iname)) if (!makebakname(bakname, sizeof(bakname), iname))
throwIOException("could not create a backup file name"); throwIOException("could not create a backup file name");
File::rename(iname,bakname); File::rename(iname, bakname);
} }
File::rename(oname,iname); File::rename(oname, iname);
} }
// copy file attributes // copy file attributes
if (oname[0]) if (oname[0]) {
{
oname[0] = 0; oname[0] = 0;
const char *name = opt->output_name ? opt->output_name : iname; const char *name = opt->output_name ? opt->output_name : iname;
UNUSED(name); UNUSED(name);
#if (USE_UTIME) #if (USE_UTIME)
// copy time stamp // copy time stamp
if (opt->preserve_timestamp) if (opt->preserve_timestamp) {
{
struct utimbuf u; struct utimbuf u;
u.actime = st.st_atime; u.actime = st.st_atime;
u.modtime = st.st_mtime; u.modtime = st.st_mtime;
@ -241,16 +221,14 @@ void do_one_file(const char *iname, char *oname)
#endif #endif
#if (HAVE_CHMOD) #if (HAVE_CHMOD)
// copy permissions // copy permissions
if (opt->preserve_mode) if (opt->preserve_mode) {
{
r = chmod(name, st.st_mode); r = chmod(name, st.st_mode);
IGNORE_ERROR(r); IGNORE_ERROR(r);
} }
#endif #endif
#if (HAVE_CHOWN) #if (HAVE_CHOWN)
// copy the ownership // copy the ownership
if (opt->preserve_ownership) if (opt->preserve_ownership) {
{
r = chown(name, st.st_uid, st.st_gid); r = chown(name, st.st_uid, st.st_gid);
IGNORE_ERROR(r); IGNORE_ERROR(r);
} }
@ -260,15 +238,12 @@ void do_one_file(const char *iname, char *oname)
UiPacker::uiConfirmUpdate(); UiPacker::uiConfirmUpdate();
} }
/************************************************************************* /*************************************************************************
// process all files from the commandline // process all files from the commandline
**************************************************************************/ **************************************************************************/
static void unlink_ofile(char *oname) static void unlink_ofile(char *oname) {
{ if (oname && oname[0]) {
if (oname && oname[0])
{
#if (HAVE_CHMOD) #if (HAVE_CHMOD)
int r; int r;
r = chmod(oname, 0777); r = chmod(oname, 0777);
@ -279,56 +254,52 @@ static void unlink_ofile(char *oname)
} }
} }
void do_files(int i, int argc, char *argv[]) {
void do_files(int i, int argc, char *argv[]) if (opt->verbose >= 1) {
{
if (opt->verbose >= 1)
{
show_head(); show_head();
UiPacker::uiHeader(); UiPacker::uiHeader();
} }
for ( ; i < argc; i++) for (; i < argc; i++) {
{
infoHeader(); infoHeader();
const char *iname = argv[i]; const char *iname = argv[i];
char oname[ACC_FN_PATH_MAX+1]; char oname[ACC_FN_PATH_MAX + 1];
oname[0] = 0; oname[0] = 0;
try { try {
do_one_file(iname,oname); do_one_file(iname, oname);
} catch (const Exception &e) { } catch (const Exception &e) {
unlink_ofile(oname); unlink_ofile(oname);
if (opt->verbose >= 1 || (opt->verbose >= 0 && !e.isWarning())) if (opt->verbose >= 1 || (opt->verbose >= 0 && !e.isWarning()))
printErr(iname,&e); printErr(iname, &e);
set_exit_code(e.isWarning() ? EXIT_WARN : EXIT_ERROR); set_exit_code(e.isWarning() ? EXIT_WARN : EXIT_ERROR);
} catch (const Error &e) { } catch (const Error &e) {
unlink_ofile(oname); unlink_ofile(oname);
printErr(iname,&e); printErr(iname, &e);
e_exit(EXIT_ERROR); e_exit(EXIT_ERROR);
} catch (std::bad_alloc *e) { } catch (std::bad_alloc *e) {
unlink_ofile(oname); unlink_ofile(oname);
printErr(iname,"out of memory"); printErr(iname, "out of memory");
UNUSED(e); UNUSED(e);
//delete e; // delete e;
e_exit(EXIT_ERROR); e_exit(EXIT_ERROR);
} catch (const std::bad_alloc &) { } catch (const std::bad_alloc &) {
unlink_ofile(oname); unlink_ofile(oname);
printErr(iname,"out of memory"); printErr(iname, "out of memory");
e_exit(EXIT_ERROR); e_exit(EXIT_ERROR);
} catch (std::exception *e) { } catch (std::exception *e) {
unlink_ofile(oname); unlink_ofile(oname);
printUnhandledException(iname,e); printUnhandledException(iname, e);
//delete e; // delete e;
e_exit(EXIT_ERROR); e_exit(EXIT_ERROR);
} catch (const std::exception &e) { } catch (const std::exception &e) {
unlink_ofile(oname); unlink_ofile(oname);
printUnhandledException(iname,&e); printUnhandledException(iname, &e);
e_exit(EXIT_ERROR); e_exit(EXIT_ERROR);
} catch (...) { } catch (...) {
unlink_ofile(oname); unlink_ofile(oname);
printUnhandledException(iname,NULL); printUnhandledException(iname, NULL);
e_exit(EXIT_ERROR); e_exit(EXIT_ERROR);
} }
} }