@@ -52,6 +52,28 @@ OIIO_PLUGIN_EXPORTS_END
5252static const uint8_t JPEG_MAGIC1 = 0xff ;
5353static const uint8_t JPEG_MAGIC2 = 0xd8 ;
5454
55+ static const char exif_marker_prefix[] = " Exif\0 " ;
56+
57+ static const char icc_marker_prefix[] = " ICC_PROFILE" ;
58+
59+ static bool
60+ is_exif_marker (jpeg_saved_marker_ptr marker)
61+ {
62+ return marker->marker == (JPEG_APP0 + 1 )
63+ && marker->data_length >= sizeof (exif_marker_prefix)
64+ && !memcmp (marker->data , exif_marker_prefix,
65+ sizeof (exif_marker_prefix));
66+ }
67+
68+ static bool
69+ is_icc_profile_marker (jpeg_saved_marker_ptr marker)
70+ {
71+ return marker->marker == (JPEG_APP0 + 2 )
72+ && marker->data_length >= ICC_HEADER_SIZE
73+ && !memcmp (marker->data , icc_marker_prefix,
74+ sizeof (icc_marker_prefix));
75+ }
76+
5577
5678// For explanations of the error handling, see the "example.c" in the
5779// libjpeg distribution.
@@ -271,8 +293,7 @@ JpgInput::open(const std::string& name, ImageSpec& newspec)
271293 m_spec.attribute (JPEG_SUBSAMPLING_ATTR, subsampling);
272294
273295 for (jpeg_saved_marker_ptr m = m_cinfo.marker_list ; m; m = m->next ) {
274- if (m->marker == (JPEG_APP0 + 1 ) && m->data_length >= 4
275- && !strncmp ((const char *)m->data , " Exif" , 4 )) {
296+ if (is_exif_marker (m)) {
276297 // The block starts with "Exif\0\0", so skip 6 bytes to get
277298 // to the start of the actual Exif data TIFF directory
278299 decode_exif (string_view ((char *)m->data + 6 , m->data_length - 6 ),
@@ -394,8 +415,7 @@ JpgInput::read_icc_profile(j_decompress_ptr cinfo, ImageSpec& spec)
394415 memset (marker_present, 0 , (MAX_SEQ_NO + 1 ));
395416
396417 for (jpeg_saved_marker_ptr m = cinfo->marker_list ; m; m = m->next ) {
397- if (m->marker == (JPEG_APP0 + 2 )
398- && !strcmp ((const char *)m->data , " ICC_PROFILE" )) {
418+ if (is_icc_profile_marker (m)) {
399419 if (num_markers == 0 )
400420 num_markers = GETJOCTET (m->data [13 ]);
401421 else if (num_markers != GETJOCTET (m->data [13 ]))
@@ -427,8 +447,7 @@ JpgInput::read_icc_profile(j_decompress_ptr cinfo, ImageSpec& spec)
427447
428448 // and fill it in
429449 for (jpeg_saved_marker_ptr m = cinfo->marker_list ; m; m = m->next ) {
430- if (m->marker == (JPEG_APP0 + 2 )
431- && !strcmp ((const char *)m->data , " ICC_PROFILE" )) {
450+ if (is_icc_profile_marker (m)) {
432451 int seq_no = GETJOCTET (m->data [12 ]);
433452 if (data_offset[seq_no] + data_length[seq_no] > icc_buf.size ()) {
434453 errorfmt (" Possible corrupt file, invalid ICC profile\n " );
0 commit comments