-
Notifications
You must be signed in to change notification settings - Fork 7.9k
phar: use crc32 bulk method instead. #6099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -882,7 +882,6 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{ | |
|
||
/* do extra field for perms later */ | ||
if (entry->is_modified) { | ||
uint32_t loc; | ||
php_stream_filter *filter; | ||
php_stream *efp; | ||
|
||
|
@@ -915,9 +914,7 @@ static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{ | |
efp = phar_get_efp(entry, 0); | ||
newcrc32 = ~0; | ||
|
||
for (loc = 0;loc < entry->uncompressed_filesize; ++loc) { | ||
CRC32(newcrc32, php_stream_getc(efp)); | ||
} | ||
crc32_stream_bulk_update(&newcrc32, efp, entry->uncompressed_filesize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
||
entry->crc32 = ~newcrc32; | ||
PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,24 +89,12 @@ static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) { | |
# endif | ||
#endif | ||
|
||
/* {{{ Calculate the crc32 polynomial of a string */ | ||
PHP_FUNCTION(crc32) | ||
uint32_t crc32_bulk_update(uint32_t crc, const char *p, size_t nr) | ||
{ | ||
char *p; | ||
size_t nr; | ||
uint32_t crcinit = 0; | ||
uint32_t crc; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_STRING(p, nr) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
crc = crcinit^0xFFFFFFFF; | ||
|
||
#if HAVE_AARCH64_CRC32 | ||
if (has_crc32_insn()) { | ||
crc = crc32_aarch64(crc, p, nr); | ||
RETURN_LONG(crc^0xFFFFFFFF); | ||
return crc; | ||
} | ||
#endif | ||
|
||
|
@@ -116,9 +104,51 @@ PHP_FUNCTION(crc32) | |
p += nr_simd; | ||
#endif | ||
|
||
/* The trailing part */ | ||
for (; nr--; ++p) { | ||
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ]; | ||
} | ||
|
||
return crc; | ||
} | ||
|
||
int crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr) | ||
{ | ||
size_t handled = 0, n; | ||
char buf[1024]; | ||
|
||
while (handled < nr) { | ||
n = nr - handled; | ||
n = (n < sizeof(buf)) ? n : sizeof(buf); /* tweak to buf size */ | ||
|
||
n = php_stream_read(fp, buf, n); | ||
if (n > 0) { | ||
*crc = crc32_bulk_update(*crc, buf, n); | ||
handled += n; | ||
} else { /* EOF */ | ||
return FAILURE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT Thanks! |
||
} | ||
} | ||
|
||
return SUCCESS; | ||
} | ||
|
||
/* {{{ Calculate the crc32 polynomial of a string */ | ||
PHP_FUNCTION(crc32) | ||
{ | ||
char *p; | ||
size_t nr; | ||
uint32_t crcinit = 0; | ||
uint32_t crc; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_STRING(p, nr) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
crc = crcinit^0xFFFFFFFF; | ||
|
||
crc = crc32_bulk_update(crc, p, nr); | ||
|
||
RETURN_LONG(crc^0xFFFFFFFF); | ||
} | ||
/* }}} */ |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems we don't need check the return as it just generate a
crc
value here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be another plus to return the processed length and compare with
uncompressed_filesize
for a better security. Otherwise might make sense to(void)
to silence possible static analyzer warnings.thanks.