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