Browse Source

[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>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
7774ceed2f
2 changed files with 38 additions and 39 deletions
  1. 29
    32
      src/core/settings.c
  2. 9
    7
      src/tests/settings_test.c

+ 29
- 32
src/core/settings.c View File

@@ -32,6 +32,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 32
 #include <ipxe/dhcp.h>
33 33
 #include <ipxe/uuid.h>
34 34
 #include <ipxe/uri.h>
35
+#include <ipxe/base16.h>
35 36
 #include <ipxe/init.h>
36 37
 #include <ipxe/settings.h>
37 38
 
@@ -1685,37 +1686,6 @@ struct setting_type setting_type_uint32 __setting_type = {
1685 1686
 	.format = format_uint_setting,
1686 1687
 };
1687 1688
 
1688
-/**
1689
- * Parse hex string setting value
1690
- *
1691
- * @v value		Formatted setting value
1692
- * @v buf		Buffer to contain raw value
1693
- * @v len		Length of buffer
1694
- * @ret len		Length of raw value, or negative error
1695
- */
1696
-static int parse_hex_setting ( const char *value, void *buf, size_t len ) {
1697
-	char *ptr = ( char * ) value;
1698
-	uint8_t *bytes = buf;
1699
-	unsigned int count = 0;
1700
-	uint8_t byte;
1701
-
1702
-	while ( 1 ) {
1703
-		byte = strtoul ( ptr, &ptr, 16 );
1704
-		if ( count++ < len )
1705
-			*bytes++ = byte;
1706
-		switch ( *ptr ) {
1707
-		case '\0' :
1708
-			return count;
1709
-		case ':' :
1710
-		case '-' :
1711
-			ptr++;
1712
-			break;
1713
-		default :
1714
-			return -EINVAL;
1715
-		}
1716
-	}
1717
-}
1718
-
1719 1689
 /**
1720 1690
  * Format hex string setting value
1721 1691
  *
@@ -1742,6 +1712,19 @@ static int format_hex_setting ( const void *raw, size_t raw_len, char *buf,
1742 1712
 	return used;
1743 1713
 }
1744 1714
 
1715
+/**
1716
+ * Parse hex string setting value (using colon delimiter)
1717
+ *
1718
+ * @v value		Formatted setting value
1719
+ * @v buf		Buffer to contain raw value
1720
+ * @v len		Length of buffer
1721
+ * @v size		Integer size, in bytes
1722
+ * @ret len		Length of raw value, or negative error
1723
+ */
1724
+static int parse_hex_setting ( const char *value, void *buf, size_t len ) {
1725
+	return hex_decode ( value, ':', buf, len );
1726
+}
1727
+
1745 1728
 /**
1746 1729
  * Format hex string setting value (using colon delimiter)
1747 1730
  *
@@ -1756,6 +1739,20 @@ static int format_hex_colon_setting ( const void *raw, size_t raw_len,
1756 1739
 	return format_hex_setting ( raw, raw_len, buf, len, ":" );
1757 1740
 }
1758 1741
 
1742
+/**
1743
+ * Parse hex string setting value (using hyphen delimiter)
1744
+ *
1745
+ * @v value		Formatted setting value
1746
+ * @v buf		Buffer to contain raw value
1747
+ * @v len		Length of buffer
1748
+ * @v size		Integer size, in bytes
1749
+ * @ret len		Length of raw value, or negative error
1750
+ */
1751
+static int parse_hex_hyphen_setting ( const char *value, void *buf,
1752
+				      size_t len ) {
1753
+	return hex_decode ( value, '-', buf, len );
1754
+}
1755
+
1759 1756
 /**
1760 1757
  * Format hex string setting value (using hyphen delimiter)
1761 1758
  *
@@ -1780,7 +1777,7 @@ struct setting_type setting_type_hex __setting_type = {
1780 1777
 /** A hex-string setting (hyphen-delimited) */
1781 1778
 struct setting_type setting_type_hexhyp __setting_type = {
1782 1779
 	.name = "hexhyp",
1783
-	.parse = parse_hex_setting,
1780
+	.parse = parse_hex_hyphen_setting,
1784 1781
 	.format = format_hex_hyphen_setting,
1785 1782
 };
1786 1783
 

+ 9
- 7
src/tests/settings_test.c View File

@@ -51,9 +51,15 @@ FILE_LICENCE ( GPL2_OR_LATER );
51 51
 	ok ( storef_setting ( settings, setting, formatted ) == 0 );	\
52 52
 	len = fetch_setting ( settings, setting, actual,		\
53 53
 			      sizeof ( actual ) );			\
54
-	DBGC ( settings, "Stored %s \"%s\", got:\n",			\
55
-	       (setting)->type->name, formatted );			\
56
-	DBGC_HDA ( settings, 0, actual, len );				\
54
+	if ( len >= 0 ) {						\
55
+		DBGC ( settings, "Stored %s \"%s\", got:\n",		\
56
+		       (setting)->type->name, formatted );		\
57
+		DBGC_HDA ( settings, 0, actual, len );			\
58
+	} else {							\
59
+		DBGC ( settings, "Stored %s \"%s\", got error %s\n",	\
60
+		       (setting)->type->name, formatted,		\
61
+		       strerror ( len ) );				\
62
+	}								\
57 63
 	ok ( len == ( int ) sizeof ( actual ) );			\
58 64
 	ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 );	\
59 65
 	} while ( 0 )
@@ -239,10 +245,6 @@ static void settings_test_exec ( void ) {
239 245
 		    RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );
240 246
 
241 247
 	/* "hex" setting type */
242
-	storef_ok ( &test_settings, &test_hex_setting,
243
-		    ":", RAW ( 0x00, 0x00 ) );
244
-	storef_ok ( &test_settings, &test_hex_setting,
245
-		    "1:2:", RAW ( 0x01, 0x02, 0x00 ) );
246 248
 	storef_ok ( &test_settings, &test_hex_setting,
247 249
 		    "08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
248 250
 		    RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,

Loading…
Cancel
Save