diff options
author | Michael Brown <mcb30@ipxe.org> | 2015-04-24 14:34:32 +0100 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2015-04-24 14:41:32 +0100 |
commit | 9aa8090d069eb0b36769f33544faf0e7e429e844 (patch) | |
tree | 02dd0bdc5afca6e625f2f6c73fd85244ab020093 /src/include/ipxe/base16.h | |
parent | b56b482fa3b48ae99c44cc60a34979f8780b076a (diff) | |
download | ipxe-9aa8090d069eb0b36769f33544faf0e7e429e844.tar.gz |
[base16] Add buffer size parameter to base16_encode() and base16_decode()
The current API for Base16 (and Base64) encoding requires the caller
to always provide sufficient buffer space. This prevents the use of
the generic encoding/decoding functionality in some situations, such
as in formatting the hex setting types.
Implement a generic hex_encode() (based on the existing
format_hex_setting()), implement base16_encode() and base16_decode()
in terms of the more generic hex_encode() and hex_decode(), and update
all callers to provide the additional buffer length parameter.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/include/ipxe/base16.h')
-rw-r--r-- | src/include/ipxe/base16.h | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/src/include/ipxe/base16.h b/src/include/ipxe/base16.h index fb20c9d5a..8c44da17e 100644 --- a/src/include/ipxe/base16.h +++ b/src/include/ipxe/base16.h @@ -32,9 +32,36 @@ static inline size_t base16_decoded_max_len ( const char *encoded ) { return ( ( strlen ( encoded ) + 1 ) / 2 ); } -extern void base16_encode ( const uint8_t *raw, size_t len, char *encoded ); -extern int hex_decode ( const char *string, char separator, void *data, +extern size_t hex_encode ( char separator, const void *raw, size_t raw_len, + char *data, size_t len ); +extern int hex_decode ( char separator, const char *encoded, void *data, size_t len ); -extern int base16_decode ( const char *encoded, uint8_t *raw ); + +/** + * Base16-encode data + * + * @v raw Raw data + * @v raw_len Length of raw data + * @v data Buffer + * @v len Length of buffer + * @ret len Encoded length + */ +static inline __attribute__ (( always_inline )) size_t +base16_encode ( const void *raw, size_t raw_len, char *data, size_t len ) { + return hex_encode ( 0, raw, raw_len, data, len ); +} + +/** + * Base16-decode data + * + * @v encoded Encoded string + * @v data Buffer + * @v len Length of buffer + * @ret len Length of data, or negative error + */ +static inline __attribute__ (( always_inline )) int +base16_decode ( const char *encoded, void *data, size_t len ) { + return hex_decode ( 0, encoded, data, len ); +} #endif /* _IPXE_BASE16_H */ |