2.2.3.2 Decompressing Input of COMPTYPE COMPRESSED

If at any point during the steps specified in this section, the end of the input is reached before the termination of decompression, then the reader MUST treat the input as corrupt.

When the COMPTYPE field is set to COMPRESSED, the decompression process is a straightforward loop, as follows:

  • Read the CONTROL field, as specified in section 2.1.3.1.1, from the input.

  • Starting with the lowest bit (the 0x01 bit) in the CONTROL field, test each bit and carry out the actions as follows.

  • After all bits in the CONTROL field have been tested, read another value of a CONTROL field from the input and repeat the bit-testing process.

For each bit, the reader MUST evaluate its value and complete the corresponding steps as specified in this section.

If the value of the bit is zero:

  1. Read a 1-byte literal from the input and write it to the output.

  2. Set the byte in the dictionary at the current write offset to the literal from step 1.

  3. Increment the write offset and update the end offset, as appropriate, as specified in section 2.1.3.1.4.

If the value of the bit is 1:

  1. Read a 16-bit dictionary reference from the input in big-endian byte-order.

  2. Extract the offset from the dictionary reference, as specified in section 2.1.3.1.5.

  3. Compare the offset to the dictionary's write offset. If they are equal, then the decompression is complete; exit the decompression loop. If they are not equal, continue to the next step.

  4. Set the dictionary's read offset to offset.

  5. Extract the length from the dictionary reference and calculate the actual length by adding 2 to the length that is extracted from the dictionary reference.

  6. Read a byte from the current dictionary read offset and write it to the output.

  7. Increment the read offset, wrapping as appropriate, as specified in section 2.1.3.1.4.

  8. Write the byte to the dictionary at the write offset.

  9. Increment the write offset and update the end offset, as appropriate, as specified in section 2.1.3.1.4.

  10. Continue from step 6 until the number of bytes calculated in step 5 has been read from the dictionary.

The input value of the CRC field, as specified in section 2.1.3.1.1, MUST be calculated from every byte in the CONTENTS field, per the process specified in section 2.1.3.2. After the decompression process, if the calculated value of the CRC field does not match the value of the CRC field in the header, then the reader MUST treat the input as corrupt.