Commit | Line | Data |
---|---|---|
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 | #include "afsutil.h" | |
15 | ||
16 | static char *c_xlate = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
17 | ||
18 | ||
19 | /* int_to_base32 | |
20 | * Create a base 32 string representation of a number. | |
21 | * The supplied string 's' must be at least 8 bytes long. | |
22 | * Use the b32_string_t tyepdef. | |
23 | */ | |
24 | char * | |
25 | int_to_base32(b32_string_t s, int a) | |
26 | { | |
27 | int i, j; | |
28 | unsigned int n; | |
29 | ||
30 | i = 0; | |
31 | if (a == 0) | |
32 | s[i++] = c_xlate[0]; | |
33 | else { | |
34 | j = 25; | |
35 | n = a & 0xc0000000; | |
36 | if (n) { | |
37 | n >>= 30; | |
38 | s[i++] = c_xlate[n]; | |
39 | a &= ~0xc0000000; | |
40 | } else { | |
41 | for (; j >= 0; j -= 5) { | |
42 | n = a & (0x1f << j); | |
43 | if (n) | |
44 | break; /* found highest bits set. */ | |
45 | } | |
46 | s[i++] = c_xlate[n >> j]; | |
47 | a &= ~(0x1f << j); | |
48 | j -= 5; | |
49 | } | |
50 | /* more to do. */ | |
51 | for (; j >= 0; j -= 5) { | |
52 | n = a & (0x1f << j); | |
53 | s[i++] = c_xlate[n >> j]; | |
54 | a &= ~(0x1f << j); | |
55 | } | |
56 | } | |
57 | s[i] = '\0'; | |
58 | return s; | |
59 | } | |
60 | ||
61 | int | |
62 | base32_to_int(char *s) | |
63 | { | |
64 | int n = 0; | |
65 | int result = 0; | |
66 | ||
67 | for (; *s; s++) { | |
68 | if (*s <= '9') { | |
69 | n = (int)(*s - '0'); | |
70 | } else { | |
71 | n = 10 + (int)(*s - 'A'); | |
72 | } | |
73 | result = (result << 5) + n; | |
74 | } | |
75 | return result; | |
76 | } |