From 937285f4dd9f74126dd86576df94244e9653bf7e Mon Sep 17 00:00:00 2001 From: Stefan Oltmann Date: Mon, 10 Jul 2023 15:02:34 +0200 Subject: [PATCH] FIX: Trim ISO characters in the input (#4) Fix: Robustness for ISO control characters at beginning or end of the XMP. Added some to the test data. --- .../kotlin/com/ashampoo/xmp/impl/DomParser.kt | 14 ++++- .../resources/com/ashampoo/xmp/sample_2.xmp | Bin 4806 -> 4817 bytes .../resources/com/ashampoo/xmp/sample_3.xmp | Bin 28131 -> 25740 bytes .../resources/com/ashampoo/xmp/sample_4.xmp | 51 +++++++++--------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/commonMain/kotlin/com/ashampoo/xmp/impl/DomParser.kt b/src/commonMain/kotlin/com/ashampoo/xmp/impl/DomParser.kt index 2ea5ab2..8cf9714 100644 --- a/src/commonMain/kotlin/com/ashampoo/xmp/impl/DomParser.kt +++ b/src/commonMain/kotlin/com/ashampoo/xmp/impl/DomParser.kt @@ -12,11 +12,23 @@ object DomParser { fun parseDocumentFromString(input: String): Document { + /* + * We encountered a situation where the XMP had NUL characters at the end + * for unknown reasons. This caused an exception in the parser. + * The test images IMG_0001.jpg and IMG_0002.jpg on the iOS simulator + * exhibited this issue, indicating that it could occur in real-world + * scenarios as well. To address this, we now trim all whitespaces and + * ISO control characters from the XMP to ensure its proper parsing. + */ + val trimmedInput = input.trim { + it.isWhitespace() || it.isISOControl() + } + try { val writer = DomWriter() - val reader = XmlStreaming.newReader(input) + val reader = XmlStreaming.newReader(trimmedInput) do { val event = reader.next() diff --git a/src/commonTest/resources/com/ashampoo/xmp/sample_2.xmp b/src/commonTest/resources/com/ashampoo/xmp/sample_2.xmp index 0fbf2e59716438dda1b01fc8aacffb86adc715f4..00310050598b0403f891401cfec7ec02935ed0e4 100644 GIT binary patch delta 20 WcmX@6dQp{;Yr`=iE(QfC?lO&9u zq|ekWbMrl1hPVF1Q*lGkPbh-^E`{t1D`DRug7*(2I=zaNpH?EHk@w%L(0q9%;BSc3 z{R4t4WmggLvr1&TwM_6C3c;_UU>^{|{ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + +