From 2a9e53566d873b226d1523adcb80f78c14b4d773 Mon Sep 17 00:00:00 2001 From: itsme Date: Fri, 9 Jul 2021 02:08:22 +0200 Subject: added support for compressed records. kodump now supports a --width option. --- crodump.py | 33 ++++++++++++++++++++++++++++----- hexdump.py | 14 +++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/crodump.py b/crodump.py index 8d69df9..80daab9 100644 --- a/crodump.py +++ b/crodump.py @@ -6,6 +6,7 @@ from binascii import b2a_hex from hexdump import hexdump, asasc, tohex, unhex, strescape from koddecoder import kodecode from readers import ByteReader +import zlib """ python3 crodump.py crodump chechnya_proverki_ul_2012 @@ -126,6 +127,8 @@ class Datafile: decdat = encdat decflag = ' ' + if args.decompress and self.iscompressed(decdat): + decdat = self.decompress(decdat) print("%5d: %08x-%08x: (%02x:%08x) %s %s%s %s" % (i+1, ofs, ofs+ln, flags, chk, infostr, decflag, toout(args, decdat), tohex(tail))) if args.verbose: @@ -134,6 +137,22 @@ class Datafile: dat = self.readdata(o, l) print("%08x-%08x: %s" % (o, o+l, toout(args, dat))) + def iscompressed(self, data): + if len(data)<11: + return + size, flag = struct.unpack_from(">HH", data, 0) + if size+5 != len(data): + return + if flag!=0x800: + return + if data[-3:] != b"\x00\x00\x02": + return + return True + + def decompress(self, data): + C = zlib.decompressobj(-15) + return C.decompress(data[8:-3]) + def destruct_bank_definition(args, data): """ decode the 'bank' / database definition @@ -284,16 +303,13 @@ def decode_kod(args, data): """ if args.nokod: # plain hexdump, no KOD decode - if args.ascdump: - print(asasc(data)) - else: - hexdump(args.offset, data) + hexdump(args.offset, data, args) elif args.shift: # explicitly specified shift. args.shift = int(args.shift, 0) enc = kodecode(args.shift, data) - hexdump(args.offset, enc) + hexdump(args.offset, enc, args) elif args.increment: # explicitly specified shift. for s in range(256): @@ -317,6 +333,11 @@ def kod_hexdump(args): args.endofs = int(args.endofs, 0) args.length = args.endofs - args.offset + if args.width: + args.width = int(args.width, 0) + else: + args.width = 64 if args.ascdump else 16 + if args.filename: with open(args.filename, "rb") as fh: if args.length is None: @@ -365,6 +386,7 @@ def main(): ko = subparsers.add_parser('kodump', help='KOD/hex dumper') ko.add_argument('--offset', '-o', type=str, default="0") ko.add_argument('--length', '-l', type=str) + ko.add_argument('--width', '-w', type=str) ko.add_argument('--endofs', '-e', type=str) ko.add_argument('--unhex', '-x', action='store_true', help="assume the input contains hex data") ko.add_argument('--shift', '-s', type=str, help="KOD decode with the specified shift") @@ -380,6 +402,7 @@ def main(): cro.add_argument('--ascdump', '-a', action='store_true') cro.add_argument('--nokod', '-n', action='store_true') cro.add_argument('--struonly', action='store_true') + cro.add_argument('--nodecompress', action='store_false', dest='decompress', default='true') cro.add_argument('dbdir', type=str) cro.set_defaults(handler=cro_dump) diff --git a/hexdump.py b/hexdump.py index cbba3c9..984a19f 100644 --- a/hexdump.py +++ b/hexdump.py @@ -26,9 +26,17 @@ def aschr(b): return "." def asasc(line): return "".join(aschr(_) for _ in line) -def hexdump(ofs, data): - for o in range(0, len(data), 16): - print("%08x: %-47s %s" % (o+ofs, ashex(data[o:o+16]), asasc(data[o:o+16]))) +def hexdump(ofs, data, args): + w = args.width + if args.ascdump: + fmt = "%08x: %s" + else: + fmt = "%%08x: %%-%ds %%s" % (3*w-1) + for o in range(0, len(data), w): + if args.ascdump: + print(fmt % (o+ofs, asasc(data[o:o+w]))) + else: + print(fmt % (o+ofs, ashex(data[o:o+w]), asasc(data[o:o+w]))) def tohex(data): return b2a_hex(data).decode('ascii') -- cgit v1.2.3