Changes

Jump to navigation Jump to search

FF7/LZSS format

623 bytes added, 01:00, 15 July 2019
m
11 revisions imported
=== LZS Compressed archive for PSX by [[User:Ficedula|Ficedula]] = Format ====
==== Format ====The LZSS archive has a very small header at 0x00 that has the length of the compressed file as an unsigned 32 bit integer. After that is the compressed data.Some files use the .lzs extension, probably to make the extension 3 characters long. It has caused some confusion, since LZS is a different compression method.
The LZS archive has a very small header at 0x00 that has the length of the decompressed file as an unsigned 32 bit integer. After that is the compressed data. ==== LZSS compression ====
==== LZS FF7 uses LZSS compression on some of their files, as devised by Professor Haruhiko Okumura. LZSS data works on a control byte scheme.Each block in the file begins with a single byte indicating how much of the block is uncompressed ('literal data'), and how much is compressed ('references'). You read the bits LSB-first, with 0=reference, 1===literal.
FF7 uses LZS compression on some of their files - more properly, a slightly modified version of LZSS compression as devised by Professor Haruhiko Okumura. LZS Literal data works on a control means just that: read one byte scheme. So each block in from the file begins with a single byte indicating how much of the block is uncompressed source ('literal compressed) data'), and how much is compressed ('references'). You read write it straight to the byte right-to-left, with 1=literal, 0=referenceoutput.
Literal References take up two bytes, and are essentially a pointer to a piece of data means just that: read one byte in from 's been written out (i.e. is part of the source (compresseddata you've already decompressed) data. LZSS uses a 4KiB buffer, and write so it straight to can only reference data in the outputlast 4KiB of data.
References take up two bytes, and are essentially a pointer to a piece of data that's been written out (i.e. is part of the data you've already decompressed). LZSS uses a 4K buffer, so it can only reference data in the last 4K of data. ==== Reference format ====
==== Reference format ====A reference takes up two bytes, and has two pieces of information in it: offset (where to find the data, or which piece of data is going to be repeated), and length (how long the piece of data is going to be). The two reference bytes look like this:
A reference takes up two bytes, and has two pieces of information in it: offset (where to find the data, or which piece of data is going to be repeated), and length (how long the piece of data is going to be). The two reference bytes look like this:   OOOO OOOO OOOOOOOO OOOO LLLLOOOOLLLL
(O = Offset, L = Length)
 
The 1st byte it the least significant byte of the offset. The second byte has the remaining 4 bits of the offset as it's '''high''' nibble, so some shifting is required to extract it properly. The remaining 4 bits is the length minus 3.
So you get a 12-bit offset and a 4-bit length, but both of these values need modifying to work on directly. The length is easy to work with: just add 3 to it. This is because if a piece of repeated data was less than 3 bytes long, you wouldn't bother repeating it - it'd take up no more space to actually just put literal data in. So all references are at least 3 in length. So a length of 0 means 3 bytes repeated, 1 means 4 bytes repeated, so on.
real_offset = tail - ((tail - 18 - raw_offset) mod 4096)
Here, 'tail' is your current output position (eg. 10,000), 'raw_offset' is the 12-bit data value you've retrieved from the compressed reference, and 'real_offset' is the position in your output buffer you can begin reading from. This is a bit complex because it's not exactly the way LZSS traditionally does (de)compression; it uses decompression. If you use a 4K circular 4KiB buffer; if , you do that, can use the offset directly. The offset is more absolute, and not relative to the cursor position or less usable directlythe position in the input stream. You should initialize the buffer position to 0xFEE and not zero. The buffer content should be initialized to zero.
Once you've got to the start position for your reference, you just copy the appropriate length of data over to your output, and you've dealt with that piece of data.
==== Example ====
If we're at position 1000 in our output, and we need to read in a new control byte because we've finished with the last one. The next data to look it is:
0x030xFC, 0x53, 0x12 .....
We read in a control byte: 0xFC. In binary, that's 11111100. That informs us that the current block of data has two compressed offsets (@ 2 bytes each), followed by 6 literal data bytes. Once we'd read in the next 10 bytes (the compressed data plus the literal data), we'd be ready to read in our next control byte and start again.
Our position in output is still 1000. So our final offset is:
<nowiki>= 1000 - ((1000 - 18 - 339) and $FFF) </nowiki>
The 339 is just $153 in decimal.The (and $FFF) is a quick way to do modulus 4096.
<nowiki>= 1000 - (643 and 0xFFF)
= 1000 - 643
= 357
</nowiki>
So our final offset is 357. We go to position 357 in our output data, read in 5 bytes (remember the length?), then write those 5 bytes out to our output. Now we're ready to read in the next bit of data (another compressed reference), and do the procedure again.
==== Complications ====
Unfortunately, that doesn't quite cover everything - there's two more things to be aware of when decompressing data that will ruin you when using FF7 files, since they do use these features.
First, if you end up with an negative offset, i.e. reading data from 'before the beginning of the file', write out nulls (zero bytes). That's because the compression buffer is, by default, initialized to zeros; so it's possible, if the start of the file contains a run of zeros, that the file may reference a block you haven't written. For example, if you're at position 50 in your output, it's possible you may get an offset indicating to go back 60 bytes to offset -10. If you have to read 5 bytes from there, you just write out 5 nulls. However, you could have to read 15 bytes from there. In that case, you write out 10 nulls (the part of the data 'before' the file start), then the 5 bytes from the beginning of the file.
Secondly, you can have a repeated run. This is almost the opposite problem: when you go off the end of your output. Say you're at offset 100 in your output, and you have to go to offset 95 to read in a reference. This is okay, but if the reference length is >5, you loop the output. So if you had to write out 15 bytes, you'd write out the five bytes that were available, then write them out again, then again, to make up the 15 bytes you needed.
The FF7 files use both of these 'tricks', so you can't ignore them.
 
If you use a circular 4KiB buffer, you can ignore these issues completely, as long as you do a one-byte-at-a-time copy for the references.

Navigation menu