|
ENCODE64 encoding can replace Encodeuri,encodeuricomponent,endode function at the same time
Because none of the selected characters will be encoded.
Class encode64{
function code ($STR) {
$KEY = ' Paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh ';
$a = strtobytes ($STR);
$len = count ($a);
$res = $len% 3;
$s = ""; $i = 2; $v = 0;
for (; $i < $len; $i + = 3) {
$v = $a [$i-2] + ($a [$i-1] << 8) + ($a [$i] << 16);
$s. = $KEY [$v & 0x3f];
$s. = $KEY [($v >> 6) & 0x3f];
$s. = $KEY [($v >>) & 0x3f];
$s. = $KEY [($v >> 18)];
}
if ($res = = 1) {
$v = $a [$i-2];
$s. = $KEY [$v & 0x3f];
$s. = $KEY [($v >> 6) & 0x3f];
} else if ($res = = 2) {
$v = $a [$i-2] + ($a [$i-1] << 8);
$s. = $KEY [$v & 0x3f];
$s. = $KEY [($v >> 6) & 0x3f];
$s. = $KEY [($v >>) & 0x3f];
}
return $s;
}
function decode ($CODESTR) {
$KEY = ' Paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh ';
$dic = Array ();
for ($i = 0; $i < $i + +) {
$dic [$KEY [$i]] = $i;
}
$len = strlen ($CODESTR);
$res = $len% 4;
$cLen = Floor ($len/4)
if ($res ==2) $clen + = 1;
ElseIf ($res ==3) $clen + = 2;
$code = Range (0, $clen);
$i = 3; $v = 0; $j = 0;
for (; $i < $len; $i + = 4) {
$v = $dic [$CODESTR [$i-3]];
$v + = $dic [$CODESTR [$i-2]] << 6;
$v + = $dic [$CODESTR [$i-1]] << 12;
$v + = $dic [$CODESTR [$i]] << 18;
$code [$j] = $v & 0xFF;
$code [$j +1] = ($v >> 8) & 0xFF;
$code [$j +2] = ($v >>) & 0xFF;
$j + = 3;
}
if ($res = = 2) {
The correct number of bytes is definitely more than 2 or 3, No 1 case, if present, discard.
$v = $dic [$CODESTR [$i-3]];
$v + = $dic [$CODESTR [$i-2]] << 6;
$code [$j] = $v & 0xFF;
} else if ($res = = 3) {
$v = $dic [$CODESTR [$i-3]];
$v + = $dic [$CODESTR [$i-2]] << 6;
$v + = $dic [$CODESTR [$i-1]] << 12;
$code [$j] = $v & 0xFF;
$code [$j +1] = ($v >> 8) & 0xFF;
}
Return Bytestostr ($code);
}
}
function Bytestostr ($bytes) {
$str = ";
foreach ($bytes as $ch) {
$str. = Chr ($ch);
}
Return Iconv (' utf-16be ', ' utf-8 ', $str);
}
function Strtobytes ($STR) {
$str = Iconv (' utf-8 ', ' utf-16be ', $str);
$len = strlen ($STR);
$bytes = Array ();
for ($i =0; $i < $len; $i + +) {
$bytes [] = Ord ($str [$i]);
}
return $bytes;
}
?>
|