This is an automated email from the ASF dual-hosted git repository. pjfanning pushed a commit to branch 6.0.x in repository https://gitbox.apache.org/repos/asf/poi.git
commit dd5f7f7bb3f01fb8d519da54003e9286e0f97017 Author: PJ Fanning <[email protected]> AuthorDate: Mon Oct 26 18:25:08 2026 +0000 XSLF findPictureData: read each picture part at most once (#1490) Follow-up to #1486. When a picture's checksum was not cached yet, getChecksum() read the part to compute it and the byte compare then read it a second time. XSLFPictureData.hasData now computes the CRC from the bytes it already read, and no longer parses the image header just to get the checksum. The collision test also checks that real duplicates are still reused past a colliding entry, and that lookups work on a reloaded deck where no checksum is cached. Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit ecfa4e668e5d62523c6cd9c10e1564708f68c75e) --- .../apache/poi/xslf/usermodel/XMLSlideShow.java | 9 +------- .../apache/poi/xslf/usermodel/XSLFPictureData.java | 25 ++++++++++++++++++++++ .../poi/xslf/usermodel/TestXSLFSlideShow.java | 16 ++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java index 8b711446f7..498282aae6 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java @@ -26,7 +26,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -53,8 +52,6 @@ import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.util.Beta; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.MathUtil; import org.apache.poi.util.NotImplemented; import org.apache.poi.util.Units; @@ -650,12 +647,8 @@ public class XMLSlideShow extends POIXMLDocument @Override public XSLFPictureData findPictureData(byte[] pictureData) { long checksum = IOUtils.calculateChecksum(pictureData); - byte[] cs = new byte[LittleEndianConsts.LONG_SIZE]; - LittleEndian.putLong(cs, 0, checksum); - for (XSLFPictureData pic : getPictureData()) { - // the checksum is only a CRC32, so compare the data as well - if (Arrays.equals(pic.getChecksum(), cs) && Arrays.equals(pic.getData(), pictureData)) { + if (pic.hasData(pictureData, checksum)) { return pic; } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java index 77b421660d..3b1a4a07d7 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java @@ -23,6 +23,7 @@ import java.awt.Dimension; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLException; @@ -153,6 +154,30 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture ); } + /** + * Checks whether this picture holds the given data. The checksum is only a CRC32, + * so a matching checksum is confirmed by comparing the data. The part is read at + * most once. + * + * @param data the picture data to compare with + * @param dataChecksum the checksum of {@code data} + * @return true, if this picture holds the same bytes as {@code data} + */ + boolean hasData(byte[] data, long dataChecksum) { + byte[] ownData = null; + if (checksum == null) { + ownData = getData(); + checksum = IOUtils.calculateChecksum(ownData); + } + if (checksum != dataChecksum) { + return false; + } + if (ownData == null) { + ownData = getData(); + } + return Arrays.equals(ownData, data); + } + /** * Determine and cache image properties */ diff --git a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFSlideShow.java b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFSlideShow.java index 27b45f5afa..07054933db 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFSlideShow.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFSlideShow.java @@ -440,6 +440,22 @@ class TestXSLFSlideShow { assertNotSame(pic1, pic2); assertArrayEquals(data2, pic2.getData()); assertEquals(2, ppt.getPictureData().size()); + + // real duplicates are still reused, also past a colliding entry + assertSame(pic1, ppt.addPicture(data1, PictureData.PictureType.PNG)); + assertSame(pic2, ppt.addPicture(data2, PictureData.PictureType.PNG)); + assertEquals(2, ppt.getPictureData().size()); + + // pictures loaded from a file have no cached checksum yet + try (XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt)) { + XSLFPictureData readPic1 = ppt2.findPictureData(data1); + XSLFPictureData readPic2 = ppt2.findPictureData(data2); + assertNotNull(readPic1); + assertNotNull(readPic2); + assertNotSame(readPic1, readPic2); + assertArrayEquals(data1, readPic1.getData()); + assertArrayEquals(data2, readPic2.getData()); + } } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
