Bugzilla #1097: PDKIM: Update embedded PolarSSL code to 0.14.2, thanks to Andreas...
[exim.git] / src / src / pdkim / base64.c
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 along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 /* $Cambridge: exim/src/src/pdkim/base64.c,v 1.3 2009/12/07 13:05:07 tom Exp $ */
27
28
29 #include "base64.h"
30
31 static const unsigned char base64_enc_map[64] =
32 {
33     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
34     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
35     'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
36     'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
37     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
38     'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
39     '8', '9', '+', '/'
40 };
41
42 static const unsigned char base64_dec_map[128] =
43 {
44     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
45     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
46     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
47     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
48     127, 127, 127,  62, 127, 127, 127,  63,  52,  53,
49      54,  55,  56,  57,  58,  59,  60,  61, 127, 127,
50     127,  64, 127, 127, 127,   0,   1,   2,   3,   4,
51       5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
52      15,  16,  17,  18,  19,  20,  21,  22,  23,  24,
53      25, 127, 127, 127, 127, 127, 127,  26,  27,  28,
54      29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
55      39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
56      49,  50,  51, 127, 127, 127, 127, 127
57 };
58
59 /*
60  * Encode a buffer into base64 format
61  */
62 int base64_encode( unsigned char *dst, int *dlen,
63                    const unsigned char *src, int  slen )
64 {
65     int i, n;
66     int C1, C2, C3;
67     unsigned char *p;
68
69     if( slen == 0 )
70         return( 0 );
71
72     n = (slen << 3) / 6;
73
74     switch( (slen << 3) - (n * 6) )
75     {
76         case  2: n += 3; break;
77         case  4: n += 2; break;
78         default: break;
79     }
80
81     if( *dlen < n + 1 )
82     {
83         *dlen = n + 1;
84         return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
85     }
86
87     n = (slen / 3) * 3;
88
89     for( i = 0, p = dst; i < n; i += 3 )
90     {
91         C1 = *src++;
92         C2 = *src++;
93         C3 = *src++;
94
95         *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
96         *p++ = base64_enc_map[(((C1 &  3) << 4) + (C2 >> 4)) & 0x3F];
97         *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
98         *p++ = base64_enc_map[C3 & 0x3F];
99     }
100
101     if( i < slen )
102     {
103         C1 = *src++;
104         C2 = ((i + 1) < slen) ? *src++ : 0;
105
106         *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
107         *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
108
109         if( (i + 1) < slen )
110              *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
111         else *p++ = '=';
112
113         *p++ = '=';
114     }
115
116     *dlen = p - dst;
117     *p = 0;
118
119     return( 0 );
120 }
121
122 /*
123  * Decode a base64-formatted buffer
124  */
125 int base64_decode( unsigned char *dst, int *dlen,
126                    const unsigned char *src, int  slen )
127 {
128     int i, j, n;
129     unsigned long x;
130     unsigned char *p;
131
132     for( i = j = n = 0; i < slen; i++ )
133     {
134         if( ( slen - i ) >= 2 &&
135             src[i] == '\r' && src[i + 1] == '\n' )
136             continue;
137
138         if( src[i] == '\n' )
139             continue;
140
141         if( src[i] == '=' && ++j > 2 )
142             return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
143
144         if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
145             return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
146
147         if( base64_dec_map[src[i]] < 64 && j != 0 )
148             return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
149
150         n++;
151     }
152
153     if( n == 0 )
154         return( 0 );
155
156     n = ((n * 6) + 7) >> 3;
157
158     if( *dlen < n )
159     {
160         *dlen = n;
161         return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
162     }
163
164    for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
165    {
166         if( *src == '\r' || *src == '\n' )
167             continue;
168
169         j -= ( base64_dec_map[*src] == 64 );
170         x  = (x << 6) | ( base64_dec_map[*src] & 0x3F );
171
172         if( ++n == 4 )
173         {
174             n = 0;
175             if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
176             if( j > 1 ) *p++ = (unsigned char)( x >>  8 );
177             if( j > 2 ) *p++ = (unsigned char)( x       );
178         }
179     }
180
181     *dlen = p - dst;
182
183     return( 0 );
184 }