Idn.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com> and Trevor Rowbotham <trevor.rowbotham@pm.me>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Intl\Idn;
  11. use Symfony\Polyfill\Intl\Idn\Resources\unidata\DisallowedRanges;
  12. use Symfony\Polyfill\Intl\Idn\Resources\unidata\Regex;
  13. /**
  14. * @see https://www.unicode.org/reports/tr46/
  15. *
  16. * @internal
  17. */
  18. final class Idn
  19. {
  20. public const ERROR_EMPTY_LABEL = 1;
  21. public const ERROR_LABEL_TOO_LONG = 2;
  22. public const ERROR_DOMAIN_NAME_TOO_LONG = 4;
  23. public const ERROR_LEADING_HYPHEN = 8;
  24. public const ERROR_TRAILING_HYPHEN = 0x10;
  25. public const ERROR_HYPHEN_3_4 = 0x20;
  26. public const ERROR_LEADING_COMBINING_MARK = 0x40;
  27. public const ERROR_DISALLOWED = 0x80;
  28. public const ERROR_PUNYCODE = 0x100;
  29. public const ERROR_LABEL_HAS_DOT = 0x200;
  30. public const ERROR_INVALID_ACE_LABEL = 0x400;
  31. public const ERROR_BIDI = 0x800;
  32. public const ERROR_CONTEXTJ = 0x1000;
  33. public const ERROR_CONTEXTO_PUNCTUATION = 0x2000;
  34. public const ERROR_CONTEXTO_DIGITS = 0x4000;
  35. public const INTL_IDNA_VARIANT_2003 = 0;
  36. public const INTL_IDNA_VARIANT_UTS46 = 1;
  37. public const IDNA_DEFAULT = 0;
  38. public const IDNA_ALLOW_UNASSIGNED = 1;
  39. public const IDNA_USE_STD3_RULES = 2;
  40. public const IDNA_CHECK_BIDI = 4;
  41. public const IDNA_CHECK_CONTEXTJ = 8;
  42. public const IDNA_NONTRANSITIONAL_TO_ASCII = 16;
  43. public const IDNA_NONTRANSITIONAL_TO_UNICODE = 32;
  44. public const MAX_DOMAIN_SIZE = 253;
  45. public const MAX_LABEL_SIZE = 63;
  46. public const BASE = 36;
  47. public const TMIN = 1;
  48. public const TMAX = 26;
  49. public const SKEW = 38;
  50. public const DAMP = 700;
  51. public const INITIAL_BIAS = 72;
  52. public const INITIAL_N = 128;
  53. public const DELIMITER = '-';
  54. public const MAX_INT = 2147483647;
  55. /**
  56. * Contains the numeric value of a basic code point (for use in representing integers) in the
  57. * range 0 to BASE-1, or -1 if b is does not represent a value.
  58. *
  59. * @var array<int, int>
  60. */
  61. private static $basicToDigit = [
  62. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  65. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
  66. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  67. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  68. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  69. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  70. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  71. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  72. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  73. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  74. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  75. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  76. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  77. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78. ];
  79. /**
  80. * @var array<int, int>
  81. */
  82. private static $virama;
  83. /**
  84. * @var array<int, string>
  85. */
  86. private static $mapped;
  87. /**
  88. * @var array<int, bool>
  89. */
  90. private static $ignored;
  91. /**
  92. * @var array<int, string>
  93. */
  94. private static $deviation;
  95. /**
  96. * @var array<int, bool>
  97. */
  98. private static $disallowed;
  99. /**
  100. * @var array<int, string>
  101. */
  102. private static $disallowed_STD3_mapped;
  103. /**
  104. * @var array<int, bool>
  105. */
  106. private static $disallowed_STD3_valid;
  107. /**
  108. * @var bool
  109. */
  110. private static $mappingTableLoaded = false;
  111. /**
  112. * @see https://www.unicode.org/reports/tr46/#ToASCII
  113. *
  114. * @param string $domainName
  115. * @param int $options
  116. * @param int $variant
  117. * @param array $idna_info
  118. *
  119. * @return string|false
  120. */
  121. public static function idn_to_ascii($domainName, $options = self::IDNA_DEFAULT, $variant = self::INTL_IDNA_VARIANT_UTS46, &$idna_info = [])
  122. {
  123. if (\PHP_VERSION_ID >= 70200 && self::INTL_IDNA_VARIANT_2003 === $variant) {
  124. @trigger_error('idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated', \E_USER_DEPRECATED);
  125. }
  126. $options = [
  127. 'CheckHyphens' => true,
  128. 'CheckBidi' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 !== ($options & self::IDNA_CHECK_BIDI),
  129. 'CheckJoiners' => self::INTL_IDNA_VARIANT_UTS46 === $variant && 0 !== ($options & self::IDNA_CHECK_CONTEXTJ),
  130. 'UseSTD3ASCIIRules' => 0 !== ($options & self::IDNA_USE_STD3_RULES),
  131. 'Transitional_Processing' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 === ($options & self::IDNA_NONTRANSITIONAL_TO_ASCII),
  132. 'VerifyDnsLength' => true,
  133. ];
  134. $info = new Info();
  135. $labels = self::process((string) $domainName, $options, $info);
  136. foreach ($labels as $i => $label) {
  137. // Only convert labels to punycode that contain non-ASCII code points
  138. if (1 === preg_match('/[^\x00-\x7F]/', $label)) {
  139. try {
  140. $label = 'xn--'.self::punycodeEncode($label);
  141. } catch (\Exception $e) {
  142. $info->errors |= self::ERROR_PUNYCODE;
  143. }
  144. $labels[$i] = $label;
  145. }
  146. }
  147. if ($options['VerifyDnsLength']) {
  148. self::validateDomainAndLabelLength($labels, $info);
  149. }
  150. $idna_info = [
  151. 'result' => implode('.', $labels),
  152. 'isTransitionalDifferent' => $info->transitionalDifferent,
  153. 'errors' => $info->errors,
  154. ];
  155. return 0 === $info->errors ? $idna_info['result'] : false;
  156. }
  157. /**
  158. * @see https://www.unicode.org/reports/tr46/#ToUnicode
  159. *
  160. * @param string $domainName
  161. * @param int $options
  162. * @param int $variant
  163. * @param array $idna_info
  164. *
  165. * @return string|false
  166. */
  167. public static function idn_to_utf8($domainName, $options = self::IDNA_DEFAULT, $variant = self::INTL_IDNA_VARIANT_UTS46, &$idna_info = [])
  168. {
  169. if (\PHP_VERSION_ID >= 70200 && self::INTL_IDNA_VARIANT_2003 === $variant) {
  170. @trigger_error('idn_to_utf8(): INTL_IDNA_VARIANT_2003 is deprecated', \E_USER_DEPRECATED);
  171. }
  172. $info = new Info();
  173. $labels = self::process((string) $domainName, [
  174. 'CheckHyphens' => true,
  175. 'CheckBidi' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 !== ($options & self::IDNA_CHECK_BIDI),
  176. 'CheckJoiners' => self::INTL_IDNA_VARIANT_UTS46 === $variant && 0 !== ($options & self::IDNA_CHECK_CONTEXTJ),
  177. 'UseSTD3ASCIIRules' => 0 !== ($options & self::IDNA_USE_STD3_RULES),
  178. 'Transitional_Processing' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 === ($options & self::IDNA_NONTRANSITIONAL_TO_UNICODE),
  179. ], $info);
  180. $idna_info = [
  181. 'result' => implode('.', $labels),
  182. 'isTransitionalDifferent' => $info->transitionalDifferent,
  183. 'errors' => $info->errors,
  184. ];
  185. return 0 === $info->errors ? $idna_info['result'] : false;
  186. }
  187. /**
  188. * @param string $label
  189. *
  190. * @return bool
  191. */
  192. private static function isValidContextJ(array $codePoints, $label)
  193. {
  194. if (!isset(self::$virama)) {
  195. self::$virama = require __DIR__.\DIRECTORY_SEPARATOR.'Resources'.\DIRECTORY_SEPARATOR.'unidata'.\DIRECTORY_SEPARATOR.'virama.php';
  196. }
  197. $offset = 0;
  198. foreach ($codePoints as $i => $codePoint) {
  199. if (0x200C !== $codePoint && 0x200D !== $codePoint) {
  200. continue;
  201. }
  202. if (!isset($codePoints[$i - 1])) {
  203. return false;
  204. }
  205. // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
  206. if (isset(self::$virama[$codePoints[$i - 1]])) {
  207. continue;
  208. }
  209. // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C(Joining_Type:T)*(Joining_Type:{R,D})) Then
  210. // True;
  211. // Generated RegExp = ([Joining_Type:{L,D}][Joining_Type:T]*\u200C[Joining_Type:T]*)[Joining_Type:{R,D}]
  212. if (0x200C === $codePoint && 1 === preg_match(Regex::ZWNJ, $label, $matches, \PREG_OFFSET_CAPTURE, $offset)) {
  213. $offset += \strlen($matches[1][0]);
  214. continue;
  215. }
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * @see https://www.unicode.org/reports/tr46/#ProcessingStepMap
  222. *
  223. * @param string $input
  224. * @param array<string, bool> $options
  225. *
  226. * @return string
  227. */
  228. private static function mapCodePoints($input, array $options, Info $info)
  229. {
  230. $str = '';
  231. $useSTD3ASCIIRules = $options['UseSTD3ASCIIRules'];
  232. $transitional = $options['Transitional_Processing'];
  233. foreach (self::utf8Decode($input) as $codePoint) {
  234. $data = self::lookupCodePointStatus($codePoint, $useSTD3ASCIIRules);
  235. switch ($data['status']) {
  236. case 'disallowed':
  237. $info->errors |= self::ERROR_DISALLOWED;
  238. // no break.
  239. case 'valid':
  240. $str .= mb_chr($codePoint, 'utf-8');
  241. break;
  242. case 'ignored':
  243. // Do nothing.
  244. break;
  245. case 'mapped':
  246. $str .= $data['mapping'];
  247. break;
  248. case 'deviation':
  249. $info->transitionalDifferent = true;
  250. $str .= ($transitional ? $data['mapping'] : mb_chr($codePoint, 'utf-8'));
  251. break;
  252. }
  253. }
  254. return $str;
  255. }
  256. /**
  257. * @see https://www.unicode.org/reports/tr46/#Processing
  258. *
  259. * @param string $domain
  260. * @param array<string, bool> $options
  261. *
  262. * @return array<int, string>
  263. */
  264. private static function process($domain, array $options, Info $info)
  265. {
  266. // If VerifyDnsLength is not set, we are doing ToUnicode otherwise we are doing ToASCII and
  267. // we need to respect the VerifyDnsLength option.
  268. $checkForEmptyLabels = !isset($options['VerifyDnsLength']) || $options['VerifyDnsLength'];
  269. if ($checkForEmptyLabels && '' === $domain) {
  270. $info->errors |= self::ERROR_EMPTY_LABEL;
  271. return [$domain];
  272. }
  273. // Step 1. Map each code point in the domain name string
  274. $domain = self::mapCodePoints($domain, $options, $info);
  275. // Step 2. Normalize the domain name string to Unicode Normalization Form C.
  276. if (!\Normalizer::isNormalized($domain, \Normalizer::FORM_C)) {
  277. $domain = \Normalizer::normalize($domain, \Normalizer::FORM_C);
  278. }
  279. // Step 3. Break the string into labels at U+002E (.) FULL STOP.
  280. $labels = explode('.', $domain);
  281. $lastLabelIndex = \count($labels) - 1;
  282. // Step 4. Convert and validate each label in the domain name string.
  283. foreach ($labels as $i => $label) {
  284. $validationOptions = $options;
  285. if ('xn--' === substr($label, 0, 4)) {
  286. try {
  287. $label = self::punycodeDecode(substr($label, 4));
  288. } catch (\Exception $e) {
  289. $info->errors |= self::ERROR_PUNYCODE;
  290. continue;
  291. }
  292. $validationOptions['Transitional_Processing'] = false;
  293. $labels[$i] = $label;
  294. }
  295. self::validateLabel($label, $info, $validationOptions, $i > 0 && $i === $lastLabelIndex);
  296. }
  297. if ($info->bidiDomain && !$info->validBidiDomain) {
  298. $info->errors |= self::ERROR_BIDI;
  299. }
  300. // Any input domain name string that does not record an error has been successfully
  301. // processed according to this specification. Conversely, if an input domain_name string
  302. // causes an error, then the processing of the input domain_name string fails. Determining
  303. // what to do with error input is up to the caller, and not in the scope of this document.
  304. return $labels;
  305. }
  306. /**
  307. * @see https://tools.ietf.org/html/rfc5893#section-2
  308. *
  309. * @param string $label
  310. */
  311. private static function validateBidiLabel($label, Info $info)
  312. {
  313. if (1 === preg_match(Regex::RTL_LABEL, $label)) {
  314. $info->bidiDomain = true;
  315. // Step 1. The first character must be a character with Bidi property L, R, or AL.
  316. // If it has the R or AL property, it is an RTL label
  317. if (1 !== preg_match(Regex::BIDI_STEP_1_RTL, $label)) {
  318. $info->validBidiDomain = false;
  319. return;
  320. }
  321. // Step 2. In an RTL label, only characters with the Bidi properties R, AL, AN, EN, ES,
  322. // CS, ET, ON, BN, or NSM are allowed.
  323. if (1 === preg_match(Regex::BIDI_STEP_2, $label)) {
  324. $info->validBidiDomain = false;
  325. return;
  326. }
  327. // Step 3. In an RTL label, the end of the label must be a character with Bidi property
  328. // R, AL, EN, or AN, followed by zero or more characters with Bidi property NSM.
  329. if (1 !== preg_match(Regex::BIDI_STEP_3, $label)) {
  330. $info->validBidiDomain = false;
  331. return;
  332. }
  333. // Step 4. In an RTL label, if an EN is present, no AN may be present, and vice versa.
  334. if (1 === preg_match(Regex::BIDI_STEP_4_AN, $label) && 1 === preg_match(Regex::BIDI_STEP_4_EN, $label)) {
  335. $info->validBidiDomain = false;
  336. return;
  337. }
  338. return;
  339. }
  340. // We are a LTR label
  341. // Step 1. The first character must be a character with Bidi property L, R, or AL.
  342. // If it has the L property, it is an LTR label.
  343. if (1 !== preg_match(Regex::BIDI_STEP_1_LTR, $label)) {
  344. $info->validBidiDomain = false;
  345. return;
  346. }
  347. // Step 5. In an LTR label, only characters with the Bidi properties L, EN,
  348. // ES, CS, ET, ON, BN, or NSM are allowed.
  349. if (1 === preg_match(Regex::BIDI_STEP_5, $label)) {
  350. $info->validBidiDomain = false;
  351. return;
  352. }
  353. // Step 6.In an LTR label, the end of the label must be a character with Bidi property L or
  354. // EN, followed by zero or more characters with Bidi property NSM.
  355. if (1 !== preg_match(Regex::BIDI_STEP_6, $label)) {
  356. $info->validBidiDomain = false;
  357. return;
  358. }
  359. }
  360. /**
  361. * @param array<int, string> $labels
  362. */
  363. private static function validateDomainAndLabelLength(array $labels, Info $info)
  364. {
  365. $maxDomainSize = self::MAX_DOMAIN_SIZE;
  366. $length = \count($labels);
  367. // Number of "." delimiters.
  368. $domainLength = $length - 1;
  369. // If the last label is empty and it is not the first label, then it is the root label.
  370. // Increase the max size by 1, making it 254, to account for the root label's "."
  371. // delimiter. This also means we don't need to check the last label's length for being too
  372. // long.
  373. if ($length > 1 && '' === $labels[$length - 1]) {
  374. ++$maxDomainSize;
  375. --$length;
  376. }
  377. for ($i = 0; $i < $length; ++$i) {
  378. $bytes = \strlen($labels[$i]);
  379. $domainLength += $bytes;
  380. if ($bytes > self::MAX_LABEL_SIZE) {
  381. $info->errors |= self::ERROR_LABEL_TOO_LONG;
  382. }
  383. }
  384. if ($domainLength > $maxDomainSize) {
  385. $info->errors |= self::ERROR_DOMAIN_NAME_TOO_LONG;
  386. }
  387. }
  388. /**
  389. * @see https://www.unicode.org/reports/tr46/#Validity_Criteria
  390. *
  391. * @param string $label
  392. * @param array<string, bool> $options
  393. * @param bool $canBeEmpty
  394. */
  395. private static function validateLabel($label, Info $info, array $options, $canBeEmpty)
  396. {
  397. if ('' === $label) {
  398. if (!$canBeEmpty && (!isset($options['VerifyDnsLength']) || $options['VerifyDnsLength'])) {
  399. $info->errors |= self::ERROR_EMPTY_LABEL;
  400. }
  401. return;
  402. }
  403. // Step 1. The label must be in Unicode Normalization Form C.
  404. if (!\Normalizer::isNormalized($label, \Normalizer::FORM_C)) {
  405. $info->errors |= self::ERROR_INVALID_ACE_LABEL;
  406. }
  407. $codePoints = self::utf8Decode($label);
  408. if ($options['CheckHyphens']) {
  409. // Step 2. If CheckHyphens, the label must not contain a U+002D HYPHEN-MINUS character
  410. // in both the thrid and fourth positions.
  411. if (isset($codePoints[2], $codePoints[3]) && 0x002D === $codePoints[2] && 0x002D === $codePoints[3]) {
  412. $info->errors |= self::ERROR_HYPHEN_3_4;
  413. }
  414. // Step 3. If CheckHyphens, the label must neither begin nor end with a U+002D
  415. // HYPHEN-MINUS character.
  416. if ('-' === substr($label, 0, 1)) {
  417. $info->errors |= self::ERROR_LEADING_HYPHEN;
  418. }
  419. if ('-' === substr($label, -1, 1)) {
  420. $info->errors |= self::ERROR_TRAILING_HYPHEN;
  421. }
  422. }
  423. // Step 4. The label must not contain a U+002E (.) FULL STOP.
  424. if (false !== strpos($label, '.')) {
  425. $info->errors |= self::ERROR_LABEL_HAS_DOT;
  426. }
  427. // Step 5. The label must not begin with a combining mark, that is: General_Category=Mark.
  428. if (1 === preg_match(Regex::COMBINING_MARK, $label)) {
  429. $info->errors |= self::ERROR_LEADING_COMBINING_MARK;
  430. }
  431. // Step 6. Each code point in the label must only have certain status values according to
  432. // Section 5, IDNA Mapping Table:
  433. $transitional = $options['Transitional_Processing'];
  434. $useSTD3ASCIIRules = $options['UseSTD3ASCIIRules'];
  435. foreach ($codePoints as $codePoint) {
  436. $data = self::lookupCodePointStatus($codePoint, $useSTD3ASCIIRules);
  437. $status = $data['status'];
  438. if ('valid' === $status || (!$transitional && 'deviation' === $status)) {
  439. continue;
  440. }
  441. $info->errors |= self::ERROR_DISALLOWED;
  442. break;
  443. }
  444. // Step 7. If CheckJoiners, the label must satisify the ContextJ rules from Appendix A, in
  445. // The Unicode Code Points and Internationalized Domain Names for Applications (IDNA)
  446. // [IDNA2008].
  447. if ($options['CheckJoiners'] && !self::isValidContextJ($codePoints, $label)) {
  448. $info->errors |= self::ERROR_CONTEXTJ;
  449. }
  450. // Step 8. If CheckBidi, and if the domain name is a Bidi domain name, then the label must
  451. // satisfy all six of the numbered conditions in [IDNA2008] RFC 5893, Section 2.
  452. if ($options['CheckBidi'] && (!$info->bidiDomain || $info->validBidiDomain)) {
  453. self::validateBidiLabel($label, $info);
  454. }
  455. }
  456. /**
  457. * @see https://tools.ietf.org/html/rfc3492#section-6.2
  458. *
  459. * @param string $input
  460. *
  461. * @return string
  462. */
  463. private static function punycodeDecode($input)
  464. {
  465. $n = self::INITIAL_N;
  466. $out = 0;
  467. $i = 0;
  468. $bias = self::INITIAL_BIAS;
  469. $lastDelimIndex = strrpos($input, self::DELIMITER);
  470. $b = false === $lastDelimIndex ? 0 : $lastDelimIndex;
  471. $inputLength = \strlen($input);
  472. $output = [];
  473. $bytes = array_map('ord', str_split($input));
  474. for ($j = 0; $j < $b; ++$j) {
  475. if ($bytes[$j] > 0x7F) {
  476. throw new \Exception('Invalid input');
  477. }
  478. $output[$out++] = $input[$j];
  479. }
  480. if ($b > 0) {
  481. ++$b;
  482. }
  483. for ($in = $b; $in < $inputLength; ++$out) {
  484. $oldi = $i;
  485. $w = 1;
  486. for ($k = self::BASE; /* no condition */; $k += self::BASE) {
  487. if ($in >= $inputLength) {
  488. throw new \Exception('Invalid input');
  489. }
  490. $digit = self::$basicToDigit[$bytes[$in++] & 0xFF];
  491. if ($digit < 0) {
  492. throw new \Exception('Invalid input');
  493. }
  494. if ($digit > intdiv(self::MAX_INT - $i, $w)) {
  495. throw new \Exception('Integer overflow');
  496. }
  497. $i += $digit * $w;
  498. if ($k <= $bias) {
  499. $t = self::TMIN;
  500. } elseif ($k >= $bias + self::TMAX) {
  501. $t = self::TMAX;
  502. } else {
  503. $t = $k - $bias;
  504. }
  505. if ($digit < $t) {
  506. break;
  507. }
  508. $baseMinusT = self::BASE - $t;
  509. if ($w > intdiv(self::MAX_INT, $baseMinusT)) {
  510. throw new \Exception('Integer overflow');
  511. }
  512. $w *= $baseMinusT;
  513. }
  514. $outPlusOne = $out + 1;
  515. $bias = self::adaptBias($i - $oldi, $outPlusOne, 0 === $oldi);
  516. if (intdiv($i, $outPlusOne) > self::MAX_INT - $n) {
  517. throw new \Exception('Integer overflow');
  518. }
  519. $n += intdiv($i, $outPlusOne);
  520. $i %= $outPlusOne;
  521. array_splice($output, $i++, 0, [mb_chr($n, 'utf-8')]);
  522. }
  523. return implode('', $output);
  524. }
  525. /**
  526. * @see https://tools.ietf.org/html/rfc3492#section-6.3
  527. *
  528. * @param string $input
  529. *
  530. * @return string
  531. */
  532. private static function punycodeEncode($input)
  533. {
  534. $n = self::INITIAL_N;
  535. $delta = 0;
  536. $out = 0;
  537. $bias = self::INITIAL_BIAS;
  538. $inputLength = 0;
  539. $output = '';
  540. $iter = self::utf8Decode($input);
  541. foreach ($iter as $codePoint) {
  542. ++$inputLength;
  543. if ($codePoint < 0x80) {
  544. $output .= \chr($codePoint);
  545. ++$out;
  546. }
  547. }
  548. $h = $out;
  549. $b = $out;
  550. if ($b > 0) {
  551. $output .= self::DELIMITER;
  552. ++$out;
  553. }
  554. while ($h < $inputLength) {
  555. $m = self::MAX_INT;
  556. foreach ($iter as $codePoint) {
  557. if ($codePoint >= $n && $codePoint < $m) {
  558. $m = $codePoint;
  559. }
  560. }
  561. if ($m - $n > intdiv(self::MAX_INT - $delta, $h + 1)) {
  562. throw new \Exception('Integer overflow');
  563. }
  564. $delta += ($m - $n) * ($h + 1);
  565. $n = $m;
  566. foreach ($iter as $codePoint) {
  567. if ($codePoint < $n && 0 === ++$delta) {
  568. throw new \Exception('Integer overflow');
  569. }
  570. if ($codePoint === $n) {
  571. $q = $delta;
  572. for ($k = self::BASE; /* no condition */; $k += self::BASE) {
  573. if ($k <= $bias) {
  574. $t = self::TMIN;
  575. } elseif ($k >= $bias + self::TMAX) {
  576. $t = self::TMAX;
  577. } else {
  578. $t = $k - $bias;
  579. }
  580. if ($q < $t) {
  581. break;
  582. }
  583. $qMinusT = $q - $t;
  584. $baseMinusT = self::BASE - $t;
  585. $output .= self::encodeDigit($t + $qMinusT % $baseMinusT, false);
  586. ++$out;
  587. $q = intdiv($qMinusT, $baseMinusT);
  588. }
  589. $output .= self::encodeDigit($q, false);
  590. ++$out;
  591. $bias = self::adaptBias($delta, $h + 1, $h === $b);
  592. $delta = 0;
  593. ++$h;
  594. }
  595. }
  596. ++$delta;
  597. ++$n;
  598. }
  599. return $output;
  600. }
  601. /**
  602. * @see https://tools.ietf.org/html/rfc3492#section-6.1
  603. *
  604. * @param int $delta
  605. * @param int $numPoints
  606. * @param bool $firstTime
  607. *
  608. * @return int
  609. */
  610. private static function adaptBias($delta, $numPoints, $firstTime)
  611. {
  612. // xxx >> 1 is a faster way of doing intdiv(xxx, 2)
  613. $delta = $firstTime ? intdiv($delta, self::DAMP) : $delta >> 1;
  614. $delta += intdiv($delta, $numPoints);
  615. $k = 0;
  616. while ($delta > ((self::BASE - self::TMIN) * self::TMAX) >> 1) {
  617. $delta = intdiv($delta, self::BASE - self::TMIN);
  618. $k += self::BASE;
  619. }
  620. return $k + intdiv((self::BASE - self::TMIN + 1) * $delta, $delta + self::SKEW);
  621. }
  622. /**
  623. * @param int $d
  624. * @param bool $flag
  625. *
  626. * @return string
  627. */
  628. private static function encodeDigit($d, $flag)
  629. {
  630. return \chr($d + 22 + 75 * ($d < 26 ? 1 : 0) - (($flag ? 1 : 0) << 5));
  631. }
  632. /**
  633. * Takes a UTF-8 encoded string and converts it into a series of integer code points. Any
  634. * invalid byte sequences will be replaced by a U+FFFD replacement code point.
  635. *
  636. * @see https://encoding.spec.whatwg.org/#utf-8-decoder
  637. *
  638. * @param string $input
  639. *
  640. * @return array<int, int>
  641. */
  642. private static function utf8Decode($input)
  643. {
  644. $bytesSeen = 0;
  645. $bytesNeeded = 0;
  646. $lowerBoundary = 0x80;
  647. $upperBoundary = 0xBF;
  648. $codePoint = 0;
  649. $codePoints = [];
  650. $length = \strlen($input);
  651. for ($i = 0; $i < $length; ++$i) {
  652. $byte = \ord($input[$i]);
  653. if (0 === $bytesNeeded) {
  654. if ($byte >= 0x00 && $byte <= 0x7F) {
  655. $codePoints[] = $byte;
  656. continue;
  657. }
  658. if ($byte >= 0xC2 && $byte <= 0xDF) {
  659. $bytesNeeded = 1;
  660. $codePoint = $byte & 0x1F;
  661. } elseif ($byte >= 0xE0 && $byte <= 0xEF) {
  662. if (0xE0 === $byte) {
  663. $lowerBoundary = 0xA0;
  664. } elseif (0xED === $byte) {
  665. $upperBoundary = 0x9F;
  666. }
  667. $bytesNeeded = 2;
  668. $codePoint = $byte & 0xF;
  669. } elseif ($byte >= 0xF0 && $byte <= 0xF4) {
  670. if (0xF0 === $byte) {
  671. $lowerBoundary = 0x90;
  672. } elseif (0xF4 === $byte) {
  673. $upperBoundary = 0x8F;
  674. }
  675. $bytesNeeded = 3;
  676. $codePoint = $byte & 0x7;
  677. } else {
  678. $codePoints[] = 0xFFFD;
  679. }
  680. continue;
  681. }
  682. if ($byte < $lowerBoundary || $byte > $upperBoundary) {
  683. $codePoint = 0;
  684. $bytesNeeded = 0;
  685. $bytesSeen = 0;
  686. $lowerBoundary = 0x80;
  687. $upperBoundary = 0xBF;
  688. --$i;
  689. $codePoints[] = 0xFFFD;
  690. continue;
  691. }
  692. $lowerBoundary = 0x80;
  693. $upperBoundary = 0xBF;
  694. $codePoint = ($codePoint << 6) | ($byte & 0x3F);
  695. if (++$bytesSeen !== $bytesNeeded) {
  696. continue;
  697. }
  698. $codePoints[] = $codePoint;
  699. $codePoint = 0;
  700. $bytesNeeded = 0;
  701. $bytesSeen = 0;
  702. }
  703. // String unexpectedly ended, so append a U+FFFD code point.
  704. if (0 !== $bytesNeeded) {
  705. $codePoints[] = 0xFFFD;
  706. }
  707. return $codePoints;
  708. }
  709. /**
  710. * @param int $codePoint
  711. * @param bool $useSTD3ASCIIRules
  712. *
  713. * @return array{status: string, mapping?: string}
  714. */
  715. private static function lookupCodePointStatus($codePoint, $useSTD3ASCIIRules)
  716. {
  717. if (!self::$mappingTableLoaded) {
  718. self::$mappingTableLoaded = true;
  719. self::$mapped = require __DIR__.'/Resources/unidata/mapped.php';
  720. self::$ignored = require __DIR__.'/Resources/unidata/ignored.php';
  721. self::$deviation = require __DIR__.'/Resources/unidata/deviation.php';
  722. self::$disallowed = require __DIR__.'/Resources/unidata/disallowed.php';
  723. self::$disallowed_STD3_mapped = require __DIR__.'/Resources/unidata/disallowed_STD3_mapped.php';
  724. self::$disallowed_STD3_valid = require __DIR__.'/Resources/unidata/disallowed_STD3_valid.php';
  725. }
  726. if (isset(self::$mapped[$codePoint])) {
  727. return ['status' => 'mapped', 'mapping' => self::$mapped[$codePoint]];
  728. }
  729. if (isset(self::$ignored[$codePoint])) {
  730. return ['status' => 'ignored'];
  731. }
  732. if (isset(self::$deviation[$codePoint])) {
  733. return ['status' => 'deviation', 'mapping' => self::$deviation[$codePoint]];
  734. }
  735. if (isset(self::$disallowed[$codePoint]) || DisallowedRanges::inRange($codePoint)) {
  736. return ['status' => 'disallowed'];
  737. }
  738. $isDisallowedMapped = isset(self::$disallowed_STD3_mapped[$codePoint]);
  739. if ($isDisallowedMapped || isset(self::$disallowed_STD3_valid[$codePoint])) {
  740. $status = 'disallowed';
  741. if (!$useSTD3ASCIIRules) {
  742. $status = $isDisallowedMapped ? 'mapped' : 'valid';
  743. }
  744. if ($isDisallowedMapped) {
  745. return ['status' => $status, 'mapping' => self::$disallowed_STD3_mapped[$codePoint]];
  746. }
  747. return ['status' => $status];
  748. }
  749. return ['status' => 'valid'];
  750. }
  751. }