Skip to content

Commit

Permalink
Support finding object declarations without endobj on previous object
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud TAMAILLON authored and BobLd committed Sep 1, 2024
1 parent bd9702a commit 1bfd6de
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/UglyToad.PdfPig.Tests/Parser/Parts/BruteForceSearcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,41 @@ public void BruteForceSearcherCorrectlyFindsAllObjectsWhenOffset()
Assert.Equal(TestDataOffsets, locations.Values);
}

[Fact]
public void BruteForceSearcherFindsAllObjectsWhenMissingEndObj()
{
const string s = @"%PDF-1.7
abcd
1 0 obj
<< /Type /Any >>
2 0 obj
<< /Type /Any >>
%AZ 0 obj
11 0 obj
769
endobj
%%EOF";

var bytes = new MemoryInputBytes(OtherEncodings.StringAsLatin1Bytes(s));

var locations = BruteForceSearcher.GetObjectLocations(bytes);

Assert.Equal(3, locations.Count);

var expectedLocations = new long[]
{
s.IndexOf("1 0 obj", StringComparison.OrdinalIgnoreCase),
s.IndexOf("2 0 obj", StringComparison.OrdinalIgnoreCase),
s.IndexOf("11 0 obj", StringComparison.OrdinalIgnoreCase)
};

Assert.Equal(expectedLocations, locations.Values);
}

private static string GetStringAt(IInputBytes bytes, long location)
{
bytes.Seek(location);
Expand Down
25 changes: 25 additions & 0 deletions src/UglyToad.PdfPig/Parser/Parts/BruteForceSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ public static IReadOnlyDictionary<IndirectReference, long> GetObjectLocations(II
currentOffset++;
}
}
else if (ReadHelper.IsWhitespace(bytes.CurrentByte))
{
var next = bytes.Peek();
if (next.HasValue && next.Value == 'o')
{
if (ReadHelper.IsString(bytes, " obj"))
{
currentlyInObject = false;
currentOffset--;
loopProtection = 0;
}
else
{
bytes.MoveNext();
currentOffset++;
loopProtection = 0;
}
}
else
{
bytes.MoveNext();
currentOffset++;
loopProtection = 0;
}
}
else
{
bytes.MoveNext();
Expand Down

0 comments on commit 1bfd6de

Please sign in to comment.