php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
engine_user.c
Go to the documentation of this file.
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 | Author: Go Kudo <zeriyoshi@php.net> |
14 +----------------------------------------------------------------------+
15*/
16
17#ifdef HAVE_CONFIG_H
18# include "config.h"
19#endif
20
21#include "php.h"
22#include "php_random.h"
23
24static php_random_result generate(void *state)
25{
27 uint64_t result = 0;
28 size_t size;
30
31 zend_call_known_instance_method_with_0_params(s->generate_method, s->object, &retval);
32
33 if (EG(exception)) {
34 return (php_random_result){
35 .size = sizeof(uint64_t),
36 .result = 0,
37 };
38 }
39
41
42 /* Guard for over 64-bit results */
43 if (size > sizeof(uint64_t)) {
44 size = sizeof(uint64_t);
45 }
46
47 if (size > 0) {
48 /* Endianness safe copy */
49 for (size_t i = 0; i < size; i++) {
50 result += ((uint64_t) (unsigned char) Z_STRVAL(retval)[i]) << (8 * i);
51 }
52 } else {
53 zend_throw_error(random_ce_Random_BrokenRandomEngineError, "A random engine must return a non-empty string");
54 return (php_random_result){
55 .size = sizeof(uint64_t),
56 .result = 0,
57 };
58 }
59
61
62 return (php_random_result){
63 .size = size,
64 .result = result,
65 };
66}
67
68static zend_long range(void *state, zend_long min, zend_long max)
69{
71 .algo = &php_random_algo_user,
72 .state = state,
73 }, min, max);
74}
75
78 generate,
79 range,
80 NULL,
81 NULL,
82};
bool exception
Definition assert.c:30
char s[4]
Definition cdf.c:77
PHPAPI const php_random_algo php_random_algo_user
Definition engine_user.c:76
#define max(a, b)
Definition exif.c:60
new_type size
Definition ffi.c:4365
#define NULL
Definition gdcache.h:45
#define PHPAPI
Definition php.h:71
#define min(a, b)
struct _php_random_algo_with_state php_random_algo_with_state
struct _php_random_status_state_user php_random_status_state_user
PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max)
Definition random.c:293
struct _php_random_algo php_random_algo
PHPAPI zend_class_entry * random_ce_Random_BrokenRandomEngineError
struct _php_random_result php_random_result
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
struct _zval_struct zval
#define EG(v)
int32_t zend_long
Definition zend_long.h:42
#define Z_STRVAL(zval)
Definition zend_types.h:974
#define Z_STRLEN(zval)
Definition zend_types.h:977
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval retval
bool result