X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fmedia%2FJpegMetadataExtractor.php;h=9ad40977bcb74d38abb4f6cd2a0ef5da6073dc16;hb=39f0f919c5708f4c672a8eb7e0891f50bf16883e;hp=5069f18045eb6facee3f159d1260cc9b3233c392;hpb=9c092814c7f275db78a6ed769aa76fdd87709c60;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/media/JpegMetadataExtractor.php b/includes/media/JpegMetadataExtractor.php index 5069f18045..229a891db8 100644 --- a/includes/media/JpegMetadataExtractor.php +++ b/includes/media/JpegMetadataExtractor.php @@ -52,11 +52,11 @@ class JpegMetadataExtractor { $segmentCount = 0; - $segments = array( - 'XMP_ext' => array(), - 'COM' => array(), - 'PSIR' => array(), - ); + $segments = [ + 'XMP_ext' => [], + 'COM' => [], + 'PSIR' => [], + ]; if ( !$filename ) { throw new MWException( "No filename specified for " . __METHOD__ ); @@ -82,9 +82,10 @@ class JpegMetadataExtractor { // this is just a sanity check throw new MWException( 'Too many jpeg segments. Aborting' ); } - if ( $buffer !== "\xFF" ) { - throw new MWException( "Error reading jpeg file marker. " . - "Expected 0xFF but got " . bin2hex( $buffer ) ); + while ( $buffer !== "\xFF" ) { + // In theory JPEG files are not allowed to contain anything between the sections, + // but in practice they sometimes do. It's customary to ignore the garbage data. + $buffer = fread( $fh, 1 ); } $buffer = fread( $fh, 1 ); @@ -93,7 +94,6 @@ class JpegMetadataExtractor { $buffer = fread( $fh, 1 ); } if ( $buffer === "\xFE" ) { - // COM section -- file comment // First see if valid utf-8, // if not try to convert it to windows-1252. @@ -102,9 +102,9 @@ class JpegMetadataExtractor { // turns $com to valid utf-8. // thus if no change, its utf-8, otherwise its something else. if ( $com !== $oldCom ) { - MediaWiki\suppressWarnings(); + Wikimedia\suppressWarnings(); $com = $oldCom = iconv( 'windows-1252', 'UTF-8//IGNORE', $oldCom ); - MediaWiki\restoreWarnings(); + Wikimedia\restoreWarnings(); } // Try it again, if its still not a valid string, then probably // binary junk or some really weird encoding, so don't extract. @@ -155,7 +155,7 @@ class JpegMetadataExtractor { } else { // segment we don't care about, so skip $size = wfUnpack( "nint", fread( $fh, 2 ), 2 ); - if ( $size['int'] <= 2 ) { + if ( $size['int'] < 2 ) { throw new MWException( "invalid marker size in jpeg" ); } fseek( $fh, $size['int'] - 2, SEEK_CUR ); @@ -173,9 +173,13 @@ class JpegMetadataExtractor { */ private static function jpegExtractMarker( &$fh ) { $size = wfUnpack( "nint", fread( $fh, 2 ), 2 ); - if ( $size['int'] <= 2 ) { + if ( $size['int'] < 2 ) { throw new MWException( "invalid marker size in jpeg" ); } + if ( $size['int'] === 2 ) { + // fread( ..., 0 ) generates a warning + return ''; + } $segment = fread( $fh, $size['int'] - 2 ); if ( strlen( $segment ) !== $size['int'] - 2 ) { throw new MWException( "Segment shorter than expected" );