Builtin.php
3 years ago
CRC32.php
3 years ago
CRCInterface.php
3 years ago
CRCTrait.php
3 years ago
Google.php
3 years ago
PHP.php
3 years ago
PHPSlicedBy4.php
3 years ago
Table.php
3 years ago
PHPSlicedBy4.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2019 Google LLC |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | namespace Google\CRC32; |
| 19 | |
| 20 | use Google\CRC32\CRCInterface; |
| 21 | use Google\CRC32\CRCTrait; |
| 22 | use Google\CRC32\Table; |
| 23 | |
| 24 | /** |
| 25 | * PHP implementation of the CRC32 sliced-by-4 algorithm. |
| 26 | * |
| 27 | * This is typically faster, but the PHP implementation seems slower than the |
| 28 | * simple implementation. |
| 29 | */ |
| 30 | final class PHPSlicedBy4 implements CRCInterface |
| 31 | { |
| 32 | use CRCTrait; |
| 33 | |
| 34 | public static function supports($algo) |
| 35 | { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | private $table; |
| 40 | |
| 41 | public function __construct($polynomial) |
| 42 | { |
| 43 | $this->polynomial = $polynomial; |
| 44 | $this->table = Table::create4($polynomial); |
| 45 | $this->reset(); |
| 46 | } |
| 47 | |
| 48 | public function reset() |
| 49 | { |
| 50 | $this->crc = ~0; |
| 51 | } |
| 52 | |
| 53 | public function update($data) |
| 54 | { |
| 55 | $crc = $this->crc; |
| 56 | $table0 = $this->table[0]; |
| 57 | $table1 = $this->table[1]; |
| 58 | $table2 = $this->table[2]; |
| 59 | $table3 = $this->table[3]; |
| 60 | |
| 61 | $len = strlen($data); |
| 62 | $remain = ($len % 4); |
| 63 | $len1 = $len - $remain; |
| 64 | for ($i = 0; $i < $len1; $i += 4) { |
| 65 | $b = (ord($data[$i+3])<<24) | |
| 66 | (ord($data[$i+2])<<16) | |
| 67 | (ord($data[$i+1])<<8) | |
| 68 | (ord($data[$i])); |
| 69 | |
| 70 | $crc = ($crc ^ $b) & 0xffffffff; |
| 71 | |
| 72 | $crc = $table3[ $crc & 0xff] ^ |
| 73 | $table2[($crc>>8) & 0xff] ^ |
| 74 | $table1[($crc>>16) & 0xff] ^ |
| 75 | $table0[($crc>>24) & 0xff]; |
| 76 | } |
| 77 | |
| 78 | switch ($remain) { |
| 79 | case 3: |
| 80 | $crc = (($crc >> 8) & 0xffffff) ^ $table0[($crc ^ ord($data[$i])) & 0xff]; |
| 81 | $crc = (($crc >> 8) & 0xffffff) ^ $table0[($crc ^ ord($data[$i+1])) & 0xff]; |
| 82 | $crc = (($crc >> 8) & 0xffffff) ^ $table0[($crc ^ ord($data[$i+2])) & 0xff]; |
| 83 | break; |
| 84 | case 2: |
| 85 | $crc = (($crc >> 8) & 0xffffff) ^ $table0[($crc ^ ord($data[$i])) & 0xff]; |
| 86 | $crc = (($crc >> 8) & 0xffffff) ^ $table0[($crc ^ ord($data[$i+1])) & 0xff]; |
| 87 | break; |
| 88 | case 1: |
| 89 | $crc = (($crc >> 8) & 0xffffff) ^ $table0[($crc ^ ord($data[$i])) & 0xff]; |
| 90 | break; |
| 91 | case 0: |
| 92 | } |
| 93 | |
| 94 | $this->crc = $crc; |
| 95 | } |
| 96 | |
| 97 | public function hash($raw_output = null) |
| 98 | { |
| 99 | return $this->crcHash(~$this->crc, $raw_output === true); |
| 100 | } |
| 101 | |
| 102 | public function version() |
| 103 | { |
| 104 | return 'crc32(' . $this->int2hex($this->polynomial) . ') software version'; |
| 105 | } |
| 106 | } |
| 107 |