SQLite Forum

Any document for varint functions such as sqlite3PutVarint and sqlite3GetVarint
Login

Any document for varint functions such as sqlite3PutVarint and sqlite3GetVarint

(1) By alanccw on 2020-07-24 06:17:59 [link] [source]

Hi,

Are there any document for the varint related functions, such as:

sqlite3PutVarint

sqlite3GetVarint

sqlite3GetVarint32

sqlite3VarintLen

and macros such as:

getVarint32

getVarint32NR

putVarint32

I search on the whole site but cannot find any.

Thanks

(2) By Rowan Worth (sqweek) on 2020-07-24 06:59:51 in reply to 1 [link] [source]

The varint encoding is described in the file format documentation: https://www.sqlite.org/fileformat.html

A variable-length integer or "varint" is a static Huffman encoding of 64-bit twos-complement integers that uses less space for small positive values. A varint is between 1 and 9 bytes in length. 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. Varints are big-endian: bits taken from the earlier byte of the varint are more significant than bits taken from the later bytes.

(4) By alanccw on 2020-07-25 00:00:22 in reply to 2 [source]

Hi, Rowan,

Thank you. I have alrady read the varint document in file format spec. I want to utilize the sqlite3PutVarint and sqlite3GetVarint functions to write & read varint so that I do not need to write my own.

(5) By Larry Brasfield (LarryBrasfield) on 2020-07-25 02:12:58 in reply to 4 [link] [source]

I wonder how you plan to alter the SQLite source or build so that definitions such as SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){ if( v<=0x7f ){ p[0] = v&0x7f; return 1; } if( v<=0x3fff ){ p[0] = ((v>>7)&0x7f)|0x80; p[1] = v&0x7f; return 2; } return putVarint64(p,v); } are give scope beyond the sqlite3.c compilation unit.

Those private function do not amount to much, so simply lifting their definitions would cost little code space and immunize your application to refactoring that might alter what they do, their signatures, or their existence.

(3) By Larry Brasfield (LarryBrasfield) on 2020-07-24 09:56:16 in reply to 1 [link] [source]

If they are not listed among the documented API functions, they are not part of the API. You may notice that all of the documented, published functions that comprise the API function set have names with a prefix, "sqlite3_". The fact that those undocumented functions do not have that prefix is another clue that they should not be relied upon by SQLite library users because they are subject to change as the implementation of the API evolves. Yet another clue is that they do not have external linkage.

If you wish to see what they do, today (but perhaps not tomorrow), you can find a link to the source within the site documentation, within which the code is as documented as the SQLite developers consider efficient for their purposes (but perhaps not yours.)