note 99939 added to function.crc32
| From: | gabridotnsatgmaildotcom at osu1 dot php dot net | Date: | Thu, 16 Sep 2010 05:05:28 +0000 |
| Subject: | note 99939 added to function.crc32 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172623@lists.php.net to get a copy of this message | ||
if you are looking for a fast function to hash a file, take a look at
http://www.php.net/manual/en/function.hash-file.php
this is crc32 file checker based on a CRC32 guide
it have performance at ~ 625 KB/s on my 2.2GHz Turion
far slower than hash_file('crc32b','filename.ext')
<?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++)
{
$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,1024))
{
$len = strlen($data);
for ($i = 0; $i < $len; $i++)
{
$byte = $Reflect8Table[ord($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;
}
?>
<?php
$a = microtime();
echo dechex(crc32_file('filename.ext'))."\n";
$b = microtime();
echo array_sum(explode(' ',$b)) - array_sum(explode(' ',$a))."\n";
?>
Output:
ec7369fe
2.384134054184 (or similiar)
----
Server IP: 202.158.52.162
Probable Submitter: 202.70.58.132
----
Manual Page -- http://www.php.net/manual/en/function.crc32.php
Edit -- https://master.php.net/note/edit/99939
Del: integrated -- https://master.php.net/note/delete/99939/integrated
Del: useless -- https://master.php.net/note/delete/99939/useless
Del: bad code -- https://master.php.net/note/delete/99939/bad+code
Del: spam -- https://master.php.net/note/delete/99939/spam
Del: non-english -- https://master.php.net/note/delete/99939/non-english
Del: in docs -- https://master.php.net/note/delete/99939/in+docs
Del: other reasons-- https://master.php.net/note/delete/99939
Reject -- https://master.php.net/note/reject/99939
Search -- https://master.php.net/manage/user-notes.php