chiark / gitweb /
Commit 2.4.5-5 as unpacked
[inn-innduct.git] / lib / radix32.c
1 /*  $Id: radix32.c 6118 2003-01-13 06:44:24Z rra $
2 **
3 **  Radix-32 strings divide a number into five-bit nibbles and use the
4 **  alphabet 0..9a..v to represent 0..32.
5 */
6
7 #include "config.h"
8 #include "clibrary.h"
9 #include <time.h>
10
11 #include "libinn.h"
12
13
14 static char     ALPHABET[] =
15     "0123456789abcdefghijklmnopqrstuv";
16
17
18 /*
19 **  Turn a number into a Radix-32 string.  Assume the number fits into
20 **  32 bits.
21 */
22 void Radix32(unsigned long l, char *buff)
23 {
24     char                        *p;
25     int                         i;
26     char                        temp[10];
27
28     /* Simple sanity checks. */
29     if ((l &= 0xFFFFFFFFL) == 0) {
30         *buff++ = ALPHABET[0];
31         *buff = '\0';
32         return;
33     }
34
35     /* Format the string, in reverse. */
36     for (p = temp; l; l >>= 5)
37         *p++ = ALPHABET[(int)(l & 037)];
38
39     /* Reverse it. */
40     for (i = p - temp; --i >= 0; )
41         *buff++ = *--p;
42     *buff = '\0';
43 }
44
45
46 #if     0
47 /*
48 **  Return a Radix-32 string as a number, or ~0 on error.
49 */
50 unsigned long
51 Decode32(p)
52     char                *p;
53 {
54     unsigned long       l;
55     char                *cp;
56
57     for (l = 0; *p; p++) {
58         if ((cp = strchr(ALPHABET, *p)) == NULL)
59             return ~0;
60         l = (l << 6) + cp - ALPHABET;
61     }
62     return l;
63 }
64 #endif  /* 0 */