aboutsummaryrefslogtreecommitdiffstats
path: root/com32/libutil/base64.c
blob: 74b101ce7f7d37d8f856d1566665228561e5e26b (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Output a base64 string.
 *
 * Options include:
 * - Character 62 and 63;
 * - To pad or not to pad.
 */

#include <inttypes.h>
#include <base64.h>

size_t genbase64(char *output, const void *input, size_t size, int flags)
{
    static char charz[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_";
    uint8_t buf[3];
    int j;
    const uint8_t *p;
    char *q;
    uint32_t bv;
    int left = size;

    charz[62] = (char)flags;
    charz[63] = (char)(flags >> 8);

    p = input;
    q = output;

    while (left > 0) {
	if (left < 3) {
	    buf[0] = p[0];
	    buf[1] = (left > 1) ? p[1] : 0;
	    buf[2] = 0;
	    p = buf;
	}

	bv = (p[0] << 16) | (p[1] << 8) | p[2];
	p += 3;
	left -= 3;

	for (j = 0; j < 4; j++) {
	    *q++ = charz[(bv >> 18) & 0x3f];
	    bv <<= 6;
	}
    }

    switch (left) {
    case -1:
	if (flags & BASE64_PAD)
	    q[-1] = '=';
	else
	    q--;
	break;

    case -2:
	if (flags & BASE64_PAD)
	    q[-2] = q[-1] = '=';
	else
	    q -= 2;
	break;

    default:
	break;
    }

    *q = '\0';

    return q - output;
}

#ifdef TEST

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int i;
    char buf[4096];
    int len, bytes;

    for (i = 1; i < argc; i++) {
	printf("Original: \"%s\"\n", argv[i]);

	len = strlen(argv[i]);
	bytes = genbase64(buf, argv[i], len, BASE64_MIME | BASE64_PAD);
	printf("    MIME: \"%s\" (%d)\n", buf, bytes);
	bytes = genbase64(buf, argv[i], len, BASE64_SAFE);
	printf("    Safe: \"%s\" (%d)\n", buf, bytes);
    }

    return 0;
}

#endif