uses
Math;
var
Base64: array[0..63] of AnsiChar = (
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/');
function IndexOfBase64(const C: AnsiChar): Integer;
begin
for Result := Low(Base64) to High(Base64) do
if Base64[Result] = C then
EXIT;
Result := -1;
end;
function DecodeBase64(Value: AnsiString): AnsiString;
var
iC, iB: Integer;
B: array of Integer;
C: array[0..3] of Integer;
begin
SetLength(B, Floor(Length(Value) / 4) * 3);
iC := 1;
iB := 0;
while iC <= (Length(Value) - 3) do
begin
C[0] := IndexOfBase64(Value[iC]);
C[1] := IndexOfBase64(Value[iC + 1]);
C[2] := IndexOfBase64(Value[iC + 2]);
C[3] := IndexOfBase64(Value[iC + 3]);
B[iB] := (C[0] shl 2) or (C[1] shr 4);
B[iB + 1] := ((C[1] and 15) shl 4) or (C[2] shr 2);
B[iB + 2] := ((C[2] and 3) shl 6) or C[3];
Inc(iC, 4);
Inc(iB, 3);
end;
SetLength(B, Length(B) - (Length(B) mod 16));
for iB := 0 to High(B) do
Result := Result + Chr(B[iB]);
end;