1106184Smarcel/*-
2106184Smarcel * Copyright (c) 2002 Marcel Moolenaar
3106184Smarcel * Copyright (c) 2002 Hiten Mahesh Pandya
4106184Smarcel * All rights reserved.
5106184Smarcel *
6106184Smarcel * Redistribution and use in source and binary forms, with or without
7106184Smarcel * modification, are permitted provided that the following conditions
8106184Smarcel * are met:
9106184Smarcel * 1. Redistributions of source code must retain the above copyright
10106184Smarcel *    notice, this list of conditions and the following disclaimer.
11106184Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12106184Smarcel *    notice, this list of conditions and the following disclaimer in the
13106184Smarcel *    documentation and/or other materials provided with the distribution.
14106184Smarcel *
15106184Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16106184Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17106184Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18106184Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19106184Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20106184Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21106184Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22106184Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23106184Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24106184Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25106184Smarcel * SUCH DAMAGE.
26106184Smarcel *
27106184Smarcel * $FreeBSD: releng/11.0/lib/libc/uuid/uuid_from_string.c 118670 2003-08-08 19:18:43Z marcel $
28106184Smarcel */
29106184Smarcel
30106184Smarcel#include <stdio.h>
31106184Smarcel#include <string.h>
32106184Smarcel#include <uuid.h>
33106184Smarcel
34106184Smarcel/*
35106184Smarcel * uuid_from_string() - convert a string representation of an UUID into
36106184Smarcel *			a binary representation.
37106184Smarcel * See also:
38106184Smarcel *	http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm
39106184Smarcel *
40106184Smarcel * NOTE: The sequence field is in big-endian, while the time fields are in
41106184Smarcel *	 native byte order.
42106184Smarcel */
43106184Smarcelvoid
44106184Smarceluuid_from_string(const char *s, uuid_t *u, uint32_t *status)
45106184Smarcel{
46106184Smarcel	int n;
47106184Smarcel
48106184Smarcel	/* Short-circuit 2 special cases: NULL pointer and empty string. */
49106184Smarcel	if (s == NULL || *s == '\0') {
50106184Smarcel		uuid_create_nil(u, status);
51106184Smarcel		return;
52106184Smarcel	}
53106184Smarcel
54106184Smarcel	/* Assume the worst. */
55106184Smarcel	if (status != NULL)
56106184Smarcel		*status = uuid_s_invalid_string_uuid;
57106184Smarcel
58106184Smarcel	/* The UUID string representation has a fixed length. */
59106184Smarcel	if (strlen(s) != 36)
60106184Smarcel		return;
61106184Smarcel
62106184Smarcel	/*
63106184Smarcel	 * We only work with "new" UUIDs. New UUIDs have the form:
64106184Smarcel	 *	01234567-89ab-cdef-0123-456789abcdef
65106184Smarcel	 * The so called "old" UUIDs, which we don't support, have the form:
66106184Smarcel	 *	0123456789ab.cd.ef.01.23.45.67.89.ab
67106184Smarcel	 */
68106184Smarcel	if (s[8] != '-')
69106184Smarcel		return;
70106184Smarcel
71106184Smarcel	n = sscanf(s,
72106184Smarcel	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
73106184Smarcel	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
74106184Smarcel	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
75106184Smarcel	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
76106184Smarcel
77106184Smarcel	/* Make sure we have all conversions. */
78106184Smarcel	if (n != 11)
79106184Smarcel		return;
80106184Smarcel
81106184Smarcel	/* We have a successful scan. Check semantics... */
82106184Smarcel	n = u->clock_seq_hi_and_reserved;
83106184Smarcel	if ((n & 0x80) != 0x00 &&			/* variant 0? */
84106184Smarcel	    (n & 0xc0) != 0x80 &&			/* variant 1? */
85106184Smarcel	    (n & 0xe0) != 0xc0) {			/* variant 2? */
86106184Smarcel		if (status != NULL)
87106184Smarcel			*status = uuid_s_bad_version;
88106184Smarcel	} else {
89106184Smarcel		if (status != NULL)
90106184Smarcel			*status = uuid_s_ok;
91106184Smarcel	}
92106184Smarcel}
93