Line data Source code
1 : /*
2 : * Base64 encoding/decoding (RFC1341)
3 : * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "os.h"
12 : #include "base64.h"
13 :
14 : static const unsigned char base64_table[65] =
15 : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16 :
17 : /**
18 : * base64_encode - Base64 encode
19 : * @src: Data to be encoded
20 : * @len: Length of the data to be encoded
21 : * @out_len: Pointer to output length variable, or %NULL if not used
22 : * Returns: Allocated buffer of out_len bytes of encoded data,
23 : * or %NULL on failure
24 : *
25 : * Caller is responsible for freeing the returned buffer. Returned buffer is
26 : * nul terminated to make it easier to use as a C string. The nul terminator is
27 : * not included in out_len.
28 : */
29 1110 : unsigned char * base64_encode(const unsigned char *src, size_t len,
30 : size_t *out_len)
31 : {
32 : unsigned char *out, *pos;
33 : const unsigned char *end, *in;
34 : size_t olen;
35 : int line_len;
36 :
37 1110 : olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
38 1110 : olen += olen / 72; /* line feeds */
39 1110 : olen++; /* nul termination */
40 1110 : if (olen < len)
41 1 : return NULL; /* integer overflow */
42 1109 : out = os_malloc(olen);
43 1109 : if (out == NULL)
44 6 : return NULL;
45 :
46 1103 : end = src + len;
47 1103 : in = src;
48 1103 : pos = out;
49 1103 : line_len = 0;
50 65316 : while (end - in >= 3) {
51 63110 : *pos++ = base64_table[(in[0] >> 2) & 0x3f];
52 189330 : *pos++ = base64_table[(((in[0] & 0x03) << 4) |
53 126220 : (in[1] >> 4)) & 0x3f];
54 189330 : *pos++ = base64_table[(((in[1] & 0x0f) << 2) |
55 126220 : (in[2] >> 6)) & 0x3f];
56 63110 : *pos++ = base64_table[in[2] & 0x3f];
57 63110 : in += 3;
58 63110 : line_len += 4;
59 63110 : if (line_len >= 72) {
60 3152 : *pos++ = '\n';
61 3152 : line_len = 0;
62 : }
63 : }
64 :
65 1103 : if (end - in) {
66 180 : *pos++ = base64_table[(in[0] >> 2) & 0x3f];
67 180 : if (end - in == 1) {
68 56 : *pos++ = base64_table[((in[0] & 0x03) << 4) & 0x3f];
69 56 : *pos++ = '=';
70 : } else {
71 372 : *pos++ = base64_table[(((in[0] & 0x03) << 4) |
72 248 : (in[1] >> 4)) & 0x3f];
73 124 : *pos++ = base64_table[((in[1] & 0x0f) << 2) & 0x3f];
74 : }
75 180 : *pos++ = '=';
76 180 : line_len += 4;
77 : }
78 :
79 1103 : if (line_len)
80 1102 : *pos++ = '\n';
81 :
82 1103 : *pos = '\0';
83 1103 : if (out_len)
84 1090 : *out_len = pos - out;
85 1103 : return out;
86 : }
87 :
88 :
89 : /**
90 : * base64_decode - Base64 decode
91 : * @src: Data to be decoded
92 : * @len: Length of the data to be decoded
93 : * @out_len: Pointer to output length variable
94 : * Returns: Allocated buffer of out_len bytes of decoded data,
95 : * or %NULL on failure
96 : *
97 : * Caller is responsible for freeing the returned buffer.
98 : */
99 387 : unsigned char * base64_decode(const unsigned char *src, size_t len,
100 : size_t *out_len)
101 : {
102 : unsigned char dtable[256], *out, *pos, block[4], tmp;
103 : size_t i, count, olen;
104 387 : int pad = 0;
105 :
106 387 : os_memset(dtable, 0x80, 256);
107 25155 : for (i = 0; i < sizeof(base64_table) - 1; i++)
108 24768 : dtable[base64_table[i]] = (unsigned char) i;
109 387 : dtable['='] = 0;
110 :
111 387 : count = 0;
112 97531 : for (i = 0; i < len; i++) {
113 97144 : if (dtable[src[i]] != 0x80)
114 95793 : count++;
115 : }
116 :
117 387 : if (count == 0 || count % 4)
118 3 : return NULL;
119 :
120 384 : olen = count / 4 * 3;
121 384 : pos = out = os_malloc(olen);
122 384 : if (out == NULL)
123 4 : return NULL;
124 :
125 380 : count = 0;
126 96512 : for (i = 0; i < len; i++) {
127 96287 : tmp = dtable[src[i]];
128 96287 : if (tmp == 0x80)
129 1215 : continue;
130 :
131 95072 : if (src[i] == '=')
132 201 : pad++;
133 95072 : block[count] = tmp;
134 95072 : count++;
135 95072 : if (count == 4) {
136 23768 : *pos++ = (block[0] << 2) | (block[1] >> 4);
137 23768 : *pos++ = (block[1] << 4) | (block[2] >> 2);
138 23768 : *pos++ = (block[2] << 6) | block[3];
139 23768 : count = 0;
140 23768 : if (pad) {
141 155 : if (pad == 1)
142 111 : pos--;
143 44 : else if (pad == 2)
144 43 : pos -= 2;
145 : else {
146 : /* Invalid padding */
147 1 : os_free(out);
148 1 : return NULL;
149 : }
150 154 : break;
151 : }
152 : }
153 : }
154 :
155 379 : *out_len = pos - out;
156 379 : return out;
157 : }
|