1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*-------------------------------------------------------------------------
*
* UUID generation functions using the OSSP UUID library
*
* Copyright (c) 2007-2011, PostgreSQL Global Development Group
*
* contrib/uuid-ossp/uuid-ossp.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
/*
* There's some confusion over the location of the uuid.h header file.
* On Debian, it's installed as ossp/uuid.h, while on Fedora, or if you
* install ossp-uuid from a tarball, it's installed as uuid.h. Don't know
* what other systems do.
*/
#ifdef HAVE_OSSP_UUID_H
#include <ossp/uuid.h>
#else
#ifdef HAVE_UUID_H
#include <uuid.h>
#else
#error OSSP uuid.h not found
#endif
#endif
/* 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 void
pguuid_complain(uuid_rc_t rc)
{
char *err = uuid_error(rc);
if (err != NULL)
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("OSSP uuid library failure: %s", err)));
else
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("OSSP uuid library failure: error code %d", rc)));
}
/*
* We create a uuid_t object just once per session and re-use it for all
* operations in this module. OSSP UUID caches the system MAC address and
* other state in this object. Reusing the object has a number of benefits:
* saving the cycles needed to fetch the system MAC address over and over,
* reducing the amount of entropy we draw from /dev/urandom, and providing a
* positive guarantee that successive generated V1-style UUIDs don't collide.
* (On a machine fast enough to generate multiple UUIDs per microsecond,
* or whatever the system's wall-clock resolution is, we'd otherwise risk
* collisions whenever random initialization of the uuid_t's clock sequence
* value chanced to produce duplicates.)
*
* However: when we're doing V3 or V5 UUID creation, uuid_make needs two
* uuid_t objects, one holding the namespace UUID and one for the result.
* It's unspecified whether it's safe to use the same uuid_t for both cases,
* so let's cache a second uuid_t for use as the namespace holder object.
*/
static uuid_t *
get_cached_uuid_t(int which)
{
static uuid_t *cached_uuid[2] = {NULL, NULL};
if (cached_uuid[which] == NULL)
{
uuid_rc_t rc;
rc = uuid_create(&cached_uuid[which]);
if (rc != UUID_RC_OK)
{
cached_uuid[which] = NULL;
pguuid_complain(rc);
}
}
return cached_uuid[which];
}
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_rc_t rc;
rc = uuid_export(uuid, UUID_FMT_STR, &ptr, &len);
if (rc != UUID_RC_OK)
pguuid_complain(rc);
return buf;
}
static void
string_to_uuid(const char *str, uuid_t *uuid)
{
uuid_rc_t rc;
rc = uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1);
if (rc != UUID_RC_OK)
pguuid_complain(rc);
}
static Datum
special_uuid_value(const char *name)
{
uuid_t *uuid = get_cached_uuid_t(0);
char *str;
uuid_rc_t rc;
rc = uuid_load(uuid, name);
if (rc != UUID_RC_OK)
pguuid_complain(rc);
str = uuid_to_string(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 = get_cached_uuid_t(0);
char *str;
uuid_rc_t rc;
rc = uuid_make(uuid, mode, ns, name);
if (rc != UUID_RC_OK)
pguuid_complain(rc);
str = uuid_to_string(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 = get_cached_uuid_t(1);
string_to_uuid(DatumGetCString(DirectFunctionCall1(uuid_out,
UUIDPGetDatum(ns))),
ns_uuid);
return uuid_generate_internal(mode,
ns_uuid,
text_to_cstring(name));
}
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);
}
|