Revise CRC calls and use php's internal functions

This commit is contained in:
Deon George
2021-07-21 20:52:17 +10:00
parent 7cd3b814bb
commit 2fdc6eabad
5 changed files with 41 additions and 118 deletions

View File

@@ -4,38 +4,11 @@ namespace App\Traits;
trait CRC
{
private function CRC16USD(string $string): int
{
$crc = self::CRC16USD_INIT;
for ($c=0;$c<strlen($string);$c++)
$crc = $this->CRC16USD_UPDATE(ord($string[$c]),$crc);
return $crc;
}
private function CRC16USD_UPDATE($b,$crc): int
{
return (self::crc16usd_tab[(($crc >> 8) ^ $b) & 0xff] ^ (($crc & 0x00ff) << 8)) & 0xffff;
}
/**
* Calculate CRC32
*
* @param string $string
* @param bool $finish
* @return int
*/
private function CRC32(string $string,bool $finish=TRUE): int
{
$crc = 0xffffffff;
for ($i=0;$i<strlen($string);$i++)
$crc = (self::crc32_tab[($crc^ord($string[$i])) & 0xff] ^ (($crc>>8) & 0x00ffffff)) & 0xffffffff;
return $finish ? $this->CRC32_FINISH($crc) : $crc;
}
private function CRC32_FINISH($crc)
{
return ~$crc & 0xffffffff;