blob: 95d4ba4ac2f059fe89ff8cc0ac35cd5b4fa530c9 [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 }
Andy Green6ee372f2012-04-09 15:09:01 +080069 if (!len)
70 continue;
Andy Greendf736162011-01-18 15:39:02 +000071
Andy Green6ee372f2012-04-09 15:09:01 +080072 if (done + 4 >= out_size)
73 return -1;
Andy Greendf736162011-01-18 15:39:02 +000074
Andy Green6ee372f2012-04-09 15:09:01 +080075 *out++ = encode[triple[0] >> 2];
76 *out++ = encode[((triple[0] & 0x03) << 4) |
77 ((triple[1] & 0xf0) >> 4)];
78 *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
Andy Greendf736162011-01-18 15:39:02 +000079 ((triple[2] & 0xc0) >> 6)] : '=');
Andy Green6ee372f2012-04-09 15:09:01 +080080 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
Andy Greendf736162011-01-18 15:39:02 +000081
Andy Green6ee372f2012-04-09 15:09:01 +080082 done += 4;
83 line += 4;
Andy Greendf736162011-01-18 15:39:02 +000084 }
85
86 if (done + 1 >= out_size)
87 return -1;
88
89 *out++ = '\0';
90
91 return done;
92}
93
94/*
95 * returns length of decoded string in out, or -1 if out was too small
96 * according to out_size
97 */
98
99int
100lws_b64_decode_string(const char *in, char *out, int out_size)
101{
102 int len;
103 int i;
104 int done = 0;
105 unsigned char v;
106 unsigned char quad[4];
107
108 while (*in) {
109
110 len = 0;
111 for (i = 0; i < 4 && *in; i++) {
112
113 v = 0;
114 while (*in && !v) {
115
116 v = *in++;
117 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
118 if (v)
119 v = (v == '$') ? 0 : v - 61;
120 if (*in) {
121 len++;
122 if (v)
123 quad[i] = v - 1;
124 } else
125 quad[i] = 0;
126 }
127 }
128 if (!len)
129 continue;
130
131 if (out_size < (done + len - 1))
132 /* out buffer is too small */
133 return -1;
134
135 if (len >= 2)
136 *out++ = quad[0] << 2 | quad[1] >> 4;
137 if (len >= 3)
138 *out++ = quad[1] << 4 | quad[2] >> 2;
139 if (len >= 4)
140 *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
141
142 done += len - 1;
143 }
144
145 if (done + 1 >= out_size)
146 return -1;
147
148 *out++ = '\0';
149
150 return done;
151}
152
153int
154lws_b64_selftest(void)
155{
156 char buf[64];
157 int n;
158 int test;
Andy Green6ee372f2012-04-09 15:09:01 +0800159 static const char * const plaintext[] = {
Andy Greendf736162011-01-18 15:39:02 +0000160 "sanity check base 64"
161 };
Andy Green6ee372f2012-04-09 15:09:01 +0800162 static const char * const coded[] = {
Andy Greendf736162011-01-18 15:39:02 +0000163 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
164 };
165
166 for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
167
168 buf[sizeof(buf) - 1] = '\0';
Andy Green4739e5c2011-01-22 12:51:57 +0000169 n = lws_b64_encode_string(plaintext[test],
170 strlen(plaintext[test]), buf, sizeof buf);
Andy Greendf736162011-01-18 15:39:02 +0000171 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
172 fprintf(stderr, "Failed lws_b64 encode selftest "
173 "%d result '%s' %d\n", test, buf, n);
174 return -1;
175 }
176
177 buf[sizeof(buf) - 1] = '\0';
178 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
179 if (n != strlen(plaintext[test]) ||
180 strcmp(buf, plaintext[test])) {
181 fprintf(stderr, "Failed lws_b64 decode selftest "
182 "%d result '%s' %d\n", test, buf, n);
183 return -1;
184 }
185 }
186
187 return 0;
188}