LZSS Compression: Difference between revisions

→‎Example C Decompression Function: tried to use this function and it didn't work. oops. fixed.
>Aruki
>Aruki
(→‎Example C Decompression Function: tried to use this function and it didn't work. oops. fixed.)
 
(4 intermediate revisions by the same user not shown)
Line 19: Line 19:
| 3
| 3
| '''Unused'''; always 0
| '''Unused'''; always 0
|-
| 0x4
| colspan=2 {{unknown|Compressed data starts}}
|}
|}


Line 57: Line 60:
If the bit corresponding to the current data chunk is not set, you read one byte group directly from the compressed buffer; simple.
If the bit corresponding to the current data chunk is not set, you read one byte group directly from the compressed buffer; simple.


If the bit is set, then that means you need to read data back from the decompressed buffer, which is where it gets a little more complicated. Read two bytes from the compressed buffer; these tell you exactly how far back in the decompressed buffer to seek, and how many byte groups to read from it. The exact way to utilize it varies depending which mode is set:
If the bit is set, then that means you need to read data back from the decompressed buffer, which is where it gets a little more complicated. Read two bytes from the compressed buffer; these tell you exactly how far back in the decompressed buffer to seek, and how many byte groups to read from it. The exact way to utilize them varies depending which mode is set:


{| class="wikitable"
{| class="wikitable"
Line 80: Line 83:
== Example C Decompression Function ==
== Example C Decompression Function ==


<syntaxhighlight lang="c" line start="1">bool DecompressLZSS(char *src, unsigned long src_len, char *dst, unsigned long dst_len)
<syntaxhighlight lang="c" line start="1">bool DecompressLZSS(u8 *src, u32 src_len, u8 *dst, u32 dst_len)
{
{
     char *src_end = src + src_len;
     u8 *src_end = src + src_len;
     char *dst_end = dst + dst_len;
     u8 *dst_end = dst + dst_len;


     // Read compressed buffer header
     // Read compressed buffer header
Line 123: Line 126:
             case 1:
             case 1:
                 count = (bytes[0] >> 4) + 3;
                 count = (bytes[0] >> 4) + 3;
                 length = (bytes[0] & 0xF) | bytes[1];
                 length = ((bytes[0] & 0xF) << 0x8) | bytes[1];
                 break;
                 break;
             case 2:
             case 2:
                 count = (bytes[0] >> 4) + 2;
                 count = (bytes[0] >> 4) + 2;
                 length = ((bytes[0] & 0xF) | bytes[1]) << 1;
                 length = (((bytes[0] & 0xF) << 0x8) | bytes[1]) << 1;
                 break;
                 break;
             case 3:
             case 3:
                 count = (bytes[0] >> 4) + 1;
                 count = (bytes[0] >> 4) + 1;
                 length = ((bytes[0] & 0xF) | bytes[1]) << 2;
                 length = (((bytes[0] & 0xF) << 0x8) | bytes[1]) << 2;
                 break;
                 break;
             }
             }


             // With the count and length calculated, we'll set a pointer to where we want to read back data from:
             // With the count and length calculated, we'll set a pointer to where we want to read back data from:
             char *seek = dst - length;
             u8 *seek = dst - length;


             // count refers to how many byte groups to read back; the size of one byte group varies depending on mode
             // count refers to how many byte groups to read back; the size of one byte group varies depending on mode
Anonymous user