PostgreSQL Source Code  git master
uuid-ossp.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "common/cryptohash.h"
#include "common/sha1.h"
#include "port/pg_bswap.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
Include dependency graph for uuid-ossp.c:

Go to the source code of this file.

Macros

#define uuid_hash   bsd_uuid_hash
 
#define UUID_MAKE_MC   0
 
#define UUID_MAKE_V1   1
 
#define UUID_MAKE_V2   2
 
#define UUID_MAKE_V3   3
 
#define UUID_MAKE_V4   4
 
#define UUID_MAKE_V5   5
 
#define dce_uuid_t   uuid_t
 
#define UUID_TO_NETWORK(uu)
 
#define UUID_TO_LOCAL(uu)
 
#define UUID_V3_OR_V5(uu, v)
 

Functions

 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 Datum uuid_generate_internal (int v, unsigned char *ns, const char *ptr, int len)
 
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)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ dce_uuid_t

#define dce_uuid_t   uuid_t

Definition at line 74 of file uuid-ossp.c.

◆ uuid_hash

#define uuid_hash   bsd_uuid_hash

Definition at line 30 of file uuid-ossp.c.

◆ UUID_MAKE_MC

#define UUID_MAKE_MC   0

Definition at line 51 of file uuid-ossp.c.

◆ UUID_MAKE_V1

#define UUID_MAKE_V1   1

Definition at line 52 of file uuid-ossp.c.

◆ UUID_MAKE_V2

#define UUID_MAKE_V2   2

Definition at line 53 of file uuid-ossp.c.

◆ UUID_MAKE_V3

#define UUID_MAKE_V3   3

Definition at line 54 of file uuid-ossp.c.

◆ UUID_MAKE_V4

#define UUID_MAKE_V4   4

Definition at line 55 of file uuid-ossp.c.

◆ UUID_MAKE_V5

#define UUID_MAKE_V5   5

Definition at line 56 of file uuid-ossp.c.

◆ UUID_TO_LOCAL

#define UUID_TO_LOCAL (   uu)
Value:
do { \
uu.time_low = pg_ntoh32(uu.time_low); \
uu.time_mid = pg_ntoh16(uu.time_mid); \
uu.time_hi_and_version = pg_ntoh16(uu.time_hi_and_version); \
} while (0)
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define pg_ntoh16(x)
Definition: pg_bswap.h:124

Definition at line 87 of file uuid-ossp.c.

◆ UUID_TO_NETWORK

#define UUID_TO_NETWORK (   uu)
Value:
do { \
uu.time_low = pg_hton32(uu.time_low); \
uu.time_mid = pg_hton16(uu.time_mid); \
uu.time_hi_and_version = pg_hton16(uu.time_hi_and_version); \
} while (0)
#define pg_hton32(x)
Definition: pg_bswap.h:121
#define pg_hton16(x)
Definition: pg_bswap.h:120

Definition at line 80 of file uuid-ossp.c.

◆ UUID_V3_OR_V5

#define UUID_V3_OR_V5 (   uu,
 
)
Value:
do { \
uu.time_hi_and_version &= 0x0FFF; \
uu.time_hi_and_version |= (v << 12); \
uu.clock_seq_hi_and_reserved &= 0x3F; \
uu.clock_seq_hi_and_reserved |= 0x80; \
} while(0)

Definition at line 94 of file uuid-ossp.c.

Function Documentation

◆ PG_FUNCTION_INFO_V1() [1/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v1  )

◆ PG_FUNCTION_INFO_V1() [2/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v1mc  )

◆ PG_FUNCTION_INFO_V1() [3/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v3  )

◆ PG_FUNCTION_INFO_V1() [4/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v4  )

◆ PG_FUNCTION_INFO_V1() [5/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v5  )

◆ PG_FUNCTION_INFO_V1() [6/10]

PG_FUNCTION_INFO_V1 ( uuid_nil  )

◆ PG_FUNCTION_INFO_V1() [7/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_dns  )

◆ PG_FUNCTION_INFO_V1() [8/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_oid  )

◆ PG_FUNCTION_INFO_V1() [9/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_url  )

◆ PG_FUNCTION_INFO_V1() [10/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_x500  )

◆ uuid_generate_internal()

