[base64] Add buffer size parameter to base64_encode() and base64_decode()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2015-04-24 15:32:04 +01:00
parent 9aa8090d06
commit 1205721cbd
7 changed files with 84 additions and 77 deletions

View File

@@ -80,30 +80,42 @@ BASE64 ( random_test,
* Report a base64 encoding test result
*
* @v test Base64 test
* @v file Test code file
* @v line Test code line
*/
#define base64_encode_ok( test ) do { \
size_t len = base64_encoded_len ( (test)->len ); \
char buf[ len + 1 /* NUL */ ]; \
ok ( len == strlen ( (test)->encoded ) ); \
base64_encode ( (test)->data, (test)->len, buf ); \
ok ( strcmp ( (test)->encoded, buf ) == 0 ); \
} while ( 0 )
static void base64_encode_okx ( struct base64_test *test, const char *file,
unsigned int line ) {
size_t len = base64_encoded_len ( test->len );
char buf[ len + 1 /* NUL */ ];
size_t check_len;
okx ( len == strlen ( test->encoded ), file, line );
check_len = base64_encode ( test->data, test->len, buf, sizeof ( buf ));
okx ( check_len == len, file, line );
okx ( strcmp ( test->encoded, buf ) == 0, file, line );
}
#define base64_encode_ok( test ) base64_encode_okx ( test, __FILE__, __LINE__ )
/**
* Report a base64 decoding test result
*
* @v test Base64 test
* @v file Test code file
* @v line Test code line
*/
#define base64_decode_ok( test ) do { \
size_t max_len = base64_decoded_max_len ( (test)->encoded ); \
uint8_t buf[max_len]; \
int len; \
len = base64_decode ( (test)->encoded, buf ); \
ok ( len >= 0 ); \
ok ( ( size_t ) len <= max_len ); \
ok ( ( size_t ) len == (test)->len ); \
ok ( memcmp ( (test)->data, buf, len ) == 0 ); \
} while ( 0 )
static void base64_decode_okx ( struct base64_test *test, const char *file,
unsigned int line ) {
size_t max_len = base64_decoded_max_len ( test->encoded );
uint8_t buf[max_len];
int len;
len = base64_decode ( test->encoded, buf, sizeof ( buf ) );
okx ( len >= 0, file, line );
okx ( ( size_t ) len <= max_len, file, line );
okx ( ( size_t ) len == test->len, file, line );
okx ( memcmp ( test->data, buf, len ) == 0, file, line );
}
#define base64_decode_ok( test ) base64_decode_okx ( test, __FILE__, __LINE__ )
/**
* Perform Base64 self-tests