- md5
- a5d37c711396fc37cec8457aceea51c8
- sha1
- 9a3e1444331415d6124c67eb99624b3e0fc7af7c
- sha256
- 803a81c8aef82c17f259b26dfedd401dfbb03b04b760ee54430cb3a22d25b872
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
<?php declare(strict_types=1);
namespace Algo26\IdnaConvert\Punycode;
use Algo26\IdnaConvert\Exception\InvalidCharacterException;
use OutOfBoundsException;
class FromPunycode extends AbstractPunycode implements PunycodeInterface
{
public function __construct(
?int $idnVersion = null,
?bool $useStd3AsciiRules = false
) {
parent::__construct();
}
/**
* @throws InvalidCharacterException
*/
public function convert(string $encoded)
{
if (!$this->isValidPunycodeString($encoded)) {
return false;
}
$decoded = [];
// Find last occurrence of the delimiter
$delimiterPosition = strrpos($encoded, '-');
if ($delimiterPosition > $this->getByteLength($this->getPunycodePrefix())) {
for ($k = $this->getByteLength($this->getPunycodePrefix()); $k < $delimiterPosition; ++$k) {
$decoded[] = ord($encoded[$k]);
}
}
$decodedLength = count($decoded);
$encodedLength = $this->getByteLength($encoded);
// Walking through the strings; init
$isFirst = true;
$bias = self::initialBias;
$currentIndex = 0;
$char = self::initialN;
$startOfLoop = ($delimiterPosition) ? ($delimiterPosition + 1) : 0;
for ($encodedIndex = $startOfLoop; $encodedIndex < $encodedLength; ++$decodedLength) {
for ($oldIndex = $currentIndex, $w = 1, $k = self::base; 1; $k += self::base) {
if ($encodedIndex + 1 > $encodedLength) {
throw new InvalidCharacterException('trying to read beyond input length');
}
$digit = $this->decodeDigit($encoded[$encodedIndex++]);
if ($digit >= self::base) {
throw new InvalidCharacterException(
sprintf(
'encountered invalid digit at #%d',
$encodedIndex - 1,
)
);
}
if ($digit > floor((PHP_INT_MAX - $currentIndex) / $w)) {
throw new OutOfBoundsException(
sprintf(
'overflow at #%d',
$encodedIndex - 1,
)
);
}
$currentIndex += $digit * $w;
$t = ($k <= $bias)
? self::tMin
: (
($k >= $bias + self::tMax)
? self::tMax
: ($k - $bias)
);
if ($digit < $t) {
break;
}
$w = (int) ($w * (self::base - $t));
}
$bias = $this->adapt($currentIndex - $oldIndex, $decodedLength + 1, $isFirst);
$isFirst = false;
$char += (int) ($currentIndex / ($decodedLength + 1));
$currentIndex %= ($decodedLength + 1);
if ($decodedLength > 0) {
// Make room for the decoded char
for ($i = $decodedLength; $i > $currentIndex; $i--) {
$decoded[$i] = $decoded[($i - 1)];
}
}
$decoded[$currentIndex++] = $char;
}
return $this->unicodeTransCoder->convert(
$decoded,
$this->unicodeTransCoder::FORMAT_UCS4_ARRAY,
$this->unicodeTransCoder::FORMAT_UTF8
);
}
private function isValidPunycodeString($encoded): bool
{
// Check for existence of the prefix
if (!str_starts_with($encoded, self::punycodePrefix)) {
return false;
}
// If nothing is left after the prefix, it is hopeless
if (strlen(trim($encoded)) <= strlen(self::punycodePrefix)) {
return false;
}
return true;
}
private function decodeDigit(string $codePoint): int
{
$codeAsInt = ord($codePoint);
if ($codeAsInt - 48 < 10) {
return $codeAsInt - 22;
}
if ($codeAsInt - 65 < 26) {
return $codeAsInt - 65;
}
if ($codeAsInt - 97 < 26) {
return $codeAsInt - 97;
}
return self::base;
}
}
The same content is in 4 releases, Joomla 5.3.0-rc1 to 6.0.0-alpha3. See them all, or every version of this file.