5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
12 *************************************************************************
14 ** SQLite4-compatible varint implementation.
18 /*************************************************************************
19 ** The following is a copy of the varint.c module from SQLite 4.
23 ** Decode the varint in z[]. Write the integer value into *pResult and
24 ** return the number of bytes in the varint.
26 static int lsmSqlite4GetVarint64(const unsigned char *z
, u64
*pResult
){
33 *pResult
= (z
[0]-241)*256 + z
[1] + 240;
37 *pResult
= 2288 + 256*z
[1] + z
[2];
41 *pResult
= (z
[1]<<16) + (z
[2]<<8) + z
[3];
44 x
= (z
[1]<<24) + (z
[2]<<16) + (z
[3]<<8) + z
[4];
50 *pResult
= (((u64
)x
)<<8) + z
[5];
54 *pResult
= (((u64
)x
)<<16) + (z
[5]<<8) + z
[6];
58 *pResult
= (((u64
)x
)<<24) + (z
[5]<<16) + (z
[6]<<8) + z
[7];
61 *pResult
= (((u64
)x
)<<32) +
62 (0xffffffff & ((z
[5]<<24) + (z
[6]<<16) + (z
[7]<<8) + z
[8]));
67 ** Write a 32-bit unsigned integer as 4 big-endian bytes.
69 static void lsmVarintWrite32(unsigned char *z
, unsigned int y
){
70 z
[0] = (unsigned char)(y
>>24);
71 z
[1] = (unsigned char)(y
>>16);
72 z
[2] = (unsigned char)(y
>>8);
73 z
[3] = (unsigned char)(y
);
77 ** Write a varint into z[]. The buffer z[] must be at least 9 characters
78 ** long to accommodate the largest possible varint. Return the number of
81 static int lsmSqlite4PutVarint64(unsigned char *z
, u64 x
){
84 z
[0] = (unsigned char)x
;
88 y
= (unsigned int)(x
- 240);
89 z
[0] = (unsigned char)(y
/256 + 241);
90 z
[1] = (unsigned char)(y
%256);
94 y
= (unsigned int)(x
- 2288);
96 z
[1] = (unsigned char)(y
/256);
97 z
[2] = (unsigned char)(y
%256);
101 w
= (unsigned int)(x
>>32);
105 z
[1] = (unsigned char)(y
>>16);
106 z
[2] = (unsigned char)(y
>>8);
107 z
[3] = (unsigned char)(y
);
111 lsmVarintWrite32(z
+1, y
);
116 z
[1] = (unsigned char)w
;
117 lsmVarintWrite32(z
+2, y
);
122 z
[1] = (unsigned char)(w
>>8);
123 z
[2] = (unsigned char)w
;
124 lsmVarintWrite32(z
+3, y
);
129 z
[1] = (unsigned char)(w
>>16);
130 z
[2] = (unsigned char)(w
>>8);
131 z
[3] = (unsigned char)w
;
132 lsmVarintWrite32(z
+4, y
);
136 lsmVarintWrite32(z
+1, w
);
137 lsmVarintWrite32(z
+5, y
);
142 ** End of SQLite 4 code.
143 *************************************************************************/
145 int lsmVarintPut64(u8
*aData
, i64 iVal
){
146 return lsmSqlite4PutVarint64(aData
, (u64
)iVal
);
149 int lsmVarintGet64(const u8
*aData
, i64
*piVal
){
150 return lsmSqlite4GetVarint64(aData
, (u64
*)piVal
);
153 int lsmVarintPut32(u8
*aData
, int iVal
){
154 return lsmSqlite4PutVarint64(aData
, (u64
)iVal
);
157 int lsmVarintGet32(u8
*z
, int *piVal
){
166 *piVal
= (z
[0]-241)*256 + z
[1] + 240;
170 *piVal
= 2288 + 256*z
[1] + z
[2];
174 *piVal
= (z
[1]<<16) + (z
[2]<<8) + z
[3];
178 ret
= lsmSqlite4GetVarint64(z
, &i
);
183 int lsmVarintLen32(int n
){
185 return lsmVarintPut32(aData
, n
);
189 ** The argument is the first byte of a varint. This function returns the
190 ** total number of bytes in the entire varint (including the first byte).
192 int lsmVarintSize(u8 c
){
193 if( c
<241 ) return 1;
194 if( c
<249 ) return 2;
195 return (int)(c
- 246);