diff --git a/src/main/java/org/verapdf/cos/COSString.java b/src/main/java/org/verapdf/cos/COSString.java index 94940cf7..5b2df8d9 100644 --- a/src/main/java/org/verapdf/cos/COSString.java +++ b/src/main/java/org/verapdf/cos/COSString.java @@ -30,7 +30,6 @@ import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; -import java.io.ByteArrayOutputStream; /** * @author Timur Kamalov @@ -365,6 +364,19 @@ public void setHexCount(long hexCount) { this.hexCount = hexCount; } + public static int compare(COSString first, COSString second) { + byte[] firstBytes = first.get(); + byte[] secondBytes = second.get(); + int commonLength = Math.min(firstBytes.length, secondBytes.length); + for (int i = 0; i < commonLength; i++) { + int comparison = Integer.compare(Byte.toUnsignedInt(firstBytes[i]), Byte.toUnsignedInt(secondBytes[i])); + if (comparison != 0) { + return comparison; + } + } + return Integer.compare(firstBytes.length, secondBytes.length); + } + @Override public boolean isEquivalentTo(COSBase o) { if (this == o) return true; diff --git a/src/main/java/org/verapdf/pd/PDAnnotation.java b/src/main/java/org/verapdf/pd/PDAnnotation.java index 07e3a817..54e78e12 100644 --- a/src/main/java/org/verapdf/pd/PDAnnotation.java +++ b/src/main/java/org/verapdf/pd/PDAnnotation.java @@ -24,6 +24,7 @@ import org.verapdf.cos.COSKey; import org.verapdf.cos.COSObjType; import org.verapdf.cos.COSObject; +import org.verapdf.cos.COSString; import org.verapdf.exceptions.LoopedException; import org.verapdf.pd.actions.PDAction; import org.verapdf.pd.actions.PDAnnotationAdditionalActions; @@ -325,7 +326,7 @@ public static COSObject getPageFromDestination(COSObject destination, ASAtom ke } PDNameTreeNode dests = namesDictionary.getDests(); if (dests != null) { - destination = dests.getObject(destination.getString()); + destination = dests.getObject((COSString) destination.getDirectBase()); if (destination == null) { return null; } diff --git a/src/main/java/org/verapdf/pd/PDDestination.java b/src/main/java/org/verapdf/pd/PDDestination.java index b4a05301..bcae99df 100644 --- a/src/main/java/org/verapdf/pd/PDDestination.java +++ b/src/main/java/org/verapdf/pd/PDDestination.java @@ -23,6 +23,7 @@ import org.verapdf.as.ASAtom; import org.verapdf.cos.COSObjType; import org.verapdf.cos.COSObject; +import org.verapdf.cos.COSString; import org.verapdf.tools.StaticResources; import java.util.logging.Level; @@ -48,7 +49,7 @@ public Boolean getIsStructDestination() { } PDNameTreeNode dests = namesDictionary.getDests(); if (dests != null) { - COSObject dest = dests.getObject(destination.getString()); + COSObject dest = dests.getObject((COSString) destination.getDirectBase()); if (dest == null) { LOGGER.log(Level.WARNING, "Named destination " + destination.getString() + " not found in the Dests name tree in the Names dictionary"); diff --git a/src/main/java/org/verapdf/pd/PDNameTreeNode.java b/src/main/java/org/verapdf/pd/PDNameTreeNode.java index e0d1981b..53f0dc93 100644 --- a/src/main/java/org/verapdf/pd/PDNameTreeNode.java +++ b/src/main/java/org/verapdf/pd/PDNameTreeNode.java @@ -21,10 +21,7 @@ package org.verapdf.pd; import org.verapdf.as.ASAtom; -import org.verapdf.cos.COSArray; -import org.verapdf.cos.COSKey; -import org.verapdf.cos.COSObjType; -import org.verapdf.cos.COSObject; +import org.verapdf.cos.*; import org.verapdf.exceptions.LoopedException; import java.util.*; @@ -36,9 +33,9 @@ public class PDNameTreeNode extends PDObject implements Iterable { private final Set parents; - private String[] limitsArray = null; + private COSString[] limitsArray = null; private List kids = null; - private Map names = null; + private Map names = null; private PDNameTreeNode(COSObject obj, Set parents) { super(obj); @@ -81,23 +78,26 @@ private List parseKids() { return Collections.emptyList(); } - public Map getNames() { + public Map getNames() { if (this.names == null) { this.names = parseNames(); } return Collections.unmodifiableMap(this.names); } - private Map parseNames() { + private Map parseNames() { COSObject names = getKey(ASAtom.NAMES); if (names != null && names.getType() == COSObjType.COS_ARRAY) { - Map res = new LinkedHashMap<>(); + // COSString.equals also compares hex flags, so name keys are matched by their bytes + Map res = new TreeMap<>(COSString::compare); for (int i = 0; i < names.size(); i += 2) { COSObject keyObj = names.at(i); - String key = keyObj == null ? null : keyObj.getString(); - if (key != null) { - COSObject value = names.at(i + 1); - res.put(key, value); + if (keyObj.getType() == COSObjType.COS_STRING) { + COSString key = (COSString) keyObj.getDirectBase(); + if (key != null) { + COSObject value = names.at(i + 1); + res.put(key, value); + } } } return res; @@ -105,36 +105,36 @@ private Map parseNames() { return Collections.emptyMap(); } - public String[] getLimitsArray() { + public COSString[] getLimitsArray() { if (limitsArray == null) { limitsArray = parseLimitsArray(); } return limitsArray; } - public String[] parseLimitsArray() { + public COSString[] parseLimitsArray() { COSObject limits = this.getKey(ASAtom.LIMITS); if (limits != null && !limits.empty() && limits.getType() == COSObjType.COS_ARRAY && limits.size() >= 2 && limits.at(0).getType() == COSObjType.COS_STRING && limits.at(1).getType() == COSObjType.COS_STRING) { - String[] res = new String[2]; - res[0] = limits.at(0).getString(); - res[1] = limits.at(1).getString(); + COSString[] res = new COSString[2]; + res[0] = (COSString) limits.at(0).getDirectBase(); + res[1] = (COSString) limits.at(1).getDirectBase(); return res; } - return new String[0]; + return new COSString[0]; } - public COSObject getObject(String key) { - String[] limits = this.getLimitsArray(); - if (limits.length == 2 && (key.compareTo(limits[0]) < 0 || key.compareTo(limits[1]) > 0)) { + public COSObject getObject(COSString key) { + COSString[] limits = this.getLimitsArray(); + if (limits.length == 2 && (COSString.compare(key, limits[0]) < 0 || COSString.compare(key, limits[1]) > 0)) { // string not in the limits return null; } if (this.knownKey(ASAtom.NAMES)) { // just get object from names or check if it is not in names - Map names = getNames(); + Map names = getNames(); return names == null ? null : names.get(key); } @@ -175,7 +175,7 @@ public Long size() { return i; } - public boolean containsKey(String key) { + public boolean containsKey(COSString key) { return getObject(key) != null; } diff --git a/src/test/java/org/verapdf/pd/PDNameTreeNodeTest.java b/src/test/java/org/verapdf/pd/PDNameTreeNodeTest.java new file mode 100644 index 00000000..cbe4154b --- /dev/null +++ b/src/test/java/org/verapdf/pd/PDNameTreeNodeTest.java @@ -0,0 +1,212 @@ +/* + * This file is part of veraPDF Parser, a module of the veraPDF project. + * Copyright (c) 2015-2026, veraPDF Consortium + * All rights reserved. + * + * veraPDF Parser is free software: you can redistribute it and/or modify + * it under the terms of either: + * + * The GNU General public license GPLv3+. + * You should have received a copy of the GNU General Public License + * along with veraPDF Parser as the LICENSE.GPL file in the root of the source + * tree. If not, see http://www.gnu.org/licenses/ or + * https://www.gnu.org/licenses/gpl-3.0.en.html. + * + * The Mozilla Public License MPLv2+. + * You should have received a copy of the Mozilla Public License along with + * veraPDF Parser as the LICENSE.MPL file in the root of the source tree. + * If a copy of the MPL was not distributed with this file, you can obtain one at + * http://mozilla.org/MPL/2.0/. + */ +package org.verapdf.pd; + +import org.junit.Test; +import org.verapdf.as.ASAtom; +import org.verapdf.cos.COSArray; +import org.verapdf.cos.COSDictionary; +import org.verapdf.cos.COSInteger; +import org.verapdf.cos.COSObject; +import org.verapdf.cos.COSString; + +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class PDNameTreeNodeTest { + + @Test + public void returnsValueForEachKeyInNames() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + array(literal("Alpha"), literal("Gamma")), + array(literal("Alpha"), integer(1), literal("Beta"), integer(2), literal("Gamma"), integer(3)), + null)); + + assertValue(1, node.getObject(key("Alpha"))); + assertValue(2, node.getObject(key("Beta"))); + assertValue(3, node.getObject(key("Gamma"))); + } + + @Test + public void searchesKeysEqualToLimits() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + array(literal("Alpha"), literal("Gamma")), + array(literal("Alpha"), integer(1), literal("Gamma"), integer(3)), + null)); + + assertValue(1, node.getObject(key("Alpha"))); + assertValue(3, node.getObject(key("Gamma"))); + } + + @Test + public void returnsNullForKeyMissingWithinLimits() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + array(literal("Alpha"), literal("Gamma")), + array(literal("Alpha"), integer(1), literal("Gamma"), integer(3)), + null)); + + assertNull(node.getObject(key("Bravo"))); + } + + @Test + public void returnsNullForKeysOutsideLimits() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + array(literal("Beta"), literal("Gamma")), + array(literal("Beta"), integer(2), literal("Gamma"), integer(3)), + null)); + + assertNull(node.getObject(key("Alpha"))); + assertNull(node.getObject(key("Zulu"))); + } + + @Test + public void findsKeyAcrossLiteralAndHexEncoding() { + PDNameTreeNode literalNode = PDNameTreeNode.create(treeNode( + null, array(literal("Beta"), integer(2)), null)); + PDNameTreeNode hexNode = PDNameTreeNode.create(treeNode( + null, array(hex("Beta"), integer(2)), null)); + + assertValue(2, literalNode.getObject(hexKey("Beta"))); + assertValue(2, hexNode.getObject(key("Beta"))); + } + + @Test + public void comparesKeysAsUnsignedBytes() { + // Byte 0xE9 is negative as a signed byte, so this checks that keys compare as unsigned bytes + byte[] highKey = {(byte) 0xE9}; + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + array(literal("Alpha"), COSString.construct(highKey, false)), + array(literal("Alpha"), integer(1), COSString.construct(highKey, false), integer(2)), + null)); + + assertValue(2, node.getObject(new COSString(highKey, false))); + } + + @Test + public void searchesNamesWhenNodeHasNoLimits() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + null, array(literal("Alpha"), integer(1), literal("Beta"), integer(2)), null)); + + assertValue(1, node.getObject(key("Alpha"))); + assertValue(2, node.getObject(key("Beta"))); + assertNull(node.getObject(key("Zulu"))); + } + + @Test + public void returnsNullWhenNodeHasNoNamesOrKids() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + array(literal("Alpha"), literal("Gamma")), null, null)); + + assertNull(node.getObject(key("Beta"))); + } + + @Test + public void findsStringKeysAfterNonStringKeyInNames() { + PDNameTreeNode node = PDNameTreeNode.create(treeNode( + null, array(integer(7), integer(10), literal("Beta"), integer(2)), null)); + + assertValue(2, node.getObject(key("Beta"))); + } + + @Test + public void searchesKidsForKey() { + PDNameTreeNode root = PDNameTreeNode.create(twoLeafTree()); + + assertValue(1, root.getObject(key("Alpha"))); + assertValue(2, root.getObject(key("Beta"))); + assertValue(3, root.getObject(key("Gamma"))); + assertValue(4, root.getObject(key("Delta"))); + } + + @Test + public void returnsNullWhenKeyIsNotInAnyKid() { + PDNameTreeNode root = PDNameTreeNode.create(twoLeafTree()); + + // Bravo lies inside the root limits but in the gap between the two kids + assertNull(root.getObject(key("Bravo"))); + assertNull(root.getObject(key("Zulu"))); + } + + private static COSObject twoLeafTree() { + return treeNode( + array(literal("Alpha"), literal("Gamma")), + null, + array( + treeNode(array(literal("Alpha"), literal("Beta")), + array(literal("Alpha"), integer(1), literal("Beta"), integer(2)), null), + treeNode(array(literal("Delta"), literal("Gamma")), + array(literal("Gamma"), integer(3), literal("Delta"), integer(4)), null))); + } + + private static COSObject treeNode(COSObject limits, COSObject names, COSObject kids) { + COSObject tree = COSDictionary.construct(); + if (limits != null) { + tree.setKey(ASAtom.LIMITS, limits); + } + if (names != null) { + tree.setKey(ASAtom.NAMES, names); + } + if (kids != null) { + tree.setKey(ASAtom.KIDS, kids); + } + return tree; + } + + private static COSObject array(COSObject... items) { + COSObject result = COSArray.construct(); + for (COSObject item : items) { + result.add(item); + } + return result; + } + + private static COSObject literal(String value) { + return COSString.construct(bytes(value), false); + } + + private static COSObject hex(String value) { + return COSString.construct(bytes(value), true); + } + + private static COSObject integer(long value) { + return COSInteger.construct(value); + } + + private static COSString key(String value) { + return new COSString(bytes(value), false); + } + + private static COSString hexKey(String value) { + return new COSString(bytes(value), true); + } + + private static byte[] bytes(String value) { + return value.getBytes(StandardCharsets.ISO_8859_1); + } + + private static void assertValue(long expected, COSObject actual) { + assertNotNull(actual); + assertEquals(Long.valueOf(expected), actual.getInteger()); + } +}