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

◆ UUID_MAKE_MC

#define UUID_MAKE_MC   0

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

Referenced by uuid_generate_v1mc().

◆ UUID_MAKE_V1

#define UUID_MAKE_V1   1

Definition at line 60 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 61 of file uuid-ossp.c.

◆ UUID_MAKE_V3

#define UUID_MAKE_V3   3

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

Referenced by uuid_generate_v3().

◆ UUID_MAKE_V4

#define UUID_MAKE_V4   4

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

Referenced by uuid_generate_v4().

◆ UUID_MAKE_V5

#define UUID_MAKE_V5   5

Definition at line 64 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 95 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 88 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 102 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 256 of file uuid-ossp.c.

References CStringGetDatum, dce_uuid_t, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, free, MD5_CTX, MD5Final, MD5Init, MD5Update, SHA1_RESULTLEN, SHA1Final, SHA1Init, SHA1Update, 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().

257 {
258  char strbuf[40];
259 
260  switch (v)
261  {
262  case 0: /* constant-value uuids */
263  strlcpy(strbuf, ptr, 37);
264  break;
265 
266  case 1: /* time/node-based uuids */
267  {
268 #ifdef HAVE_UUID_E2FS
269  uuid_t uu;
270 
271  uuid_generate_time(uu);
272  uuid_unparse(uu, strbuf);
273 
274  /*
275  * PTR, if set, replaces the trailing characters of the uuid;
276  * this is to support v1mc, where a random multicast MAC is
277  * used instead of the physical one
278  */
279  if (ptr && len <= 36)
280  strcpy(strbuf + (36 - len), ptr);
281 #else /* BSD */
282  uuid_t uu;
283  uint32_t status = uuid_s_ok;
284  char *str = NULL;
285 
286  uuid_create(&uu, &status);
287 
288  if (status == uuid_s_ok)
289  {
290  uuid_to_string(&uu, &str, &status);
291  if (status == uuid_s_ok)
292  {
293  strlcpy(strbuf, str, 37);
294 
295  /*
296  * PTR, if set, replaces the trailing characters of
297  * the uuid; this is to support v1mc, where a random
298  * multicast MAC is used instead of the physical one
299  */
300  if (ptr && len <= 36)
301  strcpy(strbuf + (36 - len), ptr);
302  }
303  if (str)
304  free(str);
305  }
306 
307  if (status != uuid_s_ok)
308  ereport(ERROR,
309  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
310  errmsg("uuid library failure: %d",
311  (int) status)));
312 #endif
313  break;
314  }
315 
316  case 3: /* namespace-based MD5 uuids */
317  case 5: /* namespace-based SHA1 uuids */
318  {
319  dce_uuid_t uu;
320 #ifdef HAVE_UUID_BSD
321  uint32_t status = uuid_s_ok;
322  char *str = NULL;
323 #endif
324 
325  if (v == 3)
326  {
327  MD5_CTX ctx;
328 
329  MD5Init(&ctx);
330  MD5Update(&ctx, ns, sizeof(uu));
331  MD5Update(&ctx, (unsigned char *) ptr, len);
332  /* we assume sizeof MD5 result is 16, same as UUID size */
333  MD5Final((unsigned char *) &uu, &ctx);
334  }
335  else
336  {
337  SHA1_CTX ctx;
338  unsigned char sha1result[SHA1_RESULTLEN];
339 
340  SHA1Init(&ctx);
341  SHA1Update(&ctx, ns, sizeof(uu));
342  SHA1Update(&ctx, (unsigned char *) ptr, len);
343  SHA1Final(sha1result, &ctx);
344  memcpy(&uu, sha1result, sizeof(uu));
345  }
346 
347  /* the calculated hash is using local order */
348  UUID_TO_NETWORK(uu);
349  UUID_V3_OR_V5(uu, v);
350 
351 #ifdef HAVE_UUID_E2FS
352  /* uuid_unparse expects local order */
353  UUID_TO_LOCAL(uu);
354  uuid_unparse((unsigned char *) &uu, strbuf);
355 #else /* BSD */
356  uuid_to_string(&uu, &str, &status);
357 
358  if (status == uuid_s_ok)
359  strlcpy(strbuf, str, 37);
360 
361  if (str)
362  free(str);
363 
364  if (status != uuid_s_ok)
365  ereport(ERROR,
366  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
367  errmsg("uuid library failure: %d",
368  (int) status)));
369 #endif
370  break;
371  }
372 
373  case 4: /* random uuid */
374  default:
375  {
376 #ifdef HAVE_UUID_E2FS
377  uuid_t uu;
378 
379  uuid_generate_random(uu);
380  uuid_unparse(uu, strbuf);
381 #else /* BSD */
382  snprintf(strbuf, sizeof(strbuf),
383  "%08lx-%04x-%04x-%04x-%04x%08lx",
384  (unsigned long) arc4random(),
385  (unsigned) (arc4random() & 0xffff),
386  (unsigned) ((arc4random() & 0xfff) | 0x4000),
387  (unsigned) ((arc4random() & 0x3fff) | 0x8000),
388  (unsigned) (arc4random() & 0xffff),
389  (unsigned long) arc4random());
390 #endif
391  break;
392  }
393  }
394 
396 }
#define MD5Final(x, y)
Definition: md5.h:73
#define MD5_CTX
Definition: md5.h:70
Datum uuid_in(PG_FUNCTION_ARGS)
Definition: uuid.c:42
#define UUID_V3_OR_V5(uu, v)
Definition: uuid-ossp.c:102
#define dce_uuid_t
Definition: uuid-ossp.c:82
#define MD5Init(x)
Definition: md5.h:71
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SHA1Init(x)
Definition: sha1.h:69
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
#define UUID_TO_NETWORK(uu)
Definition: uuid-ossp.c:88
#define MD5Update(x, y, z)
Definition: md5.h:72
#define ERROR
Definition: elog.h:43
Definition: sha1.h:41
#define CStringGetDatum(X)
Definition: postgres.h:584
#define SHA1_RESULTLEN
Definition: sha1.h:73
#define ereport(elevel, rest)
Definition: elog.h:122
#define SHA1Final(x, y)
Definition: sha1.h:71
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define SHA1Update(x, y, z)
Definition: sha1.h:70
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define UUID_TO_LOCAL(uu)
Definition: uuid-ossp.c:95
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ uuid_generate_v1()

