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