Format:Sawyer chunk encoding
| Platforms | Windows x86 |
|---|---|
| Magic | none (first byte of a chunked file is the encoding, 0x00-0x03) |
| Extensions | .SV6, .SC6, .DAT (RCT2 objects), .TD6, .SV4, .SC4, .TD4, .SV5, .SC5 |
| Endianness | little |
| On this wiki | Facts · Code · Notes · Topics |
Chunk container and compression scheme used by Chris Sawyer's games: RollerCoaster Tycoon, RollerCoaster Tycoon 2 and Chris Sawyer's Locomotion. A chunk is a 5-byte header (encoding byte, 32-bit length) followed by data stored raw, run-length encoded, run-length encoded plus a short back-reference stage, or bit-rotated.
Identification
There is no magic number. A chunked file starts directly with a chunk header: one encoding byte (0x00-0x03) and a little-endian 32-bit length of the encoded payload. Chunked files end with a 32-bit checksum after the last chunk. Some formats skip the chunk header and store the whole file as one run-length encoded stream followed by a checksum (Format:TD6 (RollerCoaster Tycoon 2), RollerCoaster Tycoon SV4/SC4).
Header
| Offset | Size | Type | Name | Notes |
|---|---|---|---|---|
| 0x00 | 1 | uint8_t |
encoding | 0 none, 1 RLE, 2 RLE + repeat, 3 rotate |
| 0x01 | 4 | uint32_t |
length | Size of the encoded payload that follows, in bytes |
The payload follows immediately; the next chunk starts at 5 + length. The decoded size is not stored: readers either know it from the format or decode into a growing buffer.
Encodings
- 0, none: payload is the data.
- 1, RLE: a sequence of code bytes. A code byte
cwith bit 7 set means: repeat the next byte257 - ctimes (2 to 129 copies). Otherwise copy the nextc + 1bytes literally (1 to 128 bytes). - 2, RLE + repeat: first undo the RLE stage, then a second stage with back-references. In the second stage,
0xFFfollowed by a byte emits that byte literally; any other bytetcopies(t & 7) + 1bytes (1 to 8) starting32 - (t >> 3)bytes back (1 to 32) in the output produced so far. Encoders start with a literal, since there is nothing to refer to yet. - 3, rotate: each byte is rotated right by 1, 3, 5, 7, 1, 3, ... bits in turn (the shift starts at 1 and advances by 2 modulo 8). Encoding rotates left.
File checksum
Chris Sawyer's Locomotion files (SV5/SC5) end with a 32-bit sum of every preceding byte of the file; OpenLoco validates it that way. Unverified: RollerCoaster Tycoon 2 SV6/SC6 use the same additive checksum (see Format:SV6 (RollerCoaster Tycoon 2)). Track designs and RollerCoaster Tycoon SV4/SC4 use a different, rotating checksum that also carries a version number (see Format:TD6 (RollerCoaster Tycoon 2) and Software:RollerCoaster Tycoon).
Pitfalls
- The RLE stage resembles Apple PackBits but is not identical: here code
0x80means 129 copies of the next byte, where PackBits treats0x80as a no-op. - Encoding 2 is two stages: RLE first on decode, repeat stage second. Applying them in the other order produces garbage that often still has the right length.
- OpenRCT2 refuses chunks whose encoded length is 16 MiB or more and chunks that decode to zero bytes; treat either as corruption.
- In the repeat stage, back-references may only point into bytes already produced; a reference before the start of the output is invalid.
Tools that read/write it
- OpenRCT2 (
src/openrct2/sawyer_coding/) reads and writes all four encodings; its unit testtest/tests/SawyerCodingTest.cpphas one encoded sample per encoding and several invalid ones, useful to test a new decoder. - OpenLoco (
src/OpenLoco/src/S5/SawyerStream.cpp) implements the same four encodings for Locomotion files.