xref: /dflybsd-src/lib/libc/uuid/uuid_to_string.c (revision d7345b10d9624b5849a78c7fa1331ab235da70e9)
1*d7345b10SMatthew Dillon /*-
2*d7345b10SMatthew Dillon  * Copyright (c) 2002,2005 Marcel Moolenaar
3*d7345b10SMatthew Dillon  * Copyright (c) 2002 Hiten Mahesh Pandya
4*d7345b10SMatthew Dillon  * All rights reserved.
5*d7345b10SMatthew Dillon  *
6*d7345b10SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
7*d7345b10SMatthew Dillon  * modification, are permitted provided that the following conditions
8*d7345b10SMatthew Dillon  * are met:
9*d7345b10SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
10*d7345b10SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
11*d7345b10SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
12*d7345b10SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
13*d7345b10SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
14*d7345b10SMatthew Dillon  *
15*d7345b10SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*d7345b10SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*d7345b10SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*d7345b10SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*d7345b10SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*d7345b10SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*d7345b10SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*d7345b10SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*d7345b10SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*d7345b10SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*d7345b10SMatthew Dillon  * SUCH DAMAGE.
26*d7345b10SMatthew Dillon  *
27*d7345b10SMatthew Dillon  * $FreeBSD: src/lib/libc/uuid/uuid_to_string.c,v 1.3 2005/01/03 02:56:15 marcel Exp $
28*d7345b10SMatthew Dillon  * $DragonFly: src/lib/libc/uuid/uuid_to_string.c,v 1.1 2007/06/16 19:57:14 dillon Exp $
29*d7345b10SMatthew Dillon  */
30*d7345b10SMatthew Dillon 
31*d7345b10SMatthew Dillon #include <stdio.h>
32*d7345b10SMatthew Dillon #include <string.h>
33*d7345b10SMatthew Dillon #include <uuid.h>
34*d7345b10SMatthew Dillon 
35*d7345b10SMatthew Dillon /*
36*d7345b10SMatthew Dillon  * uuid_to_string() - Convert a binary UUID into a string representation.
37*d7345b10SMatthew Dillon  * See also:
38*d7345b10SMatthew Dillon  *	http://www.opengroup.org/onlinepubs/009629399/uuid_to_string.htm
39*d7345b10SMatthew Dillon  *
40*d7345b10SMatthew Dillon  * NOTE: The references given above do not have a status code for when
41*d7345b10SMatthew Dillon  *	 the string could not be allocated. The status code has been
42*d7345b10SMatthew Dillon  *	 taken from the Hewlett-Packard implementation.
43*d7345b10SMatthew Dillon  */
44*d7345b10SMatthew Dillon void
45*d7345b10SMatthew Dillon uuid_to_string(const uuid_t *u, char **s, uint32_t *status)
46*d7345b10SMatthew Dillon {
47*d7345b10SMatthew Dillon 	uuid_t nil;
48*d7345b10SMatthew Dillon 
49*d7345b10SMatthew Dillon 	if (status != NULL)
50*d7345b10SMatthew Dillon 		*status = uuid_s_ok;
51*d7345b10SMatthew Dillon 
52*d7345b10SMatthew Dillon 	/* Why allow a NULL-pointer here? */
53*d7345b10SMatthew Dillon 	if (s == 0)
54*d7345b10SMatthew Dillon 		return;
55*d7345b10SMatthew Dillon 
56*d7345b10SMatthew Dillon 	if (u == NULL) {
57*d7345b10SMatthew Dillon 		u = &nil;
58*d7345b10SMatthew Dillon 		uuid_create_nil(&nil, NULL);
59*d7345b10SMatthew Dillon 	}
60*d7345b10SMatthew Dillon 
61*d7345b10SMatthew Dillon 	asprintf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
62*d7345b10SMatthew Dillon 	    u->time_low, u->time_mid, u->time_hi_and_version,
63*d7345b10SMatthew Dillon 	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
64*d7345b10SMatthew Dillon 	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
65*d7345b10SMatthew Dillon 
66*d7345b10SMatthew Dillon 	if (*s == NULL && status != NULL)
67*d7345b10SMatthew Dillon 		*status = uuid_s_no_memory;
68*d7345b10SMatthew Dillon }
69