diff --git a/ext/phar/phar.c b/ext/phar/phar.c index c2184c58d843f..988ee41e7c3da 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -2373,7 +2373,7 @@ int phar_open_executed_filename(char *alias, size_t alias_len, char **error) /* */ int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */ { - uint32_t crc = ~0; + uint32_t crc = php_crc32_bulk_init(); int len = idata->internal_file->uncompressed_filesize, ret; php_stream *fp = idata->fp; phar_entry_info *entry = idata->internal_file; @@ -2439,11 +2439,11 @@ int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, php_stream_seek(fp, idata->zero, SEEK_SET); - ret = crc32_stream_bulk_update(&crc, fp, len); + ret = php_crc32_stream_bulk_update(&crc, fp, len); php_stream_seek(fp, idata->zero, SEEK_SET); - if (SUCCESS == ret && ~crc == crc32) { + if (SUCCESS == ret && php_crc32_bulk_end(crc) == crc32) { entry->is_crc_checked = 1; return SUCCESS; } else { @@ -2793,9 +2793,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } return EOF; } - newcrc32 = ~0; - crc32_stream_bulk_update(&newcrc32, file, entry->uncompressed_filesize); - entry->crc32 = ~newcrc32; + newcrc32 = php_crc32_bulk_init(); + php_crc32_stream_bulk_update(&newcrc32, file, entry->uncompressed_filesize); + entry->crc32 = php_crc32_bulk_end(newcrc32); entry->is_crc_checked = 1; if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) { /* not compressed */ diff --git a/ext/phar/zip.c b/ext/phar/zip.c index 0b0d39c470f10..31d4bd2998215 100644 --- a/ext/phar/zip.c +++ b/ext/phar/zip.c @@ -851,10 +851,10 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{ PHAR_SET_16(perms.size, sizeof(perms) - 4); PHAR_SET_16(perms.perms, entry->flags & PHAR_ENT_PERM_MASK); { - uint32_t crc = (uint32_t) ~0; + uint32_t crc = (uint32_t) php_crc32_bulk_init(); CRC32(crc, perms.perms[0]); CRC32(crc, perms.perms[1]); - PHAR_SET_32(perms.crc32, ~crc); + PHAR_SET_32(perms.crc32, php_crc32_bulk_end(crc)); } if (entry->flags & PHAR_ENT_COMPRESSED_GZ) { @@ -912,11 +912,11 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{ } efp = phar_get_efp(entry, 0); - newcrc32 = ~0; + newcrc32 = php_crc32_bulk_init(); - crc32_stream_bulk_update(&newcrc32, efp, entry->uncompressed_filesize); + php_crc32_stream_bulk_update(&newcrc32, efp, entry->uncompressed_filesize); - entry->crc32 = ~newcrc32; + entry->crc32 = php_crc32_bulk_end(newcrc32); PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize); PHAR_SET_32(local.uncompsize, entry->uncompressed_filesize); diff --git a/ext/standard/crc32.c b/ext/standard/crc32.c index 8b4fa085058a3..92dce07db54ea 100644 --- a/ext/standard/crc32.c +++ b/ext/standard/crc32.c @@ -89,7 +89,7 @@ static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) { # endif #endif -PHPAPI uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr) +PHPAPI uint32_t php_crc32_bulk_update(uint32_t crc, const char *p, size_t nr) { #if HAVE_AARCH64_CRC32 if (has_crc32_insn()) { @@ -112,7 +112,7 @@ PHPAPI uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr) return crc; } -PHPAPI int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr) +PHPAPI int php_crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr) { size_t handled = 0, n; char buf[1024]; @@ -123,7 +123,7 @@ PHPAPI int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr) n = php_stream_read(fp, buf, n); if (n > 0) { - *crc = crc32_bulk_update(*crc, buf, n); + *crc = php_crc32_bulk_update(*crc, buf, n); handled += n; } else { /* EOF */ return FAILURE; @@ -138,17 +138,14 @@ PHP_FUNCTION(crc32) { char *p; size_t nr; - uint32_t crcinit = 0; - uint32_t crc; + uint32_t crc = php_crc32_bulk_init(); ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STRING(p, nr) ZEND_PARSE_PARAMETERS_END(); - crc = crcinit^0xFFFFFFFF; + crc = php_crc32_bulk_update(crc, p, nr); - crc = crc32_bulk_update(crc, p, nr); - - RETURN_LONG(crc^0xFFFFFFFF); + RETURN_LONG(php_crc32_bulk_end(crc)); } /* }}} */ diff --git a/ext/standard/crc32.h b/ext/standard/crc32.h index 1de234208aa9e..bdf45a4bef511 100644 --- a/ext/standard/crc32.h +++ b/ext/standard/crc32.h @@ -23,10 +23,13 @@ #define CRC32(crc, ch) (crc = (crc >> 8) ^ crc32tab[(crc ^ (ch)) & 0xff]) -PHPAPI uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr); +#define php_crc32_bulk_init() (0 ^ 0xffffffff) +#define php_crc32_bulk_end(c) ((c) ^ 0xffffffff) + +PHPAPI uint32_t php_crc32_bulk_update(uint32_t crc, const char *p, size_t nr); /* Return FAILURE if stream reading fail */ -PHPAPI int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr); +PHPAPI int php_crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr); /* generated using the AUTODIN II polynomial * x^32 + x^26 + x^23 + x^22 + x^16 +