diff options
author | Peter Eisentraut <peter_e@gmx.net> | 2007-04-21 17:26:18 +0000 |
---|---|---|
committer | Peter Eisentraut <peter_e@gmx.net> | 2007-04-21 17:26:18 +0000 |
commit | 74496bc298e099401a8b41c1c529bf9fb7aa9f3c (patch) | |
tree | 691b988a5d60c61fda649f535537eb7258873653 /contrib/uuid-ossp | |
parent | 71495f296e54468c3998ae1d9bc1654867b6369f (diff) | |
download | postgresql-74496bc298e099401a8b41c1c529bf9fb7aa9f3c.tar.gz |
Contrib module uuid-ossp for generating UUID values using the OSSP UUID
library. New configure option --with-ossp-uuid to activate.
Diffstat (limited to 'contrib/uuid-ossp')
-rw-r--r-- | contrib/uuid-ossp/Makefile | 19 | ||||
-rw-r--r-- | contrib/uuid-ossp/README.uuid-ossp | 97 | ||||
-rw-r--r-- | contrib/uuid-ossp/uninstall_uuid-ossp.sql | 15 | ||||
-rw-r--r-- | contrib/uuid-ossp/uuid-ossp.c | 198 | ||||
-rw-r--r-- | contrib/uuid-ossp/uuid-ossp.sql.in | 15 |
5 files changed, 344 insertions, 0 deletions
diff --git a/contrib/uuid-ossp/Makefile b/contrib/uuid-ossp/Makefile new file mode 100644 index 0000000000..76727a5798 --- /dev/null +++ b/contrib/uuid-ossp/Makefile @@ -0,0 +1,19 @@ +# $PostgreSQL: pgsql/contrib/uuid-ossp/Makefile,v 1.1 2007/04/21 17:26:17 petere Exp $ + +MODULE_big = uuid-ossp +OBJS = uuid-ossp.o +DATA_built = uuid-ossp.sql +DATA = uninstall_uuid-ossp.sql +DOCS = README.uuid-ossp + +SHLIB_LINK += -lossp-uuid + +ifdef USE_PGXS +PGXS := $(shell pg_config --pgxs) +include $(PGXS) +else +subdir = contrib/uuid-ossp +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/contrib/uuid-ossp/README.uuid-ossp b/contrib/uuid-ossp/README.uuid-ossp new file mode 100644 index 0000000000..53a3b72945 --- /dev/null +++ b/contrib/uuid-ossp/README.uuid-ossp @@ -0,0 +1,97 @@ +UUID Generation Functions +========================= +Peter Eisentraut <peter_e@gmx.net> + +This module provides functions to generate universally unique +identifiers (UUIDs) using one of the several standard algorithms, as +well as functions to produce certain special UUID constants. + + +Installation +------------ + +The extra library required can be found at +<http://www.ossp.org/pkg/lib/uuid/>. + + +UUID Generation +--------------- + +The relevant standards ITU-T Rec. X.667, ISO/IEC 9834-8:2005, and RFC +4122 specify four algorithms for generating UUIDs, identified by the +version numbers 1, 3, 4, and 5. (There is no version 2 algorithm.) +Each of these algorithms could be suitable for a different set of +applications. + +uuid_generate_v1() +~~~~~~~~~~~~~~~~~~ + +This function generates a version 1 UUID. This involves the MAC +address of the computer and a time stamp. Note that UUIDs of this +kind reveal the identity of the computer that created the identifier +and the time at which it did so, which might make it unsuitable for +certain security-sensitive applications. + +uuid_generate_v1mc() +~~~~~~~~~~~~~~~~~~~~ + +This function generates a version 1 UUID but uses a random multicast +MAC address instead of the real MAC address of the computer. + +uuid_generate_v3(namespace uuid, name text) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This function generates a version 3 UUID in the given namespace using +the specified input name. The namespace should be one of the special +constants produced by the uuid_ns_*() functions shown below. (It +should be any UUID in theory.) The name is an identifier in the +selected namespace. For example: + + uuid_generate_v3(uuid_ns_url(), 'http://www.postgresql.org') + +The name parameter will be MD5-hashed, so the cleartext cannot be +derived from the generated UUID. + +The generation of UUIDs by this method has no random or +environment-dependent element and is therefore reproducible. + +uuid_generate_v4() +~~~~~~~~~~~~~~~~~~ + +This function generates a version 4 UUID, which is derived entirely +from random numbers. + +uuid_generate_v5(namespace uuid, name text) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This function generates a version 5 UUID, which works like a version 3 +UUID except that SHA-1 is used as a hashing method. Version 5 should +be preferred over version 3 because SHA-1 is thought to be more secure +than MD5. + + +UUID Constants +-------------- + + uuid_nil() + +A "nil" UUID constant, which does not occur as a real UUID. + + uuid_ns_dns() + +Constant designating the DNS namespace for UUIDs. + + uuid_ns_url() + +Constant designating the URL namespace for UUIDs. + + uuid_ns_oid() + +Constant designating the ISO object identifier (OID) namespace for +UUIDs. (This pertains to ASN.1 OIDs, unrelated to the OIDs used in +PostgreSQL.) + + uuid_ns_x500() + +Constant designating the X.500 distinguished name (DN) namespace for +UUIDs. diff --git a/contrib/uuid-ossp/uninstall_uuid-ossp.sql b/contrib/uuid-ossp/uninstall_uuid-ossp.sql new file mode 100644 index 0000000000..30226e98c5 --- /dev/null +++ b/contrib/uuid-ossp/uninstall_uuid-ossp.sql @@ -0,0 +1,15 @@ +/* $PostgreSQL: pgsql/contrib/uuid-ossp/uninstall_uuid-ossp.sql,v 1.1 2007/04/21 17:26:17 petere Exp $ */ + +SET search_path = public; + +DROP FUNCTION uuid_nil(); +DROP FUNCTION uuid_ns_dns(); +DROP FUNCTION uuid_ns_url(); +DROP FUNCTION uuid_ns_oid(); +DROP FUNCTION uuid_ns_x500(); + +DROP FUNCTION uuid_generate_v1(); +DROP FUNCTION uuid_generate_v1mc(); +DROP FUNCTION uuid_generate_v3(namespace uuid, name text); +DROP FUNCTION uuid_generate_v4(); +DROP FUNCTION uuid_generate_v5(namespace uuid, name text); diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c new file mode 100644 index 0000000000..3be2cd1908 --- /dev/null +++ b/contrib/uuid-ossp/uuid-ossp.c @@ -0,0 +1,198 @@ +/*------------------------------------------------------------------------- + * + * UUID generation functions using the OSSP UUID library + * + * Copyright (c) 2007 PostgreSQL Global Development Group + * + * $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.c,v 1.1 2007/04/21 17:26:17 petere Exp $ + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" +#include "fmgr.h" +#include "utils/builtins.h" +#include "utils/uuid.h" + +#include <ossp/uuid.h> + + +/* better both be 16 */ +#if (UUID_LEN != UUID_LEN_BIN) +#error UUID length mismatch +#endif + + +PG_MODULE_MAGIC; + + +Datum uuid_nil(PG_FUNCTION_ARGS); +Datum uuid_ns_dns(PG_FUNCTION_ARGS); +Datum uuid_ns_url(PG_FUNCTION_ARGS); +Datum uuid_ns_oid(PG_FUNCTION_ARGS); +Datum uuid_ns_x500(PG_FUNCTION_ARGS); + +Datum uuid_generate_v1(PG_FUNCTION_ARGS); +Datum uuid_generate_v1mc(PG_FUNCTION_ARGS); +Datum uuid_generate_v3(PG_FUNCTION_ARGS); +Datum uuid_generate_v4(PG_FUNCTION_ARGS); +Datum uuid_generate_v5(PG_FUNCTION_ARGS); + + +PG_FUNCTION_INFO_V1(uuid_nil); +PG_FUNCTION_INFO_V1(uuid_ns_dns); +PG_FUNCTION_INFO_V1(uuid_ns_url); +PG_FUNCTION_INFO_V1(uuid_ns_oid); +PG_FUNCTION_INFO_V1(uuid_ns_x500); + +PG_FUNCTION_INFO_V1(uuid_generate_v1); +PG_FUNCTION_INFO_V1(uuid_generate_v1mc); +PG_FUNCTION_INFO_V1(uuid_generate_v3); +PG_FUNCTION_INFO_V1(uuid_generate_v4); +PG_FUNCTION_INFO_V1(uuid_generate_v5); + + +static char * +uuid_to_string(const uuid_t *uuid) +{ + char *buf = palloc(UUID_LEN_STR + 1); + void *ptr = buf; + size_t len = UUID_LEN_STR + 1; + + uuid_export(uuid, UUID_FMT_STR, &ptr, &len); + + return buf; +} + + +static void +string_to_uuid(const char *str, uuid_t *uuid) +{ + uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1); +} + + +static Datum +special_uuid_value(const char *name) +{ + uuid_t *uuid; + char *str; + + uuid_create(&uuid); + uuid_load(uuid, name); + str = uuid_to_string(uuid); + uuid_destroy(uuid); + + return DirectFunctionCall1(uuid_in, CStringGetDatum(str)); +} + + +Datum +uuid_nil(PG_FUNCTION_ARGS) +{ + return special_uuid_value("nil"); +} + + +Datum +uuid_ns_dns(PG_FUNCTION_ARGS) +{ + return special_uuid_value("ns:DNS"); +} + + +Datum +uuid_ns_url(PG_FUNCTION_ARGS) +{ + return special_uuid_value("ns:URL"); +} + + +Datum +uuid_ns_oid(PG_FUNCTION_ARGS) +{ + return special_uuid_value("ns:OID"); +} + + +Datum +uuid_ns_x500(PG_FUNCTION_ARGS) +{ + return special_uuid_value("ns:X500"); +} + + +static Datum +uuid_generate_internal(int mode, const uuid_t *ns, const char *name) +{ + uuid_t *uuid; + char *str; + + uuid_create(&uuid); + uuid_make(uuid, mode, ns, name); + str = uuid_to_string(uuid); + uuid_destroy(uuid); + + return DirectFunctionCall1(uuid_in, CStringGetDatum(str)); +} + + +Datum +uuid_generate_v1(PG_FUNCTION_ARGS) +{ + return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL); +} + + +Datum +uuid_generate_v1mc(PG_FUNCTION_ARGS) +{ + return uuid_generate_internal(UUID_MAKE_V1 | UUID_MAKE_MC, NULL, NULL); +} + + +static Datum +uuid_generate_v35_internal(int mode, pg_uuid_t *ns, text *name) +{ + uuid_t *ns_uuid; + Datum result; + + uuid_create(&ns_uuid); + string_to_uuid(DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(ns))), + ns_uuid); + + result = uuid_generate_internal(mode, + ns_uuid, + DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(name)))); + + uuid_destroy(ns_uuid); + + return result; +} + + +Datum +uuid_generate_v3(PG_FUNCTION_ARGS) +{ + pg_uuid_t *ns = PG_GETARG_UUID_P(0); + text *name = PG_GETARG_TEXT_P(1); + + return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name); +} + + +Datum +uuid_generate_v4(PG_FUNCTION_ARGS) +{ + return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL); +} + + +Datum +uuid_generate_v5(PG_FUNCTION_ARGS) +{ + pg_uuid_t *ns = PG_GETARG_UUID_P(0); + text *name = PG_GETARG_TEXT_P(1); + + return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name); +} diff --git a/contrib/uuid-ossp/uuid-ossp.sql.in b/contrib/uuid-ossp/uuid-ossp.sql.in new file mode 100644 index 0000000000..5b0599d4ad --- /dev/null +++ b/contrib/uuid-ossp/uuid-ossp.sql.in @@ -0,0 +1,15 @@ +/* $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.sql.in,v 1.1 2007/04/21 17:26:17 petere Exp $ */ + +SET search_path = public; + +CREATE FUNCTION uuid_nil() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_nil'; +CREATE FUNCTION uuid_ns_dns() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_dns'; +CREATE FUNCTION uuid_ns_url() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_url'; +CREATE FUNCTION uuid_ns_oid() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_oid'; +CREATE FUNCTION uuid_ns_x500() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_x500'; + +CREATE FUNCTION uuid_generate_v1() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v1'; +CREATE FUNCTION uuid_generate_v1mc() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v1mc'; +CREATE FUNCTION uuid_generate_v3(namespace uuid, name text) RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v3'; +CREATE FUNCTION uuid_generate_v4() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v4'; +CREATE FUNCTION uuid_generate_v5(namespace uuid, name text) RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v5'; |