xref: /freebsd-src/lib/libc/uuid/uuid_to_string.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
165393a86SMarcel Moolenaar /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
41d1b40feSMarcel Moolenaar  * Copyright (c) 2002,2005 Marcel Moolenaar
565393a86SMarcel Moolenaar  * Copyright (c) 2002 Hiten Mahesh Pandya
665393a86SMarcel Moolenaar  * All rights reserved.
765393a86SMarcel Moolenaar  *
865393a86SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
965393a86SMarcel Moolenaar  * modification, are permitted provided that the following conditions
1065393a86SMarcel Moolenaar  * are met:
1165393a86SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1265393a86SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1365393a86SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1465393a86SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1565393a86SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1665393a86SMarcel Moolenaar  *
1765393a86SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1865393a86SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1965393a86SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2065393a86SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2165393a86SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2265393a86SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2365393a86SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2465393a86SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2565393a86SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2665393a86SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2765393a86SMarcel Moolenaar  * SUCH DAMAGE.
2865393a86SMarcel Moolenaar  */
2965393a86SMarcel Moolenaar 
3065393a86SMarcel Moolenaar #include <stdio.h>
3165393a86SMarcel Moolenaar #include <string.h>
3265393a86SMarcel Moolenaar #include <uuid.h>
3365393a86SMarcel Moolenaar 
3465393a86SMarcel Moolenaar /*
3565393a86SMarcel Moolenaar  * uuid_to_string() - Convert a binary UUID into a string representation.
3665393a86SMarcel Moolenaar  * See also:
3765393a86SMarcel Moolenaar  *	http://www.opengroup.org/onlinepubs/009629399/uuid_to_string.htm
3865393a86SMarcel Moolenaar  *
3965393a86SMarcel Moolenaar  * NOTE: The references given above do not have a status code for when
4065393a86SMarcel Moolenaar  *	 the string could not be allocated. The status code has been
4165393a86SMarcel Moolenaar  *	 taken from the Hewlett-Packard implementation.
4265393a86SMarcel Moolenaar  */
4365393a86SMarcel Moolenaar void
uuid_to_string(const uuid_t * u,char ** s,uint32_t * status)441d1b40feSMarcel Moolenaar uuid_to_string(const uuid_t *u, char **s, uint32_t *status)
4565393a86SMarcel Moolenaar {
4665393a86SMarcel Moolenaar 	uuid_t nil;
4765393a86SMarcel Moolenaar 
4865393a86SMarcel Moolenaar 	if (status != NULL)
4965393a86SMarcel Moolenaar 		*status = uuid_s_ok;
5065393a86SMarcel Moolenaar 
5165393a86SMarcel Moolenaar 	/* Why allow a NULL-pointer here? */
52513004a2SPedro F. Giffuni 	if (s == NULL)
5365393a86SMarcel Moolenaar 		return;
5465393a86SMarcel Moolenaar 
5565393a86SMarcel Moolenaar 	if (u == NULL) {
5665393a86SMarcel Moolenaar 		u = &nil;
571d1b40feSMarcel Moolenaar 		uuid_create_nil(&nil, NULL);
5865393a86SMarcel Moolenaar 	}
5965393a86SMarcel Moolenaar 
6065393a86SMarcel Moolenaar 	asprintf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
6165393a86SMarcel Moolenaar 	    u->time_low, u->time_mid, u->time_hi_and_version,
6265393a86SMarcel Moolenaar 	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
6365393a86SMarcel Moolenaar 	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
6465393a86SMarcel Moolenaar 
6565393a86SMarcel Moolenaar 	if (*s == NULL && status != NULL)
6665393a86SMarcel Moolenaar 		*status = uuid_s_no_memory;
6765393a86SMarcel Moolenaar }
68