Skip to content

Commit

Permalink
[settings] Add "hexraw" setting type
Browse files Browse the repository at this point in the history
Originally-implemented-by: Jeppe Toustrup <ipxe@tenzer.dk>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jul 12, 2013
1 parent 7774cee commit 9f3bbac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/core/settings.c
Expand Up @@ -1767,6 +1767,34 @@ static int format_hex_hyphen_setting ( const void *raw, size_t raw_len,
return format_hex_setting ( raw, raw_len, buf, len, "-" );
}

/**
* Parse hex string setting value (using no 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_raw_setting ( const char *value, void *buf,
size_t len ) {
return hex_decode ( value, 0, buf, len );
}

/**
* Format hex string setting value (using no delimiter)
*
* @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_hex_raw_setting ( const void *raw, size_t raw_len,
char *buf, size_t len ) {
return format_hex_setting ( raw, raw_len, buf, len, "" );
}

/** A hex-string setting (colon-delimited) */
struct setting_type setting_type_hex __setting_type = {
.name = "hex",
Expand All @@ -1781,6 +1809,13 @@ struct setting_type setting_type_hexhyp __setting_type = {
.format = format_hex_hyphen_setting,
};

/** A hex-string setting (non-delimited) */
struct setting_type setting_type_hexraw __setting_type = {
.name = "hexraw",
.parse = parse_hex_raw_setting,
.format = format_hex_raw_setting,
};

/**
* Parse UUID setting value
*
Expand Down
1 change: 1 addition & 0 deletions src/include/ipxe/settings.h
Expand Up @@ -322,6 +322,7 @@ extern struct setting_type setting_type_uint16 __setting_type;
extern struct setting_type setting_type_uint32 __setting_type;
extern struct setting_type setting_type_hex __setting_type;
extern struct setting_type setting_type_hexhyp __setting_type;
extern struct setting_type setting_type_hexraw __setting_type;
extern struct setting_type setting_type_uuid __setting_type;

extern struct setting ip_setting __setting ( SETTING_IPv4 );
Expand Down
14 changes: 14 additions & 0 deletions src/tests/settings_test.c
Expand Up @@ -170,6 +170,12 @@ static struct setting test_hexhyp_setting = {
.type = &setting_type_hexhyp,
};

/** Test raw hex string setting type */
static struct setting test_hexraw_setting = {
.name = "test_hexraw",
.type = &setting_type_hexraw,
};

/** Test UUID setting type */
static struct setting test_uuid_setting = {
.name = "test_uuid",
Expand Down Expand Up @@ -262,6 +268,14 @@ static void settings_test_exec ( void ) {
0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
"9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );

/* "hexraw" setting type */
storef_ok ( &test_settings, &test_hexraw_setting,
"012345abcdef", RAW ( 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef ));
fetchf_ok ( &test_settings, &test_hexraw_setting,
RAW ( 0x9e, 0x4b, 0x6e, 0xef, 0x36, 0xb6, 0x46, 0xfe, 0x8f,
0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
"9e4b6eef36b646fe8f1706396bf4484e" );

/* "uuid" setting type (no store capability) */
fetchf_ok ( &test_settings, &test_uuid_setting,
RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
Expand Down

0 comments on commit 9f3bbac

Please sign in to comment.