2 * Copyright (c) 2015 M. Warner Losh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
40 * Dump len characters into *buf from val as hex and update *buf
43 tohex(char **buf
, int len
, uint32_t val
)
45 static const char *hexstr
= "0123456789abcdef";
49 for (i
= len
- 1; i
>= 0; i
--) {
50 walker
[i
] = hexstr
[val
& 0xf];
57 * uuid_to_string() - Convert a binary UUID into a string representation.
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
70 * hhhhhhhh-hhhh-hhhh-bbbb-bbbbbbbbbbbb
71 * 01234567-89ab-cdef-0123-456789abcdef
74 uuid_to_string(const uuid_t
*u
, char **s
, uint32_t *status
)
81 if (s
== NULL
) /* Regular version does this odd-ball behavior too */
86 *status
= uuid_s_no_memory
;
91 uuid_create_nil(&nil
, NULL
);
94 tohex(&w
, 8, u
->time_low
);
96 tohex(&w
, 4, u
->time_mid
);
98 tohex(&w
, 4, u
->time_hi_and_version
);
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
);
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]);