CMPL Compression - KCreator/Earth-Defence-Force-Documentation GitHub Wiki
mp-s note:
0x00 : 4bytes "CMPL" header
0x04 : 4bytes original file size(Big Endian)
0x08 : compressed contents
BlueAmulet's notes:
State:
8bit value, used as a bit buffer
Counter, tracks how many bits are available in the buffer
4096 bytes, cyclic buffer to fill and copy from
12bit value, tracks current buffer position
Initialization:
Mark the bit buffer counter as empty
Initialize copy buffer to zero
The game's implementation only initializes the first 4078 bytes
Thus making the remaining 18 bytes unsafe to copy from until written to
Set current buffer position to 4078 (0xFEE)
Loop:
If bit buffer is empty:
Read byte from input and store in bit buffer
Mark bit buffer counter as full (has 8 bits available)
If first bit in bit buffer is 1: (Copy byte from input)
Read byte from input
Write byte to output
Also write byte to current buffer position
Increment current buffer position
Else: (Copy bytes from buffer)
Read 16bit Big Endian value from input
First 4 bits + 3 is copy length
Upper 12bits is copy position
For 1 to (copy length):
Read byte from buffer at copy position
Write byte to output
Also write byte to current buffer position
Increment current buffer position
Increment copy position
Decrease bit buffer counter
Shift bit buffer to the right 1 bit, discarding first bit
If at any point all input bytes are consumed or output is full, the loop will be exited