lsm_varint.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. ** 2012-02-08
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  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.
  10. **
  11. *************************************************************************
  12. **
  13. ** SQLite4-compatible varint implementation.
  14. */
  15. #include "lsmInt.h"
  16. /*************************************************************************
  17. ** The following is a copy of the varint.c module from SQLite 4.
  18. */
  19. /*
  20. ** Decode the varint in z[]. Write the integer value into *pResult and
  21. ** return the number of bytes in the varint.
  22. */
  23. static int lsmSqlite4GetVarint64(const unsigned char *z, u64 *pResult){
  24. unsigned int x;
  25. if( z[0]<=240 ){
  26. *pResult = z[0];
  27. return 1;
  28. }
  29. if( z[0]<=248 ){
  30. *pResult = (z[0]-241)*256 + z[1] + 240;
  31. return 2;
  32. }
  33. if( z[0]==249 ){
  34. *pResult = 2288 + 256*z[1] + z[2];
  35. return 3;
  36. }
  37. if( z[0]==250 ){
  38. *pResult = (z[1]<<16) + (z[2]<<8) + z[3];
  39. return 4;
  40. }
  41. x = (z[1]<<24) + (z[2]<<16) + (z[3]<<8) + z[4];
  42. if( z[0]==251 ){
  43. *pResult = x;
  44. return 5;
  45. }
  46. if( z[0]==252 ){
  47. *pResult = (((u64)x)<<8) + z[5];
  48. return 6;
  49. }
  50. if( z[0]==253 ){
  51. *pResult = (((u64)x)<<16) + (z[5]<<8) + z[6];
  52. return 7;
  53. }
  54. if( z[0]==254 ){
  55. *pResult = (((u64)x)<<24) + (z[5]<<16) + (z[6]<<8) + z[7];
  56. return 8;
  57. }
  58. *pResult = (((u64)x)<<32) +
  59. (0xffffffff & ((z[5]<<24) + (z[6]<<16) + (z[7]<<8) + z[8]));
  60. return 9;
  61. }
  62. /*
  63. ** Write a 32-bit unsigned integer as 4 big-endian bytes.
  64. */
  65. static void lsmVarintWrite32(unsigned char *z, unsigned int y){
  66. z[0] = (unsigned char)(y>>24);
  67. z[1] = (unsigned char)(y>>16);
  68. z[2] = (unsigned char)(y>>8);
  69. z[3] = (unsigned char)(y);
  70. }
  71. /*
  72. ** Write a varint into z[]. The buffer z[] must be at least 9 characters
  73. ** long to accommodate the largest possible varint. Return the number of
  74. ** bytes of z[] used.
  75. */
  76. static int lsmSqlite4PutVarint64(unsigned char *z, u64 x){
  77. unsigned int w, y;
  78. if( x<=240 ){
  79. z[0] = (unsigned char)x;
  80. return 1;
  81. }
  82. if( x<=2287 ){
  83. y = (unsigned int)(x - 240);
  84. z[0] = (unsigned char)(y/256 + 241);
  85. z[1] = (unsigned char)(y%256);
  86. return 2;
  87. }
  88. if( x<=67823 ){
  89. y = (unsigned int)(x - 2288);
  90. z[0] = 249;
  91. z[1] = (unsigned char)(y/256);
  92. z[2] = (unsigned char)(y%256);
  93. return 3;
  94. }
  95. y = (unsigned int)x;
  96. w = (unsigned int)(x>>32);
  97. if( w==0 ){
  98. if( y<=16777215 ){
  99. z[0] = 250;
  100. z[1] = (unsigned char)(y>>16);
  101. z[2] = (unsigned char)(y>>8);
  102. z[3] = (unsigned char)(y);
  103. return 4;
  104. }
  105. z[0] = 251;
  106. lsmVarintWrite32(z+1, y);
  107. return 5;
  108. }
  109. if( w<=255 ){
  110. z[0] = 252;
  111. z[1] = (unsigned char)w;
  112. lsmVarintWrite32(z+2, y);
  113. return 6;
  114. }
  115. if( w<=32767 ){
  116. z[0] = 253;
  117. z[1] = (unsigned char)(w>>8);
  118. z[2] = (unsigned char)w;
  119. lsmVarintWrite32(z+3, y);
  120. return 7;
  121. }
  122. if( w<=16777215 ){
  123. z[0] = 254;
  124. z[1] = (unsigned char)(w>>16);
  125. z[2] = (unsigned char)(w>>8);
  126. z[3] = (unsigned char)w;
  127. lsmVarintWrite32(z+4, y);
  128. return 8;
  129. }
  130. z[0] = 255;
  131. lsmVarintWrite32(z+1, w);
  132. lsmVarintWrite32(z+5, y);
  133. return 9;
  134. }
  135. /*
  136. ** End of SQLite 4 code.
  137. *************************************************************************/
  138. int lsmVarintPut64(u8 *aData, i64 iVal){
  139. return lsmSqlite4PutVarint64(aData, (u64)iVal);
  140. }
  141. int lsmVarintGet64(const u8 *aData, i64 *piVal){
  142. return lsmSqlite4GetVarint64(aData, (u64 *)piVal);
  143. }
  144. int lsmVarintPut32(u8 *aData, int iVal){
  145. return lsmSqlite4PutVarint64(aData, (u64)iVal);
  146. }
  147. int lsmVarintGet32(u8 *z, int *piVal){
  148. u64 i;
  149. int ret;
  150. if( z[0]<=240 ){
  151. *piVal = z[0];
  152. return 1;
  153. }
  154. if( z[0]<=248 ){
  155. *piVal = (z[0]-241)*256 + z[1] + 240;
  156. return 2;
  157. }
  158. if( z[0]==249 ){
  159. *piVal = 2288 + 256*z[1] + z[2];
  160. return 3;
  161. }
  162. if( z[0]==250 ){
  163. *piVal = (z[1]<<16) + (z[2]<<8) + z[3];
  164. return 4;
  165. }
  166. ret = lsmSqlite4GetVarint64(z, &i);
  167. *piVal = (int)i;
  168. return ret;
  169. }
  170. int lsmVarintLen32(int n){
  171. u8 aData[9];
  172. return lsmVarintPut32(aData, n);
  173. }
  174. int lsmVarintLen64(i64 n){
  175. u8 aData[9];
  176. return lsmVarintPut64(aData, n);
  177. }
  178. /*
  179. ** The argument is the first byte of a varint. This function returns the
  180. ** total number of bytes in the entire varint (including the first byte).
  181. */
  182. int lsmVarintSize(u8 c){
  183. if( c<241 ) return 1;
  184. if( c<249 ) return 2;
  185. return (int)(c - 246);
  186. }