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.

Referenced by uuid_generate_internal(), and uuid_generate_v1mc().

◆ 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.

Referenced by uuid_generate_v1mc().

◆ UUID_MAKE_V1

#define UUID_MAKE_V1   1

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

Referenced by uuid_generate_v1(), and uuid_generate_v1mc().

◆ 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.

Referenced by uuid_generate_v3().

◆ UUID_MAKE_V4

#define UUID_MAKE_V4   4

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

Referenced by uuid_generate_v4().

◆ UUID_MAKE_V5

#define UUID_MAKE_V5   5

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

Referenced by uuid_generate_v5().

◆ 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_ntoh16(x)
Definition: pg_bswap.h:124
#define pg_ntoh32(x)
Definition: pg_bswap.h:125

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

Referenced by uuid_generate_internal().

◆ 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_hton16(x)
Definition: pg_bswap.h:120
#define pg_hton32(x)
Definition: pg_bswap.h:121

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

Referenced by uuid_generate_internal().

◆ 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.

Referenced by uuid_generate_internal().

Function Documentation

◆ PG_FUNCTION_INFO_V1() [1/10]

PG_FUNCTION_INFO_V1 ( uuid_nil  )

◆ PG_FUNCTION_INFO_V1() [2/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_dns  )

◆ PG_FUNCTION_INFO_V1() [3/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_url  )

◆ PG_FUNCTION_INFO_V1() [4/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_oid  )

◆ PG_FUNCTION_INFO_V1() [5/10]

PG_FUNCTION_INFO_V1 ( uuid_ns_x500  )

◆ PG_FUNCTION_INFO_V1() [6/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v1  )

◆ PG_FUNCTION_INFO_V1() [7/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v1mc  )

◆ PG_FUNCTION_INFO_V1() [8/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v3  )

◆ PG_FUNCTION_INFO_V1() [9/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v4  )

◆ PG_FUNCTION_INFO_V1() [10/10]

PG_FUNCTION_INFO_V1 ( uuid_generate_v5  )

◆ 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.

References CStringGetDatum, dce_uuid_t, DirectFunctionCall1, elog, ereport, errcode(), errmsg(), ERROR, free, pg_cryptohash_create(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), PG_MD5, PG_SHA1, SHA1_DIGEST_LENGTH, snprintf, status(), 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().

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  * PTR, if set, replaces the trailing characters of
289  * the uuid; this is to support v1mc, where a random
290  * multicast MAC is used instead of the physical one
291  */
292  if (ptr && len <= 36)
293  strcpy(strbuf + (36 - len), ptr);
294  }
295  if (str)
296  free(str);
297  }
298 
299  if (status != uuid_s_ok)
300  ereport(ERROR,
301  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
302  errmsg("uuid library failure: %d",
303  (int) status)));
304 #endif
305  break;
306  }
307 
308  case 3: /* namespace-based MD5 uuids */
309  case 5: /* namespace-based SHA1 uuids */
310  {
311  dce_uuid_t uu;
312 #ifdef HAVE_UUID_BSD
313  uint32_t status = uuid_s_ok;
314  char *str = NULL;
315 #endif
316 
317  if (v == 3)
318  {
320 
321  if (pg_cryptohash_init(ctx) < 0)
322  elog(ERROR, "could not initialize %s context", "MD5");
323  if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
324  pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
325  elog(ERROR, "could not update %s context", "MD5");
326  /* we assume sizeof MD5 result is 16, same as UUID size */
327  if (pg_cryptohash_final(ctx, (unsigned char *) &uu,
328  sizeof(uu)) < 0)
329  elog(ERROR, "could not finalize %s context", "MD5");
330  pg_cryptohash_free(ctx);
331  }
332  else
333  {
335  unsigned char sha1result[SHA1_DIGEST_LENGTH];
336 
337  if (pg_cryptohash_init(ctx) < 0)
338  elog(ERROR, "could not initialize %s context", "SHA1");
339  if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
340  pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
341  elog(ERROR, "could not update %s context", "SHA1");
342  if (pg_cryptohash_final(ctx, sha1result, sizeof(sha1result)) < 0)
343  elog(ERROR, "could not finalize %s context", "SHA1");
344  pg_cryptohash_free(ctx);
345 
346  memcpy(&uu, sha1result, sizeof(uu));
347  }
348 
349  /* the calculated hash is using local order */
350  UUID_TO_NETWORK(uu);
351  UUID_V3_OR_V5(uu, v);
352 
353 #ifdef HAVE_UUID_E2FS
354  /* uuid_unparse expects local order */
355  UUID_TO_LOCAL(uu);
356  uuid_unparse((unsigned char *) &uu, strbuf);
357 #else /* BSD */
358  uuid_to_string(&uu, &str, &status);
359 
360  if (status == uuid_s_ok)
361  strlcpy(strbuf, str, 37);
362 
363  if (str)
364  free(str);
365 
366  if (status != uuid_s_ok)
367  ereport(ERROR,
368  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
369  errmsg("uuid library failure: %d",
370  (int) status)));
371 #endif
372  break;
373  }
374 
375  case 4: /* random uuid */
376  default:
377  {
378 #ifdef HAVE_UUID_E2FS
379  uuid_t uu;
380 
381  uuid_generate_random(uu);
382  uuid_unparse(uu, strbuf);
383 #else /* BSD */
384  snprintf(strbuf, sizeof(strbuf),
385  "%08lx-%04x-%04x-%04x-%04x%08lx",
386  (unsigned long) arc4random(),
387  (unsigned) (arc4random() & 0xffff),
388  (unsigned) ((arc4random() & 0xfff) | 0x4000),
389  (unsigned) ((arc4random() & 0x3fff) | 0x8000),
390  (unsigned) (arc4random() & 0xffff),
391  (unsigned long) arc4random());
392 #endif
393  break;
394  }
395  }
396 
398 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
Datum uuid_in(PG_FUNCTION_ARGS)
Definition: uuid.c:42
#define UUID_V3_OR_V5(uu, v)
Definition: uuid-ossp.c:94
#define SHA1_DIGEST_LENGTH
Definition: sha1.h:17
#define dce_uuid_t
Definition: uuid-ossp.c:74
int errcode(int sqlerrcode)
Definition: elog.c:698
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define UUID_TO_NETWORK(uu)
Definition: uuid-ossp.c:80
#define ERROR
Definition: elog.h:46
#define CStringGetDatum(X)
Definition: postgres.h:622
#define ereport(elevel,...)
Definition: elog.h:157
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define UUID_TO_LOCAL(uu)
Definition: uuid-ossp.c:87
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216
#define snprintf
Definition: port.h:216

