Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/random/php_random.h
Line
Count
Source (jump to first uncovered line)
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
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
14
   |          Zeev Suraski <zeev@php.net>                                 |
15
   |          Sascha Schumann <sascha@schumann.cx>                        |
16
   |          Pedro Melo <melo@ip.pt>                                     |
17
   |          Sterling Hughes <sterling@php.net>                          |
18
   |          Sammy Kaye Powers <me@sammyk.me>                            |
19
   |          Go Kudo <zeriyoshi@php.net>                                 |
20
   |                                                                      |
21
   | Based on code from: Richard J. Wagner <rjwagner@writeme.com>         |
22
   |                     Makoto Matsumoto <matumoto@math.keio.ac.jp>      |
23
   |                     Takuji Nishimura                                 |
24
   |                     Shawn Cokus <Cokus@math.washington.edu>          |
25
   |                     David Blackman                                   |
26
   |                     Sebastiano Vigna <vigna@acm.org>                 |
27
   |                     Melissa O'Neill <oneill@pcg-random.org>          |
28
   +----------------------------------------------------------------------+
29
*/
30
31
#ifndef PHP_RANDOM_H
32
# define PHP_RANDOM_H
33
34
# include "php.h"
35
# include "php_random_csprng.h"
36
# include "php_random_uint128.h"
37
38
PHPAPI double php_combined_lcg(void);
39
40
typedef struct _php_random_fallback_seed_state php_random_fallback_seed_state;
41
42
PHPAPI uint64_t php_random_generate_fallback_seed(void);
43
PHPAPI uint64_t php_random_generate_fallback_seed_ex(php_random_fallback_seed_state *state);
44
45
static inline zend_long GENERATE_SEED(void)
46
0
{
47
0
  return (zend_long)php_random_generate_fallback_seed();
48
0
}
Unexecuted instantiation: exif.c:GENERATE_SEED
Unexecuted instantiation: csprng.c:GENERATE_SEED
Unexecuted instantiation: engine_mt19937.c:GENERATE_SEED
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:GENERATE_SEED
Unexecuted instantiation: engine_secure.c:GENERATE_SEED
Unexecuted instantiation: engine_user.c:GENERATE_SEED
Unexecuted instantiation: engine_xoshiro256starstar.c:GENERATE_SEED
Unexecuted instantiation: gammasection.c:GENERATE_SEED
Unexecuted instantiation: random.c:GENERATE_SEED
Unexecuted instantiation: randomizer.c:GENERATE_SEED
Unexecuted instantiation: zend_utils.c:GENERATE_SEED
Unexecuted instantiation: spl_directory.c:GENERATE_SEED
Unexecuted instantiation: spl_observer.c:GENERATE_SEED
Unexecuted instantiation: array.c:GENERATE_SEED
Unexecuted instantiation: basic_functions.c:GENERATE_SEED
Unexecuted instantiation: exec.c:GENERATE_SEED
Unexecuted instantiation: file.c:GENERATE_SEED
Unexecuted instantiation: filters.c:GENERATE_SEED
Unexecuted instantiation: ftp_fopen_wrapper.c:GENERATE_SEED
Unexecuted instantiation: head.c:GENERATE_SEED
Unexecuted instantiation: html.c:GENERATE_SEED
Unexecuted instantiation: http_fopen_wrapper.c:GENERATE_SEED
Unexecuted instantiation: link.c:GENERATE_SEED
Unexecuted instantiation: mail.c:GENERATE_SEED
Unexecuted instantiation: php_fopen_wrapper.c:GENERATE_SEED
Unexecuted instantiation: streamsfuncs.c:GENERATE_SEED
Unexecuted instantiation: string.c:GENERATE_SEED
Unexecuted instantiation: strnatcmp.c:GENERATE_SEED
Unexecuted instantiation: uniqid.c:GENERATE_SEED
Unexecuted instantiation: url_scanner_ex.c:GENERATE_SEED
Unexecuted instantiation: var.c:GENERATE_SEED
Unexecuted instantiation: fopen_wrappers.c:GENERATE_SEED
Unexecuted instantiation: main.c:GENERATE_SEED
Unexecuted instantiation: php_open_temporary_file.c:GENERATE_SEED
Unexecuted instantiation: php_variables.c:GENERATE_SEED
Unexecuted instantiation: streams.c:GENERATE_SEED
Unexecuted instantiation: internal_functions_cli.c:GENERATE_SEED
49
50
0
# define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
51
52
enum php_random_mt19937_mode {
53
  MT_RAND_MT19937 = 0,
54
  MT_RAND_PHP = 1,
55
};
56
57
0
#define PHP_RANDOM_RANGE_ATTEMPTS (50)
58
59
PHPAPI void php_mt_srand(uint32_t seed);
60
PHPAPI uint32_t php_mt_rand(void);
61
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
62
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
63
64
typedef struct _php_random_status_state_mt19937 {
65
  uint32_t count;
66
  enum php_random_mt19937_mode mode;
67
  uint32_t state[624];
68
} php_random_status_state_mt19937;
69
70
typedef struct _php_random_status_state_pcgoneseq128xslrr64 {
71
  php_random_uint128_t state;
72
} php_random_status_state_pcgoneseq128xslrr64;
73
74
typedef struct _php_random_status_state_xoshiro256starstar {
75
  uint64_t state[4];
76
} php_random_status_state_xoshiro256starstar;
77
78
typedef struct _php_random_status_state_user {
79
  zend_object *object;
80
  zend_function *generate_method;
81
} php_random_status_state_user;
82
83
typedef struct _php_random_result {
84
  uint64_t result;
85
  size_t size;
86
} php_random_result;
87
88
typedef struct _php_random_algo {
89
  const size_t state_size;
90
  php_random_result (*generate)(void *state);
91
  zend_long (*range)(void *state, zend_long min, zend_long max);
92
  bool (*serialize)(void *state, HashTable *data);
93
  bool (*unserialize)(void *state, HashTable *data);
94
} php_random_algo;
95
96
typedef struct _php_random_algo_with_state {
97
  const php_random_algo *algo;
98
  void *state;
99
} php_random_algo_with_state;
100
101
typedef struct _php_random_fallback_seed_state {
102
  bool initialized;
103
  unsigned char seed[20];
104
} php_random_fallback_seed_state;
105
106
extern PHPAPI const php_random_algo php_random_algo_mt19937;
107
extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
108
extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
109
extern PHPAPI const php_random_algo php_random_algo_secure;
110
extern PHPAPI const php_random_algo php_random_algo_user;
111
112
typedef struct _php_random_engine {
113
  php_random_algo_with_state engine;
114
  zend_object std;
115
} php_random_engine;
116
117
typedef struct _php_random_randomizer {
118
  php_random_algo_with_state engine;
119
  bool is_userland_algo;
120
  zend_object std;
121
} php_random_randomizer;
122
123
extern PHPAPI zend_class_entry *random_ce_Random_Engine;
124
extern PHPAPI zend_class_entry *random_ce_Random_CryptoSafeEngine;
125
126
extern PHPAPI zend_class_entry *random_ce_Random_RandomError;
127
extern PHPAPI zend_class_entry *random_ce_Random_BrokenRandomEngineError;
128
extern PHPAPI zend_class_entry *random_ce_Random_RandomException;
129
130
extern PHPAPI zend_class_entry *random_ce_Random_Engine_PcgOneseq128XslRr64;
131
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
132
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Xoshiro256StarStar;
133
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Secure;
134
135
extern PHPAPI zend_class_entry *random_ce_Random_Randomizer;
136
137
extern PHPAPI zend_class_entry *random_ce_Random_IntervalBoundary;
138
139
77
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
140
77
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
141
77
}
Unexecuted instantiation: exif.c:php_random_engine_from_obj
Unexecuted instantiation: csprng.c:php_random_engine_from_obj
engine_mt19937.c:php_random_engine_from_obj
Line
Count
Source
139
13
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
140
13
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
141
13
}
engine_pcgoneseq128xslrr64.c:php_random_engine_from_obj
Line
Count
Source
139
5
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
140
5
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
141
5
}
Unexecuted instantiation: engine_secure.c:php_random_engine_from_obj
Unexecuted instantiation: engine_user.c:php_random_engine_from_obj
engine_xoshiro256starstar.c:php_random_engine_from_obj
Line
Count
Source
139
5
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
140
5
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
141
5
}
Unexecuted instantiation: gammasection.c:php_random_engine_from_obj
random.c:php_random_engine_from_obj
Line
Count
Source
139
49
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
140
49
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
141
49
}
randomizer.c:php_random_engine_from_obj
Line
Count
Source
139
5
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
140
5
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
141
5
}
Unexecuted instantiation: zend_utils.c:php_random_engine_from_obj
Unexecuted instantiation: spl_directory.c:php_random_engine_from_obj
Unexecuted instantiation: spl_observer.c:php_random_engine_from_obj
Unexecuted instantiation: array.c:php_random_engine_from_obj
Unexecuted instantiation: basic_functions.c:php_random_engine_from_obj
Unexecuted instantiation: exec.c:php_random_engine_from_obj
Unexecuted instantiation: file.c:php_random_engine_from_obj
Unexecuted instantiation: filters.c:php_random_engine_from_obj
Unexecuted instantiation: ftp_fopen_wrapper.c:php_random_engine_from_obj
Unexecuted instantiation: head.c:php_random_engine_from_obj
Unexecuted instantiation: html.c:php_random_engine_from_obj
Unexecuted instantiation: http_fopen_wrapper.c:php_random_engine_from_obj
Unexecuted instantiation: link.c:php_random_engine_from_obj
Unexecuted instantiation: mail.c:php_random_engine_from_obj
Unexecuted instantiation: php_fopen_wrapper.c:php_random_engine_from_obj
Unexecuted instantiation: streamsfuncs.c:php_random_engine_from_obj
Unexecuted instantiation: string.c:php_random_engine_from_obj
Unexecuted instantiation: strnatcmp.c:php_random_engine_from_obj
Unexecuted instantiation: uniqid.c:php_random_engine_from_obj
Unexecuted instantiation: url_scanner_ex.c:php_random_engine_from_obj
Unexecuted instantiation: var.c:php_random_engine_from_obj
Unexecuted instantiation: fopen_wrappers.c:php_random_engine_from_obj
Unexecuted instantiation: main.c:php_random_engine_from_obj
Unexecuted instantiation: php_open_temporary_file.c:php_random_engine_from_obj
Unexecuted instantiation: php_variables.c:php_random_engine_from_obj
Unexecuted instantiation: streams.c:php_random_engine_from_obj
Unexecuted instantiation: internal_functions_cli.c:php_random_engine_from_obj
142
143
23
static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
144
23
  return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
