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
| ///////////////////////////////////////////////////////////////////////////
// //
// 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 Memory.Block;
interface
uses
Byte,
Byte.Block,
Integer._32.Interval,
Integer._32.Positive,
Integer._64.Positive,
Stream.Input,
Stream.Output,
Text;
type
TMemoryBlock = class(TByteBlock)
protected
function GetAddress(): PByte; virtual; abstract;
function GetSize(): TPositiveInteger32; virtual; abstract;
procedure SetSize(const Value: TPositiveInteger32); virtual; abstract;
public
procedure AssignFrom(Block: TByteBlock); override;
procedure CopyFrom(Block: TByteBlock); override;
procedure FillContents(Value: TByte; Offset: TPositiveInteger32 = 0);
procedure SetContents(const Contents: array of TByte);
procedure CopyFromAddress(Address: PByte);
function CreateSubblock(const Range: TInteger32Interval): TMemoryBlock;
function Copy(): TMemoryBlock; virtual;
procedure Read(Stream: TInputStream); override;
procedure Write(Stream: TOutputStream); override;
function GetByteCount(): TPositiveInteger64; override;
procedure SetByteCount(const Value: TPositiveInteger64); override;
procedure Clear(); override;
procedure ReadFromOffset(Stream: TInputStream; Offset: TPositiveInteger32);
function CreateInputStream(Offset: TPositiveInteger64 = 0): TInputStream; override;
function CreateOverwriteStream(Offset: TPositiveInteger64 = 0): TOutputStream; override;
function CreateRewriteStream(): TOutputStream; override;
function CreateAppendStream(): TOutputStream; override;
function CreateCircularInputStream(Offset: TPositiveInteger32 = 0): TInputStream;
function CreateCircularOverwriteStream(Offset: TPositiveInteger32 = 0): TOutputStream;
function Offset(Value: TPositiveInteger32): PByte;
function ToByteArray(): TArray<TByte>; override;
function ToText(): TText; override;
property Address: PByte read GetAddress;
property Size: TPositiveInteger32 read GetSize write SetSize;
public
class procedure ApplyXor(Target: TMemoryBlock; Mask: TMemoryBlock);
end;
implementation
uses
Exception,
Exception.NotImplemented,
Integer._32,
Integer._32.Positive.TextFormat,
Integer._64.Positive.TextFormat,
Integer._8.Positive,
Memory.Block.Stream.Input,
Memory.Block.Stream.Input.Circular,
Memory.Block.Stream.Output.Append,
Memory.Block.Stream.Output.Overwrite,
Memory.Block.Stream.Output.Overwrite.Circular,
Memory.Block.Subblock;
{ TMemoryBlock }
class procedure TMemoryBlock.ApplyXor(Target: TMemoryBlock; Mask: TMemoryBlock);
var
Target8: PPositiveInteger8;
Mask8: PPositiveInteger8;
Target32: PPositiveInteger32;
Mask32: PPositiveInteger32;
i: TInteger32;
begin
if Target.Size = Mask.Size then
begin
Target32 := PPositiveInteger32(Target.Address);
Mask32 := PPositiveInteger32(Mask.Address);
for i := 1 to Target.Size div SizeOf(TPositiveInteger32) do
begin
Target32^ := Target32^ xor Mask32^;
Inc(Target32);
Inc(Mask32);
end;
Target8 := PPositiveInteger8(Target32);
Mask8 := PPositiveInteger8(Mask32);
for i := 1 to Target.Size mod SizeOf(TPositiveInteger32) do
begin
Target8^ := Target8^ xor Mask8^;
Inc(Target8);
Inc(Mask8);
end;
end
else
raise EException.Create('Block sizes should match');
end;
procedure TMemoryBlock.AssignFrom(Block: TByteBlock);
begin
Size := Block.GetByteCount();
CopyFrom(Block);
end;
procedure TMemoryBlock.Clear();
begin
Size := 0;
end;
function TMemoryBlock.Copy(): TMemoryBlock;
begin
raise ENotSupported.Create(ClassType, 'Copy');
end;
procedure TMemoryBlock.CopyFrom(Block: TByteBlock);
begin
if Block is TMemoryBlock then
CopyFromAddress((Block as TMemoryBlock).Address)
else
inherited CopyFrom(Block);
end;
procedure TMemoryBlock.CopyFromAddress(Address: PByte);
begin
Move(Address^, Self.Address^, Size);
end;
function TMemoryBlock.CreateAppendStream(): TOutputStream;
begin
Result := TMemoryAppendStream.Create(Self);
end;
function TMemoryBlock.CreateCircularInputStream(Offset: TPositiveInteger32): TInputStream;
begin
Result := TMemoryCircularInputStream.Create(Self, Offset);
end;
function TMemoryBlock.CreateCircularOverwriteStream(Offset: TPositiveInteger32): TOutputStream;
begin
Result := TMemoryCircularOverwriteStream.Create(Self, Offset);
end;
function TMemoryBlock.CreateInputStream(Offset: TPositiveInteger64): TInputStream;
begin
if Offset <= Size then
Result := TMemoryInputStream.Create(Self, Offset)
else
raise EException.Create('Illegal offset: ' + PositiveInteger64ToText(Offset));
end;
function TMemoryBlock.CreateOverwriteStream(Offset: TPositiveInteger64): TOutputStream;
begin
if Offset <= Size then
Result := TMemoryOverwriteStream.Create(Self, Offset)
else
raise EException.Create('Illegal offset: ' + PositiveInteger64ToText(Offset));
end;
function TMemoryBlock.CreateRewriteStream(): TOutputStream;
begin
Size := 0;
Result := CreateAppendStream();
end;
function TMemoryBlock.CreateSubblock(const Range: TInteger32Interval): TMemoryBlock;
begin
if (Range.Low >= 0) and (Range.High < Size) then
Result := TMemorySubblock.Create(Self, Range)
else
raise EException.Create('Illegal range: ' + Range.ToText());
end;
procedure TMemoryBlock.FillContents(Value: TByte; Offset: TPositiveInteger32);
var
Start: PByte;
begin
Start := Address;
Inc(Start, Offset);
FillChar(Start^, Size - Offset, Value);
end;
function TMemoryBlock.Offset(Value: TPositiveInteger32): PByte;
begin
if Value < Size then
begin
Result := Address;
Inc(Result, Value);
end
else
raise EException.Create('Illegal offset: ' + PositiveInteger32ToText(Value));
end;
procedure TMemoryBlock.Read(Stream: TInputStream);
begin
Stream.Read(Address^, Size);
end;
procedure TMemoryBlock.ReadFromOffset(Stream: TInputStream; Offset: TPositiveInteger32);
var
Target: PByte;
begin
Target := Address;
Inc(Target, Offset);
Stream.Read(Target^, Size - Offset);
end;
procedure TMemoryBlock.SetByteCount(const Value: TPositiveInteger64);
begin
Size := Value;
end;
procedure TMemoryBlock.SetContents(const Contents: array of TByte);
begin
Size := Length(Contents);
if Size > 0 then
Move(Contents[0], Address^, Size);
end;
function TMemoryBlock.ToByteArray(): TArray<TByte>;
begin
SetLength(Result, Size);
if Size > 0 then
Move(Address^, Result[0], Size);
end;
function TMemoryBlock.ToText(): TText;
begin
Result := '#Bytes: ' + PositiveInteger32ToText(Size);
end;
function TMemoryBlock.GetByteCount(): TPositiveInteger64;
begin
Result := Size;
end;
procedure TMemoryBlock.Write(Stream: TOutputStream);
begin
Stream.Write(Address^, Size);
end;
end.
|