'io' ); } $h_src = hash_init( 'sha256' ); $h_dst = hash_init( 'sha256' ); $carry = ''; $first = true; $bytes = 0; $cr = false; // The previous block ended with a CR that is not yet written. $bad = 0; while ( ! feof( $in ) ) { $raw = fread( $in, self::BLOCK ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread if ( false === $raw || '' === $raw ) { break; } hash_update( $h_src, $raw ); $buf = $carry . $raw; $carry = ''; if ( $first ) { $first = false; if ( EncodingDetector::UTF8_BOM === $encoding && 0 === strncmp( $buf, "\xEF\xBB\xBF", 3 ) ) { $buf = substr( $buf, 3 ); } elseif ( EncodingDetector::UTF16LE === $encoding && 0 === strncmp( $buf, "\xFF\xFE", 2 ) ) { $buf = substr( $buf, 2 ); } elseif ( EncodingDetector::UTF8 === $encoding && 0 === strncmp( $buf, "\xEF\xBB\xBF", 3 ) ) { $buf = substr( $buf, 3 ); } } $text = self::convert( $buf, $encoding, feof( $in ), $carry, $bad ); if ( false !== strpos( $text, "\0" ) ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return array( 'error' => 'binary' ); } $text = ( $cr ? "\r" : '' ) . $text; $cr = false; if ( '' !== $text && "\r" === substr( $text, -1 ) ) { $text = substr( $text, 0, -1 ); $cr = true; } $text = (string) preg_replace( '/\r(?!\n)/', "\n", $text ); $bytes += strlen( $text ); hash_update( $h_dst, $text ); if ( ! self::put( $out, $text ) ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return array( 'error' => 'io' ); } } if ( '' !== $carry ) { $unused = ''; $tail = self::convert( $carry, $encoding, true, $unused, $bad ); $tail = ( $cr ? "\r" : '' ) . $tail; $cr = false; $tail = (string) preg_replace( '/\r(?!\n)/', "\n", $tail ); $bytes += strlen( $tail ); hash_update( $h_dst, $tail ); } else { $tail = ''; } if ( $cr ) { ++$bytes; hash_update( $h_dst, "\n" ); $tail .= "\n"; } $written = self::put( $out, $tail ); fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose // A full disk can cut the copy: every write and the final flush must succeed, or the copy is refused. if ( ! fclose( $out ) || ! $written ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return array( 'error' => 'io' ); } return array( 'sha256_src' => hash_final( $h_src ), 'sha256_norm' => hash_final( $h_dst ), 'bytes' => $bytes, 'replaced' => $bad, ); } /** * Write all bytes. * * @param resource $out Stream. * @param string $text Bytes. * @return bool Whether every byte was written. */ private static function put( $out, string $text ): bool { return '' === $text || strlen( $text ) === fwrite( $out, $text ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite } /** * Convert one buffer, leaving an incomplete trailing sequence in $carry. * * @param string $buf Bytes. * @param string $encoding Source encoding. * @param bool $last Whether no more data follows. * @param string $carry Receives bytes that belong to the next block. * @param int $replaced Incremented by the number of invalid UTF-8 bytes replaced with U+FFFD. * @return string UTF-8. */ public static function convert( string $buf, string $encoding, bool $last, string &$carry, int &$replaced = 0 ): string { $carry = ''; switch ( $encoding ) { case EncodingDetector::CP1252: return Cp1252::toUtf8( $buf ); case EncodingDetector::UTF16LE: return self::fromUtf16le( $buf, $last, $carry ); default: return self::scrubUtf8( $buf, $last, $carry, $replaced ); } } /** * UTF-8: cut an incomplete sequence at the end and replace invalid bytes with U+FFFD. * * @param string $buf Bytes. * @param bool $last Last block. * @param string $carry Receives the incomplete tail. * @param int $replaced Incremented by the number of replaced bytes. * @return string */ private static function scrubUtf8( string $buf, bool $last, string &$carry, int &$replaced ): string { $cut = $last ? 0 : EncodingDetector::incompleteTail( $buf ); if ( $cut > 0 ) { $carry = (string) substr( $buf, -$cut ); $buf = (string) substr( $buf, 0, -$cut ); } if ( 1 === preg_match( '//u', $buf ) ) { return $buf; } $pattern = '/(?:[\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})(*SKIP)(*FAIL)|[\x80-\xFF]/s'; $out = (string) preg_replace( $pattern, "\xEF\xBF\xBD", $buf, -1, $n ); $replaced += (int) $n; return $out; } /** * UTF-16LE to UTF-8 (iconv or mbstring when present, pure PHP otherwise). * * @param string $buf Bytes. * @param bool $last Last block. * @param string $carry Receives an odd byte or a lone high surrogate. * @return string */ private static function fromUtf16le( string $buf, bool $last, string &$carry ): string { if ( ! $last && strlen( $buf ) % 2 ) { $carry = (string) substr( $buf, -1 ); $buf = (string) substr( $buf, 0, -1 ); } if ( strlen( $buf ) % 2 ) { $buf = substr( $buf, 0, -1 ); } if ( ! $last && strlen( $buf ) >= 2 ) { $unit = ord( $buf[ strlen( $buf ) - 2 ] ) | ( ord( $buf[ strlen( $buf ) - 1 ] ) << 8 ); if ( $unit >= 0xD800 && $unit <= 0xDBFF ) { $carry = (string) substr( $buf, -2 ) . $carry; $buf = (string) substr( $buf, 0, -2 ); } } if ( '' === $buf ) { return ''; } if ( function_exists( 'mb_convert_encoding' ) ) { return (string) mb_convert_encoding( $buf, 'UTF-8', 'UTF-16LE' ); } if ( function_exists( 'iconv' ) ) { $r = iconv( 'UTF-16LE', 'UTF-8//IGNORE', $buf ); return false === $r ? '' : $r; } $units = array_values( (array) unpack( 'v*', $buf ) ); $out = ''; $n = count( $units ); for ( $i = 0; $i < $n; $i++ ) { $u = $units[ $i ]; if ( $u >= 0xD800 && $u <= 0xDBFF && $i + 1 < $n && $units[ $i + 1 ] >= 0xDC00 && $units[ $i + 1 ] <= 0xDFFF ) { $out .= Cp1252::utf8( 0x10000 + ( ( $u - 0xD800 ) << 10 ) + ( $units[ $i + 1 ] - 0xDC00 ) ); ++$i; } elseif ( $u >= 0xD800 && $u <= 0xDFFF ) { $out .= "\xEF\xBF\xBD"; } else { $out .= Cp1252::utf8( $u ); } } return $out; } }