DCE 1.1 RPC compatible UUID routines for libc, adapted from FreeBSD.

Encode/decode to big/little endian binary blob routines adapted from
kernel.

PR 23470.
This commit is contained in:
thorpej 2004-09-13 21:44:54 +00:00
parent f16bab2269
commit de1dfb1250
17 changed files with 1011 additions and 9 deletions

View File

@ -1,6 +1,6 @@
# $NetBSD: shl.mi,v 1.279 2004/08/08 07:39:34 provos Exp $
# $NetBSD: shl.mi,v 1.280 2004/09/13 21:44:54 thorpej Exp $
# Note: libtermcap and libtermlib are hardlinked and share the same version.
./lib/libc.so.12.122 base-sys-shlib
./lib/libc.so.12.123 base-sys-shlib
./lib/libcrypt.so.0.1 base-sys-shlib
./lib/libcrypto.so.2.1 base-crypto-shlib crypto
./lib/libedit.so.2.9 base-sys-shlib
@ -32,7 +32,7 @@
./usr/lib/libasn1.so.6.1 base-krb5-shlib kerberos
./usr/lib/libbsdmalloc.so.0.0 base-sys-shlib
./usr/lib/libbz2.so.1.0 base-sys-shlib
./usr/lib/libc.so.12.122 base-sys-shlib
./usr/lib/libc.so.12.123 base-sys-shlib
./usr/lib/libcdk.so.1.0 base-sys-shlib
./usr/lib/libcom_err.so.4.1 base-krb5-shlib kerberos
./usr/lib/libcrypt.so.0.1 base-sys-shlib

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.729 2004/09/07 12:28:34 taca Exp $
# $NetBSD: mi,v 1.730 2004/09/13 21:44:54 thorpej Exp $
./etc/mtree/set.comp comp-sys-root
./usr/bin/addr2line comp-debug-bin bfd
./usr/bin/ar comp-util-bin bfd
@ -1461,6 +1461,7 @@
./usr/include/utime.h comp-c-include
./usr/include/utmp.h comp-c-include
./usr/include/utmpx.h comp-c-include
./usr/include/uuid.h comp-c-include
./usr/include/uvm/uvm.h comp-c-include
./usr/include/uvm/uvm_amap.h comp-c-include
./usr/include/uvm/uvm_amap_i.h comp-c-include
@ -4427,6 +4428,19 @@
./usr/share/man/cat3/util.0 comp-c-catman .cat
./usr/share/man/cat3/utime.0 comp-c-catman .cat
./usr/share/man/cat3/utmpxname.0 comp-c-catman .cat
./usr/share/man/cat3/uuid.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_compare.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_create.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_create_nil.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_dec_be.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_dec_le.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_enc_be.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_enc_le.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_equal.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_from_string.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_hash.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_is_nil.0 comp-c-catman .cat
./usr/share/man/cat3/uuid_to_string.0 comp-c-catman .cat
./usr/share/man/cat3/va_arg.0 comp-c-catman .cat
./usr/share/man/cat3/va_copy.0 comp-c-catman .cat
./usr/share/man/cat3/va_end.0 comp-c-catman .cat
@ -8174,6 +8188,19 @@
./usr/share/man/man3/util.3 comp-c-man .man
./usr/share/man/man3/utime.3 comp-c-man .man
./usr/share/man/man3/utmpxname.3 comp-c-man .man
./usr/share/man/man3/uuid.0 comp-c-man .man
./usr/share/man/man3/uuid_compare.0 comp-c-man .man
./usr/share/man/man3/uuid_create.0 comp-c-man .man
./usr/share/man/man3/uuid_create_nil.0 comp-c-man .man
./usr/share/man/man3/uuid_dec_be.0 comp-c-man .man
./usr/share/man/man3/uuid_dec_le.0 comp-c-man .man
./usr/share/man/man3/uuid_enc_be.0 comp-c-man .man
./usr/share/man/man3/uuid_enc_le.0 comp-c-man .man
./usr/share/man/man3/uuid_equal.0 comp-c-man .man
./usr/share/man/man3/uuid_from_string.0 comp-c-man .man
./usr/share/man/man3/uuid_hash.0 comp-c-man .man
./usr/share/man/man3/uuid_is_nil.0 comp-c-man .man
./usr/share/man/man3/uuid_to_string.0 comp-c-man .man
./usr/share/man/man3/va_arg.3 comp-c-man .man
./usr/share/man/man3/va_copy.3 comp-c-man .man
./usr/share/man/man3/va_end.3 comp-c-man .man

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.117 2004/07/13 15:42:03 seb Exp $
# $NetBSD: Makefile,v 1.118 2004/09/13 21:44:54 thorpej Exp $
# @(#)Makefile 8.2 (Berkeley) 1/4/94
# Doing a make includes builds /usr/include
@ -20,7 +20,7 @@ INCS= a.out.h ar.h assert.h bitstring.h bm.h cpio.h ctype.h db.h dirent.h \
sgtty.h signal.h stab.h stddef.h stdio.h stdlib.h string.h \
strings.h stringlist.h struct.h sysexits.h tar.h threadlib.h time.h \
ttyent.h tzfile.h ucontext.h ulimit.h unistd.h util.h utime.h utmp.h \
utmpx.h varargs.h vis.h wchar.h wctype.h wordexp.h
utmpx.h uuid.h varargs.h vis.h wchar.h wctype.h wordexp.h
INCS+= arpa/ftp.h arpa/inet.h arpa/nameser.h arpa/nameser_compat.h \
arpa/telnet.h arpa/tftp.h
INCS+= protocols/dumprestore.h protocols/routed.h protocols/rwhod.h \

