"Professionalise" greggybits

This commit is contained in:
Elliot Nunn 2020-06-23 13:46:45 +08:00
parent e7fafad400
commit 96e063057d
2 changed files with 17 additions and 4 deletions

View File

@ -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')

View File

@ -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