xref: /freebsd/stand/libsa/uuid_to_string.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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  * $FreeBSD$
26  */
27 
28 
29 /*
30  * Note: some comments taken from lib/libc/uuid/uuid_to_string.c
31  * Copyright (c) 2002,2005 Marcel Moolenaar
32  * Copyright (c) 2002 Hiten Mahesh Pandya
33  */
34 
35 #include <stand.h>
36 #include <uuid.h>
37 
38 /*
39  * Dump len characters into *buf from val as hex and update *buf
40  */
41 static void
42 tohex(char **buf, int len, uint32_t val)
43 {
44 	static const char *hexstr = "0123456789abcdef";
45 	char *walker = *buf;
46 	int i;
47 
48 	for (i = len - 1; i >= 0; i--) {
49 		walker[i] = hexstr[val & 0xf];
50 		val >>= 4;
51 	}
52 	*buf = walker + len;
53 }
54 
55 /*
56  * uuid_to_string() - Convert a binary UUID into a string representation.
57  * See also:
58  *	http://www.opengroup.org/onlinepubs/009629399/uuid_to_string.htm
59  *
60  * NOTE: The references given above do not have a status code for when
61  *	 the string could not be allocated. The status code has been
62  *	 taken from the Hewlett-Packard implementation.
63  *
64  * NOTE: we don't support u == NULL for a nil UUID, sorry.
65  *
66  * NOTE: The sequence field is in big-endian, while the time fields are in
67  *	 native byte order.
68  *
69  *	 hhhhhhhh-hhhh-hhhh-bbbb-bbbbbbbbbbbb
70  *	 01234567-89ab-cdef-0123-456789abcdef
71  */
72 void
73 uuid_to_string(const uuid_t *u, char **s, uint32_t *status)
74 {
75 	uuid_t nil;
76 	char *w;
77 
78 	if (status != NULL)
79 		*status = uuid_s_ok;
80 	if (s == NULL)	/* Regular version does this odd-ball behavior too */
81 		return;
82 	w = *s = malloc(37);
83 	if (*s == NULL) {
84 		if (status != NULL)
85 			*status = uuid_s_no_memory;
86 		return;
87 	}
88 	if (u == NULL) {
89 		u = &nil;
90 		uuid_create_nil(&nil, NULL);
91 	}
92 	/* native */
93 	tohex(&w, 8, u->time_low);
94 	*w++ = '-';
95 	tohex(&w, 4, u->time_mid);
96 	*w++ = '-';
97 	tohex(&w, 4, u->time_hi_and_version);
98 	*w++ = '-';
99 	/* Big endian, so do a byte at a time */
100 	tohex(&w, 2, u->clock_seq_hi_and_reserved);
101 	tohex(&w, 2, u->clock_seq_low);
102 	*w++ = '-';
103 	tohex(&w, 2, u->node[0]);
104 	tohex(&w, 2, u->node[1]);
105 	tohex(&w, 2, u->node[2]);
106 	tohex(&w, 2, u->node[3]);
107 	tohex(&w, 2, u->node[4]);
108 	tohex(&w, 2, u->node[5]);
109 	*w++ = '\0';
110 }
111