-
Notifications
You must be signed in to change notification settings - Fork 56
/
uTPLb_Random.pas
280 lines (213 loc) · 6.02 KB
/
uTPLb_Random.pas
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
{* ***** BEGIN LICENSE BLOCK *****
Copyright 2009 Sean B. Durkin
This file is part of TurboPower LockBox 3. TurboPower LockBox 3 is free
software being offered under a dual licensing scheme: LGPL3 or MPL1.1.
The contents of this file are subject to the Mozilla Public License (MPL)
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Alternatively, you may redistribute it and/or modify it under the terms of
the GNU Lesser General Public License (LGPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
You should have received a copy of the Lesser GNU General Public License
along with TurboPower LockBox 3. If not, see <http://www.gnu.org/licenses/>.
TurboPower LockBox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. In relation to LGPL,
see the GNU Lesser General Public License for more details. In relation to MPL,
see the MPL License for the specific language governing rights and limitations
under the License.
The Initial Developer of the Original Code for TurboPower LockBox version 2
and earlier was TurboPower Software.
* ***** END LICENSE BLOCK ***** *}
unit uTPLb_Random;
interface
uses Classes;
type
TRandomStream = class( TStream)
private
FValue: int64;
FBuffer: int64;
FAvail: integer;
procedure Crunch;
procedure SetSeed( Value: int64);
protected
function GetSize: Int64; override;
procedure SetSize( const NewSize: Int64); override;
public
constructor Create;
destructor Destroy; override;
class function Instance: TRandomStream;
function Read ( var Buffer; Count: Longint): Longint; override;
function Write( const Buffer; Count: Longint): Longint; override;
function Seek ( const Offset: Int64; Origin: TSeekOrigin): Int64; override;
procedure Randomize;
property Seed: int64 read FValue write SetSeed;
end;
implementation
uses
{$IFDEF MSWINDOWS}Windows, {$ENDIF}Math, SysUtils, uTPLb_IntegerUtils;
var
Inst: TRandomStream = nil;
function TimeStampClock: int64;
{$IFDEF ASSEMBLER}
asm
RDTSC
end;
{$ELSE}
var
SystemTimes: TThread.TSystemTimes;
begin
TThread.GetSystemTimes( SystemTimes);
result := SystemTimes.KernelTime
end;
{$ENDIF}
procedure InitUnit_Random;
begin
end;
procedure DoneUnit_Random;
begin
Inst.Free
end;
{$IFDEF MSWINDOWS}
function CryptAcquireContext(
var phProv: THandle;
pszContainer, pszProvider: PChar;
dwProvType, dwFlags: DWORD): bool;
stdcall; external advapi32 name 'CryptAcquireContextW';
function CryptReleaseContext(
hProv: THandle;
dwFlags: DWORD): bool;
stdcall; external advapi32 name 'CryptReleaseContext';
function CryptGenRandom(
hProv: THandle;
dwLen: DWORD;
pbBuffer: pointer): bool;
stdcall; external advapi32 name 'CryptGenRandom';
const
PROV_RSA_FULL = 1;
CRYPT_SILENT = 64;
Provider = 'Microsoft Base Cryptographic Provider v1.0';
{$ENDIF}
{ TRandomStream }
constructor TRandomStream.Create;
begin
if not assigned( Inst) then
Inst := self;
Randomize
end;
{$OVERFLOWCHECKS OFF} {$RANGECHECKS OFF}
procedure TRandomStream.Crunch;
// Refer http://www.merlyn.demon.co.uk/pas-rand.htm
const
Factor: int64 = 6364136223846793005;
begin
FValue := FValue * Factor + 1 ;
FBuffer := FValue;
FAvail := SizeOf( FValue)
end;
{$RANGECHECKS ON} {$OVERFLOWCHECKS ON}
destructor TRandomStream.Destroy;
begin
if Inst = self then
Inst := nil;
inherited
end;
function TRandomStream.GetSize: Int64;
begin
result := 0
end;
class function TRandomStream.Instance: TRandomStream;
begin
if not assigned( Inst) then
TRandomStream.Create;
result := Inst
end;
procedure TRandomStream.Randomize;
{$IFDEF MSWINDOWS}
var
hProv: THandle;
dwProvType, dwFlags: DWORD;
Provider1: string;
hasOpenHandle: boolean;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
Provider1 := Provider;
dwProvType := PROV_RSA_FULL;
dwFlags := CRYPT_SILENT;
hasOpenHandle := CryptAcquireContext( hProv, nil, PChar( Provider), dwProvType, dwFlags);
try
if (not hasOpenHandle) or (not CryptGenRandom( hProv, SizeOf( FValue), @FValue)) then
FValue := TimeStampClock
finally
if hasOpenHandle then
CryptReleaseContext( hProv, 0)
end;
{$ELSE}
FValue := TimeStampClock;
{$ENDIF}
Crunch
end;
function TRandomStream.Read( var Buffer; Count: Longint): Longint;
var
P: PByte;
Amnt, AmntBits, C: integer;
Harv: int64;
Carry: uint32;
begin
result := Max( Count, 0);
if result <= 0 then exit;
P := @Buffer;
C := result;
repeat
Amnt := Min( FAvail, C);
Move( FBuffer, P^, Amnt);
Dec( FAvail, Amnt);
Dec( C, Amnt);
Inc( P, Amnt);
if FAvail <= 0 then
Crunch
else
begin
Harv := FBuffer;
if Amnt >= 4 then
begin
Int64Rec( Harv).Lo := Int64Rec( Harv).Hi;
Int64Rec( Harv).Hi := 0;
Dec( Amnt, 4)
end;
if Amnt > 0 then
begin
AmntBits := Amnt * 8;
Carry := Int64Rec( Harv).Hi shl (32 - (AmntBits));
Int64Rec( Harv).Hi := Int64Rec( Harv).Hi shr AmntBits;
Int64Rec( Harv).Lo := (Int64Rec( Harv).Lo shr AmntBits) or Carry;
end;
FBuffer := Harv
end
until C <= 0
end;
function TRandomStream.Seek( const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
result := 0
end;
procedure TRandomStream.SetSeed( Value: int64);
begin
FValue := Value;
FBuffer := FValue;
FAvail := SizeOf( FBuffer)
end;
procedure TRandomStream.SetSize( const NewSize: Int64);
begin
end;
function TRandomStream.Write( const Buffer; Count: Longint): Longint;
begin
result := Count
end;
initialization
InitUnit_Random;
finalization
DoneUnit_Random;
end.