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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Uuid;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Comparator,
Exception,
Integer._16.Positive,
Integer._32,
Integer._32.Positive,
Integer._64.Positive,
Integer._8.Positive,
Mathematics.Sign,
Stream.Input,
Stream.Output,
Text,
Uri;
type
EUuid = class(EException);
TUuidVariant = (
ReservedNcsBackwardCompatibility = $0,
LeachSalz = $2,
ReservedMicrosoftBackwardCompatibility = $6,
ReservedFutureDefinition = $7
);
TUuidVersion = (
TimeBased = $1,
DceSecurity = $2,
NameBasedMd5 = $3,
Random = $4,
NameBasedSha1 = $5
);
TUuidNode = array [0..5] of TPositiveInteger8;
PUuid = ^TUuid;
TUuid = record
private
TimeLow: TPositiveInteger32;
TimeMid: TPositiveInteger16;
TimeHiAndVersion: TPositiveInteger16;
ClockSeqHiAndReserved: TPositiveInteger8;
ClockSeqLow: TPositiveInteger8;
FNode: TUuidNode;
class function ParseElement(var Value: TText; Count: TInteger32): TPositiveInteger32; static;
class procedure ParseSeparator(var Value: TText); static;
class function ElementToText(Value: TPositiveInteger32; Digits: TPositiveInteger32): TText; static;
class function Compare(const Left: TUuid; const Right: TUuid): TSign; static;
private
function GetVariant(): TUuidVariant;
procedure SetVariant(const Value: TUuidVariant);
function GetVersion(): TUuidVersion;
procedure SetVersion(const Value: TUuidVersion);
function GetTimeStamp(): TPositiveInteger64;
procedure SetTimeStamp(const Value: TPositiveInteger64);
function GetClockSequence(): TPositiveInteger16;
procedure SetClockSequence(const Value: TPositiveInteger16);
function BitCountForVariant(Value: TUuidVariant): TPositiveInteger32;
const
TimeHiBitCount: TPositiveInteger32 = 12;
TimeHiMask: TPositiveInteger16 = (1 shl 12) - 1;
VersionMask: TPositiveInteger16 = ((1 shl 4) - 1) shl 12;
const
ClockSequenceHighBitCount: TPositiveInteger32 = 6; // TODO: We assume the Leach-Salz variant here which requires two bits
ClockSequenceHighMask: TPositiveInteger8 = (1 shl 6) - 1;
VariantMask: TPositiveInteger8 = ((1 shl 2) - 1) shl 6;
public
class function FromText(Value: TText): TUuid; static;
class function IsValidText(const Value: TText): TBoolean; static;
class operator Equal(const Left: TUuid; const Right: TUuid): TBoolean;
class operator NotEqual(const Left: TUuid; const Right: TUuid): TBoolean;
class operator GreaterThan(const Left: TUuid; const Right: TUuid): TBoolean;
class operator GreaterThanOrEqual(const Left: TUuid; const Right: TUuid): TBoolean;
class operator LessThan(const Left: TUuid; const Right: TUuid): TBoolean;
class operator LessThanOrEqual(const Left: TUuid; const Right: TUuid): TBoolean;
public
procedure Read(Stream: TInputStream);
procedure Write(Stream: TOutputStream);
function ToText(): TText;
function ToUri(): TUri;
property Variant: TUuidVariant read GetVariant write SetVariant;
property Version: TUuidVersion read GetVersion write SetVersion;
property TimeStamp: TPositiveInteger64 read GetTimeStamp write SetTimeStamp;
property ClockSequence: TPositiveInteger16 read GetClockSequence write SetClockSequence;
property Node: TUuidNode read FNode write FNode;
public
class var
Unknown: TUuid;
NameSpace_Dns: TUuid;
NameSpace_Url: TUuid;
NameSpace_Oid: TUuid;
NameSpace_X500: TUuid;
class procedure Initialize(); static;
class procedure Finalize(); static;
type
TCompare = class(TComparator<TUuid>)
public
function Compare(const Left: TUuid; const Right: TUuid): TSign; override;
public
class var
Instance: TCompare;
end;
end;
implementation
uses
Character.Condition,
Integer,
Numeral.System.Hexadecimal,
Text.Operation;
{ TUuid }
function TUuid.ToText(): TText;
var
i: TInteger32;
begin
Result :=
ElementToText(TimeLow, 8)
+ '-' + ElementToText(TimeMid, 4)
+ '-' + ElementToText(TimeHiAndVersion, 4)
+ '-' + ElementToText(ClockSeqHiAndReserved, 2) + ElementToText(ClockSeqLow, 2)
+ '-';
for i := Low(Node) to High(Node) do
Result := Result + ElementToText(Node[i], 2);
end;
function TUuid.ToUri(): TUri;
begin
Result := TUri.NewUniformResourceName('uuid', ToText());
end;
function TUuid.BitCountForVariant(Value: TUuidVariant): TPositiveInteger32;
begin
case Value of
TUuidVariant.ReservedNcsBackwardCompatibility:
Result := 1;
TUuidVariant.LeachSalz:
Result := 2;
TUuidVariant.ReservedMicrosoftBackwardCompatibility:
Result := 3;
TUuidVariant.ReservedFutureDefinition:
Result := 3;
else
raise EUuid.Create('Unknown UUID variant');
end;
end;
class function TUuid.Compare(const Left: TUuid; const Right: TUuid): TSign;
var
i: TPositiveInteger32;
begin
Result := TInteger.Compare(Left.TimeLow, Right.TimeLow);
if Result = sZero then
begin
Result := TInteger.Compare(Left.TimeMid, Right.TimeMid);
if Result = sZero then
begin
Result := TInteger.Compare(Left.TimeHiAndVersion, Right.TimeHiAndVersion);
if Result = sZero then
begin
Result := TInteger.Compare(Left.ClockSeqHiAndReserved, Right.ClockSeqHiAndReserved);
if Result = sZero then
begin
Result := TInteger.Compare(Left.ClockSeqLow, Right.ClockSeqLow);
i := Low(Left.Node);
while (Result = sZero) and (i <= High(Left.Node)) do
begin
Result := TInteger.Compare(Left.Node[i], Right.Node[i]);
Inc(i);
end;
end;
end;
end;
end;
end;
class function TUuid.ElementToText(Value: TPositiveInteger32; Digits: TPositiveInteger32): TText;
begin
// TODO: This should be done more efficiently and probably using a TPositiveInteger32Format
Result := PositiveInteger64ToHexadecimal(Value);
Result := Result.PadLeft('0', Digits);
Result := Result.ToLowerCase();
end;
class operator TUuid.Equal(const Left: TUuid; const Right: TUuid): TBoolean;
var
i: TPositiveInteger32;
begin
Result :=
(Left.TimeLow = Right.TimeLow)
and (Left.TimeMid = Right.TimeMid)
and (Left.TimeHiAndVersion = Right.TimeHiAndVersion)
and (Left.ClockSeqHiAndReserved = Right.ClockSeqHiAndReserved)
and (Left.ClockSeqLow = Right.ClockSeqLow);
i := Low(Left.Node);
while Result and (i <= High(Left.Node)) do
begin
Result := Left.Node[i] = Right.Node[i];
Inc(i);
end;
end;
class procedure TUuid.Finalize();
begin
TCompare.Instance.ReleaseReference();
end;
class function TUuid.FromText(Value: TText): TUuid;
var
i: TPositiveInteger32;
begin
if Value.Count <> 36 then
raise EUuid.Create('Invalid UUID format');
Result.TimeLow := ParseElement(Value, 8);
ParseSeparator(Value);
Result.TimeMid := ParseElement(Value, 4);
ParseSeparator(Value);
Result.TimeHiAndVersion := ParseElement(Value, 4);
ParseSeparator(Value);
Result.ClockSeqHiAndReserved := ParseElement(Value, 2);
Result.ClockSeqLow := ParseElement(Value, 2);
ParseSeparator(Value);
for i := Low(Result.Node) to High(Result.Node) do
Result.FNode[i] := ParseElement(Value, 2);
end;
function TUuid.GetClockSequence(): TPositiveInteger16;
begin
Result := ((ClockSeqHiAndReserved and ClockSequenceHighMask) shl 8) or ClockSeqLow;
end;
function TUuid.GetTimeStamp(): TPositiveInteger64;
begin
Result :=
((TimeHiAndVersion and TimeHiMask) shl 16)
or TimeMid;
Result := (Result shl 32) or (TimeLow shl 0);
end;
function TUuid.GetVariant(): TUuidVariant;
var
VariantBits: TPositiveInteger8;
begin
VariantBits := ClockSeqHiAndReserved shr 5;
if VariantBits and $4 = 0 then
Result := TUuidVariant.ReservedNcsBackwardCompatibility
else if VariantBits and $2 <> 0 then
begin
if VariantBits and $1 <> 0 then
Result := TUuidVariant.ReservedFutureDefinition
else
Result := TUuidVariant.ReservedMicrosoftBackwardCompatibility;
end
else
Result := TUuidVariant.LeachSalz;
end;
function TUuid.GetVersion(): TUuidVersion;
begin
Result := TUuidVersion(TimeHiAndVersion shr TimeHiBitCount);
end;
class operator TUuid.GreaterThan(const Left: TUuid; const Right: TUuid): TBoolean;
begin
Result := Compare(Left, Right) > sZero;
end;
class operator TUuid.GreaterThanOrEqual(const Left: TUuid; const Right: TUuid): TBoolean;
begin
Result := Compare(Left, Right) >= sZero;
end;
class procedure TUuid.Initialize();
begin
TUuid.Unknown := TUuid.FromText('00000000-0000-0000-0000-000000000000');
TUuid.NameSpace_Dns := TUuid.FromText('6ba7b810-9dad-11d1-80b4-00c04fd430c8');
TUuid.NameSpace_Url := TUuid.FromText('6ba7b811-9dad-11d1-80b4-00c04fd430c8');
TUuid.NameSpace_Oid := TUuid.FromText('6ba7b812-9dad-11d1-80b4-00c04fd430c8');
TUuid.NameSpace_X500 := TUuid.FromText('6ba7b814-9dad-11d1-80b4-00c04fd430c8');
TCompare.Instance := TCompare.Create();
TCompare.Instance.AddReference();
end;
class function TUuid.IsValidText(const Value: TText): TBoolean;
var
i: TInteger32;
begin
// TODO: This implementation can be simplified a lot
Result := Value.Count = 36;
i := 0;
while Result and (i < 8) do
begin
Result := Value[i] in ['0'..'9', 'a'..'f', 'A'..'F'];
Inc(i);
end;
if Result then
begin
Result := Value[i] = '-';
Inc(i);
while Result and (i < 13) do
begin
Result := Value[i] in ['0'..'9', 'a'..'f', 'A'..'F'];
Inc(i);
end;
if Result then
begin
Result := Value[i] = '-';
Inc(i);
while Result and (i < 18) do
begin
Result := Value[i] in ['0'..'9', 'a'..'f', 'A'..'F'];
Inc(i);
end;
if Result then
begin
Result := Value[i] = '-';
Inc(i);
while Result and (i < 23) do
begin
Result := Value[i] in ['0'..'9', 'a'..'f', 'A'..'F'];
Inc(i);
end;
if Result then
begin
Result := Value[i] = '-';
Inc(i);
while Result and (i < 36) do
begin
Result := Value[i] in ['0'..'9', 'a'..'f', 'A'..'F'];
Inc(i);
end;
end;
end;
end;
end;
end;
class operator TUuid.LessThan(const Left: TUuid; const Right: TUuid): TBoolean;
begin
Result := Compare(Left, Right) < sZero;
end;
class operator TUuid.LessThanOrEqual(const Left: TUuid; const Right: TUuid): TBoolean;
begin
Result := Compare(Left, Right) <= sZero;
end;
class operator TUuid.NotEqual(const Left: TUuid; const Right: TUuid): TBoolean;
begin
Result := not (Left = Right);
end;
class function TUuid.ParseElement(var Value: TText; Count: TInteger32): TPositiveInteger32;
var
Hexadecimal: TText;
begin
Hexadecimal := Value.RemoveFromStart(Count);
if TTextOperation.MatchesAll(Hexadecimal, TCharacterCondition.HexadecimalDigit) then
Result := HexadecimalToPositiveInteger32(Hexadecimal)
else
raise EUuid.Create('Invalid UUID format');
end;
class procedure TUuid.ParseSeparator(var Value: TText);
var
Character: TText;
begin
Character := Value.RemoveFromStart(1);
if Character <> '-' then
raise EUuid.Create('Invalid UUID format');
end;
procedure TUuid.Read(Stream: TInputStream);
begin
TimeLow := Stream.ReadBigEndian32();
TimeMid := Stream.ReadBigEndian16();
TimeHiAndVersion := Stream.ReadBigEndian16();
ClockSeqHiAndReserved := Stream.ReadBigEndian8();
ClockSeqLow := Stream.ReadBigEndian8();
Stream.Read(FNode[0], Length(Node));
end;
procedure TUuid.SetClockSequence(const Value: TPositiveInteger16);
begin
ClockSeqLow := Value and $FF;
ClockSeqHiAndReserved := (ClockSeqHiAndReserved and VariantMask) or ((Value shr 8) and ClockSequenceHighMask);
end;
procedure TUuid.SetTimeStamp(const Value: TPositiveInteger64);
var
TimeStamp: TPositiveInteger64;
begin
TimeStamp := Value;
TimeLow := TimeStamp and $FFFFFFFF;
TimeStamp := TimeStamp shr 32;
TimeMid := TimeStamp and $FFFF;
TimeHiAndVersion := (TimeHiAndVersion and VersionMask) or ((TimeStamp shr 16) and TimeHiMask);
end;
procedure TUuid.SetVariant(const Value: TUuidVariant);
var
BitCount: TPositiveInteger8;
Mask: TPositiveInteger8;
begin
BitCount := BitCountForVariant(Value);
Mask := 1 shl (8 - BitCount) - 1;
ClockSeqHiAndReserved := (ClockSeqHiAndReserved and Mask) or (Ord(Value) shl (8 - BitCount));
end;
procedure TUuid.SetVersion(const Value: TUuidVersion);
begin
TimeHiAndVersion := (TimeHiAndVersion and TimeHiMask) or (Ord(Value) shl TimeHiBitCount);
end;
procedure TUuid.Write(Stream: TOutputStream);
begin
Stream.WriteBigEndian32(TimeLow);
Stream.WriteBigEndian16(TimeMid);
Stream.WriteBigEndian16(TimeHiAndVersion);
Stream.WriteBigEndian8(ClockSeqHiAndReserved);
Stream.WriteBigEndian8(ClockSeqLow);
Stream.Write(Node[0], Length(Node));
end;
{ TUuid.TCompare }
function TUuid.TCompare.Compare(const Left: TUuid; const Right: TUuid): TSign;
begin
Result := TUuid.Compare(Left, Right);
end;
initialization
TText.AssureInitialized();
TUuid.Initialize();
finalization
TUuid.Finalize();
end.
|