note 104306 deleted from function.crc32 by danbrown
| From: | danbrown@php.net | Date: | Wed, 08 Jun 2011 00:00:01 +0000 |
| Subject: | note 104306 deleted from function.crc32 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180321@lists.php.net to get a copy of this message | ||
Note Submitter: profexer at trash-mail dot com
----
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;
}
?>