31#define uuid_hash bsd_uuid_hash
33#if defined(HAVE_UUID_H)
35#elif defined(HAVE_OSSP_UUID_H)
37#elif defined(HAVE_UUID_UUID_H)
40#error "please use configure's --with-uuid switch to select a UUID library"
46#if defined(HAVE_UUID_OSSP) && (UUID_LEN != UUID_LEN_BIN)
47#error UUID length mismatch
69 uint16_t time_hi_and_version;
70 uint8_t clock_seq_hi_and_reserved;
71 uint8_t clock_seq_low;
75#define dce_uuid_t uuid_t
81#define UUID_TO_NETWORK(uu) \
83 uu.time_low = pg_hton32(uu.time_low); \
84 uu.time_mid = pg_hton16(uu.time_mid); \
85 uu.time_hi_and_version = pg_hton16(uu.time_hi_and_version); \
88#define UUID_TO_LOCAL(uu) \
90 uu.time_low = pg_ntoh32(uu.time_low); \
91 uu.time_mid = pg_ntoh16(uu.time_mid); \
92 uu.time_hi_and_version = pg_ntoh16(uu.time_hi_and_version); \
95#define UUID_V3_OR_V5(uu, v) \
97 uu.time_hi_and_version &= 0x0FFF; \
98 uu.time_hi_and_version |= (v << 12); \
99 uu.clock_seq_hi_and_reserved &= 0x3F; \
100 uu.clock_seq_hi_and_reserved |= 0x80; \
107 .version = PG_VERSION
125pguuid_complain(uuid_rc_t rc)
127 char *
err = uuid_error(rc);
131 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
132 errmsg(
"OSSP uuid library failure: %s",
err)));
135 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
136 errmsg(
"OSSP uuid library failure: error code %d", rc)));
157get_cached_uuid_t(
int which)
159 static uuid_t *cached_uuid[2] = {NULL, NULL};
161 if (cached_uuid[which] == NULL)
165 rc = uuid_create(&cached_uuid[which]);
166 if (rc != UUID_RC_OK)
168 cached_uuid[which] = NULL;
172 return cached_uuid[which];
176uuid_to_string(
const uuid_t *uuid)
180 size_t len = UUID_LEN_STR + 1;
183 rc = uuid_export(uuid, UUID_FMT_STR, &ptr, &
len);
184 if (rc != UUID_RC_OK)
196 rc = uuid_import(uuid, UUID_FMT_STR,
str, UUID_LEN_STR + 1);
197 if (rc != UUID_RC_OK)
203special_uuid_value(
const char *
name)
205 uuid_t *uuid = get_cached_uuid_t(0);
209 rc = uuid_load(uuid,
name);
210 if (rc != UUID_RC_OK)
212 str = uuid_to_string(uuid);
221 uuid_t *uuid = get_cached_uuid_t(0);
225 rc = uuid_make(uuid,
mode, ns,
name);
226 if (rc != UUID_RC_OK)
228 str = uuid_to_string(uuid);
237 uuid_t *ns_uuid = get_cached_uuid_t(1);
267 uuid_generate_time(uu);
268 uuid_unparse(uu, strbuf);
275 if (ptr &&
len <= 36)
276 strcpy(strbuf + (36 -
len), ptr);
279 uint32_t status = uuid_s_ok;
282 uuid_create(&uu, &status);
284 if (status == uuid_s_ok)
286 uuid_to_string(&uu, &
str, &status);
287 if (status == uuid_s_ok)
296 if (strbuf[14] !=
'1')
298 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
300 errmsg(
"uuid_create() produced a version %c UUID instead of the expected version 1",
308 if (ptr &&
len <= 36)
309 strcpy(strbuf + (36 -
len), ptr);
314 if (status != uuid_s_ok)
316 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
317 errmsg(
"uuid library failure: %d",
328 uint32_t status = uuid_s_ok;
337 elog(
ERROR,
"could not initialize %s context: %s",
"MD5",
341 elog(
ERROR,
"could not update %s context: %s",
"MD5",
346 elog(
ERROR,
"could not finalize %s context: %s",
"MD5",
356 elog(
ERROR,
"could not initialize %s context: %s",
"SHA1",
360 elog(
ERROR,
"could not update %s context: %s",
"SHA1",
363 elog(
ERROR,
"could not finalize %s context: %s",
"SHA1",
367 memcpy(&uu, sha1result,
sizeof(uu));
377 uuid_unparse((
unsigned char *) &uu, strbuf);
379 uuid_to_string(&uu, &
str, &status);
381 if (status == uuid_s_ok)
386 if (status != uuid_s_ok)
388 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
389 errmsg(
"uuid library failure: %d",
401 uuid_generate_random(uu);
402 uuid_unparse(uu, strbuf);
405 "%08lx-%04x-%04x-%04x-%04x%08lx",
406 (
unsigned long) arc4random(),
407 (
unsigned) (arc4random() & 0xffff),
408 (
unsigned) ((arc4random() & 0xfff) | 0x4000),
409 (
unsigned) ((arc4random() & 0x3fff) | 0x8000),
410 (
unsigned) (arc4random() & 0xffff),
411 (
unsigned long) arc4random());
427 return special_uuid_value(
"nil");
430 "00000000-0000-0000-0000-000000000000", 36);
439 return special_uuid_value(
"ns:DNS");
442 "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
451 return special_uuid_value(
"ns:URL");
454 "6ba7b811-9dad-11d1-80b4-00c04fd430c8", 36);
463 return special_uuid_value(
"ns:OID");
466 "6ba7b812-9dad-11d1-80b4-00c04fd430c8", 36);
475 return special_uuid_value(
"ns:X500");
478 "6ba7b814-9dad-11d1-80b4-00c04fd430c8", 36);
495#elif defined(HAVE_UUID_E2FS)
500 uuid_generate_random(uu);
505 uuid_unparse(uu, strbuf);
512 (
unsigned) ((arc4random() & 0xffff) | 0x0300),
513 (
unsigned long) arc4random());
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
#define PG_GETARG_TEXT_PP(n)
#define DirectFunctionCall1(func, arg1)
static PgChecksumMode mode
size_t strlcpy(char *dst, const char *src, size_t siz)
static char * DatumGetCString(Datum X)
static Datum CStringGetDatum(const char *X)
#define SHA1_DIGEST_LENGTH
Datum uuid_ns_oid(PG_FUNCTION_ARGS)
PG_MODULE_MAGIC_EXT(.name="uuid-ossp",.version=PG_VERSION)
Datum uuid_ns_dns(PG_FUNCTION_ARGS)
Datum uuid_nil(PG_FUNCTION_ARGS)
Datum uuid_ns_url(PG_FUNCTION_ARGS)
Datum uuid_generate_v1mc(PG_FUNCTION_ARGS)
Datum uuid_generate_v4(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(uuid_nil)
Datum uuid_generate_v5(PG_FUNCTION_ARGS)
#define UUID_V3_OR_V5(uu, v)
Datum uuid_ns_x500(PG_FUNCTION_ARGS)
Datum uuid_generate_v1(PG_FUNCTION_ARGS)
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
#define UUID_TO_LOCAL(uu)
#define UUID_TO_NETWORK(uu)
Datum uuid_generate_v3(PG_FUNCTION_ARGS)
static void string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext)
Datum uuid_out(PG_FUNCTION_ARGS)
Datum uuid_in(PG_FUNCTION_ARGS)
static Datum UUIDPGetDatum(const pg_uuid_t *X)
#define PG_GETARG_UUID_P(X)
#define VARSIZE_ANY_EXHDR(PTR)
char * text_to_cstring(const text *t)