rpm  5.2.1
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 #ifdef WITH_UUID
26  uuid_rc_t rc;
27  uuid_t *uuid = NULL;
28  uuid_t *uuid_ns = NULL;
29  char *result_ptr;
30  size_t result_len;
31 
32  /* sanity check version */
33  if (!(version == 1 || (version >= 3 && version <= 5))) {
34  rpmlog(RPMLOG_ERR, _("invalid UUID version number"));
35  return 1;
36  }
37  if ((version == 3 || version == 5) && (ns == NULL || data == NULL)) {
38  rpmlog(RPMLOG_ERR, _("namespace or data required for requested UUID version\n"));
39  return 1;
40  }
41  if (buf_str == NULL && buf_bin == NULL) {
42  rpmlog(RPMLOG_ERR, _("either string or binary result buffer required\n"));
43  return 1;
44  }
45 
46  /* create UUID object */
47  if ((rc = uuid_create(&uuid)) != UUID_RC_OK) {
48  rpmlog(RPMLOG_ERR, _("failed to create UUID object: %s\n"), uuid_error(rc));
49  return 1;
50  }
51 
52  /* create optional UUID namespace object */
53  if (version == 3 || version == 5) {
54  if ((rc = uuid_create(&uuid_ns)) != UUID_RC_OK) {
55  rpmlog(RPMLOG_ERR, _("failed to create UUID namespace object: %s\n"), uuid_error(rc));
56  return 1;
57  }
58  if ((rc = uuid_load(uuid_ns, ns)) != UUID_RC_OK) {
59  if ((rc = uuid_import(uuid_ns, UUID_FMT_STR, ns, strlen(ns))) != UUID_RC_OK) {
60  rpmlog(RPMLOG_ERR, _("failed to import UUID namespace object: %s\n"), uuid_error(rc));
61  return 1;
62  }
63  }
64  }
65 
66  /* generate UUID */
67  if (version == 1)
68  rc = uuid_make(uuid, UUID_MAKE_V1);
69  else if (version == 3)
70  rc = uuid_make(uuid, UUID_MAKE_V3, uuid_ns, data);
71  else if (version == 4)
72  rc = uuid_make(uuid, UUID_MAKE_V4);
73  else if (version == 5)
74  rc = uuid_make(uuid, UUID_MAKE_V5, uuid_ns, data);
75  if (rc != UUID_RC_OK) {
76  (void) uuid_destroy(uuid);
77  if (uuid_ns != NULL)
78  (void) uuid_destroy(uuid_ns);
79  rpmlog(RPMLOG_ERR, _("failed to make UUID object: %s\n"), uuid_error(rc));
80  return 1;
81  }
82 
83  /* export UUID */
84  if (buf_str != NULL) {
85  result_ptr = buf_str;
86  result_len = UUID_LEN_STR+1;
87  if ((rc = uuid_export(uuid, UUID_FMT_STR, &result_ptr, &result_len)) != UUID_RC_OK) {
88  (void) uuid_destroy(uuid);
89  if (uuid_ns != NULL)
90  (void) uuid_destroy(uuid_ns);
91  rpmlog(RPMLOG_ERR, _("failed to export UUID object as string representation: %s\n"), uuid_error(rc));
92  return 1;
93  }
94  }
95  if (buf_bin != NULL) {
96  result_ptr = (char *)buf_bin;
97  result_len = UUID_LEN_BIN;
98  if ((rc = uuid_export(uuid, UUID_FMT_BIN, &result_ptr, &result_len)) != UUID_RC_OK) {
99  (void) uuid_destroy(uuid);
100  if (uuid_ns != NULL)
101  (void) uuid_destroy(uuid_ns);
102  rpmlog(RPMLOG_ERR, _("failed to export UUID object as binary representation: %s\n"), uuid_error(rc));
103  return 1;
104  }
105  }
106 
107  /* destroy UUID object(s) */
108  (void) uuid_destroy(uuid);
109  if (uuid_ns != NULL)
110  (void) uuid_destroy(uuid_ns);
111 
112  /* indicate success */
113  return 0;
114 #else
115  rpmlog(RPMLOG_ERR, _("UUID generator not available!\n"));
116 
117  /* indicate error */
118  return 1;
119 #endif
120 }
121