Mixing python3 strings with bytes requires .encode and .decode
modified: stub/scripts/bin2h.py modified: stub/scripts/brandelf.py modified: stub/scripts/gpp_inc.py modified: stub/scripts/xstrip.py
This commit is contained in:
parent
a3d5a0a7b8
commit
4ec866594b
@ -45,7 +45,7 @@ class opts:
|
|||||||
# ************************************************************************/
|
# ************************************************************************/
|
||||||
|
|
||||||
def w_header_c(w, ifile, ofile, n):
|
def w_header_c(w, ifile, ofile, n):
|
||||||
w("/* %s\n 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)).encode())
|
||||||
w("""\n\
|
w("""\n\
|
||||||
This file is part of the UPX executable compressor.
|
This file is part of the UPX executable compressor.
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ def w_header_c(w, ifile, ofile, n):
|
|||||||
|
|
||||||
John F. Reiser
|
John F. Reiser
|
||||||
<jreiser@users.sourceforge.net>
|
<jreiser@users.sourceforge.net>
|
||||||
*/\n\n""")
|
*/\n\n""".encode())
|
||||||
|
|
||||||
|
|
||||||
# /***********************************************************************
|
# /***********************************************************************
|
||||||
@ -87,11 +87,11 @@ class DataWriter:
|
|||||||
self.pos = None
|
self.pos = None
|
||||||
|
|
||||||
def w_bol(self, pos):
|
def w_bol(self, pos):
|
||||||
self.w("/* 0x%04x */ " % (pos))
|
self.w(("/* 0x%04x */ " % (pos)).encode())
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
def w_eol(self, fill=""):
|
def w_eol(self, fill=""):
|
||||||
if self.pos is not None:
|
if self.pos is not None:
|
||||||
self.w(fill.rstrip() + "\n")
|
self.w((fill.rstrip() + "\n").encode())
|
||||||
|
|
||||||
|
|
||||||
class DataWriter_c(DataWriter):
|
class DataWriter_c(DataWriter):
|
||||||
@ -101,8 +101,8 @@ class DataWriter_c(DataWriter):
|
|||||||
if i & 15 == 0:
|
if i & 15 == 0:
|
||||||
self.w_eol()
|
self.w_eol()
|
||||||
self.w_bol(i)
|
self.w_bol(i)
|
||||||
w("%3d" % ord(data[i]))
|
w(("%3d" % data[i]).encode())
|
||||||
if i != n - 1: w(",")
|
if i != n - 1: w(",".encode())
|
||||||
self.w_eol()
|
self.w_eol()
|
||||||
|
|
||||||
|
|
||||||
@ -169,10 +169,10 @@ class DataWriter_nasm(DataWriter):
|
|||||||
# ************************************************************************/
|
# ************************************************************************/
|
||||||
|
|
||||||
def w_checksum_c(w, s, data):
|
def w_checksum_c(w, s, data):
|
||||||
w("#define %s_SIZE %d\n" % (s, len(data)))
|
w(("#define %s_SIZE %d\n" % (s, len(data))).encode())
|
||||||
w("#define %s_ADLER32 0x%08x\n" % (s, 0xffffffff & zlib.adler32(data)))
|
w(("#define %s_ADLER32 0x%08x\n" % (s, 0xffffffff & zlib.adler32(data))).encode())
|
||||||
w("#define %s_CRC32 0x%08x\n" % (s, 0xffffffff & zlib.crc32(data)))
|
w(("#define %s_CRC32 0x%08x\n" % (s, 0xffffffff & zlib.crc32(data))).encode())
|
||||||
w("\n")
|
w("\n".encode())
|
||||||
|
|
||||||
|
|
||||||
def write_stub(w, odata, method_index, methods):
|
def write_stub(w, odata, method_index, methods):
|
||||||
@ -194,7 +194,7 @@ def write_stub(w, odata, method_index, methods):
|
|||||||
#w('__attribute__((__section__("upx_stubs")))\n')
|
#w('__attribute__((__section__("upx_stubs")))\n')
|
||||||
#w("#endif\n")
|
#w("#endif\n")
|
||||||
w("ATTRIBUTE_FOR_STUB(%s)\n" % (opts.ident))
|
w("ATTRIBUTE_FOR_STUB(%s)\n" % (opts.ident))
|
||||||
w("unsigned char %s[%d] = {\n" % (opts.ident, len(odata)))
|
w(("unsigned char %s[%d] = {\n" % (opts.ident, len(odata))).encode())
|
||||||
if opts.mode == "c":
|
if opts.mode == "c":
|
||||||
DataWriter_c(w).w_data(odata)
|
DataWriter_c(w).w_data(odata)
|
||||||
elif opts.mode == "gas":
|
elif opts.mode == "gas":
|
||||||
@ -209,11 +209,11 @@ def write_stub(w, odata, method_index, methods):
|
|||||||
assert 0, ("invalid mode", opts.mode)
|
assert 0, ("invalid mode", opts.mode)
|
||||||
if opts.ident:
|
if opts.ident:
|
||||||
if opts.mode == "c":
|
if opts.mode == "c":
|
||||||
w("};\n")
|
w("};\n".encode())
|
||||||
|
|
||||||
if len(methods) > 1:
|
if len(methods) > 1:
|
||||||
if method_index == len(methods) - 1:
|
if method_index == len(methods) - 1:
|
||||||
w("\n#endif\n")
|
w("\n#endif\n".encode())
|
||||||
|
|
||||||
|
|
||||||
# /***********************************************************************
|
# /***********************************************************************
|
||||||
@ -321,9 +321,12 @@ def main(argv):
|
|||||||
# compress stubs
|
# compress stubs
|
||||||
# (process in reverse order so that incompressible do not get sorted first)
|
# (process in reverse order so that incompressible do not get sorted first)
|
||||||
mdata, mdata_odata = [], {}
|
mdata, mdata_odata = [], {}
|
||||||
assert len(opts.methods) >= 1
|
### FIXME assert len(opts.methods) >= 1
|
||||||
r_methods = opts.methods[:]
|
### r_methods = opts.methods[:]
|
||||||
r_methods.reverse()
|
### r_methods.reverse()
|
||||||
|
r_methods = [];
|
||||||
|
for m in opts.methods:
|
||||||
|
r_methods.insert(0, m)
|
||||||
for method in r_methods:
|
for method in r_methods:
|
||||||
method, odata = compress_stub(method, idata)
|
method, odata = compress_stub(method, idata)
|
||||||
if method in mdata_odata:
|
if method in mdata_odata:
|
||||||
@ -349,7 +352,7 @@ def main(argv):
|
|||||||
if opts.mode == "c":
|
if opts.mode == "c":
|
||||||
if opts.verbose >= 0:
|
if opts.verbose >= 0:
|
||||||
w_header_c(w, ifile, ofile, len(idata))
|
w_header_c(w, ifile, ofile, len(idata))
|
||||||
w("/* clang" + "-format" + " off */\n\n")
|
w(("/* clang" + "-format" + " off */\n\n").encode())
|
||||||
for i in range(len(mdata)):
|
for i in range(len(mdata)):
|
||||||
write_stub(w, mdata_odata[mdata[i]], i, mdata)
|
write_stub(w, mdata_odata[mdata[i]], i, mdata)
|
||||||
if ofp:
|
if ofp:
|
||||||
|
|||||||
@ -54,39 +54,42 @@ def do_file(fn):
|
|||||||
|
|
||||||
def write(s):
|
def write(s):
|
||||||
if not opts.dry_run:
|
if not opts.dry_run:
|
||||||
fp.write(s)
|
fp.write(s.encode())
|
||||||
|
|
||||||
def brand_arm(s):
|
def brand_arm(s):
|
||||||
if e_ident[4:7] != s:
|
if e_ident[4:7] != s.encode():
|
||||||
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
||||||
write("\x61") # ELFOSABI_ARM
|
write("\x61") # ELFOSABI_ARM
|
||||||
def brand_freebsd(s):
|
def brand_freebsd(s):
|
||||||
if e_ident[4:7] != s:
|
if e_ident[4:7] != s.encode():
|
||||||
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
||||||
write("\x09")
|
write("\x09")
|
||||||
def brand_linux(s):
|
def brand_linux(s):
|
||||||
if e_ident[4:7] != s:
|
print ("brand_linux ", s.encode(), "e_ident ", e_ident[4:7])
|
||||||
|
if e_ident[4:7] != s.encode():
|
||||||
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
||||||
##write("\x00Linux\x00\x00\x00")
|
##write("\x00Linux\x00\x00\x00")
|
||||||
write("\x00" * 9)
|
write("\x00" * 9)
|
||||||
def brand_netbsd(s):
|
def brand_netbsd(s):
|
||||||
if e_ident[4:7] != s:
|
if e_ident[4:7] != s.encode():
|
||||||
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
||||||
write("\x02")
|
write("\x02")
|
||||||
def brand_openbsd(s):
|
def brand_openbsd(s):
|
||||||
if e_ident[4:7] != s:
|
if e_ident[4:7] != s.encode():
|
||||||
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
raise Exception("%s is not %s" % (fn, opts.bfdname))
|
||||||
write("\x0c")
|
write("\x0c")
|
||||||
|
|
||||||
if opts.bfdname[:3] == "elf":
|
if opts.bfdname[:3] == "elf":
|
||||||
if e_ident[:4] != "\x7f\x45\x4c\x46":
|
if e_ident[:4] != "\x7f\x45\x4c\x46".encode():
|
||||||
raise Exception("%s is not %s" % (fn, "ELF"))
|
raise Exception("%s is not %s" % (fn, "ELF"))
|
||||||
fp.seek(7, 0)
|
fp.seek(7, 0)
|
||||||
if opts.bfdname == "elf32-bigarm" and opts.elfosabi == "arm":
|
if opts.bfdname == "elf32-bigarm" and opts.elfosabi == "arm":
|
||||||
brand_arm("\x01\x02\x01")
|
brand_arm("\x01\x02\x01")
|
||||||
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "freebsd":
|
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "freebsd":
|
||||||
|
print ("case 4")
|
||||||
brand_freebsd("\x01\x01\x01")
|
brand_freebsd("\x01\x01\x01")
|
||||||
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "linux":
|
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "linux":
|
||||||
|
print ("case 5")
|
||||||
brand_linux("\x01\x01\x01")
|
brand_linux("\x01\x01\x01")
|
||||||
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "netbsd":
|
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "netbsd":
|
||||||
brand_netbsd("\x01\x01\x01")
|
brand_netbsd("\x01\x01\x01")
|
||||||
|
|||||||
@ -83,7 +83,7 @@ def parse_comment(state, l, comment):
|
|||||||
|
|
||||||
|
|
||||||
def handle_inc_c(state, l, ofp):
|
def handle_inc_c(state, l, ofp):
|
||||||
m = re.search(r"^\s*\#\s*include\s+([\"\<])(.+?)([\"\>])(.*)$", l)
|
m = re.search(r"^\s*\#\s*include\s+([\"\<])(.+?)([\"\>])(.*)$", l.decode())
|
||||||
if not m:
|
if not m:
|
||||||
return l
|
return l
|
||||||
q1, inc, q2, comment = m.groups()
|
q1, inc, q2, comment = m.groups()
|
||||||
@ -130,13 +130,13 @@ def handle_file(ifn, ofp, parent_state=None):
|
|||||||
ifp = open(ifn, "rb")
|
ifp = open(ifn, "rb")
|
||||||
for l in ifp.readlines():
|
for l in ifp.readlines():
|
||||||
state[2] += 1 # line counter
|
state[2] += 1 # line counter
|
||||||
l = l.rstrip("\n")
|
l = l.rstrip("\n".encode())
|
||||||
if opts.mode == "c":
|
if opts.mode == "c":
|
||||||
l = handle_inc_c(state, l, ofp)
|
l = handle_inc_c(state, l, ofp)
|
||||||
elif opts.mode == "nasm":
|
elif opts.mode == "nasm":
|
||||||
l = handle_inc_nasm(state, l, ofp)
|
l = handle_inc_nasm(state, l, ofp)
|
||||||
if l is not None:
|
if l is not None:
|
||||||
ofp.write(l + "\n")
|
ofp.write(l + "\n".encode())
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
@ -179,12 +179,12 @@ def main(argv):
|
|||||||
os.unlink(fn)
|
os.unlink(fn)
|
||||||
if files_mmd:
|
if files_mmd:
|
||||||
fp = open(fn, "wb")
|
fp = open(fn, "wb")
|
||||||
fp.write("%s : \\\n" % opts.target_mmd)
|
fp.write(("%s : \\\n" % opts.target_mmd).encode())
|
||||||
for i, f in enumerate(files_mmd):
|
for i, f in enumerate(files_mmd):
|
||||||
if i < len(files_mmd) - 1:
|
if i < len(files_mmd) - 1:
|
||||||
fp.write(" %s \\\n" % f)
|
fp.write((" %s \\\n" % f).encode())
|
||||||
else:
|
else:
|
||||||
fp.write(" %s\n" % f)
|
fp.write((" %s\n" % f).encode())
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -46,7 +46,7 @@ def strip_with_dump(dump_fn, eh, idata):
|
|||||||
new_len = 0
|
new_len = 0
|
||||||
lines = open(dump_fn, "rb").readlines()
|
lines = open(dump_fn, "rb").readlines()
|
||||||
for l in lines:
|
for l in lines:
|
||||||
l = re.sub(r"\s+", " ", l.strip())
|
l = re.sub(r"\s+", " ", l.strip().decode())
|
||||||
f = l.split(" ")
|
f = l.split(" ")
|
||||||
if len(f) >= 8:
|
if len(f) >= 8:
|
||||||
if f[7].startswith("CONTENTS"):
|
if f[7].startswith("CONTENTS"):
|
||||||
@ -67,7 +67,7 @@ def strip_with_dump(dump_fn, eh, idata):
|
|||||||
|
|
||||||
def check_dump(dump_fn):
|
def check_dump(dump_fn):
|
||||||
lines = open(dump_fn, "rb").readlines()
|
lines = open(dump_fn, "rb").readlines()
|
||||||
lines = map(lambda l: re.sub(r"\s+", " ", l.strip()).strip(), lines)
|
lines = map(lambda l: re.sub(r"\s+", " ", l.strip().decode()).strip(), lines)
|
||||||
lines = filter(None, lines)
|
lines = filter(None, lines)
|
||||||
d = "\n".join(lines)
|
d = "\n".join(lines)
|
||||||
psections = d.find("Sections:\n")
|
psections = d.find("Sections:\n")
|
||||||
@ -140,25 +140,25 @@ def do_file(fn):
|
|||||||
fp = open(fn, "r+b")
|
fp = open(fn, "r+b")
|
||||||
fp.seek(0, 0)
|
fp.seek(0, 0)
|
||||||
idata = fp.read()
|
idata = fp.read()
|
||||||
if idata[:4] != "\x7f\x45\x4c\x46":
|
if idata[:4] != "\x7f\x45\x4c\x46".encode():
|
||||||
raise Exception("%s is not %s" % (fn, "ELF"))
|
raise Exception("%s is not %s" % (fn, "ELF"))
|
||||||
if idata[4:7] == "\x01\x01\x01":
|
if idata[4:7] == "\x01\x01\x01".encode():
|
||||||
# ELF32 LE
|
# ELF32 LE
|
||||||
eh, idata = idata[:52], idata[52:]
|
eh, idata = idata[:52], idata[52:]
|
||||||
e_shnum, e_shstrndx = struct.unpack("<HH", eh[48:52])
|
e_shnum, e_shstrndx = struct.unpack("<HH", eh[48:52])
|
||||||
assert e_shstrndx + 3 == e_shnum
|
assert e_shstrndx + 3 == e_shnum
|
||||||
##eh = eh[:48] + struct.pack("<HH", e_shnum - 3, e_shstrndx)
|
##eh = eh[:48] + struct.pack("<HH", e_shnum - 3, e_shstrndx)
|
||||||
elif idata[4:7] == "\x01\x02\x01":
|
elif idata[4:7] == "\x01\x02\x01".encode():
|
||||||
# ELF32 BE
|
# ELF32 BE
|
||||||
eh, idata = idata[:52], idata[52:]
|
eh, idata = idata[:52], idata[52:]
|
||||||
e_shnum, e_shstrndx = struct.unpack(">HH", eh[48:52])
|
e_shnum, e_shstrndx = struct.unpack(">HH", eh[48:52])
|
||||||
assert e_shstrndx + 3 == e_shnum
|
assert e_shstrndx + 3 == e_shnum
|
||||||
elif idata[4:7] == "\x02\x01\x01":
|
elif idata[4:7] == "\x02\x01\x01".encode():
|
||||||
# ELF64 LE
|
# ELF64 LE
|
||||||
eh, idata = idata[:64], idata[64:]
|
eh, idata = idata[:64], idata[64:]
|
||||||
e_shnum, e_shstrndx = struct.unpack("<HH", eh[60:64])
|
e_shnum, e_shstrndx = struct.unpack("<HH", eh[60:64])
|
||||||
assert e_shstrndx + 3 == e_shnum
|
assert e_shstrndx + 3 == e_shnum
|
||||||
elif idata[4:7] == "\x02\x02\x01":
|
elif idata[4:7] == "\x02\x02\x01".encode():
|
||||||
# ELF64 BE
|
# ELF64 BE
|
||||||
eh, idata = idata[:64], idata[64:]
|
eh, idata = idata[:64], idata[64:]
|
||||||
e_shnum, e_shstrndx = struct.unpack(">HH", eh[60:64])
|
e_shnum, e_shstrndx = struct.unpack(">HH", eh[60:64])
|
||||||
@ -167,7 +167,7 @@ def do_file(fn):
|
|||||||
raise Exception("%s is not %s" % (fn, "ELF"))
|
raise Exception("%s is not %s" % (fn, "ELF"))
|
||||||
|
|
||||||
odata = None
|
odata = None
|
||||||
pos = idata.find("\0.symtab\0.strtab\0.shstrtab\0")
|
pos = idata.find("\0.symtab\0.strtab\0.shstrtab\0".encode())
|
||||||
if opts.with_dump:
|
if opts.with_dump:
|
||||||
eh, odata = strip_with_dump(opts.with_dump, eh, idata)
|
eh, odata = strip_with_dump(opts.with_dump, eh, idata)
|
||||||
# Other compilers can intermix the contents of .rela sections
|
# Other compilers can intermix the contents of .rela sections
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user