util-warning-cleanup-20011005
[openafs.git] / src / util / base64.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15
16 #ifdef AFS_SGI_XFS_IOPS_ENV
17
18 static char c_xlate[80] =
19         "+,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20
21 /* int_to_base64
22  * Create a base 64 string representation of a number.
23  * The supplied string 's' must be at least 8 bytes long.
24  * b64_string in stds.h provides a tyepdef to get the length.
25  */
26 char *int_to_base64(b64_string_t s, int a)
27 {
28     int i, j;
29     unsigned int n;
30
31     i = 0;
32     if (a==0)
33         s[i++] = c_xlate[0];
34     else {
35         j = 24;
36         n = a & 0xc0000000;
37         if (n) {
38             n >>= 30;
39             s[i++] = c_xlate[n];
40             a &= ~0xc0000000;
41         }
42         else {
43             for (; j>=0; j-=6) {
44                 n = a & (0x3f << j);
45                 if (n)
46                     break; /* found highest bits set. */
47             }
48             s[i++] = c_xlate[n>>j];
49             a &= ~(0x3f << j);
50             j -=6;
51         }
52         /* more to do. */
53         for (; j>=0; j-=6) {
54             n = a & (0x3f << j);
55             s[i++] = c_xlate[n>>j];
56             a &= ~(0x3f << j);
57         }
58     }
59     s[i] = '\0';
60     return s;
61 }
62
63 /* Mapping: +=0, ,=1, 0-9 = 2-11, A-Z = 12-37, a-z = 38-63 */
64 int base64_to_int(char *s)
65 {
66     int n = 0;
67     int result = 0;
68     unsigned char *p;
69
70     for (; *s; s++) {
71         if (*s < '0') {
72             n = *s - '+';
73         }
74         else if (*s <= '9') {
75             n = 2 + (int)(*s - '0');
76         }
77         else if (*s <= 'Z') {
78             n = 12 + (int)(*s - 'A');
79         }
80         else {
81             n = 38 + (int)(*s - 'a');
82         }
83         result = (result << 6) + n;
84     }
85     return result;
86 }
87
88
89 #endif /* AFS_SGI_XFS_IOPS_ENV */