Skip to content

Commit efb1fa6

Browse files
committed
phar: use crc32 bulk method instead.
Benefit from the hardware crc32 computing. Signed-off-by: Frank Du <frank.du@intel.com>
1 parent cfec7a4 commit efb1fa6

File tree

4 files changed

+54
-26
lines changed

4 files changed

+54
-26
lines changed

ext/phar/phar.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,9 +2439,7 @@ 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-
while (len--) {
2443-
CRC32(crc, php_stream_getc(fp));
2444-
}
2442+
crc = crc32_stream_bulk_update(crc, fp, len);
24452443

24462444
php_stream_seek(fp, idata->zero, SEEK_SET);
24472445

@@ -2539,7 +2537,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25392537
zend_off_t manifest_ftell;
25402538
zend_long offset;
25412539
size_t wrote;
2542-
uint32_t manifest_len, mytime, loc, new_manifest_count;
2540+
uint32_t manifest_len, mytime, new_manifest_count;
25432541
uint32_t newcrc32;
25442542
php_stream *file, *oldfile, *newfile, *stubfile;
25452543
php_stream_filter *filter;
@@ -2796,10 +2794,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
27962794
return EOF;
27972795
}
27982796
newcrc32 = ~0;
2799-
mytime = entry->uncompressed_filesize;
2800-
for (loc = 0;loc < mytime; ++loc) {
2801-
CRC32(newcrc32, php_stream_getc(file));
2802-
}
2797+
newcrc32 = crc32_stream_bulk_update(newcrc32, file, entry->uncompressed_filesize);
28032798
entry->crc32 = ~newcrc32;
28042799
entry->is_crc_checked = 1;
28052800
if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {

ext/phar/zip.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,6 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
882882

883883
/* do extra field for perms later */
884884
if (entry->is_modified) {
885-
uint32_t loc;
886885
php_stream_filter *filter;
887886
php_stream *efp;
888887

@@ -915,9 +914,7 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
915914
efp = phar_get_efp(entry, 0);
916915
newcrc32 = ~0;
917916

918-
for (loc = 0;loc < entry->uncompressed_filesize; ++loc) {
919-
CRC32(newcrc32, php_stream_getc(efp));
920-
}
917+
newcrc32 = crc32_stream_bulk_update(newcrc32, efp, entry->uncompressed_filesize);
921918

922919
entry->crc32 = ~newcrc32;
923920
PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize);

ext/standard/crc32.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,12 @@ static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) {
8080
# endif
8181
#endif
8282

83-
/* {{{ Calculate the crc32 polynomial of a string */
84-
PHP_FUNCTION(crc32)
83+
uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr)
8584
{
86-
char *p;
87-
size_t nr;
88-
uint32_t crcinit = 0;
89-
uint32_t crc;
90-
91-
ZEND_PARSE_PARAMETERS_START(1, 1)
92-
Z_PARAM_STRING(p, nr)
93-
ZEND_PARSE_PARAMETERS_END();
94-
95-
crc = crcinit^0xFFFFFFFF;
96-
9785
#if HAVE_AARCH64_CRC32
9886
if (has_crc32_insn()) {
9987
crc = crc32_aarch64(crc, p, nr);
100-
RETURN_LONG(crc^0xFFFFFFFF);
88+
return crc;
10189
}
10290
#endif
10391

@@ -107,9 +95,53 @@ PHP_FUNCTION(crc32)
10795
p += nr_simd;
10896
#endif
10997

98+
/* The trailing part */
11099
for (; nr--; ++p) {
111100
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ];
112101
}
102+
103+
return crc;
104+
}
105+
106+
uint32_t crc32_stream_bulk_update(uint32_t crc, php_stream *fp, size_t nr)
107+
{
108+
size_t handled = 0;
109+
char buf[1024];
110+
111+
while (handled < nr) {
112+
size_t remaining = nr - handled;
113+
size_t n = php_stream_read(fp, buf, (remaining < sizeof(buf)) ? remaining : sizeof(buf));
114+
if (n > 0) {
115+
crc = crc32_bulk_update(crc, buf, n);
116+
handled += n;
117+
} else { /* EOF */
118+
while (handled < nr) { /* Feed with EOF in case required more than actual stream */
119+
CRC32(crc, EOF);
120+
handled++;
121+
}
122+
break;
123+
}
124+
}
125+
126+
return crc;
127+
}
128+
129+
/* {{{ Calculate the crc32 polynomial of a string */
130+
PHP_FUNCTION(crc32)
131+
{
132+
char *p;
133+
size_t nr;
134+
uint32_t crcinit = 0;
135+
uint32_t crc;
136+
137+
ZEND_PARSE_PARAMETERS_START(1, 1)
138+
Z_PARAM_STRING(p, nr)
139+
ZEND_PARSE_PARAMETERS_END();
140+
141+
crc = crcinit^0xFFFFFFFF;
142+
143+
crc = crc32_bulk_update(crc, p, nr);
144+
113145
RETURN_LONG(crc^0xFFFFFFFF);
114146
}
115147
/* }}} */

ext/standard/crc32.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

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

26+
uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr);
27+
28+
uint32_t crc32_stream_bulk_update(uint32_t crc, php_stream *fp, size_t nr);
29+
2630
/* generated using the AUTODIN II polynomial
2731
* x^32 + x^26 + x^23 + x^22 + x^16 +
2832
* x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1

0 commit comments

Comments
 (0)