From 08f8e55446d28d580546192b20dc9ef125d575f3 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 23 Nov 2006 03:11:53 +0100 Subject: [PATCH] More updates on bin2h.py. --- src/stub/scripts/bin2h.py | 125 ++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/src/stub/scripts/bin2h.py b/src/stub/scripts/bin2h.py index d9f0d2e0..fc1b1bd8 100644 --- a/src/stub/scripts/bin2h.py +++ b/src/stub/scripts/bin2h.py @@ -146,57 +146,16 @@ def w_data_nasm(w, data): w_eol(w, i) -def encode_compressed_stub_header(method, idata, odata): - assert 0 < method <= 255 - if len(idata) <= 65535: - h = "UPX#" + struct.pack("= len(idata): - odata = idata - method = 0 - assert len(odata) <= len(idata), "stub compression failed" - - if len(opts.methods) > 1: +def write_stub(w, odata, method_index, methods): + method = methods[method_index] + if len(methods) > 1: if method_index == 0: w("#if (%s == %d)\n\n" % (opts.mname, method)) - elif method_index < len(opts.methods) - 1: + elif method_index < len(methods) - 1: w("\n#elif (%s == %d)\n\n" % (opts.mname, method)) else: w("\n#else\n\n") @@ -217,11 +176,59 @@ def write_stub(w, idata, method_index): if opts.mode == "c": w("};\n") - if len(opts.methods) > 1: - if method_index == len(opts.methods) - 1: + if len(methods) > 1: + if method_index == len(methods) - 1: w("\n#endif\n") +# /*********************************************************************** +# // compress stub +# ************************************************************************/ + +def encode_compressed_stub_header(method, idata, odata): + assert 0 < method <= 255 + if len(idata) <= 65535: + h = "UPX#" + struct.pack("= len(idata): + # not compressible + return 0, idata + assert len(odata) <= len(idata), "stub compression failed" + return method, odata + + # /*********************************************************************** # // main # ************************************************************************/ @@ -273,6 +280,23 @@ def main(argv): assert re.search(r"^[a-zA-Z]", opts.ident), opts.ident assert not re.search(r"[^a-zA-Z0-9_]", opts.ident), opts.ident + # 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() + for method in r_methods: + method, odata = compress_stub(method, idata) + if mdata_odata.has_key(method): + assert mdata_odata[method] == odata + else: + mdata_odata[method] = odata + mdata.append(method) + assert len(mdata) >= 1 + mdata.reverse() + ##print opts.methods, [(i, len(mdata_odata[i])) for i in mdata] + # write ofile if opts.dry_run: ofp = None @@ -287,9 +311,8 @@ def main(argv): if opts.verbose >= 0: if opts.mode == "c": w_header_c(w, ifile, ofile, len(idata)) - assert len(opts.methods) >= 1 - for i in range(len(opts.methods)): - write_stub(w, idata, i) + for i in range(len(mdata)): + write_stub(w, mdata_odata[mdata[i]], i, mdata) if ofp: if ofp is sys.stdout: ofp.flush()