xref: /PHP-8.3/ext/random/engine_combinedlcg.c (revision 3331832b)
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: Sascha Schumann <sascha@schumann.cx>                        |
14    |          Go Kudo <zeriyoshi@php.net>                                 |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 
22 #include "php.h"
23 #include "php_random.h"
24 
25 #include "Zend/zend_exceptions.h"
26 
27 /*
28  * combinedLCG() returns a pseudo random number in the range of (0, 1).
29  * The function combines two CGs with periods of
30  * 2^31 - 85 and 2^31 - 249. The period of this function
31  * is equal to the product of both primes.
32  */
33 #define MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m
34 
seed(php_random_status * status,uint64_t seed)35 static void seed(php_random_status *status, uint64_t seed)
36 {
37 	php_random_status_state_combinedlcg *s = status->state;
38 
39 	s->state[0] = seed & 0xffffffffU;
40 	s->state[1] = seed >> 32;
41 }
42 
generate(php_random_status * status)43 static uint64_t generate(php_random_status *status)
44 {
45 	php_random_status_state_combinedlcg *s = status->state;
46 	int32_t q, z;
47 
48 	MODMULT(53668, 40014, 12211, 2147483563L, s->state[0]);
49 	MODMULT(52774, 40692, 3791, 2147483399L, s->state[1]);
50 
51 	z = s->state[0] - s->state[1];
52 	if (z < 1) {
53 		z += 2147483562;
54 	}
55 
56 	return (uint64_t) z;
57 }
58 
range(php_random_status * status,zend_long min,zend_long max)59 static zend_long range(php_random_status *status, zend_long min, zend_long max)
60 {
61 	return php_random_range(&php_random_algo_combinedlcg, status, min, max);
62 }
63 
serialize(php_random_status * status,HashTable * data)64 static bool serialize(php_random_status *status, HashTable *data)
65 {
66 	php_random_status_state_combinedlcg *s = status->state;
67 	zval t;
68 
69 	for (uint32_t i = 0; i < 2; i++) {
70 		ZVAL_STR(&t, php_random_bin2hex_le(&s->state[i], sizeof(uint32_t)));
71 		zend_hash_next_index_insert(data, &t);
72 	}
73 
74 	return true;
75 }
76 
unserialize(php_random_status * status,HashTable * data)77 static bool unserialize(php_random_status *status, HashTable *data)
78 {
79 	php_random_status_state_combinedlcg *s = status->state;
80 	zval *t;
81 
82 	for (uint32_t i = 0; i < 2; i++) {
83 		t = zend_hash_index_find(data, i);
84 		if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint32_t))) {
85 			return false;
86 		}
87 		if (!php_random_hex2bin_le(Z_STR_P(t), &s->state[i])) {
88 			return false;
89 		}
90 	}
91 
92 	return true;
93 }
94 
95 const php_random_algo php_random_algo_combinedlcg = {
96 	sizeof(uint32_t),
97 	sizeof(php_random_status_state_combinedlcg),
98 	seed,
99 	generate,
100 	range,
101 	serialize,
102 	unserialize
103 };
104 
105 /* {{{ php_random_combinedlcg_seed_default */
php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg * state)106 PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state)
107 {
108 	struct timeval tv;
109 
110 	if (gettimeofday(&tv, NULL) == 0) {
111 		state->state[0] = tv.tv_usec ^ (tv.tv_usec << 11);
112 	} else {
113 		state->state[0] = 1;
114 	}
115 
116 #ifdef ZTS
117 	state->state[1] = (zend_long) tsrm_thread_id();
118 #else
119 	state->state[1] = (zend_long) getpid();
120 #endif
121 
122 	/* Add entropy to s2 by calling gettimeofday() again */
123 	if (gettimeofday(&tv, NULL) == 0) {
124 		state->state[1] ^= (tv.tv_usec << 11);
125 	}
126 }
127 /* }}} */
128