build chrom crx on windows
[gssweb.git] / json_gssapi / src / util_base64.cpp
1 /*
2  * Copyright (c) 1995-2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifdef WIN32
35 #include <windows.h>
36 #else
37 #include <pthread.h>
38 #endif
39 #include <string>
40 #include <limits.h>
41 #include <malloc.h>
42 #include <string.h>
43 #include "util_base64.h"
44
45 #ifdef WIN32
46 #define GSSWEB_THREAD_ONCE              INIT_ONCE
47 #define GSSWEB_ONCE_CALLBACK(cb)        BOOL CALLBACK cb(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
48 #define GSSWEB_ONCE_LEAVE               do { return TRUE; } while (0)
49 #define GSSWEB_ONCE(o, i)               InitOnceExecuteOnce((o), (i), NULL, NULL)
50 #define GSSWEB_ONCE_INITIALIZER         INIT_ONCE_STATIC_INIT
51 #else
52 #define GSSWEB_THREAD_ONCE              pthread_once_t
53 #define GSSWEB_ONCE_CALLBACK(cb)        void cb(void)
54 #define GSSWEB_ONCE(o, i)               pthread_once((o), (i))
55 #define GSSWEB_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
56 #define GSSWEB_ONCE_LEAVE               do { } while (0)
57 #endif
58
59 size_t
60 base64Size(const char *str);
61
62 #define BASE64_EXPAND(n)        (n * 4 / 3 + 4)
63
64 static const char base64_chars[] =
65     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66 static char base64_decode[SCHAR_MAX];
67
68 static GSSWEB_ONCE_CALLBACK(init_decode_table)
69 {
70     size_t i;
71     memset(&base64_decode[0], -1, sizeof(base64_decode));
72     for (i = 0; i < sizeof(base64_chars) / sizeof(base64_chars[0]); i++)
73         base64_decode[(unsigned int)base64_chars[i]] = (char )i;
74     GSSWEB_ONCE_LEAVE;
75 }
76
77 static int
78 pos(char c)
79 {
80     if (c <= 0)
81         return -1;
82     return base64_decode[(unsigned int)c];
83 }
84
85 void
86 base64EncodeStr(const void *data, size_t size, std::string &str)
87 {
88
89     char *s = base64Encode(data, size);
90     str = s;
91     base64Free(s);
92 }
93
94 char *
95 base64Encode(const void *data, size_t size)
96 {
97     char *s, *p;
98     size_t i;
99     int c;
100     const unsigned char *q;
101
102     if (size > INT_MAX/4 || size < 0) {
103         return NULL;
104     }
105
106     p = s = (char *)malloc(BASE64_EXPAND(size));
107     if (p == NULL) {
108         return NULL;
109     }
110     q = (const unsigned char *) data;
111
112     for (i = 0; i < size;) {
113         c = q[i++];
114         c *= 256;
115         if (i < size)
116             c += q[i];
117         i++;
118         c *= 256;
119         if (i < size)
120             c += q[i];
121         i++;
122         p[0] = base64_chars[(c & 0x00fc0000) >> 18];
123         p[1] = base64_chars[(c & 0x0003f000) >> 12];
124         p[2] = base64_chars[(c & 0x00000fc0) >> 6];
125         p[3] = base64_chars[(c & 0x0000003f) >> 0];
126         if (i > size)
127             p[3] = '=';
128         if (i > size + 1)
129             p[2] = '=';
130         p += 4;
131     }
132     *p = 0;
133     return s;
134 }
135
136 #define DECODE_ERROR 0xffffffff
137
138 static unsigned int
139 token_decode(const char *token)
140 {
141     int i, decode;
142     unsigned int val = 0;
143     unsigned int marker = 0;
144
145     for (i = 0; i < 4; i++) {
146         val *= 64;
147         if (token[i] == '=')
148             marker++;
149         else if (marker > 0)
150             return DECODE_ERROR;
151         else {
152             decode = pos(token[i]);
153             if (decode < 0)
154                 return DECODE_ERROR;
155             val += decode;
156         }
157     }
158     if (marker > 2)
159         return DECODE_ERROR;
160     return (marker << 24) | val;
161 }
162
163 void *
164 base64Decode(const char *str, size_t *size)
165 {
166     static GSSWEB_THREAD_ONCE decode_table_once = GSSWEB_ONCE_INITIALIZER;
167     const char *p;
168     unsigned char *q;
169     void *data = NULL;
170     GSSWEB_ONCE(&decode_table_once, init_decode_table);
171
172     *size = base64Size(str);
173     if (*size > 0)
174         data = malloc(*size);
175     if (data == NULL)
176         return data;
177
178     q = static_cast<unsigned char *>(data);
179     p = str;
180
181     while (*p) {
182         unsigned int val = token_decode(p);
183         unsigned int marker = (val >> 24) & 0xff;
184         if (val == DECODE_ERROR) {
185             free(data);
186             *size = 0;
187             return NULL;
188         }
189         *q++ = (val >> 16) & 0xff;
190         if (marker < 2)
191             *q++ = (val >> 8) & 0xff;
192         if (marker < 1)
193             *q++ = val & 0xff;
194         p += 4;
195         if (*p == '\n')
196             p++;
197     }
198     return data;
199 }
200
201 /* Return the size, in bytes, of the data encoded by str
202  * Return -1 if str is not a valid base64 encoding
203  */
204 size_t
205 base64Size(const char *str)
206 {
207     const char *p = str;
208     size_t size = 0;
209
210     while (*p) {
211         unsigned int val = token_decode(p);
212         unsigned int marker = (val >> 24) & 0x3;
213         if (val == DECODE_ERROR)
214             return 0;
215
216         size += 3 - marker;
217         p += 4;
218         if (*p == '\n')
219             p++;
220     }
221     return size;
222 }
223
224 void
225 base64Free(void *mem)
226 {
227     free(mem);
228 }