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] Add Random Extension #7079

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
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 strtoull
dnl
AC_CHECK_FUNCS(strtoull)

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 @@
// vim:ft=javascript

EXTENSION("random", "random.c", false /* never shared */, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
PHP_INSTALL_HEADERS("", "ext/random");
67 changes: 67 additions & 0 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Author: Go Kudo <zeriyoshi@gmail.com> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_RANDOM_H_MODULE
#define PHP_RANDOM_H_MODULE

extern zend_module_entry random_module_entry;
#define phpext_random_ptr &random_module_entry

extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_RandomNumberGenerator;
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_XorShift128Plus;
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_MT19937;
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_Secure;
extern PHPAPI zend_class_entry *php_random_ce_RandomInterface;
extern PHPAPI zend_class_entry *php_random_ce_Random;

typedef struct _php_random_rng_algo {
const size_t generate_size;
const size_t state_size;
uint64_t (*generate)(void *state);
void (*seed)(void *state, const zend_long seed); /* nullable */
int (*serialize)(void *state, HashTable *data); /* nullable */
int (*unserialize)(void *state, HashTable *data); /* nullable */
} php_random_rng_algo;

typedef struct _php_random_rng {
const php_random_rng_algo *algo;
void *state;
zend_object std;
} php_random_rng;

typedef struct _php_random {
php_random_rng *rng;
zend_object std;
} php_random;

static inline php_random_rng *php_random_rng_from_obj(zend_object *obj) {
return (php_random_rng *)((char *)(obj) - XtOffsetOf(php_random_rng, std));
}

static inline php_random *php_random_from_obj(zend_object *obj) {
return (php_random *)((char *)(obj) - XtOffsetOf(php_random, std));
}

#define Z_RANDOM_RNG_P(zval) php_random_rng_from_obj(Z_OBJ_P(zval))

#define Z_RANDOM_P(zval) php_random_from_obj(Z_OBJ_P(zval))

PHPAPI uint64_t php_random_next(php_random *random, bool shift);
PHPAPI zend_long php_random_range(php_random *random, zend_long min, zend_long max);
PHPAPI void php_random_array_data_shuffle(php_random *random, zval *array);
PHPAPI void php_random_string_shuffle(php_random *random, char *str, zend_long len);

#endif /* PHP_RANDOM_H_MODULE */
Loading