From 96e063057d1d5ee037fb4a014e32cb5d88177b60 Mon Sep 17 00:00:00 2001 From: Elliot Nunn Date: Tue, 23 Jun 2020 13:46:45 +0800 Subject: [PATCH] "Professionalise" greggybits --- bin/greggybits | 11 +++++++---- macresources/greggybits.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/bin/greggybits b/bin/greggybits index dede560..c3e1c1c 100755 --- a/bin/greggybits +++ b/bin/greggybits @@ -67,7 +67,7 @@ if __name__ == '__main__': args = parser.parse_args() for el in args.path: - from macresources.greggybits import pack, unpack + from macresources.greggybits import pack, unpack, WrongFormatError with open(el, 'r+b') as f: already_compressed = (f.read(4) == b'\xA8\x9Fer') @@ -82,11 +82,14 @@ if __name__ == '__main__': if args.debug: debug_round_trip(el, data) else: if args.debug: debug_round_trip(el, data) - data = unpack(data) + try: + data = unpack(data) + except WrongFormatError: + continue f.seek(0) f.write(data) f.truncate() - except ValueError: + except: + print(el) raise - print('some kind of error') diff --git a/macresources/greggybits.py b/macresources/greggybits.py index 9f789b2..05edc86 100644 --- a/macresources/greggybits.py +++ b/macresources/greggybits.py @@ -21,6 +21,11 @@ import struct + +class WrongFormatError(ValueError): + pass + + # predefined lookup table of the most frequent words TABLE = ( 0x0000, 0x0008, 0x4EBA, 0x206E, 0x4E75, 0x000C, 0x0004, 0x7000, @@ -61,12 +66,17 @@ TABLE_DICT = {word: idx for (idx, word) in enumerate(TABLE)} def unpack(src, _calculate_slop=False): + if len(src) < 18: raise WrongFormatError + dst = bytearray() pos = 0 magic, hdrLen, vers, iscmp, unpackSize, _dcmp, _slop, tabSize, comprFlags = struct.unpack_from(">LHBBLHHBB", src, pos) pos += 18 + if magic != 0xA89F6572 or hdrLen != 18 or vers != 9 or iscmp != 1 or _dcmp != 2: + raise WrongFormatError + hasDynamicTab = comprFlags & 1 isBitmapped = comprFlags & 2