2 * This code originally came from here
4 * http://base64.sourceforge.net/b64.c
6 * with the following license:
8 * LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
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:
19 * The above copyright notice and this permission notice shall
20 * be included in all copies or substantial portions of the
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.
33 * Bob Trower 08/04/01 -- Create Version 0.00.00B
35 * I cleaned it up quite a bit to match the (linux kernel) style of the rest
36 * of libwebsockets; this version is under LGPL2.1 + SLE like the rest of lws
37 * since he explicitly allows sublicensing, but I give the URL above so you can
38 * get the original with Bob's super-liberal terms directly if you prefer.
43 #include "private-lib-core.h"
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";
53 _lws_b64_encode_string(const char *encode, const char *in, int in_len,
54 char *out, int out_size)
56 unsigned char triple[3];
63 for (i = 0; i < 3; i++) {
72 if (done + 4 >= out_size)
75 *out++ = encode[triple[0] >> 2];
76 *out++ = encode[((triple[0] & 0x03) << 4) |
77 ((triple[1] & 0xf0) >> 4)];
78 *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
79 ((triple[2] & 0xc0) >> 6)] : '=');
80 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
86 if (done + 1 >= out_size)
95 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
97 return _lws_b64_encode_string(encode_orig, in, in_len, out, out_size);
101 lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size)
103 return _lws_b64_encode_string(encode_url, in, in_len, out, out_size);
107 * returns length of decoded string in out, or -1 if out was too small
108 * according to out_size
110 * Only reads up to in_len chars, otherwise if in_len is -1 on entry reads until
111 * the first NUL in the input.
115 _lws_b64_decode_string(const char *in, int in_len, char *out, int out_size)
117 int len, i, c = 0, done = 0;
118 unsigned char v, quad[4];
120 while (in_len && *in) {
123 for (i = 0; i < 4 && in_len && *in; i++) {
127 while (in_len && *in && !v) {
130 /* support the url base64 variant too */
135 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
137 v = (v == '$') ? 0 : v - 61;
147 if (out_size < (done + len + 1))
148 /* out buffer is too small */
152 * "The '==' sequence indicates that the last group contained
153 * only one byte, and '=' indicates that it contained two
154 * bytes." (wikipedia)
157 if ((!in_len || !*in) && c == '=')
161 *out++ = quad[0] << 2 | quad[1] >> 4;
163 *out++ = quad[1] << 4 | quad[2] >> 2;
165 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
170 if (done + 1 >= out_size)
179 lws_b64_decode_string(const char *in, char *out, int out_size)
181 return _lws_b64_decode_string(in, -1, out, out_size);
185 lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size)
187 return _lws_b64_decode_string(in, in_len, out, out_size);
192 lws_b64_selftest(void)
195 unsigned int n, r = 0;
197 /* examples from https://en.wikipedia.org/wiki/Base64 */
198 static const char * const plaintext[] = {
199 "any carnal pleasure.",
200 "any carnal pleasure",
201 "any carnal pleasur",
206 static const char * const coded[] = {
207 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
208 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
209 "YW55IGNhcm5hbCBwbGVhc3Vy",
210 "YW55IGNhcm5hbCBwbGVhc3U=",
211 "YW55IGNhcm5hbCBwbGVhcw==",
212 "QWRtaW46a2xvaWtsb2k="
215 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
217 buf[sizeof(buf) - 1] = '\0';
218 n = lws_b64_encode_string(plaintext[test],
219 strlen(plaintext[test]), buf, sizeof buf);
220 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
221 lwsl_err("Failed lws_b64 encode selftest "
222 "%d result '%s' %d\n", test, buf, n);
226 buf[sizeof(buf) - 1] = '\0';
227 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
228 if (n != strlen(plaintext[test]) ||
229 strcmp(buf, plaintext[test])) {
230 lwsl_err("Failed lws_b64 decode selftest "
231 "%d result '%s' / '%s', %d / %d\n",
232 test, buf, plaintext[test], n, strlen(plaintext[test]));
237 lwsl_notice("Base 64 selftests passed\n");