Add the "varint.c" utility program in the tool directory.

FossilOrigin-Name: ea5d56be5fe14934e4dbe9c17d46b058f487a231
This commit is contained in:
drh 2014-10-31 10:31:59 +00:00
parent 768f29002e
commit 9c0153457a
3 changed files with 130 additions and 6 deletions

View File

@ -1,5 +1,5 @@
C Remove\san\sunnecessary\sbranch\sfrom\sbalance_nonroot().
D 2014-10-31T02:51:41.215
C Add\sthe\s"varint.c"\sutility\sprogram\sin\sthe\stool\sdirectory.
D 2014-10-31T10:31:59.052
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in cf57f673d77606ab0f2d9627ca52a9ba1464146a
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -1204,12 +1204,13 @@ F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43
F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d
F tool/symbols.sh fec58532668296d7c7dc48be9c87f75ccdb5814f
F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
F tool/varint.c 5d94cb5003db9dbbcbcc5df08d66f16071aee003
F tool/vdbe-compress.tcl 5926c71f9c12d2ab73ef35c29376e756eb68361c
F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 69c3924fe834a78d4a8d86833626bf5f68e33a3a
R 85b30d88694d9feacf26ae3f80208908
P 9fc7c88e3f5221883aa6eafbf8af3be94db0c299
R 932c8189456e662bc00860a65a53ebcf
U drh
Z 7bc5c33b6cb165c738435440761728db
Z 653e5ed37198e17f610045be3bfdae23

View File

@ -1 +1 @@
9fc7c88e3f5221883aa6eafbf8af3be94db0c299
ea5d56be5fe14934e4dbe9c17d46b058f487a231

123
tool/varint.c Normal file
View File

@ -0,0 +1,123 @@
/*
** A utility program to translate SQLite varints into decimal and decimal
** integers into varints.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 i64;
typedef unsigned __int64 u64;
#else
typedef long long int i64;
typedef unsigned long long int u64;
#endif
static int hexValue(char c){
if( c>='0' && c<='9' ) return c - '0';
if( c>='a' && c<='f' ) return c - 'a' + 10;
if( c>='A' && c<='F' ) return c - 'A' + 10;
return -1;
}
static char toHex(unsigned char c){
return "0123456789abcdef"[c&0xf];
}
static int putVarint(unsigned char *p, u64 v){
int i, j, n;
unsigned char buf[10];
if( v & (((u64)0xff000000)<<32) ){
p[8] = (unsigned char)v;
v >>= 8;
for(i=7; i>=0; i--){
p[i] = (unsigned char)((v & 0x7f) | 0x80);
v >>= 7;
}
return 9;
}
n = 0;
do{
buf[n++] = (unsigned char)((v & 0x7f) | 0x80);
v >>= 7;
}while( v!=0 );
buf[0] &= 0x7f;
for(i=0, j=n-1; j>=0; j--, i++){
p[i] = buf[j];
}
return n;
}
int main(int argc, char **argv){
int i;
u64 x;
u64 uX = 0;
i64 iX;
int n;
unsigned char zHex[20];
if( argc==1 ){
fprintf(stderr,
"Usage:\n"
" %s HH HH HH ... Convert varint to decimal\n"
" %s DDDDD Convert decimal to varint\n"
" Add '+' or '-' before DDDDD to disambiguate.\n",
argv[0], argv[0]);
exit(1);
}
if( argc>2
|| (strlen(argv[1])==2 && hexValue(argv[1][0])>=0 && hexValue(argv[1][1])>=0)
){
/* Hex to decimal */
for(i=1; i<argc && i<9; i++){
if( strlen(argv[i])!=2 ){
fprintf(stderr, "Not a hex byte: %s\n", argv[i]);
exit(1);
}
x = (hexValue(argv[i][0])<<4) + hexValue(argv[i][1]);
uX = (uX<<7) + (x&0x7f);
if( (x&0x80)==0 ) break;
}
if( i==9 && i<argc ){
if( strlen(argv[i])!=2 ){
fprintf(stderr, "Not a hex byte: %s\n", argv[i]);
exit(1);
}
x = (hexValue(argv[i][0])<<4) + hexValue(argv[i][1]);
uX = (uX<<8) + x;
}
i++;
if( i<argc ){
fprintf(stderr, "Extra arguments: %s...\n", argv[i]);
exit(1);
}
}else{
char *z = argv[1];
int sign = 1;
if( z[0]=='+' ) z++;
else if( z[0]=='-' ){ z++; sign = -1; }
uX = 0;
while( z[0] ){
if( z[0]<'0' || z[0]>'9' ){
fprintf(stderr, "Not a decimal number: %s", argv[1]);
exit(1);
}
uX = uX*10 + z[0] - '0';
z++;
}
if( sign<0 ){
memcpy(&iX, &uX, 8);
iX = -iX;
memcpy(&uX, &iX, 8);
}
}
n = putVarint(zHex, uX);
printf("%lld =", (i64)uX);
for(i=0; i<n; i++){
printf(" %c%c", toHex(zHex[i]>>4), toHex(zHex[i]&0x0f));
}
printf("\n");
return 0;
}