Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / l64a.c
blob6f12b6151ec2cc0c27afed053ba38c7f7e36696e
1 /* l64a - convert long to radix-64 ascii string
2 *
3 * Conversion is performed on at most 32-bits of input value starting
4 * from least significant bits to the most significant bits.
6 * The routine splits the input value into groups of 6 bits for up to
7 * 32 bits of input. This means that the last group may be 2 bits
8 * (bits 30 and 31).
9 *
10 * Each group of 6 bits forms a value from 0-63 which is converted into
11 * a character as follows:
12 * 0 = '.'
13 * 1 = '/'
14 * 2-11 = '0' to '9'
15 * 12-37 = 'A' to 'Z'
16 * 38-63 = 'a' to 'z'
18 * When the remaining bits are zero or all 32 bits have been translated,
19 * a nul terminator is appended to the resulting string. An input value of
20 * 0 results in an empty string.
23 #include <_ansi.h>
24 #include <stdlib.h>
25 #include <reent.h>
27 #ifdef _REENT_THREAD_LOCAL
28 _Thread_local char _tls_l64a_buf[8];
29 #endif
31 static const char R64_ARRAY[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
33 char *
34 l64a (long value)
36 return _l64a_r (_REENT, value);
39 char *
40 _l64a_r (struct _reent *rptr,
41 long value)
43 char *ptr;
44 char *result;
45 int i, index;
46 unsigned long tmp = (unsigned long)value & 0xffffffff;
48 _REENT_CHECK_MISC(rptr);
49 result = _REENT_L64A_BUF(rptr);
50 ptr = result;
52 for (i = 0; i < 6; ++i)
54 if (tmp == 0)
56 *ptr = '\0';
57 break;
60 index = tmp & (64 - 1);
61 *ptr++ = R64_ARRAY[index];
62 tmp >>= 6;
65 return result;