4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** Routines for varint serialization and deserialization.
20 ** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
21 ** Except, this version does handle the single byte case that the core
22 ** version depends on being handled before its function is called.
24 int sqlite3Fts5GetVarint32(const unsigned char *p
, u32
*v
){
27 /* The 1-byte case. Overwhelmingly the most common. */
29 /* a: p0 (unmasked) */
32 /* Values between 0 and 127 */
40 /* b: p1 (unmasked) */
43 /* Values between 128 and 16383 */
54 /* a: p0<<14 | p2 (unmasked) */
57 /* Values between 16384 and 2097151 */
58 a
&= (0x7f<<14)|(0x7f);
65 /* A 32-bit varint is used to store size information in btrees.
66 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
67 ** A 3-byte varint is sufficient, for example, to record the size
68 ** of a 1048569-byte BLOB or string.
70 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
71 ** rare larger cases can be handled by the slower 64-bit varint
78 n
= sqlite3Fts5GetVarint(p
, &v64
);
79 *v
= ((u32
)v64
) & 0x7FFFFFFF;
80 assert( n
>3 && n
<=9 );
87 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants
88 ** are defined here rather than simply putting the constant expressions
89 ** inline in order to work around bugs in the RVT compiler.
91 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
93 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
95 #define SLOT_2_0 0x001fc07f
96 #define SLOT_4_2_0 0xf01fc07f
99 ** Read a 64-bit variable-length integer from memory starting at p[0].
100 ** Return the number of bytes read. The value is stored in *v.
102 u8
sqlite3Fts5GetVarint(const unsigned char *p
, u64
*v
){
106 /* a: p0 (unmasked) */
115 /* b: p1 (unmasked) */
125 /* Verify that constants are precomputed correctly */
126 assert( SLOT_2_0
== ((0x7f<<14) | (0x7f)) );
127 assert( SLOT_4_2_0
== ((0xfU
<<28) | (0x7f<<14) | (0x7f)) );
132 /* a: p0<<14 | p2 (unmasked) */
143 /* CSE1 from below */
148 /* b: p1<<14 | p3 (unmasked) */
153 /* a &= (0x7f<<14)|(0x7f); */
160 /* a: p0<<14 | p2 (masked) */
161 /* b: p1<<14 | p3 (unmasked) */
162 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
164 /* a &= (0x7f<<14)|(0x7f); */
167 /* s: p0<<14 | p2 (masked) */
172 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
175 /* we can skip these cause they were (effectively) done above in calc'ing s */
176 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
177 /* b &= (0x7f<<14)|(0x7f); */
181 *v
= ((u64
)s
)<<32 | a
;
185 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
188 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
193 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
196 /* we can skip this cause it was (effectively) done above in calc'ing s */
197 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
202 *v
= ((u64
)s
)<<32 | a
;
209 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
217 *v
= ((u64
)s
)<<32 | a
;
221 /* CSE2 from below */
226 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
231 /* a &= (0x7f<<14)|(0x7f); */
235 *v
= ((u64
)s
)<<32 | a
;
242 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
245 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
256 *v
= ((u64
)s
)<<32 | a
;
262 ** The variable-length integer encoding is as follows:
265 ** A = 0xxxxxxx 7 bits of data and one flag bit
266 ** B = 1xxxxxxx 7 bits of data and one flag bit
267 ** C = xxxxxxxx 8 bits of data
276 ** 56 bits - BBBBBBBA
277 ** 64 bits - BBBBBBBBC
280 #ifdef SQLITE_NOINLINE
281 # define FTS5_NOINLINE SQLITE_NOINLINE
283 # define FTS5_NOINLINE
287 ** Write a 64-bit variable-length integer to memory starting at p[0].
288 ** The length of data write will be between 1 and 9 bytes. The number
289 ** of bytes written is returned.
291 ** A variable-length integer consists of the lower 7 bits of each byte
292 ** for all bytes that have the 8th bit set and one byte with the 8th
293 ** bit clear. Except, if we get to the 9th byte, it stores the full
294 ** 8 bits and is the last byte.
296 static int FTS5_NOINLINE
fts5PutVarint64(unsigned char *p
, u64 v
){
299 if( v
& (((u64
)0xff000000)<<32) ){
303 p
[i
] = (u8
)((v
& 0x7f) | 0x80);
310 buf
[n
++] = (u8
)((v
& 0x7f) | 0x80);
315 for(i
=0, j
=n
-1; j
>=0; j
--, i
++){
321 int sqlite3Fts5PutVarint(unsigned char *p
, u64 v
){
327 p
[0] = ((v
>>7)&0x7f)|0x80;
331 return fts5PutVarint64(p
,v
);
335 int sqlite3Fts5GetVarintLen(u32 iVal
){
337 if( iVal
<(1 << 7 ) ) return 1;
339 assert( iVal
>=(1 << 7) );
340 if( iVal
<(1 << 14) ) return 2;
341 if( iVal
<(1 << 21) ) return 3;
342 if( iVal
<(1 << 28) ) return 4;