Author: ssteiner Date: Fri Sep 21 12:24:47 2018 New Revision: 1841574 URL: http://svn.apache.org/viewvc?rev=1841574&view=rev Log: FOP-2811: PDF larger than 100k pages can have wrong content stream
Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFStreamTestCase.java Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java?rev=1841574&r1=1841573&r2=1841574&view=diff ============================================================================== --- xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java (original) +++ xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java Fri Sep 21 12:24:47 2018 @@ -24,7 +24,8 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; -import java.util.Arrays; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; /** * Class representing a PDF stream. @@ -193,9 +194,19 @@ public class PDFStream extends AbstractP return len; } - public int streamHashCode() throws IOException { + public String streamHashCode() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); outputRawStreamData(bos); - return Arrays.hashCode(bos.toByteArray()); + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] thedigest = md.digest(bos.toByteArray()); + StringBuilder hex = new StringBuilder(); + for (byte b : thedigest) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } catch (NoSuchAlgorithmException e) { + throw new IOException(e); + } } } Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java?rev=1841574&r1=1841573&r2=1841574&view=diff ============================================================================== --- xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java (original) +++ xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java Fri Sep 21 12:24:47 2018 @@ -98,7 +98,7 @@ public class PDFDocumentHandler extends = new PDFDocumentNavigationHandler(this); private Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>(); - private Map<Integer, PDFReference> contents = new HashMap<Integer, PDFReference>(); + private Map<String, PDFReference> contents = new HashMap<String, PDFReference>(); /** * Default constructor. @@ -307,7 +307,7 @@ public class PDFDocumentHandler extends private void setUpContents() throws IOException { PDFStream stream = generator.getStream(); - int hash = stream.streamHashCode(); + String hash = stream.streamHashCode(); if (!contents.containsKey(hash)) { pdfDoc.registerObject(stream); PDFReference ref = new PDFReference(stream); Modified: xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFStreamTestCase.java URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFStreamTestCase.java?rev=1841574&r1=1841573&r2=1841574&view=diff ============================================================================== --- xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFStreamTestCase.java (original) +++ xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFStreamTestCase.java Fri Sep 21 12:24:47 2018 @@ -123,4 +123,30 @@ public class PDFStreamTestCase { stream.write("\nendstream".getBytes("US-ASCII")); return stream.toByteArray(); } + + @Test + public void testHash() throws IOException { + assertFalse(getStreamHash(65025).equals(getStreamHash(127076))); + } + + private String getStreamHash(int i) throws IOException { + PDFStream stream = new PDFStream(); + String txt = "1 0 0 -1 0 790.866 cm\n" + + "q\n" + + "0 g\n" + + "BT\n" + + "/F1 12 Tf\n" + + "1 0 0 -1 0 10.26599979 Tm [(" + i + ")] TJ\n" + + "ET\n"; + String img = "q\n" + + "126.734001 0 0 -38.244999 0 54.294998 cm\n" + + "/Im2 Do\n" + + "Q\n"; + if (i % 2 == 0) { + stream.add(txt + img + "Q\n"); + } else { + stream.add(txt + "Q\n"); + } + return stream.streamHashCode(); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: fop-commits-unsubscr...@xmlgraphics.apache.org For additional commands, e-mail: fop-commits-h...@xmlgraphics.apache.org