Skip to content

Commit

Permalink
phar: crc32: Extend and cleanup API for the new bulk crc32 functions
Browse files Browse the repository at this point in the history
As suggested on the patch discussion, adding init/end macros. Plus,
prefixed the new functions with php_ to avoid possible symbol conflicts.

Signed-off-by: Anatol Belski <ab@php.net>
  • Loading branch information
weltling committed Jul 3, 2021
1 parent fea437a commit e7123ef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions ext/phar/phar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 */
Expand Down
10 changes: 5 additions & 5 deletions ext/phar/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
15 changes: 6 additions & 9 deletions ext/standard/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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));
}
/* }}} */
7 changes: 5 additions & 2 deletions ext/standard/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down

0 comments on commit e7123ef

Please sign in to comment.