2 * Base64 encoding/decoding (RFC1341)
3 * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 static const unsigned char base64_table[65] =
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16 static const unsigned char base64_url_table[65] =
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
20 static unsigned char * base64_gen_encode(const unsigned char *src, size_t len,
22 const unsigned char *table,
25 unsigned char *out, *pos;
26 const unsigned char *end, *in;
30 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
32 olen += olen / 72; /* line feeds */
33 olen++; /* nul termination */
35 return NULL; /* integer overflow */
36 out = os_malloc(olen);
44 while (end - in >= 3) {
45 *pos++ = table[(in[0] >> 2) & 0x3f];
46 *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
47 *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
48 *pos++ = table[in[2] & 0x3f];
51 if (add_pad && line_len >= 72) {
58 *pos++ = table[(in[0] >> 2) & 0x3f];
60 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
64 *pos++ = table[(((in[0] & 0x03) << 4) |
65 (in[1] >> 4)) & 0x3f];
66 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
73 if (add_pad && line_len)
83 static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
85 const unsigned char *table)
87 unsigned char dtable[256], *out, *pos, block[4], tmp;
88 size_t i, count, olen;
92 os_memset(dtable, 0x80, 256);
93 for (i = 0; i < sizeof(base64_table) - 1; i++)
94 dtable[table[i]] = (unsigned char) i;
98 for (i = 0; i < len; i++) {
99 if (dtable[src[i]] != 0x80)
105 extra_pad = (4 - count % 4) % 4;
107 olen = (count + extra_pad) / 4 * 3;
108 pos = out = os_malloc(olen);
113 for (i = 0; i < len + extra_pad; i++) {
129 *pos++ = (block[0] << 2) | (block[1] >> 4);
130 *pos++ = (block[1] << 4) | (block[2] >> 2);
131 *pos++ = (block[2] << 6) | block[3];
139 /* Invalid padding */
148 *out_len = pos - out;
154 * base64_encode - Base64 encode
155 * @src: Data to be encoded
156 * @len: Length of the data to be encoded
157 * @out_len: Pointer to output length variable, or %NULL if not used
158 * Returns: Allocated buffer of out_len bytes of encoded data,
159 * or %NULL on failure
161 * Caller is responsible for freeing the returned buffer. Returned buffer is
162 * nul terminated to make it easier to use as a C string. The nul terminator is
163 * not included in out_len.
165 unsigned char * base64_encode(const unsigned char *src, size_t len,
168 return base64_gen_encode(src, len, out_len, base64_table, 1);
172 unsigned char * base64_url_encode(const unsigned char *src, size_t len,
173 size_t *out_len, int add_pad)
175 return base64_gen_encode(src, len, out_len, base64_url_table, add_pad);
180 * base64_decode - Base64 decode
181 * @src: Data to be decoded
182 * @len: Length of the data to be decoded
183 * @out_len: Pointer to output length variable
184 * Returns: Allocated buffer of out_len bytes of decoded data,
185 * or %NULL on failure
187 * Caller is responsible for freeing the returned buffer.
189 unsigned char * base64_decode(const unsigned char *src, size_t len,
192 return base64_gen_decode(src, len, out_len, base64_table);
196 unsigned char * base64_url_decode(const unsigned char *src, size_t len,
199 return base64_gen_decode(src, len, out_len, base64_url_table);