Base64 encoding/decoding Delphi source code

Source: Internet
Author: User

Unit cnbase64;

Interface

Uses
Sysutils, windows;

Function base64encode (inputdata: string; var outputdata: string): byte;
{* Encode the data in base64. if the encoding is successful, base64_ OK is returned.
| <PRE>
Inputdata: String-data to be encoded
VaR outputdata: String-encoded data
| </PRE>}

Function base64decode (inputdata: string; var outputdata: string): byte;
{* Perform base64 decoding on the data. If the data is decoded successfully, base64_ OK is returned.
| <PRE>
Inputdata: String-data to be decoded
VaR outputdata: String-decoded data
| </PRE>}

Const
Base64_ OK = 0; // The conversion is successful.
Base64_error = 1;
// Conversion error (unknown error) (e.g. Can't encode octet in input stream)-> error in implementation
Base64_invalid = 2;
// The input string contains invalid characters, which may occur when filterdecodeinput is set to false)
Base64_length = 3; // The data length is invalid.
Base64_dataleft = 4;
// Too much input data left (receveived 'end of encoded data' but not end of input string)
Base64_padding = 5; // the input data fails to end with the correct filling character

Implementation

VaR
Filterdecodeinput: Boolean = true;
Const
Base64tablelength = 64;
Base64table: String [base64tablelength] =
'Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + *';
Pad = ';

function base64encode (inputdata: string; var outputdata: string): byte;
var
I: integer;
currentb, prevb: byte;
C: byte;
S: Char;
inputlength: integer;

function valuetocharacter (value: byte; var character: Char): Boolean;
//*********************************** * ****************************
// .. the value in the Base64TableLength-1 range, converted to the character corresponding to base64 encoding
, returns true if the conversion is successful.
//***************************** ***********************************
begin
result: = true;
If (value> base64tablelength-1) Then
result: = false
else
character: = base64table [value + 1];
end;

Begin
Outputdata: = '';
Inputlength: = length (inputdata );
I: = 1;
If (inputlength = 0) then begin
Result: = base64_ OK;
Exit;
End;

Repeat
// The first conversion
Currentb: = ord (inputdata [I]);
I: = I + 1;
Inputlength: = inputlength-1;
C: = (currentb SHR 2 );
If not valuetocharacter (C, S) then begin
Result: = base64_error;
Exit;
End;
Outputdata: = outputdata + S;
Prevb: = currentb;

// Second Conversion
If inputlength = 0 then
Currentb: = 0
Else begin
Currentb: = ord (inputdata [I]);
I: = I + 1;
End;
Inputlength: = inputlength-1;
C: = (prevb and $03) SHL 4 + (currentb SHR 4 );
// Retrieve the last 4 digits of XX and move the last 4 digits to the left and merge the last 4 digits of XX into six digits.
If not valuetocharacter (C, S) then
{// Check whether the obtained characters are in base64table} begin
Result: = base64_error;
Exit;
End;
Outputdata: = outputdata + S;
Prevb: = currentb;

// The third Conversion
If inputlength <0 then
S: = pad
Else begin
If inputlength = 0 then
Currentb: = 0
Else begin
Currentb: = ord (inputdata [I]);
I: = I + 1;
End;
Inputlength: = inputlength-1;
C: = (prevb and $ 0f) SHL 2 + (currentb SHR 6 );
// Retrieve the last four digits of XX and move the last two digits to the left and the last six digits to six digits.
If not valuetocharacter (C, S) then
{// Check whether the obtained characters are in base64table} begin
Result: = base64_error;
Exit;
End;
End;
Outputdata: = outputdata + S;

// fourth conversion
If inputlength <0 then
S: = pad
else begin
C: = (currentb and $ 3f); // retrieve the last 6 digits of XX.
if not valuetocharacter (C, S) then
{// check whether the obtained characters are in base64table} begin
result: = base64_error;
exit;
end;
outputdata: = outputdata + S;
until inputlength <= 0;
result: = base64_ OK;
end;

function base64decode (inputdata: string; var outputdata: string): byte;
var
I: integer;
inputlength: integer;
currentb, prevb: byte;
C: byte;
S: Char;

function charactertovalue (character: Char; VaR value: byte): Boolean;
//*********************************** * ****************************
// convert the character 0 .. the value in the Base64TableLength-1 range, returns true if the conversion is successful
// returns true (that is, the character in base64table)
//*********************************** *****************************
begin
result: = true;
value: = pos (character, base64table);
If value = 0 then
result: = false
else
value: = value-1;
end;

function filterline (inputdata: string): string;
//*********************************** * ****************************
// filter all objects not in the base64table characters in, the returned value is the filtered character
//***************************** ***********************************
vaR
F: byte;
I: integer;
begin
result: = '';
for I: = 1 to length (inputdata) DO
If charactertovalue (inputdata [I], F) or (inputdata [I] = pad) Then
result: = Result + inputdata [I];
end;

Begin
If (inputdata = '') then begin
Result: = base64_ OK;
Exit;
End;
Outputdata: = '';

If filterdecodeinput then
Inputdata: = filterline (inputdata );

Inputlength: = length (inputdata );
If inputlength mod 4 <> 0 then begin
Result: = base64_length;
Exit;
End;

I: = 0;
Repeat
// The first conversion
I: = I + 1;
S: = inputdata [I];
If not charactertovalue (S, currentb) then begin
Result: = base64_invalid;
Exit;
End;
I: = I + 1;
S: = inputdata [I];
If not charactertovalue (S, prevb) then begin
Result: = base64_invalid;
Exit;
End;

C: = (currentb SHL 2) + (prevb SHR 4 );
Outputdata: = outputdata + CHR (C );

// Second Conversion
I: = I + 1;
S: = inputdata [I];
If S = pad then begin
If (I <> inputlength-1) then begin
Result: = base64_dataleft;
Exit;
End
Else
If inputdata [I + 1] <> pad then begin
Result: = base64_padding;
Exit;
End;
End
Else begin
If not charactertovalue (S, currentb) then begin
Result: = base64_invalid;
Exit;
End;
C: = (prevb SHL 4) + (currentb SHR 2 );
Outputdata: = outputdata + CHR (C );
End;

// The third Conversion
I: = I + 1;
S: = inputdata [I];
If S = pad then begin
If (I <> inputlength) then begin
Result: = base64_dataleft;
Exit;
End;
End
Else begin
If not charactertovalue (S, prevb) then begin
Result: = base64_invalid;
Exit;
End;
C: = (currentb SHL 6) + (prevb );
Outputdata: = outputdata + CHR (C );
End;

Until (I> = inputlength );
Result: = base64_ OK;
End;

End.
========================================================== ========================================================== ========
Uses cnbase64;
Cnbase64.base64encode (edit1.text, psw64 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.