varint.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. ** A utility program to translate SQLite varints into decimal and decimal
  3. ** integers into varints.
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #if defined(_MSC_VER) || defined(__BORLANDC__)
  9. typedef __int64 i64;
  10. typedef unsigned __int64 u64;
  11. #else
  12. typedef long long int i64;
  13. typedef unsigned long long int u64;
  14. #endif
  15. static int hexValue(char c){
  16. if( c>='0' && c<='9' ) return c - '0';
  17. if( c>='a' && c<='f' ) return c - 'a' + 10;
  18. if( c>='A' && c<='F' ) return c - 'A' + 10;
  19. return -1;
  20. }
  21. static char toHex(unsigned char c){
  22. return "0123456789abcdef"[c&0xf];
  23. }
  24. static int putVarint(unsigned char *p, u64 v){
  25. int i, j, n;
  26. unsigned char buf[10];
  27. if( v & (((u64)0xff000000)<<32) ){
  28. p[8] = (unsigned char)v;
  29. v >>= 8;
  30. for(i=7; i>=0; i--){
  31. p[i] = (unsigned char)((v & 0x7f) | 0x80);
  32. v >>= 7;
  33. }
  34. return 9;
  35. }
  36. n = 0;
  37. do{
  38. buf[n++] = (unsigned char)((v & 0x7f) | 0x80);
  39. v >>= 7;
  40. }while( v!=0 );
  41. buf[0] &= 0x7f;
  42. for(i=0, j=n-1; j>=0; j--, i++){
  43. p[i] = buf[j];
  44. }
  45. return n;
  46. }
  47. int main(int argc, char **argv){
  48. int i;
  49. u64 x;
  50. u64 uX = 0;
  51. i64 iX;
  52. int n;
  53. unsigned char zHex[20];
  54. if( argc==1 ){
  55. fprintf(stderr,
  56. "Usage:\n"
  57. " %s HH HH HH ... Convert varint to decimal\n"
  58. " %s DDDDD Convert decimal to varint\n"
  59. " Add '+' or '-' before DDDDD to disambiguate.\n",
  60. argv[0], argv[0]);
  61. exit(1);
  62. }
  63. if( argc>2
  64. || (strlen(argv[1])==2 && hexValue(argv[1][0])>=0 && hexValue(argv[1][1])>=0)
  65. ){
  66. /* Hex to decimal */
  67. for(i=1; i<argc && i<9; i++){
  68. if( strlen(argv[i])!=2 ){
  69. fprintf(stderr, "Not a hex byte: %s\n", argv[i]);
  70. exit(1);
  71. }
  72. x = (hexValue(argv[i][0])<<4) + hexValue(argv[i][1]);
  73. uX = (uX<<7) + (x&0x7f);
  74. if( (x&0x80)==0 ) break;
  75. }
  76. if( i==9 && i<argc ){
  77. if( strlen(argv[i])!=2 ){
  78. fprintf(stderr, "Not a hex byte: %s\n", argv[i]);
  79. exit(1);
  80. }
  81. x = (hexValue(argv[i][0])<<4) + hexValue(argv[i][1]);
  82. uX = (uX<<8) + x;
  83. }
  84. i++;
  85. if( i<argc ){
  86. fprintf(stderr, "Extra arguments: %s...\n", argv[i]);
  87. exit(1);
  88. }
  89. }else{
  90. char *z = argv[1];
  91. int sign = 1;
  92. if( z[0]=='+' ) z++;
  93. else if( z[0]=='-' ){ z++; sign = -1; }
  94. uX = 0;
  95. while( z[0] ){
  96. if( z[0]<'0' || z[0]>'9' ){
  97. fprintf(stderr, "Not a decimal number: %s", argv[1]);
  98. exit(1);
  99. }
  100. uX = uX*10 + z[0] - '0';
  101. z++;
  102. }
  103. if( sign<0 ){
  104. memcpy(&iX, &uX, 8);
  105. iX = -iX;
  106. memcpy(&uX, &iX, 8);
  107. }
  108. }
  109. n = putVarint(zHex, uX);
  110. printf("%lld =", (i64)uX);
  111. for(i=0; i<n; i++){
  112. printf(" %c%c", toHex(zHex[i]>>4), toHex(zHex[i]&0x0f));
  113. }
  114. printf("\n");
  115. return 0;
  116. }