-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuid.cpp
More file actions
322 lines (240 loc) · 6.19 KB
/
Guid.cpp
File metadata and controls
322 lines (240 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "stdafx.h"
#include "Guid.h"
#include <atlbase.h>
void swap(Guid &left, Guid &right)
{
using std::swap;
swap(left._guid, right._guid);
}
const Guid Guid::Empty = Guid(GUID_NULL);
const char Guid::_guidPattern[] = "%08lx-%04hx-%04hx-%02hx%02hx-%02hx%02hx%02hx%02hx%02hx%02hx";
Guid Guid::NewGuid()
{
GUID guid = {};
if (::CoCreateGuid(&guid) != RPC_S_OK)
{
std::exception e("Couldn't create a new GUID.");
throw e;
}
return Guid(guid);
}
Guid Guid::GuidComb()
{
// in order to stay consistent to the .NET implementation of GuidComb, we need the
// number of days between 1/1/1900 and 1/1/1970 to correct for CTime's badness
//const ULONGLONG millsecondsBeforeEpoch = 2208988800000UL;
const ULONG daysBeforeEpoch = 25567UL;
SYSTEMTIME systemTimeCurrent;
::GetLocalTime(&systemTimeCurrent);
SYSTEMTIME systemTimeEpoch = { 1970, 1, 4, 1, 0, 0, 0, 0 };
FILETIME fileTimeCurrent = {};
FILETIME fileTimeEpoch = {};
if ((::SystemTimeToFileTime(&systemTimeCurrent, &fileTimeCurrent) != TRUE) || (::SystemTimeToFileTime(&systemTimeEpoch, &fileTimeEpoch) != TRUE))
{
std::exception e("SystemTimeToFileTime call failed.");
throw e;
}
ULARGE_INTEGER largeIntegerCurrent;
largeIntegerCurrent.LowPart = fileTimeCurrent.dwLowDateTime;
largeIntegerCurrent.HighPart = fileTimeCurrent.dwHighDateTime;
ULARGE_INTEGER largeIntegerEpoch;
largeIntegerEpoch.LowPart = fileTimeEpoch.dwLowDateTime;
largeIntegerEpoch.HighPart = fileTimeEpoch.dwHighDateTime;
ULONGLONG d = (largeIntegerCurrent.QuadPart - largeIntegerEpoch.QuadPart);
d /= 24ULL;
d /= 60ULL;
d /= 60ULL;
d /= 1000ULL;
//CTime baseDate(1970, 1, 1, 0, 0, 0);
//CTime now(time);
//CTimeSpan days(now - baseDate);
// fractional milliseconds aren't taken into account; they are lost to truncation when
// converting to an integer type
ULONGLONG msecs = (systemTimeCurrent.wHour * 3600000UL) + (systemTimeCurrent.wMinute * 60000UL) + (systemTimeCurrent.wSecond * 1000UL) + systemTimeCurrent.wMilliseconds;
d += daysBeforeEpoch;
ULONGLONG m = static_cast<ULONGLONG>(msecs / 3.333333);
BYTE* daysArray = reinterpret_cast<BYTE *>(&d);
BYTE* msecsArray = reinterpret_cast<BYTE *>(&m);
Guid guid = Guid::NewGuid();
GUID g = guid._guid;
g.Data4[2] = daysArray[1];
g.Data4[3] = daysArray[0];
g.Data4[4] = msecsArray[3];
g.Data4[5] = msecsArray[2];
g.Data4[6] = msecsArray[1];
g.Data4[7] = msecsArray[0];
return Guid(g);
}
Guid Guid::Parse(const std::string &str)
{
GUID guid = GUID_NULL;
if (str.empty() == false)
{
// Microsoft's implementation of scanf does not support %hhx for reading unsigned chars
// if it did, this would be super easy
// as it is, if we just read into &guid.Data4[i], it technically works,
// except that it corrupts the stack around the end of guid.Data4
USHORT one, two, three, four, five, six, seven, eight;
::scanf_s(str.c_str(), _guidPattern
, &guid.Data1
, &guid.Data2
, &guid.Data3
, &one
, &two
, &three
, &four
, &five
, &six
, &seven
, &eight);
guid.Data4[0] = static_cast<BYTE>(one);
guid.Data4[1] = static_cast<BYTE>(two);
guid.Data4[2] = static_cast<BYTE>(three);
guid.Data4[3] = static_cast<BYTE>(four);
guid.Data4[4] = static_cast<BYTE>(five);
guid.Data4[5] = static_cast<BYTE>(six);
guid.Data4[6] = static_cast<BYTE>(seven);
guid.Data4[7] = static_cast<BYTE>(eight);
}
Guid result(guid);
return result;
}
//TODO: TryParse
/*
#define IsDigit(c) (((c) >= _T('0')) && ((c) <= _T('9')))
#define IsHexDigit(c) (IsDigit(c) || (((c) >= _T('a')) && ((c) <= _T('f'))) || (((c) >= _T('A')) && ((c) <= _T('F'))))
#define IsDashPosition(i) (((i) == 8) || ((i) == 13) || ((i) == 18) || ((i) == 23))
BOOL Guid::TryParse(LPCTSTR str, Guid &result)
{
BOOL success = FALSE;
result = GUID_NULL;
if (str != nullptr)
{
CString g(str);
g.Trim();
// length of Guid including dashes; other styles not yet supported
if (g.GetLength() == 36)
{
BOOL go = TRUE;
for (int i = 0; i < 36; i++)
{
TCHAR c = g.GetAt(i);
if ((IsDashPosition(i) && (c != _T('-'))) || !IsHexDigit(c))
{
go = FALSE;
break;
}
}
if (go == TRUE)
{
// pattern: /^[0-9a-f]{8}-[0-9a-f]{4}-[1-4][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
result = Parse(str);
success = TRUE;
}
}
}
return success;
}
*/
Guid::Guid() :
_guid(GUID_NULL)
{
}
Guid::Guid(const GUID &guid) :
_guid(guid)
{
}
Guid::Guid(const Guid &other) :
_guid(other._guid)
{
}
Guid::Guid(Guid &&other)
{
swap(*this, other);
}
Guid::~Guid()
{
}
Guid &Guid::operator =(Guid other)
{
swap(*this, other);
return *this;
}
Guid &Guid::operator =(const GUID &other)
{
_guid = other;
return *this;
}
bool Guid::operator ==(const Guid &other) const
{
BOOL eq = (_guid == other._guid);
bool result = (eq == TRUE);
return result;
}
bool Guid::operator ==(const GUID &other) const
{
BOOL eq = (_guid == other);
bool result = (eq == TRUE);
return result;
}
bool Guid::operator !=(const Guid &other) const
{
return !(*this == other);
}
bool Guid::operator !=(const GUID &other) const
{
return !(*this == other);
}
bool Guid::operator <(const Guid &other) const
{
bool result = ToString() < other.ToString();
return result;
}
bool Guid::operator <=(const Guid &other) const
{
bool result = ToString() <= other.ToString();
return result;
}
bool Guid::operator >(const Guid &other) const
{
bool result = ToString() > other.ToString();
return result;
}
bool Guid::operator >=(const Guid &other) const
{
bool result = ToString() >= other.ToString();
return result;
}
Guid::operator GUID() const
{
return _guid;
}
UINT Guid::GetVersion() const
{
UINT result = (_guid.Data3 >> 12) & 0x000F;
return result;
}
std::string Guid::ToString() const
{
char buffer[37] = {};
::sprintf_s(buffer, _guidPattern
, _guid.Data1
, _guid.Data2
, _guid.Data3
, _guid.Data4[0]
, _guid.Data4[1]
, _guid.Data4[2]
, _guid.Data4[3]
, _guid.Data4[4]
, _guid.Data4[5]
, _guid.Data4[6]
, _guid.Data4[7]);
std::string result(buffer);
return result;
}
std::unique_ptr<std::uint8_t[]> Guid::ToByteArray() const
{
std::unique_ptr<std::uint8_t[]> result = std::make_unique<std::uint8_t[]>(16);
::memcpy(result.get(), &_guid, sizeof (GUID));
return result;
}