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 "private-lib-core.h"
40
41 #include <stdio.h>
42 #include <string.h>
43
44 static const char encode_orig[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 "abcdefghijklmnopqrstuvwxyz0123456789+/";
46 static const char encode_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz0123456789-_";
48 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
49 "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
50
51 static int
_lws_b64_encode_string(const char * encode,const char * in,int in_len,char * out,int out_size)52 _lws_b64_encode_string(const char *encode, const char *in, int in_len,
53 char *out, int out_size)
54 {
55 unsigned char triple[3];
56 int i, done = 0;
57
58 while (in_len) {
59 int len = 0;
60 for (i = 0; i < 3; i++) {
61 if (in_len) {
62 triple[i] = (unsigned char)*in++;
63 len++;
64 in_len--;
65 } else
66 triple[i] = 0;
67 }
68
69 if (done + 4 >= out_size)
70 return -1;
71
72 *out++ = encode[triple[0] >> 2];
73 *out++ = encode[(((triple[0] & 0x03) << 4) & 0x30) |
74 (((triple[1] & 0xf0) >> 4) & 0x0f)];
75 *out++ = (char)(len > 1 ? encode[(((triple[1] & 0x0f) << 2) & 0x3c) |
76 (((triple[2] & 0xc0) >> 6) & 3)] : '=');
77 *out++ = (char)(len > 2 ? encode[triple[2] & 0x3f] : '=');
78
79 done += 4;
80 }
81
82 if (done + 1 >= out_size)
83 return -1;
84
85 *out++ = '\0';
86
87 return done;
88 }
89
90 int
lws_b64_encode_string(const char * in,int in_len,char * out,int out_size)91 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
92 {
93 return _lws_b64_encode_string(encode_orig, in, in_len, out, out_size);
94 }
95
96 int
lws_b64_encode_string_url(const char * in,int in_len,char * out,int out_size)97 lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size)
98 {
99 return _lws_b64_encode_string(encode_url, in, in_len, out, out_size);
100 }
101
102
103 void
lws_b64_decode_state_init(struct lws_b64state * state)104 lws_b64_decode_state_init(struct lws_b64state *state)
105 {
106 memset(state, 0, sizeof(*state));
107 }
108
109 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)110 lws_b64_decode_stateful(struct lws_b64state *s, const char *in, size_t *in_len,
111 uint8_t *out, size_t *out_size, int final)
112 {
113 const char *orig_in = in, *end_in = in + *in_len;
114 uint8_t *orig_out = out, *end_out = out + *out_size;
115
116 while (in < end_in && *in && out + 4 < end_out) {
117
118 for (; s->i < 4 && in < end_in && *in; s->i++) {
119 uint8_t v;
120
121 v = 0;
122 s->c = 0;
123 while (in < end_in && *in && !v) {
124 s->c = v = (unsigned char)*in++;
125 /* support the url base64 variant too */
126 if (v == '-')
127 s->c = v = '+';
128 if (v == '_')
129 s->c = v = '/';
130 v = (uint8_t)((v < 43 || v > 122) ? 0 : decode[v - 43]);
131 if (v)
132 v = (uint8_t)((v == '$') ? 0 : v - 61);
133 }
134 if (s->c) {
135 s->len++;
136 if (v)
137 s->quad[s->i] = (uint8_t)(v - 1);
138 } else
139 s->quad[s->i] = 0;
140 }
141
142 if (s->i != 4 && !final)
143 continue;
144
145 s->i = 0;
146
147 /*
148 * "The '==' sequence indicates that the last group contained
149 * only one byte, and '=' indicates that it contained two
150 * bytes." (wikipedia)
151 */
152
153 if ((in >= end_in || !*in) && s->c == '=')
154 s->len--;
155
156 if (s->len >= 2)
157 *out++ = (uint8_t)(s->quad[0] << 2 | s->quad[1] >> 4);
158 if (s->len >= 3)
159 *out++ = (uint8_t)(s->quad[1] << 4 | s->quad[2] >> 2);
160 if (s->len >= 4)
161 *out++ = (uint8_t)(((s->quad[2] << 6) & 0xc0) | s->quad[3]);
162
163 s->done += s->len - 1;
164 s->len = 0;
165 }
166
167 *out = '\0';
168 *in_len = (unsigned int)(in - orig_in);
169 *out_size = (unsigned int)(out - orig_out);
170
171 return 0;
172 }
173
174
175 /*
176 * returns length of decoded string in out, or -1 if out was too small
177 * according to out_size
178 *
179 * Only reads up to in_len chars, otherwise if in_len is -1 on entry reads until
180 * the first NUL in the input.
181 */
182
183 static size_t
_lws_b64_decode_string(const char * in,int in_len,char * out,size_t out_size)184 _lws_b64_decode_string(const char *in, int in_len, char *out, size_t out_size)
185 {
186 struct lws_b64state state;
187 size_t il = (size_t)in_len, ol = out_size;
188
189 if (in_len == -1)
190 il = strlen(in);
191
192 lws_b64_decode_state_init(&state);
193 lws_b64_decode_stateful(&state, in, &il, (uint8_t *)out, &ol, 1);
194
195 if (!il)
196 return 0;
197
198 return ol;
199 }
200
201 int
lws_b64_decode_string(const char * in,char * out,int out_size)202 lws_b64_decode_string(const char *in, char *out, int out_size)
203 {
204 return (int)_lws_b64_decode_string(in, -1, out, (unsigned int)out_size);
205 }
206
207 int
lws_b64_decode_string_len(const char * in,int in_len,char * out,int out_size)208 lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size)
209 {
210 return (int)_lws_b64_decode_string(in, in_len, out, (unsigned int)out_size);
211 }
212
213 #if 0
214 static const char * const plaintext[] = {
215 "any carnal pleasure.",
216 "any carnal pleasure",
217 "any carnal pleasur",
218 "any carnal pleasu",
219 "any carnal pleas",
220 "Admin:kloikloi"
221 };
222 static const char * const coded[] = {
223 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
224 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
225 "YW55IGNhcm5hbCBwbGVhc3Vy",
226 "YW55IGNhcm5hbCBwbGVhc3U=",
227 "YW55IGNhcm5hbCBwbGVhcw==",
228 "QWRtaW46a2xvaWtsb2k="
229 };
230
231 int
232 lws_b64_selftest(void)
233 {
234 char buf[64];
235 unsigned int n, r = 0;
236 unsigned int test;
237
238 lwsl_notice("%s\n", __func__);
239
240 /* examples from https://en.wikipedia.org/wiki/Base64 */
241
242 for (test = 0; test < (int)LWS_ARRAY_SIZE(plaintext); test++) {
243
244 buf[sizeof(buf) - 1] = '\0';
245 n = lws_b64_encode_string(plaintext[test],
246 strlen(plaintext[test]), buf, sizeof buf);
247 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
248 lwsl_err("Failed lws_b64 encode selftest "
249 "%d result '%s' %d\n", test, buf, n);
250 r = -1;
251 }
252
253 buf[sizeof(buf) - 1] = '\0';
254 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
255 if (n != strlen(plaintext[test]) ||
256 strcmp(buf, plaintext[test])) {
257 lwsl_err("Failed lws_b64 decode selftest "
258 "%d result '%s' / '%s', %d / %zu\n",
259 test, buf, plaintext[test], n,
260 strlen(plaintext[test]));
261 lwsl_hexdump_err(buf, n);
262 r = -1;
263 }
264 }
265
266 if (!r)
267 lwsl_notice("Base 64 selftests passed\n");
268 else
269 lwsl_notice("Base64 selftests failed\n");
270
271 return r;
272 }
273 #endif
274