Skip to content

Packed(n) is the integer array that FOR, PATCHED and the other integer codecs use for their numbers. It stores n integers as a minimum plus n fixed-width offsets. It has no codec id byte and no count of its own: the reader knows n from the codec that contains it.

Field Encoding Meaning
min zvarint the smallest value, see zvarint
width u8 bits per value, at most 64
bits ceil(n x width / 8) bytes value - min for each value, packed

The bit stream is little-endian and filled least significant bit first. Value 0 occupies bits 0 to width - 1 of the stream, value 1 the next width bits, and so on. Bit k of the stream is bit k mod 8 of byte k div 8. Unused bits at the end of the last byte are padding.

Width 0 means every value equals min, and the bit area is empty (ceil(n x 0 / 8) is 0 bytes).

  1. Read min as a zvarint.
  2. Read width as one byte. A width above 64 is Corrupt.
  3. Compute ceil(n x width / 8) with checked arithmetic and take that many bytes.
  4. For each value i, read width bits starting at bit i x width, least significant bit first. The value is min + offset.

This example is derived by hand from the rules and is not taken from a real file. Take n = 4, min = 10 and width = 3.

The encoded min is zvarint of 10: z = 20 = 0x14. The offsets are 0, 3, 5, 7, which is 000, 011, 101, 111 in 3 bits. Written least significant bit first, the 12 bits of the stream are:

bit 0 1 2 | 3 4 5 | 6 7 8 | 9 10 11
val 0 0 0 | 1 1 0 | 1 0 1 | 1 1 1

Bits 0 to 7 give 0b01011000 = 0x58. Bits 8 to 11 give 0b00001111 = 0x0F.

14 03 58 0F min 10, width 3, bits for offsets 0 3 5 7

The decoded values are 10, 13, 15, 17.

  • width is at most 64.
  • The byte count ceil(n x width / 8) must be computed with checked arithmetic and the input must contain that many bytes.
  • n comes from the enclosing codec and was checked against its limit there.