Skip to content

Commit 6324ba0

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 c8d47cf commit 6324ba0

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

ext/phar/phar.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,8 @@ int phar_open_executed_filename(char *alias, size_t alias_len, char **error) /*
23852385
int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */
23862386
{
23872387
uint32_t crc = ~0;
2388-
int len = idata->internal_file->uncompressed_filesize;
2388+
uint32_t crc_len = idata->internal_file->uncompressed_filesize;
2389+
char crc_buf[1024];
23892390
php_stream *fp = idata->fp;
23902391
phar_entry_info *entry = idata->internal_file;
23912392

@@ -2450,13 +2451,19 @@ int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error,
24502451

24512452
php_stream_seek(fp, idata->zero, SEEK_SET);
24522453

2453-
while (len--) {
2454-
CRC32(crc, php_stream_getc(fp));
2454+
while (crc_len > 0) {
2455+
size_t n = php_stream_read(fp, crc_buf, (crc_len < sizeof(crc_buf)) ? crc_len : sizeof(crc_buf));
2456+
if (n > 0) {
2457+
crc = crc32_bulk_update(crc, crc_buf, n);
2458+
crc_len -= n;
2459+
} else { /* EOF or error */
2460+
break;
2461+
}
24552462
}
24562463

24572464
php_stream_seek(fp, idata->zero, SEEK_SET);
24582465

2459-
if (~crc == crc32) {
2466+
if (crc_len == 0 && ~crc == crc32) {
24602467
entry->is_crc_checked = 1;
24612468
return SUCCESS;
24622469
} else {
@@ -2550,8 +2557,10 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25502557
zend_off_t manifest_ftell;
25512558
zend_long offset;
25522559
size_t wrote;
2553-
uint32_t manifest_len, mytime, loc, new_manifest_count;
2560+
uint32_t manifest_len, mytime, new_manifest_count;
25542561
uint32_t newcrc32;
2562+
size_t crc_len;
2563+
char crc_buf[1024];
25552564
php_stream *file, *oldfile, *newfile, *stubfile;
25562565
php_stream_filter *filter;
25572566
php_serialize_data_t metadata_hash;
@@ -2805,13 +2814,21 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
28052814
}
28062815
return EOF;
28072816
}
2817+
28082818
newcrc32 = ~0;
2809-
mytime = entry->uncompressed_filesize;
2810-
for (loc = 0;loc < mytime; ++loc) {
2811-
CRC32(newcrc32, php_stream_getc(file));
2819+
crc_len = entry->uncompressed_filesize;
2820+
while (crc_len > 0) {
2821+
size_t n = php_stream_read(file, crc_buf, (crc_len < sizeof(crc_buf)) ? crc_len : sizeof(crc_buf));
2822+
if (n > 0) {
2823+
newcrc32 = crc32_bulk_update(newcrc32, crc_buf, n);
2824+
crc_len -= n;
2825+
} else { /* EOF or error */
2826+
break;
2827+
}
28122828
}
28132829
entry->crc32 = ~newcrc32;
28142830
entry->is_crc_checked = 1;
2831+
28152832
if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
28162833
/* not compressed */
28172834
entry->compressed_filesize = entry->uncompressed_filesize;

ext/phar/zip.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,6 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
788788
phar_zip_unix3 perms;
789789
phar_zip_central_dir_file central;
790790
struct _phar_zip_pass *p;
791-
uint32_t newcrc32;
792791
zend_off_t offset;
793792
int not_really_modified = 0;
794793
p = (struct _phar_zip_pass*) arg;
@@ -845,9 +844,11 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
845844

846845
/* do extra field for perms later */
847846
if (entry->is_modified) {
848-
uint32_t loc;
849847
php_stream_filter *filter;
850848
php_stream *efp;
849+
uint32_t newcrc32;
850+
size_t crc_len;
851+
char crc_buf[1024];
851852

852853
if (entry->is_dir) {
853854
entry->is_modified = 0;
@@ -878,8 +879,15 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{
878879
efp = phar_get_efp(entry, 0);
879880
newcrc32 = ~0;
880881

881-
for (loc = 0;loc < entry->uncompressed_filesize; ++loc) {
882-
CRC32(newcrc32, php_stream_getc(efp));
882+
crc_len = entry->uncompressed_filesize;
883+
while (crc_len > 0) {
884+
size_t n = php_stream_read(efp, crc_buf, (crc_len < sizeof(crc_buf)) ? crc_len : sizeof(crc_buf));
885+
if (n > 0) {
886+
newcrc32 = crc32_bulk_update(newcrc32, crc_buf, n);
887+
crc_len -= n;
888+
} else { /* EOF or error */
889+
break;
890+
}
883891
}
884892

885893
entry->crc32 = ~newcrc32;

ext/standard/crc32.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,13 @@ static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) {
6969
# pragma GCC pop_options
7070
#endif
7171

72-
/* {{{ Calculate the crc32 polynomial of a string */
73-
PHP_FUNCTION(crc32)
72+
uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr)
7473
{
75-
char *p;
76-
size_t nr;
77-
uint32_t crcinit = 0;
78-
uint32_t crc;
79-
80-
ZEND_PARSE_PARAMETERS_START(1, 1)
81-
Z_PARAM_STRING(p, nr)
82-
ZEND_PARSE_PARAMETERS_END();
83-
84-
crc = crcinit^0xFFFFFFFF;
85-
8674
#if HAVE_AARCH64_CRC32
8775
if (has_crc32_insn()) {
8876
crc = crc32_aarch64(crc, p, nr);
89-
RETURN_LONG(crc^0xFFFFFFFF);
77+
nr = 0;
78+
p += nr;
9079
}
9180
#endif
9281

@@ -96,9 +85,30 @@ PHP_FUNCTION(crc32)
9685
p += nr_simd;
9786
#endif
9887

88+
/* The trailing part */
9989
for (; nr--; ++p) {
10090
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ];
10191
}
92+
93+
return crc;
94+
}
95+
96+
/* {{{ Calculate the crc32 polynomial of a string */
97+
PHP_FUNCTION(crc32)
98+
{
99+
char *p;
100+
size_t nr;
101+
uint32_t crcinit = 0;
102+
uint32_t crc;
103+
104+
ZEND_PARSE_PARAMETERS_START(1, 1)
105+
Z_PARAM_STRING(p, nr)
106+
ZEND_PARSE_PARAMETERS_END();
107+
108+
crc = crcinit^0xFFFFFFFF;
109+
110+
crc = crc32_bulk_update(crc, p, nr);
111+
102112
RETURN_LONG(crc^0xFFFFFFFF);
103113
}
104114
/* }}} */

ext/standard/crc32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
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+
2628
/* generated using the AUTODIN II polynomial
2729
* x^32 + x^26 + x^23 + x^22 + x^16 +
2830
* x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1

0 commit comments

Comments
 (0)