1 /*
2 * This code originally came from here
3 *
4 * http://base64.sourceforge.net/b64.c
5 *
6 * already with MIT license, which is retained.
7 *
8 * LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
9 *
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated
12 * documentation files (the "Software"), to deal in the
13 * Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute,
15 * sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so,
17 * subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall
20 * be included in all copies or substantial portions of the
21 * Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
24 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
25 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
27 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
29 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 * VERSION HISTORY:
33 * Bob Trower 08/04/01 -- Create Version 0.00.00B
34 *
35 * I cleaned it up quite a bit to match the (linux kernel) style of the rest
36 * of libwebsockets
37 */
38
39 #include <libwebsockets.h>
40
41 #include <stdio.h>
42 #include <string.h>
43 #include "private-lib-core.h"
44
45 static const char encode_orig[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46 "abcdefghijklmnopqrstuvwxyz0123456789+/";
47 static const char encode_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48 "abcdefghijklmnopqrstuvwxyz0123456789-_";
49 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
50 "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
51
52 static int
_lws_b64_encode_string(const char * encode,const char * in,int in_len,char * out,int out_size)53 _lws_b64_encode_string(const char *encode, const char *in, int in_len,
54 char *out, int out_size)
55 {
56 unsigned char triple[3];
57 int i, done = 0;
58
59 while (in_len) {
60 int len = 0;
61 for (i = 0; i < 3; i++) {
62 if (in_len) {
63 triple[i] = *in++;
64 len++;
65 in_len--;
66 } else
67 triple[i] = 0;
68 }
69
70 if (done + 4 >= out_size)
71 return -1;
72
73 *out++ = encode[triple[0] >> 2];
74 *out++ = encode[(((triple[0] & 0x03) << 4) & 0x30) |
75 (((triple[1] & 0xf0) >> 4) & 0x0f)];
76 *out++ = (len > 1 ? encode[(((triple[1] & 0x0f) << 2) & 0x3c) |
77 (((triple[2] & 0xc0) >> 6) & 3)] : '=');
78 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
79
80 done += 4;
81 }
82
83 if (done + 1 >= out_size)
84 return -1;
85
86 *out++ = '\0';
87
88 return done;
89 }
90
91 int
lws_b64_encode_string(const char * in,int in_len,char * out,int out_size)92 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
93 {
94 return _lws_b64_encode_string(encode_orig, in, in_len, out, out_size);
95 }
96
97 int
lws_b64_encode_string_url(const char * in,int in_len,char * out,int out_size)98 lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size)
99 {
100 return _lws_b64_encode_string(encode_url, in, in_len, out, out_size);
101 }
102
103
104 void
lws_b64_decode_state_init(struct lws_b64state * state)105 lws_b64_decode_state_init(struct lws_b64state *state)
106 {
107 memset(state, 0, sizeof(*state));
108 }
109
110 int
lws_b64_decode_stateful(struct lws_b64state * s,const char * in,size_t * in_len,uint8_t * out,size_t * out_size,int final)111 lws_b64_decode_stateful(struct lws_b64state *s, const char *in, size_t *in_len,
112 uint8_t *out, size_t *out_size, int final)
113 {
114 const char *orig_in = in, *end_in = in + *in_len;
115 uint8_t *orig_out = out, *end_out = out + *out_size;
116
117 while (in < end_in && *in && out + 4 < end_out) {
118
119 for (; s->i < 4 && in < end_in && *in; s->i++) {
120 uint8_t v;
121
122 v = 0;
123 s->c = 0;
124 while (in < end_in && *in && !v) {
125 s->c = v = *in++;
126 /* support the url base64 variant too */
127 if (v == '-')
128 s->c = v = '+';
129 if (v == '_')
130 s->c = v = '/';
131 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
132 if (v)
133 v = (v == '$') ? 0 : v - 61;
134 }
135 if (s->c) {
136 s->len++;
137 if (v)
138 s->quad[s->i] = v - 1;
139 } else
140 s->quad[s->i] = 0;
141 }
142
143 if (s->i != 4 && !final)
144 continue;
145
146 s->i = 0;
147
148 /*
149 * "The '==' sequence indicates that the last group contained
150 * only one byte, and '=' indicates that it contained two
151 * bytes." (wikipedia)
152 */
153
154 if ((in >= end_in || !*in) && s->c == '=')
155 s->len--;
156
157 if (s->len >= 2)
158 *out++ = s->quad[0] << 2 | s->quad[1] >> 4;
159 if (s->len >= 3)
160 *out++ = s->quad[1] << 4 | s->quad[2] >> 2;
161 if (s->len >= 4)
162 *out++ = ((s->quad[2] << 6) & 0xc0) | s->quad[3];
163
164 s->done += s->len - 1;
165 s->len = 0;
166 }
167
168 *out = '\0';
169 *in_len = in - orig_in;
170 *out_size = out - orig_out;
171
172 return 0;
173 }
174
175
176 /*
177 * returns length of decoded string in out, or -1 if out was too small
178 * according to out_size
179 *
180 * Only reads up to in_len chars, otherwise if in_len is -1 on entry reads until
181 * the first NUL in the input.
182 */
183
184 static size_t
_lws_b64_decode_string(const char * in,int in_len,char * out,int out_size)185 _lws_b64_decode_string(const char *in, int in_len, char *out, int out_size)
186 {
187 struct lws_b64state state;
188 size_t il = (size_t)in_len, ol = out_size;
189
190 if (in_len == -1)
191 il = strlen(in);
192
193 lws_b64_decode_state_init(&state);
194 lws_b64_decode_stateful(&state, in, &il, (uint8_t *)out, &ol, 1);
195
196 if (!il)
197 return 0;
198
199 return ol;
200 }
201
202 int
lws_b64_decode_string(const char * in,char * out,int out_size)203 lws_b64_decode_string(const char *in, char *out, int out_size)
204 {
205 return (int)_lws_b64_decode_string(in, -1, out, out_size);
206 }
207
208 int
lws_b64_decode_string_len(const char * in,int in_len,char * out,int out_size)209 lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size)
210 {
211 return (int)_lws_b64_decode_string(in, in_len, out, out_size);
212 }
213
214 #if 0
215 static const char * const plaintext[] = {
216 "any carnal pleasure.",
217 "any carnal pleasure",
218 "any carnal pleasur",
219 "any carnal pleasu",
220 "any carnal pleas",
221 "Admin:kloikloi"
222 };
223 static const char * const coded[] = {
224 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
225 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
226 "YW55IGNhcm5hbCBwbGVhc3Vy",
227 "YW55IGNhcm5hbCBwbGVhc3U=",
228 "YW55IGNhcm5hbCBwbGVhcw==",
229 "QWRtaW46a2xvaWtsb2k="
230 };
231
232 int
233 lws_b64_selftest(void)
234 {
235 char buf[64];
236 unsigned int n, r = 0;
237 unsigned int test;
238
239 lwsl_notice("%s\n", __func__);
240
241 /* examples from https://en.wikipedia.org/wiki/Base64 */
242
243 for (test = 0; test < (int)LWS_ARRAY_SIZE(plaintext); test++) {
244
245 buf[sizeof(buf) - 1] = '\0';
246 n = lws_b64_encode_string(plaintext[test],
247 strlen(plaintext[test]), buf, sizeof buf);
248 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
249 lwsl_err("Failed lws_b64 encode selftest "
250 "%d result '%s' %d\n", test, buf, n);
251 r = -1;
252 }
253
254 buf[sizeof(buf) - 1] = '\0';
255 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
256 if (n != strlen(plaintext[test]) ||
257 strcmp(buf, plaintext[test])) {
258 lwsl_err("Failed lws_b64 decode selftest "
259 "%d result '%s' / '%s', %d / %zu\n",
260 test, buf, plaintext[test], n,
261 strlen(plaintext[test]));
262 lwsl_hexdump_err(buf, n);
263 r = -1;
264 }
265 }
266
267 if (!r)
268 lwsl_notice("Base 64 selftests passed\n");
269 else
270 lwsl_notice("Base64 selftests failed\n");
271
272 return r;
273 }
274 #endif
275