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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
26 #include <gpxe/base64.h>
34 static const char base64
[64] =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
41 * @v len Length of raw data
42 * @v encoded Buffer for encoded string
44 * The buffer must be the correct length for the encoded string. Use
47 * char buf[ base64_encoded_len ( len ) + 1 ];
49 * (the +1 is for the terminating NUL) to provide a buffer of the
52 void base64_encode ( const uint8_t *raw
, size_t len
, char *encoded
) {
53 const uint8_t *raw_bytes
= ( ( const uint8_t * ) raw
);
54 uint8_t *encoded_bytes
= ( ( uint8_t * ) encoded
);
55 size_t raw_bit_len
= ( 8 * len
);
59 for ( bit
= 0 ; bit
< raw_bit_len
; bit
+= 6 ) {
60 tmp
= ( ( raw_bytes
[ bit
/ 8 ] << ( bit
% 8 ) ) |
61 ( raw_bytes
[ bit
/ 8 + 1 ] >> ( 8 - ( bit
% 8 ) ) ) );
62 tmp
= ( ( tmp
>> 2 ) & 0x3f );
63 *(encoded_bytes
++) = base64
[tmp
];
65 for ( ; ( bit
% 8 ) != 0 ; bit
+= 6 )
66 *(encoded_bytes
++) = '=';
67 *(encoded_bytes
++) = '\0';
69 DBG ( "Base64-encoded to \"%s\":\n", encoded
);
70 DBG_HDA ( 0, raw
, len
);
71 assert ( strlen ( encoded
) == base64_encoded_len ( len
) );
75 * Base64-decode string
77 * @v encoded Encoded string
79 * @ret len Length of raw data, or negative error
81 * The buffer must be large enough to contain the decoded data. Use
84 * char buf[ base64_decoded_max_len ( encoded ) ];
86 * to provide a buffer of the correct size.
88 int base64_decode ( const char *encoded
, uint8_t *raw
) {
89 const uint8_t *encoded_bytes
= ( ( const uint8_t * ) encoded
);
90 uint8_t *raw_bytes
= ( ( uint8_t * ) raw
);
95 unsigned int pad_count
= 0;
98 /* Zero the raw data */
99 memset ( raw
, 0, base64_decoded_max_len ( encoded
) );
102 while ( ( encoded_byte
= *(encoded_bytes
++) ) ) {
104 /* Ignore whitespace characters */
105 if ( isspace ( encoded_byte
) )
108 /* Process pad characters */
109 if ( encoded_byte
== '=' ) {
110 if ( pad_count
>= 2 ) {
111 DBG ( "Base64-encoded string \"%s\" has too "
112 "many pad characters\n", encoded
);
116 bit
-= 2; /* unused_bits = ( 2 * pad_count ) */
120 DBG ( "Base64-encoded string \"%s\" has invalid pad "
121 "sequence\n", encoded
);
125 /* Process normal characters */
126 match
= strchr ( base64
, encoded_byte
);
128 DBG ( "Base64-encoded string \"%s\" contains invalid "
129 "character '%c'\n", encoded
, encoded_byte
);
132 decoded
= ( match
- base64
);
134 /* Add to raw data */
136 raw_bytes
[ bit
/ 8 ] |= ( decoded
>> ( bit
% 8 ) );
137 raw_bytes
[ bit
/ 8 + 1 ] |= ( decoded
<< ( 8 - ( bit
% 8 ) ) );
141 /* Check that we decoded a whole number of bytes */
142 if ( ( bit
% 8 ) != 0 ) {
143 DBG ( "Base64-encoded string \"%s\" has invalid bit length "
144 "%d\n", encoded
, bit
);
149 DBG ( "Base64-decoded \"%s\" to:\n", encoded
);
150 DBG_HDA ( 0, raw
, len
);
151 assert ( len
<= base64_decoded_max_len ( encoded
) );
153 /* Return length in bytes */