@@ -63,7 +63,7 @@ static uint64_t range64(php_random_class *random_class, uint64_t umax) {
63
63
uint64_t result , limit ;
64
64
65
65
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 ) {
67
67
result = (result << 32 ) | php_random_class_next (random_class );
68
68
}
69
69
@@ -86,7 +86,7 @@ static uint64_t range64(php_random_class *random_class, uint64_t umax) {
86
86
/* Discard numbers over the limit to avoid modulo bias */
87
87
while (UNEXPECTED (result > limit )) {
88
88
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 ) {
90
90
result = (result << 32 ) | php_random_class_next (random_class );
91
91
}
92
92
}
@@ -275,7 +275,6 @@ void php_random_class_string_shuffle(php_random_class *random_class, char *str,
275
275
}
276
276
}
277
277
/* }}} */
278
-
279
278
static zend_object * php_random_class_new (zend_class_entry * ce ) {
280
279
php_random_class * random_class = zend_object_alloc (sizeof (php_random_class ), ce );
281
280
zend_object_std_init (& random_class -> std , ce );
@@ -285,6 +284,12 @@ static zend_object *php_random_class_new(zend_class_entry *ce) {
285
284
return & random_class -> std ;
286
285
}
287
286
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
+
288
293
static void php_random_class_free_obj (zend_object * object ) {
289
294
php_random_class * random_class = random_class_from_obj (object );
290
295
if (random_class -> state ) {
@@ -306,10 +311,8 @@ static zend_object *php_random_class_clone_obj(zend_object *object) {
306
311
307
312
if (old -> algo ) {
308
313
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 );
313
316
}
314
317
315
318
return new_obj ;
@@ -344,10 +347,6 @@ static uint64_t xorshift128plus_next(void *state) {
344
347
return r ;
345
348
}
346
349
347
- static void * xorshift128plus_init (void ) {
348
- return ecalloc (1 , sizeof (xorshift128plus_state ));
349
- }
350
-
351
350
static void xorshift128plus_seed (void * state , const zend_long seed ) {
352
351
xorshift128plus_state * s = (xorshift128plus_state * ) state ;
353
352
uint64_t se = (uint64_t ) seed ;
@@ -391,7 +390,6 @@ const php_random_class_algo php_random_class_algo_xorshift128plus = {
391
390
sizeof (uint64_t ),
392
391
sizeof (xorshift128plus_state ),
393
392
xorshift128plus_next ,
394
- xorshift128plus_init ,
395
393
xorshift128plus_seed ,
396
394
xorshift128plus_serialize ,
397
395
xorshift128plus_unserialize
@@ -445,10 +443,6 @@ static uint64_t mt19937_next(void *state) {
445
443
return ( s1 ^ (s1 >> 18 ) );
446
444
}
447
445
448
- static void * mt19937_init (void ) {
449
- return ecalloc (1 , sizeof (mt19937_state ));
450
- }
451
-
452
446
static void mt19937_seed (void * state , const zend_long seed ) {
453
447
mt19937_state * s = (mt19937_state * ) state ;
454
448
@@ -501,7 +495,6 @@ const php_random_class_algo php_random_class_algo_mt19937 = {
501
495
sizeof (uint32_t ),
502
496
sizeof (mt19937_state ),
503
497
mt19937_next ,
504
- mt19937_init ,
505
498
mt19937_seed ,
506
499
mt19937_serialize ,
507
500
mt19937_unserialize
@@ -526,7 +519,6 @@ const php_random_class_algo php_random_class_algo_secure = {
526
519
secure_next ,
527
520
NULL ,
528
521
NULL ,
529
- NULL ,
530
522
NULL
531
523
};
532
524
/* secure END */
@@ -605,16 +597,15 @@ PHP_METHOD(Random, __construct)
605
597
RETURN_THROWS ();
606
598
}
607
599
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 ));
616
604
}
605
+
606
+ algo -> seed (random_class -> state , seed );
617
607
}
608
+
618
609
} else if (! zend_string_equals_literal (algo_str , PHP_RANDOM_CLASS_ALGO_USER_DEFINED )) {
619
610
zend_argument_value_error (1 , "must be a valid random number generator algorithm" );
620
611
RETURN_THROWS ();
@@ -635,7 +626,7 @@ PHP_METHOD(Random, nextInt)
635
626
636
627
ret = php_random_class_next (random_class );
637
628
if (random_class -> algo ) {
638
- if (random_class -> algo -> bytes > sizeof (zend_long )) {
629
+ if (random_class -> algo -> generate_size > sizeof (zend_long )) {
639
630
if (PG (random_class_ignore_generated_size_exceeded )) {
640
631
ret = (zend_ulong ) ret ;
641
632
} else {
@@ -695,7 +686,7 @@ PHP_METHOD(Random, getBytes)
695
686
696
687
while (generated_bytes <= size ) {
697
688
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 )) {
699
690
buf = (buf << 32 ) | php_random_class_next (random_class );
700
691
}
701
692
bytes = (uint8_t * ) & buf ;
@@ -809,12 +800,9 @@ PHP_METHOD(Random, __unserialize)
809
800
zend_throw_exception (NULL , "Algorithm does not registered" , 0 );
810
801
RETURN_THROWS ();
811
802
}
812
-
813
803
intern -> algo = algo ;
814
804
815
- if (algo -> init ) {
816
- intern -> state = algo -> init ();
817
- }
805
+ php_random_class_state_initialize (intern );
818
806
819
807
if (!algo -> serialize || !algo -> unserialize ) {
820
808
zend_throw_exception (NULL , "Algorithm does not support serialization" , 0 );
0 commit comments