Snippets

Alexander Hanel Aplib Header Notes and Creator

Created by Alexander Hanel last modified
'''
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 
      compressd data. 
    * The below code returns the compressed data with the header.
    * Relies on Ange Albertini aplib.py 


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 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ÿ.¸Â‘
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 

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.