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