1189251Ssam/*
2189251Ssam * Base64 encoding/decoding (RFC1341)
3252726Srpaulo * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#include "includes.h"
10189251Ssam
11189251Ssam#include "os.h"
12189251Ssam#include "base64.h"
13189251Ssam
14189251Ssamstatic const unsigned char base64_table[65] =
15189251Ssam	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16189251Ssam
17189251Ssam/**
18189251Ssam * base64_encode - Base64 encode
19189251Ssam * @src: Data to be encoded
20189251Ssam * @len: Length of the data to be encoded
21189251Ssam * @out_len: Pointer to output length variable, or %NULL if not used
22189251Ssam * Returns: Allocated buffer of out_len bytes of encoded data,
23189251Ssam * or %NULL on failure
24189251Ssam *
25189251Ssam * Caller is responsible for freeing the returned buffer. Returned buffer is
26189251Ssam * nul terminated to make it easier to use as a C string. The nul terminator is
27189251Ssam * not included in out_len.
28189251Ssam */
29189251Ssamunsigned char * base64_encode(const unsigned char *src, size_t len,
30189251Ssam			      size_t *out_len)
31189251Ssam{
32189251Ssam	unsigned char *out, *pos;
33189251Ssam	const unsigned char *end, *in;
34189251Ssam	size_t olen;
35189251Ssam	int line_len;
36189251Ssam
37189251Ssam	olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
38189251Ssam	olen += olen / 72; /* line feeds */
39189251Ssam	olen++; /* nul termination */
40209158Srpaulo	if (olen < len)
41209158Srpaulo		return NULL; /* integer overflow */
42189251Ssam	out = os_malloc(olen);
43189251Ssam	if (out == NULL)
44189251Ssam		return NULL;
45189251Ssam
46189251Ssam	end = src + len;
47189251Ssam	in = src;
48189251Ssam	pos = out;
49189251Ssam	line_len = 0;
50189251Ssam	while (end - in >= 3) {
51189251Ssam		*pos++ = base64_table[in[0] >> 2];
52189251Ssam		*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
53189251Ssam		*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
54189251Ssam		*pos++ = base64_table[in[2] & 0x3f];
55189251Ssam		in += 3;
56189251Ssam		line_len += 4;
57189251Ssam		if (line_len >= 72) {
58189251Ssam			*pos++ = '\n';
59189251Ssam			line_len = 0;
60189251Ssam		}
61189251Ssam	}
62189251Ssam
63189251Ssam	if (end - in) {
64189251Ssam		*pos++ = base64_table[in[0] >> 2];
65189251Ssam		if (end - in == 1) {
66189251Ssam			*pos++ = base64_table[(in[0] & 0x03) << 4];
67189251Ssam			*pos++ = '=';
68189251Ssam		} else {
69189251Ssam			*pos++ = base64_table[((in[0] & 0x03) << 4) |
70189251Ssam					      (in[1] >> 4)];
71189251Ssam			*pos++ = base64_table[(in[1] & 0x0f) << 2];
72189251Ssam		}
73189251Ssam		*pos++ = '=';
74189251Ssam		line_len += 4;
75189251Ssam	}
76189251Ssam
77189251Ssam	if (line_len)
78189251Ssam		*pos++ = '\n';
79189251Ssam
80189251Ssam	*pos = '\0';
81189251Ssam	if (out_len)
82189251Ssam		*out_len = pos - out;
83189251Ssam	return out;
84189251Ssam}
85189251Ssam
86189251Ssam
87189251Ssam/**
88189251Ssam * base64_decode - Base64 decode
89189251Ssam * @src: Data to be decoded
90189251Ssam * @len: Length of the data to be decoded
91189251Ssam * @out_len: Pointer to output length variable
92189251Ssam * Returns: Allocated buffer of out_len bytes of decoded data,
93189251Ssam * or %NULL on failure
94189251Ssam *
95189251Ssam * Caller is responsible for freeing the returned buffer.
96189251Ssam */
97189251Ssamunsigned char * base64_decode(const unsigned char *src, size_t len,
98189251Ssam			      size_t *out_len)
99189251Ssam{
100252726Srpaulo	unsigned char dtable[256], *out, *pos, block[4], tmp;
101189251Ssam	size_t i, count, olen;
102252726Srpaulo	int pad = 0;
103189251Ssam
104189251Ssam	os_memset(dtable, 0x80, 256);
105189251Ssam	for (i = 0; i < sizeof(base64_table) - 1; i++)
106189251Ssam		dtable[base64_table[i]] = (unsigned char) i;
107189251Ssam	dtable['='] = 0;
108189251Ssam
109189251Ssam	count = 0;
110189251Ssam	for (i = 0; i < len; i++) {
111189251Ssam		if (dtable[src[i]] != 0x80)
112189251Ssam			count++;
113189251Ssam	}
114189251Ssam
115189251Ssam	if (count == 0 || count % 4)
116189251Ssam		return NULL;
117189251Ssam
118189251Ssam	olen = count / 4 * 3;
119189251Ssam	pos = out = os_malloc(olen);
120189251Ssam	if (out == NULL)
121189251Ssam		return NULL;
122189251Ssam
123189251Ssam	count = 0;
124189251Ssam	for (i = 0; i < len; i++) {
125189251Ssam		tmp = dtable[src[i]];
126189251Ssam		if (tmp == 0x80)
127189251Ssam			continue;
128189251Ssam
129252726Srpaulo		if (src[i] == '=')
130252726Srpaulo			pad++;
131189251Ssam		block[count] = tmp;
132189251Ssam		count++;
133189251Ssam		if (count == 4) {
134189251Ssam			*pos++ = (block[0] << 2) | (block[1] >> 4);
135189251Ssam			*pos++ = (block[1] << 4) | (block[2] >> 2);
136189251Ssam			*pos++ = (block[2] << 6) | block[3];
137189251Ssam			count = 0;
138252726Srpaulo			if (pad) {
139252726Srpaulo				if (pad == 1)
140252726Srpaulo					pos--;
141252726Srpaulo				else if (pad == 2)
142252726Srpaulo					pos -= 2;
143252726Srpaulo				else {
144252726Srpaulo					/* Invalid padding */
145252726Srpaulo					os_free(out);
146252726Srpaulo					return NULL;
147252726Srpaulo				}
148252726Srpaulo				break;
149252726Srpaulo			}
150189251Ssam		}
151189251Ssam	}
152189251Ssam
153189251Ssam	*out_len = pos - out;
154189251Ssam	return out;
155189251Ssam}
156