]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * base64.c: base64 functions | |
3 | */ | |
4 | ||
5 | #include "base64.h" | |
6 | #include <assert.h> | |
7 | ||
8 | int numerictab[] = { | |
9 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
10 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
11 | 0, 0, 0, 0, 0, 0, 0, 0, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, | |
12 | 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
13 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 62, 0, 63, 0, 0, 0, 26, 27, 28, | |
14 | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, | |
15 | 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
16 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
17 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
18 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
19 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
20 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
21 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
22 | }; | |
23 | ||
24 | char tokens[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]"; | |
25 | ||
1fbb1306 | 26 | INLINE long numerictolong(const char *numeric, int numericlen) |
c86edd1d Q |
27 | { |
28 | long mynumeric=0; | |
29 | int i; | |
30 | ||
31 | for (i=0;i<numericlen;i++) { | |
32 | mynumeric=(mynumeric << 6)+numerictab[(int) *(numeric++)]; | |
33 | } | |
34 | ||
35 | return mynumeric; | |
36 | } | |
c86edd1d Q |
37 | |
38 | char *longtonumeric(long param, int len) | |
39 | { | |
40 | static char mynum[7]; /* Static buffers rock. Multi-thread at your peril */ | |
41 | int i; | |
42 | ||
43 | /* To go with our marvellous static buffer we | |
44 | * have this rather groovy length limit. */ | |
45 | assert(len<=6); | |
46 | ||
47 | for (i=len-1;i>=0;i--) { | |
48 | mynum[i] = tokens[(param % 64)]; | |
49 | param /= 64; | |
50 | } | |
51 | mynum[len] = '\0'; | |
52 | ||
53 | return (mynum); | |
54 | } | |
55 | ||
56 | /* Slightly more sane version of the above */ | |
57 | ||
58 | char *longtonumeric2(long param, int len, char *mynum) | |
59 | { | |
60 | int i; | |
61 | ||
62 | for (i=len-1;i>=0;i--) { | |
63 | mynum[i] = tokens[(param % 64)]; | |
64 | param /= 64; | |
65 | } | |
66 | mynum[len] = '\0'; | |
67 | ||
68 | return (mynum); | |
69 | } | |
70 |