31 #define uuid_hash bsd_uuid_hash
33 #if defined(HAVE_UUID_H)
35 #elif defined(HAVE_OSSP_UUID_H)
36 #include <ossp/uuid.h>
37 #elif defined(HAVE_UUID_UUID_H)
38 #include <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
51 #ifndef HAVE_UUID_OSSP
52 #define UUID_MAKE_MC 0
53 #define UUID_MAKE_V1 1
54 #define UUID_MAKE_V2 2
55 #define UUID_MAKE_V3 3
56 #define UUID_MAKE_V4 4
57 #define UUID_MAKE_V5 5
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
79 #ifndef HAVE_UUID_OSSP
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; \
119 #ifdef HAVE_UUID_OSSP
122 pguuid_complain(uuid_rc_t rc)
124 char *
err = uuid_error(rc);
128 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
129 errmsg(
"OSSP uuid library failure: %s",
err)));
132 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
133 errmsg(
"OSSP uuid library failure: error code %d", rc)));
154 get_cached_uuid_t(
int which)
156 static uuid_t *cached_uuid[2] = {NULL, NULL};
158 if (cached_uuid[which] == NULL)
162 rc = uuid_create(&cached_uuid[which]);
163 if (rc != UUID_RC_OK)
165 cached_uuid[which] = NULL;
169 return cached_uuid[which];
173 uuid_to_string(
const uuid_t *uuid)
177 size_t len = UUID_LEN_STR + 1;
180 rc = uuid_export(uuid, UUID_FMT_STR, &ptr, &
len);
181 if (rc != UUID_RC_OK)
193 rc = uuid_import(uuid, UUID_FMT_STR,
str, UUID_LEN_STR + 1);
194 if (rc != UUID_RC_OK)
200 special_uuid_value(
const char *
name)
202 uuid_t *uuid = get_cached_uuid_t(0);
206 rc = uuid_load(uuid,
name);
207 if (rc != UUID_RC_OK)
209 str = uuid_to_string(uuid);
218 uuid_t *uuid = get_cached_uuid_t(0);
222 rc = uuid_make(uuid,
mode, ns,
name);
223 if (rc != UUID_RC_OK)
225 str = uuid_to_string(uuid);
234 uuid_t *ns_uuid = get_cached_uuid_t(1);
261 #ifdef HAVE_UUID_E2FS
264 uuid_generate_time(uu);
265 uuid_unparse(uu, strbuf);
272 if (ptr &&
len <= 36)
273 strcpy(strbuf + (36 -
len), ptr);
276 uint32_t status = uuid_s_ok;
279 uuid_create(&uu, &status);
281 if (status == uuid_s_ok)
283 uuid_to_string(&uu, &
str, &status);
284 if (status == uuid_s_ok)
293 if (strbuf[14] !=
'1')
295 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
297 errmsg(
"uuid_create() produced a version %c UUID instead of the expected version 1",
305 if (ptr &&
len <= 36)
306 strcpy(strbuf + (36 -
len), ptr);
311 if (status != uuid_s_ok)
313 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
314 errmsg(
"uuid library failure: %d",
325 uint32_t status = uuid_s_ok;
334 elog(
ERROR,
"could not initialize %s context: %s",
"MD5",
338 elog(
ERROR,
"could not update %s context: %s",
"MD5",
343 elog(
ERROR,
"could not finalize %s context: %s",
"MD5",
353 elog(
ERROR,
"could not initialize %s context: %s",
"SHA1",
357 elog(
ERROR,
"could not update %s context: %s",
"SHA1",
360 elog(
ERROR,
"could not finalize %s context: %s",
"SHA1",
364 memcpy(&uu, sha1result,
sizeof(uu));
371 #ifdef HAVE_UUID_E2FS
374 uuid_unparse((
unsigned char *) &uu, strbuf);
376 uuid_to_string(&uu, &
str, &status);
378 if (status == uuid_s_ok)
383 if (status != uuid_s_ok)
385 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
386 errmsg(
"uuid library failure: %d",
395 #ifdef HAVE_UUID_E2FS
398 uuid_generate_random(uu);
399 uuid_unparse(uu, strbuf);
402 "%08lx-%04x-%04x-%04x-%04x%08lx",
403 (
unsigned long) arc4random(),
404 (
unsigned) (arc4random() & 0xffff),
405 (
unsigned) ((arc4random() & 0xfff) | 0x4000),
406 (
unsigned) ((arc4random() & 0x3fff) | 0x8000),
407 (
unsigned) (arc4random() & 0xffff),
408 (
unsigned long) arc4random());
423 #ifdef HAVE_UUID_OSSP
424 return special_uuid_value(
"nil");
427 "00000000-0000-0000-0000-000000000000", 36);
435 #ifdef HAVE_UUID_OSSP
436 return special_uuid_value(
"ns:DNS");
439 "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
447 #ifdef HAVE_UUID_OSSP
448 return special_uuid_value(
"ns:URL");
451 "6ba7b811-9dad-11d1-80b4-00c04fd430c8", 36);
459 #ifdef HAVE_UUID_OSSP
460 return special_uuid_value(
"ns:OID");
463 "6ba7b812-9dad-11d1-80b4-00c04fd430c8", 36);
471 #ifdef HAVE_UUID_OSSP
472 return special_uuid_value(
"ns:X500");
475 "6ba7b814-9dad-11d1-80b4-00c04fd430c8", 36);
490 #ifdef HAVE_UUID_OSSP
492 #elif defined(HAVE_UUID_E2FS)
497 uuid_generate_random(uu);
502 uuid_unparse(uu, strbuf);
509 (
unsigned) ((arc4random() & 0xffff) | 0x0300),
510 (
unsigned long) arc4random());
524 #ifdef HAVE_UUID_OSSP
546 #ifdef HAVE_UUID_OSSP
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
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)
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)