# crc32

- Thread: https://externals.io/message/18675
- Original archive: https://news-web.php.net/php.internals/18675
- Started by Wojtek Meler on 2005-09-05T11:08:40+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #18675 — Wojtek Meler — 2005-09-05T11:08:40+00:00

URL: https://externals.io/message/18675#18675

I've patched original crc32 PHP function to allow incremental crc32 
counting.
It touches only a few lines of original code.
New crc32 function signature is

    int crc32(string str [,int prev_sum])

With this patch you can generate crc32 checksum for even large files 
without reading it contents to single variable.
Fe. instead of doing

|   $file_string = file_get_contents($file);
   $crc = crc32($file_string);  
|
you can do

    $crc=0;
    $f =fopen($file,'r');
    while ( $data=fread($f,1024)) $crc = crc32($data,$crc);
    fclose($f);

Icremental processing has less memory requirements.
I'd like also change base64_* functions to allow incremental processing.

Any chances apply such changes ?

Regards,
Wojtek

---

## #18676 — Wojtek Meler — 2005-09-05T11:37:55+00:00

URL: https://externals.io/message/18675#18676 · In reply to: #18675

Oops !!! I've left old license header. Here is smaller clean version that apply to CVS HEAD.

---

## #18678 — Ron Korving — 2005-09-05T14:02:21+00:00

URL: https://externals.io/message/18675#18678 · In reply to: #18675

Wouldn't it be more practical to implement these as stream filters?

Ron


"Wojtek Meler" <wmeler@wp.pl> schreef in bericht
news:431C2738.2040602@wp.pl...
> I've patched original crc32 PHP function to allow incremental crc32
> counting.
> It touches only a few lines of original code.
> New crc32 function signature is
>
>     int crc32(string str [,int prev_sum])
>
> With this patch you can generate crc32 checksum for even large files
> without reading it contents to single variable.
> Fe. instead of doing
>
> |   $file_string = file_get_contents($file);
>    $crc = crc32($file_string);
> |
> you can do
>
>     $crc=0;
>     $f =fopen($file,'r');
>     while ( $data=fread($f,1024)) $crc = crc32($data,$crc);
>     fclose($f);
>
> Icremental processing has less memory requirements.
> I'd like also change base64_* functions to allow incremental processing.
>
> Any chances apply such changes ?
>
> Regards,
> Wojtek
>


----------------------------------------------------------------------------
----


> Index: crc32.c
> ===================================================================
> RCS file: /repository/php-src/ext/standard/crc32.c,v
> retrieving revision 1.16
> diff -u -r1.16 crc32.c
> --- crc32.c 3 Aug 2005 14:07:57 -0000 1.16
> +++ crc32.c 5 Sep 2005 11:02:00 -0000
> @@ -1,13 +1,13 @@
>  /*
>
+----------------------------------------------------------------------+
> -   | PHP Version 5
|
> +   | PHP Version 4
|
>
+----------------------------------------------------------------------+
> -   | Copyright (c) 1997-2005 The PHP Group
|
> +   | Copyright (c) 1997-2003 The PHP Group
|
>
+----------------------------------------------------------------------+
> -   | This source file is subject to version 3.0 of the PHP license,
|
> +   | This source file is subject to version 2.02 of the PHP license,
|
>     | that is bundled with this package in the file LICENSE, and is
|
> -   | available through the world-wide-web at the following url:
|
> -   | http://www.php.net/license/3_0.txt.
|
> +   | available at through the world-wide-web at
|
> +   | http://www.php.net/license/2_02.txt.
|
>     | If you did not receive a copy of the PHP license and are unable to
|
>     | obtain it through the world-wide-web, please send a note to
|
>     | license@php.net so we can mail you a copy immediately.
|
> @@ -22,20 +22,19 @@
>  #include "basic_functions.h"
>  #include "crc32.h"
>
> -/* {{{ proto string crc32(string str)
> +/* {{{ proto int crc32(string str[,int prev_crc])
>     Calculate the crc32 polynomial of a string */
>  PHP_NAMED_FUNCTION(php_if_crc32)
>  {
> - unsigned int crc = ~0;
> + unsigned int crc = 0;
>   char *p;
> - int len, nr;
> + int len;
>
> - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &p, &nr) ==
FAILURE) {
> + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &p, &len,
&crc) == FAILURE) {
>   return;
>   }
> -
> - len = 0 ;
> - for (len += nr; nr--; ++p) {
> + crc = ~crc;
> + for (; len--; ++p) {
>       CRC32(crc, *p);
>   }
>   RETVAL_LONG(~crc);

---

## #18687 — Wojtek Meler — 2005-09-06T06:46:21+00:00

URL: https://externals.io/message/18675#18687 · In reply to: #18678

Ron Korving wrote:
> Wouldn't it be more practical to implement these as stream filters?

Sure - I was not aware PHP5 streams - I'm still using PHP4
string.base64 is good choice. But I'm not sure if crc fit to streams.
You can always implement fast userland filter with such function ;) .


Wojtek

---

## #18689 — Ron Korving — 2005-09-06T11:48:42+00:00

URL: https://externals.io/message/18675#18689 · In reply to: #18687

With a crc function the way you propose it, such a filter could be
effectively made in userspace, but you could of course also implement such a
filter in the core of php which could do the same trick. Maybe both would be
nice.

Ron


"Wojtek Meler" <wmeler@wp.pl> schreef in bericht
news:431D3B3D.3050607@wp.pl...
> Ron Korving wrote:
> > […]
>
> Sure - I was not aware PHP5 streams - I'm still using PHP4
> string.base64 is good choice. But I'm not sure if crc fit to streams.
> You can always implement fast userland filter with such function ;) .
>
>
> Wojtek

---

## #18692 — Sara Golemon — 2005-09-06T13:57:38+00:00

URL: https://externals.io/message/18675#18692 · In reply to: #18689

I'd suggest a set of filters as an add-on to the mhash extension:

mhash.crc32
mhash.md5
mhash.sha1
mhash.sha256
etc....

-Sara

----- Original Message ----- 
From: ""Ron Korving"" <r.korving@xit.nl>
Newsgroups: php.internals
To: <internals@lists.php.net>
Sent: Tuesday, September 06, 2005 4:48 AM
Subject: Re: crc32


> With a crc function the way you propose it, such a filter could be
> effectively made in userspace, but you could of course also implement such 
> a
> filter in the core of php which could do the same trick. Maybe both would 
> be
> nice.
>
> Ron
>
>
> "Wojtek Meler" <wmeler@wp.pl> schreef in bericht
> news:431D3B3D.3050607@wp.pl...
> > […]

---

## #18897 — Marcus Boerger — 2005-09-14T14:02:22+00:00

URL: https://externals.io/message/18675#18897 · In reply to: #18692

Hello Sara,

  nice and helpfull idea

best regards
marcus

Tuesday, September 6, 2005, 3:57:38 PM, you wrote:

> I'd suggest a set of filters as an add-on to the mhash extension:

> mhash.crc32
> mhash.md5
> mhash.sha1
> mhash.sha256
> etc....

> -Sara

> ----- Original Message ----- 
> From: ""Ron Korving"" <r.korving@xit.nl>
> Newsgroups: php.internals
> To: <internals@lists.php.net>
> Sent: Tuesday, September 06, 2005 4:48 AM
> Subject: Re: crc32


> > […]
