mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-19 19:07:56 +08:00
prevent infinite loops where a stream token's length entry references itself. perform brute force scans in case of a faulty xref table #33
This commit is contained in:
@@ -45,6 +45,8 @@
|
||||
|
||||
public IToken CurrentToken { get; private set; }
|
||||
|
||||
private IndirectReference? callingObject;
|
||||
|
||||
public long CurrentPosition => coreTokenScanner.CurrentPosition;
|
||||
|
||||
public PdfTokenScanner(IInputBytes inputBytes, IObjectLocationProvider objectLocationProvider, IFilterProvider filterProvider,
|
||||
@@ -94,8 +96,7 @@
|
||||
|
||||
if (objectNumber == null || generation == null)
|
||||
{
|
||||
throw new PdfDocumentFormatException("The obj operator (start object) was not preceded by a 2 numbers." +
|
||||
$"Instead got: {previousTokens[0]} {previousTokens[1]} obj");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read all tokens between obj and endobj.
|
||||
@@ -115,13 +116,29 @@
|
||||
|
||||
if (ReferenceEquals(coreTokenScanner.CurrentToken, OperatorToken.StartStream))
|
||||
{
|
||||
var streamIdentifier = new IndirectReference(objectNumber.Long, generation.Int);
|
||||
|
||||
// Prevent an infinite loop where a stream's length references the stream or the stream's offset.
|
||||
var getLengthFromFile = !(callingObject.HasValue && callingObject.Value.Equals(streamIdentifier));
|
||||
|
||||
var outerCallingObject = callingObject;
|
||||
|
||||
try
|
||||
{
|
||||
callingObject = streamIdentifier;
|
||||
|
||||
// Read stream: special case.
|
||||
if (TryReadStream(coreTokenScanner.CurrentTokenStart, out var stream))
|
||||
if (TryReadStream(coreTokenScanner.CurrentTokenStart, getLengthFromFile, out var stream))
|
||||
{
|
||||
readTokens.Clear();
|
||||
readTokens.Add(stream);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
callingObject = outerCallingObject;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
readTokens.Add(coreTokenScanner.CurrentToken);
|
||||
@@ -168,14 +185,14 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryReadStream(long startStreamTokenOffset, out StreamToken stream)
|
||||
private bool TryReadStream(long startStreamTokenOffset, bool getLength, out StreamToken stream)
|
||||
{
|
||||
stream = null;
|
||||
|
||||
DictionaryToken streamDictionaryToken = GetStreamDictionary();
|
||||
|
||||
// Get the expected length from the stream dictionary if present.
|
||||
long? length = GetStreamLength(streamDictionaryToken);
|
||||
long? length = getLength ? GetStreamLength(streamDictionaryToken) : default(long?);
|
||||
|
||||
// Verify again that we start with "stream"
|
||||
var hasStartStreamToken = ReadStreamTokenStart(inputBytes, startStreamTokenOffset);
|
||||
@@ -491,10 +508,30 @@
|
||||
|
||||
if (!MoveNext())
|
||||
{
|
||||
throw new InvalidOperationException($"Could not parse the object with reference: {reference}.");
|
||||
throw new PdfDocumentFormatException($"Could not parse the object with reference: {reference}.");
|
||||
}
|
||||
|
||||
return (ObjectToken)CurrentToken;
|
||||
var found = (ObjectToken)CurrentToken;
|
||||
|
||||
if (found.Number.Equals(reference))
|
||||
{
|
||||
return found;
|
||||
}
|
||||
|
||||
// Brute force read the entire file
|
||||
Seek(0);
|
||||
|
||||
while (MoveNext())
|
||||
{
|
||||
objectLocationProvider.Cache((ObjectToken)CurrentToken);
|
||||
}
|
||||
|
||||
if (!objectLocationProvider.TryGetCached(reference, out objectToken))
|
||||
{
|
||||
throw new PdfDocumentFormatException($"Could not locate object with reference: {reference} despite a full document search.");
|
||||
}
|
||||
|
||||
return objectToken;
|
||||
}
|
||||
|
||||
private ObjectToken GetObjectFromStream(IndirectReference reference, long offset)
|
||||
|
Reference in New Issue
Block a user