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