60
include/uuid.h Normal file
View File

@ -0,0 +1,60 @@
/* $NetBSD: uuid.h,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/include/uuid.h,v 1.2 2002/11/05 10:55:16 jmallett Exp $
*/
#ifndef _UUID_H_
#define _UUID_H_
#include <sys/types.h>
#include <sys/uuid.h>
/* Status codes returned by the functions. */
#define uuid_s_ok 0
#define uuid_s_bad_version 1
#define uuid_s_invalid_string_uuid 2
#define uuid_s_no_memory 3
__BEGIN_DECLS
int32_t uuid_compare(uuid_t *, uuid_t *, uint32_t *);
void uuid_create(uuid_t *, uint32_t *);
void uuid_create_nil(uuid_t *, uint32_t *);
int32_t uuid_equal(uuid_t *, uuid_t *, uint32_t *);
void uuid_from_string(const char *, uuid_t *, uint32_t *);
uint16_t uuid_hash(uuid_t *, uint32_t *);
int32_t uuid_is_nil(uuid_t *, uint32_t *);
void uuid_to_string(uuid_t *, char **, uint32_t *);
void uuid_enc_le(void *, const uuid_t *);
void uuid_dec_le(const void *, uuid_t *);
void uuid_enc_be(void *, const uuid_t *);
void uuid_dec_be(const void *, uuid_t *);
__END_DECLS
#endif /* _UUID_H_ */

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.118 2004/07/31 14:13:05 lukem Exp $
# $NetBSD: Makefile,v 1.119 2004/09/13 21:44:54 thorpej Exp $
# @(#)Makefile 8.2 (Berkeley) 2/3/94
#
# All library objects contain sccsid strings by default; they may be
@ -110,6 +110,7 @@ assym.h: genassym.sh ${ARCHDIR}/genassym.cf
.include "${.CURDIR}/thread-stub/Makefile.inc"
.include "${.CURDIR}/time/Makefile.inc"
.include "${.CURDIR}/sys/Makefile.inc"
.include "${.CURDIR}/uuid/Makefile.inc"
.if (${MKYP} != "no")
.include "${.CURDIR}/yp/Makefile.inc"
.endif

View File

@ -1,4 +1,4 @@
# $NetBSD: shlib_version,v 1.157 2004/07/13 15:45:18 seb Exp $
# $NetBSD: shlib_version,v 1.158 2004/09/13 21:44:54 thorpej Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing
#
# things we wish to do on next major version bump:
@ -17,4 +17,4 @@
# - libc/net/getnet{ent,namadr}.c, netdb.h: remove __n_pad0
#
major=12
minor=122
minor=123

View File

