Subject: LWS_UNUSED
[platform/upstream/libwebsockets.git] / lib / 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-libwebsockets.h"
44
45 static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46                              "abcdefghijklmnopqrstuvwxyz0123456789+/";
47 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
48                              "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
49
50 LWS_VISIBLE int
51 lws_b64_encode_string(const char *in, int in_len, 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_len) {
60                 len = 0;
61                 for (i = 0; i < 3; i++) {
62                         if (in_len) {
63                                 triple[i] = *in++;
64                                 len++;
65                                 in_len--;
66                         } else
67                                 triple[i] = 0;
68                 }
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
84         if (done + 1 >= out_size)
85                 return -1;
86
87         *out++ = '\0';
88
89         return done;
90 }
91
92 /*
93  * returns length of decoded string in out, or -1 if out was too small
94  * according to out_size
95  */
96
97 LWS_VISIBLE int
98 lws_b64_decode_string(const char *in, char *out, int out_size)
99 {
100         int len, i, c = 0, done = 0;
101         unsigned char v, quad[4];
102
103         while (*in) {
104
105                 len = 0;
106                 for (i = 0; i < 4 && *in; i++) {
107
108                         v = 0;
109                         c = 0;
110                         while (*in && !v) {
111                                 c = v = *in++;
112                                 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
113                                 if (v)
114                                         v = (v == '$') ? 0 : v - 61;
115                         }
116                         if (c) {
117                                 len++;
118                                 if (v)
119                                         quad[i] = v - 1;
120                         } else
121                                 quad[i] = 0;
122                 }
123
124                 if (out_size < (done + len - 1))
125                         /* out buffer is too small */
126                         return -1;
127
128                 /*
129                  * "The '==' sequence indicates that the last group contained
130                  * only one byte, and '=' indicates that it contained two
131                  * bytes." (wikipedia)
132                  */
133
134                 if (!*in && c == '=')
135                         len--;
136
137                 if (len >= 2)
138                         *out++ = quad[0] << 2 | quad[1] >> 4;
139                 if (len >= 3)
140                         *out++ = quad[1] << 4 | quad[2] >> 2;
141                 if (len >= 4)
142                         *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
143
144                 done += len - 1;
145         }
146
147         if (done + 1 >= out_size)
148                 return -1;
149
150         *out = '\0';
151
152         return done;
153 }
154
155 #if 0
156 int
157 lws_b64_selftest(void)
158 {
159         char buf[64];
160         unsigned int n,  r = 0;
161         unsigned int test;
162         /* examples from https://en.wikipedia.org/wiki/Base64 */
163         static const char * const plaintext[] = {
164                 "any carnal pleasure.",
165                 "any carnal pleasure",
166                 "any carnal pleasur",
167                 "any carnal pleasu",
168                 "any carnal pleas",
169                 "Admin:kloikloi"
170         };
171         static const char * const coded[] = {
172                 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
173                 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
174                 "YW55IGNhcm5hbCBwbGVhc3Vy",
175                 "YW55IGNhcm5hbCBwbGVhc3U=",
176                 "YW55IGNhcm5hbCBwbGVhcw==",
177                 "QWRtaW46a2xvaWtsb2k="
178         };
179
180         for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
181
182                 buf[sizeof(buf) - 1] = '\0';
183                 n = lws_b64_encode_string(plaintext[test],
184                                       strlen(plaintext[test]), buf, sizeof buf);
185                 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
186                         lwsl_err("Failed lws_b64 encode selftest "
187                                            "%d result '%s' %d\n", test, buf, n);
188                         r = -1;
189                 }
190
191                 buf[sizeof(buf) - 1] = '\0';
192                 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
193                 if (n != strlen(plaintext[test]) ||
194                                                  strcmp(buf, plaintext[test])) {
195                         lwsl_err("Failed lws_b64 decode selftest "
196                                  "%d result '%s' / '%s', %d / %d\n",
197                                  test, buf, plaintext[test], n, strlen(plaintext[test]));
198                         r = -1;
199                 }
200         }
201
202         lwsl_notice("Base 64 selftests passed\n");
203
204         return r;
205 }
206 #endif