28
28
# include "win32/winutil.h"
29
29
#endif
30
30
31
+ /*
31
32
// Copy/pasted from string.c
32
33
static char hexconvtab[] = "0123456789abcdef";
33
34
34
35
// Copy/pasted from string.c
35
- static void php_bin_to_hex (char * old , const zend_long old_len , char * hex )
36
+ static void php_bin_to_hex(zend_string *old, const zend_long old_len, zend_string *hex)
36
37
{
37
38
zend_long i, j;
38
39
39
- // @todo I don't think this is doing it right
40
+ hex = zend_string_alloc(old_len * 2, 0); // @todo is this right?
41
+
40
42
for (i = j = 0; i < old_len; i++) {
41
- hex [j ++ ] = hexconvtab [old [i ] >> 4 ];
42
- hex [j ++ ] = hexconvtab [old [i ] & 15 ];
43
+ hex->val [j++] = hexconvtab[old->val [i] >> 4];
44
+ hex->val [j++] = hexconvtab[old->val [i] & 15];
43
45
}
44
46
45
- hex [j ] = '\0' ;
47
+ hex->val [j] = '\0';
46
48
}
49
+ */
47
50
48
51
// Copy/pasted from mcrypt.c
49
- static int php_random_bytes (zend_string * bytes , zend_long size )
52
+ static int php_random_bytes (char * bytes , zend_long size )
50
53
{
51
54
int n = 0 ;
52
55
53
56
#if PHP_WIN32
54
57
/* random/urandom equivalent on Windows */
55
58
BYTE * win_bytes = (BYTE * ) bytes ;
56
- if (php_win32_get_random_bytes (win_bytes , (size_t ) size ) == FAILURE ){
59
+ if (php_win32_get_random_bytes (win_bytes , (size_t ) size ) == FAILURE ) {
57
60
php_error_docref (NULL , E_WARNING , "Could not gather sufficient random data" );
58
61
return FAILURE ;
59
62
}
@@ -75,13 +78,17 @@ static int php_random_bytes(zend_string *bytes, zend_long size)
75
78
read_bytes += n ;
76
79
}
77
80
n = read_bytes ;
81
+
78
82
close (fd );
79
83
if (n < size ) {
80
84
php_error_docref (NULL , E_WARNING , "Could not gather sufficient random data" );
81
85
return FAILURE ;
82
86
}
83
87
#endif
84
88
89
+ // @todo - Do we need to do this?
90
+ bytes [size ] = '\0' ;
91
+
85
92
return SUCCESS ;
86
93
}
87
94
@@ -101,23 +108,22 @@ PHP_FUNCTION(random_bytes)
101
108
RETURN_FALSE ;
102
109
}
103
110
104
- bytes = zend_string_alloc (size + 1 , 0 );
111
+ bytes = zend_string_alloc (size , 0 );
105
112
106
- if (php_random_bytes (bytes , size ) == FAILURE ) {
113
+ if (php_random_bytes (bytes -> val , size ) == FAILURE ) {
107
114
zend_string_release (bytes );
108
115
return ;
109
116
}
110
117
111
- RETVAL_STRINGL (bytes , size );
112
-
113
- zend_string_release (bytes );
118
+ RETURN_STR (bytes );
114
119
}
115
120
/* }}} */
116
121
117
122
/* {{{ proto string random_hex(int bytes)
118
123
Return an arbitrary length of pseudo-random bytes as hexadecimal string */
119
124
PHP_FUNCTION (random_hex )
120
125
{
126
+ /*
121
127
zend_long size;
122
128
zend_string *bytes;
123
129
zend_string *hex;
@@ -131,49 +137,69 @@ PHP_FUNCTION(random_hex)
131
137
RETURN_FALSE;
132
138
}
133
139
134
- // @todo should we half the size for hex? How for odd num of chars?
135
- bytes = zend_string_alloc (size + 1 , 0 );
136
-
137
140
if (php_random_bytes(bytes, size) == FAILURE) {
138
- zend_string_release (bytes );
139
141
return;
140
142
}
141
143
142
144
int hex_size = size * 2;
143
- hex = zend_string_alloc (hex_size + 1 , 0 );
144
- php_bin_to_hex (bytes , hex_size , hex );
145
145
146
- RETVAL_STRINGL ( hex , hex_size );
146
+ php_bin_to_hex(bytes , hex_size, hex );
147
147
148
148
zend_string_release(bytes);
149
- zend_string_release (hex );
149
+ */
150
+
151
+ RETURN_STR ("Foo!" );
150
152
}
151
153
/* }}} */
152
154
153
- /* {{{ proto int random_int(int min, int max)
155
+ /* {{{ proto int random_int(int max)
154
156
Return an arbitrary pseudo-random integer */
155
157
PHP_FUNCTION (random_int )
156
158
{
157
- zend_long min ;
158
159
zend_long max ;
159
- zend_long number ;
160
+ zend_long size ;
161
+ zend_long number = 0 ;
162
+ zend_string * bytes ;
163
+ size_t i ;
160
164
161
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "ll" , & min , & max ) == FAILURE ) {
165
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "l" , & max ) == FAILURE ) {
162
166
return ;
163
167
}
164
168
165
- if (min >= INT_MAX || max >= INT_MAX ) {
166
- php_error_docref (NULL , E_WARNING , "Cannot use range greater than %d" , INT_MAX );
169
+ if (max >= INT_MAX ) {
170
+ php_error_docref (NULL , E_WARNING , "Cannot use max greater than %d" , INT_MAX );
167
171
RETURN_FALSE ;
168
172
}
169
173
170
- if (max < min ) {
171
- php_error_docref (NULL , E_WARNING , "Max value (%d) is less than min value (%d)" , max , min );
172
- RETURN_FALSE ;
174
+ size = sizeof (number );
175
+
176
+ bytes = zend_string_alloc (size , 0 );
177
+
178
+ if (php_random_bytes (bytes -> val , size ) == FAILURE ) {
179
+ zend_string_release (bytes );
180
+ return ;
181
+ }
182
+
183
+ // @todo bin-to-int: I know this is wrong but don't know how to fix
184
+ for (i = 0 ; i < size ; i ++ ) {
185
+ unsigned char c = bytes -> val [i ++ ];
186
+ unsigned char d ;
187
+
188
+ if (c >= '0' && c <= '9' ) {
189
+ d = c - '0' ;
190
+ } else if (c >= 'a' && c <= 'f' ) {
191
+ d = c - 'a' - 10 ;
192
+ } else if (c >= 'A' && c <= 'F' ) {
193
+ d = c - 'A' - 10 ;
194
+ } else {
195
+ continue ;
196
+ }
197
+
198
+ // Binary = base-2
199
+ number = number * 2 + d ;
173
200
}
174
201
175
- // @todo Insert bin-to-int stuff here
176
- number = min + max * 100 ;
202
+ zend_string_release (bytes );
177
203
178
204
RETURN_LONG (number );
179
205
}
0 commit comments