Compression

From ProjectPokemon Wiki
Revision as of 21:45, 1 May 2015 by Alpha (talk | contribs) (Created Page + LZ Compression)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This article is incomplete.
Please feel free to add missing information and complete the article.

Compression is used in many of the games to decrease the amount of space taken up by files when not loaded into RAM or just not actively being used.

LZ Compression

The default compression in NDS and later games is LZ compression. These compression types use references to text that has already been processed. The main variant LZ77 allows references up to 0xFFF bytes back for up to 0x12 bytes.

The header for LZ compression is 4 bytes wide:

 struct lz_header {
   uint32_t type : 8;  /* First byte */
   uint32_t decompressed_size : 24;  /* Other 3 bytes */
 }

LZ77 has a type of 0x10. LZSS has a type of 0x11.

LZSS Compression

Another common variant used is LZSS which allows for more controlled decompression. LZSS allows for up to 0xFFF bytes back for up to 0x1110 bytes depending on the mode switch.

BLZ Compression

BLZ compression is used mainly for data that cannot be copied into an output buffer (it must be done in place usually because the block is too large). This was implemented in Diamond and Pearl but was not extensively used until Heart Gold and Soul Silver, where it was used to compress the ARM binaries as well as the overlays.

The magic 0xDEC0xxxx indicates the 0x1Cth byte of the compressed region. The end of the compressed region is stored at the 0x14th byte (uint32_t). It is noting that the end is an absolute location in RAM. If the end RAM address is 0, the file is considered decompressed. Compression starts at the end of the compressed region and fills in from the end of the decompressed region until they meet at the start of the decompressed region.

 struct blz_control {
   uint32_t compressed_size : 24;
   uint32_t header_size : 8;
   uint32_t decompressed_increase;
 }