⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mimeqpb64.pas

📁 MIME(Quoted-Printable & Base64) 编码解码程序
💻 PAS
字号:
//---------------------------------------------------------------------------
//  MIME(Quoted-Printable & Base64) Encode/Decode unit. (PAS)
//  Copyright (c) 2000, 01 Mental Studio - http://msrc.126.com
//  Author : Raptor - raptorz@163.com
//---------------------------------------------------------------------------
Unit MimeQPB64;

Interface

Uses
    Classes, SysUtils;

Type
    TMimeType = ( mtNone, mtQP, mtBase64, mtUnknown );
    PByte = ^Byte;

Function  MimeEncode( aSrc : TMemoryStream; aType : TMimeType ) : String; Overload;
Function  MimeEncode( aSrc : String;        aType : TMimeType ) : String; Overload;
Procedure MimeDecode( aDest : TMemoryStream; aSrc : String; aType : TMimeType );

Implementation

{$L mimeb64d.obj}

Function QPEncode(     aDest : PChar; aSrc : PByte; aLen : Integer ) : Integer; External;
Function QPDecode(     aDest : PByte; aSrc : PChar                 ) : Integer; External;
Function Base64Encode( aDest : PChar; aSrc : PByte; aLen : Integer ) : Integer; External;
Function Base64Decode( aDest : PByte; aSrc : PChar                 ) : Integer; External;

//  因为DELPHI不包含string.h中的函数,所以要写这么个函数
Function _strlen( aStr : PChar ) : Integer; cdecl;
Begin
	Result := Length( aStr );
End;

//  将数据流编码为 MIME 字符串
//  aSrc 为源数据流, aType 为 MIME 编码类型
//  返回结果字符串
Function MimeEncode( aSrc : TMemoryStream; aType : TMimeType ) : String;
Var
    buf : TMemoryStream;
Begin
    buf := TMemoryStream.Create;
    Try
        If ( aType = mtQP ) Then           //  QPEncode
        Begin
        	buf.Size := aSrc.Size * 3 + 1;
            QPEncode( PChar( buf.Memory ), PByte( aSrc.Memory ), aSrc.Size );
        End
        Else If ( aType = mtBase64 ) Then  //  Base64Encode
        Begin
        	buf.Size := aSrc.Size * 4 DIV 3 + 1;
            Base64Encode( PChar( buf.Memory ), PByte( aSrc.Memory ), aSrc.Size );
        End
        Else
        	Raise Exception.Create( 'Invalid MIME type!' );
        Result := PChar( buf.Memory );
    Finally
        buf.Free;
    End;
End;

//  将字符串编码为 MIME 字符串
//  aSrc 为源字符串, aType 为 MIME 编码类型
//  返回结果字符串
Function MimeEncode( aSrc : String; aType : TMimeType ) : String;
Var
    buf : TMemoryStream;
Begin
    buf := TMemoryStream.Create;
    Try
        If ( aType = mtQP ) Then           //  QPEncode
        Begin
        	buf.Size := Length( aSrc ) * 3 + 1;
            QPEncode( PChar( buf.Memory ), PByte( PChar( aSrc ) ), Length( aSrc ) );
        End
        Else If ( aType = mtBase64 ) Then  //  Base64Encode
        Begin
        	buf.Size := Length( aSrc ) * 4 DIV 3 + 1;
            Base64Encode( PChar( buf.Memory ), PByte( PChar( aSrc ) ), Length( aSrc ) );
        End
        Else
        	Raise Exception.Create( 'Invalid MIME type!' );
        Result := PChar( buf.Memory );
    Finally
        buf.Free;
    End;
End;

//  从字符串中解码 MIME 数据
//  aDest 为结果数据流, aSrc 为源字符串, aType 为 MIME 编码类型
Procedure MimeDecode( aDest : TMemoryStream; aSrc : String; aType : TMimeType );
Begin
    aDest.Size := Length( aSrc ) + 1;
    If ( aType = mtQP ) Then           //  QPDecode
        aDest.Size := QPDecode( PByte( aDest.Memory ), PChar( aSrc ) )
    Else If ( aType = mtBase64 ) Then  //  Base64Decode
        aDest.Size := Base64Decode( PByte( aDest.Memory ), PChar( aSrc ) )
    Else
        Raise Exception.Create( 'Invalid MIME type!' );
End;

End.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -