rpm  5.4.15
rpmuuid.c
Go to the documentation of this file.
1 
5 #if defined(__APPLE__)
6 /* workaround for "uuid_t" type conflict, between <unistd.h> and "uuid.h" */
7 #define _UUID_T
8 #define uuid_t __darwin_uuid_t
9 #include <unistd.h>
10 #undef uuid_t
11 #undef _UUID_T
12 #endif
13 
14 #include "system.h"
15 #include <string.h>
16 #include "rpmlog.h"
17 #include "rpmuuid.h"
18 #ifdef WITH_UUID
19 #include "uuid.h"
20 #endif
21 #include "debug.h"
22 
23 int rpmuuidMake(int version, const char *ns, const char *data, char *buf_str, unsigned char *buf_bin)
24 {
25  int ec = 1; /* assume error */
26 #ifdef WITH_UUID
27  uuid_rc_t rc;
28  uuid_t *uuid = NULL;
29  uuid_t *uuid_ns = NULL;
30  char *result_ptr;
31  size_t result_len;
32 
33  /* sanity check version */
34  if (!(version == 1 || (version >= 3 && version <= 5))) {
35  rpmlog(RPMLOG_ERR, _("invalid UUID version number"));
36  goto exit;
37  }
38  if ((version == 3 || version == 5) && (ns == NULL || data == NULL)) {
39  rpmlog(RPMLOG_ERR, _("namespace or data required for requested UUID version\n"));
40  goto exit;
41  }
42  if (buf_str == NULL && buf_bin == NULL) {
43  rpmlog(RPMLOG_ERR, _("either string or binary result buffer required\n"));
44  goto exit;
45  }
46 
47  /* create UUID object */
48  if ((rc = uuid_create(&uuid)) != UUID_RC_OK) {
49  rpmlog(RPMLOG_ERR, _("failed to create UUID object: %s\n"), uuid_error(rc));
50  goto exit;
51  }
52 
53  /* create optional UUID namespace object */
54  if (version == 3 || version == 5) {
55  if ((rc = uuid_create(&uuid_ns)) != UUID_RC_OK) {
56  rpmlog(RPMLOG_ERR, _("failed to create UUID namespace object: %s\n"), uuid_error(rc));
57  goto exit;
58  }
59  if ((rc = uuid_load(uuid_ns, ns)) != UUID_RC_OK) {
60  if ((rc = uuid_import(uuid_ns, UUID_FMT_STR, ns, strlen(ns))) != UUID_RC_OK) {
61  rpmlog(RPMLOG_ERR, _("failed to import UUID namespace object: %s\n"), uuid_error(rc));
62  goto exit;
63  }
64  }
65  }
66 
67  /* generate UUID */
68  if (version == 1)
69  rc = uuid_make(uuid, UUID_MAKE_V1);
70  else if (version == 3)
71  rc = uuid_make(uuid, UUID_MAKE_V3, uuid_ns, data);
72  else if (version == 4)
73  rc = uuid_make(uuid, UUID_MAKE_V4);
74  else if (version == 5)
75  rc = uuid_make(uuid, UUID_MAKE_V5, uuid_ns, data);
76  if (rc != UUID_RC_OK) {
77  rpmlog(RPMLOG_ERR, _("failed to make UUID object: %s\n"), uuid_error(rc));
78  goto exit;
79  }
80 
81  /* export UUID */
82  if (buf_str != NULL) {
83  result_ptr = buf_str;
84  result_len = UUID_LEN_STR+1;
85  if ((rc = uuid_export(uuid, UUID_FMT_STR, &result_ptr, &result_len)) != UUID_RC_OK) {
86  rpmlog(RPMLOG_ERR, _("failed to export UUID object as string representation: %s\n"), uuid_error(rc));
87  goto exit;
88  }
89  }
90  if (buf_bin != NULL) {
91  result_ptr = (char *)buf_bin;
92  result_len = UUID_LEN_BIN;
93  if ((rc = uuid_export(uuid, UUID_FMT_BIN, &result_ptr, &result_len)) != UUID_RC_OK) {
94  rpmlog(RPMLOG_ERR, _("failed to export UUID object as binary representation: %s\n"), uuid_error(rc));
95  goto exit;
96  }
97  }
98 
99 exit:
100  /* destroy UUID object(s) */
101  if (uuid != NULL)
102  (void) uuid_destroy(uuid);
103  if (uuid_ns != NULL)
104  (void) uuid_destroy(uuid_ns);
105 
106  ec = 0; /* indicate success */
107 #else
108  rpmlog(RPMLOG_ERR, _("UUID generator not available!\n"));
109 #endif
110 
111  return ec;
112 }
113 
int rpmuuidMake(int version, const char *ns, const char *data, char *buf_str, unsigned char *buf_bin)
Generate a Universally Unique Identifier (UUID).
Definition: rpmuuid.c:23
static const char uuid_ns[]
Definition: hdrfmt.c:1808
static void rpmlog(int code, const char *fmt,...)
Definition: rpmlog.h:299
Yet Another syslog(3) API clone.
const char const bson * data
Definition: mongo.h:463
#define _(Text)
Definition: system.h:29
const char * ns
Definition: mongo.h:326