-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc_streams.pas
executable file
·154 lines (120 loc) · 3.58 KB
/
rfc_streams.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
{$I xpdefine.inc }
unit rfc_streams;
interface
uses
xpstreams_codec,
typeform,
classes;
{ -------------------------- Unix line ends -------------------------- }
type TCRLFtoLFStream = class(TCodecStream)
private
BytesWritten: LongInt;
LastCharWasCR: Boolean;
public
constructor Create;
destructor Destroy; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: System.Word): Longint; override;
end;
{ ------------------- RFC 2821/976/977 Dot Escaping ------------------ }
type TDotEscapeStream = class(TCodecStream)
private
BytesWritten: LongInt;
LastWasCRLF,LastWasCR: Boolean;
public
constructor Create;
destructor Destroy; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: System.Word): Longint; override;
end;
implementation
const
cr: char = #13;
{ -------------------------- Unix line ends -------------------------- }
constructor TCRLFtoLFStream.Create;
begin
inherited Create;
BytesWritten:=0;
LastCharWasCR:=false;
end;
function TCRLFtoLFStream.Write(const Buffer; Count: Longint): Longint;
var I:Longint;
begin
Result := 0;
if Count<=0 then
exit;
if LastCharWasCR and (PChar(@Buffer)^<>#10) then
FOtherStream.WriteBuffer(cr,1);
for i:=1 to Count-1 do
if ((PChar(@Buffer)+i-1)^=#13) and
((PChar(@Buffer)+i )^=#10) then
begin
if i-Result-1>0 then
Inc(Result,FOtherStream.Write((PChar(@Buffer)+Result)^,i-Result-1));
Inc(Result,1);
if Result<>i then
exit;
end;
LastCharWasCR := (PChar(@Buffer)+Count-1)^=#13;
if Count-Result>0 then
Inc(Result,FOtherStream.Write((PChar(@Buffer)+Result)^,Count-Result-iif(LastCharWasCR,1,0)));
if LastCharWasCR then
Inc(Result);
Inc(BytesWritten,Result);
end;
function TCRLFtoLFStream.Seek(Offset: Longint; Origin: System.Word): Longint;
begin
Result := BytesWritten;
if not ((((Origin = soFromCurrent) or (Origin = soFromEnd)) and (Offset = 0))
or ((Origin = soFromBeginning) and (Offset = Result))) then
raise EStreamError.Create('Invalid stream operation');
end;
destructor TCRLFtoLFStream.Destroy;
begin
if LastCharWasCR then
FOtherStream.WriteBuffer(cr,1);
end;
{ ------------------- RFC 2821/976/977 Dot Escaping ------------------ }
constructor TDotEscapeStream.Create;
begin
inherited Create;
BytesWritten:=0;
LastWasCRLF:=false;
LastWasCR:=false;
end;
destructor TDotEscapeStream.Destroy;
const delim: array[0..4] of char = #13#10'.'#13#10;
var o:integer;
begin
o:=iif(LastWasCRLF,2,0);
FOtherStream.WriteBuffer(delim[o],sizeof(delim)-o);
inherited Destroy;
end;
function TDotEscapeStream.Write(const Buffer; Count: Longint): Longint;
var I,Beg:Longint;
begin
Result := 0;
Beg := 0;
for i:=0 to Count-1 do
begin
if LastWasCRLF and ((PChar(@Buffer)+i)^='.') then
begin
Inc(Beg,FOtherStream.Write((PChar(@Buffer)+Beg)^,i-beg+1)-1);
if Beg<>i then exit; // write error
end;
LastWasCRLF:=((PChar(@Buffer)+i)^=#10) and LastWasCR;
LastWasCR :=((PChar(@Buffer)+i)^=#13);
end;
if Count-Beg>0 then
Inc(Beg,FOtherStream.Write((PChar(@Buffer)+Beg)^,Count-beg));
Result:=Beg;
Inc(BytesWritten,Result);
end;
function TDotEscapeStream.Seek(Offset: Longint; Origin: System.Word): Longint;
begin
Result := BytesWritten;
if not ((((Origin = soFromCurrent) or (Origin = soFromEnd)) and (Offset = 0))
or ((Origin = soFromBeginning) and (Offset = Result))) then
raise EStreamError.Create('Invalid stream operation');
end;
end.