30 #define uuid_hash bsd_uuid_hash
32 #if defined(HAVE_UUID_H)
34 #elif defined(HAVE_OSSP_UUID_H)
35 #include <ossp/uuid.h>
36 #elif defined(HAVE_UUID_UUID_H)
37 #include <uuid/uuid.h>
39 #error "please use configure's --with-uuid switch to select a UUID library"
45 #if defined(HAVE_UUID_OSSP) && (UUID_LEN != UUID_LEN_BIN)
46 #error UUID length mismatch
50 #ifndef HAVE_UUID_OSSP
51 #define UUID_MAKE_MC 0
52 #define UUID_MAKE_V1 1
53 #define UUID_MAKE_V2 2
54 #define UUID_MAKE_V3 3
55 #define UUID_MAKE_V4 4
56 #define UUID_MAKE_V5 5
68 uint16_t time_hi_and_version;
69 uint8_t clock_seq_hi_and_reserved;
70 uint8_t clock_seq_low;
74 #define dce_uuid_t uuid_t
78 #ifndef HAVE_UUID_OSSP
80 #define UUID_TO_NETWORK(uu) \
82 uu.time_low = pg_hton32(uu.time_low); \
83 uu.time_mid = pg_hton16(uu.time_mid); \
84 uu.time_hi_and_version = pg_hton16(uu.time_hi_and_version); \
87 #define UUID_TO_LOCAL(uu) \
89 uu.time_low = pg_ntoh32(uu.time_low); \
90 uu.time_mid = pg_ntoh16(uu.time_mid); \
91 uu.time_hi_and_version = pg_ntoh16(uu.time_hi_and_version); \
94 #define UUID_V3_OR_V5(uu, v) \
96 uu.time_hi_and_version &= 0x0FFF; \
97 uu.time_hi_and_version |= (v << 12); \
98 uu.clock_seq_hi_and_reserved &= 0x3F; \
99 uu.clock_seq_hi_and_reserved |= 0x80; \
118 #ifdef HAVE_UUID_OSSP
121 pguuid_complain(uuid_rc_t rc)
123 char *err = uuid_error(rc);
127 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
128 errmsg(
"OSSP uuid library failure: %s", err)));
131 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
132 errmsg(
"OSSP uuid library failure: error code %d", rc)));
153 get_cached_uuid_t(
int which)
155 static uuid_t *cached_uuid[2] = {NULL, NULL};
157 if (cached_uuid[which] == NULL)
161 rc = uuid_create(&cached_uuid[which]);
162 if (rc != UUID_RC_OK)
164 cached_uuid[which] = NULL;
168 return cached_uuid[which];
172 uuid_to_string(
const uuid_t *uuid)
176 size_t len = UUID_LEN_STR + 1;
179 rc = uuid_export(uuid, UUID_FMT_STR, &ptr, &
len);
180 if (rc != UUID_RC_OK)
192 rc = uuid_import(uuid, UUID_FMT_STR,
str, UUID_LEN_STR + 1);
193 if (rc != UUID_RC_OK)
199 special_uuid_value(
const char *
name)
201 uuid_t *uuid = get_cached_uuid_t(0);
205 rc = uuid_load(uuid,
name);
206 if (rc != UUID_RC_OK)
208 str = uuid_to_string(uuid);
217 uuid_t *uuid = get_cached_uuid_t(0);
221 rc = uuid_make(uuid,
mode, ns,
name);
222 if (rc != UUID_RC_OK)
224 str = uuid_to_string(uuid);
233 uuid_t *ns_uuid = get_cached_uuid_t(1);
260 #ifdef HAVE_UUID_E2FS
263 uuid_generate_time(uu);
264 uuid_unparse(uu, strbuf);
271 if (ptr &&
len <= 36)
272 strcpy(strbuf + (36 -
len), ptr);
275 uint32_t
status = uuid_s_ok;
278 uuid_create(&uu, &
status);
292 if (ptr &&
len <= 36)
293 strcpy(strbuf + (36 -
len), ptr);
301 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
302 errmsg(
"uuid library failure: %d",
313 uint32_t
status = uuid_s_ok;
322 elog(
ERROR,
"could not initialize %s context: %s",
"MD5",
326 elog(
ERROR,
"could not update %s context: %s",
"MD5",
331 elog(
ERROR,
"could not finalize %s context: %s",
"MD5",
341 elog(
ERROR,
"could not initialize %s context: %s",
"SHA1",
345 elog(
ERROR,
"could not update %s context: %s",
"SHA1",
348 elog(
ERROR,
"could not finalize %s context: %s",
"SHA1",
352 memcpy(&uu, sha1result,
sizeof(uu));
359 #ifdef HAVE_UUID_E2FS
362 uuid_unparse((
unsigned char *) &uu, strbuf);
374 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
375 errmsg(
"uuid library failure: %d",
384 #ifdef HAVE_UUID_E2FS
387 uuid_generate_random(uu);
388 uuid_unparse(uu, strbuf);
391 "%08lx-%04x-%04x-%04x-%04x%08lx",
392 (
unsigned long) arc4random(),
393 (
unsigned) (arc4random() & 0xffff),
394 (
unsigned) ((arc4random() & 0xfff) | 0x4000),
395 (
unsigned) ((arc4random() & 0x3fff) | 0x8000),
396 (
unsigned) (arc4random() & 0xffff),
397 (
unsigned long) arc4random());
412 #ifdef HAVE_UUID_OSSP
413 return special_uuid_value(
"nil");
416 "00000000-0000-0000-0000-000000000000", 36);
424 #ifdef HAVE_UUID_OSSP
425 return special_uuid_value(
"ns:DNS");
428 "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
436 #ifdef HAVE_UUID_OSSP
437 return special_uuid_value(
"ns:URL");
440 "6ba7b811-9dad-11d1-80b4-00c04fd430c8", 36);
448 #ifdef HAVE_UUID_OSSP
449 return special_uuid_value(
"ns:OID");
452 "6ba7b812-9dad-11d1-80b4-00c04fd430c8", 36);
460 #ifdef HAVE_UUID_OSSP
461 return special_uuid_value(
"ns:X500");
464 "6ba7b814-9dad-11d1-80b4-00c04fd430c8", 36);
479 #ifdef HAVE_UUID_OSSP
481 #elif defined(HAVE_UUID_E2FS)
486 uuid_generate_random(uu);
491 uuid_unparse(uu, strbuf);
498 (
unsigned) ((arc4random() & 0xffff) | 0x0300),
499 (
unsigned long) arc4random());
513 #ifdef HAVE_UUID_OSSP
535 #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,...)
#define PG_GETARG_TEXT_PP(n)
#define DirectFunctionCall1(func, arg1)
static PgChecksumMode mode
static void static void status(const char *fmt,...) pg_attribute_printf(1
size_t strlcpy(char *dst, const char *src, size_t siz)
#define CStringGetDatum(X)
#define DatumGetCString(X)
#define VARSIZE_ANY_EXHDR(PTR)
#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)
Datum uuid_out(PG_FUNCTION_ARGS)
Datum uuid_in(PG_FUNCTION_ARGS)
#define PG_GETARG_UUID_P(X)
char * text_to_cstring(const text *t)