summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoritsme <itsme@xs4all.nl>2021-07-09 02:08:22 +0200
committeritsme <itsme@xs4all.nl>2021-07-09 02:08:22 +0200
commit2a9e53566d873b226d1523adcb80f78c14b4d773 (patch)
treeee374fcf8b441e75bad32f3d659bb75c36f55d48
parent8bcb2e9f570d3ee4a84884e5907b03e37a0b8389 (diff)
added support for compressed records. kodump now supports a --width option.
-rw-r--r--crodump.py33
-rw-r--r--hexdump.py14
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
6from hexdump import hexdump, asasc, tohex, unhex, strescape 6from hexdump import hexdump, asasc, tohex, unhex, strescape
7from koddecoder import kodecode 7from koddecoder import kodecode
8from readers import ByteReader 8from readers import ByteReader
9import zlib
9 10
10""" 11"""
11python3 crodump.py crodump chechnya_proverki_ul_2012 12python3 crodump.py crodump chechnya_proverki_ul_2012
@@ -126,6 +127,8 @@ class Datafile:
126 decdat = encdat 127 decdat = encdat
127 decflag = ' ' 128 decflag = ' '
128 129
130 if args.decompress and self.iscompressed(decdat):
131 decdat = self.decompress(decdat)
129 print("%5d: %08x-%08x: (%02x:%08x) %s %s%s %s" % (i+1, ofs, ofs+ln, flags, chk, infostr, decflag, toout(args, decdat), tohex(tail))) 132 print("%5d: %08x-%08x: (%02x:%08x) %s %s%s %s" % (i+1, ofs, ofs+ln, flags, chk, infostr, decflag, toout(args, decdat), tohex(tail)))
130 133
131 if args.verbose: 134 if args.verbose:
@@ -134,6 +137,22 @@ class Datafile:
134 dat = self.readdata(o, l) 137 dat = self.readdata(o, l)
135 print("%08x-%08x: %s" % (o, o+l, toout(args, dat))) 138 print("%08x-%08x: %s" % (o, o+l, toout(args, dat)))
136 139
140 def iscompressed(self, data):
141 if len(data)<11:
142 return
143 size, flag = struct.unpack_from(">HH", data, 0)
144 if size+5 != len(data):
145 return
146 if flag!=0x800:
147 return
148 if data[-3:] != b"\x00\x00\x02":
149 return
150 return True
151
152 def decompress(self, data):
153 C = zlib.decompressobj(-15)
154 return C.decompress(data[8:-3])
155
137def destruct_bank_definition(args, data): 156def destruct_bank_definition(args, data):
138 """ 157 """
139 decode the 'bank' / database definition 158 decode the 'bank' / database definition
@@ -284,16 +303,13 @@ def decode_kod(args, data):
284 """ 303 """
285 if args.nokod: 304 if args.nokod:
286 # plain hexdump, no KOD decode 305 # plain hexdump, no KOD decode
287 if args.ascdump: 306 hexdump(args.offset, data, args)
288 print(asasc(data))
289 else:
290 hexdump(args.offset, data)
291 307
292 elif args.shift: 308 elif args.shift:
293 # explicitly specified shift. 309 # explicitly specified shift.
294 args.shift = int(args.shift, 0) 310 args.shift = int(args.shift, 0)
295 enc = kodecode(args.shift, data) 311 enc = kodecode(args.shift, data)
296 hexdump(args.offset, enc) 312 hexdump(args.offset, enc, args)
297 elif args.increment: 313 elif args.increment:
298 # explicitly specified shift. 314 # explicitly specified shift.
299 for s in range(256): 315 for s in range(256):
@@ -317,6 +333,11 @@ def kod_hexdump(args):
317 args.endofs = int(args.endofs, 0) 333 args.endofs = int(args.endofs, 0)
318 args.length = args.endofs - args.offset 334 args.length = args.endofs - args.offset
319 335
336 if args.width:
337 args.width = int(args.width, 0)
338 else:
339 args.width = 64 if args.ascdump else 16
340
320 if args.filename: 341 if args.filename:
321 with open(args.filename, "rb") as fh: 342 with open(args.filename, "rb") as fh:
322 if args.length is None: 343 if args.length is None:
@@ -365,6 +386,7 @@ def main():
365 ko = subparsers.add_parser('kodump', help='KOD/hex dumper') 386 ko = subparsers.add_parser('kodump', help='KOD/hex dumper')
366 ko.add_argument('--offset', '-o', type=str, default="0") 387 ko.add_argument('--offset', '-o', type=str, default="0")
367 ko.add_argument('--length', '-l', type=str) 388 ko.add_argument('--length', '-l', type=str)
389 ko.add_argument('--width', '-w', type=str)
368 ko.add_argument('--endofs', '-e', type=str) 390 ko.add_argument('--endofs', '-e', type=str)
369 ko.add_argument('--unhex', '-x', action='store_true', help="assume the input contains hex data") 391 ko.add_argument('--unhex', '-x', action='store_true', help="assume the input contains hex data")
370 ko.add_argument('--shift', '-s', type=str, help="KOD decode with the specified shift") 392 ko.add_argument('--shift', '-s', type=str, help="KOD decode with the specified shift")
@@ -380,6 +402,7 @@ def main():
380 cro.add_argument('--ascdump', '-a', action='store_true') 402 cro.add_argument('--ascdump', '-a', action='store_true')
381 cro.add_argument('--nokod', '-n', action='store_true') 403 cro.add_argument('--nokod', '-n', action='store_true')
382 cro.add_argument('--struonly', action='store_true') 404 cro.add_argument('--struonly', action='store_true')
405 cro.add_argument('--nodecompress', action='store_false', dest='decompress', default='true')
383 cro.add_argument('dbdir', type=str) 406 cro.add_argument('dbdir', type=str)
384 cro.set_defaults(handler=cro_dump) 407 cro.set_defaults(handler=cro_dump)
385 408
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):
26 return "." 26 return "."
27def asasc(line): 27def asasc(line):
28 return "".join(aschr(_) for _ in line) 28 return "".join(aschr(_) for _ in line)
29def hexdump(ofs, data): 29def hexdump(ofs, data, args):
30 for o in range(0, len(data), 16): 30 w = args.width
31 print("%08x: %-47s %s" % (o+ofs, ashex(data[o:o+16]), asasc(data[o:o+16]))) 31 if args.ascdump:
32 fmt = "%08x: %s"
33 else:
34 fmt = "%%08x: %%-%ds %%s" % (3*w-1)
35 for o in range(0, len(data), w):
36 if args.ascdump:
37 print(fmt % (o+ofs, asasc(data[o:o+w])))
38 else:
39 print(fmt % (o+ofs, ashex(data[o:o+w]), asasc(data[o:o+w])))
32 40
33def tohex(data): 41def tohex(data):
34 return b2a_hex(data).decode('ascii') 42 return b2a_hex(data).decode('ascii')