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

handling name without leading / #2373

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 18 additions & 10 deletions pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
read_non_whitespace,
read_until_regex,
str_,
WHITESPACES,
)
from ..errors import STREAM_TRUNCATED_PREMATURELY, PdfReadError, PdfStreamError

Expand Down Expand Up @@ -557,6 +558,7 @@ def write_to_stream(
class NameObject(str, PdfObject): # noqa: SLOT000
delimiter_pattern = re.compile(rb"\s+|[\(\)<>\[\]{}/%]")
surfix = b"/"
NUMBER = b"#"
renumber_table: ClassVar[Dict[str, bytes]] = {
"#": b"#23",
"(": b"#28",
Expand Down Expand Up @@ -588,18 +590,24 @@ def write_to_stream(
stream.write(self.renumber())

def renumber(self) -> bytes:
out = self[0].encode("utf-8")
if out != b"/":
out = self.surfix
try:
val = self[:].encode("latin1")
# a sequence of any characters (8-bit values)
except UnicodeEncodeError as e:
logger_warning(repr(e), __name__)
val = self[:].encode("utf8")
if val[:1] != self.surfix:
logger_warning(f"Incorrect first char in NameObject:({self})", __name__)
for c in self[1:]:
if c > "~":
for x in c.encode("utf-8"):
out += f"#{x:02X}".encode()
else:
val = val[1:]
escaped = b"".join(WHITESPACES) + self.NUMBER
for c in val:
assert c # null char is not allowed
if 0x21 <= c <= 0x7E and c not in escaped:
out += bytes([c])
else:
try:
out += self.renumber_table[c]
except KeyError:
out += c.encode("utf-8")
out += self.NUMBER + f"{c:02X}".encode()
return out

@staticmethod
Expand Down
10 changes: 7 additions & 3 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_name_object(caplog):
caplog.clear()
b = BytesIO()
NameObject("hello").write_to_stream(b)
assert bytes(b.getbuffer()) == b"hello"
assert bytes(b.getbuffer()) == b"/hello"
assert "Incorrect first char" in caplog.text

caplog.clear()
Expand All @@ -228,10 +228,14 @@ def test_name_object(caplog):
assert bytes(b.getbuffer()) == b"/DIJMAC+Arial#20Black#231"
assert caplog.text == ""

b = BytesIO()
NameObject("#42").write_to_stream(b)
assert bytes(b.getbuffer()) == b"/#2342"

b = BytesIO()
NameObject("/你好世界 (%)").write_to_stream(b)
assert bytes(b.getbuffer()) == b"/#E4#BD#A0#E5#A5#BD#E4#B8#96#E7#95#8C#20#28#25#29"
assert caplog.text == ""
assert bytes(b.getbuffer()) == b"/#E4#BD#A0#E5#A5#BD#E4#B8#96#E7#95#8C#20(%)"
assert "UnicodeEncodeError" in caplog.text


def test_destination_fit_r():
Expand Down
Loading