@ -0,0 +1,25 @@
# $NetBSD: Makefile.inc,v 1.1 2004/09/13 21:44:54 thorpej Exp $
# DCE 1.1 compatible UUID implementation sources
.PATH: ${.CURDIR}/uuid
SRCS+= uuid_compare.c uuid_create.c uuid_create_nil.c uuid_equal.c \
uuid_from_string.c uuid_hash.c uuid_is_nil.c uuid_stream.c \
uuid_to_string.c
MAN+= uuid.3
MLINKS+=uuid.3 uuid_compare.3
MLINKS+=uuid.3 uuid_create.3
MLINKS+=uuid.3 uuid_create_nil.3
MLINKS+=uuid.3 uuid_equal.3
MLINKS+=uuid.3 uuid_from_string.3
MLINKS+=uuid.3 uuid_hash.3
MLINKS+=uuid.3 uuid_is_nil.3
MLINKS+=uuid.3 uuid_to_string.3
MLINKS+=uuid.3 uuid_enc_le.3
MLINKS+=uuid.3 uuid_dec_le.3
MLINKS+=uuid.3 uuid_enc_be.3
MLINKS+=uuid.3 uuid_dec_be.3

214
lib/libc/uuid/uuid.3 Normal file
View File

@ -0,0 +1,214 @@
.\" $NetBSD: uuid.3,v 1.1 2004/09/13 21:44:54 thorpej Exp $
.\"
.\" Copyright (c) 2004 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\" Copyright (c) 2002 Marcel Moolenaar
.\" Copyright (c) 2002 Hiten Mahesh Pandya
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD: src/lib/libc/uuid/uuid.3,v 1.4 2003/08/08 19:12:28 marcel Exp $
.\"
.Dd September 12, 2004
.Dt UUID 3
.Os
.Sh NAME
.Nm uuid_compare , uuid_create , uuid_create_nil , uuid_equal ,
.Nm uuid_from_string , uuid_hash , uuid_is_nil , uuid_to_string ,
.Nm uuid_enc_le , uuid_dec_le , uuid_enc_be , uuid_dec_be
.Nd Universally Unique Identifier routines
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In uuid.h
.Ft int32_t
.Fn uuid_compare "uuid_t *uuid1" "uuid_t *uuid2" "uint32_t *status"
.Ft void
.Fn uuid_create "uuid_t *uuid" "uint32_t *status"
.Ft void
.Fn uuid_create_nil "uuid_t *uuid" "uint32_t *status"
.Ft int32_t
.Fn uuid_equal "uuid_t *uuid1" "uuid_t *uuid2" "uint32_t *status"
.Ft void
.Fn uuid_from_string "const char *str" "uuid_t *uuid" "uint32_t *status"
.Ft uint16_t
.Fn uuid_hash "uuid_t *uuid" "uint32_t *status"
.Ft int32_t
.Fn uuid_is_nil "uuid_t *uuid" "uint32_t *status"
.Ft void
.Fn uuid_to_string "uuid_t *uuid" "char **str" "uint32_t *status"
.Ft void
.Fn uuid_enc_le "void *buf" "const uuid_t *uuid"
.Ft void
.Fn uuid_dec_le "const void *buf" "uuid_t *"
.Ft void
.Fn uuid_enc_be "void *buf" "const uuid_t *uuid"
.Ft void
.Fn uuid_dec_be "const void *buf" "uuid_t *"
.Sh DESCRIPTION
These routines provide for the creation and manipulation of Universally
Unique Identifiers
.Pq UUIDs ,
also referred to as Globally Unique Identifiers
.Pq GUIDs .
.Pp
The
.Fn uuid_compare
function compares two UUIDs. It returns -1 if
.Fa uuid1
preceeds
.Fa uuid2 ,
0 if they are equal, or 1 if
.Fa uuid1
follows
.Fa uuid2 .
.Pp
The
.Fn uuid_create
function creates a new UUID.
Storage for the new UUID must be pre-allocated by the caller.
.Pp
The
.Fn uuid_create_nil
function creates a nil-valued UUID.
Storage for the new UUID must be pre-allocated by the caller.
.Pp
The
.Fn uuid_equal
function compares two UUIDs to determine if they are equal.
It returns 1 if they are equal, and 0 if they are not equal.
.Pp
The
.Fn uuid_from_string
function parses a 36-character string representation of a UUID and
converts it to binary representation.
Storage for the UUID must be pre-allocated by the caller.
.Pp
The
.Fn uuid_hash
function generates a hash value for the specified UUID.
Note that the hash value is not a cryptographic hash, and should not be
assumed to be unique given two different UUIDs.
.Pp
The
.Fn uuid_is_nil
function returns 1 if the UUID is nil-valued and 0 if it is not.
.Pp
The
.Fn uuid_to_string
function converts a UUID from binary representation to string representation.
Storage for the string is dynamically allocated and returned via the
.Fa str
argument.
This poinger should be passed to
.Xr free 3
to release the allocated storage when it is no longer needed.
.Pp
The
.Fn uuid_enc_le
and
.Fn uuid_enc_be
functions encode a binary representation of a UUID into an octet stream
in little-endian and big-endian byte-order, respectively.
The destination buffer must be pre-allocated by the caller, and must be
large enough to hold the 16-octet binary UUID.
.Pp
The
.Fn uuid_dec_le
and
.Fn uuid_dec_be
functions decode a UUID from an octet stream in little-endian and
big-endian byte-order, respectively.
.Sh RETURN VALUES
The
.Fn uuid_compare ,
.Fn uuid_create ,
.Fn uuid_create_nil ,
.Fn uuid_equal ,
.Fn uuid_from_string ,
.Fn uuid_hash ,
.Fn uuid_is_nil ,
and
.Fn uuid_to_string
functions return successful or unsuccessful completion status in the
.Fa status
argument.
Possible values are:
.Pp
.Bl -tag -width ".Dv uuid_s_invalid_string_uuid"
.It Dv uuid_s_ok
The function completed successfully.
.It Dv uuid_s_bad_version
The UUID does not have a known version.
.It Dv uuid_s_invalid_string_uuid
The string representation of an UUID is not valid.
.It Dv uuid_s_no_memory
Memory could not be allocated for the operation.
.El
.Sh SEE ALSO
.Xr uuidgen 1 ,
.Xr uuidgen 2
.Sh STANDARDS
The
.Fn uuid_compare ,
.Fn uuid_create ,
.Fn uuid_create_nil ,
.Fn uuid_equal ,
.Fn uuid_from_string ,
.Fn uuid_hash ,
.Fn uuid_is_nil ,
and
.Fn uuid_to_string
functions are compatible with the DCE 1.1 RPC specification.

