xref: /PHP-8.2/ext/random/engine_user.c (revision 3331832b)
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 
generate(php_random_status * status)24 static uint64_t generate(php_random_status *status)
25 {
26 	php_random_status_state_user *s = status->state;
27 	uint64_t result = 0;
28 	size_t size;
29 	zval retval;
30 
31 	zend_call_known_instance_method_with_0_params(s->generate_method, s->object, &retval);
32 
33 	if (EG(exception)) {
34 		return 0;
35 	}
36 
37 	/* Store generated size in a state */
38 	size = Z_STRLEN(retval);
39 
40 	/* Guard for over 64-bit results */
41 	if (size > sizeof(uint64_t)) {
42 		size = sizeof(uint64_t);
43 	}
44 	status->last_generated_size = size;
45 
46 	if (size > 0) {
47 		/* Endianness safe copy */
48 		for (size_t i = 0; i < size; i++) {
49 			result += ((uint64_t) (unsigned char) Z_STRVAL(retval)[i]) << (8 * i);
50 		}
51 	} else {
52 		zend_throw_error(random_ce_Random_BrokenRandomEngineError, "A random engine must return a non-empty string");
53 		return 0;
54 	}
55 
56 	zval_ptr_dtor(&retval);
57 
58 	return result;
59 }
60 
range(php_random_status * status,zend_long min,zend_long max)61 static zend_long range(php_random_status *status, zend_long min, zend_long max)
62 {
63 	return php_random_range(&php_random_algo_user, status, min, max);
64 }
65 
66 const php_random_algo php_random_algo_user = {
67 	0,
68 	sizeof(php_random_status_state_user),
69 	NULL,
70 	generate,
71 	range,
72 	NULL,
73 	NULL,
74 };
75