Skip to content

Commit

Permalink
ROB: Fall back to non-Adobe Ascii85 format for missing end markers (#…
Browse files Browse the repository at this point in the history
…3007)

Closes #2996.
  • Loading branch information
stefan6419846 authored Dec 19, 2024
1 parent 83083bb commit 4f2cd34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,13 @@ def decode(
if isinstance(data, str):
data = data.encode()
data = data.strip(WHITESPACES_AS_BYTES)
return a85decode(data, adobe=True, ignorechars=WHITESPACES_AS_BYTES)
try:
return a85decode(data, adobe=True, ignorechars=WHITESPACES_AS_BYTES)
except ValueError as error:
if error.args[0] == "Ascii85 encoded byte sequences must end with b'~>'":
logger_warning("Ignoring missing Ascii85 end marker.", __name__)
return a85decode(data, adobe=False, ignorechars=WHITESPACES_AS_BYTES)
raise


class DCTDecode:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,29 @@ def test_flate_decode_with_image_mode_1__whitespace_at_end_of_lookup():
name = "issue2331.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
reader.pages[0].images[0]


@pytest.mark.enable_socket
def test_ascii85decode__invalid_end__recoverable(caplog):
"""From #2996"""
url = "https://github.com/user-attachments/files/18050808/1af7d56a-5c8c-4914-85b3-b2536a5525cd.pdf"
name = "issue2996.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))

page = reader.pages[1]
assert page.extract_text() == ""
assert "Ignoring missing Ascii85 end marker." in caplog.text


def test_ascii85decode__non_recoverable(caplog):
# Without our custom handling, this would complain about the final `~>` being missing.
data = "äöüß"
with pytest.raises(ValueError, match="Non-Ascii85 digit found: Ã"):
ASCII85Decode.decode(data)
assert "Ignoring missing Ascii85 end marker." in caplog.text
caplog.clear()

data += "~>"
with pytest.raises(ValueError, match="Non-Ascii85 digit found: Ã"):
ASCII85Decode.decode(data)
assert caplog.text == ""

0 comments on commit 4f2cd34

Please sign in to comment.