View File

@ -0,0 +1,87 @@
/* $NetBSD: uuid_compare.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_compare.c,v 1.3 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_compare.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <string.h>
#include <uuid.h>
/*
* uuid_compare() - compare two UUIDs.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_compare.htm
*
* NOTE: Either UUID can be NULL, meaning a nil UUID. nil UUIDs are smaller
* than any non-nil UUID.
*/
int32_t
uuid_compare(uuid_t *a, uuid_t *b, uint32_t *status)
{
int res;
if (status != NULL)
*status = uuid_s_ok;
/* Deal with NULL or equal pointers. */
if (a == b)
return (0);
if (a == NULL)
return ((uuid_is_nil(b, NULL)) ? 0 : -1);
if (b == NULL)
return ((uuid_is_nil(a, NULL)) ? 0 : 1);
/* We have to compare the hard way. */
res = (int)((int64_t)a->time_low - (int64_t)b->time_low);
if (res)
return ((res < 0) ? -1 : 1);
res = (int)a->time_mid - (int)b->time_mid;
if (res)
return ((res < 0) ? -1 : 1);
res = (int)a->time_hi_and_version - (int)b->time_hi_and_version;
if (res)
return ((res < 0) ? -1 : 1);
res = (int)a->clock_seq_hi_and_reserved -
(int)b->clock_seq_hi_and_reserved;
if (res)
return ((res < 0) ? -1 : 1);
res = (int)a->clock_seq_low - (int)b->clock_seq_low;
if (res)
return ((res < 0) ? -1 : 1);
res = memcmp(a->node, b->node, sizeof(a->node));
if (res)
return ((res < 0) ? -1 : 1);
return (0);
}

