diff --git a/include/uuid.h b/include/uuid.h new file mode 100644 index 000000000000..48076156d116 --- /dev/null +++ b/include/uuid.h @@ -0,0 +1,57 @@ +/*- + * 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$ + */ + +#ifndef _UUID_H_ +#define _UUID_H_ + +#include +#include + +/* + * This implementation mostly conforms to the DCE 1.1 specification. + * See Also: + * uuidgen(1), uuidgen(2), uuid(3) + */ + +/* 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 + +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 *); + +#endif /* _UUID_H_ */ diff --git a/lib/libc/Makefile.inc b/lib/libc/Makefile.inc index a7fe894043ae..be65f3f056e7 100644 --- a/lib/libc/Makefile.inc +++ b/lib/libc/Makefile.inc @@ -37,6 +37,7 @@ NOASM= .include "${.CURDIR}/../libc/string/Makefile.inc" .include "${.CURDIR}/../libc/sys/Makefile.inc" .include "${.CURDIR}/../libc/rpc/Makefile.inc" +.include "${.CURDIR}/../libc/uuid/Makefile.inc" .include "${.CURDIR}/../libc/xdr/Makefile.inc" .if !defined(NO_YP_LIBC) CFLAGS+= -DYP diff --git a/lib/libc/uuid/Makefile.inc b/lib/libc/uuid/Makefile.inc new file mode 100644 index 000000000000..51b2ce05fcfd --- /dev/null +++ b/lib/libc/uuid/Makefile.inc @@ -0,0 +1,22 @@ +# $FreeBSD$ + +# DCE 1.1 UUID implementation sources + +.PATH: ${.CURDIR}/../libc/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 + +INCS+= uuid.h + +.if ${LIB} == "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 +.endif diff --git a/lib/libc/uuid/uuid.3 b/lib/libc/uuid/uuid.3 new file mode 100644 index 000000000000..caf5debf694e --- /dev/null +++ b/lib/libc/uuid/uuid.3 @@ -0,0 +1,98 @@ +.\" 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 October 29, 2002 +.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 "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" +.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 , 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 +parameter. Possible values are: +.Pp +.Bl -tag -width 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 BUGS +This manpage can be improved. +.Sh STANDARDS +The UUID functions conform to the DCE 1.1 RPC specification. diff --git a/lib/libc/uuid/uuid.h b/lib/libc/uuid/uuid.h new file mode 100644 index 000000000000..48076156d116 --- /dev/null +++ b/lib/libc/uuid/uuid.h @@ -0,0 +1,57 @@ +/*- + * 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$ + */ + +#ifndef _UUID_H_ +#define _UUID_H_ + +#include +#include + +/* + * This implementation mostly conforms to the DCE 1.1 specification. + * See Also: + * uuidgen(1), uuidgen(2), uuid(3) + */ + +/* 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 + +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 *); + +#endif /* _UUID_H_ */ diff --git a/lib/libc/uuid/uuid_compare.c b/lib/libc/uuid/uuid_compare.c new file mode 100644 index 000000000000..8b6d2189e325 --- /dev/null +++ b/lib/libc/uuid/uuid_compare.c @@ -0,0 +1,76 @@ +/*- + * 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 +#include + +/* + * uuid_compare() - compare two UUIDs. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_compare.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_compare.html + * + * 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); + return (memcmp(a->node, b->node, sizeof(uuid_t))); +} diff --git a/lib/libc/uuid/uuid_create.c b/lib/libc/uuid/uuid_create.c new file mode 100644 index 000000000000..a202e1a5d9a2 --- /dev/null +++ b/lib/libc/uuid/uuid_create.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 + +/* + * uuid_create() - create an UUID. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_create.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_create.html + */ +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 000000000000..f2a69ff1a2b6 --- /dev/null +++ b/lib/libc/uuid/uuid_create_nil.c @@ -0,0 +1,47 @@ +/*- + * 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 +#include + +/* + * uuid_create_nil() - create a nil UUID. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_create_nil.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_create_nil.html + */ +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 000000000000..4d36b304c505 --- /dev/null +++ b/lib/libc/uuid/uuid_equal.c @@ -0,0 +1,56 @@ +/*- + * 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 +#include + +/* + * uuid_equal() - compare for equality. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_equal.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_equal.html + */ +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); +} diff --git a/lib/libc/uuid/uuid_from_string.c b/lib/libc/uuid/uuid_from_string.c new file mode 100644 index 000000000000..322a4ed8cbd5 --- /dev/null +++ b/lib/libc/uuid/uuid_from_string.c @@ -0,0 +1,93 @@ +/*- + * 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 +#include +#include + +/* + * 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 + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_from_string.html + * + * 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 000000000000..fade9ecd4a4f --- /dev/null +++ b/lib/libc/uuid/uuid_hash.c @@ -0,0 +1,50 @@ +/*- + * 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_hash() - generate a hash value. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_hash.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_hash.html + */ +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); +} diff --git a/lib/libc/uuid/uuid_is_nil.c b/lib/libc/uuid/uuid_is_nil.c new file mode 100644 index 000000000000..eef235708238 --- /dev/null +++ b/lib/libc/uuid/uuid_is_nil.c @@ -0,0 +1,55 @@ +/*- + * 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_is_nil() - return whether the UUID is a nil UUID. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_is_nil.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_is_nil.html + */ +int32_t +uuid_is_nil(uuid_t *u, uint32_t *status) +{ + 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 = (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 000000000000..fa2b8c227413 --- /dev/null +++ b/lib/libc/uuid/uuid_to_string.c @@ -0,0 +1,68 @@ +/*- + * 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 +#include +#include + +/* + * uuid_to_string() - Convert a binary UUID into a string representation. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_to_string.htm + * http://www.transarc.ibm.com/Library/documentation/dce/1.1/uuid_to_string.html + * + * 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; +}