Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
UUID.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlCore/UUID.hpp>
33 #include <vlCore/Time.hpp>
34 #include <vlCore/Random.hpp>
35 #include <vector>
36 
37 using namespace vl;
38 
39 //-----------------------------------------------------------------------------
41 {
42  memset(this, 0, sizeof(*this));
43 }
44 //-----------------------------------------------------------------------------
46 {
47  random.fillRandom(this,sizeof(*this));
48 
49  // Set the two most significant bits (bits 6 and 7) of the ClockSeqHiAndReserved to zero and one, respectively.
50  mClockSeqHiAndReserved &= 0x3F;
51  mClockSeqHiAndReserved |= 0x80;
52 
53  // Set the four most significant bits (bits 12 through 15) of the TimeHiAndVersion field to the 4-bit version number from Section 4.1.3.
54  mTimeHiAndVersion &= 0x0FFF;
55  mTimeHiAndVersion |= 0x4000;
56 }
57 //-----------------------------------------------------------------------------
58 void UUID::toStdString(std::string& guid_str) const
59 {
60  char str[38];
61  fillString(str, false);
62  // avoid reallocation
63  guid_str.resize(38);
64  for(int i=0; i<38; ++i)
65  guid_str[i] = str[i];
66 }
67 //-----------------------------------------------------------------------------
68 void UUID::fillString(char* guid_str, bool zero_terminate) const
69 {
70  const char* hex= "0123456789abcdef";
71 
72  // 01234567890123456789012345678901234567
73  // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
74 
75  if (zero_terminate)
76  guid_str[38] = 0;
77 
78  guid_str[0] = '{';
79  guid_str[9] = '-';
80  guid_str[14] = '-';
81  guid_str[19] = '-';
82  guid_str[24] = '-';
83  guid_str[37] = '}';
84 
85  // TimeLow:
86  for(int i=0; i<8; ++i)
87  guid_str[i+1] = hex[ (mTimeLow >> (28-4*i)) & 0xF];
88 
89  // TimeMid:
90  for(int i=0; i<4; ++i)
91  guid_str[i+10] = hex[ (mTimeMid >> (12-4*i)) & 0xF];
92 
93  // TimeHiAndVersion:
94  for(int i=0; i<4; ++i)
95  guid_str[i+15] = hex[ (mTimeHiAndVersion >> (12-4*i)) & 0xF];
96 
97  // ClockSeqHiAndReserved:
98  guid_str[20] = hex[ (mClockSeqHiAndReserved >> (4-4*0)) & 0xF];
99  guid_str[21] = hex[ (mClockSeqHiAndReserved >> (4-4*1)) & 0xF];
100 
101  // ClockSeqLow:
102  guid_str[22] = hex[ (mClockSeqLow >> (4-4*0)) & 0xF];
103  guid_str[23] = hex[ (mClockSeqLow >> (4-4*1)) & 0xF];
104 
105  // Node:
106  guid_str[25] = hex[ (mNode[0] >> (4-4*0)) & 0xF];
107  guid_str[26] = hex[ (mNode[0] >> (4-4*1)) & 0xF];
108  guid_str[27] = hex[ (mNode[1] >> (4-4*0)) & 0xF];
109  guid_str[28] = hex[ (mNode[1] >> (4-4*1)) & 0xF];
110  guid_str[29] = hex[ (mNode[2] >> (4-4*0)) & 0xF];
111  guid_str[30] = hex[ (mNode[2] >> (4-4*1)) & 0xF];
112  guid_str[31] = hex[ (mNode[3] >> (4-4*0)) & 0xF];
113  guid_str[32] = hex[ (mNode[3] >> (4-4*1)) & 0xF];
114  guid_str[33] = hex[ (mNode[4] >> (4-4*0)) & 0xF];
115  guid_str[34] = hex[ (mNode[4] >> (4-4*1)) & 0xF];
116  guid_str[35] = hex[ (mNode[5] >> (4-4*0)) & 0xF];
117  guid_str[36] = hex[ (mNode[5] >> (4-4*1)) & 0xF];
118 
119  // equivalent to the following formatting:
120 #if 0
121  char guid_cstr[100];
122  sprintf(guid_cstr, "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
125  mNode[0], mNode[1], mNode[2], mNode[3], mNode[4], mNode[5]);
126  VL_CHECK( memcmp(guid_str, guid_cstr, 38) == 0 )
127 #endif
128 }
129 //-----------------------------------------------------------------------------
130 std::string UUID::toStdString() const
131 {
132  std::string guid_str;
133  toStdString(guid_str);
134  return guid_str;
135 }
136 //-----------------------------------------------------------------------------
137 bool UUID::fromString(const char* guid_str)
138 {
139  char xxx[38];
140  memcpy(xxx, guid_str, 38);
141 
142  if (guid_str[0] != '{')
143  return false;
144 
145  if (guid_str[37] != '}')
146  return false;
147 
148  if (guid_str[9] != '-')
149  return false;
150 
151  if (guid_str[14] != '-')
152  return false;
153 
154  if (guid_str[19] != '-')
155  return false;
156 
157  if (guid_str[24] != '-')
158  return false;
159 
160  // to lower case, check hex numbers, convert to nybbles
161  unsigned int nibble[38];
162  for(int i=0; i<38; ++i)
163  {
164  if (i==0 || i==37 || i==9 || i==14 || i==19 || i==24 )
165  continue;
166 
167  if ( guid_str[i]>='0' && guid_str[i]<='9' )
168  nibble[i] = guid_str[i] - '0';
169  else
170  if ( guid_str[i]>='a' && guid_str[i]<='f' )
171  nibble[i] = guid_str[i] - 'a' + 10;
172  else
173  if ( guid_str[i]>='A' && guid_str[i]<='F' )
174  nibble[i] = guid_str[i] - 'A' + 10;
175  else
176  return false;
177  }
178 
179  // 01234567890123456789012345678901234567
180  // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
181 
182  memset(this, 0, sizeof(*this));
183 
184  mTimeLow = (nibble[1] << 28) | (nibble[2] << 24) | (nibble[3] << 20) | (nibble[4] << 16) |
185  (nibble[5] << 12) | (nibble[6] << 8) | (nibble[7] << 4) | nibble[8];
186 
187  mTimeMid = (unsigned short)((nibble[10] << 12) | (nibble[11] << 8) | (nibble[12] << 4) | nibble[13]);
188 
189  mTimeHiAndVersion = (unsigned short)((nibble[15] << 12) | (nibble[16] << 8) | (nibble[17] << 4) | nibble[18]);
190 
191  mClockSeqHiAndReserved = (unsigned char)((nibble[20] << 4) | nibble[21]);
192 
193  mClockSeqLow = (unsigned char)((nibble[22] << 4) | (nibble[23] << 0));
194 
195  mNode[0] = (unsigned char)((nibble[25] << 4) | nibble[26]);
196  mNode[1] = (unsigned char)((nibble[27] << 4) | nibble[28]);
197  mNode[2] = (unsigned char)((nibble[29] << 4) | nibble[30]);
198  mNode[3] = (unsigned char)((nibble[31] << 4) | nibble[32]);
199  mNode[4] = (unsigned char)((nibble[33] << 4) | nibble[34]);
200  mNode[5] = (unsigned char)((nibble[35] << 4) | nibble[36]);
201 
202  return true;
203 }
204 //-----------------------------------------------------------------------------
205 
unsigned short mTimeMid
Definition: UUID.hpp:84
unsigned short mTimeHiAndVersion
Definition: UUID.hpp:85
unsigned char mClockSeqLow
Definition: UUID.hpp:87
bool fromString(const char *guid_str)
Init the UUID from the specified string which must be at least 38 characters long and must be of the ...
Definition: UUID.cpp:137
void generateVersion4(const Random &random)
Generates a Version 4 UUID as defined by RFC4122 using the specified random number generator...
Definition: UUID.cpp:45
VLCORE_EXPORT real random(real min, real max)
Returns a random number N between &#39;min&#39; and &#39;max&#39; (included) with 53 bits of randomness generated usi...
Definition: math_utils.cpp:44
Visualization Library main namespace.
unsigned char mClockSeqHiAndReserved
Definition: UUID.hpp:86
void fillString(char *guid_str, bool zero_terminate=true) const
Fills a buffer with an ascii representation of the UUID of type {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx...
Definition: UUID.cpp:68
UUID()
Constructor, by default it is set to all zeros.
Definition: UUID.cpp:40
virtual bool fillRandom(void *ptr, size_t bytes) const
Fills the specified buffer with random data generated using the best quality random number generation...
Definition: Random.cpp:74
unsigned int mTimeLow
Definition: UUID.hpp:83
unsigned char mNode[6]
Definition: UUID.hpp:88
#define VL_CHECK(expr)
Definition: checks.hpp:73
std::string toStdString() const
Returns an std::string jwith an ASCII representation of the UUID of type {xxxxxxxx-xxxx-xxxx-xxxx-xxx...
Definition: UUID.cpp:130
Cryptographic random number generator.
Definition: Random.hpp:41