3 <<a64l>>, <<l64a>>---convert between radix-64 ASCII string and long
12 long a64l(const char *<[input]>);
13 char *l64a(long <[input]>);
16 Conversion is performed between long and radix-64 characters. The
17 <<l64a>> routine transforms up to 32 bits of input value starting from
18 least significant bits to the most significant bits. The input value
19 is split up into a maximum of 5 groups of 6 bits and possibly one
20 group of 2 bits (bits 31 and 30).
22 Each group of 6 bits forms a value from 0--63 which is translated into
23 a character as follows:
33 When the remaining bits are zero or all bits have been translated, a
34 null terminator is appended to the string. An input value of 0
35 results in the empty string.
37 The <<a64l>> function performs the reverse translation. Each
38 character is used to generate a 6-bit value for up to 30 bits and then
39 a 2-bit value to complete a 32-bit result. The null terminator means
40 that the remaining digits are 0. An empty input string or NULL string
41 results in 0L. An invalid string results in undefined behavior. If
42 the size of a long is greater than 32 bits, the result is sign-extended.
45 <<l64a>> returns a null-terminated string of 0 to 6 characters.
46 <<a64l>> returns the 32-bit translated value from the input character string.
49 <<l64a>> and <<a64l>> are non-ANSI and are defined by the Single Unix Specification.
51 Supporting OS subroutines required: None.
59 a64l (const char *input
)
64 unsigned long result
= 0;
71 /* it easiest to go from most significant digit to least so find end of input or up
72 to 6 characters worth */
73 for (i
= 0; i
< 6; ++i
)
83 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
85 digit
= (ch
- 'a') + 38;
87 digit
= (ch
- 'A') + 12;
89 digit
= (ch
- '0') + 2;
94 #else /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) */
110 digit
= (ch
- '0') + 2;
138 digit
= (ch
- 'A') + 12;
166 digit
= (ch
- 'a') + 38;
172 #endif /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) */
174 result
= (result
<< 6) + digit
;
177 #if LONG_MAX > 2147483647
178 /* for implementations where long is > 32 bits, the result must be sign-extended */
179 if (result
& 0x80000000)
180 return (((long)-1 >> 32) << 32) + result
;