Skip to content

Commit 14de122

Browse files
committed
Random Extension: First-implementation of RFC 2.0
1 parent db64aa3 commit 14de122

15 files changed

+1597
-0
lines changed

ext/random/config.m4

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dnl
2+
dnl Check for strtoull
3+
dnl
4+
AC_CHECK_FUNCS(strtoull)
5+
6+
dnl
7+
dnl Setup extension
8+
dnl
9+
PHP_NEW_EXTENSION(random, random.c , , , -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
10+
PHP_INSTALL_HEADERS([ext/random/])

ext/random/config.w32

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// vim:ft=javascript
2+
3+
EXTENSION("random", "random.c", false /* never shared */, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
4+
PHP_INSTALL_HEADERS("", "ext/random");

ext/random/php_random_module.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
| Author: Go Kudo <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef PHP_RANDOM_MODULE_H
18+
#define PHP_RANDOM_MODULE_H
19+
20+
extern zend_module_entry php_random_module_entry;
21+
#define phpext_random_ptr &php_random_module_entry
22+
23+
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_RandomNumberGenerator;
24+
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_XorShift128Plus;
25+
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_MT19937;
26+
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_Secure;
27+
extern PHPAPI zend_class_entry *php_random_ce_RandomInterface;
28+
extern PHPAPI zend_class_entry *php_random_ce_Random;
29+
30+
typedef struct _php_random_rng_algo {
31+
const size_t generate_size;
32+
const size_t state_size;
33+
uint64_t (*generate)(void *state);
34+
void (*seed)(void *state, const zend_long seed); /* nullable */
35+
int (*serialize)(void *state, HashTable *data); /* nullable */
36+
int (*unserialize)(void *state, HashTable *data); /* nullable */
37+
} php_random_rng_algo;
38+
39+
typedef struct _php_random_rng {
40+
const php_random_rng_algo *algo;
41+
void *state;
42+
zend_object std;
43+
} php_random_rng;
44+
45+
typedef struct _php_random {
46+
php_random_rng *rng;
47+
zend_object std;
48+
} php_random;
49+
50+
static inline php_random_rng *php_random_rng_from_obj(zend_object *obj) {
51+
return (php_random_rng *)((char *)(obj) - XtOffsetOf(php_random_rng, std));
52+
}
53+
54+
static inline php_random *php_random_from_obj(zend_object *obj) {
55+
return (php_random *)((char *)(obj) - XtOffsetOf(php_random, std));
56+
}
57+
58+
#define Z_RANDOM_RNG_P(zval) php_random_rng_from_obj(Z_OBJ_P(zval))
59+
60+
#define Z_RANDOM_P(zval) php_random_from_obj(Z_OBJ_P(zval))
61+
62+
PHPAPI uint64_t php_random_next(php_random *random, bool shift);
63+
PHPAPI zend_long php_random_range(php_random *random, zend_long min, zend_long max);
64+
PHPAPI void php_random_array_data_shuffle(php_random *random, zval *array);
65+
PHPAPI void php_random_string_shuffle(php_random *random, char *str, zend_long len);
66+
67+
#endif /* PHP_RANDOM_MODULE_H */

0 commit comments

Comments
 (0)