| @@ 10-67 (lines=58) @@ | ||
| 7 | /** |
|
| 8 | * @author Philip Burggraf <[email protected]> |
|
| 9 | */ |
|
| 10 | abstract class AbstractCRC16 extends AbstractCRC |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @param string $buffer |
|
| 14 | * |
|
| 15 | * @return int |
|
| 16 | */ |
|
| 17 | public function calculate(string $buffer): int |
|
| 18 | { |
|
| 19 | $crc = $this->init; |
|
| 20 | ||
| 21 | $bufferLength = strlen($buffer); |
|
| 22 | ||
| 23 | for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) { |
|
| 24 | $character = ord($buffer[$bufferPosition]); |
|
| 25 | ||
| 26 | if ($this->reverseIn) { |
|
| 27 | $character = $this->binaryReverse($character, 8); |
|
| 28 | } |
|
| 29 | ||
| 30 | $crc = ($this->lookupTable[(($crc >> 8) ^ $character) & 0xff] ^ ($crc << 8)) & 0xffff; |
|
| 31 | } |
|
| 32 | ||
| 33 | if ($this->reverseOut) { |
|
| 34 | $crc = $this->binaryReverse($crc, 16); |
|
| 35 | } |
|
| 36 | ||
| 37 | return $crc ^ $this->xorOut; |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @param int $polynomial |
|
| 42 | * |
|
| 43 | * @return array |
|
| 44 | */ |
|
| 45 | public function generateTable(int $polynomial): array |
|
| 46 | { |
|
| 47 | $tableSize = 256; |
|
| 48 | ||
| 49 | $table = []; |
|
| 50 | ||
| 51 | for ($iterator = 0; $iterator < $tableSize; ++$iterator) { |
|
| 52 | $temp = 0; |
|
| 53 | $a = ($iterator << 8); |
|
| 54 | for ($j = 0; $j < 8; ++$j) { |
|
| 55 | if ((($temp ^ $a) & 0x8000) !== 0) { |
|
| 56 | $temp = (($temp << 1) ^ $polynomial); |
|
| 57 | } else { |
|
| 58 | $temp <<= 1; |
|
| 59 | } |
|
| 60 | $a <<= 1; |
|
| 61 | } |
|
| 62 | $table[$iterator] = $temp & 0xffff; |
|
| 63 | } |
|
| 64 | ||
| 65 | return $table; |
|
| 66 | } |
|
| 67 | } |
|
| 68 | ||
| @@ 10-68 (lines=59) @@ | ||
| 7 | /** |
|
| 8 | * @author Philip Burggraf <[email protected]> |
|
| 9 | */ |
|
| 10 | abstract class AbstractCRC32 extends AbstractCRC |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @param string $buffer |
|
| 14 | * |
|
| 15 | * @return int |
|
| 16 | */ |
|
| 17 | public function calculate(string $buffer): int |
|
| 18 | { |
|
| 19 | $crc = $this->init; |
|
| 20 | ||
| 21 | $bufferLength = strlen($buffer); |
|
| 22 | ||
| 23 | for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) { |
|
| 24 | $character = ord($buffer[$bufferPosition]); |
|
| 25 | ||
| 26 | if ($this->reverseIn) { |
|
| 27 | $character = $this->binaryReverse($character, 8); |
|
| 28 | } |
|
| 29 | ||
| 30 | $crc = $this->lookupTable[(($crc >> 24) ^ $character) & 0xff] ^ ($crc << 8); |
|
| 31 | $crc &= 0xffffffff; |
|
| 32 | } |
|
| 33 | ||
| 34 | if ($this->reverseOut) { |
|
| 35 | $crc = $this->binaryReverse($crc, 32); |
|
| 36 | } |
|
| 37 | ||
| 38 | return $crc ^ $this->xorOut; |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * @param int $polynomial |
|
| 43 | * |
|
| 44 | * @return array |
|
| 45 | */ |
|
| 46 | public function generateTable(int $polynomial): array |
|
| 47 | { |
|
| 48 | $tableSize = 256; |
|
| 49 | ||
| 50 | $table = []; |
|
| 51 | ||
| 52 | for ($iterator = 0; $iterator < $tableSize; ++$iterator) { |
|
| 53 | $temp = 0; |
|
| 54 | $a = ($iterator << 24); |
|
| 55 | for ($j = 0; $j < 8; ++$j) { |
|
| 56 | if ((($temp ^ $a) & 0x80000000) !== 0) { |
|
| 57 | $temp = (($temp << 1) ^ $polynomial); |
|
| 58 | } else { |
|
| 59 | $temp <<= 1; |
|
| 60 | } |
|
| 61 | $a <<= 1; |
|
| 62 | } |
|
| 63 | $table[$iterator] = $temp & 0xffffffff; |
|
| 64 | } |
|
| 65 | ||
| 66 | return $table; |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||