Skip to content

Commit eee3dae

Browse files
committed
Merge pull request #182 from pwieczorkiewicz/uuid_parse
2 parents 80ba0cd + 9cd0364 commit eee3dae

File tree

3 files changed

+130
-26
lines changed

3 files changed

+130
-26
lines changed

src/util.c

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,42 +2013,39 @@ ni_uuid_print(const ni_uuid_t *uuid)
20132013
int
20142014
ni_uuid_parse(ni_uuid_t *uuid, const char *string)
20152015
{
2016-
unsigned int nibbles = 0;
2017-
uint32_t word = 0;
2016+
enum {
2017+
UUID_SEP1 = 8,
2018+
UUID_SEP2 = 13,
2019+
UUID_SEP3 = 18,
2020+
UUID_SEP4 = 23,
2021+
UUID_LEN = (32 + 4)
2022+
};
2023+
char tmp[UUID_LEN+1] = { 0 };
2024+
int ret;
20182025

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

2022-
if (*string == '\0') {
2029+
if (ni_string_empty(string)) {
20232030
memset(uuid, 0, sizeof(*uuid));
20242031
return 0;
20252032
}
20262033

2027-
while (*string) {
2028-
char cc = tolower(*string++);
2029-
2030-
if (nibbles == 32)
2031-
return -1;
2032-
2033-
if (isdigit(cc)) {
2034-
word = (word << 4) | (cc - '0');
2035-
} else if ('a' <= cc && cc <= 'f') {
2036-
word = (word << 4) | (cc - 'a' + 10);
2037-
} else {
2038-
return -1;
2039-
}
2040-
++nibbles;
2041-
2042-
if (nibbles == 8) {
2043-
uuid->words[nibbles / 8] = word;
2044-
if (*string == '-' || *string == ':')
2045-
++string;
2046-
}
2034+
if (ni_string_len(string) == UUID_LEN &&
2035+
(string[UUID_SEP1] == '-') &&
2036+
(string[UUID_SEP2] == '-') &&
2037+
(string[UUID_SEP3] == '-') &&
2038+
(string[UUID_SEP4] == '-')) {
2039+
memcpy(tmp, string, UUID_LEN);
2040+
ni_string_remove_char(tmp, '-');
20472041
}
2048-
2049-
if (nibbles < 32)
2042+
else {
20502043
return -1;
2051-
return 0;
2044+
}
2045+
2046+
ret = ni_parse_hex_data(tmp, uuid->octets, sizeof(uuid->octets), NULL);
2047+
2048+
return ret == ((UUID_LEN-4)>>1) ? 0 : -1;
20522049
}
20532050

20542051
int

testing/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ MAINTAINERCLEANFILES = Makefile.in
44

55
noinst_PROGRAMS = rtnl-test \
66
hex-test \
7+
uuid-test \
78
xml-test \
89
ibft-test \
910
xpath-test \
@@ -21,6 +22,7 @@ LDADD = $(top_builddir)/src/libwicked.la
2122

2223
rtnl_test_SOURCES = rtnl-test.c
2324
hex_test_SOURCES = hex-test.c
25+
uuid_test_SOURCES = uuid-test.c
2426
xml_test_SOURCES = xml-test.c
2527
ibft_test_SOURCES = ibft-test.c
2628
xpath_test_SOURCES = xpath-test.c

testing/uuid-test.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <stdio.h>
2+
#include <wicked/util.h>
3+
4+
int main(void)
5+
{
6+
ni_uuid_t uuid;
7+
char *string;
8+
int rv = 0;
9+
10+
memset(&uuid, 0, sizeof(uuid));
11+
12+
string = "aabbccddeeffAABBCCDDEEFF00112233";
13+
rv = ni_uuid_parse(&uuid, string);
14+
printf("\nString\t\t=%s\n", string);
15+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
16+
printf("$? = %d\n", rv);
17+
memset(&uuid, 0, sizeof(uuid));
18+
19+
string = "aabbccddeeffAABBCCDDEEFF00112233";
20+
rv = ni_uuid_parse(&uuid, string);
21+
printf("\nString\t\t=%s\n", string);
22+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
23+
printf("$? = %d\n", rv);
24+
memset(&uuid, 0, sizeof(uuid));
25+
26+
string = "aabbccddeeffAABBCCDDEEFF00112233";
27+
rv = ni_uuid_parse(&uuid, string);
28+
printf("\nString\t\t=%s\n", string);
29+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
30+
printf("$? = %d\n", rv);
31+
memset(&uuid, 0, sizeof(uuid));
32+
33+
string = "aabbRcdd-eeff-AABB-CCDD-EEFF00112233";
34+
rv = ni_uuid_parse(&uuid, string);
35+
printf("\nString\t\t=%s\n", string);
36+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
37+
printf("$? = %d\n", rv);
38+
memset(&uuid, 0, sizeof(uuid));
39+
40+
string = "aabbccdd-eeff-AABB-CCDD-EEFF00112233";
41+
rv = ni_uuid_parse(&uuid, string);
42+
printf("\nString\t\t=%s\n", string);
43+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
44+
printf("$? = %d\n", rv);
45+
memset(&uuid, 0, sizeof(uuid));
46+
47+
string = "aabbccdd-eeffAABB-CCDDEEFF-00112233";
48+
rv = ni_uuid_parse(&uuid, string);
49+
printf("\nString\t\t=%s\n", string);
50+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
51+
printf("$? = %d\n", rv);
52+
memset(&uuid, 0, sizeof(uuid));
53+
54+
string = "aa-bb-cc-dd-ee-ff-AA-BB-CC-DD-EE-FF-00-11-22-33";
55+
rv = ni_uuid_parse(&uuid, string);
56+
printf("\nString\t\t=%s\n", string);
57+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
58+
printf("$? = %d\n", rv);
59+
memset(&uuid, 0, sizeof(uuid));
60+
61+
string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
62+
rv = ni_uuid_parse(&uuid, string);
63+
printf("\nString\t\t=%s\n", string);
64+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
65+
printf("$? = %d\n", rv);
66+
memset(&uuid, 0, sizeof(uuid));
67+
68+
string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
69+
rv = ni_uuid_parse(&uuid, string);
70+
printf("\nString\t\t=%s\n", string);
71+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
72+
printf("$? = %d\n", rv);
73+
memset(&uuid, 0, sizeof(uuid));
74+
75+
string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
76+
rv = ni_uuid_parse(&uuid, string);
77+
printf("\nString\t\t=%s\n", string);
78+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
79+
printf("$? = %d\n", rv);
80+
memset(&uuid, 0, sizeof(uuid));
81+
82+
string = "aabb-ccdd-eeff-AABB-CCDD-EEFF-0011-2233";
83+
rv = ni_uuid_parse(&uuid, string);
84+
printf("\nString\t\t=%s\n", string);
85+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
86+
printf("$? = %d\n", rv);
87+
memset(&uuid, 0, sizeof(uuid));
88+
89+
string = "aabbccddeeffAABB-CCDDEEFF00112233";
90+
rv = ni_uuid_parse(&uuid, string);
91+
printf("\nString\t\t=%s\n", string);
92+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
93+
printf("$? = %d\n", rv);
94+
memset(&uuid, 0, sizeof(uuid));
95+
96+
97+
string = "aabb-ccdd-eeff-AABB-CCDD-EEFF";
98+
rv = ni_uuid_parse(&uuid, string);
99+
printf("\nString\t\t=%s\n", string);
100+
printf("UUID\t\t=%s\n", ni_uuid_print(&uuid));
101+
printf("$? = %d\n", rv);
102+
memset(&uuid, 0, sizeof(uuid));
103+
104+
return 0;
105+
}

0 commit comments

Comments
 (0)