summaryrefslogtreecommitdiffstats
path: root/src/base64.c
blob: 6a2c873aab2cb9088d445133bf708a17b4ae5440 (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
#include "base64.h"

static void
base64_decode_block(const char *src, char *dest)
{
	int i;
	char buf[4];

	/* Convert base 64 characters into values. */
	/* Supports ASCII and EBCDIC systems. */
	for (i = 0; i < 4; ++i) {
		if (src[i] >= 'A' && src[i] <= 'I') {
			buf[i] = src[i] - 'A' + 0;
		} else if (src[i] >= 'J' && src[i] <= 'R') {
			buf[i] = src[i] - 'J' + 9;
		} else if (src[i] >= 'S' && src[i] <= 'Z') {
			buf[i] = src[i] - 'S' + 18;
		} else if (src[i] >= 'a' && src[i] <= 'i') {
			buf[i] = src[i] - 'a' + 26;
		} else if (src[i] >= 'j' && src[i] <= 'r') {
			buf[i] = src[i] - 'j' + 35;
		} else if (src[i] >= 's' && src[i] <= 'z') {
			buf[i] = src[i] - 's' + 44;
		} else if (src[i] >= '0' && src[i] <= '9') {
			buf[i] = src[i] - '0' + 52;
		} else if (src[i] == '+') {
			buf[i] = 62;
		} else if (src[i] == '/') {
			buf[i] = 63;
		} else {
			buf[i] = '\0';
		}
	}

	/* Map four decoded value bytes into three output bytes. */
	dest[0] = (buf[0] << 2) | (buf[1] >> 4);
	dest[1] = (buf[1] << 4) | (buf[2] >> 2);
	dest[2] = (buf[2] << 6) | (buf[3] >> 0);
}
	
void
base64_decode(const char *src, char *dest, size_t dest_len)
{
	size_t i;

	/* Decode the input one entire block at a time. */
	for (i = 0; i < (dest_len - 1) / 3; ++i) {
		if (src[i * 4] == '\0' || 
				src[i * 4 + 1] == '\0' || 
				src[i * 4 + 2] == '\0' || 
				src[i * 4 + 3] == '\0') {
			dest[i * 3] = '\0';
			break;
		}
		base64_decode_block(src + i * 4, dest + i * 3);
	}

	/* Ensure NUL termination. */
	dest[dest_len - 1] = '\0';
}

#if 0
int
main()
{
	char dest[1024];
	base64_decode(
			"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5v"
			"dCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1"
			"dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Np"
			"b24gZnJvbSBvdGhlciBhbmltYWxzLCB3"
			"aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1p"
			"bmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFu"
			"Y2Ugb2YgZGVsaWdodCBpbiB0aGUgY29u"
			"dGludWVkIGFuZCBpbmRlZmF0aWdhYmxl"
			"IGdlbmVyYXRpb24gb2Yga25vd2xlZGdl"
			"LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhl"
			"bWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVh"
			"c3VyZS4=", dest, 1024);
	printf("Decoded string: \"%s\"\n", dest);
	base64_decode("YW55IGNhcm5hbCBwbGVhc3VyZS4=", dest, 1024);
	printf("Decoded string: \"%s\"\n", dest);
	base64_decode("YW55IGNhcm5hbCBwbGVhc3VyZQ==", dest, 1024);
	printf("Decoded string: \"%s\"\n", dest);
	base64_decode("YW55IGNhcm5hbCBwbGVhc3Vy", dest, 1024);
	printf("Decoded string: \"%s\"\n", dest);
	base64_decode("YW55IGNhcm5hbCBwbGVhc3U=", dest, 1024);
	printf("Decoded string: \"%s\"\n", dest);
	base64_decode("YW55IGNhcm5hbCBwbGVhcw==", dest, 1024);
	printf("Decoded string: \"%s\"\n", dest);
	base64_decode("YW55IGNhcm5hbCBwbGVhcw==", dest, 4);
	printf("Decoded string: \"%s\"\n", dest);
	return 0;
}
#endif