diff options
author | Miod Vallat <miod@cvs.openbsd.org> | 2014-08-31 09:36:40 +0000 |
---|---|---|
committer | Miod Vallat <miod@cvs.openbsd.org> | 2014-08-31 09:36:40 +0000 |
commit | 7a7b2be775e7fe08477ec7c07d1acafdf65f4309 (patch) | |
tree | a163fc0b20e86ffed17f519324ecfef85795af93 /lib/libc/uuid | |
parent | 05a80fe488697e276beca25d10b401c6b1a484d8 (diff) |
Add uuid support routines to libc. From FreeBSD via NetBSD via Bitrig via
Markus Mueller.
Diffstat (limited to 'lib/libc/uuid')
-rw-r--r-- | lib/libc/uuid/Makefile.inc | 26 | ||||
-rw-r--r-- | lib/libc/uuid/uuid.3 | 206 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_compare.c | 83 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_create.c | 61 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_create_nil.c | 50 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_equal.c | 59 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_from_string.c | 97 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_hash.c | 53 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_is_nil.c | 55 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_stream.c | 114 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_to_string.c | 71 |
11 files changed, 875 insertions, 0 deletions
diff --git a/lib/libc/uuid/Makefile.inc b/lib/libc/uuid/Makefile.inc new file mode 100644 index 00000000000..a66d2a833c0 --- /dev/null +++ b/lib/libc/uuid/Makefile.inc @@ -0,0 +1,26 @@ +# $OpenBSD: Makefile.inc,v 1.1 2014/08/31 09:36:39 miod Exp $ +# $NetBSD: Makefile.inc,v 1.1 2004/09/13 21:44:54 thorpej Exp $ + +# DCE 1.1 compatible UUID implementation sources + +.PATH: ${LIBCSRCDIR}/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 diff --git a/lib/libc/uuid/uuid.3 b/lib/libc/uuid/uuid.3 new file mode 100644 index 00000000000..ea4d5f776a6 --- /dev/null +++ b/lib/libc/uuid/uuid.3 @@ -0,0 +1,206 @@ +.\" $OpenBSD: uuid.3,v 1.1 2014/08/31 09:36:39 miod Exp $ +.\" $NetBSD: uuid.3,v 1.7 2008/05/02 18:11:05 martin 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. +.\" +.\" 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 $Mdocdate: August 31 2014 $ +.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 SYNOPSIS +.In uuid.h +.Ft int32_t +.Fn uuid_compare "const uuid_t *uuid1" "const 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 "const uuid_t *uuid1" "const 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 "const uuid_t *uuid" "uint32_t *status" +.Ft int32_t +.Fn uuid_is_nil "const uuid_t *uuid" "uint32_t *status" +.Ft void +.Fn uuid_to_string "const 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 +precedes +.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 pointer 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 a 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 +.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. diff --git a/lib/libc/uuid/uuid_compare.c b/lib/libc/uuid/uuid_compare.c new file mode 100644 index 00000000000..ad03c4dc5e8 --- /dev/null +++ b/lib/libc/uuid/uuid_compare.c @@ -0,0 +1,83 @@ +/* $OpenBSD: uuid_compare.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_compare.c,v 1.2 2008/04/23 07:52:32 plunky 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 "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(const uuid_t *a, const 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); +} diff --git a/lib/libc/uuid/uuid_create.c b/lib/libc/uuid/uuid_create.c new file mode 100644 index 00000000000..ec255589057 --- /dev/null +++ b/lib/libc/uuid/uuid_create.c @@ -0,0 +1,61 @@ +/* $OpenBSD: uuid_create.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $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 "namespace.h" + +#include <stdlib.h> +#include <uuid.h> + +/* + * uuid_create() - create an UUID. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_create.htm and + * RFC 4122 + * + * Create a UUID from random number as defined in section 4.4 of RFC 4122 + */ +void +uuid_create(uuid_t *u, uint32_t *status) +{ + arc4random_buf(u, sizeof(uuid_t)); + + u->clock_seq_hi_and_reserved &= ~(1 << 6); + u->clock_seq_hi_and_reserved |= (1 << 7); + + u->time_hi_and_version &= ~(1 << 12); + u->time_hi_and_version &= ~(1 << 13); + u->time_hi_and_version |= (1 << 14); + u->time_hi_and_version &= ~(1 << 15); + + if (status) + *status = uuid_s_ok; +} diff --git a/lib/libc/uuid/uuid_create_nil.c b/lib/libc/uuid/uuid_create_nil.c new file mode 100644 index 00000000000..caaaba5380b --- /dev/null +++ b/lib/libc/uuid/uuid_create_nil.c @@ -0,0 +1,50 @@ +/* $OpenBSD: uuid_create_nil.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_create_nil.c,v 1.2 2005/02/09 21:35:47 kleink 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 "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; +} diff --git a/lib/libc/uuid/uuid_equal.c b/lib/libc/uuid/uuid_equal.c new file mode 100644 index 00000000000..2a853edd20d --- /dev/null +++ b/lib/libc/uuid/uuid_equal.c @@ -0,0 +1,59 @@ +/* $OpenBSD: uuid_equal.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_equal.c,v 1.2 2008/04/23 07:52:32 plunky 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 "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(const uuid_t *a, const 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); +} diff --git a/lib/libc/uuid/uuid_from_string.c b/lib/libc/uuid/uuid_from_string.c new file mode 100644 index 00000000000..d9fe94d5f75 --- /dev/null +++ b/lib/libc/uuid/uuid_from_string.c @@ -0,0 +1,97 @@ +/* $OpenBSD: uuid_from_string.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $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 "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) != UUID_STR_LEN) + 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; + } +} diff --git a/lib/libc/uuid/uuid_hash.c b/lib/libc/uuid/uuid_hash.c new file mode 100644 index 00000000000..6036f6c481a --- /dev/null +++ b/lib/libc/uuid/uuid_hash.c @@ -0,0 +1,53 @@ +/* $OpenBSD: uuid_hash.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_hash.c,v 1.2 2008/04/23 07:52:32 plunky 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 "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(const 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); +} diff --git a/lib/libc/uuid/uuid_is_nil.c b/lib/libc/uuid/uuid_is_nil.c new file mode 100644 index 00000000000..4de961233fd --- /dev/null +++ b/lib/libc/uuid/uuid_is_nil.c @@ -0,0 +1,55 @@ +/* $OpenBSD: uuid_is_nil.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_is_nil.c,v 1.4 2008/04/23 07:52:32 plunky 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 "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(const uuid_t *u, uint32_t *status) +{ + static const uuid_t nil = { .time_low = 0 }; + + if (status) + *status = uuid_s_ok; + + if (!u) + return (1); + + return (memcmp(u, &nil, sizeof(uuid_t)) == 0 ? 1 : 0); +} diff --git a/lib/libc/uuid/uuid_stream.c b/lib/libc/uuid/uuid_stream.c new file mode 100644 index 00000000000..c22b9e850cb --- /dev/null +++ b/lib/libc/uuid/uuid_stream.c @@ -0,0 +1,114 @@ +/* $OpenBSD: uuid_stream.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky 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 "namespace.h" + +#include <sys/types.h> +#include <machine/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 struct uuid *uuid) +{ + uint8_t *p = buf; + int i; + + p[0] = htole32(uuid->time_low); + p[4] = htole16(uuid->time_mid); + p[6] = htole16(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(void const *buf, struct uuid *uuid) +{ + const uint8_t *p = buf; + int i; + + uuid->time_low = le32toh(*(uint32_t*)p); + uuid->time_mid = le16toh(*(uint16_t*)(p + 4)); + uuid->time_hi_and_version = le16toh(*(uint16_t*)(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 struct uuid *uuid) +{ + uint8_t *p = buf; + int i; + + p[0] = htobe32(uuid->time_low); + p[4] = htobe16(uuid->time_mid); + p[6] = htobe16(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(void const *buf, struct uuid *uuid) +{ + const uint8_t *p = buf; + int i; + + uuid->time_low = be32toh(*(uint32_t*)p); + uuid->time_mid = be16toh(*(uint16_t*)(p + 4)); + uuid->time_hi_and_version = be16toh(*(uint16_t*)(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]; +} diff --git a/lib/libc/uuid/uuid_to_string.c b/lib/libc/uuid/uuid_to_string.c new file mode 100644 index 00000000000..8839fa44e2f --- /dev/null +++ b/lib/libc/uuid/uuid_to_string.c @@ -0,0 +1,71 @@ +/* $OpenBSD: uuid_to_string.c,v 1.1 2014/08/31 09:36:39 miod Exp $ */ +/* $NetBSD: uuid_to_string.c,v 1.2 2008/04/23 07:52:32 plunky 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 "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(const uuid_t *u, char **s, uint32_t *status) +{ + int c; + static const uuid_t nil = { .time_low = 0 }; + + if (status != NULL) + *status = uuid_s_ok; + + /* Why allow a NULL-pointer here? */ + if (s == NULL) + return; + + if (u == NULL) + u = &nil; + + c = 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 (c == -1 && status != NULL) + *status = uuid_s_no_memory; +} |