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
PHP.php
79 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 algorithm. |
| 26 | * |
| 27 | * Uses a simple lookup table to improve the performances. |
| 28 | */ |
| 29 | final class PHP implements CRCInterface |
| 30 | { |
| 31 | use CRCTrait; |
| 32 | |
| 33 | public static function supports($algo) |
| 34 | { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | private $table = []; |
| 39 | |
| 40 | /** |
| 41 | * Creates a new instance for this polynomial. |
| 42 | * |
| 43 | * @param integer $polynomial The polynomial |
| 44 | */ |
| 45 | public function __construct($polynomial) |
| 46 | { |
| 47 | $this->polynomial = $polynomial; |
| 48 | $this->table = Table::get($polynomial); |
| 49 | $this->reset(); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | public function reset() |
| 54 | { |
| 55 | $this->crc = ~0; |
| 56 | } |
| 57 | |
| 58 | public function update($data) |
| 59 | { |
| 60 | $crc = $this->crc; |
| 61 | $table = $this->table; |
| 62 | $len = strlen($data); |
| 63 | for ($i = 0; $i < $len; ++$i) { |
| 64 | $crc = (($crc >> 8) & 0xffffff) ^ $table[($crc ^ ord($data[$i])) & 0xff]; |
| 65 | } |
| 66 | $this->crc = $crc; |
| 67 | } |
| 68 | |
| 69 | public function hash($raw_output = null) |
| 70 | { |
| 71 | return $this->crcHash(~$this->crc, $raw_output === true); |
| 72 | } |
| 73 | |
| 74 | public function version() |
| 75 | { |
| 76 | return 'crc32(' . $this->int2hex($this->polynomial) . ') software version'; |
| 77 | } |
| 78 | } |
| 79 |