blob: f97f13ea4ba98b670d7d0279d2b4e3d772716c69 [file] [log] [blame]
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001/*
2 * parse.c --- UUID parsing
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00003 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o3030daa2000-04-07 20:06:04 +00007 * This file may be redistributed under the terms of the GNU
8 * Library General Public License.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000010 */
11
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000012#include <stdlib.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000013#include <stdio.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000014#include <ctype.h>
Theodore Ts'ob1416db2001-05-01 15:32:44 +000015#include <string.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000016
17#include "uuidP.h"
18
Theodore Ts'oce2722f2001-09-10 20:30:09 -040019int uuid_parse(const char *in, uuid_t uu)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000020{
Theodore Ts'oce2722f2001-09-10 20:30:09 -040021 struct uuid uuid;
22 int i;
23 const char *cp;
24 char buf[3];
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000025
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'o8d7f4582002-07-15 23:49:57 -040030 (i == 23)) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000031 if (*cp == '-')
32 continue;
Theodore Ts'o8d7f4582002-07-15 23:49:57 -040033 else
34 return -1;
35 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000036 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}