reindent-20030715
[openafs.git] / src / util / flipbase64.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
14     ("$Header$");
15
16
17 #if defined(AFS_NAMEI_ENV)
18 #include <sys/types.h>
19 #include "afsutil.h"
20
21 /* This version of base64 gets it right and starts converting from the low
22  * bits to the high bits. 
23  */
24 /* This table needs to be in lexical order to efficiently map back from
25  * characters to the numerical value.
26  */
27 static char c_xlate[80] =
28     "+=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
29
30 /* int_to_base64
31  * Create a base 64 string representation of a number.
32  * The supplied string 's' must be at least 12 bytes long.
33  * lb64_string in stds.h provides a typedef to get the length.
34  */
35 #ifdef AFS_64BIT_ENV
36 char *
37 int64_to_flipbase64(lb64_string_t s, afs_int64 a)
38 #else
39 char *
40 int64_to_flipbase64(lb64_string_t s, u_int64_t a)
41 #endif
42 {
43     int i;
44 #ifdef AFS_64BIT_ENV
45     afs_int64 n;
46 #else
47     u_int64_t n;
48 #endif
49
50     i = 0;
51     if (a == 0)
52         s[i++] = c_xlate[0];
53     else {
54         for (n = a & 0x3f; a; n = ((a >>= 6) & 0x3f)) {
55             s[i++] = c_xlate[n];
56         }
57     }
58     s[i] = '\0';
59     return s;
60 }
61
62
63 /* Mapping: +=0, ==1, 0-9 = 2-11, A-Z = 12-37, a-z = 38-63 */
64 #ifdef AFS_64BIT_ENV
65 afs_int64
66 flipbase64_to_int64(char *s)
67 #else
68 int64_t
69 flipbase64_to_int64(char *s)
70 #endif
71 {
72 #ifdef AFS_64BIT_ENV
73     afs_int64 n = 0;
74     afs_int64 result = 0;
75 #else
76     int64_t n = 0;
77     int64_t result = 0;
78 #endif
79     int shift;
80
81     for (shift = 0; *s; s++, shift += 6) {
82         if (*s == '+')
83             n = 0;
84         else if (*s == '=')
85             n = 1;
86         else if (*s <= '9') {
87             n = 2 + (int)(*s - '0');
88         } else if (*s <= 'Z') {
89             n = 12 + (int)(*s - 'A');
90         } else if (*s <= 'z') {
91             n = 38 + (int)(*s - 'a');
92         }
93         n <<= shift;
94         result |= n;
95     }
96     return result;
97 }
98
99
100 #endif /* AFS_NAMEI_ENV */