function Base64Encode(InputData:string;var OutputData:string):Byte;
var
Times, SrcLen, i: integer;
x1, x2, x3, x4: char;
xt: byte;
begin
SrcLen := length(InputData);
if SrcLen mod 3 = 0 then
Times := SrcLen div 3
else
Times := SrcLen div 3 + 1;
SetLength(OutputData, Times * 4); //一次分配整块内存,避免一次次字符串相加,一次次释放分配内存
for i := 0 to times - 1 do
begin
if SrcLen >= (3 + i * 3) then
begin
x1 := EnCodeTab[(ord(InputData[1 + i * 3]) shr 2)];
xt := (ord(InputData[1 + i * 3]) shl 4) and 48;
xt := xt or (ord(InputData[2 + i * 3]) shr 4);
x2 := EnCodeTab[xt];
xt := (Ord(InputData[2 + i * 3]) shl 2) and 60;
xt := xt or (ord(InputData[3 + i * 3]) shr 6);
x3 := EnCodeTab[xt];
xt := (ord(InputData[3 + i * 3]) and 63);
x4 := EnCodeTab[xt];
end
else if SrcLen >= (2 + i * 3) then
begin
x1 := EnCodeTab[(ord(InputData[1 + i * 3]) shr 2) ];
xt := (ord(InputData[1 + i * 3]) shl 4) and 48;
xt := xt or (ord(InputData[2 + i * 3]) shr 4);
x2 := EnCodeTab[xt];
xt := (ord(InputData[2 + i * 3]) shl 2) and 60;
x3 := EnCodeTab[xt ];
x4 := '=';
end
else
begin
x1 := EnCodeTab[(ord(InputData[1 + i * 3]) shr 2)];
xt := (ord(InputData[1 + i * 3]) shl 4) and 48;
x2 := EnCodeTab[xt];
x3 := '=';
x4 := '=';
end;
OutputData[I shl 2 + 1] := X1;
OutputData[I shl 2 + 2] := X2;
OutputData[I shl 2 + 3] := X3;
OutputData[I shl 2 + 4] := X4;
end;
result:=BASE64_OK;
end;
function Base64Decode(InputData:string;var OutputData:string):Byte;
var
SrcLen, Times, i: integer;
x1, x2, x3, x4, xt: byte;
C: Integer;
function FilterLine(Source:String): string;
var
P, PP: PChar;
I: Integer;
begin
SrcLen := Length(Source);
GetMem(P, Srclen); //一次分配整块内存,避免一次次字符串相加,一次次释放分配内存
PP := P;
FillChar(P^, Srclen, 0);
for I := 1 to SrcLen do
begin
if Source[I] in ['0'..'9', 'A'..'Z', 'a'..'z', '+', '/', '='] then
begin
PP^ := Source[I];
Inc(PP);
end;
end;
SetString(Result, P, PP - P); //截取有效部分
FreeMem(P, SrcLen);
end;
begin
if (InputData='') then
begin
result:=BASE64_OK;
exit;
end;
OutPutData:='';
if FilterDecodeInput then
InputData:=FilterLine(InputData);
SrcLen := Length(InputData);
SetLength(OutputData, (SrcLen * 3 div 4)); //一次分配整块内存,避免一次次字符串相加,一次次释放分配内存
Times := SrcLen div 4;
C := 1;
for i := 0 to Times - 1 do
begin
x1 := DecodeTable[InputData[1 + i shl 2]];
x2 := DecodeTable[InputData[2 + i shl 2]];
x3 := DecodeTable[InputData[3 + i shl 2]];
x4 := DecodeTable[InputData[4 + i shl 2]];
x1 := x1 shl 2;
xt := x2 shr 4;
x1 := x1 or xt;
x2 := x2 shl 4;
OutputData[C] := Chr(x1);
Inc(C);
if x3 = 64 then
break;
xt := x3 shr 2;
x2 := x2 or xt;
x3 := x3 shl 6;
OutputData[C] := Chr(x2);
Inc(C);
if x4 = 64 then
break;
x3 := x3 or x4;
OutputData[C] := Chr(x3);
Inc(C);
end;
result:=BASE64_OK;
end;
end.Author:
flamingo Time: 2006-1-15 11:56
了不起。问个问题,为什么不把
function Base64Encode(InputData:string;var OutputData:string):Byte;
设计成:
function Base64Encode(InputData:TStream;var OutputData:string):Byte;Author:
wr960204 Time: 2006-1-16 11:18
function Base64Encode(InputData:string;var OutputData:string):Byte;overload;
begin
// 这里的代码见 wr960204 帖子 ......
end;
function Base64Encode(InputData:TStream;var OutputData:string):Byte;overload;
var
Str:TStringStream;
begin
Str:=TStringStream.Create('');
Str.CopyFrom(InputData,InputData.Size);
Result:=Base64Encode(Str.DataString,OutputData);
Str.Free;
end;Author:
天地弦 Time: 2006-2-10 15:24