xref: /freebsd/stand/libsa/uuid_to_string.c (revision 05248206f720394d95c2a7475429311df670a2e9)
1 /*-
2  * Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 
27 /*
28  * Note: some comments taken from lib/libc/uuid/uuid_to_string.c
29  * Copyright (c) 2002,2005 Marcel Moolenaar
30  * Copyright (c) 2002 Hiten Mahesh Pandya
31  */
32 
33 #include <stand.h>
34 #include <uuid.h>
35 
36 /*
37  * Dump len characters into *buf from val as hex and update *buf
38  */
39 static void
40 tohex(char **buf, int len, uint32_t val)
41 {
42 	static const char *hexstr = "0123456789abcdef";
43 	char *walker = *buf;
44 	int i;
45 
46 	for (i = len - 1; i >= 0; i--) {
47 		walker[i] = hexstr[val & 0xf];
48 		val >>= 4;
49 	}
50 	*buf = walker + len;
51 }
52 
53 /*
54  * uuid_to_string() - Convert a binary UUID into a string representation.
55  * See also:
56  *	http://www.opengroup.org/onlinepubs/009629399/uuid_to_string.htm
57  *
58  * NOTE: The references given above do not have a status code for when
59  *	 the string could not be allocated. The status code has been
60  *	 taken from the Hewlett-Packard implementation.
61  *
62  * NOTE: we don't support u == NULL for a nil UUID, sorry.
63  *
64  * NOTE: The sequence field is in big-endian, while the time fields are in
65  *	 native byte order.
66  *
67  *	 hhhhhhhh-hhhh-hhhh-bbbb-bbbbbbbbbbbb
68  *	 01234567-89ab-cdef-0123-456789abcdef
69  */
70 void
71 uuid_to_string(const uuid_t *u, char **s, uint32_t *status)
72 {
73 	uuid_t nil;
74 	char *w;
75 
76 	if (status != NULL)
77 		*status = uuid_s_ok;
78 	if (s == NULL)	/* Regular version does this odd-ball behavior too */
79 		return;
80 	w = *s = malloc(37);
81 	if (*s == NULL) {
82 		if (status != NULL)
83 			*status = uuid_s_no_memory;
84 		return;
85 	}
86 	if (u == NULL) {
87 		u = &nil;
88 		uuid_create_nil(&nil, NULL);
89 	}
90 	/* native */
91 	tohex(&w, 8, u->time_low);
92 	*w++ = '-';
93 	tohex(&w, 4, u->time_mid);
94 	*w++ = '-';
95 	tohex(&w, 4, u->time_hi_and_version);
96 	*w++ = '-';
97 	/* Big endian, so do a byte at a time */
98 	tohex(&w, 2, u->clock_seq_hi_and_reserved);
99 	tohex(&w, 2, u->clock_seq_low);
100 	*w++ = '-';
101 	tohex(&w, 2, u->node[0]);
102 	tohex(&w, 2, u->node[1]);
103 	tohex(&w, 2, u->node[2]);
104 	tohex(&w, 2, u->node[3]);
105 	tohex(&w, 2, u->node[4]);
106 	tohex(&w, 2, u->node[5]);
107 	*w++ = '\0';
108 }
109