Skip to content

Commit 2e6b742

Browse files
committed
1 parent 0dbedb3 commit 2e6b742

File tree

86 files changed

+4193
-1230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+4193
-1230
lines changed

EXTENSIONS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,12 @@ MAINTENANCE: Unknown
425425
STATUS: Working
426426
SINCE: 4.0.2
427427
-------------------------------------------------------------------------------
428+
EXTENSION: random
429+
PRIMARY MAINTAINER Go Kudo <[email protected]> (2022 - 2022)
430+
Tim Düsterhus <[email protected]> (2022 - 2022)
431+
MAINTENANCE: Maintained
432+
STATUS: Working
433+
-------------------------------------------------------------------------------
428434
EXTENSION: readline
429435
PRIMARY MAINTAINER: Unknown
430436
MAINTENANCE: Unknown

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ PHP NEWS
4343
- PDO_Firebird:
4444
. Fixed bug GH-8576 (Bad interpretation of length when char is UTF-8). (cmb)
4545

46+
- Random:
47+
. Add new random extension. (Go Kudo), This extension organizes and
48+
consolidates existing implementations related to random number generators.
49+
New, higher quality RNGs are available and scope issues are eliminated. (Go Kudo)
50+
4651
- Standard:
4752
. Fixed empty array returned by str_split on empty input. (Michael Vorisek)
4853
. Added ini_parse_quantity function to convert ini quantities shorthand

Zend/Optimizer/zend_func_infos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ static const func_info_t func_infos[] = {
416416
F1("posix_getrlimit", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
417417
#endif
418418
F1("pspell_suggest", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
419+
F1("random_bytes", MAY_BE_STRING),
419420
#if defined(HAVE_HISTORY_LIST)
420421
F1("readline_list_history", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
421422
#endif
@@ -651,7 +652,6 @@ static const func_info_t func_infos[] = {
651652
#endif
652653
F1("quoted_printable_decode", MAY_BE_STRING),
653654
F1("quoted_printable_encode", MAY_BE_STRING),
654-
F1("random_bytes", MAY_BE_STRING),
655655
F1("soundex", MAY_BE_STRING),
656656
F1("stream_context_create", MAY_BE_RESOURCE),
657657
F1("stream_context_get_params", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),

ext/dom/php_dom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "php.h"
2424
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
25-
#include "ext/standard/php_rand.h"
25+
#include "ext/random/php_random.h"
2626
#include "php_dom.h"
2727
#include "php_dom_arginfo.h"
2828
#include "dom_properties.h"

ext/gmp/gmp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
#include <gmp.h>
3131

3232
/* Needed for gmp_random() */
33-
#include "ext/standard/php_rand.h"
34-
#include "ext/standard/php_lcg.h"
33+
#include "ext/random/php_random.h"
3534

3635
#define GMP_ROUND_ZERO 0
3736
#define GMP_ROUND_PLUSINF 1

ext/random/CREDITS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
random
2+
Go Kudo, Tim Düsterhus, Guilliam Xavier, Christoph M. Becker, Jakub Zelenka, Bob Weinand, Máté Kocsis, and Original RNG implementators

ext/random/config.m4

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
dnl
2+
dnl Check for arc4random on BSD systems
3+
dnl
4+
AC_CHECK_DECLS([arc4random_buf])
5+
6+
dnl
7+
dnl Check for CCRandomGenerateBytes
8+
dnl header absent in previous macOs releases
9+
dnl
10+
AC_CHECK_HEADERS([CommonCrypto/CommonRandom.h])
11+
12+
dnl
13+
dnl Setup extension
14+
dnl
15+
PHP_NEW_EXTENSION(random,
16+
random.c \
17+
engine_combinedlcg.c \
18+
engine_mt19937.c \
19+
engine_pcgoneseq128xslrr64.c \
20+
engine_xoshiro256starstar.c \
21+
engine_secure.c \
22+
engine_user.c \
23+
randomizer.c,
24+
no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
25+
PHP_INSTALL_HEADERS([ext/random], [php_random.h])

ext/random/config.w32

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
2+
PHP_RANDOM="yes";
3+
ADD_SOURCES(configure_module_dirname, "engine_combinedlcg.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_xoshiro256starstar.c engine_secure.c engine_user.c randomizer.c", "random");
4+
PHP_INSTALL_HEADERS("ext/random", "php_random.h");

ext/random/engine_combinedlcg.c

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

0 commit comments

Comments
 (0)