Module eightpack_varint

submodule for "eightpack" that adds the ability to encode and decode variable byte-sized integers.

this part of the library has been separated from "eightpack" because of the unlikeyhood of being used.

  • uvar stands for an unsigned variable-sized integer.
  • ivar stands for a signed variable-sized integer.

these variable-sized encodings are especially useful for encoding the length of other variables in their header (i.e. at the beginning of their sequence).

  • the byte form of a uvar occupies a variable number of bytes to accommodate the unsigned integer that it is holding.
  • for each byte, it uses the first bit of the octet (0bXYYYYYYY) to signal whether the integer carries on to the next byte (X == 1) or not (X == 0).
  • the remaining 7 bits in each byte are used for base-7 big-endian encoding of the numeric value of the integer (YYYYYYY).
  • you can read more about it on wikipedia.

the following table lists the first few bounds of this encoding:

decimal unsigned big endian binary unsigned variable binary
0 0b00000000 0b00000000 0b00000000 0b00000000 0b00000000
127 = 2^7 - 1 0b00000000 0b00000000 0b00000000 0b01111111 0b01111111
128 = 2^7 0b00000000 0b00000000 0b00000000 0b10000000 0b10000001 0b00000000
16383 = 2^14 - 1 0b00000000 0b00000000 0b00111111 0b11111111 0b11111111 0b01111111
16384 = 2^14 0b00000000 0b00000000 0b01000000 0b00000000 0b10000001 0b10000000 0b00000000
  • the ivar encoding is similar to the uvar encoding, with the exception that in the first byte, the second-major bit Z of the octet (0b0ZYYYYYY) signals whether the number is positive (Z == 0), or negative (Z == 1).

the following table lists the first few bounds of this encoding:

decimal signed big endian binary signed variable binary
0 0b00000000 0b00000000 0b00000000 0b00000000 0b00000000 or 0b01000000
63 = 2^6 - 1 0b00000000 0b00000000 0b00000000 0b00111111 0b00111111
-63 = -(2^6 - 1) 0b00000000 0b00000000 0b00000000 0b11000001 0b01111111
8191 = 2^13 - 1 0b00000000 0b00000000 0b00011111 0b11111111 0b10111111 0b01111111
-8191 = -(2^13 - 1) 0b00000000 0b00000000 0b11100000 0b00000001 0b11111111 0b01111111

Functions

encode_varint
encode_varint_array
decode_varint
decode_varint_array
encode_uvar_array
decode_uvar_array
encode_ivar_array
decode_ivar_array
encode_uvar
decode_uvar
encode_ivar
decode_ivar