ssl/tls: add proper const attributes to functions, context members
[tropicssl.git] / library / base64.c
blob6d773c83ac96d6a31f3ac22179ead18898d67b83
1 /*
2 * RFC 1521 base64 encoding/decoding
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "tropicssl/config.h"
38 #if defined(TROPICSSL_BASE64_C)
40 #include "tropicssl/base64.h"
42 static const unsigned char base64_enc_map[64] = {
43 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
44 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
45 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
46 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
47 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
48 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
49 '8', '9', '+', '/'
52 static const unsigned char base64_dec_map[128] = {
53 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
54 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
55 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
56 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
57 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
58 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
59 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
60 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
61 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
62 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
63 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
64 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
65 49, 50, 51, 127, 127, 127, 127, 127
69 * Encode a buffer into base64 format
71 int base64_encode(unsigned char *dst, int *dlen, const unsigned char *src, int slen)
73 int i, n;
74 int C1, C2, C3;
75 unsigned char *p;
77 if (slen == 0)
78 return (0);
80 n = (slen << 3) / 6;
82 switch ((slen << 3) - (n * 6)) {
83 case 2:
84 n += 3;
85 break;
86 case 4:
87 n += 2;
88 break;
89 default:
90 break;
93 if (*dlen < n + 1) {
94 *dlen = n + 1;
95 return (TROPICSSL_ERR_BASE64_BUFFER_TOO_SMALL);
98 n = (slen / 3) * 3;
100 for (i = 0, p = dst; i < n; i += 3) {
101 C1 = *src++;
102 C2 = *src++;
103 C3 = *src++;
105 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
106 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
107 *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
108 *p++ = base64_enc_map[C3 & 0x3F];
111 if (i < slen) {
112 C1 = *src++;
113 C2 = ((i + 1) < slen) ? *src++ : 0;
115 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
116 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
118 if ((i + 1) < slen)
119 *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
120 else
121 *p++ = '=';
123 *p++ = '=';
126 *dlen = p - dst;
127 *p = 0;
129 return (0);
133 * Decode a base64-formatted buffer
135 int base64_decode(unsigned char *dst, int *dlen, const unsigned char *src, int slen)
137 int i, j, n;
138 unsigned long x;
139 unsigned char *p;
141 for (i = j = n = 0; i < slen; i++) {
142 if ((slen - i) >= 2 && src[i] == '\r' && src[i + 1] == '\n')
143 continue;
145 if (src[i] == '\n')
146 continue;
148 if (src[i] == '=' && ++j > 2)
149 return (TROPICSSL_ERR_BASE64_INVALID_CHARACTER);
151 if (src[i] > 127 || base64_dec_map[src[i]] == 127)
152 return (TROPICSSL_ERR_BASE64_INVALID_CHARACTER);
154 if (base64_dec_map[src[i]] < 64 && j != 0)
155 return (TROPICSSL_ERR_BASE64_INVALID_CHARACTER);
157 n++;
160 if (n == 0)
161 return (0);
163 n = ((n * 6) + 7) >> 3;
165 if (*dlen < n) {
166 *dlen = n;
167 return (TROPICSSL_ERR_BASE64_BUFFER_TOO_SMALL);
170 for (j = 3, n = x = 0, p = dst; i > 0; i--, src++) {
171 if (*src == '\r' || *src == '\n')
172 continue;
174 j -= (base64_dec_map[*src] == 64);
175 x = (x << 6) | (base64_dec_map[*src] & 0x3F);
177 if (++n == 4) {
178 n = 0;
179 if (j > 0)
180 *p++ = (unsigned char)(x >> 16);
181 if (j > 1)
182 *p++ = (unsigned char)(x >> 8);
183 if (j > 2)
184 *p++ = (unsigned char)(x);
188 *dlen = p - dst;
190 return (0);
193 #if defined(TROPICSSL_SELF_TEST)
195 #include <string.h>
196 #include <stdio.h>
198 static const unsigned char base64_test_dec[64] = {
199 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
200 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
201 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
202 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
203 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
204 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
205 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
206 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
209 static const unsigned char base64_test_enc[] =
210 "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
211 "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
214 * Checkup routine
216 int base64_self_test(int verbose)
218 int len;
219 unsigned char buffer[128];
220 const unsigned char *src;
222 if (verbose != 0)
223 printf(" Base64 encoding test: ");
225 len = sizeof(buffer);
226 src = base64_test_dec;
228 if (base64_encode(buffer, &len, src, 64) != 0 ||
229 memcmp(base64_test_enc, buffer, 88) != 0) {
230 if (verbose != 0)
231 printf("failed\n");
233 return (1);
236 if (verbose != 0)
237 printf("passed\n Base64 decoding test: ");
239 len = sizeof(buffer);
240 src = base64_test_enc;
242 if (base64_decode(buffer, &len, src, 88) != 0 ||
243 memcmp(base64_test_dec, buffer, 64) != 0) {
244 if (verbose != 0)
245 printf("failed\n");
247 return (1);
250 if (verbose != 0)
251 printf("passed\n\n");
253 return (0);
256 #endif
258 #endif