private.h: rename to contain dir
[platform/upstream/libwebsockets.git] / lib / misc / base64-decode.c
1 /*
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.1 + SLE like the rest of lws
37  * since he explicitly 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 #include <stdio.h>
42 #include <string.h>
43 #include "private-lib-core.h"
44
45 static const char encode_orig[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46                              "abcdefghijklmnopqrstuvwxyz0123456789+/";
47 static const char encode_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48                              "abcdefghijklmnopqrstuvwxyz0123456789-_";
49 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
50                              "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
51
52 static int
53 _lws_b64_encode_string(const char *encode, const char *in, int in_len,
54                        char *out, int out_size)
55 {
56         unsigned char triple[3];
57         int i;
58         int line = 0;
59         int done = 0;
60
61         while (in_len) {
62                 int len = 0;
63                 for (i = 0; i < 3; i++) {
64                         if (in_len) {
65                                 triple[i] = *in++;
66                                 len++;
67                                 in_len--;
68                         } else
69                                 triple[i] = 0;
70                 }
71
72                 if (done + 4 >= out_size)
73                         return -1;
74
75                 *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) |
79                                              ((triple[2] & 0xc0) >> 6)] : '=');
80                 *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
81
82                 done += 4;
83                 line += 4;
84         }
85
86         if (done + 1 >= out_size)
87                 return -1;
88
89         *out++ = '\0';
90
91         return done;
92 }
93
94 LWS_VISIBLE int
95 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
96 {
97         return _lws_b64_encode_string(encode_orig, in, in_len, out, out_size);
98 }
99
100 LWS_VISIBLE int
101 lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size)
102 {
103         return _lws_b64_encode_string(encode_url, in, in_len, out, out_size);
104 }
105
106 /*
107  * returns length of decoded string in out, or -1 if out was too small
108  * according to out_size
109  *
110  * Only reads up to in_len chars, otherwise if in_len is -1 on entry reads until
111  * the first NUL in the input.
112  */
113
114 static int
115 _lws_b64_decode_string(const char *in, int in_len, char *out, int out_size)
116 {
117         int len, i, c = 0, done = 0;
118         unsigned char v, quad[4];
119
120         while (in_len && *in) {
121
122                 len = 0;
123                 for (i = 0; i < 4 && in_len && *in; i++) {
124
125                         v = 0;
126                         c = 0;
127                         while (in_len && *in  && !v) {
128                                 c = v = *in++;
129                                 in_len--;
130                                 /* support the url base64 variant too */
131                                 if (v == '-')
132                                         c = v = '+';
133                                 if (v == '_')
134                                         c = v = '/';
135                                 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
136                                 if (v)
137                                         v = (v == '$') ? 0 : v - 61;
138                         }
139                         if (c) {
140                                 len++;
141                                 if (v)
142                                         quad[i] = v - 1;
143                         } else
144                                 quad[i] = 0;
145                 }
146
147                 if (out_size < (done + len + 1))
148                         /* out buffer is too small */
149                         return -1;
150
151                 /*
152                  * "The '==' sequence indicates that the last group contained
153                  * only one byte, and '=' indicates that it contained two
154                  * bytes." (wikipedia)
155                  */
156
157                 if ((!in_len || !*in) && c == '=')
158                         len--;
159
160                 if (len >= 2)
161                         *out++ = quad[0] << 2 | quad[1] >> 4;
162                 if (len >= 3)
163                         *out++ = quad[1] << 4 | quad[2] >> 2;
164                 if (len >= 4)
165                         *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
166
167                 done += len - 1;
168         }
169
170         if (done + 1 >= out_size)
171                 return -1;
172
173         *out = '\0';
174
175         return done;
176 }
177
178 LWS_VISIBLE int
179 lws_b64_decode_string(const char *in, char *out, int out_size)
180 {
181         return _lws_b64_decode_string(in, -1, out, out_size);
182 }
183
184 LWS_VISIBLE int
185 lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size)
186 {
187         return _lws_b64_decode_string(in, in_len, out, out_size);
188 }
189
190 #if 0
191 int
192 lws_b64_selftest(void)
193 {
194         char buf[64];
195         unsigned int n,  r = 0;
196         unsigned int test;
197         /* examples from https://en.wikipedia.org/wiki/Base64 */
198         static const char * const plaintext[] = {
199                 "any carnal pleasure.",
200                 "any carnal pleasure",
201                 "any carnal pleasur",
202                 "any carnal pleasu",
203                 "any carnal pleas",
204                 "Admin:kloikloi"
205         };
206         static const char * const coded[] = {
207                 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
208                 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
209                 "YW55IGNhcm5hbCBwbGVhc3Vy",
210                 "YW55IGNhcm5hbCBwbGVhc3U=",
211                 "YW55IGNhcm5hbCBwbGVhcw==",
212                 "QWRtaW46a2xvaWtsb2k="
213         };
214
215         for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
216
217                 buf[sizeof(buf) - 1] = '\0';
218                 n = lws_b64_encode_string(plaintext[test],
219                                       strlen(plaintext[test]), buf, sizeof buf);
220                 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
221                         lwsl_err("Failed lws_b64 encode selftest "
222                                            "%d result '%s' %d\n", test, buf, n);
223                         r = -1;
224                 }
225
226                 buf[sizeof(buf) - 1] = '\0';
227                 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
228                 if (n != strlen(plaintext[test]) ||
229                                                  strcmp(buf, plaintext[test])) {
230                         lwsl_err("Failed lws_b64 decode selftest "
231                                  "%d result '%s' / '%s', %d / %d\n",
232                                  test, buf, plaintext[test], n, strlen(plaintext[test]));
233                         r = -1;
234                 }
235         }
236
237         lwsl_notice("Base 64 selftests passed\n");
238
239         return r;
240 }
241 #endif