2.5.1.2 Buffers and Chunks

A compressed buffer consists of a series of one or more compressed output chunks. Each chunk begins with a 16-bit header.

If both bytes of the header are 0, the header is an End_of_buffer terminal that denotes the end of the compressed data stream.

Otherwise, the header MUST be formatted as follows:

  • Bit 15 indicates whether the chunk contains compressed data.

  • Bits [14:12] contain a signature indicating the format of the subsequent data.

  • Bits [11:0] contain the size of the compressed chunk, minus three bytes.

Bit 15 indicates whether the chunk contains compressed data. If this bit is zero, the chunk header is followed by uncompressed literal data. If this bit is set, the next byte of the chunk is the beginning of a Flag_group nonterminal that describes some compressed data.

Bits 14 down to 12 contain a signature value. This value MUST always be 3 (unless the header denotes the end of the compressed buffer).

Bits 11 down to 0 contain the size of the compressed chunk minus three bytes. This size otherwise includes the size of any metadata in the chunk, including the chunk header. If the chunk is uncompressed, the total amount of uncompressed data therein can be computed by adding 1 to this value (adding 3 bytes to get the total chunk size, then subtracting 2 bytes to account for the chunk header).

The End_of_buffer character is not required to terminate the compressed buffer. The character is used, however, if space allows. For example, given 20 kilobytes (KB) of uncompressed data and a 10 KB buffer to contain the compressed data, if the size of the compressed data (including metadata) is exactly 10 KB, the capacity of the buffer has been met. In such a case, the End_of_buffer terminal is not written.

Because the presence of this terminal is not guaranteed, the size of the compressed data MUST be known before data in this format is decompressed.

If an End_of_buffer terminal is added, the size of the final compressed data is considered not to include the size of the End_of_buffer terminal.