mirror of https://github.com/ipxe/ipxe.git
[base64] Avoid overrunning input data buffer
Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/6/head
parent
1af9284225
commit
40e68e1119
|
@ -54,11 +54,16 @@ void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
|
||||||
uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
|
uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
|
||||||
size_t raw_bit_len = ( 8 * len );
|
size_t raw_bit_len = ( 8 * len );
|
||||||
unsigned int bit;
|
unsigned int bit;
|
||||||
|
unsigned int byte;
|
||||||
|
unsigned int shift;
|
||||||
unsigned int tmp;
|
unsigned int tmp;
|
||||||
|
|
||||||
for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
|
for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
|
||||||
tmp = ( ( raw_bytes[ bit / 8 ] << ( bit % 8 ) ) |
|
byte = ( bit / 8 );
|
||||||
( raw_bytes[ bit / 8 + 1 ] >> ( 8 - ( 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 );
|
tmp = ( ( tmp >> 2 ) & 0x3f );
|
||||||
*(encoded_bytes++) = base64[tmp];
|
*(encoded_bytes++) = base64[tmp];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue