Skip to content

Commit 278a1d2

Browse files
committed
version 2
1 parent 49fa32e commit 278a1d2

File tree

1 file changed

+9
-42
lines changed

1 file changed

+9
-42
lines changed

ext/random/randomizer.c

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -270,43 +270,6 @@ PHP_METHOD(Random_Randomizer, getInt)
270270
}
271271
/* }}} */
272272

273-
static zend_always_inline char *bulk_convert_generated_result_to_char_32(char *ptr, uint64_t result, size_t *to_read)
274-
{
275-
if (*to_read >= sizeof(uint32_t)) {
276-
uint32_t tmp = (uint32_t) result;
277-
#ifdef WORDS_BIGENDIAN
278-
tmp = RANDOM_BSWAP32(tmp);
279-
#endif
280-
memcpy(ptr, &tmp, sizeof(uint32_t));
281-
ptr += sizeof(uint32_t);
282-
*to_read -= sizeof(uint32_t);
283-
} else {
284-
while (*to_read > 0) {
285-
*ptr++ = result & 0xff;
286-
result >>= 8;
287-
*to_read -= 1;
288-
}
289-
}
290-
291-
return ptr;
292-
}
293-
294-
static zend_always_inline char *bulk_convert_generated_result_to_char_64(char *ptr, uint64_t result, size_t *to_read)
295-
{
296-
if (*to_read >= sizeof(uint64_t)) {
297-
#ifdef WORDS_BIGENDIAN
298-
result = RANDOM_BSWAP64(result);
299-
#endif
300-
memcpy(ptr, &result, sizeof(uint64_t));
301-
ptr += sizeof(uint64_t);
302-
*to_read -= sizeof(uint64_t);
303-
} else {
304-
ptr = bulk_convert_generated_result_to_char_32(ptr, result, to_read);
305-
}
306-
307-
return ptr;
308-
}
309-
310273
/* {{{ Generate random bytes string in ordered length */
311274
PHP_METHOD(Random_Randomizer, getBytes)
312275
{
@@ -336,12 +299,16 @@ PHP_METHOD(Random_Randomizer, getBytes)
336299
zend_string_free(retval);
337300
RETURN_THROWS();
338301
}
339-
if (EXPECTED(result.size == sizeof(uint64_t))) {
340-
rptr = bulk_convert_generated_result_to_char_64(rptr, result.result, &to_read);
341-
} else if (EXPECTED(result.size == sizeof(uint32_t))){
342-
rptr = bulk_convert_generated_result_to_char_32(rptr, result.result, &to_read);
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);
343311
} else {
344-
uint64_t tmp_ret = result.result;
345312
for (size_t i = 0; i < result.size; i++) {
346313
*rptr++ = tmp_ret & 0xff;
347314
tmp_ret >>= 8;

0 commit comments

Comments
 (0)