Browse Source

[settings] Add "base64" setting type

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
dc15a5a779
3 changed files with 57 additions and 0 deletions
  1. 41
    0
      src/core/settings.c
  2. 1
    0
      src/include/ipxe/settings.h
  3. 15
    0
      src/tests/settings_test.c

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

@@ -39,6 +39,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
39 39
 #include <ipxe/uuid.h>
40 40
 #include <ipxe/uri.h>
41 41
 #include <ipxe/base16.h>
42
+#include <ipxe/base64.h>
42 43
 #include <ipxe/pci.h>
43 44
 #include <ipxe/init.h>
44 45
 #include <ipxe/version.h>
@@ -2120,6 +2121,46 @@ const struct setting_type setting_type_hexraw __setting_type = {
2120 2121
 	.format = format_hex_raw_setting,
2121 2122
 };
2122 2123
 
2124
+/**
2125
+ * Parse Base64-encoded setting value
2126
+ *
2127
+ * @v type		Setting type
2128
+ * @v value		Formatted setting value
2129
+ * @v buf		Buffer to contain raw value
2130
+ * @v len		Length of buffer
2131
+ * @v size		Integer size, in bytes
2132
+ * @ret len		Length of raw value, or negative error
2133
+ */
2134
+static int parse_base64_setting ( const struct setting_type *type __unused,
2135
+				  const char *value, void *buf, size_t len ) {
2136
+
2137
+	return base64_decode ( value, buf, len );
2138
+}
2139
+
2140
+/**
2141
+ * Format Base64-encoded setting value
2142
+ *
2143
+ * @v type		Setting type
2144
+ * @v raw		Raw setting value
2145
+ * @v raw_len		Length of raw setting value
2146
+ * @v buf		Buffer to contain formatted value
2147
+ * @v len		Length of buffer
2148
+ * @ret len		Length of formatted value, or negative error
2149
+ */
2150
+static int format_base64_setting ( const struct setting_type *type __unused,
2151
+				   const void *raw, size_t raw_len,
2152
+				   char *buf, size_t len ) {
2153
+
2154
+	return base64_encode ( raw, raw_len, buf, len );
2155
+}
2156
+
2157
+/** A Base64-encoded setting */
2158
+const struct setting_type setting_type_base64 __setting_type = {
2159
+	.name = "base64",
2160
+	.parse = parse_base64_setting,
2161
+	.format = format_base64_setting,
2162
+};
2163
+
2123 2164
 /**
2124 2165
  * Format UUID setting value
2125 2166
  *

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

@@ -415,6 +415,7 @@ extern const struct setting_type setting_type_uint32 __setting_type;
415 415
 extern const struct setting_type setting_type_hex __setting_type;
416 416
 extern const struct setting_type setting_type_hexhyp __setting_type;
417 417
 extern const struct setting_type setting_type_hexraw __setting_type;
418
+extern const struct setting_type setting_type_base64 __setting_type;
418 419
 extern const struct setting_type setting_type_uuid __setting_type;
419 420
 extern const struct setting_type setting_type_busdevfn __setting_type;
420 421
 extern const struct setting_type setting_type_dnssl __setting_type;

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

@@ -232,6 +232,12 @@ static struct setting test_hexraw_setting = {
232 232
 	.type = &setting_type_hexraw,
233 233
 };
234 234
 
235
+/** Test Base64 setting type */
236
+static struct setting test_base64_setting = {
237
+	.name = "test_base64",
238
+	.type = &setting_type_base64,
239
+};
240
+
235 241
 /** Test UUID setting type */
236 242
 static struct setting test_uuid_setting = {
237 243
 	.name = "test_uuid",
@@ -383,6 +389,15 @@ static void settings_test_exec ( void ) {
383 389
 			  0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
384 390
 		    "9e4b6eef36b646fe8f1706396bf4484e" );
385 391
 
392
+	/* "base64" setting type */
393
+	storef_ok ( &test_settings, &test_base64_setting,
394
+		    "cGFzc6\nNwaHJhc2U= ",
395
+		    RAW ( 0x70, 0x61, 0x73, 0x73, 0xa3, 0x70, 0x68, 0x72, 0x61,
396
+			  0x73, 0x65 ) );
397
+	fetchf_ok ( &test_settings, &test_base64_setting,
398
+		    RAW ( 0x80, 0x81, 0x82, 0x83, 0x84, 0x00, 0xff ),
399
+		    "gIGCg4QA/w==" );
400
+
386 401
 	/* "uuid" setting type (no store capability) */
387 402
 	fetchf_ok ( &test_settings, &test_uuid_setting,
388 403
 		    RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,

Loading…
Cancel
Save