Tomato 1.26
[tomato.git] / release / src / router / matrixssl / src / crypto / peersec / base64.c
blob603b1f8805cca0e29ea7d9e215f1cb366d87178d
1 /*
2 * base64.c
3 * Release $Name: MATRIXSSL_1_8_8_OPEN $
5 * Base64 operations
6 */
7 /*
8 * Copyright (c) PeerSec Networks, 2002-2009. All Rights Reserved.
9 * The latest version of this code is available at http://www.matrixssl.org
11 * This software is open source; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This General Public License does NOT permit incorporating this software
17 * into proprietary programs. If you are unable to comply with the GPL, a
18 * commercial license for this software may be purchased from PeerSec Networks
19 * at http://www.peersec.com
21 * This program is distributed in WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * http://www.gnu.org/copyleft/gpl.html
30 /******************************************************************************/
32 #include "../cryptoLayer.h"
34 static const unsigned char map[256] = {
35 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
36 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
37 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
38 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
39 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
40 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
41 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
42 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
43 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
44 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
45 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
46 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
47 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
48 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
49 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
50 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
51 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
52 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
53 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
54 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
55 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
56 255, 255, 255, 255 };
58 int32 ps_base64_decode(const unsigned char *in, uint32 len,
59 unsigned char *out, uint32 *outlen)
61 unsigned long t, x, y, z;
62 unsigned char c;
63 int32 g;
65 if (in == NULL || out == NULL || outlen == NULL) {
66 return -1;
68 g = 3;
69 for (x = y = z = t = 0; x < len; x++) {
70 c = map[in[x]&0xFF];
71 if (c == 255) {
72 continue;
75 the final = symbols are read and used to trim the remaining bytes
77 if (c == 254) {
78 c = 0;
80 prevent g < 0 which would potentially allow an overflow later
82 if (--g < 0) {
83 return CRYPT_INVALID_PACKET;
85 } else if (g != 3) {
87 we only allow = to be at the end
89 return CRYPT_INVALID_PACKET;
92 t = (t<<6)|c;
94 if (++y == 4) {
95 if (z + g > *outlen) {
96 return CRYPT_BUFFER_OVERFLOW;
98 out[z++] = (unsigned char)((t>>16)&255);
99 if (g > 1) {
100 out[z++] = (unsigned char)((t>>8)&255);
102 if (g > 2) {
103 out[z++] = (unsigned char)(t&255);
105 y = t = 0;
108 if (y != 0) {
109 return -1;
111 *outlen = z;
112 return 0;
115 /******************************************************************************/