Introduce a binary-safe wrapper for strlen() and use it in seems_utf8(), utf8_uri_encode(), and wp_read_image_metadata().

Use binary-safe POMO_Reader::strlen() in MO::export_to_file_handle().

fixes #28162.

git-svn-id: https://develop.svn.wordpress.org/trunk@28806 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2014-06-23 14:47:08 +00:00
parent b11b6f2f2b
commit 4fd715c937
4 changed files with 27 additions and 7 deletions

View File

@ -290,7 +290,7 @@ function wp_read_image_metadata( $file ) {
$caption = trim( $iptc['2#120'][0] );
if ( empty( $meta['title'] ) ) {
// Assume the title is stored in 2:120 if it's short.
if ( strlen( $caption ) < 80 )
if ( mbstring_binary_safe_strlen( $caption ) < 80 )
$meta['title'] = $caption;
else
$meta['caption'] = $caption;
@ -327,7 +327,7 @@ function wp_read_image_metadata( $file ) {
}
if ( ! empty( $exif['ImageDescription'] ) ) {
if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) {
if ( empty( $meta['title'] ) && mbstring_binary_safe_strlen( $exif['ImageDescription'] ) < 80 ) {
// Assume the title is stored in ImageDescription
$meta['title'] = trim( $exif['ImageDescription'] );
if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) {

View File

@ -481,7 +481,7 @@ function shortcode_unautop( $pee ) {
* @return bool True if $str fits a UTF-8 model, false otherwise.
*/
function seems_utf8($str) {
$length = strlen($str);
$length = mbstring_binary_safe_strlen($str);
for ($i=0; $i < $length; $i++) {
$c = ord($str[$i]);
if ($c < 0x80) $n = 0; # 0bbbbbbb
@ -705,7 +705,7 @@ function utf8_uri_encode( $utf8_string, $length = 0 ) {
$num_octets = 1;
$unicode_length = 0;
$string_length = strlen( $utf8_string );
$string_length = mbstring_binary_safe_strlen( $utf8_string );
for ($i = 0; $i < $string_length; $i++ ) {
$value = ord( $utf8_string[ $i ] );

View File

@ -4404,6 +4404,24 @@ function reset_mbstring_encoding() {
mbstring_binary_safe_encoding( true );
}
/**
* Uses a binary-safe encoding to get the length of a string in bytes if func_overload is enabled.
*
* @see mbstring_binary_safe_encoding()
*
* @since 4.0.0
*
* @param string $string The string to get the length of.
* @return int The length of the string in bytes.
*/
function mbstring_binary_safe_strlen( $string ) {
mbstring_binary_safe_encoding();
$length = strlen( $string );
reset_mbstring_encoding();
return $length;
}
/**
* Alternative to filter_var( $var, FILTER_VALIDATE_BOOLEAN )
*

View File

@ -75,21 +75,23 @@ class MO extends Gettext_Translations {
$current_addr++;
$originals_table = chr(0);
$reader = new POMO_Reader();
foreach($entries as $entry) {
$originals_table .= $this->export_original($entry) . chr(0);
$length = strlen($this->export_original($entry));
$length = $reader->strlen($this->export_original($entry));
fwrite($fh, pack('VV', $length, $current_addr));
$current_addr += $length + 1; // account for the NULL byte after
}
$exported_headers = $this->export_headers();
fwrite($fh, pack('VV', strlen($exported_headers), $current_addr));
fwrite($fh, pack('VV', $reader->strlen($exported_headers), $current_addr));
$current_addr += strlen($exported_headers) + 1;
$translations_table = $exported_headers . chr(0);
foreach($entries as $entry) {
$translations_table .= $this->export_translations($entry) . chr(0);
$length = strlen($this->export_translations($entry));
$length = $reader->strlen($this->export_translations($entry));
fwrite($fh, pack('VV', $length, $current_addr));
$current_addr += $length + 1;
}