View File

@ -0,0 +1,58 @@
/* $NetBSD: uuid_create.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_create.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_create.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <uuid.h>
/*
* uuid_create() - create an UUID.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_create.htm
*/
void
uuid_create(uuid_t *u, uint32_t *status)
{
uint32_t s;
if (uuidgen(u, 1) == -1)
s = uuid_s_no_memory; /* XXX */
else
s = uuid_s_ok;
if (status)
*status = s;
}

View File

@ -0,0 +1,55 @@
/* $NetBSD: uuid_create_nil.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_create_nil.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_create_nil.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <string.h>
#include <uuid.h>
/*
* uuid_create_nil() - create a nil UUID.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_create_nil.htm
*/
void
uuid_create_nil(uuid_t *u, uint32_t *status)
{
memset(u, 0, sizeof(*u));
if (status)
*status = uuid_s_ok;
}

View File

@ -0,0 +1,64 @@
/* $NetBSD: uuid_equal.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_equal.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_equal.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <string.h>
#include <uuid.h>
/*
* uuid_equal() - compare for equality.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_equal.htm
*/
int32_t
uuid_equal(uuid_t *a, uuid_t *b, uint32_t *status)
{
if (status != NULL)
*status = uuid_s_ok;
/* Deal with equal or NULL pointers. */
if (a == b)
return (1);
if (a == NULL)
return (uuid_is_nil(b, NULL));
if (b == NULL)
return (uuid_is_nil(a, NULL));
/* Do a byte for byte comparison. */
return ((memcmp(a, b, sizeof(uuid_t))) ? 0 : 1);
}

View File

