16,749
社区成员
发帖
与我相关
我的任务
分享
type
TBytes = array of Byte;
function DecodeBase64(Encoded: String):TBytes;
const
Map: array[Char] of Byte = (
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var
I6,I8: Cardinal;
P,P1: PChar;
I,Count:Integer;
begin
I := Length(Encoded);
SetLength(result,I div 4 * 3);
P1 := PChar(Result);
Count := 0;
P := PChar(Encoded);
while I>0 do
begin
if I<4 then break;
while(P^ in [#$20,#13,#10]) do begin Inc(P);dec(I); end;
if I<4 then break;
I6 := Map[P^];
Inc(P);
while(P^ in [#$20,#13,#10]) do begin Inc(P);dec(I); end;
if I<3 then break;
I6 := (I6 shl 6) + Map[P^];
Inc(P);
while(P^ in [#$20,#13,#10]) do begin Inc(P);dec(I); end;
if I<2 then break;
if P^ = '=' then Dec(Count);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
while(P^ in [#$20,#13,#10]) do begin Inc(P);dec(I); end;
if I<1 then break;
if P^ = '=' then Dec(Count);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
P1^ := Chr(I6 shr 16);
Inc(P1);
Inc(Count);
P1^ := Chr((I6 shr 8) and $ff);
Inc(P1);
Inc(Count);
P1^ := Chr(I6 and $ff);
Inc(P1);
Inc(Count);
Dec(I,4);
end;
SetLength(Result, Count);
end;
function WriteToFile(FileName: String;Data:PChar;Length:DWORD): DWORD;
var
FFileHandle:THandle;
BytesToWrite,WroteSize:DWORD;
Buffer:PChar;
begin
Result:=0;
FFileHandle:=CreateFile(PChar(LocalExpandFileName(FileName)),
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ,
nil,
OPEN_ALWAYS,//CREATE_NEW
FILE_ATTRIBUTE_NORMAL,
0);
if FFileHandle=INVALID_HANDLE_VALUE then
begin
Result:=GetLastError;
Exit;
end;
try
if GetLastError=ERROR_ALREADY_EXISTS then
begin
if SetFilePointer(FFileHandle,0,nil,FILE_END)=INVALID_SET_FILE_POINTER then
begin
Result:=GetLastError;
end
else
SetLastError(0);
end;
Buffer := Data;
BytesToWrite := Length;
while BytesToWrite>0 do
begin
WroteSize:=0;
if Not WriteFile(FFileHandle,Buffer^,BytesToWrite,WroteSize,nil) then
begin
Result:=GetLastError;
Exit;
end;
Buffer:=Pointer(DWORD(Buffer)+WroteSize);
Dec(BytesToWrite,WroteSize);
end;
finally
CloseHandle(FFileHandle);
end;
end;
var
Bytes :TBytes;
Src: String;
begin
Bytes := DecodeBase64(Src);
WriteToFile('c:\aaa.xls',PChar(Bytes),Length(Bytes));
SetLength(Bytes,0);
Bytes := Nil;
end;
type
TBytes = array of Byte;
function DecodeBase64(Encoded: String):TBytes;
const
Map: array[Char] of Byte = (
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var
I6,I8: Cardinal;
P,P1: PChar;
I,Count:Integer;
begin
I := Length(Encoded);
SetLength(result,I div 4 * 3);
P1 := PChar(Result);
Count := 0;
P := PChar(Encoded);
while I>0 do
begin
if I<4 then break;
I6 := Map[P^];
Inc(P);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
if P^ = '=' then Dec(Count);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
if P^ = '=' then Dec(Count);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
P1^ := Chr(I6 shr 16);
Inc(P1);
Inc(Count);
P1^ := Chr((I6 shr 8) and $ff);
Inc(P1);
Inc(Count);
P1^ := Chr(I6 and $ff);
Inc(P1);
Inc(Count);
Dec(I,4);
end;
SetLength(Result, Count);
end;
function DecodeBase64(Encoded: String):String;
const
Map: array[Char] of Byte = (
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var
I6,I8: Cardinal;
P,P1: PChar;
I,Count:Integer;
begin
I := Length(Encoded);
SetLength(result,I div 4 * 3);
P1 := PChar(Result);
Count := 0;
P := PChar(Encoded);
while I>0 do
begin
if I<4 then break;
I6 := Map[P^];
Inc(P);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
if P^ = '=' then Dec(Count);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
if P^ = '=' then Dec(Count);
I6 := (I6 shl 6) + Map[P^];
Inc(P);
P1^ := Chr(I6 shr 16);
Inc(P1);
Inc(Count);
P1^ := Chr((I6 shr 8) and $ff);
Inc(P1);
Inc(Count);
P1^ := Chr(I6 and $ff);
Inc(P1);
Inc(Count);
Dec(I,4);
end;
SetLength(Result, Count);
end;