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 like the rest of libwebsockets
37 * since he explictly 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.
44 #include "private-libwebsockets.h"
46 static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz0123456789+/";
48 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
49 "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
52 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
54 unsigned char triple[3];
62 for (i = 0; i < 3; i++) {
73 if (done + 4 >= out_size)
76 *out++ = encode[triple[0] >> 2];
77 *out++ = encode[((triple[0] & 0x03) << 4) |
78 ((triple[1] & 0xf0) >> 4)];
79 *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
80 ((triple[2] & 0xc0) >> 6)] : '=');
81 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
87 if (done + 1 >= out_size)
96 * returns length of decoded string in out, or -1 if out was too small
97 * according to out_size
101 lws_b64_decode_string(const char *in, char *out, int out_size)
107 unsigned char quad[4];
112 for (i = 0; i < 4 && *in; i++) {
118 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
120 v = (v == '$') ? 0 : v - 61;
132 if (out_size < (done + len - 1))
133 /* out buffer is too small */
137 *out++ = quad[0] << 2 | quad[1] >> 4;
139 *out++ = quad[1] << 4 | quad[2] >> 2;
141 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
146 if (done + 1 >= out_size)
155 lws_b64_selftest(void)
160 static const char * const plaintext[] = {
161 "sanity check base 64"
163 static const char * const coded[] = {
164 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
167 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
169 buf[sizeof(buf) - 1] = '\0';
170 n = lws_b64_encode_string(plaintext[test],
171 strlen(plaintext[test]), buf, sizeof buf);
172 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
173 lwsl_err("Failed lws_b64 encode selftest "
174 "%d result '%s' %d\n", test, buf, n);
178 buf[sizeof(buf) - 1] = '\0';
179 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
180 if (n != strlen(plaintext[test]) ||
181 strcmp(buf, plaintext[test])) {
182 lwsl_err("Failed lws_b64 decode selftest "
183 "%d result '%s' %d\n", test, buf, n);