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