Skip to content

Commit

Permalink
util: fix ni_uuid_parse() to support 8-4-4-4-12 format only
Browse files Browse the repository at this point in the history
  • Loading branch information
wipawel committed Feb 28, 2014
1 parent ac9b735 commit 9cd0364
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/util.c
Expand Up @@ -2013,42 +2013,39 @@ ni_uuid_print(const ni_uuid_t *uuid)
int
ni_uuid_parse(ni_uuid_t *uuid, const char *string)
{
unsigned int nibbles = 0;
uint32_t word = 0;
enum {
UUID_SEP1 = 8,
UUID_SEP2 = 13,
UUID_SEP3 = 18,
UUID_SEP4 = 23,
UUID_LEN = (32 + 4)
};
char tmp[UUID_LEN+1] = { 0 };
int ret;

if (uuid == NULL || string == NULL)
return -1;

if (*string == '\0') {
if (ni_string_empty(string)) {
memset(uuid, 0, sizeof(*uuid));
return 0;
}

while (*string) {
char cc = tolower(*string++);

if (nibbles == 32)
return -1;

if (isdigit(cc)) {
word = (word << 4) | (cc - '0');
} else if ('a' <= cc && cc <= 'f') {
word = (word << 4) | (cc - 'a' + 10);
} else {
return -1;
}
++nibbles;

if (nibbles == 8) {
uuid->words[nibbles / 8] = word;
if (*string == '-' || *string == ':')
++string;
}
if (ni_string_len(string) == UUID_LEN &&
(string[UUID_SEP1] == '-') &&
(string[UUID_SEP2] == '-') &&
(string[UUID_SEP3] == '-') &&
(string[UUID_SEP4] == '-')) {
memcpy(tmp, string, UUID_LEN);
ni_string_remove_char(tmp, '-');
}

if (nibbles < 32)
else {
return -1;
return 0;
}

ret = ni_parse_hex_data(tmp, uuid->octets, sizeof(uuid->octets), NULL);

return ret == ((UUID_LEN-4)>>1) ? 0 : -1;
}

int
Expand Down
2 changes: 2 additions & 0 deletions testing/Makefile.am
Expand Up @@ -4,6 +4,7 @@ MAINTAINERCLEANFILES = Makefile.in

noinst_PROGRAMS = rtnl-test \
hex-test \
uuid-test \
xml-test \
ibft-test \
xpath-test \
Expand All @@ -21,6 +22,7 @@ LDADD = $(top_builddir)/src/libwicked.la

rtnl_test_SOURCES = rtnl-test.c
hex_test_SOURCES = hex-test.c
uuid_test_SOURCES = uuid-test.c
xml_test_SOURCES = xml-test.c
ibft_test_SOURCES = ibft-test.c
xpath_test_SOURCES = xpath-test.c
Expand Down
105 changes: 105 additions & 0 deletions testing/uuid-test.c
@@ -0,0 +1,105 @@
#include <stdio.h>
#include <wicked/util.h>

int main(void)
{
ni_uuid_t uuid;
char *string;
int rv = 0;

memset(&uuid, 0, sizeof(uuid));

string = "aabbccddeeffAABBCCDDEEFF00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabbccddeeffAABBCCDDEEFF00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabbccddeeffAABBCCDDEEFF00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabbRcdd-eeff-AABB-CCDD-EEFF00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabbccdd-eeff-AABB-CCDD-EEFF00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabbccdd-eeffAABB-CCDDEEFF-00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aa-bb-cc-dd-ee-ff-AA-BB-CC-DD-EE-FF-00-11-22-33";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

string = "aabbccddeeffAABB-CCDDEEFF00112233";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));


string = "aabb-ccdd-eeff-AABB-CCDD-EEFF";
rv = ni_uuid_parse(&uuid, string);
printf("\nString\t\t=%s\n", string);
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
printf("$? = %d\n", rv);
memset(&uuid, 0, sizeof(uuid));

return 0;
}

0 comments on commit 9cd0364

Please sign in to comment.