summaryrefslogtreecommitdiff
path: root/crodump.py
diff options
context:
space:
mode:
Diffstat (limited to 'crodump.py')
-rw-r--r--crodump.py33
1 files changed, 28 insertions, 5 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