145
23
}
Unexecuted instantiation: exif.c:php_random_randomizer_from_obj
Unexecuted instantiation: csprng.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_mt19937.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_secure.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_user.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_xoshiro256starstar.c:php_random_randomizer_from_obj
Unexecuted instantiation: gammasection.c:php_random_randomizer_from_obj
random.c:php_random_randomizer_from_obj
Line
Count
Source
143
16
static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
144
16
  return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
145
16
}
randomizer.c:php_random_randomizer_from_obj
Line
Count
Source
143
7
static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
144
7
  return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
145
7
}
Unexecuted instantiation: zend_utils.c:php_random_randomizer_from_obj
Unexecuted instantiation: spl_directory.c:php_random_randomizer_from_obj
Unexecuted instantiation: spl_observer.c:php_random_randomizer_from_obj
Unexecuted instantiation: array.c:php_random_randomizer_from_obj
Unexecuted instantiation: basic_functions.c:php_random_randomizer_from_obj
Unexecuted instantiation: exec.c:php_random_randomizer_from_obj
Unexecuted instantiation: file.c:php_random_randomizer_from_obj
Unexecuted instantiation: filters.c:php_random_randomizer_from_obj
Unexecuted instantiation: ftp_fopen_wrapper.c:php_random_randomizer_from_obj
Unexecuted instantiation: head.c:php_random_randomizer_from_obj
Unexecuted instantiation: html.c:php_random_randomizer_from_obj
Unexecuted instantiation: http_fopen_wrapper.c:php_random_randomizer_from_obj
Unexecuted instantiation: link.c:php_random_randomizer_from_obj
Unexecuted instantiation: mail.c:php_random_randomizer_from_obj
Unexecuted instantiation: php_fopen_wrapper.c:php_random_randomizer_from_obj
Unexecuted instantiation: streamsfuncs.c:php_random_randomizer_from_obj
Unexecuted instantiation: string.c:php_random_randomizer_from_obj
Unexecuted instantiation: strnatcmp.c:php_random_randomizer_from_obj
Unexecuted instantiation: uniqid.c:php_random_randomizer_from_obj
Unexecuted instantiation: url_scanner_ex.c:php_random_randomizer_from_obj
Unexecuted instantiation: var.c:php_random_randomizer_from_obj
Unexecuted instantiation: fopen_wrappers.c:php_random_randomizer_from_obj
Unexecuted instantiation: main.c:php_random_randomizer_from_obj
Unexecuted instantiation: php_open_temporary_file.c:php_random_randomizer_from_obj
Unexecuted instantiation: php_variables.c:php_random_randomizer_from_obj
Unexecuted instantiation: streams.c:php_random_randomizer_from_obj
Unexecuted instantiation: internal_functions_cli.c:php_random_randomizer_from_obj
146
147
23
# define Z_RANDOM_ENGINE_P(zval) php_random_engine_from_obj(Z_OBJ_P(zval))
148
149
7
# define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
150
151
PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
152
PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status);
153
PHPAPI void php_random_status_free(void *status, const bool persistent);
154
PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo);
155
PHPAPI void php_random_engine_common_free_object(zend_object *object);
156
PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
157
PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t umax);
158
PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t umax);
159
PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max);
160
PHPAPI const php_random_algo *php_random_default_algo(void);
161
PHPAPI void *php_random_default_status(void);
162
163
static inline php_random_algo_with_state php_random_default_engine(void)
164
14
{
165
14
  php_random_algo_with_state raws;
166
14
  raws.algo = php_random_default_algo();
167
14
  raws.state = php_random_default_status();
168
14
  return raws;
169
14
}
Unexecuted instantiation: exif.c:php_random_default_engine
Unexecuted instantiation: csprng.c:php_random_default_engine
Unexecuted instantiation: engine_mt19937.c:php_random_default_engine
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:php_random_default_engine
Unexecuted instantiation: engine_secure.c:php_random_default_engine
Unexecuted instantiation: engine_user.c:php_random_default_engine
Unexecuted instantiation: engine_xoshiro256starstar.c:php_random_default_engine
Unexecuted instantiation: gammasection.c:php_random_default_engine
Unexecuted instantiation: random.c:php_random_default_engine
Unexecuted instantiation: randomizer.c:php_random_default_engine
Unexecuted instantiation: zend_utils.c:php_random_default_engine
Unexecuted instantiation: spl_directory.c:php_random_default_engine
Unexecuted instantiation: spl_observer.c:php_random_default_engine
Unexecuted instantiation: array.c:php_random_default_engine
Unexecuted instantiation: basic_functions.c:php_random_default_engine
Unexecuted instantiation: exec.c:php_random_default_engine
Unexecuted instantiation: file.c:php_random_default_engine
Unexecuted instantiation: filters.c:php_random_default_engine
Unexecuted instantiation: ftp_fopen_wrapper.c:php_random_default_engine
Unexecuted instantiation: head.c:php_random_default_engine
Unexecuted instantiation: html.c:php_random_default_engine
Unexecuted instantiation: http_fopen_wrapper.c:php_random_default_engine
Unexecuted instantiation: link.c:php_random_default_engine
Unexecuted instantiation: mail.c:php_random_default_engine
Unexecuted instantiation: php_fopen_wrapper.c:php_random_default_engine
Unexecuted instantiation: streamsfuncs.c:php_random_default_engine
string.c:php_random_default_engine
Line
Count
Source
164
14
{
165
14
  php_random_algo_with_state raws;
166
14
  raws.algo = php_random_default_algo();
167
14
  raws.state = php_random_default_status();
168
14
  return raws;
169
14
}
Unexecuted instantiation: strnatcmp.c:php_random_default_engine
Unexecuted instantiation: uniqid.c:php_random_default_engine
Unexecuted instantiation: url_scanner_ex.c:php_random_default_engine
Unexecuted instantiation: var.c:php_random_default_engine
Unexecuted instantiation: fopen_wrappers.c:php_random_default_engine
Unexecuted instantiation: main.c:php_random_default_engine
Unexecuted instantiation: php_open_temporary_file.c:php_random_default_engine
Unexecuted instantiation: php_variables.c:php_random_default_engine
Unexecuted instantiation: streams.c:php_random_default_engine
Unexecuted instantiation: internal_functions_cli.c:php_random_default_engine
170
171
PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
172
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
173
174
PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed);
175
PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
176
177
PHPAPI void php_random_pcgoneseq128xslrr64_seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed);
178
PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance);
179
180
PHPAPI void php_random_xoshiro256starstar_seed64(php_random_status_state_xoshiro256starstar *state, uint64_t seed);
181
PHPAPI void php_random_xoshiro256starstar_seed256(php_random_status_state_xoshiro256starstar *state, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3);
182
PHPAPI void php_random_xoshiro256starstar_jump(php_random_status_state_xoshiro256starstar *state);
183
PHPAPI void php_random_xoshiro256starstar_jump_long(php_random_status_state_xoshiro256starstar *state);
184
185
PHPAPI double php_random_gammasection_closed_open(php_random_algo_with_state engine, double min, double max);
186
PHPAPI double php_random_gammasection_closed_closed(php_random_algo_with_state engine, double min, double max);
187
PHPAPI double php_random_gammasection_open_closed(php_random_algo_with_state engine, double min, double max);
188
PHPAPI double php_random_gammasection_open_open(php_random_algo_with_state engine, double min, double max);
189
190
extern zend_module_entry random_module_entry;
191
# define phpext_random_ptr &random_module_entry
192
193
PHP_MINIT_FUNCTION(random);
194
PHP_MSHUTDOWN_FUNCTION(random);
195
PHP_RINIT_FUNCTION(random);
196
197
ZEND_BEGIN_MODULE_GLOBALS(random)
198
  bool combined_lcg_seeded;
199
  bool mt19937_seeded;
200
  php_random_fallback_seed_state fallback_seed_state;
201
  int32_t combined_lcg[2];
202
  php_random_status_state_mt19937 mt19937;
203
ZEND_END_MODULE_GLOBALS(random)
204
205
PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
206
207
600k
# define RANDOM_G(v)  ZEND_MODULE_GLOBALS_ACCESSOR(random, v)
208
209
#endif  /* PHP_RANDOM_H */