Skip to content

Commit 4d4a4ba

Browse files
committed
Improve comparison of num trees by comparing leftover keys if any and logging a warning to indicate specification nonconformance if hanging keys are found
ITXT-CR-260
1 parent c0b0b11 commit 4d4a4ba

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public final class LogMessageConstant {
8484
public static final String MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN = "Make copy of Catalog dictionary is forbidden.";
8585
public static final String NAME_ALREADY_EXISTS_IN_THE_NAME_TREE = "Name \"{0}\" already exists in the name tree; old value will be replaced by the new one.";
8686
public static final String NOT_TAGGED_PAGES_IN_TAGGED_DOCUMENT = "Not tagged pages are copied to the tagged document. Destination document now may contain not tagged content.";
87+
public static final String NUM_TREE_SHALL_NOT_END_WITH_KEY = "Number tree ends with a key which is invalid according to the PDF specification.";
8788
public static final String PDF_READER_CLOSING_FAILED = "PdfReader closing failed due to the error occurred!";
8889
public static final String PDF_WRITER_CLOSING_FAILED = "PdfWriter closing failed due to the error occurred!";
8990
public static final String POPUP_ENTRY_IS_NOT_POPUP_ANNOTATION = "Popup entry in the markup annotations refers not to the annotation with Popup subtype.";

kernel/src/main/java/com/itextpdf/kernel/utils/CompareTool.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.kernel.utils;
4545

46+
import com.itextpdf.io.LogMessageConstant;
4647
import com.itextpdf.io.font.PdfEncodings;
4748
import com.itextpdf.io.util.FileUtil;
4849
import com.itextpdf.io.util.SystemUtil;
@@ -71,6 +72,7 @@ This file is part of the iText (R) project.
7172
import com.itextpdf.kernel.xmp.XMPMetaFactory;
7273
import com.itextpdf.kernel.xmp.XMPUtils;
7374
import com.itextpdf.kernel.xmp.options.SerializeOptions;
75+
import org.slf4j.LoggerFactory;
7476
import org.w3c.dom.Document;
7577
import org.w3c.dom.Element;
7678
import org.w3c.dom.Node;
@@ -1150,8 +1152,26 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
11501152
PdfDictionary cmpNumTree = cmpDict.getAsDictionary(key);
11511153
LinkedList<PdfObject> outItems = new LinkedList<PdfObject>();
11521154
LinkedList<PdfObject> cmpItems = new LinkedList<PdfObject>();
1153-
flattenNumTree(outNumTree, null, outItems);
1154-
flattenNumTree(cmpNumTree, null, cmpItems);
1155+
PdfNumber outLeftover = flattenNumTree(outNumTree, null, outItems);
1156+
PdfNumber cmpLeftover = flattenNumTree(cmpNumTree, null, cmpItems);
1157+
if (outLeftover != null) {
1158+
LoggerFactory.getLogger(CompareTool.class).warn(LogMessageConstant.NUM_TREE_SHALL_NOT_END_WITH_KEY);
1159+
if (compareResult != null && cmpLeftover == null) {
1160+
compareResult.addError(currentPath, "Number tree unexpectedly ends with a key");
1161+
}
1162+
}
1163+
if (cmpLeftover != null) {
1164+
LoggerFactory.getLogger(CompareTool.class).warn(LogMessageConstant.NUM_TREE_SHALL_NOT_END_WITH_KEY);
1165+
if (compareResult != null && outLeftover == null) {
1166+
compareResult.addError(currentPath, "Number tree was expected to end with a key (although it is invalid according to the specification), but ended with a value");
1167+
}
1168+
}
1169+
if (outLeftover != null && cmpLeftover != null && !compareNumbers(outLeftover, cmpLeftover)) {
1170+
if (compareResult != null) {
1171+
compareResult.addError(currentPath, "Number tree was expected to end with a different key (although it is invalid according to the specification)");
1172+
}
1173+
return false;
1174+
}
11551175
PdfArray outArray = new PdfArray(outItems, outItems.size());
11561176
PdfArray cmpArray = new PdfArray(cmpItems, cmpItems.size());
11571177
if (!compareArraysExtended(outArray, cmpArray, currentPath, compareResult))

0 commit comments

Comments
 (0)