changed the hex-ascii conversion style

This commit is contained in:
hyung-hwan 2015-03-16 01:54:29 +00:00
parent 2bdd1e5e45
commit 2337a0f09b
2 changed files with 91 additions and 7 deletions

View File

@ -151,7 +151,7 @@ $config['subclass_prefix'] = 'MY_';
| DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!! | DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!!
| |
*/ */
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_@\-'; $config['permitted_uri_chars'] = 'a-z 0-9~%.:_@\-!*';
/* /*

View File

@ -9,10 +9,53 @@ class Converter
// convert an ascii string to its hex representation // convert an ascii string to its hex representation
function AsciiToHex($ascii) function AsciiToHex($ascii)
{ {
$hex = ''; $len = strlen($ascii);
/*
$hex = '';
for($i = 0; $i < $len; $i++)
$hex .= str_pad(base_convert(ord($ascii[$i]), 10, 16), 2, '0', STR_PAD_LEFT);
*/
$hex = '!'; # the new style conversion begins with an exclamation mark.
for ($i = 0; $i < $len; $i++)
{
if ($ascii[$i] == '\\')
{
// backslash to double backslashes.
$seg = '\\\\';
}
else if ($ascii[$i] == ':')
{
// colon to backslash-colon
$seg = '\\:';
}
else if ($ascii[$i] == '/')
{
// slash to colon
$seg = ':';
}
else if ($ascii[$i] == '.')
{
// period - no conversion
$seg = '.';
}
else
{
if (preg_match ('/^[A-Za-z0-9]$/', $ascii[$i]) === FALSE)
{
$seg = '\\' . str_pad(base_convert(ord($ascii[$i]), 10, 16), 2, '0', STR_PAD_LEFT);
}
else
{
$seg = $ascii[$i];
}
}
$hex .= $seg;
}
for($i = 0; $i < strlen($ascii); $i++)
$hex .= str_pad(base_convert(ord($ascii[$i]), 10, 16), 2, '0', STR_PAD_LEFT);
return $hex; return $hex;
} }
@ -23,10 +66,51 @@ class Converter
{ {
$ascii = ''; $ascii = '';
if (strlen($hex) % 2 == 1) $hex = '0'.$hex; $len = strlen($hex);
if ($len > 0 && $hex[0] == '!')
{
for ($i = 1; $i < $len; $i++)
{
if ($hex[$i] == '\\')
{
$j = $i + 1;
$k = $i + 2;
for($i = 0; $i < strlen($hex); $i += 2) if ($k < $len && ctype_xdigit($hex[$j]) && ctype_xdigit($hex[$k]))
$ascii .= chr(base_convert(substr($hex, $i, 2), 16, 10)); {
$seg = chr(base_convert(substr($hex, $j, 2), 16, 10));
$i = $k;
}
else if ($j < $len)
{
$seg = $hex[$j];
$i = $j;
}
else
{
// the last charater is a backslash
$seg = $hex[$i];
}
}
else if ($hex[$i] == ':')
{
$seg = '/';
}
else
{
$seg = $hex[$i];
}
$ascii .= $seg;
}
}
else
{
if (strlen($hex) % 2 == 1) $hex = '0' . $hex;
for($i = 0; $i < strlen($hex); $i += 2)
$ascii .= chr(base_convert(substr($hex, $i, 2), 16, 10));
}
return $ascii; return $ascii;
} }