Skip to content

Commit 6a17d32

Browse files
committed
version 2
1 parent c3a4623 commit 6a17d32

File tree

2 files changed

+9
-56
lines changed

2 files changed

+9
-56
lines changed

ext/random/php_random.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,30 +217,16 @@ PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
217217
/* Bytes swap */
218218
#ifdef _MSC_VER
219219
# include <stdlib.h>
220-
# define RANDOM_BSWAP32(u) _byteswap_ulong(u)
221220
# define RANDOM_BSWAP64(u) _byteswap_uint64(u)
222221
#else
223222
# ifdef __GNUC__
224-
# define RANDOM_BSWAP32(u) __builtin_bswap32(u)
225223
# define RANDOM_BSWAP64(u) __builtin_bswap64(u)
226224
# elif defined(__has_builtin)
227-
# if __has_builtin(__builtin_bswap32)
228-
# define RANDOM_BSWAP32(u) __builtin_bswap32(u)
229-
# endif // __has_builtin(__builtin_bswap32)
230225
# if __has_builtin(__builtin_bswap64)
231226
# define RANDOM_BSWAP64(u) __builtin_bswap64(u)
232227
# endif // __has_builtin(__builtin_bswap64)
233228
# endif // __GNUC__
234229
#endif // _MSC_VER
235-
#ifndef RANDOM_BSWAP32
236-
static inline uint32_t RANDOM_BSWAP32(uint32_t u)
237-
{
238-
return (((u & 0xff000000) >> 24)
239-
| ((u & 0x00ff0000) >> 8)
240-
| ((u & 0x0000ff00) << 8)
241-
| ((u & 0x000000ff) << 24));
242-
}
243-
#endif
244230
#ifndef RANDOM_BSWAP64
245231
static inline uint64_t RANDOM_BSWAP64(uint64_t u)
246232
{

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)