• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Write a 64-bit variable-length integer to memory starting at p[0].
3 ** The length of data write will be between 1 and 9 bytes.  The number
4 ** of bytes written is returned.
5 **
6 ** A variable-length integer consists of the lower 7 bits of each byte
7 ** for all bytes that have the 8th bit set and one byte with the 8th
8 ** bit clear.  Except, if we get to the 9th byte, it stores the full
9 ** 8 bits and is the last byte.
10 */
sqlite3PutVarint(unsigned char * p,u64 v)11 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
12   int i, j, n;
13   u8 buf[10];
14   if( v & (((u64)0xff000000)<<32) ){
15     p[8] = v;
16     v >>= 8;
17     for(i=7; i>=0; i--){
18       p[i] = (v & 0x7f) | 0x80;
19       v >>= 7;
20     }
21     return 9;
22   }
23   n = 0;
24   do{
25     buf[n++] = (v & 0x7f) | 0x80;
26     v >>= 7;
27   }while( v!=0 );
28   buf[0] &= 0x7f;
29   assert( n<=9 );
30   for(i=0, j=n-1; j>=0; j--, i++){
31     p[i] = buf[j];
32   }
33   return n;
34 }
35 
36