Datum uuid_generate_v1 ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal(), and UUID_MAKE_V1.

463 {
464  return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL, 0);
465 }
#define UUID_MAKE_V1
Definition: uuid-ossp.c:60
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:256

◆ uuid_generate_v1mc()

Datum uuid_generate_v1mc ( PG_FUNCTION_ARGS  )

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

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

470 {
471 #ifdef HAVE_UUID_OSSP
472  char *buf = NULL;
473 #elif defined(HAVE_UUID_E2FS)
474  char strbuf[40];
475  char *buf;
476  uuid_t uu;
477 
478  uuid_generate_random(uu);
479 
480  /* set IEEE802 multicast and local-admin bits */
481  ((dce_uuid_t *) &uu)->node[0] |= 0x03;
482 
483  uuid_unparse(uu, strbuf);
484  buf = strbuf + 24;
485 #else /* BSD */
486  char buf[16];
487 
488  /* set IEEE802 multicast and local-admin bits */
489  snprintf(buf, sizeof(buf), "-%04x%08lx",
490  (unsigned) ((arc4random() & 0xffff) | 0x0300),
491  (unsigned long) arc4random());
492 #endif
493 
495  buf, 13);
496 }
#define dce_uuid_t
Definition: uuid-ossp.c:82
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:67
#define UUID_MAKE_MC
Definition: uuid-ossp.c:59
#define UUID_MAKE_V1
Definition: uuid-ossp.c:60
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:256

◆ uuid_generate_v3()

Datum uuid_generate_v3 ( PG_FUNCTION_ARGS  )

Definition at line 500 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.

501 {
502  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
504 
505 #ifdef HAVE_UUID_OSSP
506  return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
507 #else
508  return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
509  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
510 #endif
511 }
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:29
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define UUID_MAKE_V3
Definition: uuid-ossp.c:62
const char * name
Definition: encode.c:521
Definition: uuid.h:20
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:256
Definition: c.h:487

◆ uuid_generate_v4()

Datum uuid_generate_v4 ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal(), and UUID_MAKE_V4.

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

◆ uuid_generate_v5()

Datum uuid_generate_v5 ( PG_FUNCTION_ARGS  )

Definition at line 522 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.

523 {
524  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
526 
527 #ifdef HAVE_UUID_OSSP
528  return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
529 #else
530  return uuid_generate_internal(UUID_MAKE_V5, (unsigned char *) ns,
531  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
532 #endif
533 }
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:29
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define UUID_MAKE_V5
Definition: uuid-ossp.c:64
const char * name
Definition: encode.c:521
Definition: uuid.h:20
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static Datum uuid_generate_internal(int v, unsigned char *ns, const char *ptr, int len)
Definition: uuid-ossp.c:256
Definition: c.h:487

◆ uuid_nil()

Datum uuid_nil ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

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

◆ uuid_ns_dns()

Datum uuid_ns_dns ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

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

◆ uuid_ns_oid()

Datum uuid_ns_oid ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

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

◆ uuid_ns_url()

Datum uuid_ns_url ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

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

◆ uuid_ns_x500()

Datum uuid_ns_x500 ( PG_FUNCTION_ARGS  )

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

References uuid_generate_internal().

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

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

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