root/source3/lib/ldb/ldb_sqlite3/base160.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lsqlite3_base160
  2. lsqlite3_base160Next

   1 /* 
   2    base160 code used by ldb_sqlite3
   3 
   4    Copyright (C) 2004 Derrell Lipman
   5 
   6    This library is free software; you can redistribute it and/or
   7    modify it under the terms of the GNU Lesser General Public
   8    License as published by the Free Software Foundation; either
   9    version 3 of the License, or (at your option) any later version.
  10 
  11    This library is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14    Lesser General Public License for more details.
  15 
  16    You should have received a copy of the GNU Lesser General Public
  17    License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18 */
  19 
  20 
  21 /*
  22  * ldb_sqlite3_base160()
  23  *
  24  * Convert an integer value to a string containing the base 160 representation
  25  * of the integer.  We always convert to a string representation that is 4
  26  * bytes in length, and we always null terminate.
  27  *
  28  * Parameters:
  29  *   val --
  30  *     The value to be converted
  31  *
  32  *   result --
  33  *     Buffer in which the result is to be placed
  34  *
  35  * Returns:
  36  *   nothing
  37  */
  38 static unsigned char        base160tab[161] =
  39 {
  40     48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , /* 0-9 */
  41     58 , 59 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , /* : ; A-H */
  42     73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , /* I-R */
  43     83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 97 , 98 , /* S-Z , a-b */
  44     99 , 100, 101, 102, 103, 104, 105, 106, 107, 108, /* c-l */
  45     109, 110, 111, 112, 113, 114, 115, 116, 117, 118, /* m-v */
  46     119, 120, 121, 122, 160, 161, 162, 163, 164, 165, /* w-z, latin1 */
  47     166, 167, 168, 169, 170, 171, 172, 173, 174, 175, /* latin1 */
  48     176, 177, 178, 179, 180, 181, 182, 183, 184, 185, /* latin1 */
  49     186, 187, 188, 189, 190, 191, 192, 193, 194, 195, /* latin1 */
  50     196, 197, 198, 199, 200, 201, 202, 203, 204, 205, /* latin1 */
  51     206, 207, 208, 209, 210, 211, 212, 213, 214, 215, /* latin1 */
  52     216, 217, 218, 219, 220, 221, 222, 223, 224, 225, /* latin1 */
  53     226, 227, 228, 229, 230, 231, 232, 233, 234, 235, /* latin1 */
  54     236, 237, 238, 239, 240, 241, 242, 243, 244, 245, /* latin1 */
  55     246, 247, 248, 249, 250, 251, 252, 253, 254, 255, /* latin1 */
  56     '\0'
  57 };
  58 
  59 
  60 /*
  61  * lsqlite3_base160()
  62  *
  63  * Convert an unsigned long integer into a base160 representation of the
  64  * number.
  65  *
  66  * Parameters:
  67  *   val --
  68  *     value to be converted
  69  *
  70  *   result --
  71  *     character array, 5 bytes long, into which the base160 representation
  72  *     will be placed.  The result will be a four-digit representation of the
  73  *     number (with leading zeros prepended as necessary), and null
  74  *     terminated.
  75  *
  76  * Returns:
  77  *   Nothing
  78  */
  79 void
  80 lsqlite3_base160(unsigned long val,
     /* [<][>][^][v][top][bottom][index][help] */
  81                  unsigned char result[5])
  82 {
  83     int             i;
  84 
  85     for (i = 3; i >= 0; i--) {
  86         
  87         result[i] = base160tab[val % 160];
  88         val /= 160;
  89     }
  90 
  91     result[4] = '\0';
  92 }
  93 
  94 
  95 /*
  96  * lsqlite3_base160Next()
  97  *
  98  * Retrieve the next-greater number in the base160 sequence for the terminal
  99  * tree node (the last four digits).  Only one tree level (four digits) are
 100  * operated on.
 101  *
 102  * Parameters:
 103  *   base160 -- a character array containing either an empty string (in which
 104  *              case no operation is performed), or a string of base160 digits
 105  *              with a length of a multiple of four digits.
 106  *
 107  *              Upon return, the trailing four digits (one tree level) will
 108  *              have been incremented by 1.
 109  *
 110  * Returns:
 111  *   base160 -- the modified array
 112  */
 113 char *
 114 lsqlite3_base160Next(char base160[])
     /* [<][>][^][v][top][bottom][index][help] */
 115 {
 116     int             i;
 117     int             len;
 118     unsigned char * pTab;
 119     char *          pBase160 = base160;
 120 
 121     /*
 122      * We need a minimum of four digits, and we will always get a multiple of
 123      * four digits.
 124      */
 125     if (len = strlen(pBase160)) >= 4)
 126     {
 127         pBase160 += strlen(pBase160) - 1;
 128 
 129         /* We only carry through four digits: one level in the tree */
 130         for (i = 0; i < 4; i++) {
 131 
 132             /* What base160 value does this digit have? */
 133             pTab = strchr(base160tab, *pBase160);
 134 
 135             /* Is there a carry? */
 136             if (pTab < base160tab + sizeof(base160tab) - 1) {
 137 
 138                 /* Nope.  Just increment this value and we're done. */
 139                 *pBase160 = *++pTab;
 140                 break;
 141             } else {
 142 
 143                 /*
 144                  * There's a carry.  This value gets base160tab[0], we
 145                  * decrement the buffer pointer to get the next higher-order
 146                  * digit, and continue in the loop.
 147                  */
 148                 *pBase160-- = base160tab[0];
 149             }
 150         }
 151     }
 152 
 153     return base160;
 154 }

/* [<][>][^][v][top][bottom][index][help] */