{$APPTYPE CONSOLE}
program Convert;
uses
Windows,
SysUtils;
type mass64=Array [0..63] of char;
mass4x6=Array [1..4] of byte;
mass3x8=Array [1..3] of byte;
const mass_base64: mass64=('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','+','/');
file4convert_name: string='test.txt';
file4destination_name: string='default.b64';
var
main_massiv8: mass3x8;
massiv6: mass4x6;
source_file, destination_file: file;
size_dest_file, count_repeat, size_of_inp_data,i,centpercent: longint;
count_tail, cell, index_for: byte;
Procedure Convert_Byte_Massiv(a: mass3x8; var b: mass4x6);
const
pow2in6mult: Array[0..3] of LongInt=(1,64,4096,262144);
var
collect: longint;
i: byte;
Begin
collect:=a[1]*65536 + a[2]*256 + a[3];
for i:=0 to 3 Do
Case i of
0: b[1]:=byte(collect div pow2in6mult[3-i])
Else
b[i+1]:=byte((collect div pow2in6mult[3-i]) mod 64);
End;
for i:=1 to 4 do
b[i]:=ord(mass_base64[b[i]]);
End;
BEGIN
if paramstr(1)<>''
Then
file4convert_name:=paramstr(1);
If Not(FileExists(file4convert_name))
Then
Begin
Writeln('File not found.'#13#10'Run this program: '+
'convert.exe '+
#13#10'default output file name = "default.b64"');
Halt;
End;
if paramstr(2)<>''
Then
file4destination_name:=paramstr(2);
AssignFile(source_file,file4convert_name);
AssignFile(destination_file,file4destination_name);
Reset(source_file,1);
ReWrite(destination_file,1);
size_of_inp_data:=FileSize(source_file);
count_repeat:=size_of_inp_data div 3;
count_tail:=size_of_inp_data mod 3;
size_dest_file:=0;
i:=1;
{яюлюъ бюфмюъ вюярэ - хдер гюохяэ пегскэрхпсчыецн тюикю}
{------------------------------------------------------}
centpercent:=count_repeat div 10;
While i<=count_repeat do
Begin
if (i mod centpercent)=0
Then
Write(IntToStr(i div centpercent)+'0% ');
BlockRead(source_file,main_massiv8,3);
Convert_Byte_Massiv(main_massiv8,massiv6);
BlockWrite(destination_file,massiv6,4);
size_dest_file:=size_dest_file+4;
i:=i+1;
End;
{ДНОХЯШБЮЕЛ убняр пегскэрхпсчыецн тюикю}
{------------------------------------------------------}
if count_tail<>0
Then
BlockRead(source_file,main_massiv8,count_tail);
Case count_tail of
2: Begin
main_massiv8[3]:=0;
Convert_Byte_Massiv(main_massiv8,massiv6);
BlockWrite(destination_file,massiv6,3);
size_dest_file:=size_dest_file+3;
End;
1: Begin
main_massiv8[2]:=0;
Convert_Byte_Massiv(main_massiv8,massiv6);
BlockWrite(destination_file,massiv6,2);
size_dest_file:=size_dest_file+2;
End;
End;
{ДНОХЯШБЮЕЛ Б убняр пегскэрхпсчыецн тюикю РПЕАСЕЛНЕ ЙНКХВЕЯРБН "="}
{------------------------------------------------------}
if (size_dest_file mod 4)<>0 Then
Begin
count_tail:=4-(size_dest_file mod 4);
cell:=ord('=');
for index_for:=1 to count_tail do
BlockWrite(destination_file,cell,1);
End;
CloseFile(destination_file);
CloseFile(source_file);
Writeln ('Complete!');
END.
Decoder Base64
{$APPTYPE CONSOLE}
program site;
uses
Windows,
SysUtils;
const
{ вХЯКН ДНКФМН ДЕКХРЭЯЪ МЮ 4 }
cell_size = 200;
{ йНМЯРЮМРШ ДКЪ УПЮМЕМХЪ ТЮИКНБ }
{-------------------------------}
main_file_name: string ='input.b64';
target_file_name: string='output.dat';
type
mass64 = Array [0..63] of char;
tBuff = Array [1..cell_size] of byte;
m6x4 = Array [1..4] of byte;
m8x3 = Array [1..3] of byte;
InvertMass = Array [0..255] of byte;
const mass_base64: mass64=('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','+','/');
{ дкъ ашярпнцн онхяйю яннрберярбхъ яхлбнкс б йнде BASE64 вхякнбнцн гмювемхъ. }
{ рн фе, врн х опедшдсыхи люяяхб, рнкэйн гдеяэ он йндс яхлбнкю б ascii лнфмн }
{ мюирх йнд щрнцн яхлбнкю б рюакхже BASE64. б mass_base64 - мюнанпнр, он мн- }
{ лепс хыел яюл яхлбнк. йнд 77 хяонкэгсеряъ дкъ яхлбнкнб, ме свюярбсчыху б }
{ йнде BASE64 }
{ ========================================================================== }
mass_ibase64: InvertMass =
(77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 62, 77, 77, 77, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77,
77, 77, 77, 77, 77, 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, 77, 77, 77, 77, 77, 77, 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,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77);
{ оепейнмбепрхпсер люяяхб хг 4-у "ьеярхахрмшу" аюирнб б люяяхб рпеу хкх лемее }
{ нашвмшу (б гюбхяхлнярх нр рнцн, еярэ кх б йндхпнбюммшу аюирюу яхлбнкш "=", }
{ ябхдерекэярбсчыхе н йнмже йндхпнбюммнцн тюикю) }
{ =========================================================================== }
Function DecodeMass (mass6x4: m6x4; var mass8x3: m8x3): byte;
var
i: byte;
Begin
If mass6x4[4]<>Ord('=')
Then
Begin
For i:=1 to 4 do mass6x4[i]:=mass_ibase64[mass6x4[i]];
mass8x3[1]:= (mass6x4[1] mod 64) * 4 + (mass6x4[2] div 16);
mass8x3[2]:= (mass6x4[2] mod 16) * 16 + (mass6x4[3] div 4);
mass8x3[3]:= (mass6x4[3] mod 4) * 64 + mass6x4[4];
DecodeMass:=3;
End
Else
Begin
If mass6x4[3]<>Ord('=')
Then
Begin
For i:=1 to 3 do mass6x4[i]:=mass_ibase64[mass6x4[i]];
mass8x3[1]:=(mass6x4[1] mod 64) * 4 + (mass6x4[2] div 16);
mass8x3[2]:=(mass6x4[2] mod 16) * 16 + (mass6x4[3] div 4);
DecodeMass:=2;
End
Else
Begin
For i:=1 to 2 do mass6x4[i]:=mass_ibase64[mass6x4[i]];
mass8x3[1]:=(mass6x4[1] mod 64) * 4 + (mass6x4[2] div 16);
DecodeMass:=1;
End;
End;
End;
{ оепейнмбепрхпсер анкэьни люяяхб "ьеярхахрмшу" }
{ аюирнб б люяяхб нашвмшу :) }
{ ============================================= }
Procedure AWriteBuffToFile(buff6: tBuff; count: Integer;
var buff8: tBuff; var count_cell_buff8: integer);
var
a: m6x4;
b: m8x3;
i, j, k: integer;
end_of_mass: boolean;
Begin
end_of_mass:=false; count_cell_buff8:=0; i:=1;
While (i<=(count div 4)) and not (end_of_mass) do
Begin
for j:=1 to 4 do a[j]:=buff6[(i-1)*4+j];
k:=DecodeMass(a, b);
For j:=1 to k do
Begin
buff8[count_cell_buff8+j]:=b[j];
End;
count_cell_buff8:=count_cell_buff8 + k;
If (k<>3) Then end_of_mass:=true;
i:=i+1;
End;
End;
{ опнжедспю дейндхпнбюмхъ хяундмнцн тюикю хг тнплюрю BASE64 }
{ йнкхвеярбн аюирнб бундмнцн тюикю днкфмн !!! ашрэ йпюрмн 4 }
{ ========================================================= }
Procedure DecodeBase64(fbase64_name, decode_fbase64_name: string);
// 8 8 8
// _______________________________________________________________________
// | | | |
//
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// |--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
// | | | | | | | | | | | | | | | | | | | | | | | | |
// |--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
//
// |_________________|_________________|_________________|_________________|
// 6 6 6 6
var
fbase64, file_target: file;
it_count, counter: LongInt;
long: integer;
tail_size: LongInt;
buff, buff8: tBuff;
type
mass3x8=Array[1..3] of byte;
Begin
AssignFile(fbase64,fbase64_name);
Reset(fbase64,1);
AssignFile(file_target, decode_fbase64_name);
If FileExists(decode_fbase64_name)
Then
Begin
DeleteFile(decode_fbase64_name);
End;
ReWrite(file_target,1);
it_count:=filesize(fbase64) div cell_size;
tail_size:=filesize(fbase64) mod cell_size;
counter:=1;
While counter<=it_count do
Begin
BlockRead(fbase64, buff, cell_size);
AWriteBuffToFile(buff,cell_size,buff8,long);
BlockWrite(file_target, buff8, long);
counter:=counter+1;
End;
If tail_size<>0
Then
Begin
BlockRead(fbase64, buff, tail_size);
AWriteBuffToFile(buff,tail_size,buff8,long);
BlockWrite(file_target, buff8, long);
End;
Close(fbase64);
Close(file_target);
End;
BEGIN
Writeln('-----------------------------------------');
If ParamStr(1)<>'' Then main_file_name:=ParamStr(1);
If Not(FileExists(main_file_name))
Then
Begin
Writeln('error: first parameter of this program '#13#10+
'is . this file not found');
Halt;
End;
If ParamStr(2)<>'' Then target_file_name:=ParamStr(2);
If FileExists(target_file_name)
Then
DeleteFile(target_file_name);
{ дейндхпсел хяундмши тюик хг BASE64 }
DecodeBase64(main_file_name, target_file_name);
Writeln ('Full Complete!');
END.