lws_return_http_status send content length
[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
42 #include <stdio.h>
43 #include <string.h>
44 #include "private-libwebsockets.h"
45
46 static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47                              "abcdefghijklmnopqrstuvwxyz0123456789+/";
48 static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
49                              "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
50
51 LWS_VISIBLE int
52 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
53 {
54         unsigned char triple[3];
55         int i;
56         int len;
57         int line = 0;
58         int done = 0;
59
60         while (in_len) {
61                 len = 0;
62                 for (i = 0; i < 3; i++) {
63                         if (in_len) {
64                                 triple[i] = *in++;
65                                 len++;
66                                 in_len--;
67                         } else
68                                 triple[i] = 0;
69                 }
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
85         if (done + 1 >= out_size)
86                 return -1;
87
88         *out++ = '\0';
89
90         return done;
91 }
92
93 /*
94  * returns length of decoded string in out, or -1 if out was too small
95  * according to out_size
96  */
97
98 LWS_VISIBLE int
99 lws_b64_decode_string(const char *in, char *out, int out_size)
100 {
101         int len;
102         int i;
103         int done = 0;
104         unsigned char v;
105         unsigned char quad[4];
106
107         while (*in) {
108
109                 len = 0;
110                 for (i = 0; i < 4 && *in; i++) {
111
112                         v = 0;
113                         while (*in && !v) {
114
115                                 v = *in++;
116                                 v = (v < 43 || v > 122) ? 0 : decode[v - 43];
117                                 if (v)
118                                         v = (v == '$') ? 0 : v - 61;
119                                 if (*in) {
120                                         len++;
121                                         if (v)
122                                                 quad[i] = v - 1;
123                                 } else
124                                         quad[i] = 0;
125                         }
126                 }
127
128                 if (out_size < (done + len - 1))
129                         /* out buffer is too small */
130                         return -1;
131
132                 if (len >= 2)
133                         *out++ = quad[0] << 2 | quad[1] >> 4;
134                 if (len >= 3)
135                         *out++ = quad[1] << 4 | quad[2] >> 2;
136                 if (len >= 4)
137                         *out++ = ((quad[2] << 6) & 0xc0) | quad[3];
138
139                 done += len - 1;
140         }
141
142         if (done + 1 >= out_size)
143                 return -1;
144
145         *out++ = '\0';
146
147         return done;
148 }
149
150 int
151 lws_b64_selftest(void)
152 {
153         char buf[64];
154         unsigned int n;
155         unsigned int test;
156         static const char * const plaintext[] = {
157                 "sanity check base 64"
158         };
159         static const char * const coded[] = {
160                 "c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
161         };
162
163         for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
164
165                 buf[sizeof(buf) - 1] = '\0';
166                 n = lws_b64_encode_string(plaintext[test],
167                                       strlen(plaintext[test]), buf, sizeof buf);
168                 if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
169                         lwsl_err("Failed lws_b64 encode selftest "
170                                            "%d result '%s' %d\n", test, buf, n);
171                         return -1;
172                 }
173
174                 buf[sizeof(buf) - 1] = '\0';
175                 n = lws_b64_decode_string(coded[test], buf, sizeof buf);
176                 if (n != strlen(plaintext[test]) ||
177                                                  strcmp(buf, plaintext[test])) {
178                         lwsl_err("Failed lws_b64 decode selftest "
179                                            "%d result '%s' %d\n", test, buf, n);
180                         return -1;
181                 }
182         }
183
184         return 0;
185 }