Skip to content

Commit

Permalink
Minor changes to CRC32 checking of ZD2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
mungewell committed Oct 19, 2022
1 parent 2d033c8 commit 6b6c015
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
35 changes: 18 additions & 17 deletions decode_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import zoomzt2
import hashlib
import crcmod
from sys import exit

#--------------------------------------------------
def main():
Expand Down Expand Up @@ -63,12 +64,9 @@ def main():
help="extract XML from donor",
action="store_true", dest="dxml")

donor.add_argument("-v", "--crc",
donor.add_argument("-V", "--crc",
help="validate CRC32 checksum",
action="store_true", dest="crc")
donor.add_argument("-V", "--recalc",
help="recalculate CRC32 checksum",
action="store_true", dest="recalc")

donor.add_argument("-o", "--output",
help="output combined result to FILE", dest="output")
Expand All @@ -94,6 +92,9 @@ def main():

if (config['checksum'] == crc32.crcValue ^ 0xffffffff):
print("Checksum Validated: 0x%8.8x" % config['checksum'])
else:
exit("Checksum Invalid: 0x%8.8x, should be 0x%8.8x" % \
(config['checksum'], crc32.crcValue ^ 0xffffffff))

if options.dump and data:
config = zoomzt2.ZD2.parse(data)
Expand All @@ -115,7 +116,7 @@ def main():
if data and options.bitmap:
outfile = open(options.bitmap, "wb")
if not outfile:
sys.exit("Unable to open FILE for writing")
exit("Unable to open FILE for writing")

config = zoomzt2.ZD2.parse(data)
outfile.write(config["ICON"]["data"])
Expand All @@ -124,7 +125,7 @@ def main():
if data and options.xml:
outfile = open(options.xml, "wb")
if not outfile:
sys.exit("Unable to open FILE for writing")
exit("Unable to open FILE for writing")

config = zoomzt2.ZD2.parse(data)
if options.japan:
Expand All @@ -136,7 +137,7 @@ def main():
if data and options.text:
outfile = open(options.text, "wb")
if not outfile:
sys.exit("Unable to open FILE for writing")
exit("Unable to open FILE for writing")

config = zoomzt2.ZD2.parse(data)
if options.japan:
Expand All @@ -148,7 +149,7 @@ def main():
if data and options.info:
outfile = open(options.info, "wb")
if not outfile:
sys.exit("Unable to open FILE for writing")
exit("Unable to open FILE for writing")

config = zoomzt2.ZD2.parse(data)
outfile.write(config["INFO"]["data"])
Expand All @@ -157,7 +158,7 @@ def main():
if data and options.code:
outfile = open(options.code, "wb")
if not outfile:
sys.exit("Unable to open FILE for writing")
exit("Unable to open FILE for writing")

config = zoomzt2.ZD2.parse(data)
outfile.write(config["DATA"]["data"])
Expand All @@ -166,13 +167,13 @@ def main():
if data and options.output:
outfile = open(options.output, "wb")
if not outfile:
sys.exit("Unable to open output FILE for writing")
exit("Unable to open output FILE for writing")
config = zoomzt2.ZD2.parse(data)

if options.donor:
infile = open(options.donor, "rb")
if not infile:
sys.exit("Unable to open donor FILE for reading")
exit("Unable to open donor FILE for reading")
else:
ddata = infile.read()
infile.close()
Expand All @@ -193,13 +194,13 @@ def main():

data = zoomzt2.ZD2.build(config)

if options.recalc and data:
crc32 = crcmod.Crc(0x104c11db7, rev=True, initCrc=0x00000000, xorOut=0xFFFFFFFF)
crc32.update(data[12:-16])
# recalc the CRC32
crc32 = crcmod.Crc(0x104c11db7, rev=True, initCrc=0x00000000, xorOut=0xFFFFFFFF)
crc32.update(data[12:-16])

config['checksum'] = crc32.crcValue ^ 0xffffffff
print("Checksum Recalculated: 0x%8.8x" % config['checksum'])
data = zoomzt2.ZD2.build(config)
config['checksum'] = crc32.crcValue ^ 0xffffffff
print("Checksum Recalculated: 0x%8.8x" % config['checksum'])
data = zoomzt2.ZD2.build(config)

outfile.write(data)
outfile.close()
Expand Down
6 changes: 3 additions & 3 deletions zoomzt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
"length" / Int32ul,
"checksum" / Int32ul,

#"hexdump" / HexDump(Peek(Bytes(77))),

"hex1" / HexDump(Peek(Bytes(77))),
"unknown" / Bytes(77),

"version" / PaddedString(4, "ascii"),
Const(b"\x00\x00"),
"group" / Byte,
Expand Down Expand Up @@ -152,7 +152,7 @@
"PRMJ" / PRMJ,
"PRME" / PRME,

#"tail" / HexDump(Peek(Bytes(16))),
"hex5" / HexDump(Peek(Bytes(16))),
"unknown5" / Bytes(16),
)

Expand Down

0 comments on commit 6b6c015

Please sign in to comment.