PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
uuid-ossp.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
#include <netinet/in.h>
#include <arpa/inet.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, 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

#define dce_uuid_t   uuid_t

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

Referenced by uuid_generate_internal(), and uuid_generate_v1mc().

#define uuid_hash   bsd_uuid_hash

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

#define UUID_MAKE_MC   0

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

Referenced by uuid_generate_v1mc().

#define UUID_MAKE_V1   1

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

Referenced by uuid_generate_v1(), and uuid_generate_v1mc().

#define UUID_MAKE_V2   2

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

#define UUID_MAKE_V3   3

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

Referenced by uuid_generate_v3().

#define UUID_MAKE_V4   4

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

Referenced by uuid_generate_v4().

#define UUID_MAKE_V5   5

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

Referenced by uuid_generate_v5().

#define UUID_TO_LOCAL (   uu)
Value:
do { \
uu.time_low = ntohl(uu.time_low); \
uu.time_mid = ntohs(uu.time_mid); \
uu.time_hi_and_version = ntohs(uu.time_hi_and_version); \
} while (0)

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

Referenced by uuid_generate_internal().

#define UUID_TO_NETWORK (   uu)
Value:
do { \
uu.time_low = htonl(uu.time_low); \
uu.time_mid = htons(uu.time_mid); \
uu.time_hi_and_version = htons(uu.time_hi_and_version); \
} while (0)

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

Referenced by uuid_generate_internal().

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

Referenced by uuid_generate_internal().

Function Documentation

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,
char *  ptr,
int  len 
)
static

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

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

260 {
261  char strbuf[40];
262 
263  switch (v)
264  {
265  case 0: /* constant-value uuids */
266  strlcpy(strbuf, ptr, 37);
267  break;
268 
269  case 1: /* time/node-based uuids */
270  {
271 #ifdef HAVE_UUID_E2FS
272  uuid_t uu;
273 
274  uuid_generate_time(uu);
275  uuid_unparse(uu, strbuf);
276 
277  /*
278  * PTR, if set, replaces the trailing characters of the uuid;
279  * this is to support v1mc, where a random multicast MAC is
280  * used instead of the physical one
281  */
282  if (ptr && len <= 36)
283  strcpy(strbuf + (36 - len), ptr);
284 #else /* BSD */
285  uuid_t uu;
286  uint32_t status = uuid_s_ok;
287  char *str = NULL;
288 
289  uuid_create(&uu, &status);
290 
291  if (status == uuid_s_ok)
292  {
293  uuid_to_string(&uu, &str, &status);
294  if (status == uuid_s_ok)
295  {
296  strlcpy(strbuf, str, 37);
297 
298  /*
299  * PTR, if set, replaces the trailing characters of
300  * the uuid; this is to support v1mc, where a random
301  * multicast MAC is used instead of the physical one
302  */
303  if (ptr && len <= 36)
304  strcpy(strbuf + (36 - len), ptr);
305  }
306  if (str)
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  {
330  MD5_CTX ctx;
331 
332  MD5Init(&ctx);
333  MD5Update(&ctx, ns, sizeof(uu));
334  MD5Update(&ctx, (unsigned char *) ptr, len);
335  /* we assume sizeof MD5 result is 16, same as UUID size */
336  MD5Final((unsigned char *) &uu, &ctx);
337  }
338  else
339  {
340  SHA1_CTX ctx;
341  unsigned char sha1result[SHA1_RESULTLEN];
342 
343  SHA1Init(&ctx);
344  SHA1Update(&ctx, ns, sizeof(uu));
345  SHA1Update(&ctx, (unsigned char *) ptr, len);
346  SHA1Final(sha1result, &ctx);
347  memcpy(&uu, sha1result, sizeof(uu));
348  }
349 
350  /* the calculated hash is using local order */
351  UUID_TO_NETWORK(uu);
352  UUID_V3_OR_V5(uu, v);
353 
354 #ifdef HAVE_UUID_E2FS
355  /* uuid_unparse expects local order */
356  UUID_TO_LOCAL(uu);
357  uuid_unparse((unsigned char *) &uu, strbuf);
358 #else /* BSD */
359  uuid_to_string(&uu, &str, &status);
360 
361  if (status == uuid_s_ok)
362  strlcpy(strbuf, str, 37);
363 
364  if (str)
365  free(str);
366 
367  if (status != uuid_s_ok)
368  ereport(ERROR,
369  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
370  errmsg("uuid library failure: %d",
371  (int) status)));
372 #endif
373  break;
374  }
375 
376  case 4: /* random uuid */
377  default:
378  {
379 #ifdef HAVE_UUID_E2FS
380  uuid_t uu;
381 
382  uuid_generate_random(uu);
383  uuid_unparse(uu, strbuf);
384 #else /* BSD */
385  snprintf(strbuf, sizeof(strbuf),
386  "%08lx-%04x-%04x-%04x-%04x%08lx",
387  (unsigned long) arc4random(),
388  (unsigned) (arc4random() & 0xffff),
389  (unsigned) ((arc4random() & 0xfff) | 0x4000),
390  (unsigned) ((arc4random() & 0x3fff) | 0x8000),
391  (unsigned) (arc4random() & 0xffff),
392  (unsigned long) arc4random());
393 #endif
394  break;
395  }
396  }
397 
399 }
#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:105
#define dce_uuid_t
Definition: uuid-ossp.c:85
#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:584
#define UUID_TO_NETWORK(uu)
Definition: uuid-ossp.c:91
#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 NULL
Definition: c.h:229
#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:98
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
Datum uuid_generate_v1 ( PG_FUNCTION_ARGS  )

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

