$word) { if ($word !== 0) { $start = -1; continue; } if ($start === -1) { $start = $i; } if ($i - $start + 1 > $bestLen) { $bestStart = $start; $bestLen = $i - $start + 1; } } if ($bestLen < 2) { $bestLen = 0; } // RFC 5952 section 5: embedded IPv4 notation for IPv4-mapped // (::ffff:0:0/96) and IPv4-compatible (::/96) addresses, the same // condition BIND-derived inet_ntop() and curl use. bestStart must be // zero: a five or six field zero run elsewhere is not an IPv4 prefix. $mixed = $bestStart === 0 && ($bestLen === 6 || ($bestLen === 5 && $words[5] === 0xFFFF)); $groups = []; for ($i = 0, $n = $mixed ? 6 : 8; $i < $n; ++$i) { $groups[] = dechex($words[$i]); } if ($mixed) { $groups[] = sprintf( '%d.%d.%d.%d', $words[6] >> 8, $words[6] & 0xFF, $words[7] >> 8, $words[7] & 0xFF ); } if ($bestLen === 0) { return implode(':', $groups); } return implode(':', array_slice($groups, 0, $bestStart)).'::'.implode(':', array_slice($groups, $bestStart + $bestLen)); } private static function hasValidHostPercentEncoding(string $host): bool { // Mirror of the raw reg-name policy above for percent-encoded octets: // reject malformed sequences (RFC 3986 requires "%" HEXDIG HEXDIG) and // octets that decode to bytes the raw grammar rejects - C0 controls, // SP, DEL, the delimiters / ? # @ \ [ ], the port colon, and % itself. // Octets decoding to any other byte (unreserved, sub-delims, and // non-ASCII UTF-8 data) remain accepted. $invalidEncoding = preg_match( '/%(?!'.self::HEX_OCTET.')|%(?:[01][0-9A-Fa-f]|2[035F]|3[AF]|40|5[BCD]|7F)/i', $host ); return $invalidEncoding === 0; } private static function isValidIpLiteralHost(string $host): bool { if (!str_starts_with($host, '[') || !str_ends_with($host, ']')) { return false; } $address = substr($host, 1, -1); if (\filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false) { return true; } // RFC 6874 IPv6 zone identifiers are intentionally not supported here. // Bracketed hosts are validated as IPv6 or IPvFuture only. return preg_match('/^v[0-9a-f]+\.['.self::CHAR_UNRESERVED.self::CHAR_SUB_DELIMS.':]+$/iD', $address) === 1; } /** * Parses a textual IPv6 address into its eight 16-bit words, or null * when the text is not a structurally valid address. * * The grammar enforced here is the RFC 3986 `IPv6address` rule: one to four * hexadecimal digits per field, at most one `::` eliding one or more zero * fields, and an optional dotted-decimal tail of four octets (0-255, no * leading zeros) as the final 32 bits. FILTER_VALIDATE_IP accepts exactly * this grammar, so the filter guard in tryCanonicalizeIpv6() and this * parser always agree and the null paths here can only fail closed. * * @return list|null */ private static function parseIpv6Words(string $address): ?array { // A dotted-decimal tail is only valid as the final 32 bits, after the // final colon. Rewrite it into its two hexadecimal fields so the // remainder of the parse handles hexadecimal fields only. $dot = strpos($address, '.'); if ($dot !== false) { $colon = strrpos($address, ':'); if ($colon === false || $colon > $dot) { return null; } $octets = explode('.', substr($address, $colon + 1)); if (count($octets) !== 4) { return null; } $bytes = []; foreach ($octets as $octet) { if ($octet === '' || strlen($octet) > 3 || !ctype_digit($octet)) { return null; } if ($octet[0] === '0' && $octet !== '0') { return null; } $byte = (int) $octet; if ($byte > 255) { return null; } $bytes[] = $byte; } $address = substr($address, 0, $colon + 1) .dechex(($bytes[0] << 8) | $bytes[1]) .':' .dechex(($bytes[2] << 8) | $bytes[3]); } $halves = explode('::', $address); if (count($halves) > 2) { return null; } $head = self::parseHexFields($halves[0]); if ($head === null) { return null; } if (count($halves) === 1) { return count($head) === 8 ? $head : null; } $tail = self::parseHexFields($halves[1]); if ($tail === null) { return null; } // The "::" must elide at least one zero field. $elided = 8 - count($head) - count($tail); if ($elided < 1) { return null; } return array_merge($head, array_fill(0, $elided, 0), $tail); } /** * Parses a colon-separated run of 16-bit hexadecimal fields, or null * when a field is empty, longer than four digits, or not hexadecimal. * * @return list|null */ private static function parseHexFields(string $fields): ?array { if ($fields === '') { return []; } $words = []; foreach (explode(':', $fields) as $field) { if ($field === '' || strlen($field) > 4 || !ctype_xdigit($field)) { return null; } $words[] = intval($field, 16); } return $words; } }