Base64 code courtesy of 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;
}
Example TCP code:
new TCPObject(test);
$domain = "psp-networks.com";
$user = "some_username";
$pass = "some_password";
$headers = "GET /testing HTTP/1.1\r\n" @
"Host: " @ $domain @ "\r\n" @
"Authorization: Basic " @ base64Encode($user @ ":" @ $pass) @ "\r\n\r\n";
test.connect(%domain @ ":80");
function test::onConnected(%this) {
test.send($headers);
}
function test::onLine(%this, %line) {
echo(%line);
}
(not tested, should work fine though)