1*de5f0841Skrw /* $OpenBSD: uuid_from_string.c,v 1.3 2021/08/30 20:41:33 krw Exp $ */
29a210c53Smiod /* $NetBSD: uuid_from_string.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
39a210c53Smiod
49a210c53Smiod /*
59a210c53Smiod * Copyright (c) 2002 Marcel Moolenaar
69a210c53Smiod * Copyright (c) 2002 Hiten Mahesh Pandya
79a210c53Smiod * All rights reserved.
89a210c53Smiod *
99a210c53Smiod * Redistribution and use in source and binary forms, with or without
109a210c53Smiod * modification, are permitted provided that the following conditions
119a210c53Smiod * are met:
129a210c53Smiod * 1. Redistributions of source code must retain the above copyright
139a210c53Smiod * notice, this list of conditions and the following disclaimer.
149a210c53Smiod * 2. Redistributions in binary form must reproduce the above copyright
159a210c53Smiod * notice, this list of conditions and the following disclaimer in the
169a210c53Smiod * documentation and/or other materials provided with the distribution.
179a210c53Smiod *
189a210c53Smiod * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199a210c53Smiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209a210c53Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219a210c53Smiod * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
229a210c53Smiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239a210c53Smiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249a210c53Smiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259a210c53Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269a210c53Smiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279a210c53Smiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289a210c53Smiod * SUCH DAMAGE.
299a210c53Smiod *
309a210c53Smiod * $FreeBSD: src/lib/libc/uuid/uuid_from_string.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
319a210c53Smiod */
329a210c53Smiod
33*de5f0841Skrw #include <ctype.h>
349a210c53Smiod #include <stdio.h>
359a210c53Smiod #include <string.h>
369a210c53Smiod #include <uuid.h>
379a210c53Smiod
389a210c53Smiod /*
399a210c53Smiod * uuid_from_string() - convert a string representation of an UUID into
409a210c53Smiod * a binary representation.
419a210c53Smiod * See also:
429a210c53Smiod * http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm
439a210c53Smiod *
449a210c53Smiod * NOTE: The sequence field is in big-endian, while the time fields are in
459a210c53Smiod * native byte order.
469a210c53Smiod */
479a210c53Smiod void
uuid_from_string(const char * s,uuid_t * u,uint32_t * status)489a210c53Smiod uuid_from_string(const char *s, uuid_t *u, uint32_t *status)
499a210c53Smiod {
509a210c53Smiod int n;
519a210c53Smiod
529a210c53Smiod /* Short-circuit 2 special cases: NULL pointer and empty string. */
539a210c53Smiod if (s == NULL || *s == '\0') {
549a210c53Smiod uuid_create_nil(u, status);
559a210c53Smiod return;
569a210c53Smiod }
579a210c53Smiod
589a210c53Smiod /* Assume the worst. */
599a210c53Smiod if (status != NULL)
609a210c53Smiod *status = uuid_s_invalid_string_uuid;
619a210c53Smiod
629a210c53Smiod /* The UUID string representation has a fixed length. */
639a210c53Smiod if (strlen(s) != UUID_STR_LEN)
649a210c53Smiod return;
659a210c53Smiod
669a210c53Smiod /*
679a210c53Smiod * We only work with "new" UUIDs. New UUIDs have the form:
689a210c53Smiod * 01234567-89ab-cdef-0123-456789abcdef
699a210c53Smiod * The so called "old" UUIDs, which we don't support, have the form:
709a210c53Smiod * 0123456789ab.cd.ef.01.23.45.67.89.ab
719a210c53Smiod */
72*de5f0841Skrw for (n = 0; n < UUID_STR_LEN; n++) {
73*de5f0841Skrw switch (n) {
74*de5f0841Skrw case 8:
75*de5f0841Skrw case 13:
76*de5f0841Skrw case 18:
77*de5f0841Skrw case 23:
78*de5f0841Skrw if (s[n] != '-')
799a210c53Smiod return;
80*de5f0841Skrw break;
81*de5f0841Skrw default:
82*de5f0841Skrw if (!isxdigit((unsigned char)(s[n])))
83*de5f0841Skrw return;
84*de5f0841Skrw break;
85*de5f0841Skrw }
86*de5f0841Skrw }
879a210c53Smiod
889a210c53Smiod n = sscanf(s,
899a210c53Smiod "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
909a210c53Smiod &u->time_low, &u->time_mid, &u->time_hi_and_version,
919a210c53Smiod &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
929a210c53Smiod &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
939a210c53Smiod
949a210c53Smiod /* Make sure we have all conversions. */
959a210c53Smiod if (n != 11)
969a210c53Smiod return;
979a210c53Smiod
989a210c53Smiod /* We have a successful scan. Check semantics... */
999a210c53Smiod n = u->clock_seq_hi_and_reserved;
1009a210c53Smiod if ((n & 0x80) != 0x00 && /* variant 0? */
1019a210c53Smiod (n & 0xc0) != 0x80 && /* variant 1? */
1029a210c53Smiod (n & 0xe0) != 0xc0) { /* variant 2? */
1039a210c53Smiod if (status != NULL)
1049a210c53Smiod *status = uuid_s_bad_version;
1059a210c53Smiod } else {
1069a210c53Smiod if (status != NULL)
1079a210c53Smiod *status = uuid_s_ok;
1089a210c53Smiod }
1099a210c53Smiod }
110