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

Allow rebuilding object offsets when objects do not have endobj token #894

Merged
Show file tree
Hide file tree
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
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
27 changes: 26 additions & 1 deletion 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 Expand Up @@ -156,7 +181,7 @@ public static IReadOnlyDictionary<IndirectReference, long> GetObjectLocations(II

currentlyInObject = true;

currentOffset++;
currentOffset += objBuffer.Length;

bytes.Seek(currentOffset);
loopProtection = 0;
Expand Down
Loading