Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * parse.c --- UUID parsing |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
Theodore Ts'o | 3030daa | 2000-04-07 20:06:04 +0000 | [diff] [blame] | 7 | * This file may be redistributed under the terms of the GNU |
| 8 | * Library General Public License. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 9 | * %End-Header% |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 14 | #include <ctype.h> |
Theodore Ts'o | b1416db | 2001-05-01 15:32:44 +0000 | [diff] [blame] | 15 | #include <string.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 16 | |
| 17 | #include "uuidP.h" |
| 18 | |
Theodore Ts'o | ce2722f | 2001-09-10 20:30:09 -0400 | [diff] [blame] | 19 | int uuid_parse(const char *in, uuid_t uu) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 20 | { |
Theodore Ts'o | ce2722f | 2001-09-10 20:30:09 -0400 | [diff] [blame] | 21 | struct uuid uuid; |
| 22 | int i; |
| 23 | const char *cp; |
| 24 | char buf[3]; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 25 | |
| 26 | if (strlen(in) != 36) |
| 27 | return -1; |
| 28 | for (i=0, cp = in; i <= 36; i++,cp++) { |
| 29 | if ((i == 8) || (i == 13) || (i == 18) || |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 30 | (i == 23)) { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 31 | if (*cp == '-') |
| 32 | continue; |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 33 | else |
| 34 | return -1; |
| 35 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 36 | if (i== 36) |
| 37 | if (*cp == 0) |
| 38 | continue; |
| 39 | if (!isxdigit(*cp)) |
| 40 | return -1; |
| 41 | } |
| 42 | uuid.time_low = strtoul(in, NULL, 16); |
| 43 | uuid.time_mid = strtoul(in+9, NULL, 16); |
| 44 | uuid.time_hi_and_version = strtoul(in+14, NULL, 16); |
| 45 | uuid.clock_seq = strtoul(in+19, NULL, 16); |
| 46 | cp = in+24; |
| 47 | buf[2] = 0; |
| 48 | for (i=0; i < 6; i++) { |
| 49 | buf[0] = *cp++; |
| 50 | buf[1] = *cp++; |
| 51 | uuid.node[i] = strtoul(buf, NULL, 16); |
| 52 | } |
| 53 | |
| 54 | uuid_pack(&uuid, uu); |
| 55 | return 0; |
| 56 | } |