• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* exif-data.c
2  *
3  * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA.
19  */
20 
21 #include <config.h>
22 
23 #include <libexif/exif-mnote-data.h>
24 #include <libexif/exif-data.h>
25 #include <libexif/exif-ifd.h>
26 #include <libexif/exif-mnote-data-priv.h>
27 #include <libexif/exif-utils.h>
28 #include <libexif/exif-loader.h>
29 #include <libexif/exif-log.h>
30 #include <libexif/i18n.h>
31 #include <libexif/exif-system.h>
32 
33 #include <libexif/canon/exif-mnote-data-canon.h>
34 #include <libexif/fuji/exif-mnote-data-fuji.h>
35 #include <libexif/olympus/exif-mnote-data-olympus.h>
36 #include <libexif/pentax/exif-mnote-data-pentax.h>
37 
38 #include <inttypes.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 
43 #undef JPEG_MARKER_SOI
44 #define JPEG_MARKER_SOI  0xd8
45 #undef JPEG_MARKER_APP0
46 #define JPEG_MARKER_APP0 0xe0
47 #undef JPEG_MARKER_APP1
48 #define JPEG_MARKER_APP1 0xe1
49 
50 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
51 
52 struct _ExifDataPrivate
53 {
54 	ExifByteOrder order;
55 
56 	ExifMnoteData *md;
57 
58 	ExifLog *log;
59 	ExifMem *mem;
60 
61 	unsigned int ref_count;
62 
63 	/* Temporarily used while loading data */
64 	unsigned int offset_mnote;
65 
66 	ExifDataOption options;
67 	ExifDataType data_type;
68 };
69 
70 static void *
exif_data_alloc(ExifData * data,unsigned int i)71 exif_data_alloc (ExifData *data, unsigned int i)
72 {
73 	void *d;
74 
75 	if (!data || !i)
76 		return NULL;
77 
78 	d = exif_mem_alloc (data->priv->mem, i);
79 	if (d)
80 		return d;
81 
82 	EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
83 	return NULL;
84 }
85 
86 ExifMnoteData *
exif_data_get_mnote_data(ExifData * d)87 exif_data_get_mnote_data (ExifData *d)
88 {
89 	return (d && d->priv) ? d->priv->md : NULL;
90 }
91 
92 ExifData *
exif_data_new(void)93 exif_data_new (void)
94 {
95 	ExifMem *mem = exif_mem_new_default ();
96 	ExifData *d = exif_data_new_mem (mem);
97 
98 	exif_mem_unref (mem);
99 
100 	return d;
101 }
102 
103 ExifData *
exif_data_new_mem(ExifMem * mem)104 exif_data_new_mem (ExifMem *mem)
105 {
106 	ExifData *data;
107 	unsigned int i;
108 
109 	if (!mem)
110 		return NULL;
111 
112 	data = exif_mem_alloc (mem, sizeof (ExifData));
113 	if (!data)
114 		return (NULL);
115 	data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
116 	if (!data->priv) {
117 	  	exif_mem_free (mem, data);
118 		return (NULL);
119 	}
120 	data->priv->ref_count = 1;
121 
122 	data->priv->mem = mem;
123 	exif_mem_ref (mem);
124 
125 	for (i = 0; i < EXIF_IFD_COUNT; i++) {
126 		data->ifd[i] = exif_content_new_mem (data->priv->mem);
127 		if (!data->ifd[i]) {
128 			exif_data_free (data);
129 			return (NULL);
130 		}
131 		data->ifd[i]->parent = data;
132 	}
133 
134 	/* Default options */
135 #ifndef NO_VERBOSE_TAG_STRINGS
136 	/*
137 	 * When the tag list is compiled away, setting this option prevents
138 	 * any tags from being loaded
139 	 */
140 	exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
141 #endif
142 	exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
143 
144 	/* Default data type: none */
145 	exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
146 
147 	return (data);
148 }
149 
150 ExifData *
exif_data_new_from_data(const unsigned char * data,unsigned int size)151 exif_data_new_from_data (const unsigned char *data, unsigned int size)
152 {
153 	ExifData *edata;
154 
155 	edata = exif_data_new ();
156 	exif_data_load_data (edata, data, size);
157 	return (edata);
158 }
159 
160 static int
exif_data_load_data_entry(ExifData * data,ExifEntry * entry,const unsigned char * d,unsigned int size,unsigned int offset)161 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
162 			   const unsigned char *d,
163 			   unsigned int size, unsigned int offset)
164 {
165 	unsigned int s, doff;
166 
167 	entry->tag        = exif_get_short (d + offset + 0, data->priv->order);
168 	entry->format     = exif_get_short (d + offset + 2, data->priv->order);
169 	entry->components = exif_get_long  (d + offset + 4, data->priv->order);
170 
171 	/* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent
172 	 * has not been set yet
173 	 */
174 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
175 		  "Loading entry 0x%x ('%s')...", entry->tag,
176 		  exif_tag_get_name (entry->tag));
177 
178 	/* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
179 	 *   -> { 0x000000000 .. 0x7fffffff8 } */
180 	s = exif_format_get_size(entry->format) * entry->components;
181 	if ((s < entry->components) || (s == 0)){
182 		return 0;
183 	}
184 
185 	/*
186 	 * Size? If bigger than 4 bytes, the actual data is not
187 	 * in the entry but somewhere else (offset).
188 	 */
189 	if (s > 4)
190 		doff = exif_get_long (d + offset + 8, data->priv->order);
191 	else
192 		doff = offset + 8;
193 
194 	/* Sanity checks */
195 	int64_t doff64 = doff;
196 	int64_t s64 = s;
197 	if (doff64 + s64 > (int64_t) size) {
198 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
199 				  "Tag data past end of buffer (%" PRId64 " > %u)",
200 				  doff64+s64, size);
201 		return 0;
202 	}
203 
204 	entry->data = exif_data_alloc (data, s);
205 	if (entry->data) {
206 		entry->size = s;
207 		memcpy (entry->data, d + doff, s);
208 	} else {
209 		/* FIXME: What do our callers do if (entry->data == NULL)? */
210 		EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
211 	}
212 
213 	/* If this is the MakerNote, remember the offset */
214 	if (entry->tag == EXIF_TAG_MAKER_NOTE) {
215 		if (!entry->data) {
216 			exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
217 					  "MakerNote found with empty data");
218 		} else if (entry->size > 6) {
219 			exif_log (data->priv->log,
220 					       EXIF_LOG_CODE_DEBUG, "ExifData",
221 					       "MakerNote found (%02x %02x %02x %02x "
222 					       "%02x %02x %02x...).",
223 					       entry->data[0], entry->data[1], entry->data[2],
224 					       entry->data[3], entry->data[4], entry->data[5],
225 					       entry->data[6]);
226 		}
227 		data->priv->offset_mnote = doff;
228 	}
229 	return 1;
230 }
231 
232 static void
exif_data_save_data_entry(ExifData * data,ExifEntry * e,unsigned char ** d,unsigned int * ds,unsigned int offset)233 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
234 			   unsigned char **d, unsigned int *ds,
235 			   unsigned int offset)
236 {
237 	unsigned int doff, s;
238 	unsigned int ts;
239 
240 	if (!data || !data->priv)
241 		return;
242 
243 	/*
244 	 * Each entry is 12 bytes long. The memory for the entry has
245 	 * already been allocated.
246 	 */
247 	exif_set_short (*d + 6 + offset + 0,
248 			data->priv->order, (ExifShort) e->tag);
249 	exif_set_short (*d + 6 + offset + 2,
250 			data->priv->order, (ExifShort) e->format);
251 
252 	if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
253 		/* If this is the maker note tag, update it. */
254 		if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
255 			/* TODO: this is using the wrong ExifMem to free e->data */
256 			exif_mem_free (data->priv->mem, e->data);
257 			e->data = NULL;
258 			e->size = 0;
259 			exif_mnote_data_set_offset (data->priv->md, *ds - 6);
260 			exif_mnote_data_save (data->priv->md, &e->data, &e->size);
261 			e->components = e->size;
262 		}
263 	}
264 
265 	exif_set_long  (*d + 6 + offset + 4,
266 			data->priv->order, e->components);
267 
268 	/*
269 	 * Size? If bigger than 4 bytes, the actual data is not in
270 	 * the entry but somewhere else.
271 	 */
272 	s = exif_format_get_size (e->format) * e->components;
273 	if (s > 4) {
274 		unsigned char *t;
275 		doff = *ds - 6;
276 		ts = *ds + s;
277 
278 		/*
279 		 * According to the TIFF specification,
280 		 * the offset must be an even number. If we need to introduce
281 		 * a padding byte, we set it to 0.
282 		 */
283 		if (s & 1)
284 			ts++;
285 		t = exif_mem_realloc (data->priv->mem, *d, ts);
286 		if (!t) {
287 			EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
288 		  	return;
289 		}
290 		*d = t;
291 		*ds = ts;
292 		exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
293 		if (s & 1)
294 			*(*d + *ds - 1) = '\0';
295 
296 	} else
297 		doff = offset + 8;
298 
299 	/* Write the data. Fill unneeded bytes with 0. Do not crash with
300 	 * e->data is NULL */
301 	if (e->data) {
302 		unsigned int len = s;
303 		if (e->size < s) len = e->size;
304 		memcpy (*d + 6 + doff, e->data, len);
305 	} else {
306 		memset (*d + 6 + doff, 0, s);
307 	}
308 	if (s < 4)
309 		memset (*d + 6 + doff + s, 0, (4 - s));
310 }
311 
312 static void
exif_data_load_data_thumbnail(ExifData * data,const unsigned char * d,unsigned int ds,ExifLong o,ExifLong s)313 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
314 			       unsigned int ds, ExifLong o, ExifLong s)
315 {
316 	/* Sanity checks */
317 	if ((o + s < o) || (o + s < s) || (o + s > ds) || (o > ds)) {
318 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
319 			  "Bogus thumbnail offset (%u) or size (%u).",
320 			  o, s);
321 		return;
322 	}
323 
324 	if (data->data)
325 		exif_mem_free (data->priv->mem, data->data);
326 	if (!(data->data = exif_data_alloc (data, s))) {
327 		EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
328 		data->size = 0;
329 		return;
330 	}
331 	data->size = s;
332 	memcpy (data->data, d + o, s);
333 }
334 
335 #undef CHECK_REC
336 #define CHECK_REC(i) 					\
337 if ((i) == ifd) {				\
338 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
339 		"ExifData", "Recursive entry in IFD "	\
340 		"'%s' detected. Skipping...",		\
341 		exif_ifd_get_name (i));			\
342 	break;						\
343 }							\
344 if (data->ifd[(i)]->count) {				\
345 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,	\
346 		"ExifData", "Attempt to load IFD "	\
347 		"'%s' multiple times detected. "	\
348 		"Skipping...",				\
349 		exif_ifd_get_name (i));			\
350 	break;						\
351 }
352 
353 /*! Load data for an IFD.
354  *
355  * \param[in,out] data #ExifData
356  * \param[in] ifd IFD to load
357  * \param[in] d pointer to buffer containing raw IFD data
358  * \param[in] ds size of raw data in buffer at \c d
359  * \param[in] offset offset into buffer at \c d at which IFD starts
360  * \param[in] recursion_depth number of times this function has been
361  * recursively called without returning
362  */
363 static void
exif_data_load_data_content(ExifData * data,ExifIfd ifd,const unsigned char * d,unsigned int ds,unsigned int offset,unsigned int recursion_depth)364 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
365 			     const unsigned char *d,
366 			     unsigned int ds, unsigned int offset, unsigned int recursion_depth)
367 {
368 	ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
369 	ExifShort n;
370 	ExifEntry *entry;
371 	unsigned int i;
372 	ExifTag tag;
373 
374 	if (!data || !data->priv)
375 		return;
376 
377 	/* check for valid ExifIfd enum range */
378 	if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
379 	  return;
380 
381 	if (recursion_depth > 30) {
382 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
383 			  "Deep recursion detected!");
384 		return;
385 	}
386 
387 	/* Read the number of entries */
388 	if ((offset + 2 < offset) || (offset + 2 < 2) || (offset + 2 > ds)) {
389 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
390 			  "Tag data past end of buffer (%u > %u)", offset+2, ds);
391 		return;
392 	}
393 	n = exif_get_short (d + offset, data->priv->order);
394 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
395 	          "Loading %hu entries...", n);
396 	offset += 2;
397 
398 	/* Check if we have enough data. */
399 	if (offset + 12 * n > ds) {
400 		n = (ds - offset) / 12;
401 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
402 				  "Short data; only loading %hu entries...", n);
403 	}
404 
405 	for (i = 0; i < n; i++) {
406 
407 		tag = exif_get_short (d + offset + 12 * i, data->priv->order);
408 		switch (tag) {
409 		case EXIF_TAG_EXIF_IFD_POINTER:
410 		case EXIF_TAG_GPS_INFO_IFD_POINTER:
411 		case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
412 		case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
413 		case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
414 			o = exif_get_long (d + offset + 12 * i + 8,
415 					   data->priv->order);
416 			/* FIXME: IFD_POINTER tags aren't marked as being in a
417 			 * specific IFD, so exif_tag_get_name_in_ifd won't work
418 			 */
419 			exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
420 				  "Sub-IFD entry 0x%x ('%s') at %u.", tag,
421 				  exif_tag_get_name(tag), o);
422 			switch (tag) {
423 			case EXIF_TAG_EXIF_IFD_POINTER:
424 				CHECK_REC (EXIF_IFD_EXIF);
425 				exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
426 				break;
427 			case EXIF_TAG_GPS_INFO_IFD_POINTER:
428 				CHECK_REC (EXIF_IFD_GPS);
429 				exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
430 				break;
431 			case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
432 				CHECK_REC (EXIF_IFD_INTEROPERABILITY);
433 				exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
434 				break;
435 			case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
436 				thumbnail_offset = o;
437 				if (thumbnail_offset && thumbnail_length)
438 					exif_data_load_data_thumbnail (data, d,
439 								       ds, thumbnail_offset,
440 								       thumbnail_length);
441 				break;
442 			case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
443 				thumbnail_length = o;
444 				if (thumbnail_offset && thumbnail_length)
445 					exif_data_load_data_thumbnail (data, d,
446 								       ds, thumbnail_offset,
447 								       thumbnail_length);
448 				break;
449 			default:
450 				return;
451 			}
452 			break;
453 		default:
454 
455 			/*
456 			 * If we don't know the tag, don't fail. It could be that new
457 			 * versions of the standard have defined additional tags. Note that
458 			 * 0 is a valid tag in the GPS IFD.
459 			 */
460 			if (!exif_tag_get_name_in_ifd (tag, ifd)) {
461 
462 				/*
463 				 * Special case: Tag and format 0. That's against specification
464 				 * (at least up to 2.2). But Photoshop writes it anyways.
465 				 */
466 				if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
467 					exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
468 						  "Skipping empty entry at position %u in '%s'.", i,
469 						  exif_ifd_get_name (ifd));
470 					break;
471 				}
472 				exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
473 					  "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
474 					  "to <libexif-devel@lists.sourceforge.net>.", tag, i,
475 					  exif_ifd_get_name (ifd));
476 				if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
477 					break;
478 			}
479 			entry = exif_entry_new_mem (data->priv->mem);
480 			if (exif_data_load_data_entry (data, entry, d, ds,
481 						   offset + 12 * i))
482 				exif_content_add_entry (data->ifd[ifd], entry);
483 			exif_entry_unref (entry);
484 			break;
485 		}
486 	}
487 }
488 
489 static int
cmp_func(const unsigned char * p1,const unsigned char * p2,ExifByteOrder o)490 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
491 {
492 	ExifShort tag1 = exif_get_short (p1, o);
493 	ExifShort tag2 = exif_get_short (p2, o);
494 
495 	return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
496 }
497 
498 static int
cmp_func_intel(const void * elem1,const void * elem2)499 cmp_func_intel (const void *elem1, const void *elem2)
500 {
501 	return cmp_func ((const unsigned char *) elem1,
502 			 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
503 }
504 
505 static int
cmp_func_motorola(const void * elem1,const void * elem2)506 cmp_func_motorola (const void *elem1, const void *elem2)
507 {
508 	return cmp_func ((const unsigned char *) elem1,
509 			 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
510 }
511 
512 static void
exif_data_save_data_content(ExifData * data,ExifContent * ifd,unsigned char ** d,unsigned int * ds,unsigned int offset)513 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
514 			     unsigned char **d, unsigned int *ds,
515 			     unsigned int offset)
516 {
517 	unsigned int j, n_ptr = 0, n_thumb = 0;
518 	ExifIfd i;
519 	unsigned char *t;
520 	unsigned int ts;
521 
522 	if (!data || !data->priv || !ifd || !d || !ds)
523 		return;
524 
525 	for (i = 0; i < EXIF_IFD_COUNT; i++)
526 		if (ifd == data->ifd[i])
527 			break;
528 	if (i == EXIF_IFD_COUNT)
529 		return;	/* error */
530 
531 	/*
532 	 * Check if we need some extra entries for pointers or the thumbnail.
533 	 */
534 	switch (i) {
535 	case EXIF_IFD_0:
536 
537 		/*
538 		 * The pointer to IFD_EXIF is in IFD_0. The pointer to
539 		 * IFD_INTEROPERABILITY is in IFD_EXIF.
540 		 */
541 		if (data->ifd[EXIF_IFD_EXIF]->count ||
542 		    data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
543 			n_ptr++;
544 
545 		/* The pointer to IFD_GPS is in IFD_0. */
546 		if (data->ifd[EXIF_IFD_GPS]->count)
547 			n_ptr++;
548 
549 		break;
550 	case EXIF_IFD_1:
551 		if (data->size)
552 			n_thumb = 2;
553 		break;
554 	case EXIF_IFD_EXIF:
555 		if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
556 			n_ptr++;
557 	default:
558 		break;
559 	}
560 
561 	/*
562 	 * Allocate enough memory for all entries
563 	 * and the number of entries.
564 	 */
565 	ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
566 	t = exif_mem_realloc (data->priv->mem, *d, ts);
567 	if (!t) {
568 		EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
569 	  	return;
570 	}
571 	*d = t;
572 	*ds = ts;
573 
574 	/* Save the number of entries */
575 	exif_set_short (*d + 6 + offset, data->priv->order,
576 			(ExifShort) (ifd->count + n_ptr + n_thumb));
577 	offset += 2;
578 
579 	/*
580 	 * Save each entry. Make sure that no memcpys from NULL pointers are
581 	 * performed
582 	 */
583 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
584 		  "Saving %i entries (IFD '%s', offset: %i)...",
585 		  ifd->count, exif_ifd_get_name (i), offset);
586 	for (j = 0; j < ifd->count; j++) {
587 		if (ifd->entries[j]) {
588 			exif_data_save_data_entry (data, ifd->entries[j], d, ds,
589 				offset + 12 * j);
590 		}
591 	}
592 
593 	offset += 12 * ifd->count;
594 
595 	/* Now save special entries. */
596 	switch (i) {
597 	case EXIF_IFD_0:
598 
599 		/*
600 		 * The pointer to IFD_EXIF is in IFD_0.
601 		 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
602 		 * therefore, if IFD_INTEROPERABILITY is not empty, we need
603 		 * IFD_EXIF even if latter is empty.
604 		 */
605 		if (data->ifd[EXIF_IFD_EXIF]->count ||
606 		    data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
607 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
608 					EXIF_TAG_EXIF_IFD_POINTER);
609 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
610 					EXIF_FORMAT_LONG);
611 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
612 					1);
613 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
614 					*ds - 6);
615 			exif_data_save_data_content (data,
616 						     data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
617 			offset += 12;
618 		}
619 
620 		/* The pointer to IFD_GPS is in IFD_0, too. */
621 		if (data->ifd[EXIF_IFD_GPS]->count) {
622 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
623 					EXIF_TAG_GPS_INFO_IFD_POINTER);
624 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
625 					EXIF_FORMAT_LONG);
626 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
627 					1);
628 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
629 					*ds - 6);
630 			exif_data_save_data_content (data,
631 						     data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
632 			offset += 12;
633 		}
634 
635 		break;
636 	case EXIF_IFD_EXIF:
637 
638 		/*
639 		 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
640 		 * See note above.
641 		 */
642 		if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
643 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
644 					EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
645 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
646 					EXIF_FORMAT_LONG);
647 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
648 					1);
649 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
650 					*ds - 6);
651 			exif_data_save_data_content (data,
652 						     data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
653 						     *ds - 6);
654 			offset += 12;
655 		}
656 
657 		break;
658 	case EXIF_IFD_1:
659 
660 		/*
661 		 * Information about the thumbnail (if any) is saved in
662 		 * IFD_1.
663 		 */
664 		if (data->size) {
665 
666 			/* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
667 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
668 					EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
669 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
670 					EXIF_FORMAT_LONG);
671 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
672 					1);
673 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
674 					*ds - 6);
675 			ts = *ds + data->size;
676 			t = exif_mem_realloc (data->priv->mem, *d, ts);
677 			if (!t) {
678 				EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
679 						    ts);
680 			  	return;
681 			}
682 			*d = t;
683 			*ds = ts;
684 			memcpy (*d + *ds - data->size, data->data, data->size);
685 			offset += 12;
686 
687 			/* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
688 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
689 					EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
690 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
691 					EXIF_FORMAT_LONG);
692 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
693 					1);
694 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
695 					data->size);
696 			offset += 12;
697 		}
698 
699 		break;
700 	default:
701 		break;
702 	}
703 
704 	/* Sort the directory according to TIFF specification */
705 	qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
706 	       (ifd->count + n_ptr + n_thumb), 12,
707 	       (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
708 
709 	/* Correctly terminate the directory */
710 	if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
711 				data->size)) {
712 
713 		/*
714 		 * We are saving IFD 0. Tell where IFD 1 starts and save
715 		 * IFD 1.
716 		 */
717 		exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
718 		exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
719 					     *ds - 6);
720 	} else
721 		exif_set_long (*d + 6 + offset, data->priv->order, 0);
722 }
723 
724 typedef enum {
725 	EXIF_DATA_TYPE_MAKER_NOTE_NONE		= 0,
726 	EXIF_DATA_TYPE_MAKER_NOTE_CANON		= 1,
727 	EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS	= 2,
728 	EXIF_DATA_TYPE_MAKER_NOTE_PENTAX	= 3,
729 	EXIF_DATA_TYPE_MAKER_NOTE_NIKON		= 4,
730 	EXIF_DATA_TYPE_MAKER_NOTE_CASIO		= 5,
731 	EXIF_DATA_TYPE_MAKER_NOTE_FUJI 		= 6
732 } ExifDataTypeMakerNote;
733 
734 /*! If MakerNote is recognized, load it.
735  *
736  * \param[in,out] data #ExifData
737  * \param[in] d pointer to raw EXIF data
738  * \param[in] ds length of data at d
739  */
740 static void
interpret_maker_note(ExifData * data,const unsigned char * d,unsigned int ds)741 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
742 {
743 	int mnoteid;
744 	ExifEntry* e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
745 	if (!e)
746 		return;
747 
748 	if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
749 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
750 			"ExifData", "Olympus MakerNote variant type %d", mnoteid);
751 		data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
752 
753 	} else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
754 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
755 			"ExifData", "Canon MakerNote variant type %d", mnoteid);
756 		data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
757 
758 	} else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
759 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
760 			"ExifData", "Fuji MakerNote variant type %d", mnoteid);
761 		data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
762 
763 	/* NOTE: Must do Pentax detection last because some of the
764 	 * heuristics are pretty general. */
765 	} else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
766 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
767 			"ExifData", "Pentax MakerNote variant type %d", mnoteid);
768 		data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
769 	}
770 
771 	/*
772 	 * If we are able to interpret the maker note, do so.
773 	 */
774 	if (data->priv->md) {
775 		exif_mnote_data_log (data->priv->md, data->priv->log);
776 		exif_mnote_data_set_byte_order (data->priv->md,
777 						data->priv->order);
778 		exif_mnote_data_set_offset (data->priv->md,
779 					    data->priv->offset_mnote);
780 		exif_mnote_data_load (data->priv->md, d, ds);
781 	}
782 }
783 
784 #define LOG_TOO_SMALL \
785 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
786 		_("Size of data too small to allow for EXIF data."));
787 
788 void
exif_data_load_data(ExifData * data,const unsigned char * d_orig,unsigned int ds)789 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
790 		     unsigned int ds)
791 {
792 	unsigned int l;
793 	ExifLong offset;
794 	ExifShort n;
795 	const unsigned char *d = d_orig;
796 	unsigned int len, fullds;
797 
798 	if (!data || !data->priv || !d || !ds)
799 		return;
800 
801 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
802 		  "Parsing %i byte(s) EXIF data...\n", ds);
803 
804 	/*
805 	 * It can be that the data starts with the EXIF header. If it does
806 	 * not, search the EXIF marker.
807 	 */
808 	if (ds < 6) {
809 		LOG_TOO_SMALL;
810 		return;
811 	}
812 	if (!memcmp (d, ExifHeader, 6)) {
813 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
814 			  "Found EXIF header.");
815 	} else {
816 		while (ds >= 3) {
817 			while (ds && (d[0] == 0xff)) {
818 				d++;
819 				ds--;
820 			}
821 
822 			/* JPEG_MARKER_SOI */
823 			if (ds && d[0] == JPEG_MARKER_SOI) {
824 				d++;
825 				ds--;
826 				continue;
827 			}
828 
829 			/* JPEG_MARKER_APP0 */
830 			if (ds >= 3 && d[0] == JPEG_MARKER_APP0) {
831 				d++;
832 				ds--;
833 				l = (d[0] << 8) | d[1];
834 				if (l > ds)
835 					return;
836 				d += l;
837 				ds -= l;
838 				continue;
839 			}
840 
841 			/* JPEG_MARKER_APP1 */
842 			if (ds && d[0] == JPEG_MARKER_APP1)
843 				break;
844 
845 			/* Unknown marker or data. Give up. */
846 			exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
847 				  "ExifData", _("EXIF marker not found."));
848 			return;
849 		}
850 		if (ds < 3) {
851 			LOG_TOO_SMALL;
852 			return;
853 		}
854 		d++;
855 		ds--;
856 		len = (d[0] << 8) | d[1];
857 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
858 			  "We have to deal with %i byte(s) of EXIF data.",
859 			  len);
860 		d += 2;
861 		ds -= 2;
862 	}
863 
864 	/*
865 	 * Verify the exif header
866 	 * (offset 2, length 6).
867 	 */
868 	if (ds < 6) {
869 		LOG_TOO_SMALL;
870 		return;
871 	}
872 	if (memcmp (d, ExifHeader, 6)) {
873 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
874 			  "ExifData", _("EXIF header not found."));
875 		return;
876 	}
877 
878 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
879 		  "Found EXIF header.");
880 
881 	/* Sanity check the data length */
882 	if (ds < 14)
883 		return;
884 
885 	/* The JPEG APP1 section can be no longer than 64 KiB (including a
886 	   16-bit length), so cap the data length to protect against overflow
887 	   in future offset calculations */
888 	fullds = ds;
889 	if (ds > 0xfffe)
890 		ds = 0xfffe;
891 
892 	/* Byte order (offset 6, length 2) */
893 	if (!memcmp (d + 6, "II", 2))
894 		data->priv->order = EXIF_BYTE_ORDER_INTEL;
895 	else if (!memcmp (d + 6, "MM", 2))
896 		data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
897 	else {
898 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
899 			  "ExifData", _("Unknown encoding."));
900 		return;
901 	}
902 
903 	/* Fixed value */
904 	if (exif_get_short (d + 8, data->priv->order) != 0x002a)
905 		return;
906 
907 	/* IFD 0 offset */
908 	offset = exif_get_long (d + 10, data->priv->order);
909 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
910 		  "IFD 0 at %i.", (int) offset);
911 
912 	/* Sanity check the offset, being careful about overflow */
913 	if (offset > ds || (uint64_t)offset + 6 + 2 > ds)
914 		return;
915 
916 	/* Parse the actual exif data (usually offset 14 from start) */
917 	exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
918 
919 	/* IFD 1 offset */
920 	n = exif_get_short (d + 6 + offset, data->priv->order);
921 	if ((uint64_t)offset + 6 + 2 + 12 * n + 4 > ds)
922 		return;
923 
924 	offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
925 	if (offset) {
926 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
927 			  "IFD 1 at %i.", (int) offset);
928 
929 		/* Sanity check. */
930 		if (offset > ds || (uint64_t)offset + 6 > ds) {
931 			exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
932 				  "ExifData", "Bogus offset of IFD1.");
933 		} else {
934 		   exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
935 		}
936 	}
937 
938 	/*
939 	 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
940 	 * cameras use pointers in the maker note tag that point to the
941 	 * space between IFDs. Here is the only place where we have access
942 	 * to that data.
943 	 */
944 	interpret_maker_note(data, d, fullds);
945 
946 	/* Fixup tags if requested */
947 	if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
948 		exif_data_fix (data);
949 }
950 
951 void
exif_data_save_data(ExifData * data,unsigned char ** d,unsigned int * ds)952 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
953 {
954 	if (ds)
955 		*ds = 0;	/* This means something went wrong */
956 
957 	if (!data || !d || !ds)
958 		return;
959 
960 	/* Header */
961 	*ds = 14;
962 	*d = exif_data_alloc (data, *ds);
963 	if (!*d)  {
964 		*ds = 0;
965 		return;
966 	}
967 	memcpy (*d, ExifHeader, 6);
968 
969 	/* Order (offset 6) */
970 	if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
971 		memcpy (*d + 6, "II", 2);
972 	} else {
973 		memcpy (*d + 6, "MM", 2);
974 	}
975 
976 	/* Fixed value (2 bytes, offset 8) */
977 	exif_set_short (*d + 8, data->priv->order, 0x002a);
978 
979 	/*
980 	 * IFD 0 offset (4 bytes, offset 10).
981 	 * We will start 8 bytes after the
982 	 * EXIF header (2 bytes for order, another 2 for the test, and
983 	 * 4 bytes for the IFD 0 offset make 8 bytes together).
984 	 */
985 	exif_set_long (*d + 10, data->priv->order, 8);
986 
987 	/* Now save IFD 0. IFD 1 will be saved automatically. */
988 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
989 		  "Saving IFDs...");
990 	exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
991 				     *ds - 6);
992 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
993 		  "Saved %i byte(s) EXIF data.", *ds);
994 }
995 
996 ExifData *
exif_data_new_from_file(const char * path)997 exif_data_new_from_file (const char *path)
998 {
999 	ExifData *edata;
1000 	ExifLoader *loader;
1001 
1002 	loader = exif_loader_new ();
1003 	exif_loader_write_file (loader, path);
1004 	edata = exif_loader_get_data (loader);
1005 	exif_loader_unref (loader);
1006 
1007 	return (edata);
1008 }
1009 
1010 void
exif_data_ref(ExifData * data)1011 exif_data_ref (ExifData *data)
1012 {
1013 	if (!data)
1014 		return;
1015 
1016 	data->priv->ref_count++;
1017 }
1018 
1019 void
exif_data_unref(ExifData * data)1020 exif_data_unref (ExifData *data)
1021 {
1022 	if (!data)
1023 		return;
1024 
1025 	data->priv->ref_count--;
1026 	if (!data->priv->ref_count)
1027 		exif_data_free (data);
1028 }
1029 
1030 void
exif_data_free(ExifData * data)1031 exif_data_free (ExifData *data)
1032 {
1033 	unsigned int i;
1034 	ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1035 
1036 	if (!data)
1037 		return;
1038 
1039 	for (i = 0; i < EXIF_IFD_COUNT; i++) {
1040 		if (data->ifd[i]) {
1041 			exif_content_unref (data->ifd[i]);
1042 			data->ifd[i] = NULL;
1043 		}
1044 	}
1045 
1046 	if (data->data) {
1047 		exif_mem_free (mem, data->data);
1048 		data->data = NULL;
1049 	}
1050 
1051 	if (data->priv) {
1052 		if (data->priv->log) {
1053 			exif_log_unref (data->priv->log);
1054 			data->priv->log = NULL;
1055 		}
1056 		if (data->priv->md) {
1057 			exif_mnote_data_unref (data->priv->md);
1058 			data->priv->md = NULL;
1059 		}
1060 		exif_mem_free (mem, data->priv);
1061 		exif_mem_free (mem, data);
1062 	}
1063 
1064 	exif_mem_unref (mem);
1065 }
1066 
1067 void
exif_data_dump(ExifData * data)1068 exif_data_dump (ExifData *data)
1069 {
1070 	unsigned int i;
1071 
1072 	if (!data)
1073 		return;
1074 
1075 	for (i = 0; i < EXIF_IFD_COUNT; i++) {
1076 		if (data->ifd[i] && data->ifd[i]->count) {
1077 			printf ("Dumping IFD '%s'...\n",
1078 				exif_ifd_get_name (i));
1079 			exif_content_dump (data->ifd[i], 0);
1080 		}
1081 	}
1082 
1083 	if (data->data) {
1084 		printf ("%i byte(s) thumbnail data available.", data->size);
1085 		if (data->size >= 4) {
1086 			printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1087 				data->data[0], data->data[1],
1088 				data->data[data->size - 2],
1089 				data->data[data->size - 1]);
1090 		}
1091 	}
1092 }
1093 
1094 ExifByteOrder
exif_data_get_byte_order(ExifData * data)1095 exif_data_get_byte_order (ExifData *data)
1096 {
1097 	if (!data)
1098 		return (0);
1099 
1100 	return (data->priv->order);
1101 }
1102 
1103 void
exif_data_foreach_content(ExifData * data,ExifDataForeachContentFunc func,void * user_data)1104 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1105 			   void *user_data)
1106 {
1107 	unsigned int i;
1108 
1109 	if (!data || !func)
1110 		return;
1111 
1112 	for (i = 0; i < EXIF_IFD_COUNT; i++)
1113 		func (data->ifd[i], user_data);
1114 }
1115 
1116 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1117 struct _ByteOrderChangeData {
1118 	ExifByteOrder old, new;
1119 };
1120 
1121 static void
entry_set_byte_order(ExifEntry * e,void * data)1122 entry_set_byte_order (ExifEntry *e, void *data)
1123 {
1124 	ByteOrderChangeData *d = data;
1125 
1126 	if (!e)
1127 		return;
1128 
1129 	exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1130 }
1131 
1132 static void
content_set_byte_order(ExifContent * content,void * data)1133 content_set_byte_order (ExifContent *content, void *data)
1134 {
1135 	exif_content_foreach_entry (content, entry_set_byte_order, data);
1136 }
1137 
1138 void
exif_data_set_byte_order(ExifData * data,ExifByteOrder order)1139 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1140 {
1141 	ByteOrderChangeData d;
1142 
1143 	if (!data || (order == data->priv->order))
1144 		return;
1145 
1146 	d.old = data->priv->order;
1147 	d.new = order;
1148 	exif_data_foreach_content (data, content_set_byte_order, &d);
1149 	data->priv->order = order;
1150 	if (data->priv->md)
1151 		exif_mnote_data_set_byte_order (data->priv->md, order);
1152 }
1153 
1154 void
exif_data_log(ExifData * data,ExifLog * log)1155 exif_data_log (ExifData *data, ExifLog *log)
1156 {
1157 	unsigned int i;
1158 
1159 	if (!data || !data->priv)
1160 		return;
1161 	exif_log_unref (data->priv->log);
1162 	data->priv->log = log;
1163 	exif_log_ref (log);
1164 
1165 	for (i = 0; i < EXIF_IFD_COUNT; i++)
1166 		exif_content_log (data->ifd[i], log);
1167 }
1168 
1169 /* Used internally within libexif */
1170 ExifLog *exif_data_get_log (ExifData *);
1171 ExifLog *
exif_data_get_log(ExifData * data)1172 exif_data_get_log (ExifData *data)
1173 {
1174 	if (!data || !data->priv)
1175 		return NULL;
1176 	return data->priv->log;
1177 }
1178 
1179 static const struct {
1180 	ExifDataOption option;
1181 	const char *name;
1182 	const char *description;
1183 } exif_data_option[] = {
1184 	{EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1185 	 N_("Ignore unknown tags when loading EXIF data.")},
1186 	{EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1187 	 N_("Add, correct and remove entries to get EXIF data that follows "
1188 	    "the specification.")},
1189 	{EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1190 	 N_("When loading and resaving Exif data, save the maker note unmodified."
1191 	    " Be aware that the maker note can get corrupted.")},
1192 	{0, NULL, NULL}
1193 };
1194 
1195 const char *
exif_data_option_get_name(ExifDataOption o)1196 exif_data_option_get_name (ExifDataOption o)
1197 {
1198 	unsigned int i;
1199 
1200 	for (i = 0; exif_data_option[i].name; i++)
1201 		if (exif_data_option[i].option == o)
1202 			break;
1203 	return _(exif_data_option[i].name);
1204 }
1205 
1206 const char *
exif_data_option_get_description(ExifDataOption o)1207 exif_data_option_get_description (ExifDataOption o)
1208 {
1209 	unsigned int i;
1210 
1211 	for (i = 0; exif_data_option[i].description; i++)
1212 		if (exif_data_option[i].option == o)
1213 			break;
1214 	return _(exif_data_option[i].description);
1215 }
1216 
1217 void
exif_data_set_option(ExifData * d,ExifDataOption o)1218 exif_data_set_option (ExifData *d, ExifDataOption o)
1219 {
1220 	if (!d)
1221 		return;
1222 
1223 	d->priv->options |= o;
1224 }
1225 
1226 void
exif_data_unset_option(ExifData * d,ExifDataOption o)1227 exif_data_unset_option (ExifData *d, ExifDataOption o)
1228 {
1229 	if (!d)
1230 		return;
1231 
1232 	d->priv->options &= ~o;
1233 }
1234 
1235 static void
fix_func(ExifContent * c,void * UNUSED (data))1236 fix_func (ExifContent *c, void *UNUSED(data))
1237 {
1238 	switch (exif_content_get_ifd (c)) {
1239 	case EXIF_IFD_1:
1240 		if (c->parent->data)
1241 			exif_content_fix (c);
1242 		else if (c->count) {
1243 			exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1244 				  "No thumbnail but entries on thumbnail. These entries have been "
1245 				  "removed.");
1246 			while (c->count) {
1247 				unsigned int cnt = c->count;
1248 				exif_content_remove_entry (c, c->entries[c->count - 1]);
1249 				if (cnt == c->count) {
1250 					/* safety net */
1251 					exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1252 					"failed to remove last entry from entries.");
1253 					c->count--;
1254 				}
1255 			}
1256 		}
1257 		break;
1258 	default:
1259 		exif_content_fix (c);
1260 	}
1261 }
1262 
1263 void
exif_data_fix(ExifData * d)1264 exif_data_fix (ExifData *d)
1265 {
1266 	exif_data_foreach_content (d, fix_func, NULL);
1267 }
1268 
1269 void
exif_data_set_data_type(ExifData * d,ExifDataType dt)1270 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1271 {
1272 	if (!d || !d->priv)
1273 		return;
1274 
1275 	d->priv->data_type = dt;
1276 }
1277 
1278 ExifDataType
exif_data_get_data_type(ExifData * d)1279 exif_data_get_data_type (ExifData *d)
1280 {
1281 	return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;
1282 }
1283