| Conditions | 7 |
| Paths | 22 |
| Total Lines | 36 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 52 | public function calculate(string $buffer): int |
||
| 53 | { |
||
| 54 | $bufferLength = \strlen($buffer); |
||
| 55 | |||
| 56 | $mask = (((1 << ($this->bitLength - 1)) - 1) << 1) | 1; |
||
| 57 | $highBit = 1 << ($this->bitLength - 1); |
||
| 58 | |||
| 59 | $crc = $this->init; |
||
| 60 | |||
| 61 | for ($iterator = 0; $iterator < $bufferLength; ++$iterator) { |
||
| 62 | $character = \ord($buffer[$iterator]); |
||
| 63 | if ($this->reverseIn) { |
||
| 64 | $character = $this->binaryReverse($character, 8); |
||
| 65 | } |
||
| 66 | |||
| 67 | for ($j = 0x80; $j; $j >>= 1) { |
||
| 68 | $bit = $crc & $highBit; |
||
| 69 | $crc <<= 1; |
||
| 70 | |||
| 71 | if ($character & $j) { |
||
| 72 | $bit ^= $highBit; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($bit) { |
||
| 76 | $crc ^= $this->poly; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($this->reverseOut) { |
||
| 82 | $crc = $this->binaryReverse($crc, $this->bitLength); |
||
| 83 | } |
||
| 84 | |||
| 85 | $crc ^= $this->xorOut; |
||
| 86 | |||
| 87 | return $crc & $mask; |
||
| 88 | } |
||
| 152 |