Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDFBOX-5283: ignore dublicate object keys when correcting xRefs #129

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 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<>();
HashSet<COSObjectKey> validKeys = new HashSet<>();
for (Entry<COSObjectKey, Long> objectEntry : xrefOffset.entrySet())
{
COSObjectKey objectKey = objectEntry.getKey();
Expand All @@ -1178,13 +1179,24 @@ else if (foundObjectKey != objectKey)
{
// Generation was fixed - need to update map later, after iteration
correctedKeys.put(objectKey, foundObjectKey);
} else {
validKeys.add(objectKey);
}
}
}
for (Entry<COSObjectKey, COSObjectKey> correctedKeyEntry : correctedKeys.entrySet())
{
xrefOffset.put(correctedKeyEntry.getValue(),
xrefOffset.remove(correctedKeyEntry.getKey()));
Map<COSObjectKey, Long> correctedPointers = new HashMap<COSObjectKey, Long>();
for (Entry<COSObjectKey, COSObjectKey> correctedKeyEntry : correctedKeys.entrySet()) {
if (!validKeys.contains(correctedKeyEntry.getValue())) {
// Only replace entries, if the original entry does not point to a valid object
correctedPointers.put(correctedKeyEntry.getValue(), xrefOffset.get(correctedKeyEntry.getKey()));
}
}
for (Entry<COSObjectKey, COSObjectKey> correctedKeyEntry : correctedKeys.entrySet()) {
// remove old invalid, as some might not be replaced
xrefOffset.remove(correctedKeyEntry.getKey());
}
for (Entry<COSObjectKey, Long> pointer : correctedPointers.entrySet()) {
xrefOffset.put(pointer.getKey(), pointer.getValue());
}
return true;
}
Expand Down Expand Up @@ -1235,19 +1247,23 @@ private COSObjectKey findObjectKey(COSObjectKey objectKey, long offset) throws I
{
source.seek(offset);
// try to read the given object/generation number
if (objectKey.getNumber() == readObjectNumber())
long foundObjectNumber = readObjectNumber();
if(objectKey.getNumber() != 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();
// finally try to read the object marker
readExpectedString(OBJ_MARKER, true);
if (genNumber == objectKey.getGeneration())
{
int genNumber = readGenerationNumber();
// finally try to read the object marker
readExpectedString(OBJ_MARKER, true);
if (genNumber == objectKey.getGeneration())
{
return objectKey;
}
else if (isLenient && genNumber > objectKey.getGeneration())
{
return new COSObjectKey(objectKey.getNumber(), genNumber);
}
return objectKey;
}
else if (isLenient && genNumber > objectKey.getGeneration())
{
return new COSObjectKey(objectKey.getNumber(), genNumber);
}
}
catch (IOException exception)
Expand Down