public function testSort() { $input = [3, 1, 2]; $native = new Native(); $output = $native->sort($input); $expected = [1, 2, 3]; $this->assertTrue($output); $this->assertSame($expected, $input); }
function phlp($expression) { $special = Native::special(); $reducers = Native::reducers(); $funcs = Native::funcs(); $fn = null; // only to check if first value IS a known function. $accepted = array_merge($special, $reducers, $funcs, Native::getdef()); if (is_string($expression[0]) && (function_exists($expression[0]) || array_key_exists($expression[0], $accepted) || array_key_exists($expression[0], Native::getdef()))) { $fn = $expression[0]; } if (array_key_exists($fn, $special)) { return call_user_func_array($special[$fn], array_slice($expression, 1)); } if ($fn === 'def') { return call_user_func_array($fn, array_slice($expression, 1)); } is_array($expression) && ($args = array_map(function ($arg) use($fn, $reducers) { if (is_array($arg)) { return phlp($arg); } elseif (is_string($arg) && array_key_exists($arg, Native::getdef()) && ($dfs = Native::getdef())) { return $dfs[$arg]; } else { return $arg; } }, array_slice($expression, count($fn)))); if (array_key_exists($fn, $reducers)) { return array_reduce($args, $reducers[$fn]); } elseif (array_key_exists($fn, Native::getdef()) && ($dfs = Native::getdef())) { return call_user_func_array($dfs[$fn], $args); } elseif (array_key_exists($fn, $funcs)) { return call_user_func_array($funcs[$fn], $args); } elseif ($fn && func_args_are_arrays($args)) { return call_user_func_array($fn, $args); } elseif ($signature = has_known_signature_type($fn)) { return call_user_func("call_user_func{$signature}", $fn, $args); } elseif ($fn) { return array_map(function ($arg) use($fn) { return call_user_func($fn, $arg); }, $args); } else { return $args; } }
/** * Decrypts the ciphertext using Blowfish with the given key. * * @param string $ciphertext the encrypted string * @param string $key the encryption key * @param int $mode one of BLOWFISH_MODE_CBC, BLOWFISH_MODE_EBC. BLOWFISH_MODE_CBC is recommened * @param int $padding one of BLOWFISH_PADDING_NONE, BLOWFISH_PADDING_RFC, BLOWFISH_PADDING_ZERO. BLOWFISH_PADDING_RFC is recommened * @param int $iv the initialisation vector. Required when using CBC mode. * @return string Returns the plaintext string. * @author Matt Harris **/ function decrypt($ciphertext, $key, $mode = Native::BLOWFISH_MODE_CBC, $padding = Native::BLOWFISH_PADDING_RFC, $iv = NULL) { if ($mode == Native::BLOWFISH_MODE_CBC and empty($iv)) { throw new \Exception('CBC Mode requires an IV key'); return; } $plaintext = ''; $fish = new Native($key, $mode, $padding, $iv); $block =& $fish->blockSize; $len = strlen($ciphertext); # encrypt in 1 byte intervals for ($i = 0; $i < $len; $i += $block) { list(, $xL, $xR) = unpack('N2', substr($ciphertext, $i, $block)); $fish->_decipher($xL, $xR); if ($mode == Native::BLOWFISH_MODE_CBC) { $chain = $i == 0 ? $fish->IV : substr($ciphertext, $i - $block, $block); $plaintext .= pack('N2', $xL, $xR) ^ $chain; } else { $plaintext .= pack('N2', $xL, $xR); } } $plaintext = $fish->_unpad($plaintext); unset($fish); return $plaintext; }