xref: /dragonfly/lib/libc/uuid/uuid_from_string.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 2002 Marcel Moolenaar
3*86d7f5d3SJohn Marino  * Copyright (c) 2002 Hiten Mahesh Pandya
4*86d7f5d3SJohn Marino  * All rights reserved.
5*86d7f5d3SJohn Marino  *
6*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
7*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
8*86d7f5d3SJohn Marino  * are met:
9*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
10*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
11*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
12*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
13*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
14*86d7f5d3SJohn Marino  *
15*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*86d7f5d3SJohn Marino  * SUCH DAMAGE.
26*86d7f5d3SJohn Marino  *
27*86d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/uuid/uuid_from_string.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
28*86d7f5d3SJohn Marino  * $DragonFly: src/lib/libc/uuid/uuid_from_string.c,v 1.1 2007/06/16 19:57:14 dillon Exp $
29*86d7f5d3SJohn Marino  */
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino #include <stdio.h>
32*86d7f5d3SJohn Marino #include <string.h>
33*86d7f5d3SJohn Marino #include <uuid.h>
34*86d7f5d3SJohn Marino 
35*86d7f5d3SJohn Marino /*
36*86d7f5d3SJohn Marino  * uuid_from_string() - convert a string representation of an UUID into
37*86d7f5d3SJohn Marino  *			a binary representation.
38*86d7f5d3SJohn Marino  * See also:
39*86d7f5d3SJohn Marino  *	http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm
40*86d7f5d3SJohn Marino  *
41*86d7f5d3SJohn Marino  * NOTE: The sequence field is in big-endian, while the time fields are in
42*86d7f5d3SJohn Marino  *	 native byte order.
43*86d7f5d3SJohn Marino  */
44*86d7f5d3SJohn Marino void
uuid_from_string(const char * s,uuid_t * u,uint32_t * status)45*86d7f5d3SJohn Marino uuid_from_string(const char *s, uuid_t *u, uint32_t *status)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino 	int n;
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino 	/* Short-circuit 2 special cases: NULL pointer and empty string. */
50*86d7f5d3SJohn Marino 	if (s == NULL || *s == '\0') {
51*86d7f5d3SJohn Marino 		uuid_create_nil(u, status);
52*86d7f5d3SJohn Marino 		return;
53*86d7f5d3SJohn Marino 	}
54*86d7f5d3SJohn Marino 
55*86d7f5d3SJohn Marino 	/* Assume the worst. */
56*86d7f5d3SJohn Marino 	if (status != NULL)
57*86d7f5d3SJohn Marino 		*status = uuid_s_invalid_string_uuid;
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino 	/* The UUID string representation has a fixed length. */
60*86d7f5d3SJohn Marino 	if (strlen(s) != 36)
61*86d7f5d3SJohn Marino 		return;
62*86d7f5d3SJohn Marino 
63*86d7f5d3SJohn Marino 	/*
64*86d7f5d3SJohn Marino 	 * We only work with "new" UUIDs. New UUIDs have the form:
65*86d7f5d3SJohn Marino 	 *	01234567-89ab-cdef-0123-456789abcdef
66*86d7f5d3SJohn Marino 	 * The so called "old" UUIDs, which we don't support, have the form:
67*86d7f5d3SJohn Marino 	 *	0123456789ab.cd.ef.01.23.45.67.89.ab
68*86d7f5d3SJohn Marino 	 */
69*86d7f5d3SJohn Marino 	if (s[8] != '-')
70*86d7f5d3SJohn Marino 		return;
71*86d7f5d3SJohn Marino 
72*86d7f5d3SJohn Marino 	n = sscanf(s,
73*86d7f5d3SJohn Marino 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
74*86d7f5d3SJohn Marino 	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
75*86d7f5d3SJohn Marino 	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
76*86d7f5d3SJohn Marino 	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
77*86d7f5d3SJohn Marino 
78*86d7f5d3SJohn Marino 	/* Make sure we have all conversions. */
79*86d7f5d3SJohn Marino 	if (n != 11)
80*86d7f5d3SJohn Marino 		return;
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino 	/* We have a successful scan. Check semantics... */
83*86d7f5d3SJohn Marino 	n = u->clock_seq_hi_and_reserved;
84*86d7f5d3SJohn Marino 	if ((n & 0x80) != 0x00 &&			/* variant 0? */
85*86d7f5d3SJohn Marino 	    (n & 0xc0) != 0x80 &&			/* variant 1? */
86*86d7f5d3SJohn Marino 	    (n & 0xe0) != 0xc0) {			/* variant 2? */
87*86d7f5d3SJohn Marino 		if (status != NULL)
88*86d7f5d3SJohn Marino 			*status = uuid_s_bad_version;
89*86d7f5d3SJohn Marino 	} else {
90*86d7f5d3SJohn Marino 		if (status != NULL)
91*86d7f5d3SJohn Marino 			*status = uuid_s_ok;
92*86d7f5d3SJohn Marino 	}
93*86d7f5d3SJohn Marino }
94