Skip to content

Commit

Permalink
only accept whitespace characters as trailing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan6419846 authored Dec 8, 2023
1 parent 7621d8e commit 676abc8
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 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 logger_warning, WHITESPACES
from .constants import ColorSpaces
from .errors import PdfReadError
from .generic import (
Expand Down Expand Up @@ -195,14 +195,13 @@ def bits2byte(data: bytes, size: Tuple[int, int], bits: int) -> bytes:
else:
if img.mode == "1":
# Two values ("high" and "low").
if len(lookup) != 2 * nb:
if len(lookup) < 2 * nb:
raise PdfReadError(f"Not enough lookup values: Expected {2 * nb}, got {len(lookup)}.")
logger_warning(
f"Expected {2 * nb} lookup values, got {len(lookup)}. Ignoring trailing ones.",
__name__,
)
lookup = lookup[:2 * nb]
expected_count = 2 * nb
if len(lookup) != expected_count:
if len(lookup) < expected_count:
raise PdfReadError(f"Not enough lookup values: Expected {expected_count}, got {len(lookup)}.")
lookup = lookup[:expected_count]

Check warning on line 202 in pypdf/_xobj_image_helpers.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_xobj_image_helpers.py#L201-L202

Added lines #L201 - L202 were not covered by tests
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

0 comments on commit 676abc8

Please sign in to comment.