Updated by Alexander Hanel 2016-07-31 View revision File aplib_header.py Modified Side-by-side diff More Ignore whitespace Hide word diff Alexaner Hanel Note: - * Some implementations of aplib do not require a header - * I could not find a tool that could create the aplib header from data - compressd data. The below code returns the compressed data with the header. + * Some implementations of aplib do not require a header. + * I could not find a tool that could create the aplib header from + compressd data. + * The below code returns the compressed data with the header. * Relies on Ange Albertini aplib.py -aplib header +aplib header example Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00000000 41 50 33 32 18 00 00 00 6A 2D 00 00 C3 5B 3C CC AP32....j-..Ã[<Ì 00000010 00 64 00 00 3A F8 81 21 4D 38 5A 90 38 03 66 02 .d..:ø.!M8Z.8.f. -no header +no header example Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00000000 4D 38 5A 90 38 03 66 02 04 09 71 FF 81 B8 C2 91 M8Z.8.f...qÿ.¸Â‘
Created by Alexander Hanel 2016-07-31 View revision File aplib_header.py Added Side-by-side diff More Ignore whitespace Hide word diff +''' +Author: + Alexaner Hanel + +Note: + * Some implementations of aplib do not require a header + * I could not find a tool that could create the aplib header from data + compressd data. The below code returns the compressed data with the header. + * Relies on Ange Albertini aplib.py + + +aplib header +Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F + +00000000 41 50 33 32 18 00 00 00 6A 2D 00 00 C3 5B 3C CC AP32....j-..Ã[<Ì +00000010 00 64 00 00 3A F8 81 21 4D 38 5A 90 38 03 66 02 .d..:ø.!M8Z.8.f. + +no header +Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F + +00000000 4D 38 5A 90 38 03 66 02 04 09 71 FF 81 B8 C2 91 M8Z.8.f...qÿ.¸Â‘ +00000010 01 40 C2 15 C6 80 09 1C 0E 1F BA F8 00 B4 09 CD .@Â.Æ€....ºø.´.Í + +Aplib header notes + * tag: 4 bytes, 41 50 33 32 or "AP32" + * header_size: 4 bytes // offset to the compressed data + * packed_size: 4 bytes // size of the compressed data + * packed_crc: 4 bytes // crc of the compressed data + * original_size: 4 bytes // original size of the uncompressed data + * original_crc: 4 bytes // crc of the uncompress data + +Source: See Aplib's spack.asm + +''' + +from zlib import crc32 +from aplib import * +import struct + + +HEADER_TAG = "AP32" +HEADER_SIZE = struct.pack("<I", 0x18) + +def create_header(data): + header = HEADER_TAG + HEADER_SIZE + header += struct.pack("<I", len(data)) + header += struct.pack("<I", crc32(data) & 0xFFFFFFFF) + try: + decompressed_data = decompress(data).do()[0] + except: + return None + header += struct.pack("<I", len(decompressed_data)) + header += struct.pack("<I", crc32(decompressed_data) & 0xFFFFFFFF) + return header + data