-
Notifications
You must be signed in to change notification settings - Fork 24
/
Cromis.Streams.Filters.pas
325 lines (274 loc) · 8.55 KB
/
Cromis.Streams.Filters.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
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
unit Cromis.Streams.Filters;
interface
uses
SysUtils, Classes, Contnrs;
type
TStreamFilters = class;
TCustomFilter = class
protected
FID: AnsiString;
public
procedure InputFilter(const InputStream, OutputStream: TStream); virtual; abstract;
procedure OutputFilter(const InputStream, OutputStream: TStream); virtual; abstract;
property ID: AnsiString read FID;
end;
IUsedFilters = Interface(IInterface)
['{C5E58525-BA97-4CC9-ADF1-C35844A1BEB5}']
function FilterList: TStreamFilters;
end;
TStreamFilters = class
private
FFilterList: TObjectList;
function GetFilters(const Index: Integer): TCustomFilter;
function GetCount: Integer;
function GetOwnFilters: Boolean;
procedure SetOwnFilters(const Value: Boolean);
public
constructor Create(const OwnFilters: Boolean = True);
destructor Destroy; override;
procedure RemoveAll;
procedure AddFilter(const Filter: TCustomFilter);
procedure RemoveFilter(const Filter: TCustomFilter);
function IsFiltered(const Stream: TStream): Boolean;
function FindFilter(const ID: AnsiString): TCustomFilter;
procedure ApplyInput(const InStream, OutStream: TStream);
function ApplyOutput(const InStream, OutStream: TStream): IUsedFilters;
property Filters[const Index: Integer]: TCustomFilter read GetFilters; default;
property OwnFilters: Boolean read GetOwnFilters write SetOwnFilters;
property Count: Integer read GetCount;
end;
type
TFilterClass = class of TCustomFilter;
// function to check if the stream is filtered
function IsFiltered(const Stream: TStream): Boolean;
implementation
type
TFilterMark = array [0..2] of Cardinal;
type
TUsedFilters = class(TInterfacedObject, IUsedFilters)
private
FFilterList: TStreamFilters;
public
constructor Create;
destructor Destroy; override;
function FilterList: TStreamFilters;
end;
var
FilterMark: TFilterMark = (0,0,0);
function IsFiltered(const Stream: TStream): Boolean;
var
OldPosition: Int64;
CompareMark: TFilterMark;
begin
OldPosition := Stream.Position;
try
Stream.Seek(0, soFromBeginning);
Stream.Read(CompareMark[0], SizeOf(TFilterMark));
Result := CompareMem(@CompareMark[0], @FilterMark[0], SizeOf(TFilterMark));
finally
Stream.Position := OldPosition;
end;
end;
{ TStreamFilters }
procedure TStreamFilters.AddFilter(const Filter: TCustomFilter);
begin
if Trim(string(Filter.ID)) = '' then
raise Exception.Create('Filter needs to have a valid ID');
FFilterList.Add(Filter);
end;
procedure TStreamFilters.ApplyInput(const InStream, OutStream: TStream);
var
I: Integer;
Input: TMemoryStream;
Output: TMemoryStream;
NameSize: Cardinal;
FilterData: TMemoryStream;
FilterCount: Cardinal;
begin
if FFilterList.Count > 0 then
begin
Input := TMemoryStream.Create;
try
Output := TMemoryStream.Create;
try
FilterData := TMemoryStream.Create;
try
InStream.Position := 0;
FilterCount := 0;
for I := 0 to FFilterList.Count - 1 do
begin
case I = 0 of
True: Filters[I].InputFilter(InStream, Output);
False: Filters[I].InputFilter(Input, Output);
end;
NameSize := Length(Filters[I].ID) * SizeOf(AnsiChar);
FilterData.Write(NameSize, SizeOf(Cardinal));
FilterData.Write(Filters[I].ID[1], NameSize);
Inc(FilterCount);
if I < FFilterList.Count - 1 then
begin
Input.Clear;
Input.CopyFrom(Output, 0);
Input.Position := 0;
Output.Clear
end;
end;
OutStream.Size := 0;
Output.Position := 0;
FilterData.Position := 0;
// copy last output to storage
OutStream.Write(FilterMark[0], SizeOf(TFilterMark));
OutStream.Write(FilterCount, SizeOf(Cardinal));
OutStream.CopyFrom(FilterData, FilterData.Size);
OutStream.CopyFrom(Output, Output.Size);
finally
FilterData.Free;
end;
finally
Output.Free;
end;
finally
Input.Free;
end;
end;
end;
function TStreamFilters.ApplyOutput(const InStream, OutStream: TStream): IUsedFilters;
var
I: Integer;
Input: TMemoryStream;
Output: TMemoryStream;
NameSize: Cardinal;
FilterName: AnsiString;
FilterCount: Cardinal;
OldPosition: Int64;
CompareMark: TFilterMark;
ChainFilter: TCustomFilter;
begin
Result := TUsedFilters.Create;
if FFilterList.Count > 0 then
begin
Input := TMemoryStream.Create;
try
Output := TMemoryStream.Create;
try
if InStream.Size > SizeOf(TFilterMark) then
begin
OldPosition := InStream.Position;
InStream.Seek(0, soFromBeginning);
InStream.Read(CompareMark[0], SizeOf(TFilterMark));
if not CompareMem(@CompareMark[0], @FilterMark[0], SizeOf(TFilterMark)) then
begin
InStream.Position := OldPosition;
Exit;
end;
if InStream.Read(FilterCount, SizeOf(Cardinal)) = SizeOf(Cardinal) then
begin
for I := 0 to FilterCount - 1 do
begin
if InStream.Read(NameSize, SizeOf(Cardinal)) < SizeOf(Cardinal) then
raise Exception.Create('Filtered data is corrupted');
// set the length of filter name
SetLength(FilterName, NameSize);
if InStream.Read(FilterName[1], NameSize) < SizeOf(Cardinal) then
raise Exception.Create('Filtered data is corrupted');
ChainFilter := FindFilter(FilterName);
if ChainFilter = nil then
raise Exception.CreateFmt('Required filter with ID %s is not on the list of available filters', [FilterName]);
// add filter to output list
Result.FilterList.AddFilter(ChainFilter);
end;
for I := Result.FilterList.Count - 1 downto 0 do
begin
case I = Result.FilterList.Count - 1 of
True: Result.FilterList[I].OutputFilter(InStream, Output);
False: Result.FilterList[I].OutputFilter(Input, Output);
end;
if I > 0 then
begin
Input.Clear;
Input.CopyFrom(Output, 0);
Input.Position := 0;
Output.Clear;
end;
OutStream.Size := 0;
Output.Position := 0;
// copy last output to storage
OutStream.CopyFrom(Output, Output.Size);
OutStream.Position := 0;
end;
end;
end;
finally
Output.Free;
end;
finally
Input.Free;
end;
end;
end;
constructor TStreamFilters.Create(const OwnFilters: Boolean = True);
begin
FFilterList := TObjectList.Create(OwnFilters);
end;
destructor TStreamFilters.Destroy;
begin
FreeAndNil(FFilterList);
inherited;
end;
function TStreamFilters.FindFilter(const ID: AnsiString): TCustomFilter;
var
I: Integer;
begin
Result := nil;
for I := 0 to FFilterList.Count - 1 do
begin
if SameText(string(TCustomFilter(FFilterList[I]).ID), string(ID)) then
begin
Result := TCustomFilter(FFilterList[I]);
Exit;
end;
end;
end;
function TStreamFilters.GetCount: Integer;
begin
Result := FFilterList.Count;
end;
function TStreamFilters.GetFilters(const Index: Integer): TCustomFilter;
begin
Result := TCustomFilter(FFilterList[Index]);
end;
function TStreamFilters.GetOwnFilters: Boolean;
begin
Result := FFilterList.OwnsObjects;
end;
function TStreamFilters.IsFiltered(const Stream: TStream): Boolean;
begin
Result := IsFiltered(Stream);
end;
procedure TStreamFilters.RemoveAll;
begin
FFilterList.Clear;
end;
procedure TStreamFilters.RemoveFilter(const Filter: TCustomFilter);
begin
FFilterList.Remove(Filter);
end;
procedure TStreamFilters.SetOwnFilters(const Value: Boolean);
begin
FFilterList.OwnsObjects := Value;
end;
{ TUsedFilters }
constructor TUsedFilters.Create;
begin
FFilterList := TStreamFilters.Create(False);
end;
destructor TUsedFilters.Destroy;
begin
FreeAndNil(FFilterList);
inherited;
end;
function TUsedFilters.FilterList: TStreamFilters;
begin
Result := FFilterList;
end;
end.