summaryrefslogtreecommitdiffstats
path: root/src/core/base64.c
diff options
context:
space:
mode:
authorMichael Brown2012-05-20 17:38:57 +0200
committerMichael Brown2012-05-20 17:47:57 +0200
commit40e68e1119ed45ed27c586836d4cb9400a96db1c (patch)
tree1d7640bd5a31ed0e81bcb13dbe9b4b1ea799b115 /src/core/base64.c
parent[efi] Work around platforms which choke on EFI_PCI_DEVICE_ENABLE (diff)
downloadipxe-40e68e1119ed45ed27c586836d4cb9400a96db1c.tar.gz
ipxe-40e68e1119ed45ed27c586836d4cb9400a96db1c.tar.xz
ipxe-40e68e1119ed45ed27c586836d4cb9400a96db1c.zip
[base64] Avoid overrunning input data buffer
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/base64.c')
-rw-r--r--src/core/base64.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/core/base64.c b/src/core/base64.c
index 7514c1fa..761b42f2 100644
--- a/src/core/base64.c
+++ b/src/core/base64.c
@@ -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];
}