From: Derk-Jan Hartman Date: Wed, 11 Sep 2019 22:12:22 +0000 (+0200) Subject: Fix XMP parser errors due to trailing nullchar X-Git-Tag: 1.31.4~11 X-Git-Url: http://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=7668c368fda48d8a8c18ae8e223b12bafd0bd4c6 Fix XMP parser errors due to trailing nullchar JPEG files can have trailing \0 chars at the end of the XMP value. Use trim() to remove these from the string value. Bug: T118799 Change-Id: Id4ab223ef432e5d2c0dd3b4e332320db02422700 (cherry picked from commit 9ce26a564d066a33ba7ae2a6502e3d57e7e4d48b) --- diff --git a/includes/media/JpegMetadataExtractor.php b/includes/media/JpegMetadataExtractor.php index 3c778f36c1..89fc64dc83 100644 --- a/includes/media/JpegMetadataExtractor.php +++ b/includes/media/JpegMetadataExtractor.php @@ -120,14 +120,17 @@ class JpegMetadataExtractor { $temp = self::jpegExtractMarker( $fh ); // check what type of app segment this is. if ( substr( $temp, 0, 29 ) === "http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) { - $segments["XMP"] = substr( $temp, 29 ); + // use trim to remove trailing \0 chars + $segments["XMP"] = trim( substr( $temp, 29 ) ); } elseif ( substr( $temp, 0, 35 ) === "http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) { - $segments["XMP_ext"][] = substr( $temp, 35 ); + // use trim to remove trailing \0 chars + $segments["XMP_ext"][] = trim( substr( $temp, 35 ) ); } elseif ( substr( $temp, 0, 29 ) === "XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) { // Some images (especially flickr images) seem to have this. // I really have no idea what the deal is with them, but // whatever... - $segments["XMP"] = substr( $temp, 29 ); + // use trim to remove trailing \0 chars + $segments["XMP"] = trim( substr( $temp, 29 ) ); wfDebug( __METHOD__ . ' Found XMP section with wrong app identifier ' . "Using anyways.\n" ); } elseif ( substr( $temp, 0, 6 ) === "Exif\0\0" ) { diff --git a/tests/phpunit/data/media/jpeg-xmp-nullchar.jpg b/tests/phpunit/data/media/jpeg-xmp-nullchar.jpg new file mode 100644 index 0000000000..76ae2b486c Binary files /dev/null and b/tests/phpunit/data/media/jpeg-xmp-nullchar.jpg differ diff --git a/tests/phpunit/includes/media/JpegMetadataExtractorTest.php b/tests/phpunit/includes/media/JpegMetadataExtractorTest.php index c943cef906..b745152fe7 100644 --- a/tests/phpunit/includes/media/JpegMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/JpegMetadataExtractorTest.php @@ -1,7 +1,7 @@ assertEquals( $expected, bin2hex( $res['PSIR'][0] ) ); } + public function testXMPExtractionNullChar() { + $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-nullchar.jpg' ); + $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' ); + $this->assertEquals( $expected, $res['XMP'] ); + } + public function testXMPExtractionAltAppId() { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-alt.jpg' ); $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );