Skip to content

Commit 24a2c7b

Browse files
committed
remove init
1 parent 2da3d4d commit 24a2c7b

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

ext/standard/php_random_class.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ extern PHPAPI zend_class_entry *php_ce_random;
2525

2626
typedef struct _php_random_class_algo {
2727
const char* ident;
28-
const unsigned char bytes;
28+
const size_t generate_size;
2929
const size_t state_size;
3030
uint64_t (*next)(void *state);
31-
void* (*init)(void);
3231
void (*seed)(void *state, const zend_long seed);
3332
int (*serialize)(void *state, zval *return_value);
3433
int (*unserialize)(void *state, HashTable *data);

ext/standard/random_class.c

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static uint64_t range64(php_random_class *random_class, uint64_t umax) {
6363
uint64_t result, limit;
6464

6565
result = php_random_class_next(random_class);
66-
if (random_class->algo && random_class->algo->bytes == 32) {
66+
if (random_class->algo && random_class->algo->generate_size == 32) {
6767
result = (result << 32) | php_random_class_next(random_class);
6868
}
6969

@@ -86,7 +86,7 @@ static uint64_t range64(php_random_class *random_class, uint64_t umax) {
8686
/* Discard numbers over the limit to avoid modulo bias */
8787
while (UNEXPECTED(result > limit)) {
8888
result = php_random_class_next(random_class);
89-
if (random_class->algo && random_class->algo->bytes == 32) {
89+
if (random_class->algo && random_class->algo->generate_size == 32) {
9090
result = (result << 32) | php_random_class_next(random_class);
9191
}
9292
}
@@ -275,7 +275,6 @@ void php_random_class_string_shuffle(php_random_class *random_class, char *str,
275275
}
276276
}
277277
/* }}} */
278-
279278
static zend_object *php_random_class_new(zend_class_entry *ce) {
280279
php_random_class *random_class = zend_object_alloc(sizeof(php_random_class), ce);
281280
zend_object_std_init(&random_class->std, ce);
@@ -285,6 +284,12 @@ static zend_object *php_random_class_new(zend_class_entry *ce) {
285284
return &random_class->std;
286285
}
287286

287+
static void php_random_class_state_initialize(php_random_class *random_class) {
288+
if (random_class->algo && random_class->algo->state_size > 0) {
289+
random_class->state = ecalloc(1, random_class->algo->state_size);
290+
}
291+
}
292+
288293
static void php_random_class_free_obj(zend_object *object) {
289294
php_random_class *random_class = random_class_from_obj(object);
290295
if (random_class->state) {
@@ -306,10 +311,8 @@ static zend_object *php_random_class_clone_obj(zend_object *object) {
306311

307312
if (old->algo) {
308313
new->algo = old->algo;
309-
if (old->algo->init && old->algo->state_size && old->state) {
310-
new->state = old->algo->init();
311-
memcpy(new->state, old->state, old->algo->state_size);
312-
}
314+
php_random_class_state_initialize(new);
315+
memcpy(new->state, old->state, old->algo->state_size);
313316
}
314317

315318
return new_obj;
@@ -344,10 +347,6 @@ static uint64_t xorshift128plus_next(void *state) {
344347
return r;
345348
}
346349

347-
static void* xorshift128plus_init(void) {
348-
return ecalloc(1, sizeof(xorshift128plus_state));
349-
}
350-
351350
static void xorshift128plus_seed(void *state, const zend_long seed) {
352351
xorshift128plus_state *s = (xorshift128plus_state*) state;
353352
uint64_t se = (uint64_t) seed;
@@ -391,7 +390,6 @@ const php_random_class_algo php_random_class_algo_xorshift128plus = {
391390
sizeof(uint64_t),
392391
sizeof(xorshift128plus_state),
393392
xorshift128plus_next,
394-
xorshift128plus_init,
395393
xorshift128plus_seed,
396394
xorshift128plus_serialize,
397395
xorshift128plus_unserialize
@@ -445,10 +443,6 @@ static uint64_t mt19937_next(void *state) {
445443
return ( s1 ^ (s1 >> 18) );
446444
}
447445

448-
static void* mt19937_init(void) {
449-
return ecalloc(1, sizeof(mt19937_state));
450-
}
451-
452446
static void mt19937_seed(void *state, const zend_long seed) {
453447
mt19937_state *s = (mt19937_state*) state;
454448

@@ -501,7 +495,6 @@ const php_random_class_algo php_random_class_algo_mt19937 = {
501495
sizeof(uint32_t),
502496
sizeof(mt19937_state),
503497
mt19937_next,
504-
mt19937_init,
505498
mt19937_seed,
506499
mt19937_serialize,
507500
mt19937_unserialize
@@ -526,7 +519,6 @@ const php_random_class_algo php_random_class_algo_secure = {
526519
secure_next,
527520
NULL,
528521
NULL,
529-
NULL,
530522
NULL
531523
};
532524
/* secure END */
@@ -605,16 +597,15 @@ PHP_METHOD(Random, __construct)
605597
RETURN_THROWS();
606598
}
607599

608-
if (algo->init) {
609-
random_class->state = algo->init();
610-
if (algo->seed) {
611-
if (seed_is_null) {
612-
seed = php_random_bytes_silent(&seed, sizeof(zend_long));
613-
}
614-
615-
algo->seed(random_class->state, seed);
600+
php_random_class_state_initialize(random_class);
601+
if (algo->seed) {
602+
if (seed_is_null) {
603+
seed = php_random_bytes_silent(&seed, sizeof(zend_long));
616604
}
605+
606+
algo->seed(random_class->state, seed);
617607
}
608+
618609
} else if (! zend_string_equals_literal(algo_str, PHP_RANDOM_CLASS_ALGO_USER_DEFINED)) {
619610
zend_argument_value_error(1, "must be a valid random number generator algorithm");
620611
RETURN_THROWS();
@@ -635,7 +626,7 @@ PHP_METHOD(Random, nextInt)
635626

636627
ret = php_random_class_next(random_class);
637628
if (random_class->algo) {
638-
if (random_class->algo->bytes > sizeof(zend_long)) {
629+
if (random_class->algo->generate_size > sizeof(zend_long)) {
639630
if (PG(random_class_ignore_generated_size_exceeded)) {
640631
ret = (zend_ulong) ret;
641632
} else {
@@ -695,7 +686,7 @@ PHP_METHOD(Random, getBytes)
695686

696687
while (generated_bytes <= size) {
697688
buf = php_random_class_next(random_class);
698-
if (random_class->algo && random_class->algo->bytes == sizeof(uint32_t)) {
689+
if (random_class->algo && random_class->algo->generate_size == sizeof(uint32_t)) {
699690
buf = (buf << 32) | php_random_class_next(random_class);
700691
}
701692
bytes = (uint8_t *) &buf;
@@ -809,12 +800,9 @@ PHP_METHOD(Random, __unserialize)
809800
zend_throw_exception(NULL, "Algorithm does not registered", 0);
810801
RETURN_THROWS();
811802
}
812-
813803
intern->algo = algo;
814804

815-
if (algo->init) {
816-
intern->state = algo->init();
817-
}
805+
php_random_class_state_initialize(intern);
818806

819807
if (!algo->serialize || !algo->unserialize) {
820808
zend_throw_exception(NULL, "Algorithm does not support serialization", 0);

0 commit comments

Comments
 (0)