Skip to content

Commit cc6e295

Browse files
committed
restore it once
1 parent 31986eb commit cc6e295

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

ext/random/randomizer.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ PHP_METHOD(Random_Randomizer, getBytes)
278278

279279
zend_string *retval;
280280
zend_long user_length;
281+
size_t total_size = 0;
281282

282283
ZEND_PARSE_PARAMETERS_START(1, 1)
283284
Z_PARAM_LONG(user_length)
@@ -290,32 +291,17 @@ PHP_METHOD(Random_Randomizer, getBytes)
290291

291292
size_t length = (size_t)user_length;
292293
retval = zend_string_alloc(length, 0);
293-
char *rptr = ZSTR_VAL(retval);
294294

295-
size_t to_read = length;
296-
while (to_read > 0) {
295+
while (total_size < length) {
297296
php_random_result result = engine.algo->generate(engine.state);
298297
if (EG(exception)) {
299298
zend_string_free(retval);
300299
RETURN_THROWS();
301300
}
302-
303-
uint64_t tmp_ret = result.result;
304-
if (to_read >= result.size && result.size == sizeof(uint64_t)) {
305-
#ifdef WORDS_BIGENDIAN
306-
tmp_ret = RANDOM_BSWAP64(tmp_ret);
307-
#endif
308-
memcpy(rptr, &tmp_ret, sizeof(uint64_t));
309-
to_read -= sizeof(uint64_t);
310-
rptr += sizeof(uint64_t);
311-
} else {
312-
for (size_t i = 0; i < result.size; i++) {
313-
*rptr++ = tmp_ret & 0xff;
314-
tmp_ret >>= 8;
315-
to_read--;
316-
if (to_read == 0) {
317-
break;
318-
}
301+
for (size_t i = 0; i < result.size; i++) {
302+
ZSTR_VAL(retval)[total_size++] = (result.result >> (i * 8)) & 0xff;
303+
if (total_size >= length) {
304+
break;
319305
}
320306
}
321307
}

0 commit comments

Comments
 (0)