Updated bin2h.py.

This commit is contained in:
Markus F.X.J. Oberhumer 2006-11-23 02:23:39 +01:00
parent 29c099fe56
commit 68f54a3247
39 changed files with 150 additions and 92 deletions

View File

@ -161,9 +161,8 @@ endef
# default tools
tc.default.bin2h = python $(top_srcdir)/src/stub/scripts/bin2h.py --ident=auto-stub
##tc.default.bin2h-c = $(call tc,bin2h) --compress=upx-stub-deflate
##tc.default.bin2h-c = $(call tc,bin2h) --compress=upx-stub-lzma
tc.default.bin2h-c = $(call tc,bin2h) --compress=none
##tc.default.bin2h-c = $(call tc,bin2h) --compress=14,15,0
tc.default.bin2h-c = $(call tc,bin2h) --compress=0
tc.default.brandelf = python $(top_srcdir)/src/stub/scripts/brandelf.py $(if $(tc_bfdname),--bfdname=$(tc_bfdname))
tc.default.gpp_inc = python $(top_srcdir)/src/stub/scripts/gpp_inc.py
tc.default.gpp_mkdep = python $(top_srcdir)/src/stub/scripts/gpp_inc.py -o /dev/null

View File

@ -1,4 +1,5 @@
/* amd64-linux.elf-entry.h -- created from amd64-linux.elf-entry.bin, 12687 (0x318f) bytes
/* amd64-linux.elf-entry.h
created from amd64-linux.elf-entry.bin, 12687 (0x318f) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* amd64-linux.elf-fold.h -- created from amd64-linux.elf-fold.bin, 1775 (0x6ef) bytes
/* amd64-linux.elf-fold.h
created from amd64-linux.elf-fold.bin, 1775 (0x6ef) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* arm-linux.elf-entry.h -- created from arm-linux.elf-entry.bin, 19530 (0x4c4a) bytes
/* arm-linux.elf-entry.h
created from arm-linux.elf-entry.bin, 19530 (0x4c4a) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* arm-linux.elf-fold.h -- created from arm-linux.elf-fold.bin, 2108 (0x83c) bytes
/* arm-linux.elf-fold.h
created from arm-linux.elf-fold.bin, 2108 (0x83c) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* arm.v4a-wince.pe.h -- created from arm.v4a-wince.pe.bin, 19616 (0x4ca0) bytes
/* arm.v4a-wince.pe.h
created from arm.v4a-wince.pe.bin, 19616 (0x4ca0) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* arm.v4t-wince.pe.h -- created from arm.v4t-wince.pe.bin, 9992 (0x2708) bytes
/* arm.v4t-wince.pe.h
created from arm.v4t-wince.pe.bin, 9992 (0x2708) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* armeb-linux.elf-entry.h -- created from armeb-linux.elf-entry.bin, 19529 (0x4c49) bytes
/* armeb-linux.elf-entry.h
created from armeb-linux.elf-entry.bin, 19529 (0x4c49) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* armeb-linux.elf-fold.h -- created from armeb-linux.elf-fold.bin, 2108 (0x83c) bytes
/* armeb-linux.elf-fold.h
created from armeb-linux.elf-fold.bin, 2108 (0x83c) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i086-dos16.com.h -- created from i086-dos16.com.bin, 9078 (0x2376) bytes
/* i086-dos16.com.h
created from i086-dos16.com.bin, 9078 (0x2376) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i086-dos16.exe.h -- created from i086-dos16.exe.bin, 19030 (0x4a56) bytes
/* i086-dos16.exe.h
created from i086-dos16.exe.bin, 19030 (0x4a56) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i086-dos16.sys.h -- created from i086-dos16.sys.bin, 10474 (0x28ea) bytes
/* i086-dos16.sys.h
created from i086-dos16.sys.bin, 10474 (0x28ea) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-bsd.elf-entry.h -- created from i386-bsd.elf-entry.bin, 52750 (0xce0e) bytes
/* i386-bsd.elf-entry.h
created from i386-bsd.elf-entry.bin, 52750 (0xce0e) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-bsd.elf-fold.h -- created from i386-bsd.elf-fold.bin, 1789 (0x6fd) bytes
/* i386-bsd.elf-fold.h
created from i386-bsd.elf-fold.bin, 1789 (0x6fd) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-bsd.elf.execve-entry.h -- created from i386-bsd.elf.execve-entry.bin, 53357 (0xd06d) bytes
/* i386-bsd.elf.execve-entry.h
created from i386-bsd.elf.execve-entry.bin, 53357 (0xd06d) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-bsd.elf.execve-fold.h -- created from i386-bsd.elf.execve-fold.bin, 1031 (0x407) bytes
/* i386-bsd.elf.execve-fold.h
created from i386-bsd.elf.execve-fold.bin, 1031 (0x407) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-dos32.djgpp2.h -- created from i386-dos32.djgpp2.bin, 33548 (0x830c) bytes
/* i386-dos32.djgpp2.h
created from i386-dos32.djgpp2.bin, 33548 (0x830c) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-dos32.tmt.h -- created from i386-dos32.tmt.bin, 34553 (0x86f9) bytes
/* i386-dos32.tmt.h
created from i386-dos32.tmt.bin, 34553 (0x86f9) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-dos32.watcom.le.h -- created from i386-dos32.watcom.le.bin, 35309 (0x89ed) bytes
/* i386-dos32.watcom.le.h
created from i386-dos32.watcom.le.bin, 35309 (0x89ed) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf-entry.h -- created from i386-linux.elf-entry.bin, 52744 (0xce08) bytes
/* i386-linux.elf-entry.h
created from i386-linux.elf-entry.bin, 52744 (0xce08) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf-fold.h -- created from i386-linux.elf-fold.bin, 1691 (0x69b) bytes
/* i386-linux.elf-fold.h
created from i386-linux.elf-fold.bin, 1691 (0x69b) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf.execve-entry.h -- created from i386-linux.elf.execve-entry.bin, 53359 (0xd06f) bytes
/* i386-linux.elf.execve-entry.h
created from i386-linux.elf.execve-entry.bin, 53359 (0xd06f) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf.execve-fold.h -- created from i386-linux.elf.execve-fold.bin, 931 (0x3a3) bytes
/* i386-linux.elf.execve-fold.h
created from i386-linux.elf.execve-fold.bin, 931 (0x3a3) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf.interp-entry.h -- created from i386-linux.elf.interp-entry.bin, 64126 (0xfa7e) bytes
/* i386-linux.elf.interp-entry.h
created from i386-linux.elf.interp-entry.bin, 64126 (0xfa7e) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf.interp-fold.h -- created from i386-linux.elf.interp-fold.bin, 1519 (0x5ef) bytes
/* i386-linux.elf.interp-fold.h
created from i386-linux.elf.interp-fold.bin, 1519 (0x5ef) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf.shell-entry.h -- created from i386-linux.elf.shell-entry.bin, 41479 (0xa207) bytes
/* i386-linux.elf.shell-entry.h
created from i386-linux.elf.shell-entry.bin, 41479 (0xa207) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.elf.shell-fold.h -- created from i386-linux.elf.shell-fold.bin, 1194 (0x4aa) bytes
/* i386-linux.elf.shell-fold.h
created from i386-linux.elf.shell-fold.bin, 1194 (0x4aa) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.kernel.vmlinux-head.h -- created from i386-linux.kernel.vmlinux-head.bin, 39 (0x27) bytes
/* i386-linux.kernel.vmlinux-head.h
created from i386-linux.kernel.vmlinux-head.bin, 39 (0x27) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.kernel.vmlinux.h -- created from i386-linux.kernel.vmlinux.bin, 34465 (0x86a1) bytes
/* i386-linux.kernel.vmlinux.h
created from i386-linux.kernel.vmlinux.bin, 34465 (0x86a1) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-linux.kernel.vmlinuz.h -- created from i386-linux.kernel.vmlinuz.bin, 35988 (0x8c94) bytes
/* i386-linux.kernel.vmlinuz.h
created from i386-linux.kernel.vmlinuz.bin, 35988 (0x8c94) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-openbsd.elf-fold.h -- created from i386-openbsd.elf-fold.bin, 2025 (0x7e9) bytes
/* i386-openbsd.elf-fold.h
created from i386-openbsd.elf-fold.bin, 2025 (0x7e9) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* i386-win32.pe.h -- created from i386-win32.pe.bin, 42482 (0xa5f2) bytes
/* i386-win32.pe.h
created from i386-win32.pe.bin, 42482 (0xa5f2) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* m68k-atari.tos.h -- created from m68k-atari.tos.bin, 25878 (0x6516) bytes
/* m68k-atari.tos.h
created from m68k-atari.tos.bin, 25878 (0x6516) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* mipsel.r3000-ps1.h -- created from mipsel.r3000-ps1.bin, 26795 (0x68ab) bytes
/* mipsel.r3000-ps1.h
created from mipsel.r3000-ps1.bin, 26795 (0x68ab) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* powerpc-darwin.macho-entry.h -- created from powerpc-darwin.macho-entry.bin, 9698 (0x25e2) bytes
/* powerpc-darwin.macho-entry.h
created from powerpc-darwin.macho-entry.bin, 9698 (0x25e2) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* powerpc-darwin.macho-fold.h -- created from powerpc-darwin.macho-fold.bin, 1660 (0x67c) bytes
/* powerpc-darwin.macho-fold.h
created from powerpc-darwin.macho-fold.bin, 1660 (0x67c) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* powerpc-linux.elf-entry.h -- created from powerpc-linux.elf-entry.bin, 10127 (0x278f) bytes
/* powerpc-linux.elf-entry.h
created from powerpc-linux.elf-entry.bin, 10127 (0x278f) bytes
This file is part of the UPX executable compressor.

View File

@ -1,4 +1,5 @@
/* powerpc-linux.elf-fold.h -- created from powerpc-linux.elf-fold.bin, 1944 (0x798) bytes
/* powerpc-linux.elf-fold.h
created from powerpc-linux.elf-fold.bin, 1944 (0x798) bytes
This file is part of the UPX executable compressor.

View File

@ -32,9 +32,10 @@ import getopt, os, re, struct, sys, zlib
class opts:
compress = None
dry_run = 0
ident = None
methods = [ 0 ]
mname = "STUB_COMPRESS_METHOD"
mode = "c"
verbose = 0
@ -44,7 +45,7 @@ class opts:
# ************************************************************************/
def w_header_c(w, ifile, ofile, n):
w("/* %s -- created from %s, %d (0x%x) bytes\n" % (os.path.basename(ofile), os.path.basename(ifile), n, n))
w("/* %s\n created from %s, %d (0x%x) bytes\n" % (os.path.basename(ofile), os.path.basename(ifile), n, n))
w("""\n\
This file is part of the UPX executable compressor.
@ -153,10 +154,74 @@ def encode_compressed_stub_header(method, idata, odata):
else:
h = "UPX#" + "\x00" + struct.pack("<BII", method, len(idata), len(odata))
assert len(h) == 14
assert len(h) + len(odata) < len(idata), "compression failed"
##assert len(h) + len(odata) < len(idata), ("stub compression failed", len(h), len(odata), len(idata))
return h
# /***********************************************************************
# //
# ************************************************************************/
def write_stub(w, idata, method_index):
method = opts.methods[method_index]
# compress
if method == 0:
odata = idata
elif method == 14: # M_LZMA:
import pylzma
odata = pylzma.compress(idata, eos=0)
## FIXME: internal pylzma-0.3.0 error
##assert pylzma.decompress(odata, maxlength=len(idata)) == idata
# recode lzma-header
prop = ord(odata[0])
pb = (prop / 9) / 5; lp = (prop / 9) % 5; lc = prop % 9
h = chr(((lc + lp) << 3) | pb) + chr((lp << 4) | lc)
odata = h + odata[5:]
# encode upx stub header
odata = encode_compressed_stub_header(method, idata, odata) + odata
elif method == 15: # M_DEFLATE:
odata = zlib.compress(idata, 9)
# strip zlib-header and zlib-trailer (adler32)
odata = odata[2:-4]
assert zlib.decompress(odata, -15) == idata
# encode upx stub header
odata = encode_compressed_stub_header(method, idata, odata) + odata
else:
raise Exception, ("invalid method", method, opts.methods)
if 0 and len(odata) >= len(idata):
odata = idata
method = 0
assert len(odata) <= len(idata), "stub compression failed"
if len(opts.methods) > 1:
if method_index == 0:
w("#if (%s == %d)\n\n" % (opts.mname, method))
elif method_index < len(opts.methods) - 1:
w("\n#elif (%s == %d)\n\n" % (opts.mname, method))
else:
w("\n#else\n\n")
if opts.ident:
if opts.mode == "c":
w_checksum_c(w, opts.ident.upper(), odata)
w("unsigned char %s[%d] = {\n" % (opts.ident, len(odata)))
if opts.mode == "c":
w_data_c(w, odata)
elif opts.mode == "gas":
w_data_gas(w, odata)
elif opts.mode == "nasm":
w_data_nasm(w, odata)
else:
assert 0, opts.mode
if opts.ident:
if opts.mode == "c":
w("};\n")
if len(opts.methods) > 1:
if method_index == len(opts.methods) - 1:
w("\n#endif\n")
# /***********************************************************************
# // main
# ************************************************************************/
@ -170,7 +235,7 @@ def main(argv):
if 0: pass
elif opt in ["-q", "--quiet"]: opts.verbose = opts.verbose - 1
elif opt in ["-v", "--verbose"]: opts.verbose = opts.verbose + 1
elif opt in ["--compress"]: opts.compress = optarg
elif opt in ["--compress"]: opts.methods = map(int, optarg.split(","))
elif opt in ["--dry-run"]: opts.dry_run = opts.dry_run + 1
elif opt in ["--ident"]: opts.ident = optarg
elif opt in ["--mode"]: opts.mode = optarg.lower()
@ -190,44 +255,13 @@ def main(argv):
sys.exit(1)
# read ifile
ifile = os.path.normpath(ifile)
ifp = open(ifile, "rb")
idata = ifp.read()
ifp.close()
assert len(idata) == st.st_size
# compress
if opts.compress in [None, "none"]:
odata = idata
elif opts.compress in ["zlib"]:
# zlib with header and adler32 checksum
odata = zlib.compress(idata, 9)
assert zlib.decompress(odata) == idata
elif opts.compress in ["upx-stub-deflate"]:
odata = zlib.compress(idata, 9)
# strip zlib-header and zlib-trailer (adler32)
odata = odata[2:-4]
assert zlib.decompress(odata, -15) == idata
# encode upx stub header
M_DEFLATE = 15
odata = encode_compressed_stub_header(M_DEFLATE, idata, odata) + odata
elif opts.compress in ["upx-stub-lzma"]:
import pylzma
odata = pylzma.compress(idata, eos=0)
## FIXME: internal pylzma-0.3.0 error
##assert pylzma.decompress(odata, maxlength=len(idata)) == idata
# strip lzma-header
prop = ord(odata[0])
pb = (prop / 9) / 5; lp = (prop / 9) % 5; lc = prop % 9
h = chr(((lc + lp) << 3) | pb) + chr((lp << 4) | lc)
odata = h + odata[5:]
# encode upx stub header
M_LZMA = 14
odata = encode_compressed_stub_header(M_LZMA, idata, odata) + odata
else:
raise Exception, ("invalid --compress=", opts.compress)
assert len(odata) <= len(idata), "compression failed"
# ident
# opts.ident
if opts.ident in ["auto", "auto-stub"]:
s = os.path.basename(ifile)
s = re.sub(r"\.(bin|out)$", "", s)
@ -253,21 +287,9 @@ def main(argv):
if opts.verbose >= 0:
if opts.mode == "c":
w_header_c(w, ifile, ofile, len(idata))
if opts.ident:
if opts.mode == "c":
w_checksum_c(w, opts.ident.upper(), odata)
w("unsigned char %s[%d] = {\n" % (opts.ident, len(odata)))
if opts.mode == "c":
w_data_c(w, odata)
elif opts.mode == "gas":
w_data_gas(w, odata)
elif opts.mode == "nasm":
w_data_nasm(w, odata)
else:
assert 0, opts.mode
if opts.ident:
if opts.mode == "c":
w("};\n")
assert len(opts.methods) >= 1
for i in range(len(opts.methods)):
write_stub(w, idata, i)
if ofp:
if ofp is sys.stdout:
ofp.flush()