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