Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Random Extension 5.x + Random Extension Improvement #8094

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions EXTENSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ MAINTENANCE: Unknown
STATUS: Working
SINCE: 4.0.2
-------------------------------------------------------------------------------
EXTENSION: random
PRIMARY MAINTAINER Go Kudo <zeriyoshi@gmail.com> (2022 - 2022)
Tim Düsterhus <timwolla@php.net> (2022 - 2022)
MAINTENANCE: Maintained
STATUS: Working
-------------------------------------------------------------------------------
EXTENSION: readline
PRIMARY MAINTAINER: Unknown
MAINTENANCE: Unknown
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ PHP NEWS
- PDO_Firebird:
. Fixed bug GH-8576 (Bad interpretation of length when char is UTF-8). (cmb)

- Random:
. Add new random extension. (Go Kudo), This extension organizes and
consolidates existing implementations related to random number generators.
New, higher quality RNGs are available and scope issues are eliminated. (Go Kudo)

- Standard:
. Fixed empty array returned by str_split on empty input. (Michael Vorisek)
. Added ini_parse_quantity function to convert ini quantities shorthand
Expand Down
2 changes: 1 addition & 1 deletion Zend/Optimizer/zend_func_infos.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ static const func_info_t func_infos[] = {
F1("posix_getrlimit", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
#endif
F1("pspell_suggest", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
F1("random_bytes", MAY_BE_STRING),
#if defined(HAVE_HISTORY_LIST)
F1("readline_list_history", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
#endif
Expand Down Expand Up @@ -651,7 +652,6 @@ static const func_info_t func_infos[] = {
#endif
F1("quoted_printable_decode", MAY_BE_STRING),
F1("quoted_printable_encode", MAY_BE_STRING),
F1("random_bytes", MAY_BE_STRING),
F1("soundex", MAY_BE_STRING),
F1("stream_context_create", MAY_BE_RESOURCE),
F1("stream_context_get_params", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "ext/standard/php_rand.h"
#include "ext/random/php_random.h"
#include "php_dom.h"
#include "php_dom_arginfo.h"
#include "dom_properties.h"
Expand Down
3 changes: 1 addition & 2 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
#include <gmp.h>

/* Needed for gmp_random() */
#include "ext/standard/php_rand.h"
#include "ext/standard/php_lcg.h"
#include "ext/random/php_random.h"

#define GMP_ROUND_ZERO 0
#define GMP_ROUND_PLUSINF 1
Expand Down
2 changes: 2 additions & 0 deletions ext/random/CREDITS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
random
Go Kudo, Tim Düsterhus, Guilliam Xavier, Christoph M. Becker, Jakub Zelenka, Bob Weinand, Máté Kocsis, and Original RNG implementators
25 changes: 25 additions & 0 deletions ext/random/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
dnl
dnl Check for arc4random on BSD systems
dnl
AC_CHECK_DECLS([arc4random_buf])

dnl
dnl Check for CCRandomGenerateBytes
dnl header absent in previous macOs releases
dnl
AC_CHECK_HEADERS([CommonCrypto/CommonRandom.h])

dnl
dnl Setup extension
dnl
PHP_NEW_EXTENSION(random,
random.c \
engine_combinedlcg.c \
engine_mt19937.c \
engine_pcgoneseq128xslrr64.c \
engine_xoshiro256starstar.c \
engine_secure.c \
engine_user.c \
randomizer.c,
no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
PHP_INSTALL_HEADERS([ext/random], [php_random.h])
4 changes: 4 additions & 0 deletions ext/random/config.w32
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
PHP_RANDOM="yes";
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");
PHP_INSTALL_HEADERS("ext/random", "php_random.h");
128 changes: 128 additions & 0 deletions ext/random/engine_combinedlcg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Sascha Schumann <sascha@schumann.cx> |
| Go Kudo <g-kudo@colopl.co.jp> |
+----------------------------------------------------------------------+
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "php.h"
#include "php_random.h"

#include "ext/spl/spl_exceptions.h"
#include "Zend/zend_exceptions.h"

/*
* combinedLCG() returns a pseudo random number in the range of (0, 1).
* The function combines two CGs with periods of
* 2^31 - 85 and 2^31 - 249. The period of this function
* is equal to the product of both primes.
*/
#define MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m

static void seed(php_random_status *status, uint64_t seed)
{
php_random_status_state_combinedlcg *s = status->state;

s->state[0] = seed & 0xffffffffU;
s->state[1] = seed >> 32;
}

static uint64_t generate(php_random_status *status)
{
php_random_status_state_combinedlcg *s = status->state;
int32_t q, z;

MODMULT(53668, 40014, 12211, 2147483563L, s->state[0]);
MODMULT(52774, 40692, 3791, 2147483399L, s->state[1]);

z = s->state[0] - s->state[1];
if (z < 1) {
z += 2147483562;
}

return (uint64_t) z;
}

static zend_long range(php_random_status *status, zend_long min, zend_long max)
{
return php_random_range(&php_random_algo_combinedlcg, status, min, max);
}

static bool serialize(php_random_status *status, HashTable *data)
{
php_random_status_state_combinedlcg *s = status->state;
zval t;

for (uint32_t i = 0; i < 2; i++) {
ZVAL_STR(&t, php_random_bin2hex_le(&s->state[i], sizeof(uint32_t)));
zend_hash_next_index_insert(data, &t);
}

return true;
}

static bool unserialize(php_random_status *status, HashTable *data)
{
php_random_status_state_combinedlcg *s = status->state;
zval *t;

for (uint32_t i = 0; i < 2; i++) {
t = zend_hash_index_find(data, i);
if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint32_t))) {
return false;
}
if (!php_random_hex2bin_le(Z_STR_P(t), &s->state[i])) {
return false;
}
}

return true;
}

const php_random_algo php_random_algo_combinedlcg = {
sizeof(uint32_t),
sizeof(php_random_status_state_combinedlcg),
seed,
generate,
range,
serialize,
unserialize
};

/* {{{ php_random_combinedlcg_seed_default */
PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state)
{
struct timeval tv;

if (gettimeofday(&tv, NULL) == 0) {
state->state[0] = tv.tv_usec ^ (tv.tv_usec << 11);
} else {
state->state[0] = 1;
}

#ifdef ZTS
state->state[1] = (zend_long) tsrm_thread_id();
#else
state->state[1] = (zend_long) getpid();
#endif

/* Add entropy to s2 by calling gettimeofday() again */
if (gettimeofday(&tv, NULL) == 0) {
state->state[1] ^= (tv.tv_usec << 11);
}
}
/* }}} */
Loading