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

Made a fix for a bad ref containing stream length. #37

Open
wants to merge 1 commit into
base: master
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
63 changes: 58 additions & 5 deletions src/PdfSharp/Pdf.IO/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public int MoveToObject(PdfObjectID objectID)
return _lexer.Position = position;
}

/// <summary>
/// Tries to set PDF input stream position to the specified object.
/// </summary>
public bool TryMoveToObject(PdfObjectID objectID, out int position)
{
position = _document._irefTable[objectID].Position;
if (position == -1)
{
position = _lexer.Position;
return false;
}

_lexer.Position = position;
return true;
}

public Symbol Symbol
{
get { return _lexer.Symbol; }
Expand Down Expand Up @@ -118,7 +134,8 @@ public PdfObject ReadObject(PdfObject pdfObject, PdfObjectID objectID, bool incl
int generationNumber = objectID.GenerationNumber;
if (!fromObjecStream)
{
MoveToObject(objectID);
if (!TryMoveToObject(objectID, out int position))
return null;
objectNumber = ReadInteger();
generationNumber = ReadInteger();
}
Expand Down Expand Up @@ -339,11 +356,47 @@ private int GetStreamLength(PdfDictionary dict)
if (reference != null)
{
ParserState state = SaveState();
object length = ReadObject(null, reference.ObjectID, false, false);
object pdf_obj = ReadObject(null, reference.ObjectID, false, false);
RestoreState(state);
int len = ((PdfIntegerObject)length).Value;
dict.Elements["/Length"] = new PdfInteger(len);
return len;




int len = -1;
if (pdf_obj is PdfIntegerObject length_obj)
{
len = length_obj.Value;
}
// For whatever reason, ReadObject() did not return a valid PdfIntegerObject
else
{
// Read 1k chunks until we find an "endstream" symbol
string content = "";
int read_pos = _lexer.Position;
int se = -1;
while (true)
{
int read_len = Math.Min(_lexer.PdfLength - read_pos, 1024);
content += _lexer.ReadRawString(read_pos, read_len);
read_pos += 1024;

se = content.IndexOf("endstream", StringComparison.Ordinal);
if (se != -1)
{
len = se - 2; // By spec, the stream should start on a new line. remove crlf chars from the count.
break;
}

if (read_pos >= _lexer.PdfLength)
break;
}
}

if (len != -1)
{
dict.Elements["/Length"] = new PdfInteger(len);
return len;
}
}
throw new InvalidOperationException("Cannot retrieve stream length.");
}
Expand Down