static Datum uuid_generate_internal ( int  v,
unsigned char *  ns,
const char *  ptr,
int  len 
)
static

Definition at line 248 of file uuid-ossp.c.

249 {
250  char strbuf[40];
251 
252  switch (v)
253  {
254  case 0: /* constant-value uuids */
255  strlcpy(strbuf, ptr, 37);
256  break;
257 
258  case 1: /* time/node-based uuids */
259  {
260 #ifdef HAVE_UUID_E2FS
261  uuid_t uu;
262 
263  uuid_generate_time(uu);
264  uuid_unparse(uu, strbuf);
265 
266  /*
267  * PTR, if set, replaces the trailing characters of the uuid;
268  * this is to support v1mc, where a random multicast MAC is
269  * used instead of the physical one
270  */
271  if (ptr && len <= 36)
272  strcpy(strbuf + (36 - len), ptr);
273 #else /* BSD */
274  uuid_t uu;
275  uint32_t status = uuid_s_ok;
276  char *str = NULL;
277 
278  uuid_create(&uu, &status);
279 
280  if (status == uuid_s_ok)
281  {
282  uuid_to_string(&uu, &str, &status);
283  if (status == uuid_s_ok)
284  {
285  strlcpy(strbuf, str, 37);
286 
287  /*
288  * In recent NetBSD, uuid_create() has started
289  * producing v4 instead of v1 UUIDs. Check the
290  * version field and complain if it's not v1.
291  */
292  if (strbuf[14] != '1')
293  ereport(ERROR,
294  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
295  /* translator: %c will be a hex digit */
296  errmsg("uuid_create() produced a version %c UUID instead of the expected version 1",
297  strbuf[14])));
298 
299  /*
300  * PTR, if set, replaces the trailing characters of
301  * the uuid; this is to support v1mc, where a random
302  * multicast MAC is used instead of the physical one
303  */
304  if (ptr && len <= 36)
305  strcpy(strbuf + (36 - len), ptr);
306  }
307  free(str);
308  }
309 
310  if (status != uuid_s_ok)
311  ereport(ERROR,
312  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
313  errmsg("uuid library failure: %d",
314  (int) status)));
315 #endif
316  break;
317  }
318 
319  case 3: /* namespace-based MD5 uuids */
320  case 5: /* namespace-based SHA1 uuids */
321  {
322  dce_uuid_t uu;
323 #ifdef HAVE_UUID_BSD
324  uint32_t status = uuid_s_ok;
325  char *str = NULL;
326 #endif
327 
328  if (v == 3)
329  {
331 
332  if (pg_cryptohash_init(ctx) < 0)
333  elog(ERROR, "could not initialize %s context: %s", "MD5",
334  pg_cryptohash_error(ctx));
335  if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
336  pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
337  elog(ERROR, "could not update %s context: %s", "MD5",
338  pg_cryptohash_error(ctx));
339  /* we assume sizeof MD5 result is 16, same as UUID size */
340  if (pg_cryptohash_final(ctx, (unsigned char *) &uu,
341  sizeof(uu)) < 0)
342  elog(ERROR, "could not finalize %s context: %s", "MD5",
343  pg_cryptohash_error(ctx));
344  pg_cryptohash_free(ctx);
345  }
346  else
347  {
349  unsigned char sha1result[SHA1_DIGEST_LENGTH];
350 
351  if (pg_cryptohash_init(ctx) < 0)
352  elog(ERROR, "could not initialize %s context: %s", "SHA1",
353  pg_cryptohash_error(ctx));
354  if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
355  pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
356  elog(ERROR, "could not update %s context: %s", "SHA1",
357  pg_cryptohash_error(ctx));
358  if (pg_cryptohash_final(ctx, sha1result, sizeof(sha1result)) < 0)
359  elog(ERROR, "could not finalize %s context: %s", "SHA1",
360  pg_cryptohash_error(ctx));
361  pg_cryptohash_free(ctx);
362 
363  memcpy(&uu, sha1result, sizeof(uu));
364  }
365 
366  /* the calculated hash is using local order */
367  UUID_TO_NETWORK(uu);
368  UUID_V3_OR_V5(uu, v);
369 
370 #ifdef HAVE_UUID_E2FS
371  /* uuid_unparse expects local order */
372  UUID_TO_LOCAL(uu);
373  uuid_unparse((unsigned char *) &uu, strbuf);
374 #else /* BSD */
375  uuid_to_string(&uu, &str, &status);
376 
377  if (status == uuid_s_ok)
378  strlcpy(strbuf, str, 37);
379 
380  free(str);
381 
382  if (status != uuid_s_ok)
383  ereport(ERROR,
384  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
385  errmsg("uuid library failure: %d",
386  (int) status)));
387 #endif
388  break;
389  }
390 
391  case 4: /* random uuid */
392  default:
393  {
394 #ifdef HAVE_UUID_E2FS
395  uuid_t uu;
396 
397  uuid_generate_random(uu);
398  uuid_unparse(uu, strbuf);
399 #else /* BSD */
400  snprintf(strbuf, sizeof(strbuf),
401  "%08lx-%04x-%04x-%04x-%04x%08lx",
402  (unsigned long) arc4random(),
403  (unsigned) (arc4random() & 0xffff),
404  (unsigned) ((arc4random() & 0xfff) | 0x4000),
405  (unsigned) ((arc4random() & 0x3fff) | 0x8000),
406  (unsigned) (arc4random() & 0xffff),
407  (unsigned long) arc4random());
408 #endif
409  break;
410  }
411  }
412 
414 }
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:139
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:101
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:243
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:177
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:259
@ PG_SHA1
Definition: cryptohash.h:22
@ PG_MD5
Definition: cryptohash.h:21
int errcode(int sqlerrcode)
Definition: elog.c:735
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define free(a)
Definition: header.h:65
const void size_t len
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define snprintf
Definition: port.h:238
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:698
#define SHA1_DIGEST_LENGTH
Definition: sha1.h:17
#define dce_uuid_t
Definition: uuid-ossp.c:74
#define UUID_V3_OR_V5(uu, v)
Definition: uuid-ossp.c:94
#define UUID_TO_LOCAL(uu)
Definition: uuid-ossp.c:87
#define UUID_TO_NETWORK(uu)
Definition: uuid-ossp.c:80
Datum uuid_in(PG_FUNCTION_ARGS)
Definition: uuid.c:41

