mirror of
https://github.com/ipxe/ipxe
synced 2025-12-23 05:21:49 +03:00
[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>
This commit is contained in:
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user