blob: e0206c8de8285022ddc1715d2aaf0625cb5f0027 [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
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.
39 */
40
41
42#include <stdio.h>
43#include <string.h>
44
45static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46 "abcdefghijklmnopqrstuvwxyz0123456789+/";
47static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
48 "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
49
50int
Andy Green4739e5c2011-01-22 12:51:57 +000051lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
Andy Greendf736162011-01-18 15:39:02 +000052{
53 unsigned char triple[3];
54 int i;
55 int len;
56 int line = 0;
57 int done = 0;
58
Andy Green4739e5c2011-01-22 12:51:57 +000059 while (in_len) {
Andy Greendf736162011-01-18 15:39:02 +000060 len = 0;
61 for (i = 0; i < 3; i++) {
Andy Green4739e5c2011-01-22 12:51:57 +000062 if (in_len) {
Andy Greendf736162011-01-18 15:39:02 +000063 triple[i] = *in++;
64 len++;
Andy Green4739e5c2011-01-22 12:51:57 +000065 in_len--;
Andy Greendf736162011-01-18 15:39:02 +000066 } else
67 triple[i] = 0;
68 }
69 if (len) {
70
71 if (done + 4 >= out_size)
72 return -1;
73
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] : '=');
80
81 done += 4;
82 line += 4;
83 }
84 if (line >= 72) {
85
86 if (done + 2 >= out_size)
87 return -1;
88
89 *out++ = '\r';
90 *out++ = '\n';
91 done += 2;
92 line = 0;
93 }
94 }
95
96 if (done + 1 >= out_size)
97 return -1;
98
99 *out++ = '\0';
100
101 return done;
102}
103
104/*
105 * returns length of decoded string in out, or -1 if out was too small
106 * according to out_size
107 */
108
109int
110lws_b64_decode_string(const char *in, char *out, int out_size)
111{
112 int len;
113 int i;
114 int done = 0;
115 unsigned char v;
116 unsigned char quad[4];
117
118 while (*in) {
119
120 len = 0;
121 for (i = 0; i < 4 && *in; i++) {
122
123 v = 0;
124 while (*in && !v) {
125
126 v = *in++;
127 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
128 if (v)
129 v = (v == '$') ? 0 : v - 61;
130 if (*in) {
131 len++;
132 if (v)
133 quad[i] = v - 1;
134 } else
135 quad[i] = 0;
136 }
137 }
138 if (!len)
139 continue;
140
141 if (out_size < (done + len - 1))
142 /* out buffer is too small */
143 return -1;
144
145 if (len >= 2)
146 *out++ = quad[0] << 2 | quad[1] >> 4;
147 if (len >= 3)
148 *out++ = quad[1] << 4 | quad[2] >> 2;
149 if (len >= 4)
150 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
151
152 done += len - 1;
153 }
154
155 if (done + 1 >= out_size)
156 return -1;
157
158 *out++ = '\0';
159
160 return done;
161}
162
163int
164lws_b64_selftest(void)
165{
166 char buf[64];
167 int n;
168 int test;
169 static const char *plaintext[] = {
170 "sanity check base 64"
171 };
172 static const char *coded[] = {
173 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
174 };
175
176 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
177
178 buf[sizeof(buf) - 1] = '\0';
Andy Green4739e5c2011-01-22 12:51:57 +0000179 n = lws_b64_encode_string(plaintext[test],
180 strlen(plaintext[test]), buf, sizeof buf);
Andy Greendf736162011-01-18 15:39:02 +0000181 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);
184 return -1;
185 }
186
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);
193 return -1;
194 }
195 }
196
197 return 0;
198}