|null Code points, or null when the bytes are not well-formed. */ public static function decode( string $bytes ): ?array { return self::walk( $bytes, true ); } /** * The same rule as `decode`, deciding it without keeping the answer. * * This is the one UTF-8 check that runs on bytes nobody has authenticated * yet: `Grant\ExecuteBody::parse` calls it on the raw request body before the * grant over that body has been verified. Deciding validity by building the * decoded array and discarding it made an anonymous request buy a PHP array * entry for every ASCII byte it sent — sixteen bytes of heap per byte on the * wire. A 4 MiB body reached a 200 MB peak and exhausted a 96 MB * `memory_limit` outright, at the line below, before anything had been * verified. The bound in `Grant\ExecuteBody` now caps what can arrive here; * this is why what arrives costs what it weighs. */ public static function isWellFormed( string $bytes ): bool { return null !== self::walk( $bytes, false ); } /** * The rule itself, written once. * * `decode` and `isWellFormed` are the same walk over the same bytes and * differ only in whether the code points are kept, so they share one walker * rather than one each. A second copy of these branches would be a second * chance to disagree with `TextDecoder` about which byte sequences exist, * which is the divergence this whole file is here to prevent — and it would * be the copy nothing decodes with that drifted, since only the decoding one * is exercised by the canonicalization corpus. * * @param bool $collect Whether to materialise the code points. * @return array|null The code points when `$collect`, an empty array * when not, and null when the bytes are not * well-formed. */ private static function walk( string $bytes, bool $collect ): ?array { $out = array(); $length = strlen( $bytes ); $i = 0; while ( $i < $length ) { $b = ord( $bytes[ $i ] ); if ( $b < 0x80 ) { if ( $collect ) { $out[] = $b; } ++$i; continue; } if ( $b >= 0xC2 && $b <= 0xDF ) { $need = 1; $cp = $b & 0x1F; } elseif ( $b >= 0xE0 && $b <= 0xEF ) { $need = 2; $cp = $b & 0x0F; } elseif ( $b >= 0xF0 && $b <= 0xF4 ) { $need = 3; $cp = $b & 0x07; } else { // 0x80-0xC1 is either a stray continuation byte or an overlong lead. return null; } if ( $i + $need > $length - 1 ) { return null; } for ( $k = 1; $k <= $need; ++$k ) { $c = ord( $bytes[ $i + $k ] ); if ( $c < 0x80 || $c > 0xBF ) { return null; } $cp = ( $cp << 6 ) | ( $c & 0x3F ); } // Overlong, surrogate and out-of-range forms, rejected by value. if ( 2 === $need && $cp < 0x800 ) { return null; } if ( 3 === $need && $cp < 0x10000 ) { return null; } if ( $cp >= 0xD800 && $cp <= 0xDFFF ) { return null; } if ( $cp > 0x10FFFF ) { return null; } if ( $collect ) { $out[] = $cp; } $i += $need + 1; } return $out; } /** Encodes one code point. Surrogates are the caller's problem; see Domain. */ public static function encodeCodePoint( int $cp ): string { if ( $cp < 0x80 ) { return chr( $cp ); } if ( $cp < 0x800 ) { return chr( 0xC0 | ( $cp >> 6 ) ) . chr( 0x80 | ( $cp & 0x3F ) ); } if ( $cp < 0x10000 ) { return chr( 0xE0 | ( $cp >> 12 ) ) . chr( 0x80 | ( ( $cp >> 6 ) & 0x3F ) ) . chr( 0x80 | ( $cp & 0x3F ) ); } return chr( 0xF0 | ( $cp >> 18 ) ) . chr( 0x80 | ( ( $cp >> 12 ) & 0x3F ) ) . chr( 0x80 | ( ( $cp >> 6 ) & 0x3F ) ) . chr( 0x80 | ( $cp & 0x3F ) ); } /** * Ordering keys is where byte order and UTF-16 order part company, and the * profile says UTF-16 code unit. * * UTF-8 byte order is code-point order, so it sorts an astral character * after U+E000..U+FFFF; UTF-16 sorts it before, because its first code unit * is a high surrogate in D800..DBFF. Comparing the UTF-16BE encodings byte * for byte is exactly "sort by UTF-16 code unit", which is what a JavaScript * string comparison does. */ public static function toUtf16Be( string $utf8 ): string { $points = self::decode( $utf8 ); if ( null === $points ) { // Only reachable for a string that never came through Domain. return $utf8; } $out = ''; foreach ( $points as $cp ) { if ( $cp < 0x10000 ) { $out .= chr( $cp >> 8 ) . chr( $cp & 0xFF ); continue; } $v = $cp - 0x10000; $high = 0xD800 + ( $v >> 10 ); $low = 0xDC00 + ( $v & 0x3FF ); $out .= chr( $high >> 8 ) . chr( $high & 0xFF ) . chr( $low >> 8 ) . chr( $low & 0xFF ); } return $out; } /** * NFC, section 7.4. * * `ext-intl` is neither a WordPress requirement nor present on many shared * hosts, so the plugin ships symfony/polyfill-intl-normalizer through the * Jetpack autoloader and the class is available either way. Skipping * normalisation where the extension is missing would make the same site * accept or reject the same grant depending on its PHP build, which is worse * than either answer. */ public static function nfc( string $s ): string { $normalized = \Normalizer::normalize( $s, \Normalizer::FORM_C ); return false === $normalized ? $s : $normalized; } }