util: Handle serverLogMutex lock across forks
[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  * Portions Copyright (c) 2003 Apple Computer, Inc.
10  */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #include <roken.h>
16
17 #include "afsutil.h"
18
19 /* This version of base64 gets it right and starts converting from the low
20  * bits to the high bits.
21  */
22 /* This table needs to be in lexical order to efficiently map back from
23  * characters to the numerical value.
24  *
25  * In c_reverse, we use 99 to represent an illegal value, rather than -1
26  * which would assume "char" is signed.
27  */
28 #ifdef AFS_DARWIN_ENV
29 static char c_xlate[80] =
30         "!\"#$%&()*+,-0123456789:;<=>?@[]^_`abcdefghijklmnopqrstuvwxyz{|}~";
31 static char c_reverse[] = {
32     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
33     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
34     99,  0,  1,  2,  3,  4,  5, 99,  6,  7,  8,  9, 10, 11, 99, 99,
35     12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
36     28, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
37     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 29, 99, 30, 31, 32,
38     33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
39     49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 99,
40     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
41     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
42     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
43     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
44     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
45     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
46     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
47     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
48 };
49 #else /* AFS_DARWIN_ENV */
50 static char c_xlate[80] =
51     "+=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
52 static char c_reverse[] = {
53     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
54     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
55     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,  0, 99, 99, 99, 99,
56      2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 99, 99, 99,  1, 99, 99,
57     99, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
58     27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 99, 99, 99, 99, 99,
59     99, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
60     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 99, 99, 99, 99, 99,
61     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
62     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
63     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
64     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
65     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
66     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
67     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
68     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
69 };
70 #endif /* AFS_DARWIN_ENV */
71
72 /* int_to_base64
73  * Create a base 64 string representation of a number.
74  * The supplied string 's' must be at least 12 bytes long.
75  * lb64_string in stds.h provides a typedef to get the length.
76  */
77 char *
78 int64_to_flipbase64(lb64_string_t s, afs_uint64 a)
79 {
80     int i;
81     afs_uint64 n;
82
83     i = 0;
84     if (a == 0)
85         s[i++] = c_xlate[0];
86     else {
87         for (n = a & 0x3f; a; n = ((a >>= 6) & 0x3f)) {
88             s[i++] = c_xlate[n];
89         }
90     }
91     s[i] = '\0';
92     return s;
93 }
94
95
96 afs_int64
97 flipbase64_to_int64(char *s)
98 {
99     afs_int64 n = 0;
100     afs_int64 result = 0;
101     int shift;
102
103     for (shift = 0; *s; s++) {
104         n = c_reverse[*(unsigned char *)s];
105         if (n >= 64)    /* should never happen */
106             continue;
107         n <<= shift;
108         result |= n ;
109         shift += 6;
110     }
111     return result;
112 }