1 /* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
2 /* l64a - convert long to radix-64 ascii string
3  *
4  * Conversion is performed on at most 32-bits of input value starting
5  * from least significant bits to the most significant bits.
6  *
7  * The routine splits the input value into groups of 6 bits for up to
8  * 32 bits of input.  This means that the last group may be 2 bits
9  * (bits 30 and 31).
10  *
11  * Each group of 6 bits forms a value from 0-63 which is converted into
12  * a character as follows:
13  *         0 = '.'
14  *         1 = '/'
15  *         2-11 = '0' to '9'
16  *        12-37 = 'A' to 'Z'
17  *        38-63 = 'a' to 'z'
18  *
19  * When the remaining bits are zero or all 32 bits have been translated,
20  * a nul terminator is appended to the resulting string.  An input value of
21  * 0 results in an empty string.
22  */
23 
24 #include <_ansi.h>
25 #include <stdlib.h>
26 
27 static const char R64_ARRAY[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
28 
29 char *
l64a(long value)30 l64a (long value)
31 {
32   char *ptr;
33   char *result;
34   int i, index;
35   unsigned long tmp = (unsigned long)value & 0xffffffff;
36   static NEWLIB_THREAD_LOCAL char _l64a_buf[8];
37 
38   result = _l64a_buf;
39   ptr = result;
40 
41   for (i = 0; i < 6; ++i)
42     {
43       if (tmp == 0)
44 	{
45 	  *ptr = '\0';
46 	  break;
47 	}
48 
49       index = tmp & (64 - 1);
50       *ptr++ = R64_ARRAY[index];
51       tmp >>= 6;
52     }
53 
54   return result;
55 }
56