note 104306 added to function.crc32

From: Date: Tue, 07 Jun 2011 23:12:57 +0000
Subject: note 104306 added to function.crc32
Groups: php.notes 
Request: Send a blank email to php-notes+get-180320@lists.php.net to get a copy of this message
You can optimize the function of gabri (http://www.php.net/manual/en/function.crc32.php#99939) and accelerate it by creating a character code table and remove the loop functions ord (). <?php function crc32_file ($filename) { $f = @fopen($filename,'rb'); if (!$f) return false; static $CRC32Table, $Reflect8Table; if (!isset($CRC32Table)) { $Polynomial = 0x04c11db7; $topBit = 1 << 31; for($i = 0; $i < 256; $i++) { $charTable[ chr($i) ] = $i; $remainder = $i << 24; for ($j = 0; $j < 8; $j++) { if ($remainder & $topBit) $remainder = ($remainder << 1) ^ $Polynomial; else $remainder = $remainder << 1; } $CRC32Table[$i] = $remainder; if (isset($Reflect8Table[$i])) continue; $str = str_pad(decbin($i), 8, '0', STR_PAD_LEFT); $num = bindec(strrev($str)); $Reflect8Table[$i] = $num; $Reflect8Table[$num] = $i; } } $remainder = 0xffffffff; while ($data = fread($f,524288)) { $len = strlen($data); for ($i = 0; $i < $len; $i++) { $byte = $Reflect8Table[ $charTable[ $data[$i] ] ]; $index = (($remainder >> 24) & 0xff) ^ $byte; $crc = $CRC32Table[$index]; $remainder = ($remainder << 8) ^ $crc; } } $str = decbin($remainder); $str = str_pad($str, 32, '0', STR_PAD_LEFT); $remainder = bindec(strrev($str)); return $remainder ^ 0xffffffff; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 88.135.249.96 ---- Manual Page -- http://www.php.net/manual/en/function.crc32.php Edit -- https://master.php.net/note/edit/104306 Del: integrated -- https://master.php.net/note/delete/104306/integrated Del: useless -- https://master.php.net/note/delete/104306/useless Del: bad code -- https://master.php.net/note/delete/104306/bad+code Del: spam -- https://master.php.net/note/delete/104306/spam Del: non-english -- https://master.php.net/note/delete/104306/non-english Del: in docs -- https://master.php.net/note/delete/104306/in+docs Del: other reasons-- https://master.php.net/note/delete/104306 Reject -- https://master.php.net/note/reject/104306 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180320) next »