Please note that in the below encode function there is a bug!
<?php
if (($c==0x3d) || ($c>=0x80) || ($c<0x20))
?>
$c should be checked against less or equal to encode spaces!
so the correct code is
<?php
if (($c==0x3d) || ($c>=0x80) || ($c<=0x20))
?>
Fix the code or post this note, please