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
CRC32.php
113 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\Builtin; |
| 21 | use Google\CRC32\Google; |
| 22 | use Google\CRC32\PHP; |
| 23 | |
| 24 | /** |
| 25 | * Various CRC32 implementations. |
| 26 | * |
| 27 | * ``` |
| 28 | * use Google\CRC32\CRC32; |
| 29 | * |
| 30 | * $crc = CRC32::create(CRC32::CASTAGNOLI); |
| 31 | * $crc->update('hello'); |
| 32 | * |
| 33 | * echo $crc->hash(); |
| 34 | * ``` |
| 35 | */ |
| 36 | class CRC32 |
| 37 | { |
| 38 | use CRCTrait; |
| 39 | |
| 40 | /** |
| 41 | * IEEE polynomial as used by ethernet (IEEE 802.3), v.42, fddi, gzip, |
| 42 | * zip, png, ... |
| 43 | */ |
| 44 | const IEEE = 0xedb88320; |
| 45 | |
| 46 | /** |
| 47 | * Castagnoli's polynomial, used in iSCSI, SCTP, Google Cloud Storage, |
| 48 | * Apache Kafka, and has hardware-accelerated in modern intel CPUs. |
| 49 | * https://doi.org/10.1109/26.231911 |
| 50 | */ |
| 51 | const CASTAGNOLI = 0x82f63b78; |
| 52 | |
| 53 | /** |
| 54 | * Koopman's polynomial. |
| 55 | * https://doi.org/10.1109/DSN.2002.1028931 |
| 56 | */ |
| 57 | const KOOPMAN = 0xeb31d82e; |
| 58 | |
| 59 | /** |
| 60 | * The size of the checksum in bytes. |
| 61 | */ |
| 62 | const SIZE = 4; |
| 63 | |
| 64 | private static $mapping = [ |
| 65 | self::IEEE => 'IEEE', |
| 66 | self::CASTAGNOLI => 'Castagnoli', |
| 67 | self::KOOPMAN => 'Koopman', |
| 68 | ]; |
| 69 | |
| 70 | private function __construct() |
| 71 | { |
| 72 | // Prevent instantiation. |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the best CRC implementation available on this machine. |
| 77 | * |
| 78 | * @param integer $polynomial The CRC polynomial. Use a 32-bit number, |
| 79 | * or one of the supplied constants, CRC32::IEEE, |
| 80 | * CRC32::CASTAGNOLI, or CRC32::KOOPMAN. |
| 81 | * |
| 82 | * @return CRC32Interface |
| 83 | */ |
| 84 | public static function create($polynomial) |
| 85 | { |
| 86 | if (Google::supports($polynomial) && function_exists('crc32c')) { |
| 87 | return new Google(); |
| 88 | } |
| 89 | |
| 90 | if (Builtin::supports($polynomial)) { |
| 91 | return new Builtin($polynomial); |
| 92 | } |
| 93 | |
| 94 | // Fallback to the pure PHP version |
| 95 | return new PHP($polynomial); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Prints the human friendly name for this polynomial. |
| 100 | * |
| 101 | * @param integer $polynomial The CRC polynomial. |
| 102 | * |
| 103 | * @return string |
| 104 | */ |
| 105 | public static function string($polynomial) |
| 106 | { |
| 107 | if (isset(self::$mapping[$polynomial])) { |
| 108 | return self::$mapping[$polynomial]; |
| 109 | } |
| 110 | return '0x' . self::int2hex($polynomial); |
| 111 | } |
| 112 | } |
| 113 |