blob: bb074fca9ac96d776da7552a18bb96b20d7f1e6b [file] [log] [blame]
Andy Greendf736162011-01-18 15:39:02 +00001/*
2 * This code originally came from here
3 *
4 * http://base64.sourceforge.net/b64.c
5 *
6 * with the following license:
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; this version is under LGPL2 like the rest of libwebsockets
Peter Pentcheve46f4122015-10-01 12:25:05 +030037 * since he explicitly allows sublicensing, but I give the URL above so you can
Andy Greendf736162011-01-18 15:39:02 +000038 * get the original with Bob's super-liberal terms directly if you prefer.
39 */
40
41
42#include <stdio.h>
43#include <string.h>
Joakim Soderberg4c531232013-02-06 15:26:58 +090044#include "private-libwebsockets.h"
Andy Greendf736162011-01-18 15:39:02 +000045
46static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz0123456789+/";
48static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
49 "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
50
Peter Pentchev9a4fef72013-03-30 09:52:21 +080051LWS_VISIBLE int
Andy Green4739e5c2011-01-22 12:51:57 +000052lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
Andy Greendf736162011-01-18 15:39:02 +000053{
54 unsigned char triple[3];
55 int i;
56 int len;
57 int line = 0;
58 int done = 0;
59
Andy Green4739e5c2011-01-22 12:51:57 +000060 while (in_len) {
Andy Greendf736162011-01-18 15:39:02 +000061 len = 0;
62 for (i = 0; i < 3; i++) {
Andy Green4739e5c2011-01-22 12:51:57 +000063 if (in_len) {
Andy Greendf736162011-01-18 15:39:02 +000064 triple[i] = *in++;
65 len++;
Andy Green4739e5c2011-01-22 12:51:57 +000066 in_len--;
Andy Greendf736162011-01-18 15:39:02 +000067 } else
68 triple[i] = 0;
69 }
Andy Greendf736162011-01-18 15:39:02 +000070
Andy Green6ee372f2012-04-09 15:09:01 +080071 if (done + 4 >= out_size)
72 return -1;
Andy Greendf736162011-01-18 15:39:02 +000073
Andy Green6ee372f2012-04-09 15:09:01 +080074 *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) |
Andy Greendf736162011-01-18 15:39:02 +000078 ((triple[2] & 0xc0) >> 6)] : '=');
Andy Green6ee372f2012-04-09 15:09:01 +080079 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
Andy Greendf736162011-01-18 15:39:02 +000080
Andy Green6ee372f2012-04-09 15:09:01 +080081 done += 4;
82 line += 4;
Andy Greendf736162011-01-18 15:39:02 +000083 }
84
85 if (done + 1 >= out_size)
86 return -1;
87
88 *out++ = '\0';
89
90 return done;
91}
Andy Greencade6142014-09-30 16:35:16 +080092
93/*
94 * returns length of decoded string in out, or -1 if out was too small
95 * according to out_size
96 */
97
98LWS_VISIBLE int
99lws_b64_decode_string(const char *in, char *out, int out_size)
100{
101 int len;
102 int i;
103 int done = 0;
104 unsigned char v;
105 unsigned char quad[4];
106
107 while (*in) {
108
109 len = 0;
110 for (i = 0; i < 4 && *in; i++) {
111
112 v = 0;
113 while (*in && !v) {
114
115 v = *in++;
116 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
117 if (v)
118 v = (v == '$') ? 0 : v - 61;
119 if (*in) {
120 len++;
121 if (v)
122 quad[i] = v - 1;
123 } else
124 quad[i] = 0;
125 }
126 }
Andy Greencade6142014-09-30 16:35:16 +0800127
128 if (out_size < (done + len - 1))
129 /* out buffer is too small */
130 return -1;
131
132 if (len >= 2)
133 *out++ = quad[0] << 2 | quad[1] >> 4;
134 if (len >= 3)
135 *out++ = quad[1] << 4 | quad[2] >> 2;
136 if (len >= 4)
137 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
138
139 done += len - 1;
140 }
141
142 if (done + 1 >= out_size)
143 return -1;
144
145 *out++ = '\0';
146
147 return done;
148}
149
150int
151lws_b64_selftest(void)
152{
153 char buf[64];
Andy Green2cd30742015-11-02 13:10:33 +0800154 unsigned int n;
155 unsigned int test;
Andy Greencade6142014-09-30 16:35:16 +0800156 static const char * const plaintext[] = {
157 "sanity check base 64"
158 };
159 static const char * const coded[] = {
160 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
161 };
162
163 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
164
165 buf[sizeof(buf) - 1] = '\0';
166 n = lws_b64_encode_string(plaintext[test],
167 strlen(plaintext[test]), buf, sizeof buf);
168 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
169 lwsl_err("Failed lws_b64 encode selftest "
170 "%d result '%s' %d\n", test, buf, n);
171 return -1;
172 }
173
174 buf[sizeof(buf) - 1] = '\0';
175 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
176 if (n != strlen(plaintext[test]) ||
177 strcmp(buf, plaintext[test])) {
178 lwsl_err("Failed lws_b64 decode selftest "
179 "%d result '%s' %d\n", test, buf, n);
180 return -1;
181 }
182 }
183
184 return 0;
185}