You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2021/05/02 12:59:32 UTC
svn commit: r1889404 - in
/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox:
cos/COSObjectKey.java pdfparser/COSParser.java
Author: lehmi
Date: Sun May 2 12:59:32 2021
New Revision: 1889404
URL: http://svn.apache.org/viewvc?rev=1889404&view=rev
Log:
PDFBOX-5151: deprecate fixGeneration to ensure an immutable hashcode as proposed by Roland Meub
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSObjectKey.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSObjectKey.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSObjectKey.java?rev=1889404&r1=1889403&r2=1889404&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSObjectKey.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSObjectKey.java Sun May 2 12:59:32 2021
@@ -75,6 +75,8 @@ public class COSObjectKey implements Com
* This will set the generation number. It is intended for fixes only.
*
* @param genNumber the new generation number.
+ *
+ * @deprecated will be removed in the next major release
*/
public void fixGeneration(int genNumber)
{
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1889404&r1=1889403&r2=1889404&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java Sun May 2 12:59:32 2021
@@ -1447,20 +1447,34 @@ public class COSParser extends BaseParse
{
return true;
}
+ Map<COSObjectKey, COSObjectKey> correctedKeys = new HashMap<COSObjectKey, COSObjectKey>();
for (Entry<COSObjectKey, Long> objectEntry : xrefOffset.entrySet())
{
COSObjectKey objectKey = objectEntry.getKey();
Long objectOffset = objectEntry.getValue();
// a negative offset number represents an object number itself
// see type 2 entry in xref stream
- if (objectOffset != null && objectOffset >= 0
- && !checkObjectKey(objectKey, objectOffset))
+ if (objectOffset != null && objectOffset >= 0)
{
- LOG.debug("Stop checking xref offsets as at least one (" + objectKey
- + ") couldn't be dereferenced");
- return false;
+ COSObjectKey foundObjectKey = findObjectKey(objectKey, objectOffset);
+ if (foundObjectKey == null)
+ {
+ LOG.debug("Stop checking xref offsets as at least one (" + objectKey
+ + ") couldn't be dereferenced");
+ return false;
+ }
+ else if (foundObjectKey != objectKey)
+ {
+ // Generation was fixed - need to update map later, after iteration
+ correctedKeys.put(objectKey, foundObjectKey);
+ }
}
}
+ for (Entry<COSObjectKey, COSObjectKey> correctedKeyEntry : correctedKeys.entrySet())
+ {
+ xrefOffset.put(correctedKeyEntry.getValue(),
+ xrefOffset.remove(correctedKeyEntry.getKey()));
+ }
return true;
}
@@ -1490,21 +1504,22 @@ public class COSParser extends BaseParse
}
/**
- * Check if the given object can be found at the given offset.
+ * Check if the given object can be found at the given offset. Returns the provided object key if everything is ok.
+ * If the generation number differs it will be fixed and a new object key is returned.
*
- * @param objectKey the object we are looking for
+ * @param objectKey the key of object we are looking for
* @param offset the offset where to look
- * @return returns true if the given object can be dereferenced at the given offset
+ * @return returns the found/fixed object key
+ *
* @throws IOException if something went wrong
*/
- private boolean checkObjectKey(COSObjectKey objectKey, long offset) throws IOException
+ private COSObjectKey findObjectKey(COSObjectKey objectKey, long offset) throws IOException
{
// there can't be any object at the very beginning of a pdf
if (offset < MINIMUM_SEARCH_OFFSET)
{
- return false;
+ return null;
}
- boolean objectKeyFound = false;
try
{
source.seek(offset);
@@ -1512,27 +1527,24 @@ public class COSParser extends BaseParse
if (objectKey.getNumber() == readObjectNumber())
{
int genNumber = readGenerationNumber();
+ // finally try to read the object marker
+ readExpectedString(OBJ_MARKER, true);
if (genNumber == objectKey.getGeneration())
{
- // finally try to read the object marker
- readExpectedString(OBJ_MARKER, true);
- objectKeyFound = true;
+ return objectKey;
}
else if (isLenient && genNumber > objectKey.getGeneration())
{
- // finally try to read the object marker
- readExpectedString(OBJ_MARKER, true);
- objectKeyFound = true;
- objectKey.fixGeneration(genNumber);
+ return new COSObjectKey(objectKey.getNumber(), genNumber);
}
}
}
catch (IOException exception)
{
// Swallow the exception, obviously there isn't any valid object number
+ LOG.debug("No valid object at given location " + offset + " - ignoring", exception);
}
- // return resulting value
- return objectKeyFound;
+ return null;
}
/**