blob: d38ba45d640e3bddd0ee919ab6dbfd01f8f386e0 [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
51lws_b64_encode_string(const char *in, char *out, int out_size)
52{
53 unsigned char triple[3];
54 int i;
55 int len;
56 int line = 0;
57 int done = 0;
58
59 while (*in) {
60 len = 0;
61 for (i = 0; i < 3; i++) {
62 if (*in) {
63 triple[i] = *in++;
64 len++;
65 } else
66 triple[i] = 0;
67 }
68 if (len) {
69
70 if (done + 4 >= out_size)
71 return -1;
72
73 *out++ = encode[triple[0] >> 2];
74 *out++ = encode[((triple[0] & 0x03) << 4) |
75 ((triple[1] & 0xf0) >> 4)];
76 *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
77 ((triple[2] & 0xc0) >> 6)] : '=');
78 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
79
80 done += 4;
81 line += 4;
82 }
83 if (line >= 72) {
84
85 if (done + 2 >= out_size)
86 return -1;
87
88 *out++ = '\r';
89 *out++ = '\n';
90 done += 2;
91 line = 0;
92 }
93 }
94
95 if (done + 1 >= out_size)
96 return -1;
97
98 *out++ = '\0';
99
100 return done;
101}
102
103/*
104 * returns length of decoded string in out, or -1 if out was too small
105 * according to out_size
106 */
107
108int
109lws_b64_decode_string(const char *in, char *out, int out_size)
110{
111 int len;
112 int i;
113 int done = 0;
114 unsigned char v;
115 unsigned char quad[4];
116
117 while (*in) {
118
119 len = 0;
120 for (i = 0; i < 4 && *in; i++) {
121
122 v = 0;
123 while (*in && !v) {
124
125 v = *in++;
126 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
127 if (v)
128 v = (v == '$') ? 0 : v - 61;
129 if (*in) {
130 len++;
131 if (v)
132 quad[i] = v - 1;
133 } else
134 quad[i] = 0;
135 }
136 }
137 if (!len)
138 continue;
139
140 if (out_size < (done + len - 1))
141 /* out buffer is too small */
142 return -1;
143
144 if (len >= 2)
145 *out++ = quad[0] << 2 | quad[1] >> 4;
146 if (len >= 3)
147 *out++ = quad[1] << 4 | quad[2] >> 2;
148 if (len >= 4)
149 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
150
151 done += len - 1;
152 }
153
154 if (done + 1 >= out_size)
155 return -1;
156
157 *out++ = '\0';
158
159 return done;
160}
161
162int
163lws_b64_selftest(void)
164{
165 char buf[64];
166 int n;
167 int test;
168 static const char *plaintext[] = {
169 "sanity check base 64"
170 };
171 static const char *coded[] = {
172 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
173 };
174
175 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
176
177 buf[sizeof(buf) - 1] = '\0';
178 n = lws_b64_encode_string(plaintext[test], buf, sizeof buf);
179 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
180 fprintf(stderr, "Failed lws_b64 encode selftest "
181 "%d result '%s' %d\n", test, buf, n);
182 return -1;
183 }
184
185 buf[sizeof(buf) - 1] = '\0';
186 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
187 if (n != strlen(plaintext[test]) ||
188 strcmp(buf, plaintext[test])) {
189 fprintf(stderr, "Failed lws_b64 decode selftest "
190 "%d result '%s' %d\n", test, buf, n);
191 return -1;
192 }
193 }
194
195 return 0;
196}