2
* Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2 of the
7
* License, or any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
* You can also choose to distribute this program under the terms of
20
* the Unmodified Binary Distribution Licence (as given in the file
21
* COPYING.UBDL), provided that you have satisfied its requirements.
24
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31
#include <ipxe/base64.h>
39
static const char base64[64] =
40
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46
* @v raw_len Length of raw data
48
* @v len Length of buffer
49
* @ret len Encoded length
51
size_t base64_encode ( const void *raw, size_t raw_len, char *data,
53
const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
54
size_t raw_bit_len = ( 8 * raw_len );
61
for ( bit = 0 ; bit < raw_bit_len ; bit += 6, used++ ) {
64
tmp = ( raw_bytes[byte] << shift );
65
if ( ( byte + 1 ) < raw_len )
66
tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
67
tmp = ( ( tmp >> 2 ) & 0x3f );
69
data[used] = base64[tmp];
71
for ( ; ( bit % 8 ) != 0 ; bit += 6, used++ ) {
78
data[ len - 1 ] = '\0'; /* Ensure terminator exists */
84
* Base64-decode string
86
* @v encoded Encoded string
88
* @v len Length of buffer
89
* @ret len Length of data, or negative error
91
int base64_decode ( const char *encoded, void *data, size_t len ) {
92
const char *in = encoded;
98
unsigned int pad_count = 0;
101
/* Zero the output buffer */
102
memset ( data, 0, len );
105
while ( ( in_char = *(in++) ) ) {
107
/* Ignore whitespace characters */
108
if ( isspace ( in_char ) )
111
/* Process pad characters */
112
if ( in_char == '=' ) {
113
if ( pad_count >= 2 ) {
114
DBG ( "Base64-encoded string \"%s\" has too "
115
"many pad characters\n", encoded );
119
bit -= 2; /* unused_bits = ( 2 * pad_count ) */
123
DBG ( "Base64-encoded string \"%s\" has invalid pad "
124
"sequence\n", encoded );
128
/* Process normal characters */
129
match = strchr ( base64, in_char );
131
DBG ( "Base64-encoded string \"%s\" contains invalid "
132
"character '%c'\n", encoded, in_char );
135
in_bits = ( match - base64 );
137
/* Add to raw data */
139
offset = ( bit / 8 );
141
out[offset] |= ( in_bits >> ( bit % 8 ) );
144
out[offset] |= ( in_bits << ( 8 - ( bit % 8 ) ) );
148
/* Check that we decoded a whole number of bytes */
149
if ( ( bit % 8 ) != 0 ) {
150
DBG ( "Base64-encoded string \"%s\" has invalid bit length "
151
"%d\n", encoded, bit );
155
/* Return length in bytes */