diff options
Diffstat (limited to 'lib/libc/uuid')
-rw-r--r-- | lib/libc/uuid/Makefile.inc | 19 | ||||
-rw-r--r-- | lib/libc/uuid/Symbol.map | 14 | ||||
-rw-r--r-- | lib/libc/uuid/uuid.3 | 100 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_compare.c | 76 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_create.c | 45 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_create_nil.c | 46 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_equal.c | 55 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_from_string.c | 92 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_hash.c | 49 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_is_nil.c | 54 | ||||
-rw-r--r-- | lib/libc/uuid/uuid_to_string.c | 67 |
11 files changed, 617 insertions, 0 deletions
diff --git a/lib/libc/uuid/Makefile.inc b/lib/libc/uuid/Makefile.inc new file mode 100644 index 0000000..d2ef521 --- /dev/null +++ b/lib/libc/uuid/Makefile.inc @@ -0,0 +1,19 @@ +# $FreeBSD$ + +# DCE 1.1 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_to_string.c +SYM_MAPS+= ${.CURDIR}/uuid/Symbol.map + +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 diff --git a/lib/libc/uuid/Symbol.map b/lib/libc/uuid/Symbol.map new file mode 100644 index 0000000..974a964 --- /dev/null +++ b/lib/libc/uuid/Symbol.map @@ -0,0 +1,14 @@ +/* + * $FreeBSD$ + */ + +FBSD_1.0 { + uuid_to_string; + uuid_is_nil; + uuid_hash; + uuid_from_string; + uuid_equal; + uuid_create_nil; + uuid_create; + uuid_compare; +}; diff --git a/lib/libc/uuid/uuid.3 b/lib/libc/uuid/uuid.3 new file mode 100644 index 0000000..08cf737 --- /dev/null +++ b/lib/libc/uuid/uuid.3 @@ -0,0 +1,100 @@ +.\" 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$ +.\" +.Dd January 3, 2005 +.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 +.Nd DCE 1.1 compliant UUID functions +.Sh LIBRARY +.Lb libc +.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" +.Sh DESCRIPTION +The family of DCE 1.1 compliant UUID functions allow applications to operate +on universally unique identifiers, or UUIDs. +The +.Fn uuid_create +and +.Fn uuid_create_nil +functions create UUIDs. +The +.Fn uuid_compare , +.Fn uuid_equal +and +.Fn uuid_is_nil +functions can be used to test UUIDs. +To convert from the binary representation to the string representation or +vice versa, use +.Fn uuid_to_string +or +.Fn uuid_from_string +respectively. +A 16-bit hash value can be obtained by calling +.Fn uuid_hash . +.Sh RETURN VALUES +The successful or unsuccessful completion of the function is returned 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 +The meaning of the code escaped the writers mind. +.El +.Sh SEE ALSO +.Xr uuidgen 1 , +.Xr uuidgen 2 +.Sh STANDARDS +The UUID functions conform to the DCE 1.1 RPC specification. +.Sh BUGS +This manpage can be improved. diff --git a/lib/libc/uuid/uuid_compare.c b/lib/libc/uuid/uuid_compare.c new file mode 100644 index 0000000..13d7c27 --- /dev/null +++ b/lib/libc/uuid/uuid_compare.c @@ -0,0 +1,76 @@ +/*- + * Copyright (c) 2002,2005 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$ + */ + +#include <string.h> +#include <uuid.h> + +/* A macro used to improve the readability of uuid_compare(). */ +#define DIFF_RETURN(a, b, field) do { \ + if ((a)->field != (b)->field) \ + return (((a)->field < (b)->field) ? -1 : 1); \ +} while (0) + +/* + * 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. */ + DIFF_RETURN(a, b, time_low); + DIFF_RETURN(a, b, time_mid); + DIFF_RETURN(a, b, time_hi_and_version); + DIFF_RETURN(a, b, clock_seq_hi_and_reserved); + DIFF_RETURN(a, b, clock_seq_low); + + res = memcmp(a->node, b->node, sizeof(a->node)); + if (res) + return ((res < 0) ? -1 : 1); + return (0); +} + +#undef DIFF_RETURN diff --git a/lib/libc/uuid/uuid_create.c b/lib/libc/uuid/uuid_create.c new file mode 100644 index 0000000..da3f1d3 --- /dev/null +++ b/lib/libc/uuid/uuid_create.c @@ -0,0 +1,45 @@ +/*- + * 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$ + */ + +#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) +{ + + if (status) + *status = uuid_s_ok; + + uuidgen(u, 1); +} diff --git a/lib/libc/uuid/uuid_create_nil.c b/lib/libc/uuid/uuid_create_nil.c new file mode 100644 index 0000000..6e85ae7 --- /dev/null +++ b/lib/libc/uuid/uuid_create_nil.c @@ -0,0 +1,46 @@ +/*- + * 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$ + */ + +#include <strings.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) +{ + + if (status) + *status = uuid_s_ok; + + bzero(u, sizeof(*u)); +} diff --git a/lib/libc/uuid/uuid_equal.c b/lib/libc/uuid/uuid_equal.c new file mode 100644 index 0000000..e4c48e5 --- /dev/null +++ b/lib/libc/uuid/uuid_equal.c @@ -0,0 +1,55 @@ +/*- + * Copyright (c) 2002,2005 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$ + */ + +#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 0000000..b09a04d --- /dev/null +++ b/lib/libc/uuid/uuid_from_string.c @@ -0,0 +1,92 @@ +/*- + * 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$ + */ + +#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; + } +} diff --git a/lib/libc/uuid/uuid_hash.c b/lib/libc/uuid/uuid_hash.c new file mode 100644 index 0000000..e5695ee --- /dev/null +++ b/lib/libc/uuid/uuid_hash.c @@ -0,0 +1,49 @@ +/*- + * Copyright (c) 2002,2005 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$ + */ + +#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 0000000..ee18653 --- /dev/null +++ b/lib/libc/uuid/uuid_is_nil.c @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2002,2005 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$ + */ + +#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) +{ + const uint32_t *p; + + if (status) + *status = uuid_s_ok; + + if (!u) + return (1); + + /* + * Pick the largest type that has equivalent alignment constraints + * as an UUID and use it to test if the UUID consists of all zeroes. + */ + p = (const uint32_t*)u; + return ((p[0] == 0 && p[1] == 0 && p[2] == 0 && p[3] == 0) ? 1 : 0); +} diff --git a/lib/libc/uuid/uuid_to_string.c b/lib/libc/uuid/uuid_to_string.c new file mode 100644 index 0000000..f816391 --- /dev/null +++ b/lib/libc/uuid/uuid_to_string.c @@ -0,0 +1,67 @@ +/*- + * Copyright (c) 2002,2005 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$ + */ + +#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) +{ + 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(&nil, 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; +} |