Skip to content

Commit e200b78

Browse files
committed
1 parent 6a405d7 commit e200b78

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/docx/image/tiff.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ def _parse_value(cls, stream_rdr, offset, value_count, value_offset):
236236
The length of the string, including a terminating '\x00' (NUL) character, is in
237237
`value_count`.
238238
"""
239+
if value_count <= 4:
240+
return stream_rdr.read_str(value_count - 1, offset, 8)
241+
239242
return stream_rdr.read_str(value_count - 1, value_offset)
240243

241244

tests/image/test_jpeg.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Unit test suite for docx.image.jpeg module"""
22

33
import io
4+
import os
45

56
import pytest
67

78
from docx.image.constants import JPEG_MARKER_CODE, MIME_TYPE
9+
from docx.image.image import Image
810
from docx.image.helpers import BIG_ENDIAN, StreamReader
911
from docx.image.jpeg import (
1012
Exif,
@@ -21,6 +23,7 @@
2123
)
2224
from docx.image.tiff import Tiff
2325

26+
from ..unitutil.file import test_file_dir
2427
from ..unitutil.mock import (
2528
ANY,
2629
call,
@@ -31,6 +34,21 @@
3134
)
3235

3336

37+
class DescribeJpeg_integration:
38+
"""Integration tests using real image files."""
39+
40+
def it_can_parse_a_jpeg_with_inline_ascii_exif_values(self):
41+
"""Regression test for #184/#187 — iOS JPEGs whose EXIF Software field
42+
is short enough (<=4 bytes) to be stored inline in the Value/Offset
43+
slot rather than at a separate file offset.
44+
45+
Prior to the fix this raised UnexpectedEndOfFileError.
46+
"""
47+
jpeg_path = os.path.join(test_file_dir, "ios-exif-inline-ascii.jpg")
48+
image = Image.from_file(jpeg_path)
49+
assert image.content_type == MIME_TYPE.JPEG
50+
51+
3452
class DescribeJpeg:
3553
def it_knows_its_content_type(self):
3654
jpeg = Jpeg(None, None, None, None)

tests/image/test_tiff.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,18 @@ def it_can_parse_an_ascii_string_IFD_entry(self):
417417
val = _AsciiIfdEntry._parse_value(stream_rdr, None, 7, 0)
418418
assert val == "foobar"
419419

420+
@pytest.mark.parametrize("byte_order", [BIG_ENDIAN, LITTLE_ENDIAN])
421+
def it_can_parse_an_inline_ascii_string_ifd_entry(self, byte_order):
422+
bytes_ = {
423+
BIG_ENDIAN: b"\x00\x01\x00\x02\x00\x00\x00\x048.3\x00",
424+
LITTLE_ENDIAN: b"\x01\x00\x02\x00\x04\x00\x00\x008.3\x00",
425+
}[byte_order]
426+
stream_rdr = StreamReader(io.BytesIO(bytes_), byte_order)
427+
428+
ifd_entry = _AsciiIfdEntry.from_stream(stream_rdr, 0)
429+
430+
assert (ifd_entry.tag, ifd_entry.value) == (1, "8.3")
431+
420432

421433
class Describe_ShortIfdEntry:
422434
def it_can_parse_a_short_int_IFD_entry(self):
1.4 MB
Loading

0 commit comments

Comments
 (0)