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

Accept arbitrary width integers in nunpack #1029

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `RecursionError` when corrupt PDF specifies a recursive /Pages object ([#998](https://github.com/pdfminer/pdfminer.six/pull/998))
- `TypeError` when corrupt PDF specifies text-positioning operators with invalid values ([#1000](https://github.com/pdfminer/pdfminer.six/pull/1000))
- inline image parsing fails when stream data contains "EI\n" ([#1008](https://github.com/pdfminer/pdfminer.six/issues/1008))
- `TypeError` raised by extract_text method with compressed PDF file ([#886](https://github.com/pdfminer/pdfminer.six/issues/886))

### Removed

Expand Down
15 changes: 2 additions & 13 deletions pdfminer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io
import pathlib
import string
import struct
from html import escape
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -359,22 +358,12 @@ def choplist(n: int, seq: Iterable[_T]) -> Iterator[Tuple[_T, ...]]:


def nunpack(s: bytes, default: int = 0) -> int:
"""Unpacks 1 to 4 or 8 byte integers (big endian)."""
"""Unpacks variable-length unsigned integers (big endian)."""
length = len(s)
if not length:
return default
elif length == 1:
return ord(s)
elif length == 2:
return cast(int, struct.unpack(">H", s)[0])
elif length == 3:
return cast(int, struct.unpack(">L", b"\x00" + s)[0])
elif length == 4:
return cast(int, struct.unpack(">L", s)[0])
elif length == 8:
return cast(int, struct.unpack(">Q", s)[0])
else:
raise PDFTypeError("invalid length: %d" % length)
return int.from_bytes(s, byteorder="big", signed=False)


PDFDocEncoding = "".join(
Expand Down
Binary file added samples/contrib/issue-886-xref-stream-widths.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/test_highlevel_extracttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def run_with_file(sample_path):
"contrib/issue_566_test_2.pdf": "甲方:中国饮料有限公司(盖章)",
"contrib/issue-625-identity-cmap.pdf": "Termin płatności: 2021-05-03",
"contrib/issue-791-non-unicode-cmap.pdf": "Peněžní prostředky na účtech",
"contrib/issue-886-xref-stream-widths.pdf": "Hello",
}


Expand Down Expand Up @@ -146,6 +147,12 @@ def test_issue_791_non_unicode_cmap(self):
s = run_with_file(test_file)
self.assertEqual(s.strip(), test_strings[test_file])

def test_issue_886_xref_stream_widths(self):
"""Ensure that we can support arbitrary width integers in xref streams"""
test_file = "contrib/issue-886-xref-stream-widths.pdf"
s = run_with_file(test_file)
self.assertEqual(s.strip(), test_strings[test_file])


class TestExtractPages(unittest.TestCase):
def _get_test_file_path(self):
Expand Down
Loading