note 100062 deleted from function.crc32 by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Oct 2011 21:24:37 +0000 |
| Subject: | note 100062 deleted from function.crc32 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183678@lists.php.net to get a copy of this message | ||
Note Submitter: chernyshevsky at hotmail dot com
----
Here's how you can compute the CRC32 of a stream as it's being read using a stream filter
with the help of the crc32_combine() function provided by petteri at qred dot fi:
// registering filter
class crc32_filter extends php_user_filter {
public $params;
public function filter($in, $out, &$consumed, $closing) {
while($bucket = stream_bucket_make_writeable($in)) {
$consumed += $bucket->datalen;
$this->params->crc32 = crc32_combine($this->params->crc32, crc32($bucket->data),
$bucket->datalen);
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
stream_filter_register('crc32', 'crc32_filter');
// usage
$params->crc32 = 0;
stream_filter_append($file, 'crc32', STREAM_FILTER_READ, $params);
while($stuff = fread($file, 10)) {
}
echo $params->crc32;
This technique is useful if you want to use the zlib.deflate filter to compress incoming data, but
also need the CRC32 signature of the original file. In this case, you append the crc32 filter first,
then append zlib.deflate. When all the data from the stream is read, the crc32 member in the params
object is the value you want.