Skip to content

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ext/random/engine_mt19937.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ static inline void mt19937_reload(php_random_status_state_mt19937 *state)

PHPAPI inline void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed)
{
uint32_t i, prev_state;
uint32_t i;

/* Initialize generator state with seed
See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
In previous versions, most significant bits (MSBs) of the seed affect
only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto. */
state->state[0] = seed;
for (i = 1; i < N; i++) {
prev_state = state->state[i - 1];
uint32_t prev_state = state->state[i - 1];
state->state[i] = (1812433253U * (prev_state ^ (prev_state >> 30)) + i) & 0xffffffffU;
}
state->count = i;
Expand Down Expand Up @@ -188,7 +188,7 @@ static bool serialize(void *state, HashTable *data)
return true;
}

static bool unserialize(void *state, HashTable *data)
static bool unserialize(void *state, const HashTable *data)
{
php_random_status_state_mt19937 *s = state;
zval *t;
Expand Down
5 changes: 2 additions & 3 deletions ext/random/engine_pcgoneseq128xslrr64.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,18 @@ static bool serialize(void *state, HashTable *data)
return true;
}

static bool unserialize(void *state, HashTable *data)
static bool unserialize(void *state, const HashTable *data)
{
php_random_status_state_pcgoneseq128xslrr64 *s = state;
uint64_t u[2];
zval *t;

/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
if (zend_hash_num_elements(data) != 2) {
return false;
}

for (uint32_t i = 0; i < 2; i++) {
t = zend_hash_index_find(data, i);
zval *t = zend_hash_index_find(data, i);
if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint64_t))) {
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions ext/random/engine_xoshiro256starstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,17 @@ static bool serialize(void *state, HashTable *data)
return true;
}

static bool unserialize(void *state, HashTable *data)
static bool unserialize(void *state, const HashTable *data)
{
php_random_status_state_xoshiro256starstar *s = state;
zval *t;

/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
if (zend_hash_num_elements(data) != 4) {
return false;
}

for (uint32_t i = 0; i < 4; i++) {
t = zend_hash_index_find(data, i);
zval *t = zend_hash_index_find(data, i);
if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint64_t))) {
return false;
}
Expand Down
21 changes: 10 additions & 11 deletions ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -250,7 +250,7 @@ PHPAPI void php_random_status_free(void *status, const bool persistent)
pefree(status, persistent);
}

PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo)
PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, const zend_object_handlers *handlers, const php_random_algo *algo)
{
php_random_engine *engine = zend_object_alloc(sizeof(php_random_engine), ce);

Expand Down Expand Up @@ -355,12 +355,13 @@ 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 c, l, d;
unsigned char *ptr = (unsigned char *) dest;
int is_letter, i = 0;
uint32_t is_letter, i = 0;

#ifdef WORDS_BIGENDIAN
/* force little endian */
Expand Down Expand Up @@ -462,7 +463,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);
Expand Down Expand Up @@ -524,9 +525,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);
}
Expand Down Expand Up @@ -562,9 +562,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);
}
Expand Down Expand Up @@ -633,7 +632,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.
Expand Down
27 changes: 14 additions & 13 deletions ext/random/randomizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ PHP_METHOD(Random_Randomizer, __construct)
/* {{{ Generate a float in [0, 1) */
PHP_METHOD(Random_Randomizer, nextFloat)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
php_random_algo_with_state engine = randomizer->engine;

uint64_t result;
Expand Down Expand Up @@ -136,7 +136,7 @@ PHP_METHOD(Random_Randomizer, nextFloat)
*/
PHP_METHOD(Random_Randomizer, getFloat)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
double min, max;
zend_object *bounds = NULL;
int bounds_type = 'C' + sizeof("ClosedOpen") - 1;
Expand All @@ -159,8 +159,8 @@ PHP_METHOD(Random_Randomizer, getFloat)
}

if (bounds) {
zval *case_name = zend_enum_fetch_case_name(bounds);
zend_string *bounds_name = Z_STR_P(case_name);
const zval *case_name = zend_enum_fetch_case_name(bounds);
const zend_string *bounds_name = Z_STR_P(case_name);

bounds_type = ZSTR_VAL(bounds_name)[0] + ZSTR_LEN(bounds_name);
}
Expand Down Expand Up @@ -210,7 +210,7 @@ PHP_METHOD(Random_Randomizer, getFloat)
/* {{{ Generate positive random number */
PHP_METHOD(Random_Randomizer, nextInt)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
php_random_algo_with_state engine = randomizer->engine;

ZEND_PARSE_PARAMETERS_NONE();
Expand All @@ -231,7 +231,7 @@ PHP_METHOD(Random_Randomizer, nextInt)
/* {{{ Generate random number in range */
PHP_METHOD(Random_Randomizer, getInt)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
php_random_algo_with_state engine = randomizer->engine;

uint64_t result;
Expand Down Expand Up @@ -274,7 +274,7 @@ PHP_METHOD(Random_Randomizer, getInt)
/* {{{ Generate random bytes string in ordered length */
PHP_METHOD(Random_Randomizer, getBytes)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
php_random_algo_with_state engine = randomizer->engine;

zend_string *retval;
Expand Down Expand Up @@ -330,7 +330,7 @@ PHP_METHOD(Random_Randomizer, getBytes)
RETURN_THROWS();
}

non_64:
non_64:

for (size_t i = 0; i < result.size; i++) {
ZSTR_VAL(retval)[total_size++] = result.result & 0xff;
Expand All @@ -349,7 +349,7 @@ PHP_METHOD(Random_Randomizer, getBytes)
/* {{{ Shuffling array */
PHP_METHOD(Random_Randomizer, shuffleArray)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
zval *array;

ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -366,7 +366,7 @@ PHP_METHOD(Random_Randomizer, shuffleArray)
/* {{{ Shuffling binary */
PHP_METHOD(Random_Randomizer, shuffleBytes)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
zend_string *bytes;

ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -387,8 +387,8 @@ PHP_METHOD(Random_Randomizer, shuffleBytes)
/* {{{ Pick keys */
PHP_METHOD(Random_Randomizer, pickArrayKeys)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
zval *input, t;
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
zval *input;
zend_long num_req;

ZEND_PARSE_PARAMETERS_START(2, 2);
Expand All @@ -408,6 +408,7 @@ PHP_METHOD(Random_Randomizer, pickArrayKeys)

/* Keep compatibility, But the result is always an array */
if (Z_TYPE_P(return_value) != IS_ARRAY) {
zval t;
ZVAL_COPY_VALUE(&t, return_value);
array_init(return_value);
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &t);
Expand All @@ -418,7 +419,7 @@ PHP_METHOD(Random_Randomizer, pickArrayKeys)
/* {{{ Get Random Bytes for String */
PHP_METHOD(Random_Randomizer, getBytesFromString)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
php_random_algo_with_state engine = randomizer->engine;

zend_long user_length;
Expand Down
Loading