References NULL, uuid_generate_internal(), and UUID_MAKE_V1.

466 {
468 }
static Datum uuid_generate_internal(int v, unsigned char *ns, char *ptr, int len)
Definition: uuid-ossp.c:259
#define NULL
Definition: c.h:229
#define UUID_MAKE_V1
Definition: uuid-ossp.c:63
Datum uuid_generate_v1mc ( PG_FUNCTION_ARGS  )

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

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

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

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

504 {
505  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
507 
508 #ifdef HAVE_UUID_OSSP
509  return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
510 #else
511  return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
512  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
513 #endif
514 }
#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
static Datum uuid_generate_internal(int v, unsigned char *ns, char *ptr, int len)
Definition: uuid-ossp.c:259
#define UUID_MAKE_V3
Definition: uuid-ossp.c:65
const char * name
Definition: encode.c:521
Definition: uuid.h:20
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum uuid_generate_v4 ( PG_FUNCTION_ARGS  )

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

References NULL, uuid_generate_internal(), and UUID_MAKE_V4.

519 {
521 }
#define UUID_MAKE_V4
Definition: uuid-ossp.c:66
static Datum uuid_generate_internal(int v, unsigned char *ns, char *ptr, int len)
Definition: uuid-ossp.c:259
#define NULL
Definition: c.h:229
Datum uuid_generate_v5 ( PG_FUNCTION_ARGS  )

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

526 {
527  pg_uuid_t *ns = PG_GETARG_UUID_P(0);
529 
530 #ifdef HAVE_UUID_OSSP
531  return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
532 #else
533  return uuid_generate_internal(UUID_MAKE_V5, (unsigned char *) ns,
534  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
535 #endif
536 }
#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:67
static Datum uuid_generate_internal(int v, unsigned char *ns, char *ptr, int len)
Definition: uuid-ossp.c:259
const char * name
Definition: encode.c:521
Definition: uuid.h:20
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum uuid_nil ( PG_FUNCTION_ARGS  )

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

References NULL, and uuid_generate_internal().

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

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

References NULL, and uuid_generate_internal().

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

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

References NULL, and uuid_generate_internal().

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

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

References NULL, and uuid_generate_internal().

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

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

References NULL, and uuid_generate_internal().

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

Variable Documentation

PG_MODULE_MAGIC

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