Author: tilman Date: Thu Sep 29 17:50:46 2022 New Revision: 1904336 URL: http://svn.apache.org/viewvc?rev=1904336&view=rev Log: PDFBOX-5521: detect and avoid old signatures in the incrementally saved part
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=1904336&r1=1904335&r2=1904336&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Thu Sep 29 17:50:46 2022 @@ -761,7 +761,8 @@ public class COSWriter implements ICOSVi { throw new IOException("Can't write new byteRange '" + byteRange + "' not enough space: byteRange.length(): " + byteRange.length() + - ", byteRangeLength: " + byteRangeLength); + ", byteRangeLength: " + byteRangeLength + + ", byteRangeOffset: " + byteRangeOffset); } // copy the new incremental data into a buffer (e.g. signature dict, trailer) @@ -1038,14 +1039,7 @@ public class COSWriter implements ICOSVi @Override public Object visitFromDictionary(COSDictionary obj) throws IOException { - if (!reachedSignature) - { - COSBase itemType = obj.getItem(COSName.TYPE); - if (COSName.SIG.equals(itemType) || COSName.DOC_TIME_STAMP.equals(itemType)) - { - reachedSignature = true; - } - } + detectPossibleSignature(obj); getStandardOutput().write(DICT_OPEN); getStandardOutput().writeEOL(); for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) @@ -1143,6 +1137,32 @@ public class COSWriter implements ICOSVi return null; } + private void detectPossibleSignature(COSDictionary obj) throws IOException + { + if (!reachedSignature && incrementalUpdate) + { + COSBase itemType = obj.getItem(COSName.TYPE); + if (COSName.SIG.equals(itemType) || COSName.DOC_TIME_STAMP.equals(itemType)) + { + COSArray byteRange = obj.getCOSArray(COSName.BYTERANGE); + if (byteRange != null && byteRange.size() == 4) + { + COSBase base2 = byteRange.get(2); + COSBase base3 = byteRange.get(3); + if (base2 instanceof COSInteger && base3 instanceof COSInteger) + { + long br2 = ((COSInteger) base2).longValue(); + long br3 = ((COSInteger) base3).longValue(); + if (br2 + br3 > incrementalInput.length()) + { + reachedSignature = true; + } + } + } + } + } + } + @Override public Object visitFromDocument(COSDocument doc) throws IOException {