<?php
declare(strict_types=1);
function ct_select(
bool $returnLeft,
string $left,
string $right
): string {
$length = mb_strlen($left, '8bit');
if (mb_strlen($right, '8bit') !== $length) {
throw new Exception('ct_select() expects two strings of equal length');
}
// Mask byte
$mask = (-$returnLeft) & 0xff;
// X
$x = (string) ($left ^ $right);
// Output = Right XOR (X AND Mask)
$output = '';
for ($i = 0; $i < $length; $i++) {
$rightCharCode = unpack('C', $right[$i])[1];
$xCharCode = unpack('C', $x[$i])[1];
$output .= pack(
'C',
$rightCharCode ^ ($xCharCode & $mask)
);
}
return $output;
}
var_dump(
ct_select(true, 'apple', 'tiger'),
ct_select(false, 'apple', 'tiger'),
);
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.14
- string(5) "apple"
string(5) "tiger"
- Output for 7.2.0 - 7.2.34
- Parse error: syntax error, unexpected ')' in /in/rjqb1 on line 35
Process exited with code 255.
preferences:
78.8 ms | 407 KiB | 5 Q