forked from Xor-el/HashLib4Pascal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHlpArrayUtils.pas
More file actions
250 lines (209 loc) · 6.36 KB
/
Copy pathHlpArrayUtils.pas
File metadata and controls
250 lines (209 loc) · 6.36 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
unit HlpArrayUtils;
{$I ..\Include\HashLib.inc}
interface
uses
SysUtils,
HlpHashLibTypes;
type
TArrayUtils = class sealed(TObject)
public
class function AreEqual(const ABuffer1, ABuffer2: THashLibByteArray)
: Boolean; overload; static;
class function ConstantTimeAreEqual(const ABuffer1,
ABuffer2: THashLibByteArray): Boolean; static;
class procedure Fill(const ABuffer: THashLibByteArray; AFrom, ATo: Int32;
AFiller: Byte); overload; static;
class procedure Fill(const ABuffer: THashLibUInt32Array; AFrom, ATo: Int32;
AFiller: UInt32); overload; static;
class procedure Fill(const ABuffer: THashLibUInt64Array; AFrom, ATo: Int32;
AFiller: UInt64); overload; static;
class procedure FillMemory(ABufferPtr: Pointer; ASize: Int64;
AFiller: Byte); static;
class procedure ZeroFill(const ABuffer: THashLibByteArray);
overload; static;
class procedure ZeroFill(const ABuffer: THashLibUInt32Array);
overload; static;
class procedure ZeroFill(const ABuffer: THashLibUInt64Array);
overload; static;
class procedure ZeroFill(const AMatrixBuffer: THashLibMatrixUInt32Array);
overload; static;
class procedure ZeroFill(const AMatrixBuffer: THashLibMatrixUInt64Array);
overload; static;
class function Concatenate(const ABuffer1, ABuffer2: THashLibByteArray)
: THashLibByteArray; overload; static;
class function Concatenate(const ABuffer1, ABuffer2: THashLibUInt32Array)
: THashLibUInt32Array; overload; static;
class function Clone(const AMatrixBuffer: THashLibMatrixUInt32Array)
: THashLibMatrixUInt32Array; overload; static;
class function Clone(const AMatrixBuffer: THashLibMatrixUInt64Array)
: THashLibMatrixUInt64Array; overload; static;
end;
implementation
{ TArrayUtils }
class function TArrayUtils.AreEqual(const ABuffer1,
ABuffer2: THashLibByteArray): Boolean;
begin
if System.Length(ABuffer1) <> System.Length(ABuffer2) then
begin
Result := False;
Exit;
end;
Result := CompareMem(ABuffer1, ABuffer2, System.Length(ABuffer1) *
System.SizeOf(Byte));
end;
{$B+}
class function TArrayUtils.ConstantTimeAreEqual(const ABuffer1,
ABuffer2: THashLibByteArray): Boolean;
var
LIdx: Int32;
LDiff: UInt32;
begin
LDiff := UInt32(System.Length(ABuffer1)) xor UInt32(System.Length(ABuffer2));
LIdx := 0;
while (LIdx <= System.High(ABuffer1)) and (LIdx <= System.High(ABuffer2)) do
begin
LDiff := LDiff or (UInt32(ABuffer1[LIdx] xor ABuffer2[LIdx]));
System.Inc(LIdx);
end;
Result := LDiff = 0;
end;
{$B-}
class procedure TArrayUtils.Fill(const ABuffer: THashLibByteArray;
AFrom, ATo: Int32; AFiller: Byte);
begin
if ABuffer <> nil then
begin
System.FillChar(ABuffer[AFrom], (ATo - AFrom) *
System.SizeOf(Byte), AFiller);
end;
end;
class procedure TArrayUtils.Fill(const ABuffer: THashLibUInt32Array;
AFrom, ATo: Int32; AFiller: UInt32);
begin
if ABuffer <> nil then
begin
{$IFDEF FPC}
System.FillDWord(ABuffer[AFrom], (ATo - AFrom), AFiller);
{$ELSE}
while AFrom < ATo do
begin
ABuffer[AFrom] := AFiller;
System.Inc(AFrom);
end;
{$ENDIF}
end;
end;
class procedure TArrayUtils.Fill(const ABuffer: THashLibUInt64Array;
AFrom, ATo: Int32; AFiller: UInt64);
begin
if ABuffer <> nil then
begin
{$IFDEF FPC}
System.FillQWord(ABuffer[AFrom], (ATo - AFrom), AFiller);
{$ELSE}
while AFrom < ATo do
begin
ABuffer[AFrom] := AFiller;
System.Inc(AFrom);
end;
{$ENDIF}
end;
end;
class procedure TArrayUtils.FillMemory(ABufferPtr: Pointer; ASize: Int64;
AFiller: Byte);
begin
if ABufferPtr <> nil then
begin
System.FillChar(ABufferPtr^, ASize, AFiller);
end;
end;
class procedure TArrayUtils.ZeroFill(const ABuffer: THashLibByteArray);
begin
TArrayUtils.Fill(ABuffer, 0, System.Length(ABuffer), Byte(0));
end;
class procedure TArrayUtils.ZeroFill(const ABuffer: THashLibUInt32Array);
begin
TArrayUtils.Fill(ABuffer, 0, System.Length(ABuffer), UInt32(0));
end;
class procedure TArrayUtils.ZeroFill(const ABuffer: THashLibUInt64Array);
begin
TArrayUtils.Fill(ABuffer, 0, System.Length(ABuffer), UInt64(0));
end;
class procedure TArrayUtils.ZeroFill(const AMatrixBuffer
: THashLibMatrixUInt32Array);
var
LIdx: Int32;
begin
for LIdx := System.Low(AMatrixBuffer) to System.High(AMatrixBuffer) do
begin
TArrayUtils.ZeroFill(AMatrixBuffer[LIdx]);
end;
end;
class procedure TArrayUtils.ZeroFill(const AMatrixBuffer
: THashLibMatrixUInt64Array);
var
LIdx: Int32;
begin
for LIdx := System.Low(AMatrixBuffer) to System.High(AMatrixBuffer) do
begin
TArrayUtils.ZeroFill(AMatrixBuffer[LIdx]);
end;
end;
class function TArrayUtils.Concatenate(const ABuffer1,
ABuffer2: THashLibByteArray): THashLibByteArray;
var
LABuffer1Length: Int32;
begin
LABuffer1Length := System.Length(ABuffer1);
System.SetLength(Result, LABuffer1Length + System.Length(ABuffer2));
if ABuffer1 <> nil then
begin
System.Move(ABuffer1[0], Result[0], LABuffer1Length * System.SizeOf(Byte));
end;
if ABuffer2 <> nil then
begin
System.Move(ABuffer2[0], Result[LABuffer1Length], System.Length(ABuffer2) *
System.SizeOf(Byte));
end;
end;
class function TArrayUtils.Concatenate(const ABuffer1,
ABuffer2: THashLibUInt32Array): THashLibUInt32Array;
var
LABuffer1Length: Int32;
begin
LABuffer1Length := System.Length(ABuffer1);
System.SetLength(Result, LABuffer1Length + System.Length(ABuffer2));
if ABuffer1 <> nil then
begin
System.Move(ABuffer1[0], Result[0], LABuffer1Length *
System.SizeOf(UInt32));
end;
if ABuffer2 <> nil then
begin
System.Move(ABuffer2[0], Result[LABuffer1Length], System.Length(ABuffer2) *
System.SizeOf(UInt32));
end;
end;
class function TArrayUtils.Clone(const AMatrixBuffer: THashLibMatrixUInt32Array)
: THashLibMatrixUInt32Array;
var
LIdx: Int32;
begin
System.SetLength(Result, System.Length(AMatrixBuffer));
for LIdx := System.Low(AMatrixBuffer) to System.High(AMatrixBuffer) do
begin
Result[LIdx] := System.Copy(AMatrixBuffer[LIdx]);
end;
end;
class function TArrayUtils.Clone(const AMatrixBuffer: THashLibMatrixUInt64Array)
: THashLibMatrixUInt64Array;
var
LIdx: Int32;
begin
System.SetLength(Result, System.Length(AMatrixBuffer));
for LIdx := System.Low(AMatrixBuffer) to System.High(AMatrixBuffer) do
begin
Result[LIdx] := System.Copy(AMatrixBuffer[LIdx]);
end;
end;
end.