Compact serialization for variable-length integers
authoralexhz <balthazar@yandex.ru>
Wed, 15 Jan 2014 11:42:48 +0000 (11:42 +0000)
committeralexhz <balthazar@yandex.ru>
Wed, 15 Jan 2014 11:42:48 +0000 (11:42 +0000)
commitb8b636c64f4b611daf4dd9b6cfaf6d6102ac7121
tree0680cdab63f780c7ee773222ee1e4fb11cb2707b
parentbafc8da08d2a0ece54ac443aa4569974b319cccb
Compact serialization for variable-length integers

Variable-length integers: bytes are a MSB base-128 encoding of the number.
The high bit in each byte signifies whether another digit follows. To make
the encoding is one-to-one, one is subtracted from all but the last digit.
Thus, the byte sequence a[] with length len, where all but the last byte
has bit 128 set, encodes the number:

  (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))

  Properties:
  * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
  * Every integer has exactly one encoding
  * Encoding does not depend on size of original integer type
src/checkpoints.h
src/serialize.h