File size: 22,955 Bytes
3a3c77b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | # Copyright (c) 2006, Mathieu Fenniak
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Utility functions for PDF library."""
__author__ = "Mathieu Fenniak"
__author_email__ = "biziqe@mathieu.fenniak.net"
import functools
import logging
import re
import sys
import warnings
from dataclasses import dataclass
from datetime import datetime, timezone
from io import DEFAULT_BUFFER_SIZE
from os import SEEK_CUR
from re import Pattern
from typing import (
IO,
Any,
NoReturn,
Optional,
Union,
)
if sys.version_info[:2] >= (3, 10):
# Python 3.10+: https://www.python.org/dev/peps/pep-0484/
from typing import TypeAlias
else:
from typing_extensions import TypeAlias
if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self
from .errors import (
STREAM_TRUNCATED_PREMATURELY,
DeprecationError,
LimitReachedError,
PdfStreamError,
)
TransformationMatrixType: TypeAlias = tuple[
tuple[float, float, float], tuple[float, float, float], tuple[float, float, float]
]
CompressedTransformationMatrix: TypeAlias = tuple[
float, float, float, float, float, float
]
StreamType = IO[Any]
BinaryStreamType = IO[bytes]
StrByteType = Union[str, StreamType]
def parse_iso8824_date(text: Optional[str]) -> Optional[datetime]:
orgtext = text
if not text:
return None
if text[0].isdigit():
text = "D:" + text
if text.endswith(("Z", "z")):
text += "0000"
text = text.replace("z", "+").replace("Z", "+").replace("'", "")
i = max(text.find("+"), text.find("-"))
if i > 0 and i != len(text) - 5:
text += "00"
for f in (
"D:%Y",
"D:%Y%m",
"D:%Y%m%d",
"D:%Y%m%d%H",
"D:%Y%m%d%H%M",
"D:%Y%m%d%H%M%S",
"D:%Y%m%d%H%M%S%z",
):
try:
d = datetime.strptime(text, f) # noqa: DTZ007
except ValueError:
continue
else:
if text.endswith("+0000"):
d = d.replace(tzinfo=timezone.utc)
return d
raise ValueError(f"Can not convert date: {orgtext}")
def format_iso8824_date(dt: datetime) -> str:
"""
Convert a datetime object to PDF date string format.
Converts datetime to the PDF date format D:YYYYMMDDHHmmSSOHH'mm
as specified in the PDF Reference.
Args:
dt: A datetime object to convert.
Returns:
A date string in PDF format.
"""
date_str = dt.strftime("D:%Y%m%d%H%M%S")
if dt.tzinfo is not None:
offset = dt.utcoffset()
assert offset is not None
total_seconds = int(offset.total_seconds())
hours, remainder = divmod(abs(total_seconds), 3600)
minutes = remainder // 60
sign = "+" if total_seconds >= 0 else "-"
date_str += f"{sign}{hours:02d}'{minutes:02d}'"
return date_str
def _get_max_pdf_version_header(header1: str, header2: str) -> str:
versions = (
"%PDF-1.3",
"%PDF-1.4",
"%PDF-1.5",
"%PDF-1.6",
"%PDF-1.7",
"%PDF-2.0",
)
pdf_header_indices = []
if header1 in versions:
pdf_header_indices.append(versions.index(header1))
if header2 in versions:
pdf_header_indices.append(versions.index(header2))
if len(pdf_header_indices) == 0:
raise ValueError(f"Neither {header1!r} nor {header2!r} are proper headers")
return versions[max(pdf_header_indices)]
WHITESPACES = (b"\x00", b"\t", b"\n", b"\f", b"\r", b" ")
WHITESPACES_AS_BYTES = b"".join(WHITESPACES)
WHITESPACES_AS_REGEXP = b"[" + WHITESPACES_AS_BYTES + b"]"
def read_until_whitespace(stream: StreamType, max_bytes: Optional[int] = None) -> bytes:
"""
Read non-whitespace characters and return them.
Stops upon encountering whitespace or when max_bytes is reached.
Args:
stream: The data stream from which was read.
max_bytes: The maximum number of bytes returned; by default unlimited.
Returns:
The data which was read.
"""
txt = bytearray()
while True:
tok = stream.read(1)
if tok.isspace() or not tok:
break
txt += tok
if len(txt) == max_bytes:
break
return bytes(txt)
def read_non_whitespace(stream: BinaryStreamType) -> bytes:
"""
Find and read the next non-whitespace character (ignores whitespace).
Args:
stream: The data stream from which was read.
Returns:
The data which was read.
"""
tok = stream.read(1)
while tok in WHITESPACES:
tok = stream.read(1)
return tok
def skip_over_whitespace(stream: StreamType) -> bool:
"""
Similar to read_non_whitespace, but return a boolean if at least one
whitespace character was read.
Args:
stream: The data stream from which was read.
Returns:
True if one or more whitespace was skipped, otherwise return False.
"""
tok = stream.read(1)
cnt = 0
while tok in WHITESPACES:
cnt += 1
tok = stream.read(1)
return cnt > 0
def check_if_whitespace_only(value: bytes) -> bool:
"""
Check if the given value consists of whitespace characters only.
Args:
value: The bytes to check.
Returns:
True if the value only has whitespace characters, otherwise return False.
"""
return all(b in WHITESPACES_AS_BYTES for b in value)
NEUTRAL_CHARACTER_RANGES = (
("\x00", "\x2F"), # ASCII control codes, space, and early punctuation (!"#$%)
("\x3A", "\x40"), # ASCII operators and punctuation between digits & A (:;<=>?@)
("\u2000", "\u206F"), # General punctuation
("\u20A0", "\u21FF"), # Currency symbols, diacritical marks, letter-like symbols, number forms, arrows
)
def is_char_neutral(char: str, custom_special_characters: str = "") -> bool:
"""Check if a character is part of neutral character ranges"""
if any(start <= char <= end for start, end in NEUTRAL_CHARACTER_RANGES):
return True
return bool(custom_special_characters and char in custom_special_characters)
RTL_CHARACTER_RANGES = (
("\u0590", "\u08FF"), # Hebrew, Arabic, Syriac, Thaana, N'Ko, etc.
("\uFB1D", "\uFDFF"), # Hebrew & Arabic Presentation Forms-A
("\uFE70", "\uFEFF"), # Arabic Presentation Forms-B
)
def is_char_rtl(char: str, custom_rtl_min: str = "", custom_rtl_max: str = "") -> bool:
"""Check if a character is part of RTL character ranges"""
if any(start <= char <= end for start, end in RTL_CHARACTER_RANGES):
return True
return bool(custom_rtl_min and custom_rtl_max and (custom_rtl_min <= char <= custom_rtl_max))
def skip_over_comment(stream: StreamType) -> None:
tok = stream.read(1)
stream.seek(-1, 1)
if tok == b"%":
while tok not in (b"\n", b"\r"):
tok = stream.read(1)
if tok == b"":
raise PdfStreamError("File ended unexpectedly.")
def read_until_regex(*, stream: StreamType, regex: Pattern[bytes], length: int = sys.maxsize) -> bytes:
"""
Read until the regular expression pattern matched (ignore the match).
Treats EOF on the underlying stream as the end of the token to be matched.
Args:
stream: The stream to read from.
regex: The pattern to search for.
length: The (approximated) maximum number of bytes to read before raising an exception.
Returns:
The read bytes.
"""
parts: list[bytes] = []
total_length = 0
tail = b""
chunk_size = 16
while True:
token = stream.read(chunk_size)
if not token:
return b"".join(parts)
token_length = len(token)
if (current_length := total_length + token_length) >= length:
raise LimitReachedError(
f"Read stream length of {current_length} exceeds maximum allowed length of {length}."
)
# Search overlap of previous tail + new chunk to catch
# multi-byte regex matches spanning chunk boundaries.
current_buffer = tail + token
search_match = regex.search(current_buffer)
parts.append(token)
if search_match is not None:
overlap = len(tail)
actual_start = total_length - overlap + search_match.start()
stream.seek(actual_start - total_length - token_length, 1)
return b"".join(parts)[:actual_start]
total_length += token_length
# Fixed overlap: 16 bytes is sufficient for the short
# delimiter patterns used in PDF parsing.
tail = token[-16:]
if chunk_size < 8192:
chunk_size <<= 1
def read_block_backwards(stream: BinaryStreamType, to_read: int) -> bytes:
"""
Given a stream at position X, read a block of size to_read ending at position X.
This changes the stream's position to the beginning of where the block was
read.
Args:
stream:
to_read:
Returns:
The data which was read.
"""
if stream.tell() < to_read:
raise PdfStreamError("Could not read malformed PDF file")
# Seek to the start of the block we want to read.
stream.seek(-to_read, SEEK_CUR)
read = stream.read(to_read)
# Seek to the start of the block we read after reading it.
stream.seek(-to_read, SEEK_CUR)
return read
def read_previous_line(stream: StreamType) -> bytes:
"""
Given a byte stream with current position X, return the previous line.
All characters between the first CR/LF byte found before X
(or, the start of the file, if no such byte is found) and position X
After this call, the stream will be positioned one byte after the
first non-CRLF character found beyond the first CR/LF byte before X,
or, if no such byte is found, at the beginning of the stream.
Args:
stream: StreamType:
Returns:
The data which was read.
"""
line_content = []
found_crlf = False
if stream.tell() == 0:
raise PdfStreamError(STREAM_TRUNCATED_PREMATURELY)
while True:
to_read = min(DEFAULT_BUFFER_SIZE, stream.tell())
if to_read == 0:
break
# Read the block. After this, our stream will be one
# beyond the initial position.
block = read_block_backwards(stream, to_read)
idx = len(block) - 1
if not found_crlf:
# We haven't found our first CR/LF yet.
# Read off characters until we hit one.
while idx >= 0 and block[idx] not in b"\r\n":
idx -= 1
if idx >= 0:
found_crlf = True
if found_crlf:
# We found our first CR/LF already (on this block or
# a previous one).
# Our combined line is the remainder of the block
# plus any previously read blocks.
line_content.append(block[idx + 1 :])
# Continue to read off any more CRLF characters.
while idx >= 0 and block[idx] in b"\r\n":
idx -= 1
else:
# Didn't find CR/LF yet - add this block to our
# previously read blocks and continue.
line_content.append(block)
if idx >= 0:
# We found the next non-CRLF character.
# Set the stream position correctly, then break
stream.seek(idx + 1, SEEK_CUR)
break
# Join all the blocks in the line (which are in reverse order)
return b"".join(line_content[::-1])
def matrix_multiply(
a: TransformationMatrixType, b: TransformationMatrixType
) -> TransformationMatrixType:
return tuple( # type: ignore[return-value]
tuple(sum(float(i) * float(j) for i, j in zip(row, col)) for col in zip(*b))
for row in a
)
def mark_location(stream: StreamType) -> None:
"""Create text file showing current location in context."""
# Mainly for debugging
radius = 5000
stream.seek(-radius, 1)
with open("pypdf_pdfLocation.txt", "wb") as output_fh:
output_fh.write(stream.read(radius))
output_fh.write(b"HERE")
output_fh.write(stream.read(radius))
stream.seek(-radius, 1)
def deprecate(msg: str, stacklevel: int = 3) -> None:
warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel)
def deprecation(msg: str) -> NoReturn:
raise DeprecationError(msg)
def deprecate_with_replacement(old_name: str, new_name: str, removed_in: str) -> None:
"""Issue a warning that a feature will be removed, but has a replacement."""
deprecate(
f"{old_name} is deprecated and will be removed in pypdf {removed_in}. Use {new_name} instead.",
4,
)
def deprecation_with_replacement(old_name: str, new_name: str, removed_in: str) -> NoReturn:
"""Raise an exception that a feature was already removed, but has a replacement."""
deprecation(
f"{old_name} is deprecated and was removed in pypdf {removed_in}. Use {new_name} instead."
)
def deprecate_no_replacement(name: str, removed_in: str) -> None:
"""Issue a warning that a feature will be removed without replacement."""
deprecate(f"{name} is deprecated and will be removed in pypdf {removed_in}.", 4)
def deprecation_no_replacement(name: str, removed_in: str) -> NoReturn:
"""Raise an exception that a feature was already removed without replacement."""
deprecation(f"{name} is deprecated and was removed in pypdf {removed_in}.")
def logger_error(message: str, *, source: str, **values: Any) -> None:
"""
Use this instead of logger.error directly.
That allows people to overwrite it more easily.
See the docs on when to use which:
https://pypdf.readthedocs.io/en/latest/user/suppress-warnings.html
"""
if values:
logging.getLogger(source).error(message, values)
else:
logging.getLogger(source).error(message)
def logger_warning(message: str, *, source: str, **values: Any) -> None:
"""
Use this instead of logger.warning directly.
That allows people to overwrite it more easily.
## Exception, warnings.warn, logger_warning
- Exceptions should be used if the user should write code that deals with
an error case, e.g. the PDF being completely broken.
- warnings.warn should be used if the user needs to fix their code, e.g.
DeprecationWarnings
- logger_warning should be used if the user needs to know that an issue was
handled by pypdf, e.g. a non-compliant PDF being read in a way that
pypdf could apply a robustness fix to still read it. This applies mainly
to strict=False mode.
"""
if values:
logging.getLogger(source).warning(message, values)
else:
# Keep parity with logger_error and support plain warning messages.
# Passing an empty dict to logging is not equivalent to passing no args:
# plain messages would fail while being formatted.
logging.getLogger(source).warning(message)
def rename_kwargs(
func_name: str, kwargs: dict[str, Any], aliases: dict[str, str], fail: bool = False
) -> None:
"""
Helper function to deprecate arguments.
Args:
func_name: Name of the function to be deprecated
kwargs:
aliases:
fail:
"""
for old_term, new_term in aliases.items():
if old_term in kwargs:
if fail:
raise DeprecationError(
f"{old_term} is deprecated as an argument. Use {new_term} instead"
)
if new_term in kwargs:
raise TypeError(
f"{func_name} received both {old_term} and {new_term} as "
f"an argument. {old_term} is deprecated. "
f"Use {new_term} instead."
)
kwargs[new_term] = kwargs.pop(old_term)
warnings.warn(
message=(
f"{old_term} is deprecated as an argument. Use {new_term} instead"
),
category=DeprecationWarning,
stacklevel=3,
)
def _human_readable_bytes(bytes: int) -> str:
if bytes < 10**3:
return f"{bytes} Byte"
if bytes < 10**6:
return f"{bytes / 10**3:.1f} kB"
if bytes < 10**9:
return f"{bytes / 10**6:.1f} MB"
return f"{bytes / 10**9:.1f} GB"
# The following class has been copied from Django:
# https://github.com/django/django/blob/adae619426b6f50046b3daaa744db52989c9d6db/django/utils/functional.py#L51-L65
# It received some modifications to comply with our own coding standards.
#
# Original license:
#
# ---------------------------------------------------------------------------------
# Copyright (c) Django Software Foundation and individual contributors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of Django nor the names of its contributors may be used
# to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------------
class classproperty: # noqa: N801
"""
Decorator that converts a method with a single cls argument into a property
that can be accessed directly from the class.
"""
def __init__(self, method=None) -> None: # type: ignore # noqa: ANN001
self.fget = method
def __get__(self, instance, cls=None) -> Any: # type: ignore # noqa: ANN001
return self.fget(cls)
def getter(self, method) -> Self: # type: ignore # noqa: ANN001
self.fget = method
return self
@dataclass
class File:
from .generic import IndirectObject # noqa: PLC0415
name: str = ""
"""
Filename as identified within the PDF file.
"""
data: bytes = b""
"""
Data as bytes.
"""
indirect_reference: Optional[IndirectObject] = None
"""
Reference to the object storing the stream.
"""
def __str__(self) -> str:
return f"{self.__class__.__name__}(name={self.name}, data: {_human_readable_bytes(len(self.data))})"
def __repr__(self) -> str:
return self.__str__()[:-1] + f", hash: {hash(self.data)})"
@functools.total_ordering
class Version:
COMPONENT_PATTERN = re.compile(r"^(\d+)(.*)$")
def __init__(self, version_str: str) -> None:
self.version_str = version_str
self.components = self._parse_version(version_str)
def _parse_version(self, version_str: str) -> list[tuple[int, str]]:
components = version_str.split(".")
parsed_components = []
for component in components:
match = Version.COMPONENT_PATTERN.match(component)
if not match:
parsed_components.append((0, component))
continue
integer_prefix = match.group(1)
suffix = match.group(2)
if integer_prefix is None:
integer_prefix = 0
parsed_components.append((int(integer_prefix), suffix))
return parsed_components
def __eq__(self, other: object) -> bool:
if not isinstance(other, Version):
return False
return self.components == other.components
def __hash__(self) -> int:
# Convert to tuple as lists cannot be hashed.
return hash((self.__class__, tuple(self.components)))
def __lt__(self, other: Any) -> bool:
if not isinstance(other, Version):
raise ValueError(f"Version cannot be compared against {type(other)}")
for self_component, other_component in zip(self.components, other.components):
self_value, self_suffix = self_component
other_value, other_suffix = other_component
if self_value < other_value:
return True
if self_value > other_value:
return False
if self_suffix < other_suffix:
return True
if self_suffix > other_suffix:
return False
return len(self.components) < len(other.components)
@dataclass
class _TraversalState:
"""Sometimes we need mutable objects which just count something."""
entry_count: int = 0
has_logged: bool = False
|