2
0
mirror of https://github.com/xcat2/xNBA.git synced 2025-02-11 16:23:47 +00:00

[base64] Avoid overrunning input data buffer

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2012-05-20 16:38:57 +01:00
parent 1af9284225
commit 40e68e1119

View File

@ -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];
}