HuffmanCodec

Nintendo DS Huffman codec.

Supports both the 4-bit (magic 0x24) and 8-bit (magic 0x28) variants of the Nintendo DS Huffman compression format as documented in GBATek / CUE references.

Compressed file layout

byte  0      : magic  (0x24 = 4-bit, 0x28 = 8-bit)
bytes 1-3 : uncompressed size, 24-bit little-endian
byte 4 : halfTreeSize (H) — tree occupies (H+1)*2 bytes
bytes 5..5+(H+1)*2-1 : Huffman tree node pairs
(padding to next 4-byte boundary)
remaining : compressed data as 32-bit little-endian words, bits consumed MSB-first

Tree node pair format

Each node is a pair of two bytes at indices [2*n, 2*n+1]:

  • leftDesc = tree[2*n]

  • rightDesc = tree[2*n+1]

For a descriptor byte d at node pair index n:

  • bits 5-0 : offset from the next pair; child pair index = n + 1 + (d & 0x3F)

  • bit 7 of leftDesc : left child is a leaf

  • bit 6 of leftDesc : right child is a leaf

When the child is a leaf, the symbol value is stored in tree[childPair * 2] (first byte of the child pair).

Functions

Link copied to clipboard

Compresses input using 8-bit Nintendo DS Huffman encoding (magic 0x28).

Link copied to clipboard

Decompresses Nintendo DS Huffman-encoded data.