Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
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 @@ -31,8 +31,7 @@
#include "gmp_arginfo.h"

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

ZEND_DECLARE_MODULE_GLOBALS(gmp)
static ZEND_GINIT_FUNCTION(gmp);
Expand Down
10 changes: 10 additions & 0 deletions ext/random/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dnl
dnl Check for arc4random on BSD systems
dnl
AC_CHECK_DECLS([arc4random_buf])

dnl
dnl Setup extension
dnl
PHP_NEW_EXTENSION(random, random.c , , , -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
PHP_INSTALL_HEADERS([ext/random])
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";
PHP_INSTALL_HEADERS("ext/random", "php_random.h");
ADD_MAKEFILE_FRAGMENT();
116 changes: 116 additions & 0 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <[email protected]> |
| Zeev Suraski <[email protected]> |
| Sascha Schumann <[email protected]> |
| Pedro Melo <[email protected]> |
| Sterling Hughes <[email protected]> |
| Sammy Kaye Powers <[email protected]> |
| |
| Rand based on code from: Shawn Cokus <[email protected]> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_RANDOM_H
#define PHP_RANDOM_H

/* System Rand functions */
#ifndef RAND_MAX
# define RAND_MAX PHP_MT_RAND_MAX
#endif

#define PHP_RAND_MAX PHP_MT_RAND_MAX

/*
* A bit of tricky math here. We want to avoid using a modulus because
* that simply tosses the high-order bits and might skew the distribution
* of random values over the range. Instead we map the range directly.
*
* We need to map the range from 0...M evenly to the range a...b
* Let n = the random number and n' = the mapped random number
*
* Then we have: n' = a + n(b-a)/M
*
* We have a problem here in that only n==M will get mapped to b which
# means the chances of getting b is much much less than getting any of
# the other values in the range. We can fix this by increasing our range
# artificially and using:
#
# n' = a + n(b-a+1)/M
*
# Now we only have a problem if n==M which would cause us to produce a
# number of b+1 which would be bad. So we bump M up by one to make sure
# this will never happen, and the final algorithm looks like this:
#
# n' = a + n(b-a+1)/(M+1)
*
* -RL
*/
#define RAND_RANGE_BADSCALING(__n, __min, __max, __tmax) \
(__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))

#define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */

#define MT_RAND_MT19937 0
#define MT_RAND_PHP 1

#define MT_N (624)

extern zend_module_entry random_module_entry;
#define phpext_random_ptr &random_module_entry

PHP_MINIT_FUNCTION(random);
PHP_MSHUTDOWN_FUNCTION(random);

ZEND_BEGIN_MODULE_GLOBALS(random)
int32_t lcg_s1;
int32_t lcg_s2;
int lcg_seeded;
uint32_t mt_rand_state[MT_N+1]; /* state vector + 1 extra to not violate ANSI C */
uint32_t *mt_rand_next; /* next random value is computed from here */
int mt_rand_left; /* can *next++ this many times before reloading */
bool mt_rand_is_seeded; /* Whether mt_rand() has been seeded */
zend_long mt_rand_mode;
int random_fd;
ZEND_END_MODULE_GLOBALS(random)

#ifdef PHP_WIN32
# define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
#else
# define GENERATE_SEED() (((zend_long) (time(0) * getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
#endif

#define php_random_bytes_throw(b, s) php_random_bytes((b), (s), 1)
#define php_random_bytes_silent(b, s) php_random_bytes((b), (s), 0)

#define php_random_int_throw(min, max, result) \
php_random_int((min), (max), (result), 1)
#define php_random_int_silent(min, max, result) \
php_random_int((min), (max), (result), 0)

PHPAPI double php_combined_lcg(void);

PHPAPI void php_mt_srand(uint32_t seed);
PHPAPI uint32_t php_mt_rand(void);
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);

PHPAPI void php_srand(zend_long seed);
PHPAPI zend_long php_rand(void);

PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw);
PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);

#define RANDOM_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(random, v)

#endif
Loading