note 54561 added to function.crc32
| From: | schmeic-php at lovo dot org | Date: | Thu, 07 Jul 2005 20:26:41 +0000 |
| Subject: | note 54561 added to function.crc32 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91612@lists.php.net to get a copy of this message | ||
I think that Mario's (spectrumizer at cycos dot net) CRC16 algorithm might be wrong. At least
according to these two online calculators:
http://www.zorc.breitbandkatze.de/crc.html
http://www.lammertbies.nl/comm/info/crc-calculation.html
However, it seems that there can be some confusion concerning "official" CRC16
algorithms. See this article for two possible CRC16-CCITT algorithms:
http://www.joegeluso.com/software/articles/ccitt.htm
Here is a php implementation of the "bad" CRC16-CCITT algorithm from the article. The
"bad" implementation might not match the "official" algorithm, but is widely
used, including by the two crc calculators mentioned above.
Note that crc16-CCITT is also referred to as crc16c.
<?php
// this function is used to calculate the (common) crc16c for an entire buffer
function calculate_common_crc16c($buffer)
{
$crc16c = 0xffff; // the crc initial value
$buffer_length = strlen($buffer);
for ($i = 0; $i < $buffer_length; $i++)
{
$ch = ord($buffer[$i]);
$crc16c = update_common_crc16c($ch, $crc16c);
}
return $crc16c;
}
// this function is used to calculate the (common) crc16c byte by byte
// $ch is the next byte and $crc16c is the result from the last call, or 0xffff initially
function update_common_crc16c($ch, $crc16c)
{
global $crc16c_polynomial;
// This comment was in the code from
// http://www.joegeluso.com/software/articles/ccitt.htm
// Why are they shifting this byte left by 8 bits??
// How do the low bits of the poly ever see it?
$ch <<= 8;
for($i = 0; $i < 8; $i++)
{
if (($crc16c ^ $ch) & 0x8000)
{
$xor_flag = true;
}
else
{
$xor_flag = false;
}
$crc16c = $crc16c << 1;
if ($xor_flag)
{
$crc16c = $crc16c ^ $crc16c_polynomial;
}
$ch = $ch << 1;
}
// mask off (zero out) the upper two bytes
$crc16c = $crc16c & 0x0000ffff;
return $crc16c;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.crc32.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54561
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54561&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54561&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54561&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54561&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54561&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54561&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54561&report=yes
Search -- http://master.php.net/manage/user-notes.php