home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 11
/
Freelog011.iso
/
BestOf
/
PhoenixMail
/
Source
/
phoenix
/
BASE64Sup.pas
next >
Wrap
Pascal/Delphi Source File
|
1999-01-06
|
4KB
|
170 lines
{*****************************************************************************
*
* BASE64Sup.pas - BASE 64 support (19-July-1998)
*
* Copyright (c) 1998-99 Michael Haller
*
* Author: Michael Haller
* E-mail: michael@discountdrive.com
* Homepage: http://www.discountdrive.com/sunrise/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*----------------------------------------------------------------------------
*
* Revision history:
*
* DATE REV DESCRIPTION
* ----------- --- ----------------------------------------------------------
*
*****************************************************************************}
unit BASE64Sup;
interface
procedure PrepareBase64Support;
function StringToBase64(S: String): String;
function Base64ToString(S: String): String;
implementation
var
Bit6ToBase64: array[0..63] of Byte;
Base64ToBit6: array[0..255] of SmallInt;
iEncodeInput: SmallInt;
EncodeShifter: LongInt;
iDecodeInput: SmallInt;
DecodeShifter: LongInt;
procedure Init;
begin
iEncodeInput := 0;
EncodeShifter := 0;
iDecodeInput := 0;
DecodeShifter := 0;
end;
function AddByteToEncode(B: Byte): Boolean;
begin
Result := True;
if iEncodeInput = 3 then Exit;
EncodeShifter := EncodeShifter shl 8;
EncodeShifter := EncodeShifter or B;
Inc(iEncodeInput);
Result := (iEncodeInput = 3);
end;
function GetBase64String: String;
var
S: String;
I: SmallInt;
begin
Result := '';
if iEncodeInput = 0 then Exit;
EncodeShifter := EncodeShifter shl ((3-iEncodeInput)*8);
S := '';
for I := 1 to 4 do begin
S := Chr(Bit6ToBase64[EncodeShifter and $3F])+S;
EncodeShifter := EncodeShifter shr 6;
end;
for I := iEncodeInput+2 to Length(S) do S[I] := '=';
Result := S;
EncodeShifter := 0;
iEncodeInput := 0;
end;
function AddBase64ToDecode(B: Byte): Boolean;
begin
Result := True;
if iDecodeInput = 4 then Exit;
B := Base64ToBit6[B];
if B > $40 then begin Result := False; Exit; end;
if B = $40 then begin B := 0; {Exit;} end;
DecodeShifter := DecodeShifter shl 6;
DecodeShifter := DecodeShifter or B;
Inc(iDecodeInput);
Result := (iDecodeInput = 4);
end;
function GetByteString: String;
var
S: String;
I, iDecodeOutput: SmallInt;
begin
Result := '';
if iDecodeInput = 0 then Exit;
DecodeShifter := DecodeShifter shl ((4-iDecodeInput)*6);
S := '';
for I := 1 to 3 do begin
S := Chr(DecodeShifter and $FF)+S;
DecodeShifter := DecodeShifter shr 8;
end;
iDecodeOutput := iDecodeInput;
if iDecodeOutput = 4 then iDecodeOutput := 3;
Result := '';
for I := 1 to iDecodeOutput do
Result := Result+S[I];
DecodeShifter := 0;
iDecodeInput := 0;
end;
procedure PrepareBase64Support;
var
I: SmallInt;
begin
for I := 0 to 25 do Bit6ToBase64[I] := Ord('A')+I;
for I := 0 to 25 do Bit6ToBase64[26+I] := Ord('a')+I;
for I := 0 to 9 do Bit6ToBase64[52+I] := Ord('0')+I;
Bit6ToBase64[62] := Ord('+');
Bit6ToBase64[63] := Ord('/');
for I := 0 to 255 do Base64ToBit6[I] := 255;
for I := 0 to 63 do Base64ToBit6[Bit6ToBase64[I]] := I;
Base64ToBit6[Ord('=')] := $40;
Init;
end;
function StringToBase64(S: String): String;
var
I: SmallInt;
sOut: String;
begin
Init;
sOut := '';
for I := 1 to Length(S) do
if AddByteToEncode(Ord(S[I])) then
sOut := sOut+GetBase64String;
sOut := sOut+GetBase64String;
Result := sOut;
end;
function Base64ToString(S: String): String;
var
I: SmallInt;
sOut: String;
begin
Init;
sOut := '';
for I := 1 to Length(S) do begin
if AddBase64ToDecode(Ord(S[I])) then begin
sOut := sOut+GetByteString;
end;
end;
sOut := sOut+GetByteString;
Result := sOut;
end;
end.