]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[settings] Add "base64" setting type
authorMichael Brown <mcb30@ipxe.org>
Fri, 24 Apr 2015 12:54:40 +0000 (13:54 +0100)
committerMichael Brown <mcb30@ipxe.org>
Fri, 24 Apr 2015 14:36:41 +0000 (15:36 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/settings.c
src/include/ipxe/settings.h
src/tests/settings_test.c

index be48ea7a7d85d68db97627ea329d831516f9b990..12e6c7d61b1228576675c22971a0040eb6d5dbbc 100644 (file)
@@ -39,6 +39,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <ipxe/uuid.h>
 #include <ipxe/uri.h>
 #include <ipxe/base16.h>
+#include <ipxe/base64.h>
 #include <ipxe/pci.h>
 #include <ipxe/init.h>
 #include <ipxe/version.h>
@@ -2120,6 +2121,46 @@ const struct setting_type setting_type_hexraw __setting_type = {
        .format = format_hex_raw_setting,
 };
 
+/**
+ * Parse Base64-encoded setting value
+ *
+ * @v type             Setting type
+ * @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_base64_setting ( const struct setting_type *type __unused,
+                                 const char *value, void *buf, size_t len ) {
+
+       return base64_decode ( value, buf, len );
+}
+
+/**
+ * Format Base64-encoded setting value
+ *
+ * @v type             Setting type
+ * @v raw              Raw setting value
+ * @v raw_len          Length of raw setting value
+ * @v buf              Buffer to contain formatted value
+ * @v len              Length of buffer
+ * @ret len            Length of formatted value, or negative error
+ */
+static int format_base64_setting ( const struct setting_type *type __unused,
+                                  const void *raw, size_t raw_len,
+                                  char *buf, size_t len ) {
+
+       return base64_encode ( raw, raw_len, buf, len );
+}
+
+/** A Base64-encoded setting */
+const struct setting_type setting_type_base64 __setting_type = {
+       .name = "base64",
+       .parse = parse_base64_setting,
+       .format = format_base64_setting,
+};
+
 /**
  * Format UUID setting value
  *
index 45d600b2074fce3d660e87ed3de853d006661b24..95a553cc8ee19b14a2c881126389373cf00f665e 100644 (file)
@@ -415,6 +415,7 @@ extern const struct setting_type setting_type_uint32 __setting_type;
 extern const struct setting_type setting_type_hex __setting_type;
 extern const struct setting_type setting_type_hexhyp __setting_type;
 extern const struct setting_type setting_type_hexraw __setting_type;
+extern const struct setting_type setting_type_base64 __setting_type;
 extern const struct setting_type setting_type_uuid __setting_type;
 extern const struct setting_type setting_type_busdevfn __setting_type;
 extern const struct setting_type setting_type_dnssl __setting_type;
index d80a6ea86d8003cff1a6e4ee514b240031439698..f7fb35d0d67bc1e93703878c87de795d967b84ab 100644 (file)
@@ -232,6 +232,12 @@ static struct setting test_hexraw_setting = {
        .type = &setting_type_hexraw,
 };
 
+/** Test Base64 setting type */
+static struct setting test_base64_setting = {
+       .name = "test_base64",
+       .type = &setting_type_base64,
+};
+
 /** Test UUID setting type */
 static struct setting test_uuid_setting = {
        .name = "test_uuid",
@@ -383,6 +389,15 @@ static void settings_test_exec ( void ) {
                          0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
                    "9e4b6eef36b646fe8f1706396bf4484e" );
 
+       /* "base64" setting type */
+       storef_ok ( &test_settings, &test_base64_setting,
+                   "cGFzc6\nNwaHJhc2U= ",
+                   RAW ( 0x70, 0x61, 0x73, 0x73, 0xa3, 0x70, 0x68, 0x72, 0x61,
+                         0x73, 0x65 ) );
+       fetchf_ok ( &test_settings, &test_base64_setting,
+                   RAW ( 0x80, 0x81, 0x82, 0x83, 0x84, 0x00, 0xff ),
+                   "gIGCg4QA/w==" );
+
        /* "uuid" setting type (no store capability) */
        fetchf_ok ( &test_settings, &test_uuid_setting,
                    RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,