mirror of https://github.com/ipxe/ipxe.git
[base64] Allow base64_encode() to handle arbitrary data
Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/1/head
parent
b9b59a585b
commit
dfcce165a5
|
@ -33,23 +33,24 @@ static const char base64[64] =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base64-encode a string
|
* Base64-encode data
|
||||||
*
|
*
|
||||||
* @v raw Raw string
|
* @v raw Raw data
|
||||||
|
* @v len Length of raw data
|
||||||
* @v encoded Buffer for encoded string
|
* @v encoded Buffer for encoded string
|
||||||
*
|
*
|
||||||
* The buffer must be the correct length for the encoded string. Use
|
* The buffer must be the correct length for the encoded string. Use
|
||||||
* something like
|
* something like
|
||||||
*
|
*
|
||||||
* char buf[ base64_encoded_len ( strlen ( raw ) ) + 1 ];
|
* char buf[ base64_encoded_len ( len ) + 1 ];
|
||||||
*
|
*
|
||||||
* (the +1 is for the terminating NUL) to provide a buffer of the
|
* (the +1 is for the terminating NUL) to provide a buffer of the
|
||||||
* correct size.
|
* correct size.
|
||||||
*/
|
*/
|
||||||
void base64_encode ( const char *raw, char *encoded ) {
|
void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
|
||||||
const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
|
const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
|
||||||
uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
|
uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
|
||||||
size_t raw_bit_len = ( 8 * strlen ( raw ) );
|
size_t raw_bit_len = ( 8 * len );
|
||||||
unsigned int bit;
|
unsigned int bit;
|
||||||
unsigned int tmp;
|
unsigned int tmp;
|
||||||
|
|
||||||
|
@ -63,6 +64,7 @@ void base64_encode ( const char *raw, char *encoded ) {
|
||||||
*(encoded_bytes++) = '=';
|
*(encoded_bytes++) = '=';
|
||||||
*(encoded_bytes++) = '\0';
|
*(encoded_bytes++) = '\0';
|
||||||
|
|
||||||
DBG ( "Base64-encoded \"%s\" as \"%s\"\n", raw, encoded );
|
DBG ( "Base64-encoded to \"%s\":\n", encoded );
|
||||||
assert ( strlen ( encoded ) == base64_encoded_len ( strlen ( raw ) ) );
|
DBG_HDA ( 0, raw, len );
|
||||||
|
assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,15 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate length of base64-encoded string
|
* Calculate length of base64-encoded data
|
||||||
*
|
*
|
||||||
* @v raw_len Raw string length (excluding NUL)
|
* @v raw_len Raw data length
|
||||||
* @ret encoded_len Encoded string length (excluding NUL)
|
* @ret encoded_len Encoded string length (excluding NUL)
|
||||||
*/
|
*/
|
||||||
static inline size_t base64_encoded_len ( size_t raw_len ) {
|
static inline size_t base64_encoded_len ( size_t raw_len ) {
|
||||||
return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
|
return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void base64_encode ( const char *raw, char *encoded );
|
extern void base64_encode ( const uint8_t *raw, size_t len, char *encoded );
|
||||||
|
|
||||||
#endif /* _IPXE_BASE64_H */
|
#endif /* _IPXE_BASE64_H */
|
||||||
|
|
|
@ -424,7 +424,7 @@ static void http_step ( struct process *process ) {
|
||||||
size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
|
size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
|
||||||
strlen ( password ) ) : 0 );
|
strlen ( password ) ) : 0 );
|
||||||
size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
|
size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
|
||||||
char user_pw[ user_pw_len + 1 /* NUL */ ];
|
uint8_t user_pw[ user_pw_len + 1 /* NUL */ ];
|
||||||
char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
|
char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
|
||||||
int rc;
|
int rc;
|
||||||
int request_len = unparse_uri ( NULL, 0, http->uri,
|
int request_len = unparse_uri ( NULL, 0, http->uri,
|
||||||
|
@ -443,11 +443,11 @@ static void http_step ( struct process *process ) {
|
||||||
/* Construct authorisation, if applicable */
|
/* Construct authorisation, if applicable */
|
||||||
if ( user ) {
|
if ( user ) {
|
||||||
/* Make "user:password" string from decoded fields */
|
/* Make "user:password" string from decoded fields */
|
||||||
snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
|
snprintf ( ( ( char * ) user_pw ), sizeof ( user_pw ),
|
||||||
user, password );
|
"%s:%s", user, password );
|
||||||
|
|
||||||
/* Base64-encode the "user:password" string */
|
/* Base64-encode the "user:password" string */
|
||||||
base64_encode ( user_pw, user_pw_base64 );
|
base64_encode ( user_pw, user_pw_len, user_pw_base64 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send GET request */
|
/* Send GET request */
|
||||||
|
|
Loading…
Reference in New Issue