Lzss decompression - Gemini-Loboto3/DC2-Mod-SDK GitHub Wiki

Most data formats that are either textures or game data are compressed with an lzss algorithm.

This is a C++ algorithm used to decompress the data:

u32 Dc2LzssDec(u8* src, u8*& dst, u32 src_size)
{
	// bit flag for decompression
	// (when its value is 1 the decompressor will read a new flag)
	int flag = 1;
	// allocate a buffer big enough for holding the decompressed data
	dst = new u8[src_size * 8];
	// temp buffer for killing the decompression loop
	u8* end = &src[src_size];
	// to be used when the decompression is complete
	u8* dec = dst;

	while (src < end)
	{
		// read flag and add extra bit for fancy math
		if (flag == 1) flag = *src++ | 0x100;
		// read next byte
		u8 ch = *src++;
		// process literal
		if (flag & 1) * dec++ = ch;
		// compressed sequence
		else
		{
			// read additional byte
			u8 t = *src++;
			// calculate jump and size
			int jump = ((t & 0xF) << 8) | ch;
			int size = (t >> 4) + 2;
			// seek to decompression buffer and copy to dest
			u8* src_dec = &dec[-jump];
			for (int i = 0; i < size; i++)
				* dec++ = *src_dec++;
		}
		// next flag
		flag >>= 1;
	}

	// return decompressed size
	return (u32)(dec - dst);
}