Skip to content

Commit

Permalink
[base64] Avoid overrunning input data buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed May 20, 2012
1 parent 1af9284 commit 40e68e1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/base64.c
Expand Up @@ -54,11 +54,16 @@ void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
size_t raw_bit_len = ( 8 * len );
unsigned int bit;
unsigned int byte;
unsigned int shift;
unsigned int tmp;

for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
tmp = ( ( raw_bytes[ bit / 8 ] << ( bit % 8 ) ) |
( raw_bytes[ bit / 8 + 1 ] >> ( 8 - ( bit % 8 ) ) ) );
byte = ( bit / 8 );
shift = ( bit % 8 );
tmp = ( raw_bytes[byte] << shift );
if ( ( byte + 1 ) < len )
tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
tmp = ( ( tmp >> 2 ) & 0x3f );
*(encoded_bytes++) = base64[tmp];
}
Expand Down

0 comments on commit 40e68e1

Please sign in to comment.