Skip to content

Commit e7123ef

Browse files
committed
phar: crc32: Extend and cleanup API for the new bulk crc32 functions
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>
1 parent fea437a commit e7123ef

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

ext/phar/phar.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ int phar_open_executed_filename(char *alias, size_t alias_len, char **error) /*
23732373
*/
23742374
int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */
23752375
{
2376-
uint32_t crc = ~0;
2376+
uint32_t crc = php_crc32_bulk_init();
23772377
int len = idata->internal_file->uncompressed_filesize, ret;
23782378
php_stream *fp = idata->fp;
23792379
phar_entry_info *entry = idata->internal_file;
@@ -2439,11 +2439,11 @@ int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error,
24392439

24402440
php_stream_seek(fp, idata->zero, SEEK_SET);
24412441

2442-
ret = crc32_stream_bulk_update(&crc, fp, len);
2442+
ret = php_crc32_stream_bulk_update(&crc, fp, len);
24432443

24442444
php_stream_seek(fp, idata->zero, SEEK_SET);
24452445

2446-
if (SUCCESS == ret && ~crc == crc32) {
2446+
if (SUCCESS == ret && php_crc32_bulk_end(crc) == crc32) {
24472447
entry->is_crc_checked = 1;
24482448
return SUCCESS;
24492449
} else {
@@ -2793,9 +2793,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
27932793
}
27942794
return EOF;
27952795
}
2796-
newcrc32 = ~0;
2797-
crc32_stream_bulk_update(&newcrc32, file, entry->uncompressed_filesize);
2798-
entry->crc32 = ~newcrc32;
2796+
newcrc32 = php_crc32_bulk_init();
2797+
php_crc32_stream_bulk_update(&newcrc32, file, entry->uncompressed_filesize);
2798+
entry->crc32 = php_crc32_bulk_end(newcrc32);
27992799
entry->is_crc_checked = 1;
28002800
if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
28012801
/* not compressed */

ext/phar/zip.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,10 +851,10 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
851851
PHAR_SET_16(perms.size, sizeof(perms) - 4);
852852
PHAR_SET_16(perms.perms, entry->flags & PHAR_ENT_PERM_MASK);
853853
{
854-
uint32_t crc = (uint32_t) ~0;
854+
uint32_t crc = (uint32_t) php_crc32_bulk_init();
855855
CRC32(crc, perms.perms[0]);
856856
CRC32(crc, perms.perms[1]);
857-
PHAR_SET_32(perms.crc32, ~crc);
857+
PHAR_SET_32(perms.crc32, php_crc32_bulk_end(crc));
858858
}
859859

860860
if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
@@ -912,11 +912,11 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
912912
}
913913

914914
efp = phar_get_efp(entry, 0);
915-
newcrc32 = ~0;
915+
newcrc32 = php_crc32_bulk_init();
916916

917-
crc32_stream_bulk_update(&newcrc32, efp, entry->uncompressed_filesize);
917+
php_crc32_stream_bulk_update(&newcrc32, efp, entry->uncompressed_filesize);
918918

919-
entry->crc32 = ~newcrc32;
919+
entry->crc32 = php_crc32_bulk_end(newcrc32);
920920
PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize);
921921
PHAR_SET_32(local.uncompsize, entry->uncompressed_filesize);
922922

ext/standard/crc32.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) {
8989
# endif
9090
#endif
9191

92-
PHPAPI uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr)
92+
PHPAPI uint32_t php_crc32_bulk_update(uint32_t crc, const char *p, size_t nr)
9393
{
9494
#if HAVE_AARCH64_CRC32
9595
if (has_crc32_insn()) {
@@ -112,7 +112,7 @@ PHPAPI uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr)
112112
return crc;
113113
}
114114

115-
PHPAPI int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr)
115+
PHPAPI int php_crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr)
116116
{
117117
size_t handled = 0, n;
118118
char buf[1024];
@@ -123,7 +123,7 @@ PHPAPI int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr)
123123

124124
n = php_stream_read(fp, buf, n);
125125
if (n > 0) {
126-
*crc = crc32_bulk_update(*crc, buf, n);
126+
*crc = php_crc32_bulk_update(*crc, buf, n);
127127
handled += n;
128128
} else { /* EOF */
129129
return FAILURE;
@@ -138,17 +138,14 @@ PHP_FUNCTION(crc32)
138138
{
139139
char *p;
140140
size_t nr;
141-
uint32_t crcinit = 0;
142-
uint32_t crc;
141+
uint32_t crc = php_crc32_bulk_init();
143142

144143
ZEND_PARSE_PARAMETERS_START(1, 1)
145144
Z_PARAM_STRING(p, nr)
146145
ZEND_PARSE_PARAMETERS_END();
147146

148-
crc = crcinit^0xFFFFFFFF;
147+
crc = php_crc32_bulk_update(crc, p, nr);
149148

150-
crc = crc32_bulk_update(crc, p, nr);
151-
152-
RETURN_LONG(crc^0xFFFFFFFF);
149+
RETURN_LONG(php_crc32_bulk_end(crc));
153150
}
154151
/* }}} */

ext/standard/crc32.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323

2424
#define CRC32(crc, ch) (crc = (crc >> 8) ^ crc32tab[(crc ^ (ch)) & 0xff])
2525

26-
PHPAPI uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr);
26+
#define php_crc32_bulk_init() (0 ^ 0xffffffff)
27+
#define php_crc32_bulk_end(c) ((c) ^ 0xffffffff)
28+
29+
PHPAPI uint32_t php_crc32_bulk_update(uint32_t crc, const char *p, size_t nr);
2730

2831
/* Return FAILURE if stream reading fail */
29-
PHPAPI int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr);
32+
PHPAPI int php_crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr);
3033

3134
/* generated using the AUTODIN II polynomial
3235
* x^32 + x^26 + x^23 + x^22 + x^16 +

0 commit comments

Comments
 (0)