-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/random: Various minor refactorings #18435
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
Open
Girgias
wants to merge
6
commits into
php:master
Choose a base branch
from
Girgias:random-refacto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−48
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b3f9b4
ext/random: Reduce scope of variables
Girgias 47af7d0
ext/random: Add const qualifier
Girgias e62db48
ext/random: Use uint32_t type instead of int type
Girgias db32255
ext/random: Use zend_string macro instead of accessing value directly
Girgias c1b8705
ext/random: Remove useless variable
Girgias 038886a
ext/standard: sync php_hex2bin() with php_random_hex2bin_le()
Girgias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -240,7 +240,7 @@ PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool per | |||||
| return algo->state_size > 0 ? pecalloc(1, algo->state_size, persistent) : NULL; | ||||||
| } | ||||||
|
|
||||||
| PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status) | ||||||
| PHPAPI void *php_random_status_copy(const php_random_algo *algo, const void *old_status, void *new_status) | ||||||
| { | ||||||
| return memcpy(new_status, old_status, algo->state_size); | ||||||
| } | ||||||
|
|
@@ -354,12 +354,12 @@ PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len) | |||||
|
|
||||||
| /* {{{ php_random_hex2bin_le */ | ||||||
| /* stolen from standard/string.c */ | ||||||
| PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest) | ||||||
| PHPAPI bool php_random_hex2bin_le(const zend_string *hexstr, void *dest) | ||||||
| { | ||||||
| size_t len = hexstr->len >> 1; | ||||||
| unsigned char *str = (unsigned char *) hexstr->val, c, l, d; | ||||||
| const unsigned char *str = (unsigned char *) ZSTR_VAL(hexstr); | ||||||
| unsigned char *ptr = (unsigned char *) dest; | ||||||
|
Member
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.
Suggested change
Unnecessary cast from |
||||||
| int is_letter, i = 0; | ||||||
| uint32_t i = 0; | ||||||
|
Member
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. This one is a
Suggested change
Alternatively pointer arithmetic could be used on |
||||||
|
|
||||||
| #ifdef WORDS_BIGENDIAN | ||||||
| /* force little endian */ | ||||||
|
|
@@ -368,9 +368,10 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest) | |||||
| #else | ||||||
| for (size_t j = 0; j < len; j++) { | ||||||
| #endif | ||||||
| c = str[i++]; | ||||||
| l = c & ~0x20; | ||||||
| is_letter = ((uint32_t) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(uint32_t) - 1); | ||||||
| unsigned char c = str[i++]; | ||||||
| unsigned char l = c & ~0x20; | ||||||
| uint32_t is_letter = ((uint32_t) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(uint32_t) - 1); | ||||||
| unsigned char d; | ||||||
|
|
||||||
| /* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */ | ||||||
| if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(uint32_t) - 1)) | is_letter)) { | ||||||
|
|
@@ -461,7 +462,7 @@ PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max) | |||||
| * rand() allows min > max, mt_rand does not */ | ||||||
| PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max) | ||||||
| { | ||||||
| php_random_status_state_mt19937 *s = php_random_default_status(); | ||||||
| const php_random_status_state_mt19937 *s = php_random_default_status(); | ||||||
|
|
||||||
| if (s->mode == MT_RAND_MT19937) { | ||||||
| return php_mt_rand_range(min, max); | ||||||
|
|
@@ -523,9 +524,8 @@ PHP_FUNCTION(mt_srand) | |||||
| PHP_FUNCTION(mt_rand) | ||||||
| { | ||||||
| zend_long min, max; | ||||||
| int argc = ZEND_NUM_ARGS(); | ||||||
|
|
||||||
| if (argc == 0) { | ||||||
| if (ZEND_NUM_ARGS() == 0) { | ||||||
| /* genrand_int31 in mt19937ar.c performs a right shift */ | ||||||
| RETURN_LONG(php_mt_rand() >> 1); | ||||||
| } | ||||||
|
|
@@ -561,9 +561,8 @@ PHP_FUNCTION(mt_getrandmax) | |||||
| PHP_FUNCTION(rand) | ||||||
| { | ||||||
| zend_long min, max; | ||||||
| int argc = ZEND_NUM_ARGS(); | ||||||
|
|
||||||
| if (argc == 0) { | ||||||
| if (ZEND_NUM_ARGS() == 0) { | ||||||
| /* genrand_int31 in mt19937ar.c performs a right shift */ | ||||||
| RETURN_LONG(php_mt_rand() >> 1); | ||||||
| } | ||||||
|
|
@@ -632,7 +631,7 @@ PHP_FUNCTION(random_int) | |||||
| } | ||||||
| /* }}} */ | ||||||
|
|
||||||
| static inline void fallback_seed_add(PHP_SHA1_CTX *c, void *p, size_t l){ | ||||||
| static inline void fallback_seed_add(PHP_SHA1_CTX *c, const void *p, size_t l){ | ||||||
| /* Wrapper around PHP_SHA1Update allowing to pass | ||||||
| * arbitrary pointers without (unsigned char*) casts | ||||||
| * everywhere. | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.