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.
45 static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46 "abcdefghijklmnopqrstuvwxyz0123456789+/";
47 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
48 "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
51 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
53 unsigned char triple[3];
61 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] : '=');
86 if (done + 2 >= out_size)
96 if (done + 1 >= out_size)
105 * returns length of decoded string in out, or -1 if out was too small
106 * according to out_size
110 lws_b64_decode_string(const char *in, char *out, int out_size)
116 unsigned char quad[4];
121 for (i = 0; i < 4 && *in; i++) {
127 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
129 v = (v == '$') ? 0 : v - 61;
141 if (out_size < (done + len - 1))
142 /* out buffer is too small */
146 *out++ = quad[0] << 2 | quad[1] >> 4;
148 *out++ = quad[1] << 4 | quad[2] >> 2;
150 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
155 if (done + 1 >= out_size)
164 lws_b64_selftest(void)
169 static const char *plaintext[] = {
170 "sanity check base 64"
172 static const char *coded[] = {
173 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
176 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
178 buf[sizeof(buf) - 1] = '\0';
179 n = lws_b64_encode_string(plaintext[test],
180 strlen(plaintext[test]), buf, sizeof buf);
181 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
182 fprintf(stderr, "Failed lws_b64 encode selftest "
183 "%d result '%s' %d\n", test, buf, n);
187 buf[sizeof(buf) - 1] = '\0';
188 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
189 if (n != strlen(plaintext[test]) ||
190 strcmp(buf, plaintext[test])) {
191 fprintf(stderr, "Failed lws_b64 decode selftest "
192 "%d result '%s' %d\n", test, buf, n);