clrghouz/app/Traits/CRC.php

63 lines
1.4 KiB
PHP

<?php
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;
}
private function CRC32_UPDATE($b,$crc)
{
return ((self::crc32_tab[($crc^$b) & 0xff] ^ (($crc>>8) & 0x00ffffff)) & 0xffffffff);
}
private function LSZ_INIT_CRC()
{
return ($this->ls_Protocol & self::LSZ_OPTCRC32) ? self::LSZ_INIT_CRC32 : self::LSZ_INIT_CRC16;
}
private function LSZ_FINISH_CRC($crc)
{
return ($this->ls_Protocol & self::LSZ_OPTCRC32) ? $this->CRC32_FINISH($crc) : $crc;
}
private function LSZ_UPDATE_CRC($b,$crc)
{
return ($this->ls_Protocol & self::LSZ_OPTCRC32) ? $this->CRC32_UPDATE($b,$crc) : $this->CRC16USD_UPDATE($b,$crc);
}
}