xref: /PHP-8.2/ext/random/engine_combinedlcg.c (revision 162e1dce)
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 php_random_result 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 (php_random_result){
57 		.size = sizeof(uint32_t),
58 		.result = (uint64_t) z,
59 	};
60 }
61 
range(php_random_status * status,zend_long min,zend_long max)62 static zend_long range(php_random_status *status, zend_long min, zend_long max)
63 {
64 	return php_random_range(&php_random_algo_combinedlcg, status, min, max);
65 }
66 
serialize(php_random_status * status,HashTable * data)67 static bool serialize(php_random_status *status, HashTable *data)
68 {
69 	php_random_status_state_combinedlcg *s = status->state;
70 	zval t;
71 
72 	for (uint32_t i = 0; i < 2; i++) {
73 		ZVAL_STR(&t, php_random_bin2hex_le(&s->state[i], sizeof(uint32_t)));
74 		zend_hash_next_index_insert(data, &t);
75 	}
76 
77 	return true;
78 }
79 
unserialize(php_random_status * status,HashTable * data)80 static bool unserialize(php_random_status *status, HashTable *data)
81 {
82 	php_random_status_state_combinedlcg *s = status->state;
83 	zval *t;
84 
85 	for (uint32_t i = 0; i < 2; i++) {
86 		t = zend_hash_index_find(data, i);
87 		if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint32_t))) {
88 			return false;
89 		}
90 		if (!php_random_hex2bin_le(Z_STR_P(t), &s->state[i])) {
91 			return false;
92 		}
93 	}
94 
95 	return true;
96 }
97 
98 const php_random_algo php_random_algo_combinedlcg = {
99 	sizeof(php_random_status_state_combinedlcg),
100 	seed,
101 	generate,
102 	range,
103 	serialize,
104 	unserialize
105 };
106 
107 /* {{{ php_random_combinedlcg_seed_default */
php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg * state)108 PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state)
109 {
110 	struct timeval tv;
111 
112 	if (gettimeofday(&tv, NULL) == 0) {
113 		state->state[0] = tv.tv_usec ^ (tv.tv_usec << 11);
114 	} else {
115 		state->state[0] = 1;
116 	}
117 
118 #ifdef ZTS
119 	state->state[1] = (zend_long) tsrm_thread_id();
120 #else
121 	state->state[1] = (zend_long) getpid();
122 #endif
123 
124 	/* Add entropy to s2 by calling gettimeofday() again */
125 	if (gettimeofday(&tv, NULL) == 0) {
126 		state->state[1] ^= (tv.tv_usec << 11);
127 	}
128 }
129 /* }}} */
130