References CStringGetDatum(), dce_uuid_t, DirectFunctionCall1, elog(), ereport, errcode(), errmsg(), ERROR, free, len, pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), PG_MD5, PG_SHA1, SHA1_DIGEST_LENGTH, snprintf, status(), generate_unaccent_rules::str, strlcpy(), uuid_in(), UUID_TO_LOCAL, UUID_TO_NETWORK, and UUID_V3_OR_V5.

Referenced by uuid_generate_v1(), uuid_generate_v1mc(), uuid_generate_v3(), uuid_generate_v4(), uuid_generate_v5(), uuid_nil(), uuid_ns_dns(), uuid_ns_oid(), uuid_ns_url(), and uuid_ns_x500().

◆ uuid_generate_v1()

Datum uuid_generate_v1 ( PG_FUNCTION_ARGS  )

Definition at line 480 of file uuid-ossp.c.

481 {
482  return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL, 0);
483 }
#define UUID_MAKE_V1
Definition: uuid-ossp.c:52
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

References uuid_generate_internal(), and UUID_MAKE_V1.

◆ uuid_generate_v1mc()

Datum uuid_generate_v1mc ( PG_FUNCTION_ARGS  )

Definition at line 487 of file uuid-ossp.c.

488 {
489 #ifdef HAVE_UUID_OSSP
490  char *buf = NULL;
491 #elif defined(HAVE_UUID_E2FS)
492  char strbuf[40];
493  char *buf;
494  uuid_t uu;
495 
496  uuid_generate_random(uu);
497 
498  /* set IEEE802 multicast and local-admin bits */
499  ((dce_uuid_t *) &uu)->node[0] |= 0x03;
500 
501  uuid_unparse(uu, strbuf);
502  buf = strbuf + 24;
503 #else /* BSD */
504  char buf[16];
505 
506  /* set IEEE802 multicast and local-admin bits */
507  snprintf(buf, sizeof(buf), "-%04x%08lx",
508  (unsigned) ((arc4random() & 0xffff) | 0x0300),
509  (unsigned long) arc4random());
510 #endif
511 
513  buf, 13);
514 }
static char * buf
Definition: pg_test_fsync.c:67
#define UUID_MAKE_MC
Definition: uuid-ossp.c:51

