Browse Source

[settings] Add "hexraw" setting type

Originally-implemented-by: Jeppe Toustrup <ipxe@tenzer.dk>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
9f3bbaca07
3 changed files with 50 additions and 0 deletions
  1. 35
    0
      src/core/settings.c
  2. 1
    0
      src/include/ipxe/settings.h
  3. 14
    0
      src/tests/settings_test.c

+ 35
- 0
src/core/settings.c View File

@@ -1767,6 +1767,34 @@ static int format_hex_hyphen_setting ( const void *raw, size_t raw_len,
1767 1767
 	return format_hex_setting ( raw, raw_len, buf, len, "-" );
1768 1768
 }
1769 1769
 
1770
+/**
1771
+ * Parse hex string setting value (using no delimiter)
1772
+ *
1773
+ * @v value		Formatted setting value
1774
+ * @v buf		Buffer to contain raw value
1775
+ * @v len		Length of buffer
1776
+ * @v size		Integer size, in bytes
1777
+ * @ret len		Length of raw value, or negative error
1778
+ */
1779
+static int parse_hex_raw_setting ( const char *value, void *buf,
1780
+				   size_t len ) {
1781
+	return hex_decode ( value, 0, buf, len );
1782
+}
1783
+
1784
+/**
1785
+ * Format hex string setting value (using no delimiter)
1786
+ *
1787
+ * @v raw		Raw setting value
1788
+ * @v raw_len		Length of raw setting value
1789
+ * @v buf		Buffer to contain formatted value
1790
+ * @v len		Length of buffer
1791
+ * @ret len		Length of formatted value, or negative error
1792
+ */
1793
+static int format_hex_raw_setting ( const void *raw, size_t raw_len,
1794
+				    char *buf, size_t len ) {
1795
+	return format_hex_setting ( raw, raw_len, buf, len, "" );
1796
+}
1797
+
1770 1798
 /** A hex-string setting (colon-delimited) */
1771 1799
 struct setting_type setting_type_hex __setting_type = {
1772 1800
 	.name = "hex",
@@ -1781,6 +1809,13 @@ struct setting_type setting_type_hexhyp __setting_type = {
1781 1809
 	.format = format_hex_hyphen_setting,
1782 1810
 };
1783 1811
 
1812
+/** A hex-string setting (non-delimited) */
1813
+struct setting_type setting_type_hexraw __setting_type = {
1814
+	.name = "hexraw",
1815
+	.parse = parse_hex_raw_setting,
1816
+	.format = format_hex_raw_setting,
1817
+};
1818
+
1784 1819
 /**
1785 1820
  * Parse UUID setting value
1786 1821
  *

+ 1
- 0
src/include/ipxe/settings.h View File

@@ -322,6 +322,7 @@ extern struct setting_type setting_type_uint16 __setting_type;
322 322
 extern struct setting_type setting_type_uint32 __setting_type;
323 323
 extern struct setting_type setting_type_hex __setting_type;
324 324
 extern struct setting_type setting_type_hexhyp __setting_type;
325
+extern struct setting_type setting_type_hexraw __setting_type;
325 326
 extern struct setting_type setting_type_uuid __setting_type;
326 327
 
327 328
 extern struct setting ip_setting __setting ( SETTING_IPv4 );

+ 14
- 0
src/tests/settings_test.c View File

@@ -170,6 +170,12 @@ static struct setting test_hexhyp_setting = {
170 170
 	.type = &setting_type_hexhyp,
171 171
 };
172 172
 
173
+/** Test raw hex string setting type */
174
+static struct setting test_hexraw_setting = {
175
+	.name = "test_hexraw",
176
+	.type = &setting_type_hexraw,
177
+};
178
+
173 179
 /** Test UUID setting type */
174 180
 static struct setting test_uuid_setting = {
175 181
 	.name = "test_uuid",
@@ -262,6 +268,14 @@ static void settings_test_exec ( void ) {
262 268
 			  0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
263 269
 		    "9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );
264 270
 
271
+	/* "hexraw" setting type */
272
+	storef_ok ( &test_settings, &test_hexraw_setting,
273
+		    "012345abcdef", RAW ( 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef ));
274
+	fetchf_ok ( &test_settings, &test_hexraw_setting,
275
+		    RAW ( 0x9e, 0x4b, 0x6e, 0xef, 0x36, 0xb6, 0x46, 0xfe, 0x8f,
276
+			  0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
277
+		    "9e4b6eef36b646fe8f1706396bf4484e" );
278
+
265 279
 	/* "uuid" setting type (no store capability) */
266 280
 	fetchf_ok ( &test_settings, &test_uuid_setting,
267 281
 		    RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,

Loading…
Cancel
Save