xref: /dflybsd-src/lib/libc/uuid/uuid_from_string.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino  * Copyright (c) 2002 Marcel Moolenaar
386d7f5d3SJohn Marino  * Copyright (c) 2002 Hiten Mahesh Pandya
486d7f5d3SJohn Marino  * All rights reserved.
586d7f5d3SJohn Marino  *
686d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
786d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
886d7f5d3SJohn Marino  * are met:
986d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1086d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1186d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1286d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1386d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
1486d7f5d3SJohn Marino  *
1586d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1686d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1786d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1886d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1986d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2086d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2186d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2286d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2386d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2486d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2586d7f5d3SJohn Marino  * SUCH DAMAGE.
2686d7f5d3SJohn Marino  *
2786d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/uuid/uuid_from_string.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
2886d7f5d3SJohn Marino  * $DragonFly: src/lib/libc/uuid/uuid_from_string.c,v 1.1 2007/06/16 19:57:14 dillon Exp $
2986d7f5d3SJohn Marino  */
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino #include <stdio.h>
3286d7f5d3SJohn Marino #include <string.h>
3386d7f5d3SJohn Marino #include <uuid.h>
3486d7f5d3SJohn Marino 
3586d7f5d3SJohn Marino /*
3686d7f5d3SJohn Marino  * uuid_from_string() - convert a string representation of an UUID into
3786d7f5d3SJohn Marino  *			a binary representation.
3886d7f5d3SJohn Marino  * See also:
3986d7f5d3SJohn Marino  *	http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm
4086d7f5d3SJohn Marino  *
4186d7f5d3SJohn Marino  * NOTE: The sequence field is in big-endian, while the time fields are in
4286d7f5d3SJohn Marino  *	 native byte order.
4386d7f5d3SJohn Marino  */
4486d7f5d3SJohn Marino void
uuid_from_string(const char * s,uuid_t * u,uint32_t * status)4586d7f5d3SJohn Marino uuid_from_string(const char *s, uuid_t *u, uint32_t *status)
4686d7f5d3SJohn Marino {
4786d7f5d3SJohn Marino 	int n;
4886d7f5d3SJohn Marino 
4986d7f5d3SJohn Marino 	/* Short-circuit 2 special cases: NULL pointer and empty string. */
5086d7f5d3SJohn Marino 	if (s == NULL || *s == '\0') {
5186d7f5d3SJohn Marino 		uuid_create_nil(u, status);
5286d7f5d3SJohn Marino 		return;
5386d7f5d3SJohn Marino 	}
5486d7f5d3SJohn Marino 
5586d7f5d3SJohn Marino 	/* Assume the worst. */
5686d7f5d3SJohn Marino 	if (status != NULL)
5786d7f5d3SJohn Marino 		*status = uuid_s_invalid_string_uuid;
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino 	/* The UUID string representation has a fixed length. */
6086d7f5d3SJohn Marino 	if (strlen(s) != 36)
6186d7f5d3SJohn Marino 		return;
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino 	/*
6486d7f5d3SJohn Marino 	 * We only work with "new" UUIDs. New UUIDs have the form:
6586d7f5d3SJohn Marino 	 *	01234567-89ab-cdef-0123-456789abcdef
6686d7f5d3SJohn Marino 	 * The so called "old" UUIDs, which we don't support, have the form:
6786d7f5d3SJohn Marino 	 *	0123456789ab.cd.ef.01.23.45.67.89.ab
6886d7f5d3SJohn Marino 	 */
6986d7f5d3SJohn Marino 	if (s[8] != '-')
7086d7f5d3SJohn Marino 		return;
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino 	n = sscanf(s,
7386d7f5d3SJohn Marino 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
7486d7f5d3SJohn Marino 	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
7586d7f5d3SJohn Marino 	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
7686d7f5d3SJohn Marino 	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino 	/* Make sure we have all conversions. */
7986d7f5d3SJohn Marino 	if (n != 11)
8086d7f5d3SJohn Marino 		return;
8186d7f5d3SJohn Marino 
8286d7f5d3SJohn Marino 	/* We have a successful scan. Check semantics... */
8386d7f5d3SJohn Marino 	n = u->clock_seq_hi_and_reserved;
8486d7f5d3SJohn Marino 	if ((n & 0x80) != 0x00 &&			/* variant 0? */
8586d7f5d3SJohn Marino 	    (n & 0xc0) != 0x80 &&			/* variant 1? */
8686d7f5d3SJohn Marino 	    (n & 0xe0) != 0xc0) {			/* variant 2? */
8786d7f5d3SJohn Marino 		if (status != NULL)
8886d7f5d3SJohn Marino 			*status = uuid_s_bad_version;
8986d7f5d3SJohn Marino 	} else {
9086d7f5d3SJohn Marino 		if (status != NULL)
9186d7f5d3SJohn Marino 			*status = uuid_s_ok;
9286d7f5d3SJohn Marino 	}
9386d7f5d3SJohn Marino }
94