Coverage Report

Created: 2026-06-02 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/random/php_random.h
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
12
   |          Zeev Suraski <zeev@php.net>                                 |
13
   |          Sascha Schumann <sascha@schumann.cx>                        |
14
   |          Pedro Melo <melo@ip.pt>                                     |
15
   |          Sterling Hughes <sterling@php.net>                          |
16
   |          Sammy Kaye Powers <me@sammyk.me>                            |
17
   |          Go Kudo <zeriyoshi@php.net>                                 |
18
   |                                                                      |
19
   | Based on code from: Richard J. Wagner <rjwagner@writeme.com>         |
20
   |                     Makoto Matsumoto <matumoto@math.keio.ac.jp>      |
21
   |                     Takuji Nishimura                                 |
22
   |                     Shawn Cokus <Cokus@math.washington.edu>          |
23
   |                     David Blackman                                   |
24
   |                     Sebastiano Vigna <vigna@acm.org>                 |
25
   |                     Melissa O'Neill <oneill@pcg-random.org>          |
26
   +----------------------------------------------------------------------+
27
*/
28
29
#ifndef PHP_RANDOM_H
30
# define PHP_RANDOM_H
31
32
# include "php.h"
33
# include "php_random_csprng.h"
34
# include "php_random_uint128.h"
35
# include "random_decl.h"
36
37
PHPAPI double php_combined_lcg(void);
38
39
0
# define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
40
41
enum php_random_mt19937_mode {
42
  MT_RAND_MT19937 = 0,
43
  MT_RAND_PHP = 1,
44
};
45
46
0
#define PHP_RANDOM_RANGE_ATTEMPTS (50)
47
48
PHPAPI void php_mt_srand(uint32_t seed);
49
PHPAPI uint32_t php_mt_rand(void);
50
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
51
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
52
53
typedef struct php_random_status_state_mt19937 {
54
  uint32_t count;
55
  enum php_random_mt19937_mode mode;
56
  uint32_t state[624];
57
} php_random_status_state_mt19937;
58
59
typedef struct php_random_status_state_pcgoneseq128xslrr64 {
60
  php_random_uint128_t state;
61
} php_random_status_state_pcgoneseq128xslrr64;
62
63
typedef struct php_random_status_state_xoshiro256starstar {
64
  uint64_t state[4];
65
} php_random_status_state_xoshiro256starstar;
66
67
typedef struct php_random_status_state_user {
68
  zend_object *object;
69
  zend_function *generate_method;
70
} php_random_status_state_user;
71
72
typedef struct php_random_result {
73
  uint64_t result;
74
  size_t size;
75
} php_random_result;
76
77
typedef struct php_random_algo {
78
  const size_t state_size;
79
  php_random_result (*generate)(void *state);
80
  zend_long (*range)(void *state, zend_long min, zend_long max);
81
  bool (*serialize)(void *state, HashTable *data);
82
  bool (*unserialize)(void *state, HashTable *data);
83
} php_random_algo;
84
85
typedef struct php_random_algo_with_state {
86
  const php_random_algo *algo;
87
  void *state;
88
} php_random_algo_with_state;
89
90
typedef struct php_random_fallback_seed_state {
91
  bool initialized;
92
  unsigned char seed[20];
93
} php_random_fallback_seed_state;
94
95
extern PHPAPI const php_random_algo php_random_algo_mt19937;
96
extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
97
extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
98
extern PHPAPI const php_random_algo php_random_algo_secure;
99
extern PHPAPI const php_random_algo php_random_algo_user;
100
101
typedef struct php_random_engine {
102
  php_random_algo_with_state engine;
103
  zend_object std;
104
} php_random_engine;
105
106
typedef struct php_random_randomizer {
107
  php_random_algo_with_state engine;
108
  bool is_userland_algo;
109
  zend_object std;
110
} php_random_randomizer;
111
112
extern PHPAPI zend_class_entry *random_ce_Random_Engine;
113
extern PHPAPI zend_class_entry *random_ce_Random_CryptoSafeEngine;
114
115
extern PHPAPI zend_class_entry *random_ce_Random_RandomError;
116
extern PHPAPI zend_class_entry *random_ce_Random_BrokenRandomEngineError;
117
extern PHPAPI zend_class_entry *random_ce_Random_RandomException;
118
119
extern PHPAPI zend_class_entry *random_ce_Random_Engine_PcgOneseq128XslRr64;
120
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
121
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Xoshiro256StarStar;
122
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Secure;
123
124
extern PHPAPI zend_class_entry *random_ce_Random_Randomizer;
125
126
extern PHPAPI zend_class_entry *random_ce_Random_IntervalBoundary;
127
128
36
#define php_random_engine_from_obj(object) ZEND_CONTAINER_OF(object, php_random_engine, std)
129
4
#define php_random_randomizer_from_obj(object) ZEND_CONTAINER_OF(object, php_random_randomizer, std)
130
131
9
# define Z_RANDOM_ENGINE_P(zval) php_random_engine_from_obj(Z_OBJ_P(zval))
132
133
2
# define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
134
135
PHPAPI uint64_t php_random_generate_fallback_seed(void);
136
PHPAPI uint64_t php_random_generate_fallback_seed_ex(php_random_fallback_seed_state *state);
137
138
static inline zend_long GENERATE_SEED(void)
139
0
{
140
0
  return (zend_long)php_random_generate_fallback_seed();
141
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
142
143
PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
144
PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status);
145
PHPAPI void php_random_status_free(void *status, const bool persistent);
146
PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, const php_random_algo *algo);
147
PHPAPI void php_random_engine_common_free_object(zend_object *object);
148
PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
149
PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t umax);
150
PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t umax);
151
PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max);
152
PHPAPI const php_random_algo *php_random_default_algo(void);
153
PHPAPI void *php_random_default_status(void);
154
155
static inline php_random_algo_with_state php_random_default_engine(void)
156
12
{
157
12
  php_random_algo_with_state raws;
158
12
  raws.algo = php_random_default_algo();
159
12
  raws.state = php_random_default_status();
160
12
  return raws;
161
12
}
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
156
12
{
157
12
  php_random_algo_with_state raws;
158
12
  raws.algo = php_random_default_algo();
159
12
  raws.state = php_random_default_status();
160
12
  return raws;
161
12
}
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
162
163
PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
164
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
165
166
PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed);
167
PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
168
169
PHPAPI void php_random_pcgoneseq128xslrr64_seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed);
170
PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance);
171
172
PHPAPI void php_random_xoshiro256starstar_seed64(php_random_status_state_xoshiro256starstar *state, uint64_t seed);
173
PHPAPI void php_random_xoshiro256starstar_seed256(php_random_status_state_xoshiro256starstar *state, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3);
174
PHPAPI void php_random_xoshiro256starstar_jump(php_random_status_state_xoshiro256starstar *state);
175
PHPAPI void php_random_xoshiro256starstar_jump_long(php_random_status_state_xoshiro256starstar *state);
176
177
PHPAPI double php_random_gammasection_closed_open(php_random_algo_with_state engine, double min, double max);
178
PHPAPI double php_random_gammasection_closed_closed(php_random_algo_with_state engine, double min, double max);
179
PHPAPI double php_random_gammasection_open_closed(php_random_algo_with_state engine, double min, double max);
180
PHPAPI double php_random_gammasection_open_open(php_random_algo_with_state engine, double min, double max);
181
182
extern zend_module_entry random_module_entry;
183
# define phpext_random_ptr &random_module_entry
184
185
PHP_MINIT_FUNCTION(random);
186
PHP_MSHUTDOWN_FUNCTION(random);
187
PHP_RINIT_FUNCTION(random);
188
189
ZEND_BEGIN_MODULE_GLOBALS(random)
190
  bool combined_lcg_seeded;
191
  bool mt19937_seeded;
192
  php_random_fallback_seed_state fallback_seed_state;
193
  int32_t combined_lcg[2];
194
  php_random_status_state_mt19937 mt19937;
195
ZEND_END_MODULE_GLOBALS(random)
196
197
PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
198
199
88.9k
# define RANDOM_G(v)  ZEND_MODULE_GLOBALS_ACCESSOR(random, v)
200
201
#endif  /* PHP_RANDOM_H */