summaryrefslogtreecommitdiffstats
path: root/src/core/settings.c
diff options
context:
space:
mode:
authorMichael Brown2013-07-12 14:45:55 +0200
committerMichael Brown2013-07-12 15:14:36 +0200
commit7774ceed2f57d3711b29ccc464e6c88f20e3c83b (patch)
tree9939250819e8612447a50582ccc73546bf22e6e3 /src/core/settings.c
parent[base16] Generalise base16_decode() to hex_decode() (diff)
downloadipxe-7774ceed2f57d3711b29ccc464e6c88f20e3c83b.tar.gz
ipxe-7774ceed2f57d3711b29ccc464e6c88f20e3c83b.tar.xz
ipxe-7774ceed2f57d3711b29ccc464e6c88f20e3c83b.zip
[settings] Use hex_decode() to parse hex settings
Use hex_decode() to parse "hex" and "hexhyp" settings. Note that this parser is stricter than the old parser; it now requires exactly two hex digits for each byte. (The old parser was based upon strtoul() and so would allow leading whitespace and a leading plus or minus sign.) Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/settings.c')
-rw-r--r--src/core/settings.c61
1 files changed, 29 insertions, 32 deletions
diff --git a/src/core/settings.c b/src/core/settings.c
index 54d48d32..42caae0c 100644
--- a/src/core/settings.c
+++ b/src/core/settings.c
@@ -32,6 +32,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/dhcp.h>
#include <ipxe/uuid.h>
#include <ipxe/uri.h>
+#include <ipxe/base16.h>
#include <ipxe/init.h>
#include <ipxe/settings.h>
@@ -1686,37 +1687,6 @@ struct setting_type setting_type_uint32 __setting_type = {
};
/**
- * Parse hex string setting value
- *
- * @v value Formatted setting value
- * @v buf Buffer to contain raw value
- * @v len Length of buffer
- * @ret len Length of raw value, or negative error
- */
-static int parse_hex_setting ( const char *value, void *buf, size_t len ) {
- char *ptr = ( char * ) value;
- uint8_t *bytes = buf;
- unsigned int count = 0;
- uint8_t byte;
-
- while ( 1 ) {
- byte = strtoul ( ptr, &ptr, 16 );
- if ( count++ < len )
- *bytes++ = byte;
- switch ( *ptr ) {
- case '\0' :
- return count;
- case ':' :
- case '-' :
- ptr++;
- break;
- default :
- return -EINVAL;
- }
- }
-}
-
-/**
* Format hex string setting value
*
* @v raw Raw setting value
@@ -1743,6 +1713,19 @@ static int format_hex_setting ( const void *raw, size_t raw_len, char *buf,
}
/**
+ * Parse hex string setting value (using colon delimiter)
+ *
+ * @v value Formatted setting value
+ * @v buf Buffer to contain raw value
+ * @v len Length of buffer
+ * @v size Integer size, in bytes
+ * @ret len Length of raw value, or negative error
+ */
+static int parse_hex_setting ( const char *value, void *buf, size_t len ) {
+ return hex_decode ( value, ':', buf, len );
+}
+
+/**
* Format hex string setting value (using colon delimiter)
*
* @v raw Raw setting value
@@ -1757,6 +1740,20 @@ static int format_hex_colon_setting ( const void *raw, size_t raw_len,
}
/**
+ * Parse hex string setting value (using hyphen delimiter)
+ *
+ * @v value Formatted setting value
+ * @v buf Buffer to contain raw value
+ * @v len Length of buffer
+ * @v size Integer size, in bytes
+ * @ret len Length of raw value, or negative error
+ */
+static int parse_hex_hyphen_setting ( const char *value, void *buf,
+ size_t len ) {
+ return hex_decode ( value, '-', buf, len );
+}
+
+/**
* Format hex string setting value (using hyphen delimiter)
*
* @v raw Raw setting value
@@ -1780,7 +1777,7 @@ struct setting_type setting_type_hex __setting_type = {
/** A hex-string setting (hyphen-delimited) */
struct setting_type setting_type_hexhyp __setting_type = {
.name = "hexhyp",
- .parse = parse_hex_setting,
+ .parse = parse_hex_hyphen_setting,
.format = format_hex_hyphen_setting,
};