SHARE
TWEET

abglz77decomp

a guest Mar 22nd, 2017 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2.  
  3. def decomp(rom, off):
  4.     """ Decomps lz77 compressed data specified by GBATEK, returns an uint8 list"""
  5.     if (rom.u8(off) >> 4) & 0xF != 1: raise Exception("Lz77 Error: Data is not lz77 compressed")
  6.     size = rom.u8(off + 1) + (rom.u8(off + 2) << 8) + (rom.u8(off + 3) << 16) #LE read of bit 8-31 of size field
  7.     out = [0] * size
  8.     off += 4 #Data starts at offset = 0x4
  9.     dst = 0
  10.     while size:
  11.         enc = rom.u8(off)
  12.         off += 1
  13.         for i in range(7, -1, -1):
  14.             if enc & (1 << i):
  15.                 #Reference to literal chain
  16.                 ref = rom.u8(off) + (rom.u8(off+1) << 8)
  17.                 off += 2
  18.  
  19.                 #Decode literal Reference
  20.                 disp = (ref >> 8) | ((ref & 0xF) << 8)
  21.                 length = ((ref >> 4) & 0xF) + 3 #Length+3 by GBATEK specs
  22.  
  23.                 #Output the literal
  24.                 for j in range(0, length):
  25.                     try:
  26.                         out[dst] = out[dst-disp-1]
  27.                     except Exception as e:
  28.                         print("Warning in lz77 decomp, malformed data :"+str(e))
  29.                         out[dst] = 0
  30.                     dst += 1
  31.                     size -= 1
  32.                     if not size: return out
  33.  
  34.             else:
  35.                 #New literal, raw dump
  36.                 out[dst] = rom.u8(off)
  37.                 off += 1
  38.                 dst += 1
  39.                 size -= 1
  40.                 if not size: return out
  41.     return out
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top