I have these lovely functions by truce, but I have no idea how to use them on a non-ascii file, such as an image.
The best idea I can come up with is doing this:
%file = new fileObject();
%file.openForRead(%filepath);
while(!%file.isEOF())
$allfile = $allfile @ %file.readline();
%file.close();
%file.delete();
However, due to the fact that openForRead opens it in an ascii format (?), it won't read properly, and will give me the same gibberish as if I had opened it in a text editor.
What alternatives do I have?
////////////////////////////////////////
// Base64 Pack by Truce //
////////////////////////////////////////
function convertBase(%val,%atype,%btype)
{
%vlen = strLen(%val);
%alen = strLen(%atype);
%blen = strLen(%btype);
for(%i = 0; %i < %vlen; %i++)
%sum += striPos(%atype,getSubStr(%val,%i,1)) * mPow(%alen,%vlen - %i - 1);
while(1)
{
%rem = %sum % %blen;
%new = getSubStr(%btype,%rem,1) @ %new;
%sum = mFloor(%sum / %blen);
if(!%sum)
break;
}
return %new;
}
function base64Encode(%str)
{
%base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
%asciimap = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" @
"OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
%len = strLen(%str);
for(%i = 0; %i < %len; %i++)
{
%chr = getSubStr(%str,%i,1);
%ascii = strPos(%asciimap,%chr) + 32;
%bin = convertBase(%ascii,"0123456789","01");
while(strLen(%bin) < 8)
%bin = "0" @ %bin;
%all = %all @ %bin;
}
%len = strLen(%all);
for(%i = 0; %i < %len; %i += 6)
{
%pack = getSubStr(%all,%i,6);
while(strLen(%pack) < 6)
%pack = %pack @ "0";
%dec = convertBase(%pack,"01","0123456789");
%new = %new @ getSubStr(%base64map,%dec,1);
}
while(strLen(%new) % 4 > 0)
%new = %new @ "=";
return %new;
}
function base64Decode(%str)
{
%base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
%asciimap = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" @
"OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
while(getSubStr(%str,strLen(%str) - 1,1) $= "=")
%str = getSubStr(%str,0,strLen(%str) - 1);
%len = strLen(%str);
for(%i = 0; %i < %len; %i++)
{
%chr = getSubStr(%str,%i,1);
%pos = strPos(%base64map,%chr);
%bin = convertBase(%pos,"0123456789","01");
while(strLen(%bin) < 6)
%bin = "0" @ %bin;
%all = %all @ %bin;
}
while(strLen(%all) % 8 > 0)
%all = getSubStr(%all,0,strLen(%all) - 1);
%len = strLen(%all);
for(%i = 0; %i < %len; %i += 8)
{
%bin = getSubStr(%all,%i,8);
%dec = convertBase(%bin,"01","0123456789") - 32;
%chr = getSubStr(%asciiMap,%dec,1);
%new = %new @ %chr;
}
return %new;
}