-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLBP_UUID.cpp
330 lines (267 loc) · 6 KB
/
LBP_UUID.cpp
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
323
324
325
326
327
328
329
330
#include "stdafx.h"
#include "LBP_UUID.h"
#include "LBP_CRC32.h"
#pragma comment (lib, "rpcrt4.lib")
CLBP_UUID::CLBP_UUID()
{
// The UUID must be initialized to zero.
memset((UUID*)this, 0, sizeof(UUID));
}
CLBP_UUID::CLBP_UUID(const UUID& uuid)
{
*(UUID*)this = uuid;
}
CLBP_UUID::CLBP_UUID(const CLBPString& s)
{
SetAsString(s);
}
CLBP_UUID::CLBP_UUID(const char * sz)
{
SetAsString(sz);
}
CLBP_UUID::CLBP_UUID(const WCHAR * wsz)
{
SetAsString(wsz);
}
void CLBP_UUID::Create(void)
{
UuidCreate((UUID*)this);
}
bool CLBP_UUID::IsNil(void) const
{
#ifdef LBPRHLIB
return ON_UuidIsNil((UUID&)*this);
#else
RPC_STATUS s;
return UuidIsNil((UUID*)this, &s) ? true : false;
#endif
}
int CLBP_UUID::Compare(const UUID& uuid) const
{
#ifdef LBPRHLIB
return ON_UuidCompare((UUID*)this, (UUID*)&uuid);
#else
RPC_STATUS s;
return UuidCompare((UUID*)this, (UUID*)&uuid, &s);
#endif
}
int CLBP_UUID::Compare(const char * sz) const
{
#ifdef LBPRHLIB
const ON_UUID u = ON_UuidFromString(sz);
return Compare(u);
#else
UUID u;
UuidFromStringA((unsigned char *)sz, &u);
return Compare(u);
#endif
}
int CLBP_UUID::Compare(const WCHAR * wsz) const
{
#ifdef LBPRHLIB
const ON_UUID u = ON_UuidFromString(wsz);
return Compare(u);
#else
UUID u;
UuidFromStringW(reinterpret_cast<USHORT*>(CLBPString(wsz).AsNonConstWideString()), &u);
return Compare(u);
#endif
}
CLBP_UUID& CLBP_UUID::operator = (const UUID& uuid)
{
*(UUID*)this = uuid;
return *this;
}
CLBP_UUID& CLBP_UUID::operator = (const char * sz)
{
SetAsString(sz);
return *this;
}
CLBP_UUID& CLBP_UUID::operator = (const WCHAR * wsz)
{
SetAsString(wsz);
return *this;
}
CLBP_UUID& CLBP_UUID::operator = (const CLBPString& s)
{
SetAsString(s);
return *this;
}
bool CLBP_UUID::operator == (const char * sz) const
{
return sz == *this;
}
bool CLBP_UUID::operator == (const WCHAR * wsz) const
{
return wsz == *this;
}
bool CLBP_UUID::operator == (const UUID& uuid) const
{
#ifdef LBPRHLIB
return 0==ON_UuidCompare(this, &uuid);
#else
return IsEqualGUID(*this, uuid) ? true : false;
#endif
}
bool CLBP_UUID::operator != (const char * sz) const
{
return !operator==(sz);
}
bool CLBP_UUID::operator != (const WCHAR * wsz) const
{
return !operator==(wsz);
}
bool CLBP_UUID::operator != (const UUID& uuid) const
{
return !operator==(uuid);
}
bool operator == (const char * sz, const UUID& uuid)
{
#ifdef LBPRHLIB
const ON_UUID u = ON_UuidFromString(sz);
return InlineIsEqualGUID(u, uuid) ? true : false;
#else
UUID u;
UuidFromStringA((UCHAR*)sz, &u);
return IsEqualGUID(u, uuid) ? true : false;
#endif
}
bool operator == (const WCHAR * wsz, const UUID& uuid)
{
#ifdef LBPRHLIB
const ON_UUID u = ON_UuidFromString(wsz);
return InlineIsEqualGUID(u, uuid) ? true : false;
#else
UUID u;
UuidFromStringW(reinterpret_cast<USHORT*>(CLBPString(wsz).AsNonConstWideString()), &u);
return IsEqualGUID(u, uuid) ? true : false;
#endif
}
bool operator != (const char * sz, const UUID& uuid)
{
return !(operator == (sz, uuid));
}
bool operator != (const WCHAR * wsz, const UUID& uuid)
{
return !(operator == (wsz, uuid));
}
CLBPString CLBP_UUID::String(int numChars) const
{
#ifndef LBPRHLIB
USHORT* pString = NULL;
UuidToStringW((UUID*)this, &pString);
ASSERT(wcslen((WCHAR*)pString) == ciNumChars);
CLBPString s((WCHAR*)pString, ciNumChars);
RpcStringFreeW(&pString);
#else
wchar_t str[ciNumChars+1];
ON_UuidToString((UUID&)*this, str);
ASSERT(wcslen(str) == ciNumChars);
CLBPString s(str, ciNumChars);
#endif
s.MakeUpper();
if (numChars < ciNumChars)
{
s = s.Left(max(0, numChars)) + L"...";
}
return s;
}
CLBPString CLBP_UUID::RegistryFormat(int numChars) const
{
return L"{" + String(numChars) + L"}";
}
void CLBP_UUID::SetAsString(const char* sz)
{
SetAsString(CLBPString(sz).Wide());
}
void CLBP_UUID::SetAsString(const WCHAR* wsz)
{
ASSERT(NULL != wsz);
if (NULL == wsz)
return;
if (*wsz == L'{')
{
ASSERT(wcslen(wsz) == 38);
// Support for registry style strings
CLBPString szNew(wsz+1, 37);
szNew = szNew.Left(szNew.GetLength()-1);
SetAsString(szNew.Wide());
return;
}
#ifdef LBPRHLIB
*this = ON_UuidFromString(wsz);
#else
if (UuidFromStringW((unsigned short *)wsz, this) != RPC_S_OK)
{
UuidCreateNil((UUID*)this);
}
#endif
}
void CLBP_UUID::SetAsString(const CLBPString& s)
{
SetAsString(s.AsWideString());
}
bool CLBP_UUID::CreateMashFromString(const char* sz)
{
CLBPString s(sz);
// Example UUID
// {1B6FE7D6-A246-4134-A32B-88B2966C76D3} - 32 Hex characters
// Maximum length of string is 16 - we need to hash the last bit if it's longer.
// Hash will take up 8 hex characters which means if the string is longer than 16
// we need to shorten it to 12.
CLBPString sHex;
if (s.GetLength() > 16)
{
const int iLen = (int)strlen(s.AsMBCSString());
const DWORD dw = CLBP_CRC32::Calculate((const void*)(s.AsMBCSString()), iLen);
s = s.Left(12);
sHex = s.HexStringMBCS();
CLBPString s2;
s2.Format(L"%X", dw);
sHex += s2;
}
else
if (s.GetLength() < 16)
{
s += L"00000000000000000";
s = s.Left(16);
sHex = s.HexStringMBCS();
}
else
{
sHex = s.HexStringMBCS();
}
CLBPString sUUID;
sUUID.Format("%S-%S-%S-%S-%S", sHex.Left(8) .Wide(),
sHex.Mid(8,4) .Wide(),
sHex.Mid(12,4).Wide(),
sHex.Mid(16,4).Wide(),
sHex.Right(12));
sUUID.MakeUpper();
SetAsString(sUUID);
return true;
}
DWORD CLBP_UUID::Hash(void) const
{
return CLBP_CRC32::Calculate(this, sizeof(UUID));
}
bool CLBP_UUID::IsValidUuid(const WCHAR* wsz) // Static.
{
// Format is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
const int len = int(wcslen(wsz));
if (len != ciNumChars)
return false;
for (int i = 0; i < len; i++)
{
const wchar_t c = wsz[i];
if ((8 == i) || (13 == i) || (18 == i) || (23 == i))
{
if ('-' != c)
return false;
}
else
if ( ! (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f'))))
return false;
}
return true;
}