-
Notifications
You must be signed in to change notification settings - Fork 3
/
checksumstreamscommon.pas
53 lines (43 loc) · 1.2 KB
/
checksumstreamscommon.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
unit checksumstreamscommon;
interface
uses sysutils,classes,contnrs;
type EDamagedfile = class(exception);
function checksum(data: pbytearray; size: integer): longword;
procedure writestring(stream: tstream; const s: string);
procedure copyuntil(source: tstream; dest: tmemorystream; key: string);
implementation
procedure copyuntil(source: tstream; dest: tmemorystream; key: string);
var t1: integer;
comp: string;
startpos, endpos: integer;
begin
startpos := source.position;
setlength(comp, length(key));
for t1 := startpos + 1 {don't check the first position..} to source.size - length(key) do begin
source.Seek(t1, sofrombeginning);
endpos := t1;
source.Read(comp[1], length(key));
if comp = key then begin
source.position := startpos;
dest.copyfrom(source, endpos - startpos);
source.position := endpos + length(key);
exit;
end;
end;
end;
procedure writestring(stream: tstream; const s: string);
begin
if length(s) > 0 then
stream.write(s[1], length(s));
end;
{$R-}
function checksum(data: pbytearray; size: integer): longword;
begin
result := 0;
while size > 0 do begin
dec(size);
result := result + data^[size];
end;
end;
{$R+}
end.