php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
random.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Sammy Kaye Powers <me@sammyk.me> |
14 | Go Kudo <zeriyoshi@php.net> |
15 | Tim Düsterhus <timwolla@php.net> |
16 +----------------------------------------------------------------------+
17*/
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <stdlib.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <math.h>
27
28#include "php.h"
29
31#include "Zend/zend_enum.h"
33
34#include "php_random.h"
35#include "php_random_csprng.h"
36#include "ext/standard/sha1.h"
37
38#ifdef HAVE_UNISTD_H
39# include <unistd.h>
40#endif
41
42#ifdef PHP_WIN32
43# include "win32/time.h"
44# include "win32/winutil.h"
45# include <process.h>
46#else
47# include <sys/time.h>
48#endif
49
50#ifdef HAVE_SYS_PARAM_H
51# include <sys/param.h>
52#endif
53
54#include "random_arginfo.h"
55
57
60
65
67
69
73
74static zend_object_handlers random_engine_mt19937_object_handlers;
75static zend_object_handlers random_engine_pcgoneseq128xslrr64_object_handlers;
76static zend_object_handlers random_engine_xoshiro256starstar_object_handlers;
77static zend_object_handlers random_engine_secure_object_handlers;
78static zend_object_handlers random_randomizer_object_handlers;
79
80PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t umax)
81{
82 const php_random_algo *algo = engine.algo;
83 void *state = engine.state;
84
85 uint32_t result;
86 size_t total_size;
87
88 result = 0;
89 total_size = 0;
90 do {
92 result = result | (((uint32_t) r.result) << (total_size * 8));
93 total_size += r.size;
94 if (EG(exception)) {
95 return 0;
96 }
97 } while (total_size < sizeof(uint32_t));
98
99 /* Special case where no modulus is required */
100 if (UNEXPECTED(umax == UINT32_MAX)) {
101 return result;
102 }
103
104 /* Increment the max so range is inclusive of max */
105 umax++;
106
107 /* Powers of two are not biased */
108 if ((umax & (umax - 1)) == 0) {
109 return result & (umax - 1);
110 }
111
112 /* Ceiling under which UINT32_MAX % max == 0 */
113 uint32_t limit = UINT32_MAX - (UINT32_MAX % umax) - 1;
114
115 /* Discard numbers over the limit to avoid modulo bias */
116 uint32_t count = 0;
117 while (UNEXPECTED(result > limit)) {
118 /* If the requirements cannot be met in a cycles, return fail */
120 zend_throw_error(random_ce_Random_BrokenRandomEngineError, "Failed to generate an acceptable random number in %d attempts", PHP_RANDOM_RANGE_ATTEMPTS);
121 return 0;
122 }
123
124 result = 0;
125 total_size = 0;
126 do {
128 result = result | (((uint32_t) r.result) << (total_size * 8));
129 total_size += r.size;
130 if (EG(exception)) {
131 return 0;
132 }
133 } while (total_size < sizeof(uint32_t));
134 }
135
136 return result % umax;
137}
138
140{
141 const php_random_algo *algo = engine.algo;
142 void *state = engine.state;
143
144 uint64_t result;
145 size_t total_size;
146
147 result = 0;
148 total_size = 0;
149 do {
151 result = result | (r.result << (total_size * 8));
152 total_size += r.size;
153 if (EG(exception)) {
154 return 0;
155 }
156 } while (total_size < sizeof(uint64_t));
157
158 /* Special case where no modulus is required */
159 if (UNEXPECTED(umax == UINT64_MAX)) {
160 return result;
161 }
162
163 /* Increment the max so range is inclusive of max */
164 umax++;
165
166 /* Powers of two are not biased */
167 if ((umax & (umax - 1)) == 0) {
168 return result & (umax - 1);
169 }
170
171 /* Ceiling under which UINT64_MAX % max == 0 */
172 uint64_t limit = UINT64_MAX - (UINT64_MAX % umax) - 1;
173
174 /* Discard numbers over the limit to avoid modulo bias */
175 uint32_t count = 0;
176 while (UNEXPECTED(result > limit)) {
177 /* If the requirements cannot be met in a cycles, return fail */
179 zend_throw_error(random_ce_Random_BrokenRandomEngineError, "Failed to generate an acceptable random number in %d attempts", PHP_RANDOM_RANGE_ATTEMPTS);
180 return 0;
181 }
182
183 result = 0;
184 total_size = 0;
185 do {
187 result = result | (r.result << (total_size * 8));
188 total_size += r.size;
189 if (EG(exception)) {
190 return 0;
191 }
192 } while (total_size < sizeof(uint64_t));
193 }
194
195 return result % umax;
196}
197
198static zend_object *php_random_engine_mt19937_new(zend_class_entry *ce)
199{
200 return &php_random_engine_common_init(ce, &random_engine_mt19937_object_handlers, &php_random_algo_mt19937)->std;
201}
202
203static zend_object *php_random_engine_pcgoneseq128xslrr64_new(zend_class_entry *ce)
204{
205 return &php_random_engine_common_init(ce, &random_engine_pcgoneseq128xslrr64_object_handlers, &php_random_algo_pcgoneseq128xslrr64)->std;
206}
207
208static zend_object *php_random_engine_xoshiro256starstar_new(zend_class_entry *ce)
209{
210 return &php_random_engine_common_init(ce, &random_engine_xoshiro256starstar_object_handlers, &php_random_algo_xoshiro256starstar)->std;
211}
212
213static zend_object *php_random_engine_secure_new(zend_class_entry *ce)
214{
215 return &php_random_engine_common_init(ce, &random_engine_secure_object_handlers, &php_random_algo_secure)->std;
216}
217
218static zend_object *php_random_randomizer_new(zend_class_entry *ce)
219{
220 php_random_randomizer *randomizer = zend_object_alloc(sizeof(php_random_randomizer), ce);
221
222 zend_object_std_init(&randomizer->std, ce);
223 object_properties_init(&randomizer->std, ce);
224
225 return &randomizer->std;
226}
227
228static void randomizer_free_obj(zend_object *object) {
229 php_random_randomizer *randomizer = php_random_randomizer_from_obj(object);
230
231 if (randomizer->is_userland_algo) {
232 php_random_status_free(randomizer->engine.state, false);
233 }
234
235 zend_object_std_dtor(&randomizer->std);
236}
237
239{
240 return algo->state_size > 0 ? pecalloc(1, algo->state_size, persistent) : NULL;
241}
242
243PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status)
244{
245 return memcpy(new_status, old_status, algo->state_size);
246}
247
249{
251}
252
254{
255 php_random_engine *engine = zend_object_alloc(sizeof(php_random_engine), ce);
256
257 zend_object_std_init(&engine->std, ce);
258 object_properties_init(&engine->std, ce);
259
260 engine->engine = (php_random_algo_with_state){
261 .algo = algo,
262 .state = php_random_status_alloc(algo, false)
263 };
264 engine->std.handlers = handlers;
265
266 return engine;
267}
268
270{
271 php_random_engine *engine = php_random_engine_from_obj(object);
272
273 php_random_status_free(engine->engine.state, false);
274 zend_object_std_dtor(object);
275}
276
278{
279 php_random_engine *old_engine = php_random_engine_from_obj(object);
280 php_random_engine *new_engine = php_random_engine_from_obj(old_engine->std.ce->create_object(old_engine->std.ce));
281
282 new_engine->engine.algo = old_engine->engine.algo;
283 if (old_engine->engine.state) {
284 new_engine->engine.state = php_random_status_copy(old_engine->engine.algo, old_engine->engine.state, new_engine->engine.state);
285 }
286
287 zend_objects_clone_members(&new_engine->std, &old_engine->std);
288
289 return &new_engine->std;
290}
291
292/* {{{ php_random_range */
294{
296
297 if (umax > UINT32_MAX) {
298 return (zend_long) (php_random_range64(engine, umax) + min);
299 }
300
301 return (zend_long) (php_random_range32(engine, umax) + min);
302}
303/* }}} */
304
305/* {{{ php_random_default_algo */
310/* }}} */
311
312/* {{{ php_random_default_status */
325/* }}} */
326
327/* this is read-only, so it's ok */
328ZEND_SET_ALIGNED(16, static const char hexconvtab[]) = "0123456789abcdef";
329
330/* {{{ php_random_bin2hex_le */
331/* stolen from standard/string.c */
332PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len)
333{
334 zend_string *str;
335 size_t i;
336
337 str = zend_string_safe_alloc(len, 2 * sizeof(char), 0, 0);
338
339 i = 0;
340#ifdef WORDS_BIGENDIAN
341 /* force little endian */
342 for (size_t h = len; 0 < h; h--) {
343 size_t j = h-1;
344#else
345 for (size_t j = 0; j < len; j++) {
346#endif
347 ZSTR_VAL(str)[i++] = hexconvtab[((unsigned char *) ptr)[j] >> 4];
348 ZSTR_VAL(str)[i++] = hexconvtab[((unsigned char *) ptr)[j] & 15];
349 }
350 ZSTR_VAL(str)[i] = '\0';
351
352 return str;
353}
354/* }}} */
355
356/* {{{ php_random_hex2bin_le */
357/* stolen from standard/string.c */
358PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
359{
360 size_t len = hexstr->len >> 1;
361 unsigned char *str = (unsigned char *) hexstr->val, c, l, d;
362 unsigned char *ptr = (unsigned char *) dest;
363 int is_letter, i = 0;
364
365#ifdef WORDS_BIGENDIAN
366 /* force little endian */
367 for (size_t h = len; 0 < h; h--) {
368 size_t j = h-1;
369#else
370 for (size_t j = 0; j < len; j++) {
371#endif
372 c = str[i++];
373 l = c & ~0x20;
374 is_letter = ((uint32_t) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(uint32_t) - 1);
375
376 /* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */
377 if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(uint32_t) - 1)) | is_letter)) {
378 d = (l - 0x10 - 0x27 * is_letter) << 4;
379 } else {
380 return false;
381 }
382 c = str[i++];
383 l = c & ~0x20;
384 is_letter = ((uint32_t) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(uint32_t) - 1);
385 if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(uint32_t) - 1)) | is_letter)) {
386 d |= l - 0x10 - 0x27 * is_letter;
387 } else {
388 return false;
389 }
390 ptr[j] = d;
391 }
392 return true;
393}
394/* }}} */
395
396/* {{{ php_combined_lcg */
398{
399 int32_t *state = RANDOM_G(combined_lcg);
400
402 uint64_t seed = 0;
403
404 if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
406 }
407
408 state[0] = seed & 0xffffffffU;
409 state[1] = seed >> 32;
411 }
412
413 /*
414 * combinedLCG() returns a pseudo random number in the range of (0, 1).
415 * The function combines two CGs with periods of
416 * 2^31 - 85 - 1 and 2^31 - 249 - 1. The period of this function
417 * is equal to the product of the two underlying periods, divided
418 * by factors shared by the underlying periods, i.e. 2.3 * 10^18.
419 *
420 * see: https://library.sciencemadness.org/lanl1_a/lib-www/numerica/f7-1.pdf
421 */
422#define PHP_COMBINED_LCG_MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m
423
424 int32_t q, z;
425
426 /* state[0] = (state[0] * 40014) % 2147483563; */
427 PHP_COMBINED_LCG_MODMULT(53668, 40014, 12211, 2147483563L, state[0]);
428 /* state[1] = (state[1] * 40692) % 2147483399; */
429 PHP_COMBINED_LCG_MODMULT(52774, 40692, 3791, 2147483399L, state[1]);
430
431 z = state[0] - state[1];
432 if (z < 1) {
433 z += 2147483562;
434 }
435
436 return ((uint64_t)z) * 4.656613e-10;
437}
438/* }}} */
439
440/* {{{ php_mt_srand */
441PHPAPI void php_mt_srand(uint32_t seed)
442{
444}
445/* }}} */
446
447/* {{{ php_mt_rand */
448PHPAPI uint32_t php_mt_rand(void)
449{
450 return (uint32_t) php_random_algo_mt19937.generate(php_random_default_status()).result;
451}
452/* }}} */
453
454/* {{{ php_mt_rand_range */
459/* }}} */
460
461/* {{{ php_mt_rand_common
462 * rand() allows min > max, mt_rand does not */
464{
466
467 if (s->mode == MT_RAND_MT19937) {
468 return php_mt_rand_range(min, max);
469 }
470
471 uint64_t r = php_random_algo_mt19937.generate(php_random_default_status()).result >> 1;
472
473 /* This is an inlined version of the RAND_RANGE_BADSCALING macro that does not invoke UB when encountering
474 * (max - min) > ZEND_LONG_MAX.
475 */
476 zend_ulong offset = (double) ( (double) max - min + 1.0) * (r / (PHP_MT_RAND_MAX + 1.0));
477
478 return (zend_long) (offset + min);
479}
480/* }}} */
481
482/* {{{ Returns a value from the combined linear congruential generator */
489/* }}} */
490
491/* {{{ Seeds Mersenne Twister random number generator */
493{
494 zend_long seed = 0;
495 bool seed_is_null = true;
498
501 Z_PARAM_LONG_OR_NULL(seed, seed_is_null)
504
505 switch (mode) {
506 case MT_RAND_PHP:
507 state->mode = MT_RAND_PHP;
508 zend_error(E_DEPRECATED, "The MT_RAND_PHP variant of Mt19937 is deprecated");
509 break;
510 default:
511 state->mode = MT_RAND_MT19937;
512 }
513
514 if (seed_is_null) {
516 } else {
517 php_random_mt19937_seed32(state, (uint64_t) seed);
518 }
519 RANDOM_G(mt19937_seeded) = true;
520}
521/* }}} */
522
523/* {{{ Returns a random number from Mersenne Twister */
525{
527 int argc = ZEND_NUM_ARGS();
528
529 if (argc == 0) {
530 /* genrand_int31 in mt19937ar.c performs a right shift */
531 RETURN_LONG(php_mt_rand() >> 1);
532 }
533
538
539 if (UNEXPECTED(max < min)) {
540 zend_argument_value_error(2, "must be greater than or equal to argument #1 ($min)");
542 }
543
545}
546/* }}} */
547
548/* {{{ Returns the maximum value a random number from Mersenne Twister can have */
550{
552
553 /*
554 * Melo: it could be 2^^32, but we only use 2^^31 to maintain
555 * compatibility with the previous php_rand
556 */
557 RETURN_LONG(PHP_MT_RAND_MAX); /* 2^^31 */
558}
559/* }}} */
560
561/* {{{ Returns a random number from Mersenne Twister */
563{
565 int argc = ZEND_NUM_ARGS();
566
567 if (argc == 0) {
568 /* genrand_int31 in mt19937ar.c performs a right shift */
569 RETURN_LONG(php_mt_rand() >> 1);
570 }
571
576
577 if (max < min) {
579 }
580
582}
583/* }}} */
584
585/* {{{ Return an arbitrary length of pseudo-random bytes as binary string */
587{
589 zend_string *bytes;
590
594
595 if (size < 1) {
596 zend_argument_value_error(1, "must be greater than 0");
598 }
599
600 bytes = zend_string_alloc(size, 0);
601
602 if (php_random_bytes_throw(ZSTR_VAL(bytes), size) == FAILURE) {
603 zend_string_release_ex(bytes, 0);
605 }
606
607 ZSTR_VAL(bytes)[size] = '\0';
608
609 RETURN_STR(bytes);
610}
611/* }}} */
612
613/* {{{ Return an arbitrary pseudo-random integer */
615{
617
622
623 if (min > max) {
624 zend_argument_value_error(1, "must be less than or equal to argument #2 ($max)");
626 }
627
628 if (php_random_int_throw(min, max, &result) == FAILURE) {
630 }
631
633}
634/* }}} */
635
636static inline void fallback_seed_add(PHP_SHA1_CTX *c, void *p, size_t l){
637 /* Wrapper around PHP_SHA1Update allowing to pass
638 * arbitrary pointers without (unsigned char*) casts
639 * everywhere.
640 */
641 PHP_SHA1Update(c, p, l);
642}
643
645{
646 /* Mix various values using SHA-1 as a PRF to obtain as
647 * much entropy as possible, hopefully generating an
648 * unpredictable and independent uint64_t. Nevertheless,
649 * the output of this function MUST NOT be treated as
650 * being cryptographically safe.
651 */
652 PHP_SHA1_CTX c;
653 struct timeval tv;
654 void *pointer;
655 pid_t pid;
656#ifdef ZTS
657 THREAD_T tid;
658#endif
659 char buf[64 + 1];
660
661 PHP_SHA1Init(&c);
662 if (!state->initialized) {
663 /* Current time. */
665 fallback_seed_add(&c, &tv, sizeof(tv));
666 /* Various PIDs. */
667 pid = getpid();
668 fallback_seed_add(&c, &pid, sizeof(pid));
669#ifndef PHP_WIN32
670 pid = getppid();
671 fallback_seed_add(&c, &pid, sizeof(pid));
672#endif
673#ifdef ZTS
674 tid = tsrm_thread_id();
675 fallback_seed_add(&c, &tid, sizeof(tid));
676#endif
677 /* Pointer values to benefit from ASLR. */
678 pointer = &state;
679 fallback_seed_add(&c, &pointer, sizeof(pointer));
680 pointer = &c;
681 fallback_seed_add(&c, &pointer, sizeof(pointer));
682 /* Updated time. */
684 fallback_seed_add(&c, &tv, sizeof(tv));
685 /* Hostname. */
686 memset(buf, 0, sizeof(buf));
687 if (gethostname(buf, sizeof(buf) - 1) == 0) {
688 fallback_seed_add(&c, buf, strlen(buf));
689 }
690 /* CSPRNG. */
691 if (php_random_bytes_silent(buf, 16) == SUCCESS) {
692 fallback_seed_add(&c, buf, 16);
693 }
694 /* Updated time. */
696 fallback_seed_add(&c, &tv, sizeof(tv));
697 } else {
698 /* Current time. */
700 fallback_seed_add(&c, &tv, sizeof(tv));
701 /* Previous state. */
702 fallback_seed_add(&c, state->seed, 20);
703 }
704 PHP_SHA1Final(state->seed, &c);
705 state->initialized = true;
706
707 uint64_t result = 0;
708
709 for (size_t i = 0; i < sizeof(result); i++) {
710 result = result | (((uint64_t)state->seed[i]) << (i * 8));
711 }
712
713 return result;
714}
715
720
721/* {{{ PHP_GINIT_FUNCTION */
722static PHP_GINIT_FUNCTION(random)
723{
724 random_globals->fallback_seed_state.initialized = false;
725}
726/* }}} */
727
728/* {{{ PHP_MINIT_FUNCTION */
730{
731 /* Random\Engine */
732 random_ce_Random_Engine = register_class_Random_Engine();
733
734 /* Random\CryptoSafeEngine */
735 random_ce_Random_CryptoSafeEngine = register_class_Random_CryptoSafeEngine(random_ce_Random_Engine);
736
737 /* Random\RandomError */
738 random_ce_Random_RandomError = register_class_Random_RandomError(zend_ce_error);
739
740 /* Random\BrokenRandomEngineError */
741 random_ce_Random_BrokenRandomEngineError = register_class_Random_BrokenRandomEngineError(random_ce_Random_RandomError);
742
743 /* Random\RandomException */
744 random_ce_Random_RandomException = register_class_Random_RandomException(zend_ce_exception);
745
746 /* Random\Engine\Mt19937 */
747 random_ce_Random_Engine_Mt19937 = register_class_Random_Engine_Mt19937(random_ce_Random_Engine);
748 random_ce_Random_Engine_Mt19937->create_object = php_random_engine_mt19937_new;
749 memcpy(&random_engine_mt19937_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
750 random_engine_mt19937_object_handlers.offset = XtOffsetOf(php_random_engine, std);
751 random_engine_mt19937_object_handlers.free_obj = php_random_engine_common_free_object;
752 random_engine_mt19937_object_handlers.clone_obj = php_random_engine_common_clone_object;
753
754 /* Random\Engine\PcgOnseq128XslRr64 */
755 random_ce_Random_Engine_PcgOneseq128XslRr64 = register_class_Random_Engine_PcgOneseq128XslRr64(random_ce_Random_Engine);
756 random_ce_Random_Engine_PcgOneseq128XslRr64->create_object = php_random_engine_pcgoneseq128xslrr64_new;
757 memcpy(&random_engine_pcgoneseq128xslrr64_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
758 random_engine_pcgoneseq128xslrr64_object_handlers.offset = XtOffsetOf(php_random_engine, std);
759 random_engine_pcgoneseq128xslrr64_object_handlers.free_obj = php_random_engine_common_free_object;
760 random_engine_pcgoneseq128xslrr64_object_handlers.clone_obj = php_random_engine_common_clone_object;
761
762 /* Random\Engine\Xoshiro256StarStar */
763 random_ce_Random_Engine_Xoshiro256StarStar = register_class_Random_Engine_Xoshiro256StarStar(random_ce_Random_Engine);
764 random_ce_Random_Engine_Xoshiro256StarStar->create_object = php_random_engine_xoshiro256starstar_new;
765 memcpy(&random_engine_xoshiro256starstar_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
766 random_engine_xoshiro256starstar_object_handlers.offset = XtOffsetOf(php_random_engine, std);
767 random_engine_xoshiro256starstar_object_handlers.free_obj = php_random_engine_common_free_object;
768 random_engine_xoshiro256starstar_object_handlers.clone_obj = php_random_engine_common_clone_object;
769
770 /* Random\Engine\Secure */
771 random_ce_Random_Engine_Secure = register_class_Random_Engine_Secure(random_ce_Random_CryptoSafeEngine);
772 random_ce_Random_Engine_Secure->create_object = php_random_engine_secure_new;
773 memcpy(&random_engine_secure_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
774 random_engine_secure_object_handlers.offset = XtOffsetOf(php_random_engine, std);
775 random_engine_secure_object_handlers.free_obj = php_random_engine_common_free_object;
776 random_engine_secure_object_handlers.clone_obj = NULL;
777
778 /* Random\Randomizer */
779 random_ce_Random_Randomizer = register_class_Random_Randomizer();
780 random_ce_Random_Randomizer->create_object = php_random_randomizer_new;
781 random_ce_Random_Randomizer->default_object_handlers = &random_randomizer_object_handlers;
782 memcpy(&random_randomizer_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
783 random_randomizer_object_handlers.offset = XtOffsetOf(php_random_randomizer, std);
784 random_randomizer_object_handlers.free_obj = randomizer_free_obj;
785 random_randomizer_object_handlers.clone_obj = NULL;
786
787 /* Random\IntervalBoundary */
788 random_ce_Random_IntervalBoundary = register_class_Random_IntervalBoundary();
789
790 register_random_symbols(module_number);
791
792 return SUCCESS;
793}
794/* }}} */
795
796/* {{{ PHP_MSHUTDOWN_FUNCTION */
798{
800
801 return SUCCESS;
802}
803/* }}} */
804
805/* {{{ PHP_RINIT_FUNCTION */
807{
809 RANDOM_G(mt19937_seeded) = false;
810
811 return SUCCESS;
812}
813/* }}} */
814
815/* {{{ random_module_entry */
818 "random", /* Extension name */
819 ext_functions, /* zend_function_entry */
820 PHP_MINIT(random), /* PHP_MINIT - Module initialization */
821 PHP_MSHUTDOWN(random), /* PHP_MSHUTDOWN - Module shutdown */
822 PHP_RINIT(random), /* PHP_RINIT - Request initialization */
823 NULL, /* PHP_RSHUTDOWN - Request shutdown */
824 NULL, /* PHP_MINFO - Module info */
825 PHP_VERSION, /* Version */
826 PHP_MODULE_GLOBALS(random), /* ZTS Module globals */
827 PHP_GINIT(random), /* PHP_GINIT - Global initialization */
828 NULL, /* PHP_GSHUTDOWN - Global shutdown */
829 NULL, /* Post deactivate */
831};
832/* }}} */
size_t len
Definition apprentice.c:174
bool exception
Definition assert.c:30
gettimeofday(bool $as_float=false)
count(Countable|array $value, int $mode=COUNT_NORMAL)
char s[4]
Definition cdf.c:77
PHPAPI void php_random_csprng_shutdown(void)
Definition csprng.c:267
DNS_STATUS status
Definition dns_win32.c:49
PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed)
PHPAPI const php_random_algo php_random_algo_mt19937
PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state)
PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64
PHPAPI const php_random_algo php_random_algo_secure
PHPAPI const php_random_algo php_random_algo_xoshiro256starstar
#define max(a, b)
Definition exif.c:60
new_type size
Definition ffi.c:4365
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
memset(ptr, 0, type->size)
ffi persistent
Definition ffi.c:3633
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
zend_long offset
char * mode
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
again j
#define PHP_GINIT
Definition php.h:397
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_RINIT
Definition php.h:394
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_GINIT_FUNCTION
Definition php.h:405
#define PHP_RINIT_FUNCTION
Definition php.h:402
#define PHP_MINIT
Definition php.h:392
#define PHPAPI
Definition php.h:71
#define PHP_MODULE_GLOBALS
Definition php.h:408
#define UINT32_MAX
zend_stack handlers
Definition php_output.h:139
#define min(a, b)
PHPAPI zend_class_entry * random_ce_Random_Engine
PHPAPI zend_object * php_random_engine_common_clone_object(zend_object *object)
Definition random.c:277
PHPAPI zend_class_entry * random_ce_Random_RandomError
PHPAPI zend_class_entry * random_ce_Random_Engine_PcgOneseq128XslRr64
PHPAPI zend_class_entry * random_ce_Random_Randomizer
bool mt19937_seeded
Definition php_random.h:199
php_random_fallback_seed_state fallback_seed_state
Definition php_random.h:200
struct _php_random_algo_with_state php_random_algo_with_state
PHPAPI zend_class_entry * random_ce_Random_Engine_Mt19937
php_random_status_state_mt19937 mt19937
Definition php_random.h:202
PHPAPI zend_class_entry * random_ce_Random_RandomException
int32_t combined_lcg[2]
Definition php_random.h:201
PHPAPI void php_random_engine_common_free_object(zend_object *object)
Definition random.c:269
struct _php_random_status_state_mt19937 php_random_status_state_mt19937
PHPAPI zend_class_entry * random_ce_Random_CryptoSafeEngine
PHPAPI zend_class_entry * random_ce_Random_IntervalBoundary
PHPAPI zend_class_entry * random_ce_Random_Engine_Xoshiro256StarStar
struct _php_random_fallback_seed_state php_random_fallback_seed_state
Definition php_random.h:40
struct _php_random_engine php_random_engine
PHPAPI zend_class_entry * random_ce_Random_Engine_Secure
#define PHP_MT_RAND_MAX
Definition php_random.h:50
@ MT_RAND_PHP
Definition php_random.h:54
@ MT_RAND_MT19937
Definition php_random.h:53
#define RANDOM_G(v)
struct _php_random_algo php_random_algo
#define PHP_RANDOM_RANGE_ATTEMPTS
Definition php_random.h:57
bool combined_lcg_seeded
Definition php_random.h:198
struct _php_random_randomizer php_random_randomizer
PHPAPI zend_class_entry * random_ce_Random_BrokenRandomEngineError
zend_module_entry random_module_entry
Definition random.c:816
struct _php_random_result php_random_result
PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t umax)
#define PHP_VERSION
Definition php_version.h:7
PHPAPI zend_string * php_random_bin2hex_le(const void *ptr, const size_t len)
Definition random.c:332
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
Definition random.c:358
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
Definition random.c:463
PHPAPI zend_object * php_random_engine_common_clone_object(zend_object *object)
Definition random.c:277
PHPAPI php_random_engine * php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo)
Definition random.c:253
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max)
Definition random.c:455
PHPAPI void * php_random_status_alloc(const php_random_algo *algo, const bool persistent)
Definition random.c:238
PHPAPI void php_mt_srand(uint32_t seed)
Definition random.c:441
PHPAPI uint64_t php_random_generate_fallback_seed(void)
Definition random.c:716
PHPAPI uint64_t php_random_generate_fallback_seed_ex(php_random_fallback_seed_state *state)
Definition random.c:644
PHPAPI void php_random_engine_common_free_object(zend_object *object)
Definition random.c:269
PHPAPI const php_random_algo * php_random_default_algo(void)
Definition random.c:306
#define PHP_COMBINED_LCG_MODMULT(a, b, c, m, s)
PHPAPI void * php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status)
Definition random.c:243
PHPAPI uint32_t php_mt_rand(void)
Definition random.c:448
PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max)
Definition random.c:293
PHPAPI double php_combined_lcg(void)
Definition random.c:397
PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t umax)
Definition random.c:139
PHPAPI void * php_random_default_status(void)
Definition random.c:313
PHPAPI void php_random_status_free(void *status, const bool persistent)
Definition random.c:248
mt_srand(?int $seed=null, int $mode=MT_RAND_MT19937)
mt_getrandmax()
mt_rand(int $min=UNKNOWN, int $max=UNKNOWN)
lcg_value()
random_bytes(int $length)
random_int(int $min, int $max)
rand(int $min=UNKNOWN, int $max=UNKNOWN)
struct timeval tv
Definition session.c:1280
p
Definition session.c:1105
PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX *context)
Definition sha1.c:215
PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX *context, const unsigned char *input, size_t inputLen)
Definition sha1.c:173
#define PHP_SHA1Init(ctx)
Definition sha1.h:28
const php_random_algo * algo
Definition php_random.h:97
php_random_result(* generate)(void *state)
Definition php_random.h:90
const size_t state_size
Definition php_random.h:89
zend_object std
Definition php_random.h:114
php_random_algo_with_state engine
Definition php_random.h:113
php_random_algo_with_state engine
Definition php_random.h:118
zend_object *(* create_object)(zend_class_entry *class_type)
Definition zend.h:195
zend_object_free_obj_t free_obj
zend_object_clone_obj_t clone_obj
zend_class_entry * ce
Definition zend_types.h:560
char val[1]
Definition zend_types.h:377
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_error(int type, const char *format,...)
Definition zend.c:1666
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_DOUBLE(d)
Definition zend_API.h:1038
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
Definition zend_API.h:268
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETURN_STR(s)
Definition zend_API.h:1039
#define Z_PARAM_LONG_OR_NULL(dest, is_null)
Definition zend_API.h:1899
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pecalloc(nmemb, size, persistent)
Definition zend_alloc.h:200
strlen(string $string)
zend_string_release_ex(func->internal_function.function_name, 0)
#define E_DEPRECATED
Definition zend_errors.h:37
ZEND_API zend_class_entry * zend_ce_exception
ZEND_API zend_class_entry * zend_ce_error
#define EG(v)
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
struct _zend_string zend_string
#define STANDARD_MODULE_HEADER
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES_EX
#define zend_get_std_object_handlers()
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
#define EXPECTED(condition)
#define XtOffsetOf(s_type, field)
#define ZEND_SET_ALIGNED(alignment, decl)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
@ FAILURE
Definition zend_types.h:61
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
bool result