Whamcloud - gitweb
compare.c (uuid_compare), copy.c (uuid_copy),
[tools/e2fsprogs.git] / lib / uuid / parse.c
1 /*
2  * parse.c --- UUID parsing
3  * 
4  * Copyright (C) 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU 
8  * Library General Public License.
9  * %End-Header%
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16
17 #include "uuidP.h"
18
19 int uuid_parse(const char *in, uuid_t uu)
20 {
21         struct uuid     uuid;
22         int             i;
23         const char      *cp;
24         char            buf[3];
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) ||
30                     (i == 23))
31                         if (*cp == '-')
32                                 continue;
33                 if (i== 36)
34                         if (*cp == 0)
35                                 continue;
36                 if (!isxdigit(*cp))
37                         return -1;
38         }
39         uuid.time_low = strtoul(in, NULL, 16);
40         uuid.time_mid = strtoul(in+9, NULL, 16);
41         uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
42         uuid.clock_seq = strtoul(in+19, NULL, 16);
43         cp = in+24;
44         buf[2] = 0;
45         for (i=0; i < 6; i++) {
46                 buf[0] = *cp++;
47                 buf[1] = *cp++;
48                 uuid.node[i] = strtoul(buf, NULL, 16);
49         }
50         
51         uuid_pack(&uuid, uu);
52         return 0;
53 }