PostgreSQL Source Code  git master
uuid-ossp.c File Reference
#include "postgres.h"
#include "common/cryptohash.h"
#include "common/sha1.h"
#include "fmgr.h"
#include "port/pg_bswap.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
#include "varatt.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 75 of file uuid-ossp.c.

◆ uuid_hash

#define uuid_hash   bsd_uuid_hash

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

◆ UUID_MAKE_MC

#define UUID_MAKE_MC   0

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

◆ UUID_MAKE_V1

#define UUID_MAKE_V1   1

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

◆ UUID_MAKE_V2

#define UUID_MAKE_V2   2

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

◆ UUID_MAKE_V3

#define UUID_MAKE_V3   3

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

◆ UUID_MAKE_V4

#define UUID_MAKE_V4   4

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

◆ UUID_MAKE_V5

#define UUID_MAKE_V5   5

Definition at line 57 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 88 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 81 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 95 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 249 of file uuid-ossp.c.

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

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, 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 481 of file uuid-ossp.c.

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

References uuid_generate_internal(), and UUID_MAKE_V1.

◆ uuid_generate_v1mc()

Datum uuid_generate_v1mc ( PG_FUNCTION_ARGS  )

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

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

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 519 of file uuid-ossp.c.

520 {
521  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
523 
524 #ifdef HAVE_UUID_OSSP
525  return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
526 #else
527  return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
529 #endif
530 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
Definition: uuid.h:21
Definition: c.h:687
#define UUID_MAKE_V3
Definition: uuid-ossp.c:55
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:40
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
const char * name

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 534 of file uuid-ossp.c.

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

References uuid_generate_internal(), and UUID_MAKE_V4.

◆ uuid_generate_v5()

Datum uuid_generate_v5 ( PG_FUNCTION_ARGS  )

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

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

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 421 of file uuid-ossp.c.

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

References uuid_generate_internal().

◆ uuid_ns_dns()

Datum uuid_ns_dns ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

◆ uuid_ns_oid()

Datum uuid_ns_oid ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

◆ uuid_ns_url()

Datum uuid_ns_url ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

◆ uuid_ns_x500()

Datum uuid_ns_x500 ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

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