File: l64a.c

package info (click to toggle)
picolibc 1.8.11-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 50,064 kB
  • sloc: ansic: 404,031; asm: 24,984; sh: 2,585; python: 2,289; perl: 680; pascal: 329; exp: 287; makefile: 222; cpp: 71; xml: 40
file content (53 lines) | stat: -rw-r--r-- 1,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
/* l64a - convert long to radix-64 ascii string
 *
 * Conversion is performed on at most 32-bits of input value starting
 * from least significant bits to the most significant bits.
 *
 * The routine splits the input value into groups of 6 bits for up to
 * 32 bits of input.  This means that the last group may be 2 bits
 * (bits 30 and 31).
 *
 * Each group of 6 bits forms a value from 0-63 which is converted into
 * a character as follows:
 *         0 = '.'
 *         1 = '/'
 *         2-11 = '0' to '9'
 *        12-37 = 'A' to 'Z'
 *        38-63 = 'a' to 'z'
 *
 * When the remaining bits are zero or all 32 bits have been translated,
 * a nul terminator is appended to the resulting string.  An input value of
 * 0 results in an empty string.
 */

#define _DEFAULT_SOURCE
#include <stdlib.h>

static const char R64_ARRAY[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

char *
l64a(long value)
{
    char                      *ptr;
    char                      *result;
    int                        i, index;
    unsigned long              tmp = (unsigned long)value & 0xffffffff;
    static __THREAD_LOCAL char _l64a_buf[8];

    result = _l64a_buf;
    ptr = result;

    for (i = 0; i < 6; ++i) {
        if (tmp == 0) {
            *ptr = '\0';
            break;
        }

        index = tmp & (64 - 1);
        *ptr++ = R64_ARRAY[index];
        tmp >>= 6;
    }

    return result;
}