◆ uuid_generate_v1()

Datum uuid_generate_v1 ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal(), and UUID_MAKE_V1.

465 {
466  return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL, 0);
467 }
#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

◆ uuid_generate_v1mc()

Datum uuid_generate_v1mc ( PG_FUNCTION_ARGS  )

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

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

472 {
473 #ifdef HAVE_UUID_OSSP
474  char *buf = NULL;
475 #elif defined(HAVE_UUID_E2FS)
476  char strbuf[40];
477  char *buf;
478  uuid_t uu;
479 
480  uuid_generate_random(uu);
481 
482  /* set IEEE802 multicast and local-admin bits */
483  ((dce_uuid_t *) &uu)->node[0] |= 0x03;
484 
485  uuid_unparse(uu, strbuf);
486  buf = strbuf + 24;
487 #else /* BSD */
488  char buf[16];
489 
490  /* set IEEE802 multicast and local-admin bits */
491  snprintf(buf, sizeof(buf), "-%04x%08lx",
492  (unsigned) ((arc4random() & 0xffff) | 0x0300),
493  (unsigned long) arc4random());
494 #endif
495 
497  buf, 13);
498 }
#define dce_uuid_t
Definition: uuid-ossp.c:74
static char * buf
Definition: pg_test_fsync.c:68
#define UUID_MAKE_MC
Definition: uuid-ossp.c:51
#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
#define snprintf
Definition: port.h:216

◆ uuid_generate_v3()

Datum uuid_generate_v3 ( PG_FUNCTION_ARGS  )

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

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

503 {
504  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
506 
507 #ifdef HAVE_UUID_OSSP
508  return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
509 #else
510  return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
511  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
512 #endif
513 }
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:29
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define UUID_MAKE_V3
Definition: uuid-ossp.c:54
const char * name
Definition: encode.c:515
Definition: uuid.h:20
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248
Definition: c.h:621

◆ uuid_generate_v4()

Datum uuid_generate_v4 ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal(), and UUID_MAKE_V4.

518 {
519  return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL, 0);
520 }
#define UUID_MAKE_V4
Definition: uuid-ossp.c:55
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

◆ uuid_generate_v5()

Datum uuid_generate_v5 ( PG_FUNCTION_ARGS  )

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

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

525 {
526  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
528 
529 #ifdef HAVE_UUID_OSSP
530  return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
531 #else
532  return uuid_generate_internal(UUID_MAKE_V5, (unsigned char *) ns,
533  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
534 #endif
535 }
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:29
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define UUID_MAKE_V5
Definition: uuid-ossp.c:56
const char * name
Definition: encode.c:515
Definition: uuid.h:20
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248
Definition: c.h:621

◆ uuid_nil()

Datum uuid_nil ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

405 {
406 #ifdef HAVE_UUID_OSSP
407  return special_uuid_value("nil");
408 #else
409  return uuid_generate_internal(0, NULL,
410  "00000000-0000-0000-0000-000000000000", 36);
411 #endif
412 }
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

◆ uuid_ns_dns()

Datum uuid_ns_dns ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

417 {
418 #ifdef HAVE_UUID_OSSP
419  return special_uuid_value("ns:DNS");
420 #else
421  return uuid_generate_internal(0, NULL,
422  "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
423 #endif
424 }
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

◆ uuid_ns_oid()

Datum uuid_ns_oid ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

441 {
442 #ifdef HAVE_UUID_OSSP
443  return special_uuid_value("ns:OID");
444 #else
445  return uuid_generate_internal(0, NULL,
446  "6ba7b812-9dad-11d1-80b4-00c04fd430c8", 36);
447 #endif
448 }
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

◆ uuid_ns_url()

Datum uuid_ns_url ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

429 {
430 #ifdef HAVE_UUID_OSSP
431  return special_uuid_value("ns:URL");
432 #else
433  return uuid_generate_internal(0, NULL,
434  "6ba7b811-9dad-11d1-80b4-00c04fd430c8", 36);
435 #endif
436 }
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

◆ uuid_ns_x500()

Datum uuid_ns_x500 ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

453 {
454 #ifdef HAVE_UUID_OSSP
455  return special_uuid_value("ns:X500");
456 #else
457  return uuid_generate_internal(0, NULL,
458  "6ba7b814-9dad-11d1-80b4-00c04fd430c8", 36);
459 #endif
460 }
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:248

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

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