📄 mimeutil.pas
字号:
{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Object: Mime support routines (RFC2045).
Creation: May 03, 2003 (Extracted from SmtpProt unit)
Version: 1.03
EMail: http://www.overbyte.be http://www.rtfm.be/fpiette
francois.piette@overbyte.be francois.piette@rtfm.be
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1997-2005 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@overbyte.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
History:
May 03, 2003 V1.00 Initial release
Jun 19, 2003 V1.01 Fixed SplitQuotedPrintableString. Thanks to Arno Garrels
<arno.garrels@gmx.de>
Jan 12, 2004 V1.02 Marc HUBAUT <mhu@wanadoo.fr> fixed DoFileEncBase64 in case
of file size is a multple of 3.
May 31, 2004 V1.03 Used ICSDEFS.INC, added const with version and copyright
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
{$I ICSDEFS.INC}
{$IFDEF DELPHI6_UP}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN SYMBOL_LIBRARY OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$ENDIF}
{$IFNDEF VER80} { Not for Delphi 1 }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
{$ENDIF}
{$IFDEF BCB3_UP}
{$ObjExportAll On}
{$ENDIF}
unit MimeUtil;
interface
{$R-}
uses
SysUtils, Classes;
const
TMimeUtilsVersion = 103;
CopyRight : String = ' MimeUtils (c) 1997-2005 F. Piette V1.03 ';
{ Functions to encode/decode string as a "quoted-printable" string RFC2045}
function EncodeQuotedPrintable(const S: String) : String;
function DecodeQuotedPrintable(const S: String) : String;
function SplitQuotedPrintableString(const S : String) : String;
{ Find a Content-Type from a file name }
function FilenameToContentType(FileName : String) : String;
{ Base 64 encoding }
function Base64Encode(Input : String) : String;
function Base64Decode(Input : String) : String;
function InitFileEncBase64(const FileName : String;
ShareMode : Word) : TStream;
function DoFileEncBase64(var Stream : TStream;
var More : Boolean) : String;
procedure EndFileEncBase64(var Stream : TStream);
{ Dot a start of line escaping for SMTP and NNTP (double the dot) }
procedure DotEscape(var S : String);
implementation
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ See also SplitQuotedPrintableString ! }
function EncodeQuotedPrintable(const S: String) : String;
var
I, J : Integer;
begin
Result := '';
I := 1;
while I <= Length(S) do begin
J := I;
while (I <= Length(S)) and
(S[I] <> '=') and
(S[I] >= ' ') and
(Ord(S[I]) <= 126) do
Inc(I);
if I > Length(S) then begin
if J = 1 then
Result := S { Optimisation }
else
Result := Result + Copy(S, J, I - J);
Exit;
end;
Result := Result + Copy(S, J, I - J) + '=' +
UpperCase(IntToHex(Ord(S[I]), 2));
Inc(I);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ A line ending with an equal sign is continued on the next line. This is }
{ what RFC2045 refers as a "soft line break". }
{ This routine doesn't take care of the equal sign at the end of string. }
{ It is simply ignored. The caller must check that condition and merge }
{ successives lines. But the routine handle embedded soft line break. }
function DecodeQuotedPrintable(const S: String) : String;
var
I, J : Integer;
begin
Result := '';
I := 1;
while I <= Length(S) do begin
J := I;
while (I <= Length(S)) and (S[I] <> '=') do
Inc(I);
Result := Result + Copy(S, J, I - J);
if I >= Length(S) then
break;
if S[I + 1] = #13 then { Could also check for #10 }
{ Soft line break, nothing to do except continuing }
else
Result := Result + Char(StrToInt('$' + Copy(S, I + 1, 2)));
Inc(I, 3);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function SplitQuotedPrintableString(const S : String) : String;
var
I, J : Integer;
begin
if Length(S) <= 76 then begin
{ No need to split }
Result := S;
Exit;
end;
Result := '';
J := 1;
I := 76;
while TRUE do begin
if S[I - 1] = '=' then
Dec(I)
else if S[I - 2] = '=' then
Dec(I, 2);
Result := Result + Copy(S, J, I - J) + '=' + #13#10;
J := I;
Inc(I, 75);
if I > Length(S) then begin
Result := Result + Copy(S, J, I - J);
break;
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure DotEscape(var S : String);
var
I : Integer;
begin
if S = '' then
Exit;
if S[1] = '.' then begin
Insert('.', S, 1);
I := 3;
end
else
I := 1;
while I < (Length(S) - 2) do begin
if (S[I] = #13) and (S[I + 1] = #10) and (S[I + 2] = '.') then begin
Insert('.', S, I + 2);
Inc(I, 4);
continue;
end;
Inc(I);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function FilenameToContentType(FileName : String) : String;
var
Ext : String;
begin
{ We probably should the registry to find MIME type for known file types }
Ext := LowerCase(ExtractFileExt(FileName));
if Length(Ext) > 1 then
Ext := Copy(Ext, 2, Length(Ext));
if (Ext = 'htm') or (Ext = 'html') then
Result := 'text/html'
else if Ext = 'gif' then
Result := 'image/gif'
else if Ext = 'bmp' then
Result := 'image/bmp'
else if (Ext = 'jpg') or (Ext = 'jpeg') then
Result := 'image/jpeg'
else if Ext = 'txt' then
Result := 'text/plain'
else
Result := 'application/octet-stream';
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function InitFileEncBase64(
const FileName : String;
ShareMode : Word) : TStream;
begin
Result := TFileStream.Create(FileName, fmOpenRead or ShareMode);
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -