CppCommon  1.0.4.1
C++ Common Library
uuid.cpp
Go to the documentation of this file.
1 
9 #include "system/uuid.h"
10 
11 #include "memory/memory.h"
12 
13 #if defined(__MSYS__) || defined(_WIN32) || defined(_WIN64)
14 #include <windows.h>
15 #include <rpc.h>
16 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
17 #include <uuid/uuid.h>
18 #endif
19 
20 namespace CppCommon {
21 
22 std::string UUID::string() const
23 {
24  const char* digits = "0123456789abcdef";
25 
26  std::string result(36, '0');
27 
28  int index = 0;
29  for (auto value : _data)
30  {
31  result[index++] = digits[(value >> 4) & 0x0F];
32  result[index++] = digits[(value >> 0) & 0x0F];
33  if ((index == 8) || (index == 13) || (index == 18) || (index == 23))
34  result[index++] = '-';
35  }
36 
37  return result;
38 }
39 
41 {
42  UUID result;
43 #if defined(__MSYS__) || defined(_WIN32) || defined(_WIN64)
44  ::UUID uuid;
45  if (UuidCreateSequential(&uuid) != RPC_S_OK)
46  throwex SystemException("Cannot generate sequential UUID!");
47 
48  result._data[0] = (uuid.Data1 >> 24) & 0xFF;
49  result._data[1] = (uuid.Data1 >> 16) & 0xFF;
50  result._data[2] = (uuid.Data1 >> 8) & 0xFF;
51  result._data[3] = (uuid.Data1 >> 0) & 0xFF;
52 
53  result._data[4] = (uuid.Data2 >> 8) & 0xFF;
54  result._data[5] = (uuid.Data2 >> 0) & 0xFF;
55 
56  result._data[6] = (uuid.Data3 >> 8) & 0xFF;
57  result._data[7] = (uuid.Data3 >> 0) & 0xFF;
58 
59  result._data[8] = uuid.Data4[0];
60  result._data[9] = uuid.Data4[1];
61 
62  result._data[10] = uuid.Data4[2];
63  result._data[11] = uuid.Data4[3];
64  result._data[12] = uuid.Data4[4];
65  result._data[13] = uuid.Data4[5];
66  result._data[14] = uuid.Data4[6];
67  result._data[15] = uuid.Data4[7];
68 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
69  uuid_t uuid;
70  uuid_generate_time(uuid);
71 
72  result._data[0] = uuid[0];
73  result._data[1] = uuid[1];
74  result._data[2] = uuid[2];
75  result._data[3] = uuid[3];
76 
77  result._data[4] = uuid[4];
78  result._data[5] = uuid[5];
79 
80  result._data[6] = uuid[6];
81  result._data[7] = uuid[7];
82 
83  result._data[8] = uuid[8];
84  result._data[9] = uuid[9];
85 
86  result._data[10] = uuid[10];
87  result._data[11] = uuid[11];
88  result._data[12] = uuid[12];
89  result._data[13] = uuid[13];
90  result._data[14] = uuid[14];
91  result._data[15] = uuid[15];
92 #endif
93  return result;
94 }
95 
97 {
98  UUID result;
99 #if defined(__MSYS__) || defined(_WIN32) || defined(_WIN64)
100  ::UUID uuid;
101  if (UuidCreate(&uuid) != RPC_S_OK)
102  throwex SystemException("Cannot generate random UUID!");
103 
104  result._data[0] = (uuid.Data1 >> 24) & 0xFF;
105  result._data[1] = (uuid.Data1 >> 16) & 0xFF;
106  result._data[2] = (uuid.Data1 >> 8) & 0xFF;
107  result._data[3] = (uuid.Data1 >> 0) & 0xFF;
108 
109  result._data[4] = (uuid.Data2 >> 8) & 0xFF;
110  result._data[5] = (uuid.Data2 >> 0) & 0xFF;
111 
112  result._data[6] = (uuid.Data3 >> 8) & 0xFF;
113  result._data[7] = (uuid.Data3 >> 0) & 0xFF;
114 
115  result._data[8] = uuid.Data4[0];
116  result._data[9] = uuid.Data4[1];
117 
118  result._data[10] = uuid.Data4[2];
119  result._data[11] = uuid.Data4[3];
120  result._data[12] = uuid.Data4[4];
121  result._data[13] = uuid.Data4[5];
122  result._data[14] = uuid.Data4[6];
123  result._data[15] = uuid.Data4[7];
124 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
125  uuid_t uuid;
126  uuid_generate_random(uuid);
127 
128  result._data[0] = uuid[0];
129  result._data[1] = uuid[1];
130  result._data[2] = uuid[2];
131  result._data[3] = uuid[3];
132 
133  result._data[4] = uuid[4];
134  result._data[5] = uuid[5];
135 
136  result._data[6] = uuid[6];
137  result._data[7] = uuid[7];
138 
139  result._data[8] = uuid[8];
140  result._data[9] = uuid[9];
141 
142  result._data[10] = uuid[10];
143  result._data[11] = uuid[11];
144  result._data[12] = uuid[12];
145  result._data[13] = uuid[13];
146  result._data[14] = uuid[14];
147  result._data[15] = uuid[15];
148 #endif
149  return result;
150 }
151 
153 {
154  UUID result;
155  Memory::CryptoFill(result._data.data(), result._data.size());
156  return result;
157 }
158 
159 } // namespace CppCommon
static void CryptoFill(void *buffer, size_t size)
Fill the given memory buffer with cryptographic strong random bytes.
Definition: memory.cpp:136
System exception.
Definition: exceptions.h:107
Universally unique identifier (UUID)
Definition: uuid.h:36
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
Definition: uuid.cpp:22
static UUID Random()
Generate random UUID4 (randomly or pseudo-randomly generated version)
Definition: uuid.cpp:96
static UUID Sequential()
Generate sequential UUID1 (time based version)
Definition: uuid.cpp:40
static UUID Secure()
Generate secure UUID4 (secure generated version)
Definition: uuid.cpp:152
#define throwex
Throw extended exception macro.
Definition: exceptions.h:23
Memory management definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Universally unique identifier (UUID) definition.