References buf, dce_uuid_t, snprintf, uuid_generate_internal(), UUID_MAKE_MC, and UUID_MAKE_V1.

◆ uuid_generate_v3()

Datum uuid_generate_v3 ( PG_FUNCTION_ARGS  )

Definition at line 518 of file uuid-ossp.c.

519 {
520  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
522 
523 #ifdef HAVE_UUID_OSSP
524  return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
525 #else
526  return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
528 #endif
529 }
const char * name
Definition: encode.c:561
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define VARDATA_ANY(PTR)
Definition: postgres.h:362
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:355
Definition: uuid.h:21
Definition: c.h:623
#define UUID_MAKE_V3
Definition: uuid-ossp.c:54
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:40

References name, PG_GETARG_TEXT_PP, PG_GETARG_UUID_P, uuid_generate_internal(), UUID_MAKE_V3, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ uuid_generate_v4()

Datum uuid_generate_v4 ( PG_FUNCTION_ARGS  )

Definition at line 533 of file uuid-ossp.c.

534 {
535  return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL, 0);
536 }
#define UUID_MAKE_V4
Definition: uuid-ossp.c:55

References uuid_generate_internal(), and UUID_MAKE_V4.

◆ uuid_generate_v5()

Datum uuid_generate_v5 ( PG_FUNCTION_ARGS  )

Definition at line 540 of file uuid-ossp.c.

541 {
542  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
544 
545 #ifdef HAVE_UUID_OSSP
546  return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
547 #else
548  return uuid_generate_internal(UUID_MAKE_V5, (unsigned char *) ns,
550 #endif
551 }
#define UUID_MAKE_V5
Definition: uuid-ossp.c:56

References name, PG_GETARG_TEXT_PP, PG_GETARG_UUID_P, uuid_generate_internal(), UUID_MAKE_V5, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ uuid_nil()

Datum uuid_nil ( PG_FUNCTION_ARGS  )

Definition at line 420 of file uuid-ossp.c.

421 {
422 #ifdef HAVE_UUID_OSSP
423  return special_uuid_value("nil");
424 #else
425  return uuid_generate_internal(0, NULL,
426  "00000000-0000-0000-0000-000000000000", 36);
427 #endif
428 }

References uuid_generate_internal().

◆ uuid_ns_dns()

Datum uuid_ns_dns ( PG_FUNCTION_ARGS  )

Definition at line 432 of file uuid-ossp.c.

433 {
434 #ifdef HAVE_UUID_OSSP
435  return special_uuid_value("ns:DNS");
436 #else
437  return uuid_generate_internal(0, NULL,
438  "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
439 #endif
440 }

References uuid_generate_internal().

◆ uuid_ns_oid()

Datum uuid_ns_oid ( PG_FUNCTION_ARGS  )

Definition at line 456 of file uuid-ossp.c.

457 {
458 #ifdef HAVE_UUID_OSSP
459  return special_uuid_value("ns:OID");
460 #else
461  return uuid_generate_internal(0, NULL,
462  "6ba7b812-9dad-11d1-80b4-00c04fd430c8", 36);
463 #endif
464 }

References uuid_generate_internal().

◆ uuid_ns_url()

Datum uuid_ns_url ( PG_FUNCTION_ARGS  )

Definition at line 444 of file uuid-ossp.c.

445 {
446 #ifdef HAVE_UUID_OSSP
447  return special_uuid_value("ns:URL");
448 #else
449  return uuid_generate_internal(0, NULL,
450  "6ba7b811-9dad-11d1-80b4-00c04fd430c8", 36);
451 #endif
452 }

References uuid_generate_internal().

◆ uuid_ns_x500()

Datum uuid_ns_x500 ( PG_FUNCTION_ARGS  )

Definition at line 468 of file uuid-ossp.c.

469 {
470 #ifdef HAVE_UUID_OSSP
471  return special_uuid_value("ns:X500");
472 #else
473  return uuid_generate_internal(0, NULL,
474  "6ba7b814-9dad-11d1-80b4-00c04fd430c8", 36);
475 #endif
476 }

References uuid_generate_internal().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 104 of file uuid-ossp.c.