Minor cosmetic changes, show hex_dump when data purged from protocol stream

This commit is contained in:
Deon George
2022-02-13 10:24:31 +11:00
parent f142284a2b
commit f216f42917
3 changed files with 109 additions and 111 deletions

View File

@@ -4,11 +4,10 @@
* Calculate CCITT-CRC16 checksum
*/
if (! function_exists('crc16')) {
function crc16($data)
function crc16($data): int
{
$crc = 0x0000;
for ($i = 0; $i < strlen($data); $i++)
{
for ($i = 0; $i < strlen($data); $i++) {
$x = (($crc >> 8) ^ ord($data[$i])) & 0xFF;
$x ^= $x >> 4;
$crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
@@ -21,15 +20,14 @@ if (! function_exists('crc16')) {
* Dump out data into a hex dump
*/
if (! function_exists('hex_dump')) {
function hex_dump($data,$newline="\n",$width=16)
function hex_dump($data,$newline="\n",$width=16): string
{
$result = '';
$pad = '.'; # padding for non-visible characters
$to = $from = '';
for ($i=0; $i<=0xFF; $i++)
{
for ($i=0; $i<=0xFF; $i++) {
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
@@ -38,8 +36,7 @@ if (! function_exists('hex_dump')) {
$chars = str_split(strtr($data,$from,$to),$width);
$offset = 0;
foreach ($hex as $i => $line)
{
foreach ($hex as $i => $line) {
$result .= sprintf('%08X: %-48s [%s]%s',
$offset,
substr_replace(implode(' ',str_split($line,2)),' ',8*3,0),
@@ -57,7 +54,7 @@ if (! function_exists('hex_dump')) {
* Send a value has hex chars
*/
if (! function_exists('hexstr')) {
function hexstr(int $int)
function hexstr(int $int): string
{
if ($int > 0xffff)
throw new Exception('Int too large for hexstr');
@@ -176,6 +173,7 @@ if (! function_exists('optimize_path')) {
if ($host !== $cur) {
$cur = $host;
$result->push($address);
} else {
$result->push($node);
}