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