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