2019-05-11 01:17:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate CCITT-CRC16 checksum
|
|
|
|
*/
|
|
|
|
if (! function_exists('crc16')) {
|
|
|
|
function crc16($data)
|
|
|
|
{
|
|
|
|
$crc = 0x0000;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return $crc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump out data into a hex dump
|
|
|
|
*/
|
|
|
|
if (! function_exists('hex_dump')) {
|
|
|
|
function hex_dump($data,$newline="\n",$width=16)
|
|
|
|
{
|
|
|
|
$result = '';
|
|
|
|
|
|
|
|
$pad = '.'; # padding for non-visible characters
|
|
|
|
$to = $from = '';
|
|
|
|
|
|
|
|
for ($i=0; $i<=0xFF; $i++)
|
|
|
|
{
|
|
|
|
$from .= chr($i);
|
|
|
|
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hex = str_split(bin2hex($data),$width*2);
|
|
|
|
$chars = str_split(strtr($data,$from,$to),$width);
|
|
|
|
|
|
|
|
$offset = 0;
|
|
|
|
foreach ($hex as $i => $line)
|
|
|
|
{
|
|
|
|
$result .= sprintf('%08X: %-48s [%s]%s',
|
|
|
|
$offset,
|
|
|
|
substr_replace(implode(' ',str_split($line,2)),' ',8*3,0),
|
|
|
|
$chars[$i],
|
|
|
|
$newline);
|
|
|
|
|
|
|
|
$offset += $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2019-05-20 07:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-21 10:52:17 +00:00
|
|
|
* Send a value has hex chars
|
2019-05-20 07:18:18 +00:00
|
|
|
*/
|
2021-07-21 10:52:17 +00:00
|
|
|
if (! function_exists('hexstr')) {
|
|
|
|
function hexstr(int $int)
|
2019-05-20 07:18:18 +00:00
|
|
|
{
|
2021-07-21 10:52:17 +00:00
|
|
|
if ($int > 0xffff)
|
|
|
|
throw new Exception('Int too large for hexstr');
|
2019-05-20 07:18:18 +00:00
|
|
|
|
2021-07-21 10:52:17 +00:00
|
|
|
$hexdigitslower = '0123456789abcdef';
|
|
|
|
$x = '';
|
2019-05-20 07:18:18 +00:00
|
|
|
|
2021-07-21 10:52:17 +00:00
|
|
|
if ($int > 0xff) {
|
|
|
|
$x .= substr($hexdigitslower,($int&0xf000)>>12,1);
|
|
|
|
$x .= substr($hexdigitslower,($int&0x0f00)>>8,1);
|
|
|
|
}
|
2019-05-20 07:18:18 +00:00
|
|
|
|
2021-07-21 10:52:17 +00:00
|
|
|
$x .= substr($hexdigitslower,($int&0x00f0)>>4,1);
|
|
|
|
$x .= substr($hexdigitslower,($int&0x000f),1);
|
2019-05-20 07:18:18 +00:00
|
|
|
|
2021-07-21 10:52:17 +00:00
|
|
|
return $x;
|
2019-05-20 07:18:18 +00:00
|
|
|
}
|
2019-05-11 01:17:56 +00:00
|
|
|
}
|