note 76080 added to function.crc32
| From: | petteri at qred dot fi | Date: | Fri, 29 Jun 2007 06:03:30 +0000 |
| Subject: | note 76080 added to function.crc32 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128071@lists.php.net to get a copy of this message | ||
If you have php version<5.1.2 you might use this.
It's a little bit faster than __crc32_file() :)
Ps. crc32_combine() is ported directly from zlib.
<?php
function crc32_file($filename)
{
$fp=fopen($filename, "rb");
$old_crc=false;
if ($fp != false) {
$buffer = '';
while (!feof($fp)) {
$buffer=fread($fp, 10485760);
$len=strlen($buffer);
$t=crc32($buffer);
if ($old_crc) {
$crc32=crc32_combine($old_crc, $t, $len);
$old_crc=$crc32;
} else {
$crc32=$old_crc=$t;
}
}
fclose($fp);
} else {
print "Cannot open file\n";
}
return $crc32;
}
function crc32_combine($crc1, $crc2, $len2)
{
$odd[0]=0xedb88320;
$row=1;
for($n=1;$n<32;$n++) {
$odd[$n]=$row;
$row<<=1;
}
gf2_matrix_square($even,$odd);
gf2_matrix_square($odd,$even);
do {
/* apply zeros operator for this bit of len2 */
gf2_matrix_square($even, $odd);
if ($len2 & 1)
$crc1=gf2_matrix_times($even, $crc1);
$len2>>=1;
/* if no more bits set, then done */
if ($len2==0)
break;
/* another iteration of the loop with odd and even swapped */
gf2_matrix_square($odd, $even);
if ($len2 & 1)
$crc1=gf2_matrix_times($odd, $crc1);
$len2>>= 1;
} while ($len2 != 0);
$crc1 ^= $crc2;
return $crc1;
}
function gf2_matrix_square(&$square, &$mat)
{
for ($n=0;$n<32;$n++) {
$square[$n]=gf2_matrix_times($mat, $mat[$n]);
}
}
function gf2_matrix_times($mat, $vec)
{
$sum=0;
$i=0;
while ($vec) {
if ($vec & 1) {
$sum ^= $mat[$i];
}
$vec>>= 1;
$i++;
}
return $sum;
}
?>
----
Server IP: 62.237.7.3
Probable Submitter: 62.142.226.172
----
Manual Page -- http://www.php.net/manual/en/function.crc32.php
Edit -- https://master.php.net/note/edit/76080
Del: integrated -- https://master.php.net/note/delete/76080/integrated
Del: useless -- https://master.php.net/note/delete/76080/useless
Del: bad code -- https://master.php.net/note/delete/76080/bad+code
Del: spam -- https://master.php.net/note/delete/76080/spam
Del: non-english -- https://master.php.net/note/delete/76080/non-english
Del: in docs -- https://master.php.net/note/delete/76080/in+docs
Del: other reasons-- https://master.php.net/note/delete/76080
Reject -- https://master.php.net/note/reject/76080
Search -- https://master.php.net/manage/user-notes.php