[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / base64.c
blob971f6344ce70b07a29d41c5a8d03d3589df2ab79
1 /*
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 );
21 #include <stdint.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <gpxe/base64.h>
28 /** @file
30 * Base64 encoding
34 static const char base64[64] =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
37 /**
38 * Base64-encode data
40 * @v raw Raw data
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
45 * something like
47 * char buf[ base64_encoded_len ( len ) + 1 ];
49 * (the +1 is for the terminating NUL) to provide a buffer of the
50 * correct size.
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 );
56 unsigned int bit;
57 unsigned int tmp;
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 ) );
74 /**
75 * Base64-decode string
77 * @v encoded Encoded string
78 * @v raw Raw data
79 * @ret len Length of raw data, or negative error
81 * The buffer must be large enough to contain the decoded data. Use
82 * something like
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 );
91 uint8_t encoded_byte;
92 char *match;
93 int decoded;
94 unsigned int bit = 0;
95 unsigned int pad_count = 0;
96 size_t len;
98 /* Zero the raw data */
99 memset ( raw, 0, base64_decoded_max_len ( encoded ) );
101 /* Decode string */
102 while ( ( encoded_byte = *(encoded_bytes++) ) ) {
104 /* Ignore whitespace characters */
105 if ( isspace ( encoded_byte ) )
106 continue;
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 );
113 return -EINVAL;
115 pad_count++;
116 bit -= 2; /* unused_bits = ( 2 * pad_count ) */
117 continue;
119 if ( pad_count ) {
120 DBG ( "Base64-encoded string \"%s\" has invalid pad "
121 "sequence\n", encoded );
122 return -EINVAL;
125 /* Process normal characters */
126 match = strchr ( base64, encoded_byte );
127 if ( ! match ) {
128 DBG ( "Base64-encoded string \"%s\" contains invalid "
129 "character '%c'\n", encoded, encoded_byte );
130 return -EINVAL;
132 decoded = ( match - base64 );
134 /* Add to raw data */
135 decoded <<= 2;
136 raw_bytes[ bit / 8 ] |= ( decoded >> ( bit % 8 ) );
137 raw_bytes[ bit / 8 + 1 ] |= ( decoded << ( 8 - ( bit % 8 ) ) );
138 bit += 6;
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 );
145 return -EINVAL;
147 len = ( bit / 8 );
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 */
154 return ( len );