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:
John Reiser 2025-10-10 08:38:59 -07:00
parent a3d5a0a7b8
commit 4ec866594b
4 changed files with 44 additions and 38 deletions

View File

@ -45,7 +45,7 @@ class opts:
# ************************************************************************/
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\
This file is part of the UPX executable compressor.
@ -74,7 +74,7 @@ def w_header_c(w, ifile, ofile, n):
John F. Reiser
<jreiser@users.sourceforge.net>
*/\n\n""")
*/\n\n""".encode())
# /***********************************************************************
@ -87,11 +87,11 @@ class DataWriter:
self.pos = None
def w_bol(self, pos):
self.w("/* 0x%04x */ " % (pos))
self.w(("/* 0x%04x */ " % (pos)).encode())
self.pos = pos
def w_eol(self, fill=""):
if self.pos is not None:
self.w(fill.rstrip() + "\n")
self.w((fill.rstrip() + "\n").encode())
class DataWriter_c(DataWriter):
@ -101,8 +101,8 @@ class DataWriter_c(DataWriter):
if i & 15 == 0:
self.w_eol()
self.w_bol(i)
w("%3d" % ord(data[i]))
if i != n - 1: w(",")
w(("%3d" % data[i]).encode())
if i != n - 1: w(",".encode())
self.w_eol()
@ -169,10 +169,10 @@ class DataWriter_nasm(DataWriter):
# ************************************************************************/
def w_checksum_c(w, s, data):
w("#define %s_SIZE %d\n" % (s, len(data)))
w("#define %s_ADLER32 0x%08x\n" % (s, 0xffffffff & zlib.adler32(data)))
w("#define %s_CRC32 0x%08x\n" % (s, 0xffffffff & zlib.crc32(data)))
w("\n")
w(("#define %s_SIZE %d\n" % (s, len(data))).encode())
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))).encode())
w("\n".encode())
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("#endif\n")
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":
DataWriter_c(w).w_data(odata)
elif opts.mode == "gas":
@ -209,11 +209,11 @@ def write_stub(w, odata, method_index, methods):
assert 0, ("invalid mode", opts.mode)
if opts.ident:
if opts.mode == "c":
w("};\n")
w("};\n".encode())
if 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
# (process in reverse order so that incompressible do not get sorted first)
mdata, mdata_odata = [], {}
assert len(opts.methods) >= 1
r_methods = opts.methods[:]
r_methods.reverse()
### FIXME assert len(opts.methods) >= 1
### r_methods = opts.methods[:]
### r_methods.reverse()
r_methods = [];
for m in opts.methods:
r_methods.insert(0, m)
for method in r_methods:
method, odata = compress_stub(method, idata)
if method in mdata_odata:
@ -349,7 +352,7 @@ def main(argv):
if opts.mode == "c":
if opts.verbose >= 0:
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)):
write_stub(w, mdata_odata[mdata[i]], i, mdata)
if ofp:

View File

@ -54,39 +54,42 @@ def do_file(fn):
def write(s):
if not opts.dry_run:
fp.write(s)
fp.write(s.encode())
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))
write("\x61") # ELFOSABI_ARM
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))
write("\x09")
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))
##write("\x00Linux\x00\x00\x00")
write("\x00" * 9)
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))
write("\x02")
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))
write("\x0c")
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"))
fp.seek(7, 0)
if opts.bfdname == "elf32-bigarm" and opts.elfosabi == "arm":
brand_arm("\x01\x02\x01")
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "freebsd":
print ("case 4")
brand_freebsd("\x01\x01\x01")
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "linux":
print ("case 5")
brand_linux("\x01\x01\x01")
elif opts.bfdname == "elf32-i386" and opts.elfosabi == "netbsd":
brand_netbsd("\x01\x01\x01")

View File

@ -83,7 +83,7 @@ def parse_comment(state, l, comment):
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:
return l
q1, inc, q2, comment = m.groups()
@ -130,13 +130,13 @@ def handle_file(ifn, ofp, parent_state=None):
ifp = open(ifn, "rb")
for l in ifp.readlines():
state[2] += 1 # line counter
l = l.rstrip("\n")
l = l.rstrip("\n".encode())
if opts.mode == "c":
l = handle_inc_c(state, l, ofp)
elif opts.mode == "nasm":
l = handle_inc_nasm(state, l, ofp)
if l is not None:
ofp.write(l + "\n")
ofp.write(l + "\n".encode())
def main(argv):
@ -179,12 +179,12 @@ def main(argv):
os.unlink(fn)
if files_mmd:
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):
if i < len(files_mmd) - 1:
fp.write(" %s \\\n" % f)
fp.write((" %s \\\n" % f).encode())
else:
fp.write(" %s\n" % f)
fp.write((" %s\n" % f).encode())
fp.close()

View File

@ -46,7 +46,7 @@ def strip_with_dump(dump_fn, eh, idata):
new_len = 0
lines = open(dump_fn, "rb").readlines()
for l in lines:
l = re.sub(r"\s+", " ", l.strip())
l = re.sub(r"\s+", " ", l.strip().decode())
f = l.split(" ")
if len(f) >= 8:
if f[7].startswith("CONTENTS"):
@ -67,7 +67,7 @@ def strip_with_dump(dump_fn, eh, idata):
def check_dump(dump_fn):
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)
d = "\n".join(lines)
psections = d.find("Sections:\n")
@ -140,25 +140,25 @@ def do_file(fn):
fp = open(fn, "r+b")
fp.seek(0, 0)
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"))
if idata[4:7] == "\x01\x01\x01":
if idata[4:7] == "\x01\x01\x01".encode():
# ELF32 LE
eh, idata = idata[:52], idata[52:]
e_shnum, e_shstrndx = struct.unpack("<HH", eh[48:52])
assert e_shstrndx + 3 == e_shnum
##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
eh, idata = idata[:52], idata[52:]
e_shnum, e_shstrndx = struct.unpack(">HH", eh[48:52])
assert e_shstrndx + 3 == e_shnum
elif idata[4:7] == "\x02\x01\x01":
elif idata[4:7] == "\x02\x01\x01".encode():
# ELF64 LE
eh, idata = idata[:64], idata[64:]
e_shnum, e_shstrndx = struct.unpack("<HH", eh[60:64])
assert e_shstrndx + 3 == e_shnum
elif idata[4:7] == "\x02\x02\x01":
elif idata[4:7] == "\x02\x02\x01".encode():
# ELF64 BE
eh, idata = idata[:64], idata[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"))
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:
eh, odata = strip_with_dump(opts.with_dump, eh, idata)
# Other compilers can intermix the contents of .rela sections