Skip to content

Commit

Permalink
only replace xRef with corrected entries, if the original entry was n…
Browse files Browse the repository at this point in the history
…ot valid
  • Loading branch information
Schmidor committed Oct 5, 2021
1 parent 8604732 commit cc01382
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ private boolean validateXrefOffsets(Map<COSObjectKey, Long> xrefOffset) throws I
return true;
}
Map<COSObjectKey, COSObjectKey> correctedKeys = new HashMap<>();
Map<COSObjectKey, COSObjectKey> validKeys = new HashMap<>();
for (Entry<COSObjectKey, Long> objectEntry : xrefOffset.entrySet())
{
COSObjectKey objectKey = objectEntry.getKey();
Expand All @@ -1178,13 +1179,18 @@ else if (foundObjectKey != objectKey)
{
// Generation was fixed - need to update map later, after iteration
correctedKeys.put(objectKey, foundObjectKey);
} else {
validKeys.put(objectKey, foundObjectKey);
}
}
}
for (Entry<COSObjectKey, COSObjectKey> correctedKeyEntry : correctedKeys.entrySet())
{
xrefOffset.put(correctedKeyEntry.getValue(),
xrefOffset.remove(correctedKeyEntry.getKey()));
if(!validKeys.containsKey(correctedKeyEntry.getValue())) {
// Only replacy entries, if the original entry does not point to a valid object
xrefOffset.put(correctedKeyEntry.getValue(),
xrefOffset.remove(correctedKeyEntry.getKey()));
}
}
return true;
}
Expand Down Expand Up @@ -1237,8 +1243,10 @@ private COSObjectKey findObjectKey(COSObjectKey objectKey, long offset) throws I
// try to read the given object/generation number
long foundObjectNumber = readObjectNumber();
if(objectKey.getNumber() != foundObjectNumber){
System.out.println("found wrong object number. expected [" + objectKey.getNumber() +"] found ["+ foundObjectNumber + "]");
LOG.warn("found wrong object number. expected [" + objectKey.getNumber() +"] found ["+ foundObjectNumber + "]");
if(!isLenient) return null;
else objectKey = new COSObjectKey(foundObjectNumber, objectKey.getGeneration());
}

int genNumber = readGenerationNumber();
Expand Down

0 comments on commit cc01382

Please sign in to comment.