diff options
author | Michael Brown | 2013-07-12 14:44:20 +0200 |
---|---|---|
committer | Michael Brown | 2013-07-12 15:14:36 +0200 |
commit | 076f58c4bf9fa1ae0faa65a731523cb531705974 (patch) | |
tree | abe42a7625ab2fc15a572e4e6959310a2b0c9ce3 /src/core/base16.c | |
parent | [test] Add self-tests for base16 (diff) | |
download | ipxe-076f58c4bf9fa1ae0faa65a731523cb531705974.tar.gz ipxe-076f58c4bf9fa1ae0faa65a731523cb531705974.tar.xz ipxe-076f58c4bf9fa1ae0faa65a731523cb531705974.zip |
[base16] Generalise base16_decode() to hex_decode()
Provide a generic hex_decode() routine which can be shared between the
Base16 code and the "hex" and "hexhyp" settings parsers.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/base16.c')
-rw-r--r-- | src/core/base16.c | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/src/core/base16.c b/src/core/base16.c index 1f0e536c..7fa4b200 100644 --- a/src/core/base16.c +++ b/src/core/base16.c @@ -61,6 +61,48 @@ void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) { } /** + * Decode hexadecimal string + * + * @v encoded Encoded string + * @v separator Byte separator character, or 0 for no separator + * @v data Buffer + * @v len Length of buffer + * @ret len Length of data, or negative error + */ +int hex_decode ( const char *encoded, char separator, void *data, size_t len ) { + uint8_t *out = data; + unsigned int count = 0; + unsigned int sixteens; + unsigned int units; + + while ( *encoded ) { + + /* Check separator, if applicable */ + if ( count && separator && ( ( *(encoded++) != separator ) ) ) + return -EINVAL; + + /* Extract digits. Note that either digit may be NUL, + * which would be interpreted as an invalid value by + * strtoul_charval(); there is therefore no need for an + * explicit end-of-string check. + */ + sixteens = strtoul_charval ( *(encoded++) ); + if ( sixteens >= 16 ) + return -EINVAL; + units = strtoul_charval ( *(encoded++) ); + if ( units >= 16 ) + return -EINVAL; + + /* Store result */ + if ( count < len ) + out[count] = ( ( sixteens << 4 ) | units ); + count++; + + } + return count; +} + +/** * Base16-decode data * * @v encoded Encoded string @@ -75,33 +117,15 @@ void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) { * to provide a buffer of the correct size. */ int base16_decode ( const char *encoded, uint8_t *raw ) { - const char *encoded_bytes = encoded; - uint8_t *raw_bytes = raw; - char buf[3]; - char *endp; - size_t len; - - while ( encoded_bytes[0] ) { - if ( ! encoded_bytes[1] ) { - DBG ( "Base16-encoded string \"%s\" has invalid " - "length\n", encoded ); - return -EINVAL; - } - memcpy ( buf, encoded_bytes, 2 ); - buf[2] = '\0'; - *(raw_bytes++) = strtoul ( buf, &endp, 16 ); - if ( *endp != '\0' ) { - DBG ( "Base16-encoded string \"%s\" has invalid " - "byte \"%s\"\n", encoded, buf ); - return -EINVAL; - } - encoded_bytes += 2; - } - len = ( raw_bytes - raw ); + int len; + + len = hex_decode ( encoded, 0, raw, -1UL ); + if ( len < 0 ) + return len; DBG ( "Base16-decoded \"%s\" to:\n", encoded ); DBG_HDA ( 0, raw, len ); assert ( len <= base16_decoded_max_len ( encoded ) ); - return ( len ); + return len; } |