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
Table.php
114 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 | final class Table |
| 21 | { |
| 22 | private static $tables = array(); |
| 23 | |
| 24 | /** |
| 25 | * Echos the given table. Useful for building a static table to include in source code. |
| 26 | * |
| 27 | * @param array $table The table |
| 28 | */ |
| 29 | public static function output(array $table) |
| 30 | { |
| 31 | foreach ($table as $i => $value) { |
| 32 | echo "0x" . int2hex($value) . ","; |
| 33 | if ($i % 4 == 3) { |
| 34 | echo "\n"; |
| 35 | } else { |
| 36 | echo " "; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | echo "\n\n"; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Gets a CRC table, by creating it, or using a previously cached result. |
| 45 | * |
| 46 | * @param integer $polynomial The polynomial |
| 47 | * |
| 48 | * @return array The table |
| 49 | */ |
| 50 | public static function get($polynomial) |
| 51 | { |
| 52 | if (isset(self::$tables[$polynomial])) { |
| 53 | return self::$tables[$polynomial]; |
| 54 | } |
| 55 | self::$tables[$polynomial] = self::create($polynomial); |
| 56 | return self::$tables[$polynomial]; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Create a CRC table. |
| 61 | * |
| 62 | * @param integer $polynomial The polynomial. |
| 63 | * |
| 64 | * @return array The table. |
| 65 | */ |
| 66 | public static function create($polynomial) |
| 67 | { |
| 68 | $table = array_fill(0, 256, 0); |
| 69 | |
| 70 | for ($i = 0; $i < 256; $i++) { |
| 71 | $crc = $i; |
| 72 | for ($j = 0; $j < 8; $j++) { |
| 73 | if ($crc & 1 == 1) { |
| 74 | $crc = ($crc >> 1) ^ $polynomial; |
| 75 | } else { |
| 76 | $crc >>= 1; |
| 77 | } |
| 78 | } |
| 79 | $table[$i] = $crc; |
| 80 | } |
| 81 | |
| 82 | return $table; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Create a CRC table sliced by 4. |
| 87 | * |
| 88 | * @param integer $polynomial The polynomial. |
| 89 | * |
| 90 | * @return array The table. |
| 91 | */ |
| 92 | public static function create4($polynomial) |
| 93 | { |
| 94 | $table = array_fill(0, 4, array_fill(0, 256, 0)); |
| 95 | $table[0] = self::create($polynomial); |
| 96 | |
| 97 | for ($i = 0; $i < 256; $i++) { |
| 98 | // for Slicing-by-4 and Slicing-by-8 |
| 99 | $table[1][$i] = ($table[0][$i] >> 8) ^ $table[0][$table[0][$i] & 0xFF]; |
| 100 | $table[2][$i] = ($table[1][$i] >> 8) ^ $table[0][$table[1][$i] & 0xFF]; |
| 101 | $table[3][$i] = ($table[2][$i] >> 8) ^ $table[0][$table[2][$i] & 0xFF]; |
| 102 | |
| 103 | /* |
| 104 | // only Slicing-by-8 |
| 105 | $table[4][$i] = ($table[3][$i] >> 8) ^ $table[0][$table[3][$i] & 0xFF]; |
| 106 | $table[5][$i] = ($table[4][$i] >> 8) ^ $table[0][$table[4][$i] & 0xFF]; |
| 107 | $table[6][$i] = ($table[5][$i] >> 8) ^ $table[0][$table[5][$i] & 0xFF]; |
| 108 | $table[7][$i] = ($table[6][$i] >> 8) ^ $table[0][$table[6][$i] & 0xFF]; |
| 109 | */ |
| 110 | } |
| 111 | return $table; |
| 112 | } |
| 113 | } |
| 114 |