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