xref: /php-src/ext/random/engine_user.c (revision 99e7cf07)
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(void * state)24 static php_random_result generate(void *state)
25 {
26 	php_random_status_state_user *s = 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 (php_random_result){
35 			.size = sizeof(uint64_t),
36 			.result = 0,
37 		};
38 	}
39 
40 	size = Z_STRLEN(retval);
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 
60 	zval_ptr_dtor(&retval);
61 
62 	return (php_random_result){
63 		.size = size,
64 		.result = result,
65 	};
66 }
67 
range(void * state,zend_long min,zend_long max)68 static zend_long range(void *state, zend_long min, zend_long max)
69 {
70 	return php_random_range((php_random_algo_with_state){
71 		.algo = &php_random_algo_user,
72 		.state = state,
73 	}, min, max);
74 }
75 
76 const php_random_algo php_random_algo_user = {
77 	sizeof(php_random_status_state_user),
78 	generate,
79 	range,
80 	NULL,
81 	NULL,
82 };
83