added support for invalid endstream
This commit is contained in:
1 parent
4e3b3e10b7
commit
ee011a5da0
2 files changed
+47
-2
No files matched your search
@@ -29,7 +29,6 @@ from .exceptions import (
|
|||||||
def get_first_object(
|
def get_first_object(
|
||||||
buffer: BufferStream, offset: int, state: Optional[ParsingState] = None
|
buffer: BufferStream, offset: int, state: Optional[ParsingState] = None
|
||||||
) -> PDFObject:
|
) -> PDFObject:
|
||||||
with buffer.get_window():
|
|
||||||
buffer.seek(offset)
|
buffer.seek(offset)
|
||||||
return next(iter_objects(buffer, state))
|
return next(iter_objects(buffer, state))
|
||||||
|
|
||||||
@@ -127,7 +126,15 @@ def _on_stream(buffer: BufferStream, state: ParsingState, token: TokenKeyword):
|
|||||||
last_ctx.stream = stream = PDFStream.from_pdfdict(token.end_loc + 1, obj)
|
last_ctx.stream = stream = PDFStream.from_pdfdict(token.end_loc + 1, obj)
|
||||||
if isinstance(stream.length, PDFInteger):
|
if isinstance(stream.length, PDFInteger):
|
||||||
buffer.seek(stream.offset + stream.length.to_python())
|
buffer.seek(stream.offset + stream.length.to_python())
|
||||||
next_token = next(iter_tokens(buffer))
|
next_token = None
|
||||||
|
for i, next_token in enumerate(iter_tokens(buffer)):
|
||||||
|
if (
|
||||||
|
isinstance(next_token, TokenKeyword)
|
||||||
|
and next_token.value == b"endstream"
|
||||||
|
):
|
||||||
|
if i > 0:
|
||||||
|
stream.length.value = next_token.start_loc - stream.offset
|
||||||
|
break
|
||||||
if not (
|
if not (
|
||||||
isinstance(next_token, TokenKeyword)
|
isinstance(next_token, TokenKeyword)
|
||||||
and next_token.value == b"endstream"
|
and next_token.value == b"endstream"
|
||||||
|
|||||||
@@ -216,3 +216,41 @@ endobj
|
|||||||
"otherfeature": [10, 20],
|
"otherfeature": [10, 20],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_parse_bad_len_stream(self):
|
||||||
|
buffer = BufferStream.from_bytes(
|
||||||
|
br"""
|
||||||
|
5 0 obj
|
||||||
|
<< /Type /XRef /Length 123 /Filter /FlateDecode /otherfeature [10 20]>>
|
||||||
|
stream
|
||||||
|
"<99><A3><C1>l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567
|
||||||
|
<80>]]]]]endstream
|
||||||
|
endobj
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
for obj in iter_objects(buffer):
|
||||||
|
self.assertIsInstance(obj, IndirectObject)
|
||||||
|
self.assertIsInstance(obj.get_object(), PDFDictionary)
|
||||||
|
self.assertIsInstance(obj.stream, PDFStream) # type: ignore
|
||||||
|
self.assertDictEqual(
|
||||||
|
obj.stream.to_python(), # type: ignore
|
||||||
|
{
|
||||||
|
"offset": 100,
|
||||||
|
"length": 123 + 5,
|
||||||
|
"filter": ["/FlateDecode"],
|
||||||
|
"decode_parms": [],
|
||||||
|
"f": None,
|
||||||
|
"ffilter": [],
|
||||||
|
"fdecode_parms": [],
|
||||||
|
"dl": None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertDictEqual(
|
||||||
|
obj.get_object().to_python(),
|
||||||
|
{
|
||||||
|
"Filter": "/FlateDecode",
|
||||||
|
"Length": 123 + 5,
|
||||||
|
"Type": "/XRef",
|
||||||
|
"otherfeature": [10, 20],
|
||||||
|
},
|
||||||
|
)
|
||||||
Reference in new issue
Block a user