blob: dbc908b86274c87e7bbee899106dcba88499767f [file] [log] [blame]
Harald Weltec7572392021-09-17 08:23:09 +02001/*
2 * RFC 1521 base64 encoding/decoding
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 *
6 * This file is part of mbed TLS (https://tls.mbed.org)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Harald Welte292f9e72021-09-17 08:35:32 +020023#include <osmocom/core/base64.h>
Harald Weltec7572392021-09-17 08:23:09 +020024
25#include <stdint.h>
Harald Weltec7572392021-09-17 08:23:09 +020026#include <stdio.h>
Harald Welte292f9e72021-09-17 08:35:32 +020027#include <errno.h>
Harald Weltec7572392021-09-17 08:23:09 +020028
29static const unsigned char base64_enc_map[64] =
30{
31 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
32 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
33 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
34 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
35 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
36 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
37 '8', '9', '+', '/'
38};
39
40static const unsigned char base64_dec_map[128] =
41{
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, 127, 127, 127, 127, 127, 127, 127,
46 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
47 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
48 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
49 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
50 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
51 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
52 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
53 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
54 49, 50, 51, 127, 127, 127, 127, 127
55};
56
57/*
58 * Encode a buffer into base64 format
59 */
Harald Welte292f9e72021-09-17 08:35:32 +020060int osmo_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
Harald Weltec7572392021-09-17 08:23:09 +020061 const unsigned char *src, size_t slen )
62{
63 size_t i, n;
64 int C1, C2, C3;
65 unsigned char *p;
66
67 if( slen == 0 )
68 {
69 *olen = 0;
70 return( 0 );
71 }
72
73 n = ( slen << 3 ) / 6;
74
75 switch( ( slen << 3 ) - ( n * 6 ) )
76 {
77 case 2: n += 3; break;
78 case 4: n += 2; break;
79 default: break;
80 }
81
82 if( dlen < n + 1 )
83 {
84 *olen = n + 1;
Harald Welte292f9e72021-09-17 08:35:32 +020085 return( -ENOBUFS );
Harald Weltec7572392021-09-17 08:23:09 +020086 }
87
88 n = ( slen / 3 ) * 3;
89
90 for( i = 0, p = dst; i < n; i += 3 )
91 {
92 C1 = *src++;
93 C2 = *src++;
94 C3 = *src++;
95
96 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
97 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
98 *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
99 *p++ = base64_enc_map[C3 & 0x3F];
100 }
101
102 if( i < slen )
103 {
104 C1 = *src++;
105 C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
106
107 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
108 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
109
110 if( ( i + 1 ) < slen )
111 *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
112 else *p++ = '=';
113
114 *p++ = '=';
115 }
116
117 *olen = p - dst;
118 *p = 0;
119
120 return( 0 );
121}
122
123/*
124 * Decode a base64-formatted buffer
125 */
Harald Welte292f9e72021-09-17 08:35:32 +0200126int osmo_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
Harald Weltec7572392021-09-17 08:23:09 +0200127 const unsigned char *src, size_t slen )
128{
129 size_t i, n;
130 uint32_t j, x;
131 unsigned char *p;
132
133 /* First pass: check for validity and get output length */
134 for( i = n = j = 0; i < slen; i++ )
135 {
136 /* Skip spaces before checking for EOL */
137 x = 0;
138 while( i < slen && src[i] == ' ' )
139 {
140 ++i;
141 ++x;
142 }
143
144 /* Spaces at end of buffer are OK */
145 if( i == slen )
146 break;
147
148 if( ( slen - i ) >= 2 &&
149 src[i] == '\r' && src[i + 1] == '\n' )
150 continue;
151
152 if( src[i] == '\n' )
153 continue;
154
155 /* Space inside a line is an error */
156 if( x != 0 )
Harald Welte292f9e72021-09-17 08:35:32 +0200157 return( -EINVAL );
Harald Weltec7572392021-09-17 08:23:09 +0200158
159 if( src[i] == '=' && ++j > 2 )
Harald Welte292f9e72021-09-17 08:35:32 +0200160 return( -EINVAL );
Harald Weltec7572392021-09-17 08:23:09 +0200161
162 if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
Harald Welte292f9e72021-09-17 08:35:32 +0200163 return( -EINVAL );
Harald Weltec7572392021-09-17 08:23:09 +0200164
165 if( base64_dec_map[src[i]] < 64 && j != 0 )
Harald Welte292f9e72021-09-17 08:35:32 +0200166 return( -EINVAL );
Harald Weltec7572392021-09-17 08:23:09 +0200167
168 n++;
169 }
170
171 if( n == 0 )
172 return( 0 );
173
174 n = ( ( n * 6 ) + 7 ) >> 3;
175 n -= j;
176
177 if( dst == NULL || dlen < n )
178 {
179 *olen = n;
Harald Welte292f9e72021-09-17 08:35:32 +0200180 return( -ENOBUFS );
Harald Weltec7572392021-09-17 08:23:09 +0200181 }
182
183 for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
184 {
185 if( *src == '\r' || *src == '\n' || *src == ' ' )
186 continue;
187
188 j -= ( base64_dec_map[*src] == 64 );
189 x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
190
191 if( ++n == 4 )
192 {
193 n = 0;
194 if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
195 if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
196 if( j > 2 ) *p++ = (unsigned char)( x );
197 }
198 }
199
200 *olen = p - dst;
201
202 return( 0 );
203}