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 164 : 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 164 : olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
38 164 : olen += olen / 72; /* line feeds */
39 164 : olen++; /* nul termination */
40 164 : if (olen < len)
41 0 : return NULL; /* integer overflow */
42 164 : out = os_malloc(olen);
43 164 : if (out == NULL)
44 0 : return NULL;
45 :
46 164 : end = src + len;
47 164 : in = src;
48 164 : pos = out;
49 164 : line_len = 0;
50 8826 : while (end - in >= 3) {
51 8498 : *pos++ = base64_table[(in[0] >> 2) & 0x3f];
52 25494 : *pos++ = base64_table[(((in[0] & 0x03) << 4) |
53 16996 : (in[1] >> 4)) & 0x3f];
54 25494 : *pos++ = base64_table[(((in[1] & 0x0f) << 2) |
55 16996 : (in[2] >> 6)) & 0x3f];
56 8498 : *pos++ = base64_table[in[2] & 0x3f];
57 8498 : in += 3;
58 8498 : line_len += 4;
59 8498 : if (line_len >= 72) {
60 423 : *pos++ = '\n';
61 423 : line_len = 0;
62 : }
63 : }
64 :
65 164 : if (end - in) {
66 55 : *pos++ = base64_table[(in[0] >> 2) & 0x3f];
67 55 : if (end - in == 1) {
68 20 : *pos++ = base64_table[((in[0] & 0x03) << 4) & 0x3f];
69 20 : *pos++ = '=';
70 : } else {
71 105 : *pos++ = base64_table[(((in[0] & 0x03) << 4) |
72 70 : (in[1] >> 4)) & 0x3f];
73 35 : *pos++ = base64_table[((in[1] & 0x0f) << 2) & 0x3f];
74 : }
75 55 : *pos++ = '=';
76 55 : line_len += 4;
77 : }
78 :
79 164 : if (line_len)
80 163 : *pos++ = '\n';
81 :
82 164 : *pos = '\0';
83 164 : if (out_len)
84 160 : *out_len = pos - out;
85 164 : 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 132 : 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 132 : int pad = 0;
105 :
106 132 : os_memset(dtable, 0x80, 256);
107 8580 : for (i = 0; i < sizeof(base64_table) - 1; i++)
108 8448 : dtable[base64_table[i]] = (unsigned char) i;
109 132 : dtable['='] = 0;
110 :
111 132 : count = 0;
112 30714 : for (i = 0; i < len; i++) {
113 30582 : if (dtable[src[i]] != 0x80)
114 30076 : count++;
115 : }
116 :
117 132 : if (count == 0 || count % 4)
118 0 : return NULL;
119 :
120 132 : olen = count / 4 * 3;
121 132 : pos = out = os_malloc(olen);
122 132 : if (out == NULL)
123 0 : return NULL;
124 :
125 132 : count = 0;
126 30627 : for (i = 0; i < len; i++) {
127 30539 : tmp = dtable[src[i]];
128 30539 : if (tmp == 0x80)
129 463 : continue;
130 :
131 30076 : if (src[i] == '=')
132 55 : pad++;
133 30076 : block[count] = tmp;
134 30076 : count++;
135 30076 : if (count == 4) {
136 7519 : *pos++ = (block[0] << 2) | (block[1] >> 4);
137 7519 : *pos++ = (block[1] << 4) | (block[2] >> 2);
138 7519 : *pos++ = (block[2] << 6) | block[3];
139 7519 : count = 0;
140 7519 : if (pad) {
141 44 : if (pad == 1)
142 33 : pos--;
143 11 : else if (pad == 2)
144 11 : pos -= 2;
145 : else {
146 : /* Invalid padding */
147 0 : os_free(out);
148 0 : return NULL;
149 : }
150 44 : break;
151 : }
152 : }
153 : }
154 :
155 132 : *out_len = pos - out;
156 132 : return out;
157 : }
|