2 * Base64 encoding/decoding (RFC1341)
3 * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
15 static const unsigned char base64_table[65] =
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17 static const unsigned char base64_url_table[65] =
18 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
21 static unsigned char * base64_gen_encode(const unsigned char *src, size_t len,
23 const unsigned char *table,
26 unsigned char *out, *pos;
27 const unsigned char *end, *in;
31 if (len >= SIZE_MAX / 4)
33 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
35 olen += olen / 72; /* line feeds */
36 olen++; /* nul termination */
38 return NULL; /* integer overflow */
39 out = os_malloc(olen);
47 while (end - in >= 3) {
48 *pos++ = table[(in[0] >> 2) & 0x3f];
49 *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
50 *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
51 *pos++ = table[in[2] & 0x3f];
54 if (add_pad && line_len >= 72) {
61 *pos++ = table[(in[0] >> 2) & 0x3f];
63 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
67 *pos++ = table[(((in[0] & 0x03) << 4) |
68 (in[1] >> 4)) & 0x3f];
69 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
76 if (add_pad && line_len)
86 static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
88 const unsigned char *table)
90 unsigned char dtable[256], *out, *pos, block[4], tmp;
91 size_t i, count, olen;
95 os_memset(dtable, 0x80, 256);
96 for (i = 0; i < sizeof(base64_table) - 1; i++)
97 dtable[table[i]] = (unsigned char) i;
101 for (i = 0; i < len; i++) {
102 if (dtable[src[i]] != 0x80)
108 extra_pad = (4 - count % 4) % 4;
110 olen = (count + extra_pad) / 4 * 3;
111 pos = out = os_malloc(olen);
116 for (i = 0; i < len + extra_pad; i++) {
132 *pos++ = (block[0] << 2) | (block[1] >> 4);
133 *pos++ = (block[1] << 4) | (block[2] >> 2);
134 *pos++ = (block[2] << 6) | block[3];
142 /* Invalid padding */
151 *out_len = pos - out;
157 * base64_encode - Base64 encode
158 * @src: Data to be encoded
159 * @len: Length of the data to be encoded
160 * @out_len: Pointer to output length variable, or %NULL if not used
161 * Returns: Allocated buffer of out_len bytes of encoded data,
162 * or %NULL on failure
164 * Caller is responsible for freeing the returned buffer. Returned buffer is
165 * nul terminated to make it easier to use as a C string. The nul terminator is
166 * not included in out_len.
168 unsigned char * base64_encode(const unsigned char *src, size_t len,
171 return base64_gen_encode(src, len, out_len, base64_table, 1);
175 unsigned char * base64_url_encode(const unsigned char *src, size_t len,
176 size_t *out_len, int add_pad)
178 return base64_gen_encode(src, len, out_len, base64_url_table, add_pad);
183 * base64_decode - Base64 decode
184 * @src: Data to be decoded
185 * @len: Length of the data to be decoded
186 * @out_len: Pointer to output length variable
187 * Returns: Allocated buffer of out_len bytes of decoded data,
188 * or %NULL on failure
190 * Caller is responsible for freeing the returned buffer.
192 unsigned char * base64_decode(const unsigned char *src, size_t len,
195 return base64_gen_decode(src, len, out_len, base64_table);
199 unsigned char * base64_url_decode(const unsigned char *src, size_t len,
202 return base64_gen_decode(src, len, out_len, base64_url_table);