Skip to content

Commit

Permalink
BUG: Support UTF-16-LE Strings (#1884)
Browse files Browse the repository at this point in the history
Fixes #1838
  • Loading branch information
adamchainz authored Jul 3, 2023
1 parent 8963a57 commit 29c79fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pypdf/generic/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def create_string_object(
return TextStringObject(string.decode(forced_encoding))
else:
try:
if string.startswith(codecs.BOM_UTF16_BE):
if string.startswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
retval = TextStringObject(string.decode("utf-16"))
retval.autodetect_utf16 = True
return retval
Expand Down
16 changes: 16 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,22 @@ def test_indirect_object_invalid_read():
assert exc.value.args[0] == "Error reading indirect object reference at byte 0x5"


def test_create_string_object_utf16be_bom():
result = create_string_object(
b"\xfe\xff\x00P\x00a\x00p\x00e\x00r\x00P\x00o\x00r\x00t\x00 \x001\x004\x00\x00"
)
assert result == "PaperPort 14\x00"
assert result.autodetect_utf16 is True


def test_create_string_object_utf16le_bom():
result = create_string_object(
b"\xff\xfeP\x00a\x00p\x00e\x00r\x00P\x00o\x00r\x00t\x00 \x001\x004\x00\x00\x00"
)
assert result == "PaperPort 14\x00"
assert result.autodetect_utf16 is True


def test_create_string_object_force():
assert create_string_object(b"Hello World", []) == "Hello World"
assert create_string_object(b"Hello World", {72: "A"}) == "Aello World"
Expand Down

0 comments on commit 29c79fc

Please sign in to comment.