aboutsummaryrefslogtreecommitdiff
path: root/lib/uuid.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2010-11-15 10:18:10 -0800
committerBen Pfaff <blp@nicira.com>2010-11-15 10:18:10 -0800
commitbf9712678fc9ec85bf2ac54407e16d76aa22e7b6 (patch)
tree2052e512f4606aad7ec50bea03ccf33118e0b44a /lib/uuid.c
parent96fc46e8fdafd6467906e11e0fb493e2b78f2fb5 (diff)
util: Add function hexits_value() for parsing multiple hex digits.
Suggested-by: Justin Pettit <jpettit@nicira.com>
Diffstat (limited to 'lib/uuid.c')
-rw-r--r--lib/uuid.c50
1 files changed, 35 insertions, 15 deletions
diff --git a/lib/uuid.c b/lib/uuid.c
index 2aac4c72..e2590242 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -156,23 +156,43 @@ uuid_from_string(struct uuid *uuid, const char *s)
bool
uuid_from_string_prefix(struct uuid *uuid, const char *s)
{
- static const char template[] = "00000000-1111-1111-2222-222233333333";
- const char *t;
+ /* 0 1 2 3 */
+ /* 012345678901234567890123456789012345 */
+ /* ------------------------------------ */
+ /* 00000000-1111-1111-2222-222233333333 */
- uuid_zero(uuid);
- for (t = template; ; t++, s++) {
- if (*t >= '0' && *t <= '3') {
- uint32_t *part = &uuid->parts[*t - '0'];
- if (!isxdigit(*s)) {
- goto error;
- }
- *part = (*part << 4) + hexit_value(*s);
- } else if (*t == 0) {
- return true;
- } else if (*t != *s) {
- goto error;
- }
+ bool ok;
+
+ uuid->parts[0] = hexits_value(s, 8, &ok);
+ if (!ok || s[8] != '-') {
+ goto error;
+ }
+
+ uuid->parts[1] = hexits_value(s + 9, 4, &ok) << 16;
+ if (!ok || s[13] != '-') {
+ goto error;
+ }
+
+ uuid->parts[1] += hexits_value(s + 14, 4, &ok);
+ if (!ok || s[18] != '-') {
+ goto error;
+ }
+
+ uuid->parts[2] = hexits_value(s + 19, 4, &ok) << 16;
+ if (!ok || s[23] != '-') {
+ goto error;
+ }
+
+ uuid->parts[2] += hexits_value(s + 24, 4, &ok);
+ if (!ok) {
+ goto error;
+ }
+
+ uuid->parts[3] = hexits_value(s + 28, 8, &ok);
+ if (!ok) {
+ goto error;
}
+ return true;
error:
uuid_zero(uuid);