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

ROB: Replace error by warning for EOD in RunLengthDecode/ASCIIHexDecode #2334

Merged
merged 11 commits into from
Mar 30, 2024
10 changes: 8 additions & 2 deletions pypdf/_xobj_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from io import BytesIO
from typing import Any, List, Tuple, Union, cast

from ._utils import logger_warning
from ._utils import WHITESPACES, logger_warning
from .constants import ColorSpaces
from .errors import PdfReadError
from .generic import (
Expand Down Expand Up @@ -195,7 +195,13 @@
else:
if img.mode == "1":
# Two values ("high" and "low").
assert len(lookup) == 2 * nb, len(lookup)
expected_count = 2 * nb
if len(lookup) != expected_count:
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
if len(lookup) < expected_count:
raise PdfReadError(f"Not enough lookup values: Expected {expected_count}, got {len(lookup)}.")

Check warning on line 201 in pypdf/_xobj_image_helpers.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_xobj_image_helpers.py#L201

Added line #L201 was not covered by tests
lookup = lookup[:expected_count]
if not all(_value in WHITESPACES for _value in lookup[expected_count:]):
raise PdfReadError(f"Too many lookup values: Expected {expected_count}, got {len(lookup)}.")

Check warning on line 204 in pypdf/_xobj_image_helpers.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_xobj_image_helpers.py#L204

Added line #L204 was not covered by tests
colors_arr = [lookup[:nb], lookup[nb:]]
arr = b"".join(
[
Expand Down
6 changes: 4 additions & 2 deletions pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def decode(
index = 0
while True:
if index >= len(data):
raise PdfStreamError("Unexpected EOD in ASCIIHexDecode")
break # reach End Of String even if no EOD
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
# raise PdfStreamError("Unexpected EOD in ASCIIHexDecode")
char = data[index : index + 1]
if char == b">":
break
Expand Down Expand Up @@ -340,7 +341,8 @@ def decode(
index = 0
while True:
if index >= len(data):
raise PdfStreamError("Unexpected EOD in RunLengthDecode")
break # reach End Of String even if no EOD
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
# raise PdfStreamError("Unexpected EOD in ASCIIHexDecode")
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
length = data[index]
index += 1
if length == 128:
Expand Down
25 changes: 15 additions & 10 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from PIL import Image

from pypdf import PdfReader
from pypdf.errors import DeprecationError, PdfReadError, PdfStreamError
from pypdf.errors import DeprecationError, PdfReadError
from pypdf.filters import (
ASCII85Decode,
ASCIIHexDecode,
Expand Down Expand Up @@ -131,9 +131,9 @@ def test_ascii_hex_decode_method(data, expected):

def test_ascii_hex_decode_missing_eod():
"""ASCIIHexDecode.decode() raises error when no EOD character is present."""
with pytest.raises(PdfStreamError) as exc:
ASCIIHexDecode.decode("")
assert exc.value.args[0] == "Unexpected EOD in ASCIIHexDecode"
# with pytest.raises(PdfStreamError) as exc:
ASCIIHexDecode.decode("")
# assert exc.value.args[0] == "Unexpected EOD in ASCIIHexDecode"


@pytest.mark.enable_socket()
Expand Down Expand Up @@ -561,14 +561,10 @@ def test_runlengthdecode():
url = "https://github.com/py-pdf/pypdf/files/12162905/out.pdf"
name = "FailedRLE1.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
with pytest.raises(PdfStreamError) as exc:
reader.pages[0].images[0]
assert exc.value.args[0] == "Unexpected EOD in RunLengthDecode"
reader.pages[0].images[0]
url = "https://github.com/py-pdf/pypdf/files/12162926/out.pdf"
name = "FailedRLE2.pdf"
with pytest.raises(PdfStreamError) as exc:
reader.pages[0].images[0]
assert exc.value.args[0] == "Unexpected EOD in RunLengthDecode"
reader.pages[0].images[0]


@pytest.mark.enable_socket()
Expand Down Expand Up @@ -649,3 +645,12 @@ def test_flate_decode_with_image_mode_1():
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
for image in reader.pages[7].images:
_ = image


@pytest.mark.enable_socket()
def test_flate_decode_with_image_mode_1__whitespace_at_end_of_lookup():
"""From #2331"""
url = "https://github.com/py-pdf/pypdf/files/13611048/out1.pdf"
name = "issue2331.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
reader.pages[0].images[0]
Loading