@ -0,0 +1,101 @@
/* $NetBSD: uuid_from_string.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_from_string.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_from_string.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <stdio.h>
#include <string.h>
#include <uuid.h>
/*
* uuid_from_string() - convert a string representation of an UUID into
* a binary representation.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm
*
* NOTE: The sequence field is in big-endian, while the time fields are in
* native byte order.
*/
void
uuid_from_string(const char *s, uuid_t *u, uint32_t *status)
{
int n;
/* Short-circuit 2 special cases: NULL pointer and empty string. */
if (s == NULL || *s == '\0') {
uuid_create_nil(u, status);
return;
}
/* Assume the worst. */
if (status != NULL)
*status = uuid_s_invalid_string_uuid;
/* The UUID string representation has a fixed length. */
if (strlen(s) != 36)
return;
/*
* We only work with "new" UUIDs. New UUIDs have the form:
* 01234567-89ab-cdef-0123-456789abcdef
* The so called "old" UUIDs, which we don't support, have the form:
* 0123456789ab.cd.ef.01.23.45.67.89.ab
*/
if (s[8] != '-')
return;
n = sscanf(s,
"%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
&u->time_low, &u->time_mid, &u->time_hi_and_version,
&u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
&u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
/* Make sure we have all conversions. */
if (n != 11)
return;
/* We have a successful scan. Check semantics... */
n = u->clock_seq_hi_and_reserved;
if ((n & 0x80) != 0x00 && /* variant 0? */
(n & 0xc0) != 0x80 && /* variant 1? */
(n & 0xe0) != 0xc0) { /* variant 2? */
if (status != NULL)
*status = uuid_s_bad_version;
} else {
if (status != NULL)
*status = uuid_s_ok;
}
}

58
lib/libc/uuid/uuid_hash.c Normal file
View File

@ -0,0 +1,58 @@
/* $NetBSD: uuid_hash.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_hash.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_hash.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <uuid.h>
/*
* uuid_hash() - generate a hash value.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_hash.htm
*/
uint16_t
uuid_hash(uuid_t *u, uint32_t *status)
{
if (status)
*status = uuid_s_ok;
/*
* Use the most frequently changing bits in the UUID as the hash
* value. This should yield a good enough distribution...
*/
return ((u) ? u->time_low & 0xffff : 0);
}

View File

@ -0,0 +1,59 @@
/* $NetBSD: uuid_is_nil.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_is_nil.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_is_nil.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <string.h>
#include <uuid.h>
/*
* uuid_is_nil() - return whether the UUID is a nil UUID.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_is_nil.htm
*/
int32_t
uuid_is_nil(uuid_t *u, uint32_t *status)
{
static const uuid_t nil = { 0 };
if (status)
*status = uuid_s_ok;
if (!u)
return (1);
return (memcmp(u, &nil, sizeof(uuid_t)) == 0 ? 1 : 0);
}

117
lib/libc/uuid/uuid_stream.c Normal file
View File

@ -0,0 +1,117 @@
/* $NetBSD: uuid_stream.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*
* Copyright (c) 2002 Marcel Moolenaar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_stream.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <sys/endian.h>
#include <uuid.h>
/*
* Encode/Decode UUID into octet-stream.
* http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | time_low |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | time_mid | time_hi_and_version |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |clk_seq_hi_res | clk_seq_low | node (0-1) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | node (2-5) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* NOTE: These routines are not part of the DCE RPC API. There are
* provided for convenience.
*/
void
uuid_enc_le(void *buf, const uuid_t *uuid)
{
uint8_t *p = buf;
int i;
le32enc(p, uuid->time_low);
le16enc(p + 4, uuid->time_mid);
le16enc(p + 6, uuid->time_hi_and_version);
p[8] = uuid->clock_seq_hi_and_reserved;
p[9] = uuid->clock_seq_low;
for (i = 0; i < _UUID_NODE_LEN; i++)
p[10 + i] = uuid->node[i];
}
void
uuid_dec_le(const void *buf, uuid_t *uuid)
{
const uint8_t *p = buf;
int i;
uuid->time_low = le32dec(p);
uuid->time_mid = le16dec(p + 4);
uuid->time_hi_and_version = le16dec(p + 6);
uuid->clock_seq_hi_and_reserved = p[8];
uuid->clock_seq_low = p[9];
for (i = 0; i < _UUID_NODE_LEN; i++)
uuid->node[i] = p[10 + i];
}
void
uuid_enc_be(void *buf, const uuid_t *uuid)
{
uint8_t *p = buf;
int i;
be32enc(p, uuid->time_low);
be16enc(p + 4, uuid->time_mid);
be16enc(p + 6, uuid->time_hi_and_version);
p[8] = uuid->clock_seq_hi_and_reserved;
p[9] = uuid->clock_seq_low;
for (i = 0; i < _UUID_NODE_LEN; i++)
p[10 + i] = uuid->node[i];
}
void
uuid_dec_be(const void *buf, uuid_t *uuid)
{
const uint8_t *p = buf;
int i;
uuid->time_low = be32dec(p);
uuid->time_mid = le16dec(p + 4);
uuid->time_hi_and_version = be16dec(p + 6);
uuid->clock_seq_hi_and_reserved = p[8];
uuid->clock_seq_low = p[9];
for (i = 0; i < _UUID_NODE_LEN; i++)
uuid->node[i] = p[10 + i];
}

View File

@ -0,0 +1,76 @@
/* $NetBSD: uuid_to_string.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
* Copyright (c) 2002 Hiten Mahesh Pandya
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libc/uuid/uuid_to_string.c,v 1.2 2003/08/08 19:18:43 marcel Exp $
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: uuid_to_string.c,v 1.1 2004/09/13 21:44:54 thorpej Exp $");
#endif
#include "namespace.h"
#include <stdio.h>
#include <string.h>
#include <uuid.h>
/*
* uuid_to_string() - Convert a binary UUID into a string representation.
* See also:
* http://www.opengroup.org/onlinepubs/009629399/uuid_to_string.htm
*
* NOTE: The references given above do not have a status code for when
* the string could not be allocated. The status code has been
* taken from the Hewlett-Packard implementation.
*/
void
uuid_to_string(uuid_t *u, char **s, uint32_t *status)
{
uuid_t nil;
if (status != NULL)
*status = uuid_s_ok;
/* Why allow a NULL-pointer here? */
if (s == 0)
return;
if (u == NULL) {
u = &nil;
uuid_create_nil(u, NULL);
}
asprintf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
u->time_low, u->time_mid, u->time_hi_and_version,
u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
if (*s == NULL && status != NULL)
*status = uuid_s_no_memory;
}