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.
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++) {
71 if (done + 4 >= out_size)
74 *out++ = encode[triple[0] >> 2];
75 *out++ = encode[((triple[0] & 0x03) << 4) |
76 ((triple[1] & 0xf0) >> 4)];
77 *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
78 ((triple[2] & 0xc0) >> 6)] : '=');
79 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
85 if (done + 1 >= out_size)
94 * returns length of decoded string in out, or -1 if out was too small
95 * according to out_size
99 lws_b64_decode_string(const char *in, char *out, int out_size)
105 unsigned char quad[4];
110 for (i = 0; i < 4 && *in; i++) {
116 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
118 v = (v == '$') ? 0 : v - 61;
128 if (out_size < (done + len - 1))
129 /* out buffer is too small */
133 *out++ = quad[0] << 2 | quad[1] >> 4;
135 *out++ = quad[1] << 4 | quad[2] >> 2;
137 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
142 if (done + 1 >= out_size)
152 lws_b64_selftest(void)
157 static const char * const plaintext[] = {
158 "sanity check base 64"
160 static const char * const coded[] = {
161 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
164 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
166 buf[sizeof(buf) - 1] = '\0';
167 n = lws_b64_encode_string(plaintext[test],
168 strlen(plaintext[test]), buf, sizeof buf);
169 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
170 lwsl_err("Failed lws_b64 encode selftest "
171 "%d result '%s' %d\n", test, buf, n);
175 buf[sizeof(buf) - 1] = '\0';
176 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
177 if (n != strlen(plaintext[test]) ||
178 strcmp(buf, plaintext[test])) {
179 lwsl_err("Failed lws_b64 decode selftest "
180 "%d result '%s' %d\n", test, buf, n);