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
This commit is contained in:
Derk-Jan Hartman 2019-09-12 00:12:22 +02:00 committed by TheDJ
parent f379b3f897
commit 9ce26a564d
3 changed files with 13 additions and 4 deletions

View file

@ -122,14 +122,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" ) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View file

@ -1,7 +1,7 @@
<?php
/**
* @todo Could use a test of extended XMP segments. Hard to find programs that
* create example files, and creating my own in vim propbably wouldn't
* create example files, and creating my own in vim probably wouldn't
* serve as a very good "test". (Adobe photoshop probably creates such files
* but it costs money). The implementation of it currently in MediaWiki is based
* solely on reading the standard, without any real world test files.
@ -76,6 +76,12 @@ class JpegMetadataExtractorTest extends MediaWikiIntegrationTestCase {
$this->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' );