Skip to content

Commit e96b077

Browse files
committed
Got random_bytes() working again.
First attempt at random_int(). Commented-out random_hex() until the other functions are rocking.
1 parent 3c5fcac commit e96b077

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

ext/standard/random.c

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,35 @@
2828
# include "win32/winutil.h"
2929
#endif
3030

31+
/*
3132
// Copy/pasted from string.c
3233
static char hexconvtab[] = "0123456789abcdef";
3334
3435
// 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)
3637
{
3738
zend_long i, j;
3839
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+
4042
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];
4345
}
4446
45-
hex[j] = '\0';
47+
hex->val[j] = '\0';
4648
}
49+
*/
4750

4851
// 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)
5053
{
5154
int n = 0;
5255

5356
#if PHP_WIN32
5457
/* random/urandom equivalent on Windows */
5558
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) {
5760
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
5861
return FAILURE;
5962
}
@@ -75,13 +78,17 @@ static int php_random_bytes(zend_string *bytes, zend_long size)
7578
read_bytes += n;
7679
}
7780
n = read_bytes;
81+
7882
close(fd);
7983
if (n < size) {
8084
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
8185
return FAILURE;
8286
}
8387
#endif
8488

89+
// @todo - Do we need to do this?
90+
bytes[size] = '\0';
91+
8592
return SUCCESS;
8693
}
8794

@@ -101,23 +108,22 @@ PHP_FUNCTION(random_bytes)
101108
RETURN_FALSE;
102109
}
103110

104-
bytes = zend_string_alloc(size + 1, 0);
111+
bytes = zend_string_alloc(size, 0);
105112

106-
if (php_random_bytes(bytes, size) == FAILURE) {
113+
if (php_random_bytes(bytes->val, size) == FAILURE) {
107114
zend_string_release(bytes);
108115
return;
109116
}
110117

111-
RETVAL_STRINGL(bytes, size);
112-
113-
zend_string_release(bytes);
118+
RETURN_STR(bytes);
114119
}
115120
/* }}} */
116121

117122
/* {{{ proto string random_hex(int bytes)
118123
Return an arbitrary length of pseudo-random bytes as hexadecimal string */
119124
PHP_FUNCTION(random_hex)
120125
{
126+
/*
121127
zend_long size;
122128
zend_string *bytes;
123129
zend_string *hex;
@@ -131,49 +137,69 @@ PHP_FUNCTION(random_hex)
131137
RETURN_FALSE;
132138
}
133139
134-
// @todo should we half the size for hex? How for odd num of chars?
135-
bytes = zend_string_alloc(size + 1, 0);
136-
137140
if (php_random_bytes(bytes, size) == FAILURE) {
138-
zend_string_release(bytes);
139141
return;
140142
}
141143
142144
int hex_size = size * 2;
143-
hex = zend_string_alloc(hex_size + 1, 0);
144-
php_bin_to_hex(bytes, hex_size, hex);
145145
146-
RETVAL_STRINGL(hex, hex_size);
146+
php_bin_to_hex(bytes, hex_size, hex);
147147
148148
zend_string_release(bytes);
149-
zend_string_release(hex);
149+
*/
150+
151+
RETURN_STR("Foo!");
150152
}
151153
/* }}} */
152154

153-
/* {{{ proto int random_int(int min, int max)
155+
/* {{{ proto int random_int(int max)
154156
Return an arbitrary pseudo-random integer */
155157
PHP_FUNCTION(random_int)
156158
{
157-
zend_long min;
158159
zend_long max;
159-
zend_long number;
160+
zend_long size;
161+
zend_long number = 0;
162+
zend_string *bytes;
163+
size_t i;
160164

161-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) {
165+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max) == FAILURE) {
162166
return;
163167
}
164168

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);
167171
RETURN_FALSE;
168172
}
169173

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;
173200
}
174201

175-
// @todo Insert bin-to-int stuff here
176-
number = min + max * 100;
202+
zend_string_release(bytes);
177203

178204
RETURN_LONG(number);
179205
}

0 commit comments

Comments
 (0)