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

BUG: Support UTF-16-LE Strings #1884

Merged
merged 1 commit into from
Jul 3, 2023
Merged
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
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 @@ -1031,6 +1031,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