Add php_random_int internal API
authorLeigh <leigh@php.net>
Wed, 19 Oct 2016 17:00:54 +0000 (18:00 +0100)
committerLeigh <leigh@php.net>
Thu, 20 Oct 2016 09:18:07 +0000 (10:18 +0100)
This is the internal API compliment to `php_random_bytes`

ext/standard/php_random.h
ext/standard/random.c

index c4034e7d8d4c83bdd4f19b88f53831ac82d4af5d..090e9d1dc464fd1a421330c197b696d337b0f5a4 100644 (file)
@@ -34,7 +34,13 @@ typedef struct {
 #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 int php_random_bytes(void *bytes, size_t size, zend_bool should_throw);
+PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, zend_bool should_throw);
 
 #ifdef ZTS
 # define RANDOM_G(v) ZEND_TSRMG(random_globals_id, php_random_globals *, v)
index c0e1abd2d2af88636b135dbaa4176414a2686b63..1161d76fe2b39ad04fbf694041163b9eeacd39a2 100644 (file)
@@ -81,7 +81,6 @@ PHP_MSHUTDOWN_FUNCTION(random)
 /* }}} */
 
 /* {{{ */
-
 PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
 {
 #if PHP_WIN32
@@ -214,37 +213,27 @@ PHP_FUNCTION(random_bytes)
 }
 /* }}} */
 
-/* {{{ proto int random_int(int min, int max)
-Return an arbitrary pseudo-random integer */
-PHP_FUNCTION(random_int)
+/* {{{ */
+PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, zend_bool should_throw)
 {
-       zend_long min;
-       zend_long max;
        zend_ulong umax;
-       zend_ulong result;
-
-       if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) {
-               return;
-       }
-
-       if (min > max) {
-               zend_throw_exception(zend_ce_error, "Minimum value must be less than or equal to the maximum value", 0);
-               return;
-       }
+       zend_ulong trial;
 
        if (min == max) {
-               RETURN_LONG(min);
+               *result = min;
+               return SUCCESS;
        }
 
        umax = max - min;
 
-       if (php_random_bytes_throw(&result, sizeof(result)) == FAILURE) {
-               return;
+       if (php_random_bytes(&trial, sizeof(trial), should_throw) == FAILURE) {
+               return FAILURE;
        }
 
        /* Special case where no modulus is required */
        if (umax == ZEND_ULONG_MAX) {
-               RETURN_LONG((zend_long)result);
+               *result = (zend_long)trial;
+               return SUCCESS;
        }
 
        /* Increment the max so the range is inclusive of max */
@@ -256,14 +245,40 @@ PHP_FUNCTION(random_int)
                zend_ulong limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
 
                /* Discard numbers over the limit to avoid modulo bias */
-               while (result > limit) {
-                       if (php_random_bytes_throw(&result, sizeof(result)) == FAILURE) {
-                               return;
+               while (trial > limit) {
+                       if (php_random_bytes(&trial, sizeof(trial), should_throw) == FAILURE) {
+                               return FAILURE;
                        }
                }
        }
 
-       RETURN_LONG((zend_long)((result % umax) + min));
+       *result = (zend_long)((trial % umax) + min);
+       return SUCCESS;
+}
+/* }}} */
+
+/* {{{ proto int random_int(int min, int max)
+Return an arbitrary pseudo-random integer */
+PHP_FUNCTION(random_int)
+{
+       zend_long min;
+       zend_long max;
+       zend_long result;
+
+       if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) {
+               return;
+       }
+
+       if (min > max) {
+               zend_throw_exception(zend_ce_error, "Minimum value must be less than or equal to the maximum value", 0);
+               return;
+       }
+
+       if (php_random_int_throw(min, max, &result) == FAILURE) {
+               return;
+       }
+
+       RETURN_LONG(result);
 }
 /* }}} */