1 /*
2  *  RFC 1521 base64 encoding/decoding
3  *
4  *  Copyright (C) 2006-2010, Brainspark B.V.
5  *
6  *  This file is part of PolarSSL (http://www.polarssl.org)
7  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8  *
9  *  All rights reserved.
10  *
11  *  This program is free software; 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.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 /*
25  *  Pacman Notes:
26  *
27  *  Taken from the PolarSSL project at www.polarssl.org under terms of the
28  *  GPL. This is from version 0.14.2 of the library, and has been modified
29  *  as following, which may be helpful for future updates:
30  *  * remove "polarssl/config.h" include
31  *  * change include from "polarssl/base64.h" to "base64.h"
32  *  * removal of SELF_TEST code
33  */
34 
35 #include <stdint.h>
36 
37 #include "base64.h"
38 
39 #if 0
40 static const unsigned char base64_enc_map[64] =
41 {
42     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
43     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
44     'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
45     'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
46     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
47     'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
48     '8', '9', '+', '/'
49 };
50 #endif
51 
52 static const unsigned char base64_dec_map[128] =
53 {
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, 127, 127, 127, 127, 127, 127, 127,
58     127, 127, 127,  62, 127, 127, 127,  63,  52,  53,
59      54,  55,  56,  57,  58,  59,  60,  61, 127, 127,
60     127,  64, 127, 127, 127,   0,   1,   2,   3,   4,
61       5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
62      15,  16,  17,  18,  19,  20,  21,  22,  23,  24,
63      25, 127, 127, 127, 127, 127, 127,  26,  27,  28,
64      29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
65      39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
66      49,  50,  51, 127, 127, 127, 127, 127
67 };
68 
69 #if 0
70 /*
71  * Encode a buffer into base64 format
72  */
73 int base64_encode( unsigned char *dst, size_t *dlen,
74                    const unsigned char *src, size_t slen )
75 {
76     size_t i, n;
77     int C1, C2, C3;
78     unsigned char *p;
79 
80     if( slen == 0 )
81         return( 0 );
82 
83     n = (slen << 3) / 6;
84 
85     switch( (slen << 3) - (n * 6) )
86     {
87         case  2: n += 3; break;
88         case  4: n += 2; break;
89         default: break;
90     }
91 
92     if( *dlen < n + 1 )
93     {
94         *dlen = n + 1;
95         return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
96     }
97 
98     n = (slen / 3) * 3;
99 
100     for( i = 0, p = dst; i < n; i += 3 )
101     {
102         C1 = *src++;
103         C2 = *src++;
104         C3 = *src++;
105 
106         *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
107         *p++ = base64_enc_map[(((C1 &  3) << 4) + (C2 >> 4)) & 0x3F];
108         *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
109         *p++ = base64_enc_map[C3 & 0x3F];
110     }
111 
112     if( i < slen )
113     {
114         C1 = *src++;
115         C2 = ((i + 1) < slen) ? *src++ : 0;
116 
117         *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
118         *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
119 
120         if( (i + 1) < slen )
121              *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
122         else *p++ = '=';
123 
124         *p++ = '=';
125     }
126 
127     *dlen = p - dst;
128     *p = 0;
129 
130     return( 0 );
131 }
132 #endif
133 
134 /*
135  * Decode a base64-formatted buffer
136  */
base64_decode(unsigned char * dst,size_t * dlen,const unsigned char * src,size_t slen)137 int base64_decode( unsigned char *dst, size_t *dlen,
138                    const unsigned char *src, size_t slen )
139 {
140     size_t i, n;
141     uint32_t j, x;
142     unsigned char *p;
143 
144     for( i = j = n = 0; i < slen; i++ )
145     {
146         if( ( slen - i ) >= 2 &&
147             src[i] == '\r' && src[i + 1] == '\n' )
148             continue;
149 
150         if( src[i] == '\n' )
151             continue;
152 
153         if( src[i] == '=' && ++j > 2 )
154             return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
155 
156         if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
157             return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
158 
159         if( base64_dec_map[src[i]] < 64 && j != 0 )
160             return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
161 
162         n++;
163     }
164 
165     if( n == 0 )
166         return( 0 );
167 
168     n = ((n * 6) + 7) >> 3;
169 
170     if( *dlen < n )
171     {
172         *dlen = n;
173         return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
174     }
175 
176    for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
177    {
178         if( *src == '\r' || *src == '\n' )
179             continue;
180 
181         j -= ( base64_dec_map[*src] == 64 );
182         x  = (x << 6) | ( base64_dec_map[*src] & 0x3F );
183 
184         if( ++n == 4 )
185         {
186             n = 0;
187             if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
188             if( j > 1 ) *p++ = (unsigned char)( x >>  8 );
189             if( j > 2 ) *p++ = (unsigned char)( x       );
190         }
191     }
192 
193     *dlen = p - dst;
194 
195     return( 0 );
196 }
197