aboutsummaryrefslogtreecommitdiffstats
path: root/libar2_encode_base64.c
blob: f9507e9c9c11af7706ddbf701c2377df0cf8e170 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
static char lut[256] = ALPHABET ALPHABET ALPHABET ALPHABET;

#define O1(I1, I2, I3) ((I1) >> 2)
#define O2(I1, I2, I3) (((I1) << 4) | ((I2) >> 4))
#define O3(I1, I2, I3) (((I2) << 2) | ((I3) >> 6))
#define O4(I1, I2, I3) (I3)


size_t
libar2_encode_base64(char *buf, const void *data_, size_t len)
{
	const unsigned char *data = data_;
	unsigned char a, b, c;
	size_t q, r, i;

	q = len / 3;
	r = len % 3;

	if (buf) {
		buf = &buf[q * 4];
		data = &data[q * 3];

		if (r == 1) {
			a = data[0];
			buf[0] = lut[O1(a, 0, 0) & 255];
	                buf[1] = lut[O2(a, 0, 0) & 255];
			buf[2] = '\0';
		} else if (r == 2) {
			a = data[0], b = data[1];
			buf[0] = lut[O1(a, b, 0) & 255];
	                buf[1] = lut[O2(a, b, 0) & 255];
			buf[2] = lut[O3(a, b, 0) & 255];
			buf[3] = '\0';
		} else {
			buf[0] = '\0';
		}

		for (i = 0; i < q; i++) {
			data -= 3;
			buf -= 4;
			a = data[0], b = data[1], c = data[2];
			buf[0] = lut[O1(a, b, c) & 255];
	                buf[1] = lut[O2(a, b, c) & 255];
			buf[2] = lut[O3(a, b, c) & 255];
	                buf[3] = lut[O4(a, b, c) & 255];
		}
	}

	return (q * 4) + r + !!r + 1;
}