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  * 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: %s", "MD5",
323  pg_cryptohash_error(ctx));
324  if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
325  pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
326  elog(ERROR, "could not update %s context: %s", "MD5",
327  pg_cryptohash_error(ctx));
328  /* we assume sizeof MD5 result is 16, same as UUID size */
329  if (pg_cryptohash_final(ctx, (unsigned char *) &uu,
330  sizeof(uu)) < 0)
331  elog(ERROR, "could not finalize %s context: %s", "MD5",
332  pg_cryptohash_error(ctx));
333  pg_cryptohash_free(ctx);
334  }
335  else
336  {
338  unsigned char sha1result[SHA1_DIGEST_LENGTH];
339 
340  if (pg_cryptohash_init(ctx) < 0)
341  elog(ERROR, "could not initialize %s context: %s", "SHA1",
342  pg_cryptohash_error(ctx));
343  if (pg_cryptohash_update(ctx, ns, sizeof(uu)) < 0 ||
344  pg_cryptohash_update(ctx, (unsigned char *) ptr, len) < 0)
345  elog(ERROR, "could not update %s context: %s", "SHA1",
346  pg_cryptohash_error(ctx));
347  if (pg_cryptohash_final(ctx, sha1result, sizeof(sha1result)) < 0)
348  elog(ERROR, "could not finalize %s context: %s", "SHA1",
349  pg_cryptohash_error(ctx));
350  pg_cryptohash_free(ctx);
351 
352  memcpy(&uu, sha1result, sizeof(uu));
353  }
354 
355  /* the calculated hash is using local order */
356  UUID_TO_NETWORK(uu);
357  UUID_V3_OR_V5(uu, v);
358 
359 #ifdef HAVE_UUID_E2FS
360  /* uuid_unparse expects local order */
361  UUID_TO_LOCAL(uu);
362  uuid_unparse((unsigned char *) &uu, strbuf);
363 #else /* BSD */
364  uuid_to_string(&uu, &str, &status);
365 
366  if (status == uuid_s_ok)
367  strlcpy(strbuf, str, 37);
368 
369  if (str)
370  free(str);
371 
372  if (status != uuid_s_ok)
373  ereport(ERROR,
374  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
375  errmsg("uuid library failure: %d",
376  (int) status)));
377 #endif
378  break;
379  }
380 
381  case 4: /* random uuid */
382  default:
383  {
384 #ifdef HAVE_UUID_E2FS
385  uuid_t uu;
386 
387  uuid_generate_random(uu);
388  uuid_unparse(uu, strbuf);
389 #else /* BSD */
390  snprintf(strbuf, sizeof(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());
398 #endif
399  break;
400  }
401  }
402 
404 }
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:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
#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:229
#define snprintf
Definition: port.h:225
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define CStringGetDatum(X)
Definition: postgres.h:622
#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 470 of file uuid-ossp.c.

471 {
472  return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL, 0);
473 }
#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 477 of file uuid-ossp.c.

478 {
479 #ifdef HAVE_UUID_OSSP
480  char *buf = NULL;
481 #elif defined(HAVE_UUID_E2FS)
482  char strbuf[40];
483  char *buf;
484  uuid_t uu;
485 
486  uuid_generate_random(uu);
487 
488  /* set IEEE802 multicast and local-admin bits */
489  ((dce_uuid_t *) &uu)->node[0] |= 0x03;
490 
491  uuid_unparse(uu, strbuf);
492  buf = strbuf + 24;
493 #else /* BSD */
494  char buf[16];
495 
496  /* set IEEE802 multicast and local-admin bits */
497  snprintf(buf, sizeof(buf), "-%04x%08lx",
498  (unsigned) ((arc4random() & 0xffff) | 0x0300),
499  (unsigned long) arc4random());
500 #endif
501 
503  buf, 13);
504 }
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 508 of file uuid-ossp.c.

509 {
510  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
512 
513 #ifdef HAVE_UUID_OSSP
514  return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
515 #else
516  return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
518 #endif
519 }
const char * name
Definition: encode.c:561
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: uuid.h:21
Definition: c.h:622
#define UUID_MAKE_V3
Definition: uuid-ossp.c:54
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:29

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

524 {
525  return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL, 0);
526 }
#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 530 of file uuid-ossp.c.

531 {
532  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
534 
535 #ifdef HAVE_UUID_OSSP
536  return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
537 #else
538  return uuid_generate_internal(UUID_MAKE_V5, (unsigned char *) ns,
540 #endif
541 }
#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 410 of file uuid-ossp.c.

411 {
412 #ifdef HAVE_UUID_OSSP
413  return special_uuid_value("nil");
414 #else
415  return uuid_generate_internal(0, NULL,
416  "00000000-0000-0000-0000-000000000000", 36);
417 #endif
418 }

References uuid_generate_internal().

◆ uuid_ns_dns()

Datum uuid_ns_dns ( PG_FUNCTION_ARGS  )

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

423 {
424 #ifdef HAVE_UUID_OSSP
425  return special_uuid_value("ns:DNS");
426 #else
427  return uuid_generate_internal(0, NULL,
428  "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
429 #endif
430 }

References uuid_generate_internal().

◆ uuid_ns_oid()

Datum uuid_ns_oid ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

◆ uuid_ns_url()

Datum uuid_ns_url ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

◆ uuid_ns_x500()

Datum uuid_ns_x500 ( PG_FUNCTION_ARGS  )

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

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

References uuid_generate_internal().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

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