SQLite Forum

varint documentation is misleading
Login

varint documentation is misleading

(1) By anonymous on 2020-06-10 14:06:34 [source]

https://sqlite.org/src4/doc/trunk/www/varint.wiki

Decode

  • If A0 is between 0 and 240 inclusive, then the result is the value of A0.
  • If A0 is between 241 and 248 inclusive, then the result is 240+256*(A0-241)+A1.

  • ...

However:

https://www3.sqlite.org/src/file?name=src/util.c

/*
** Read a 64-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read.  The value is stored in *v.
*/
u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
  u32 a,b,s;

  if( ((signed char*)p)[0]>=0 ){
    *v = *p;
    return 1;
  }
  if( ((signed char*)p)[1]>=0 ){
    *v = ((u32)(p[0]&0x7f)<<7) | p[1];
    return 2;
  }

  ...

- which doesn't look like the description above at all.

https://www.sqlite.org/fileformat2.html#varint seems to be correct-ish:

The varint consists of either zero or more bytes which have the high-order bit set followed by a single byte with the high-order bit clear, or nine bytes, whichever is shorter. The lower seven bits of each of the first eight bytes and all 8 bits of the ninth byte are used to reconstruct the 64-bit twos-complement integer.

- but the wording could be improved/simplified, e.g.:

The varint consists of 0-8 bytes which have the high-order bit set followed by a single byte with the high-order bit clear. The lower seven bits of each byte are used to reconstruct the 64-bit twos-complement integer.

(2) By Richard Hipp (drh) on 2020-06-10 14:35:42 in reply to 1 [link] [source]

You seem to be comparing the definition of Varint for SQLite4 against the implementation of Varint for SQLite3.

The Varint definition for SQLite3 is here: https://www.sqlite.org/fileformat2.html#varint

(3) By anonymous on 2020-06-10 15:17:41 in reply to 2 [link] [source]

I see, thanks - https://sqlite.org/src4/doc/trunk/www/varint.wiki is the first result in google for "sqlite varint", I didn't notice "4" there.