backport to buster
[hcoop/debian/openafs.git] / src / util / base64.c
CommitLineData
805e021f
CE
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
14
15#ifdef AFS_SGI_XFS_IOPS_ENV
16
17static char c_xlate[80] =
18 "+,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
19
20/* int_to_base64
21 * Create a base 64 string representation of a number.
22 * The supplied string 's' must be at least 8 bytes long.
23 * b64_string in stds.h provides a tyepdef to get the length.
24 */
25char *
26int_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 } else {
42 for (; j >= 0; j -= 6) {
43 n = a & (0x3f << j);
44 if (n)
45 break; /* found highest bits set. */
46 }
47 s[i++] = c_xlate[n >> j];
48 a &= ~(0x3f << j);
49 j -= 6;
50 }
51 /* more to do. */
52 for (; j >= 0; j -= 6) {
53 n = a & (0x3f << j);
54 s[i++] = c_xlate[n >> j];
55 a &= ~(0x3f << j);
56 }
57 }
58 s[i] = '\0';
59 return s;
60}
61
62/* Mapping: +=0, ,=1, 0-9 = 2-11, A-Z = 12-37, a-z = 38-63 */
63int
64base64_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 } else if (*s <= '9') {
74 n = 2 + (int)(*s - '0');
75 } else if (*s <= 'Z') {
76 n = 12 + (int)(*s - 'A');
77 } else {
78 n = 38 + (int)(*s - 'a');
79 }
80 result = (result << 6) + n;
81 }
82 return result;
83}
84
85
86#endif /* AFS_SGI_XFS_IOPS_ENV */