-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIMimeStreams.pas
188 lines (156 loc) · 5.57 KB
/
DIMimeStreams.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
{ The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is DIMimeStreams.pas.
The Initial Developer of the Original Code is Ralf Junker <[email protected]>.
All Rights Reserved.
------------------------------------------------------------------------------ }
unit DIMimeStreams;
{$I DICompilers.inc}
interface
uses
{$IFDEF HAS_UNITSCOPE}System.Classes{$ELSE}Classes{$ENDIF};
procedure MimeEncodeStream(
const InputStream: TStream;
const OutputStream: TStream);
procedure MimeEncodeStreamNoCRLF(
const InputStream: TStream;
const OutputStream: TStream);
procedure MimeDecodeStream(
const InputStream: TStream;
const OutputStream: TStream);
procedure MimeEncodeFile(
const InputFileName: string;
const OutputFileName: string);
procedure MimeEncodeFileNoCRLF(
const InputFileName: string;
const OutputFileName: string);
procedure MimeDecodeFile(
const InputFileName: string;
const OutputFileName: string);
implementation
uses
DISystemCompat,
{$IFDEF HAS_UNITSCOPE}System.SysUtils{$ELSE}SysUtils{$ENDIF}, DIMime;
const
MIME_BUFFER_SIZE = MIME_DECODED_LINE_BREAK * 3 * 4 * 4;
procedure MimeEncodeStream(
const InputStream: TStream;
const OutputStream: TStream);
var
InputBuffer: packed array[0..MIME_BUFFER_SIZE - 1] of Byte;
OutputBuffer: packed array[0..(MIME_BUFFER_SIZE + 2) div 3 * 4 + MIME_BUFFER_SIZE div MIME_DECODED_LINE_BREAK * 2 - 1] of Byte;
BytesRead: NativeUInt;
iDelta, ODelta: NativeUInt;
begin
BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
while BytesRead = SizeOf(InputBuffer) do
begin
MimeEncodeFullLines(InputBuffer, SizeOf(InputBuffer), OutputBuffer);
OutputStream.Write(OutputBuffer, SizeOf(OutputBuffer));
BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
end;
MimeEncodeFullLines(InputBuffer, BytesRead, OutputBuffer);
iDelta := BytesRead div MIME_DECODED_LINE_BREAK;
ODelta := iDelta * (MIME_ENCODED_LINE_BREAK + 2);
iDelta := iDelta * MIME_DECODED_LINE_BREAK;
MimeEncodeNoCRLF((PAnsiChar(@InputBuffer) + iDelta)^, BytesRead - iDelta, (PAnsiChar(@OutputBuffer) + ODelta)^);
OutputStream.Write(OutputBuffer, MimeEncodedSize(BytesRead));
end;
procedure MimeEncodeStreamNoCRLF(
const InputStream: TStream;
const OutputStream: TStream);
var
InputBuffer: packed array[0..MIME_BUFFER_SIZE - 1] of Byte;
OutputBuffer: packed array[0..((MIME_BUFFER_SIZE + 2) div 3) * 4 - 1] of Byte;
BytesRead: NativeUInt;
begin
BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
while BytesRead = SizeOf(InputBuffer) do
begin
MimeEncodeNoCRLF(InputBuffer, SizeOf(InputBuffer), OutputBuffer);
OutputStream.Write(OutputBuffer, SizeOf(OutputBuffer));
BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
end;
MimeEncodeNoCRLF(InputBuffer, BytesRead, OutputBuffer);
OutputStream.Write(OutputBuffer, MimeEncodedSizeNoCRLF(BytesRead));
end;
procedure MimeDecodeStream(
const InputStream: TStream;
const OutputStream: TStream);
var
ByteBuffer, ByteBufferSpace: NativeUInt;
InputBuffer: packed array[0..MIME_BUFFER_SIZE - 1] of Byte;
OutputBuffer: packed array[0..(MIME_BUFFER_SIZE + 3) div 4 * 3 - 1] of Byte;
BytesRead: NativeUInt;
begin
ByteBuffer := 0;
ByteBufferSpace := 4;
BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
while BytesRead > 0 do
begin
OutputStream.Write(OutputBuffer, MimeDecodePartial(InputBuffer, BytesRead, OutputBuffer, ByteBuffer, ByteBufferSpace));
BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
end;
OutputStream.Write(OutputBuffer, MimeDecodePartialEnd(OutputBuffer, ByteBuffer, ByteBufferSpace));
end;
procedure MimeEncodeFile(
const InputFileName: string;
const OutputFileName: string);
var
InputStream, OutputStream: TFileStream;
begin
InputStream := TFileStream.Create(InputFileName, fmOpenRead or fmShareDenyWrite);
try
OutputStream := TFileStream.Create(OutputFileName, fmCreate);
try
MimeEncodeStream(InputStream, OutputStream);
finally
OutputStream.Free;
end;
finally
InputStream.Free;
end;
end;
procedure MimeEncodeFileNoCRLF(
const InputFileName: string;
const OutputFileName: string);
var
InputStream, OutputStream: TFileStream;
begin
InputStream := TFileStream.Create(InputFileName, fmOpenRead or fmShareDenyWrite);
try
OutputStream := TFileStream.Create(OutputFileName, fmCreate);
try
MimeEncodeStreamNoCRLF(InputStream, OutputStream);
finally
OutputStream.Free;
end;
finally
InputStream.Free;
end;
end;
procedure MimeDecodeFile(
const InputFileName: string;
const OutputFileName: string);
var
InputStream, OutputStream: TFileStream;
begin
InputStream := TFileStream.Create(InputFileName, fmOpenRead or fmShareDenyWrite);
try
OutputStream := TFileStream.Create(OutputFileName, fmCreate);
try
MimeDecodeStream(InputStream, OutputStream);
finally
OutputStream.Free;
end;
finally
InputStream.Free;
end;
end;
end.