uses lacrt;

Procedure Zakoduj_Do_B64(zdr,cil:pchar;zdr_vel:longint;var cil_vel:longint);
const b64:pchar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var zdr_3:array[0..2] of byte;
    cil_4:array[0..3] of byte;
    i,j,m,n:longint;
begin
cil_vel:=0;
i:=0;
j:=0;
m:=zdr_vel div 3 * 3;
n:=zdr_vel mod 3;


while i<m do
   begin
   cil[j+0]:=b64[byte(zdr[i]) shr 2];
   cil[j+1]:=b64[(byte(zdr[i]) and 3) shl 4 or (byte(zdr[i+1]) shr 4)];
   cil[j+2]:=b64[(byte(zdr[i+1]) and 15) shl 2 or (byte(zdr[i+2]) shr 6)];
   cil[j+3]:=b64[byte(zdr[i+2]) and 63];
   inc(i,3);
   inc(j,4);
   end;

if n=1 then
   begin
   cil[j+0]:=b64[byte(zdr[i]) shr 2];
   cil[j+1]:=b64[(byte(zdr[i]) and 3) shl 4];
   cil[j+2]:='=';
   cil[j+3]:='=';
   inc(i,1);
   inc(j,4);
   end
   else
if n=2 then
   begin
   cil[j+0]:=b64[byte(zdr[i]) shr 2];
   cil[j+1]:=b64[(byte(zdr[i]) and 3) shl 4 or (byte(zdr[i+1]) shr 4)];
   cil[j+2]:=b64[(byte(zdr[i+1]) and 15) shl 2];
   cil[j+3]:='=';
   end;
{if n=0 then ...nedelej nic...}
cil_vel:=j;
end;


var f1,f2:file;
    l1,l2:longint;

    s1,s2:string;
    k:string[4];
    p1,p2:pchar;

begin
s1:=ParamStr(1);
if s1='' then begin writeln('USAGE: FILE2B64 <infile>');Halt(0);end;
if not (ExistFile(s1)) then begin writeln('The file "'+s1+'" does not exist.');Halt(0);end;
if Convert_UP(StripExt(s1))='.INC' then k:='.IN2' else k:='.INC';
Assign(f1,s1);
Reset(f1,1);
l1:=FileSize(f1);
if l1=0 then begin Close(f1);writeln('The file "'+s1+'" is empty.');Halt(0);end;
GetMem(p1,l1);
BlockRead(f1,p1^,l1);
Close(f1);

s2:=StripPathName(s1)+k;
Assign(f2,s2);
Rewrite(f2,1);
GetMem(p2,l1*4 div 3+4);
Zakoduj_Do_B64(p1,p2,l1,l2);
BlockWrite(f2,p2^,l2);
Close(f2);
end.
