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