Skip to content

Commit 65ea801

Browse files
committed
Changed calculate bitmask 8 bytes at a time.
1 parent df6d85a commit 65ea801

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

ext/random/randomizer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,8 @@ PHP_METHOD(Random_Randomizer, getBytesFromString)
432432
mask |= mask >> 2;
433433
mask |= mask >> 4;
434434

435+
/* Example: if mask is 0xAB, will be 0xABABABABABABABAB */
436+
uint64_t mask_repeat = (~((uint64_t) 0) / 0xff) * mask;
435437
int failures = 0;
436438
while (total_size < length) {
437439
php_random_result result = engine.algo->generate(engine.state);
@@ -440,8 +442,9 @@ PHP_METHOD(Random_Randomizer, getBytesFromString)
440442
RETURN_THROWS();
441443
}
442444

445+
uint64_t offsets = result.result & mask_repeat;
443446
for (size_t i = 0; i < result.size; i++) {
444-
uint64_t offset = (result.result >> (i * 8)) & mask;
447+
uint64_t offset = offsets >> (i * 8) & 0xff;
445448

446449
if (offset > max_offset) {
447450
if (++failures > PHP_RANDOM_RANGE_ATTEMPTS) {

0 commit comments

Comments
 (0)