• 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 #include <libexif/huawei/exif-mnote-data-huawei.h>
39 
40 #include <math.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 
45 #undef JPEG_MARKER_SOI
46 #define JPEG_MARKER_SOI  0xd8
47 #undef JPEG_MARKER_APP0
48 #define JPEG_MARKER_APP0 0xe0
49 #undef JPEG_MARKER_APP1
50 #define JPEG_MARKER_APP1 0xe1
51 #undef JPEG_HEADER_LEN
52 #define JPEG_HEADER_LEN 6
53 
54 #define CHECKOVERFLOW(offset,datasize,structsize) (( offset >= datasize) || (structsize > datasize) || (offset > datasize - structsize ))
55 
56 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
57 
58 struct _ExifDataPrivate
59 {
60 	ExifByteOrder order;
61 
62 	ExifMnoteData *md;
63 
64 	ExifLog *log;
65 	ExifMem *mem;
66 
67 	unsigned int ref_count;
68 
69 	/* Temporarily used while loading data */
70 	unsigned int offset_mnote;
71 
72 	ExifDataOption options;
73 	ExifDataType data_type;
74 };
75 
76 static void *
exif_data_alloc(ExifData * data,unsigned int i)77 exif_data_alloc (ExifData *data, unsigned int i)
78 {
79 	void *d;
80 
81 	if (!data || !i)
82 		return NULL;
83 
84 	d = exif_mem_alloc (data->priv->mem, i);
85 	if (d)
86 		return d;
87 
88 	EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
89 	return NULL;
90 }
91 
92 ExifMnoteData *
exif_data_get_mnote_data(ExifData * d)93 exif_data_get_mnote_data (ExifData *d)
94 {
95 	return (d && d->priv) ? d->priv->md : NULL;
96 }
97 
98 ExifData *
exif_data_new(void)99 exif_data_new (void)
100 {
101 	ExifMem *mem = exif_mem_new_default ();
102 	ExifData *d = exif_data_new_mem (mem);
103 
104 	exif_mem_unref (mem);
105 
106 	return d;
107 }
108 
109 ExifData *
exif_data_new_mem(ExifMem * mem)110 exif_data_new_mem (ExifMem *mem)
111 {
112 	ExifData *data;
113 	unsigned int i;
114 
115 	if (!mem)
116 		return NULL;
117 
118 	data = exif_mem_alloc (mem, sizeof (ExifData));
119 	if (!data)
120 		return (NULL);
121 	data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
122 	if (!data->priv) {
123 	  	exif_mem_free (mem, data);
124 		return (NULL);
125 	}
126 	data->priv->ref_count = 1;
127 
128 	data->priv->mem = mem;
129 	exif_mem_ref (mem);
130 
131 	for (i = 0; i < EXIF_IFD_COUNT; i++) {
132 		data->ifd[i] = exif_content_new_mem (data->priv->mem);
133 		if (!data->ifd[i]) {
134 			exif_data_free (data);
135 			return (NULL);
136 		}
137 		data->ifd[i]->parent = data;
138 	}
139 
140 	/* Default options */
141 #ifndef NO_VERBOSE_TAG_STRINGS
142 	/*
143 	 * When the tag list is compiled away, setting this option prevents
144 	 * any tags from being loaded
145 	 */
146 	exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
147 #endif
148 	exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
149 
150 	/* Default data type: none */
151 	exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
152 
153 	return (data);
154 }
155 
156 ExifData *
exif_data_new_from_data(const unsigned char * data,unsigned int size)157 exif_data_new_from_data (const unsigned char *data, unsigned int size)
158 {
159 	ExifData *edata;
160 
161 	edata = exif_data_new ();
162 	exif_data_load_data (edata, data, size);
163 	return (edata);
164 }
165 
166 static int
exif_data_load_data_entry(ExifData * data,ExifEntry * entry,const unsigned char * d,unsigned int size,unsigned int offset)167 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
168 			   const unsigned char *d,
169 			   unsigned int size, unsigned int offset)
170 {
171 	unsigned int s, doff;
172 
173 	entry->tag        = exif_get_short (d + offset + 0, data->priv->order);
174 	entry->format     = exif_get_short (d + offset + 2, data->priv->order);
175 	entry->components = exif_get_long  (d + offset + 4, data->priv->order);
176 
177 	/* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent
178 	 * has not been set yet
179 	 */
180 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
181 		  "Loading entry 0x%x ('%s')...", entry->tag,
182 		  exif_tag_get_name (entry->tag));
183 
184 	/* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
185 	 *   -> { 0x000000000 .. 0x7fffffff8 } */
186 	s = exif_format_get_size(entry->format) * entry->components;
187 	if ((s < entry->components) || (s == 0)){
188 		return 0;
189 	}
190 
191 	/*
192 	 * Size? If bigger than 4 bytes, the actual data is not
193 	 * in the entry but somewhere else (offset).
194 	 */
195 	if (s > 4)
196 		doff = exif_get_long (d + offset + 8, data->priv->order);
197 	else
198 		doff = offset + 8;
199 
200 	/* Sanity checks */
201 	if (doff >= size) {
202 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
203 				  "Tag starts past end of buffer (%u > %u)", doff, size);
204 		return 0;
205 	}
206 
207 	if (s > size - doff) {
208 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
209 				  "Tag data goes past end of buffer (%u > %u)", doff+s, size);
210 		return 0;
211 	}
212 
213 	entry->data = exif_data_alloc (data, s);
214 	if (entry->data) {
215 		entry->size = s;
216 		memcpy (entry->data, d + doff, s);
217 	} else {
218 		EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
219 		return 0;
220 	}
221 
222 	/* If this is the MakerNote, remember the offset */
223 	if (entry->tag == EXIF_TAG_MAKER_NOTE) {
224 		if (!entry->data) {
225 			exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
226 					  "MakerNote found with empty data");
227 		} else if (entry->size > 6) {
228 			exif_log (data->priv->log,
229 					       EXIF_LOG_CODE_DEBUG, "ExifData",
230 					       "MakerNote found (%02x %02x %02x %02x "
231 					       "%02x %02x %02x...).",
232 					       entry->data[0], entry->data[1], entry->data[2],
233 					       entry->data[3], entry->data[4], entry->data[5],
234 					       entry->data[6]);
235 		}
236 
237 		if (!data->priv->offset_mnote) {
238 			data->priv->offset_mnote = doff;
239 		}
240 		if (entry->data && (entry->size >= 8) &&
241 			!memcmp(entry->data, HUAWEI_HEADER, 8)) {
242 			data->priv->offset_mnote = doff;
243 		}
244 	}
245 	return 1;
246 }
247 
248 static void
exif_data_save_data_entry(ExifData * data,ExifEntry * e,unsigned char ** d,unsigned int * ds,unsigned int offset)249 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
250 			   unsigned char **d, unsigned int *ds,
251 			   unsigned int offset)
252 {
253 	unsigned int doff, s;
254 	unsigned int ts;
255 
256 	if (!data || !data->priv)
257 		return;
258 
259 	/*
260 	 * Each entry is 12 bytes long. The memory for the entry has
261 	 * already been allocated.
262 	 */
263 	exif_set_short (*d + 6 + offset + 0,
264 			data->priv->order, (ExifShort) e->tag);
265 	exif_set_short (*d + 6 + offset + 2,
266 			data->priv->order, (ExifShort) e->format);
267 
268 	if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
269 		/* If this is the maker note tag, update it. */
270 		do {
271 			if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
272 				if (is_huawei_md(data->priv->md) &&
273 					e->data && (e->size >= 8) &&
274 					memcmp(e->data, HUAWEI_HEADER, 8))
275 					break;
276 
277 				/* TODO: this is using the wrong ExifMem to free e->data */
278 				exif_mem_free (data->priv->mem, e->data);
279 				e->data = NULL;
280 				e->size = 0;
281 				exif_mnote_data_set_offset (data->priv->md, *ds - 6);
282 				exif_mnote_data_save (data->priv->md, &e->data, &e->size);
283 				e->components = e->size;
284 				if (exif_format_get_size (e->format) != 1) {
285 					/* e->format is taken from input code,
286 					* but we need to make sure it is a 1 byte
287 					* entity due to the multiplication below. */
288 					e->format = EXIF_FORMAT_UNDEFINED;
289 				}
290 			}
291 		} while(0);
292 	}
293 
294 	exif_set_long  (*d + 6 + offset + 4,
295 			data->priv->order, e->components);
296 
297 	/*
298 	 * Size? If bigger than 4 bytes, the actual data is not in
299 	 * the entry but somewhere else.
300 	 */
301 	s = exif_format_get_size (e->format) * e->components;
302 	if (s > 4) {
303 		unsigned char *t;
304 		doff = *ds - 6;
305 		ts = *ds + s;
306 
307 		/*
308 		 * According to the TIFF specification,
309 		 * the offset must be an even number. If we need to introduce
310 		 * a padding byte, we set it to 0.
311 		 */
312 		if (s & 1)
313 			ts++;
314 		t = exif_mem_realloc (data->priv->mem, *d, ts);
315 		if (!t) {
316 			EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
317 		  	return;
318 		}
319 		*d = t;
320 		*ds = ts;
321 		exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
322 		if (s & 1)
323 			*(*d + *ds - 1) = '\0';
324 
325 	} else
326 		doff = offset + 8;
327 
328 	/* Write the data. Fill unneeded bytes with 0. Do not crash with
329 	 * e->data is NULL */
330 	if (e->data) {
331 		unsigned int len = s;
332 		if (e->size < s) len = e->size;
333 		memcpy (*d + 6 + doff, e->data, len);
334 	} else {
335 		memset (*d + 6 + doff, 0, s);
336 	}
337 	if (s < 4)
338 		memset (*d + 6 + doff + s, 0, (4 - s));
339 }
340 
341 static void
exif_data_save_data_entry_general(ExifData * data,ExifEntry * e,unsigned char ** d,unsigned int * ds,unsigned int offset)342 exif_data_save_data_entry_general (ExifData *data, ExifEntry *e,
343 								   unsigned char **d, unsigned int *ds,
344 								   unsigned int offset)
345 {
346 	unsigned int doff, s;
347 	unsigned int ts;
348 
349 	if (!data || !data->priv)
350 		return;
351 
352 	/*
353 	 * Each entry is 12 bytes long. The memory for the entry has
354 	 * already been allocated.
355 	 */
356 	exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 0,
357 		data->priv->order, (ExifShort)e->tag);
358 	exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 2,
359 		data->priv->order, (ExifShort)e->format);
360 
361 	if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
362 		/* If this is the maker note tag, update it. */
363 		do {
364 			if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
365 				if (is_huawei_md(data->priv->md) &&
366 					e->data && (e->size >= 8) &&
367 					memcmp(e->data, HUAWEI_HEADER, 8))
368 					break;
369 
370 				/* TODO: this is using the wrong ExifMem to free e->data */
371 				exif_mem_free(data->priv->mem, e->data);
372 				e->data = NULL;
373 				e->size = 0;
374 				exif_mnote_data_set_offset(data->priv->md, *ds - 6 + JPEG_HEADER_LEN);
375 				exif_mnote_data_save(data->priv->md, &e->data, &e->size);
376 				e->components = e->size;
377 				if (exif_format_get_size(e->format) != 1) {
378 					/* e->format is taken from input code,
379 					* but we need to make sure it is a 1 byte
380 					* entity due to the multiplication below. */
381 					e->format = EXIF_FORMAT_UNDEFINED;
382 				}
383 			}
384 		} while(0);
385 	}
386 
387 	exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 4,
388 		data->priv->order, e->components);
389 
390 	/*
391 	 * Size? If bigger than 4 bytes, the actual data is not in
392 	 * the entry but somewhere else.
393 	 */
394 	s = exif_format_get_size(e->format) * e->components;
395 	if (s > 4) {
396 		unsigned char* t;
397 		doff = *ds - 6 + JPEG_HEADER_LEN;
398 		ts = *ds + s;
399 
400 		/*
401 		 * According to the TIFF specification,
402 		 * the offset must be an even number. If we need to introduce
403 		 * a padding byte, we set it to 0.
404 		 */
405 		if (s & 1)
406 			ts++;
407 		t = exif_mem_realloc(data->priv->mem, *d, ts);
408 		if (!t) {
409 			EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", ts);
410 			return;
411 		}
412 		*d = t;
413 		*ds = ts;
414 		exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 8, data->priv->order, doff);
415 		if (s & 1)
416 			*(*d + *ds - 1) = '\0';
417 
418 	} else
419 		doff = offset + 8;
420 
421 	/* Write the data. Fill unneeded bytes with 0. Do not crash with
422 	 * e->data is NULL */
423 	if (e->data) {
424 		unsigned int len = s;
425 		if (e->size < s) len = e->size;
426 		memcpy(*d + 6 - JPEG_HEADER_LEN + doff, e->data, len);
427 	} else {
428 		memset(*d + 6 - JPEG_HEADER_LEN + doff, 0, s);
429 	}
430 	if (s < 4)
431 		memset(*d + 6 - JPEG_HEADER_LEN + doff + s, 0, (4 - s));
432 }
433 
434 static void
exif_data_load_data_thumbnail(ExifData * data,const unsigned char * d,unsigned int ds,ExifLong o,ExifLong s)435 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
436 			       unsigned int ds, ExifLong o, ExifLong s)
437 {
438 	/* Sanity checks */
439 	if (o >= ds) {
440 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", "Bogus thumbnail offset (%u).", o);
441 		return;
442 	}
443 	if (CHECKOVERFLOW(o,ds,s)) {
444 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", "Bogus thumbnail size (%u), max would be %u.", s, ds-o);
445 		return;
446 	}
447 	if (data->data)
448 		exif_mem_free (data->priv->mem, data->data);
449 	if (!(data->data = exif_data_alloc (data, s))) {
450 		EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
451 		data->size = 0;
452 		return;
453 	}
454 	data->size = s;
455 	memcpy (data->data, d + o, s);
456 }
457 
458 #undef CHECK_REC
459 #define CHECK_REC(i) 					\
460 if ((i) == ifd) {				\
461 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
462 		"ExifData", "Recursive entry in IFD "	\
463 		"'%s' detected. Skipping...",		\
464 		exif_ifd_get_name (i));			\
465 	break;						\
466 }							\
467 if (data->ifd[(i)]->count) {				\
468 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,	\
469 		"ExifData", "Attempt to load IFD "	\
470 		"'%s' multiple times detected. "	\
471 		"Skipping...",				\
472 		exif_ifd_get_name (i));			\
473 	break;						\
474 }
475 
476 /*! Calculate the recursion cost added by one level of IFD loading.
477  *
478  * The work performed is related to the cost in the exponential relation
479  *   work=1.1**cost
480  */
481 static unsigned int
level_cost(unsigned int n)482 level_cost(unsigned int n)
483 {
484     static const double log_1_1 = 0.09531017980432493;
485 
486 	/* Adding 0.1 protects against the case where n==1 */
487 	return ceil(log(n + 0.1)/log_1_1);
488 }
489 
490 static void
load_thumbnail_entry(ExifData * data,ExifTag tag,ExifIfd ifd,const unsigned char * d,unsigned int ds,unsigned int offset,unsigned i)491 load_thumbnail_entry (ExifData *data, ExifTag tag, ExifIfd ifd,
492 					  const unsigned char *d,
493 					  unsigned int ds, unsigned int offset, unsigned i)
494 {
495 	/*
496 		* If we don't know the tag, don't fail. It could be that new
497 		* versions of the standard have defined additional tags. Note that
498 		* 0 is a valid tag in the GPS IFD.
499 		*/
500 	if (!exif_tag_get_name_in_ifd (tag, ifd)) {
501 
502 		/*
503 			* Special case: Tag and format 0. That's against specification
504 			* (at least up to 2.2). But Photoshop writes it anyways.
505 			*/
506 		if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
507 			exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
508 					"Skipping empty entry at position %u in '%s'.", i,
509 					exif_ifd_get_name (ifd));
510 			return;
511 		}
512 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
513 				"Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
514 				"to <libexif-devel@lists.sourceforge.net>.", tag, i,
515 				exif_ifd_get_name (ifd));
516 		if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
517 			return;
518 	}
519 	ExifEntry *entry = exif_entry_new_mem (data->priv->mem);
520 	if (!entry) {
521 			exif_log (data->priv->log, EXIF_LOG_CODE_NO_MEMORY, "ExifData",
522 									"Could not allocate memory");
523 			return;
524 	}
525 	if (exif_data_load_data_entry (data, entry, d, ds,
526 					offset + 12 * i))
527 		exif_content_add_entry (data->ifd[ifd], entry);
528 	exif_entry_unref (entry);
529 }
530 
531 /*! Load data for an IFD.
532  *
533  * \param[in,out] data #ExifData
534  * \param[in] ifd IFD to load
535  * \param[in] d pointer to buffer containing raw IFD data
536  * \param[in] ds size of raw data in buffer at \c d
537  * \param[in] offset offset into buffer at \c d at which IFD starts
538  * \param[in] recursion_cost factor indicating how expensive this recursive
539  * call could be
540  */
541 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)542 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
543 			     const unsigned char *d,
544 			     unsigned int ds, unsigned int offset, unsigned int recursion_cost)
545 {
546 	ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
547 	ExifShort n;
548 	ExifEntry *entry;
549 	unsigned int i;
550 	ExifTag tag;
551 
552 	if (!data || !data->priv)
553 		return;
554 
555 	/* check for valid ExifIfd enum range */
556 	if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
557 	  return;
558 
559 	if (recursion_cost > 170) {
560 		/*
561 		 * recursion_cost is a logarithmic-scale indicator of how expensive this
562 		 * recursive call might end up being. It is an indicator of the depth of
563 		 * recursion as well as the potential for worst-case future recursive
564 		 * calls. Since it's difficult to tell ahead of time how often recursion
565 		 * will occur, this assumes the worst by assuming every tag could end up
566 		 * causing recursion.
567 		 * The value of 170 was chosen to limit typical EXIF structures to a
568 		 * recursive depth of about 6, but pathological ones (those with very
569 		 * many tags) to only 2.
570 		 */
571 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
572 			  "Deep/expensive recursion detected!");
573 		return;
574 	}
575 
576 	/* Read the number of entries */
577 	if (CHECKOVERFLOW(offset, ds, 2)) {
578 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
579 			  "Tag data past end of buffer (%u+2 > %u)", offset, ds);
580 		return;
581 	}
582 	n = exif_get_short (d + offset, data->priv->order);
583 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
584 	          "Loading %hu entries...", n);
585 	offset += 2;
586 
587 	/* Check if we have enough data. */
588 	if (CHECKOVERFLOW(offset, ds, 12*n)) {
589 		n = (ds - offset) / 12;
590 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
591 				  "Short data; only loading %hu entries...", n);
592 	}
593 
594 	for (i = 0; i < n; i++) {
595 
596 		tag = exif_get_short (d + offset + 12 * i, data->priv->order);
597 		switch (tag) {
598 		case EXIF_TAG_EXIF_IFD_POINTER:
599 		case EXIF_TAG_GPS_INFO_IFD_POINTER:
600 		case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
601 		case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
602 		case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
603 			o = exif_get_long (d + offset + 12 * i + 8,
604 					   data->priv->order);
605 			if (o >= ds) {
606 				exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
607 					  "Tag data past end of buffer (%u > %u)", offset+2, ds);
608 				return;
609 			}
610 			/* FIXME: IFD_POINTER tags aren't marked as being in a
611 			 * specific IFD, so exif_tag_get_name_in_ifd won't work
612 			 */
613 			exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
614 				  "Sub-IFD entry 0x%x ('%s') at %u.", tag,
615 				  exif_tag_get_name(tag), o);
616 			switch (tag) {
617 			case EXIF_TAG_EXIF_IFD_POINTER:
618 				CHECK_REC (EXIF_IFD_EXIF);
619 				exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o,
620 					recursion_cost + level_cost(n));
621 				break;
622 			case EXIF_TAG_GPS_INFO_IFD_POINTER:
623 				CHECK_REC (EXIF_IFD_GPS);
624 				exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o,
625 					recursion_cost + level_cost(n));
626 				break;
627 			case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
628 				CHECK_REC (EXIF_IFD_INTEROPERABILITY);
629 				exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o,
630 					recursion_cost + level_cost(n));
631 				break;
632 			case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
633 				thumbnail_offset = o;
634 				if (thumbnail_offset && thumbnail_length)
635 					exif_data_load_data_thumbnail (data, d,
636 								       ds, thumbnail_offset,
637 								       thumbnail_length);
638 				load_thumbnail_entry(data, tag, ifd, d, ds, offset, i);
639 				break;
640 			case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
641 				thumbnail_length = o;
642 				if (thumbnail_offset && thumbnail_length)
643 					exif_data_load_data_thumbnail (data, d,
644 								       ds, thumbnail_offset,
645 								       thumbnail_length);
646 				load_thumbnail_entry(data, tag, ifd, d, ds, offset, i);
647 				break;
648 			default:
649 				return;
650 			}
651 			break;
652 		default:
653 			/*
654 			 * If we don't know the tag, don't fail. It could be that new
655 			 * versions of the standard have defined additional tags. Note that
656 			 * 0 is a valid tag in the GPS IFD.
657 			 */
658 			if (!exif_tag_get_name_in_ifd (tag, ifd)) {
659 
660 				/*
661 				 * Special case: Tag and format 0. That's against specification
662 				 * (at least up to 2.2). But Photoshop writes it anyways.
663 				 */
664 				if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
665 					exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
666 						  "Skipping empty entry at position %u in '%s'.", i,
667 						  exif_ifd_get_name (ifd));
668 					break;
669 				}
670 				exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
671 					  "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
672 					  "to <libexif-devel@lists.sourceforge.net>.", tag, i,
673 					  exif_ifd_get_name (ifd));
674 				if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
675 					break;
676 			}
677 			entry = exif_entry_new_mem (data->priv->mem);
678 			if (!entry) {
679 				  exif_log (data->priv->log, EXIF_LOG_CODE_NO_MEMORY, "ExifData",
680                                           "Could not allocate memory");
681 				  return;
682 			}
683 			if (exif_data_load_data_entry (data, entry, d, ds,
684 						   offset + 12 * i))
685 				exif_content_add_entry (data->ifd[ifd], entry);
686 			exif_entry_unref (entry);
687 			break;
688 		}
689 	}
690 }
691 
692 static int
cmp_func(const unsigned char * p1,const unsigned char * p2,ExifByteOrder o)693 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
694 {
695 	ExifShort tag1 = exif_get_short (p1, o);
696 	ExifShort tag2 = exif_get_short (p2, o);
697 
698 	return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
699 }
700 
701 static int
cmp_func_intel(const void * elem1,const void * elem2)702 cmp_func_intel (const void *elem1, const void *elem2)
703 {
704 	return cmp_func ((const unsigned char *) elem1,
705 			 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
706 }
707 
708 static int
cmp_func_motorola(const void * elem1,const void * elem2)709 cmp_func_motorola (const void *elem1, const void *elem2)
710 {
711 	return cmp_func ((const unsigned char *) elem1,
712 			 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
713 }
714 
715 static void
exif_data_save_data_content(ExifData * data,ExifContent * ifd,unsigned char ** d,unsigned int * ds,unsigned int offset)716 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
717 			     unsigned char **d, unsigned int *ds,
718 			     unsigned int offset)
719 {
720 	unsigned int j, n_ptr = 0, n_thumb = 0;
721 	ExifIfd i;
722 	unsigned char *t;
723 	unsigned int ts;
724 
725 	if (!data || !data->priv || !ifd || !d || !ds)
726 		return;
727 
728 	for (i = 0; i < EXIF_IFD_COUNT; i++)
729 		if (ifd == data->ifd[i])
730 			break;
731 	if (i == EXIF_IFD_COUNT)
732 		return;	/* error */
733 
734 	/*
735 	 * Check if we need some extra entries for pointers or the thumbnail.
736 	 */
737 	switch (i) {
738 	case EXIF_IFD_0:
739 
740 		/*
741 		 * The pointer to IFD_EXIF is in IFD_0. The pointer to
742 		 * IFD_INTEROPERABILITY is in IFD_EXIF.
743 		 */
744 		if (data->ifd[EXIF_IFD_EXIF]->count ||
745 		    data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
746 			n_ptr++;
747 
748 		/* The pointer to IFD_GPS is in IFD_0. */
749 		if (data->ifd[EXIF_IFD_GPS]->count)
750 			n_ptr++;
751 
752 		break;
753 	case EXIF_IFD_1:
754 		if (data->size)
755 			n_thumb = 2;
756 		break;
757 	case EXIF_IFD_EXIF:
758 		if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
759 			n_ptr++;
760 	default:
761 		break;
762 	}
763 
764 	/*
765 	 * Allocate enough memory for all entries
766 	 * and the number of entries.
767 	 */
768 	ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
769 	t = exif_mem_realloc (data->priv->mem, *d, ts);
770 	if (!t) {
771 		EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
772 	  	return;
773 	}
774 	*d = t;
775 	*ds = ts;
776 
777 	/* Save the number of entries */
778 	exif_set_short (*d + 6 + offset, data->priv->order,
779 			(ExifShort) (ifd->count + n_ptr + n_thumb));
780 	offset += 2;
781 
782 	/*
783 	 * Save each entry. Make sure that no memcpys from NULL pointers are
784 	 * performed
785 	 */
786 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
787 		  "Saving %i entries (IFD '%s', offset: %i)...",
788 		  ifd->count, exif_ifd_get_name (i), offset);
789 	for (j = 0; j < ifd->count; j++) {
790 		if (ifd->entries[j]) {
791 			exif_data_save_data_entry (data, ifd->entries[j], d, ds,
792 				offset + 12 * j);
793 		}
794 	}
795 
796 	offset += 12 * ifd->count;
797 
798 	/* Now save special entries. */
799 	switch (i) {
800 	case EXIF_IFD_0:
801 
802 		/*
803 		 * The pointer to IFD_EXIF is in IFD_0.
804 		 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
805 		 * therefore, if IFD_INTEROPERABILITY is not empty, we need
806 		 * IFD_EXIF even if latter is empty.
807 		 */
808 		if (data->ifd[EXIF_IFD_EXIF]->count ||
809 		    data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
810 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
811 					EXIF_TAG_EXIF_IFD_POINTER);
812 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
813 					EXIF_FORMAT_LONG);
814 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
815 					1);
816 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
817 					*ds - 6);
818 			exif_data_save_data_content (data,
819 						     data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
820 			offset += 12;
821 		}
822 
823 		/* The pointer to IFD_GPS is in IFD_0, too. */
824 		if (data->ifd[EXIF_IFD_GPS]->count) {
825 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
826 					EXIF_TAG_GPS_INFO_IFD_POINTER);
827 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
828 					EXIF_FORMAT_LONG);
829 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
830 					1);
831 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
832 					*ds - 6);
833 			exif_data_save_data_content (data,
834 						     data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
835 			offset += 12;
836 		}
837 
838 		break;
839 	case EXIF_IFD_EXIF:
840 
841 		/*
842 		 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
843 		 * See note above.
844 		 */
845 		if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
846 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
847 					EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
848 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
849 					EXIF_FORMAT_LONG);
850 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
851 					1);
852 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
853 					*ds - 6);
854 			exif_data_save_data_content (data,
855 						     data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
856 						     *ds - 6);
857 			offset += 12;
858 		}
859 
860 		break;
861 	case EXIF_IFD_1:
862 
863 		/*
864 		 * Information about the thumbnail (if any) is saved in
865 		 * IFD_1.
866 		 */
867 		if (data->size) {
868 
869 			/* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
870 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
871 					EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
872 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
873 					EXIF_FORMAT_LONG);
874 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
875 					1);
876 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
877 					*ds - 6);
878 			ts = *ds + data->size;
879 			t = exif_mem_realloc (data->priv->mem, *d, ts);
880 			if (!t) {
881 				EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
882 						    ts);
883 			  	return;
884 			}
885 			*d = t;
886 			*ds = ts;
887 			memcpy (*d + *ds - data->size, data->data, data->size);
888 			offset += 12;
889 
890 			/* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
891 			exif_set_short (*d + 6 + offset + 0, data->priv->order,
892 					EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
893 			exif_set_short (*d + 6 + offset + 2, data->priv->order,
894 					EXIF_FORMAT_LONG);
895 			exif_set_long  (*d + 6 + offset + 4, data->priv->order,
896 					1);
897 			exif_set_long  (*d + 6 + offset + 8, data->priv->order,
898 					data->size);
899 			offset += 12;
900 		}
901 
902 		break;
903 	default:
904 		break;
905 	}
906 
907 	/* Sort the directory according to TIFF specification */
908 	qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
909 	       (ifd->count + n_ptr + n_thumb), 12,
910 	       (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
911 
912 	/* Correctly terminate the directory */
913 	if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
914 				data->size)) {
915 
916 		/*
917 		 * We are saving IFD 0. Tell where IFD 1 starts and save
918 		 * IFD 1.
919 		 */
920 		exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
921 		exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
922 					     *ds - 6);
923 	} else
924 		exif_set_long (*d + 6 + offset, data->priv->order, 0);
925 }
926 
927 static void
exif_data_save_data_content_general(ExifData * data,ExifContent * ifd,unsigned char ** d,unsigned int * ds,unsigned int offset)928 exif_data_save_data_content_general (ExifData* data, ExifContent* ifd,
929 									 unsigned char** d, unsigned int* ds,
930 									 unsigned int offset)
931 {
932 	unsigned int j, n_ptr = 0, n_thumb = 0;
933 	ExifIfd i;
934 	unsigned char* t;
935 	unsigned int ts;
936 
937 	if (!data || !data->priv || !ifd || !d || !ds)
938 		return;
939 
940 	for (i = 0; i < EXIF_IFD_COUNT; i++)
941 		if (ifd == data->ifd[i])
942 			break;
943 	if (i == EXIF_IFD_COUNT)
944 		return;	/* error */
945 
946 	/*
947 	 * Check if we need some extra entries for pointers or the thumbnail.
948 	 */
949 	switch (i) {
950 	case EXIF_IFD_0:
951 
952 		/*
953 		 * The pointer to IFD_EXIF is in IFD_0. The pointer to
954 		 * IFD_INTEROPERABILITY is in IFD_EXIF.
955 		 */
956 		if (data->ifd[EXIF_IFD_EXIF]->count ||
957 			data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
958 			n_ptr++;
959 
960 		/* The pointer to IFD_GPS is in IFD_0. */
961 		if (data->ifd[EXIF_IFD_GPS]->count)
962 			n_ptr++;
963 
964 		break;
965 	case EXIF_IFD_1:
966 		if (data->size)
967 			n_thumb = 2;
968 		break;
969 	case EXIF_IFD_EXIF:
970 		if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
971 			n_ptr++;
972 	default:
973 		break;
974 	}
975 
976 	/*
977 	 * Allocate enough memory for all entries
978 	 * and the number of entries.
979 	 */
980 	ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
981 	t = exif_mem_realloc(data->priv->mem, *d, ts);
982 	if (!t) {
983 		EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", ts);
984 		return;
985 	}
986 	*d = t;
987 	*ds = ts;
988 
989 	/* Save the number of entries */
990 	exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset, data->priv->order,
991 		(ExifShort)(ifd->count + n_ptr + n_thumb));
992 	offset += 2;
993 
994 	/*
995 	 * Save each entry. Make sure that no memcpys from NULL pointers are
996 	 * performed
997 	 */
998 	exif_log(data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
999 		"Saving %i entries (IFD '%s', offset: %i)...",
1000 		ifd->count, exif_ifd_get_name(i), offset);
1001 	printf("current ifd->count %u\n", ifd->count);
1002 	for (j = 0; j < ifd->count; j++) {
1003 		if (ifd->entries[j]) {
1004 			exif_data_save_data_entry_general(data, ifd->entries[j], d, ds,
1005 				offset + 12 * j);
1006 		}
1007 	}
1008 
1009 	offset += 12 * ifd->count;
1010 
1011 	/* Now save special entries. */
1012 	switch (i) {
1013 	case EXIF_IFD_0:
1014 
1015 		/*
1016 		 * The pointer to IFD_EXIF is in IFD_0.
1017 		 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
1018 		 * therefore, if IFD_INTEROPERABILITY is not empty, we need
1019 		 * IFD_EXIF even if latter is empty.
1020 		 */
1021 		if (data->ifd[EXIF_IFD_EXIF]->count ||
1022 			data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
1023 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 0, data->priv->order,
1024 				EXIF_TAG_EXIF_IFD_POINTER);
1025 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 2, data->priv->order,
1026 				EXIF_FORMAT_LONG);
1027 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 4, data->priv->order,
1028 				1);
1029 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 8, data->priv->order,
1030 				*ds - 6 + JPEG_HEADER_LEN);
1031 			exif_data_save_data_content_general(data,
1032 				data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6 + JPEG_HEADER_LEN);
1033 			offset += 12;
1034 		}
1035 
1036 		/* The pointer to IFD_GPS is in IFD_0, too. */
1037 		if (data->ifd[EXIF_IFD_GPS]->count) {
1038 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 0, data->priv->order,
1039 				EXIF_TAG_GPS_INFO_IFD_POINTER);
1040 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 2, data->priv->order,
1041 				EXIF_FORMAT_LONG);
1042 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 4, data->priv->order,
1043 				1);
1044 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 8, data->priv->order,
1045 				*ds - 6 + JPEG_HEADER_LEN);
1046 			exif_data_save_data_content_general(data,
1047 				data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6 + JPEG_HEADER_LEN);
1048 			offset += 12;
1049 		}
1050 
1051 		break;
1052 	case EXIF_IFD_EXIF:
1053 
1054 		/*
1055 		 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
1056 		 * See note above.
1057 		 */
1058 		if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
1059 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 0, data->priv->order,
1060 				EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
1061 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 2, data->priv->order,
1062 				EXIF_FORMAT_LONG);
1063 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 4, data->priv->order,
1064 				1);
1065 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 8, data->priv->order,
1066 				*ds - 6 + JPEG_HEADER_LEN);
1067 			exif_data_save_data_content_general(data,
1068 				data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
1069 				*ds - 6 + JPEG_HEADER_LEN);
1070 			offset += 12;
1071 		}
1072 
1073 		break;
1074 	case EXIF_IFD_1:
1075 
1076 		/*
1077 		 * Information about the thumbnail (if any) is saved in
1078 		 * IFD_1.
1079 		 */
1080 		if (data->size) {
1081 
1082 			/* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
1083 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 0, data->priv->order,
1084 				EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
1085 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 2, data->priv->order,
1086 				EXIF_FORMAT_LONG);
1087 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 4, data->priv->order,
1088 				1);
1089 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 8, data->priv->order,
1090 				*ds - 6 + JPEG_HEADER_LEN);
1091 			ts = *ds + data->size;
1092 			t = exif_mem_realloc(data->priv->mem, *d, ts);
1093 			if (!t) {
1094 				EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData",
1095 					ts);
1096 				return;
1097 			}
1098 			*d = t;
1099 			*ds = ts;
1100 			memcpy(*d + *ds - data->size, data->data, data->size);
1101 			offset += 12;
1102 
1103 			/* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
1104 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 0, data->priv->order,
1105 				EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
1106 			exif_set_short(*d + 6 - JPEG_HEADER_LEN + offset + 2, data->priv->order,
1107 				EXIF_FORMAT_LONG);
1108 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 4, data->priv->order,
1109 				1);
1110 			exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset + 8, data->priv->order,
1111 				data->size);
1112 			offset += 12;
1113 		}
1114 
1115 		break;
1116 	default:
1117 		break;
1118 	}
1119 
1120 	/* Sort the directory according to TIFF specification */
1121 	qsort(*d + 6 - JPEG_HEADER_LEN + offset - (ifd->count + n_ptr + n_thumb) * 12,
1122 		(ifd->count + n_ptr + n_thumb), 12,
1123 		(data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
1124 
1125 	/* Correctly terminate the directory */
1126 	if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
1127 		data->size)) {
1128 
1129 		/*
1130 		 * We are saving IFD 0. Tell where IFD 1 starts and save
1131 		 * IFD 1.
1132 		 */
1133 		exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset, data->priv->order, *ds - 6 + JPEG_HEADER_LEN);
1134 		exif_data_save_data_content_general(data, data->ifd[EXIF_IFD_1], d, ds,
1135 			*ds - 6 + JPEG_HEADER_LEN);
1136 	}
1137 	else
1138 		exif_set_long(*d + 6 - JPEG_HEADER_LEN + offset, data->priv->order, 0);
1139 }
1140 
1141 typedef enum {
1142 	EXIF_DATA_TYPE_MAKER_NOTE_NONE		= 0,
1143 	EXIF_DATA_TYPE_MAKER_NOTE_CANON		= 1,
1144 	EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS	= 2,
1145 	EXIF_DATA_TYPE_MAKER_NOTE_PENTAX	= 3,
1146 	EXIF_DATA_TYPE_MAKER_NOTE_NIKON		= 4,
1147 	EXIF_DATA_TYPE_MAKER_NOTE_CASIO		= 5,
1148 	EXIF_DATA_TYPE_MAKER_NOTE_FUJI 		= 6
1149 } ExifDataTypeMakerNote;
1150 
1151 /*! If MakerNote is recognized, load it.
1152  *
1153  * \param[in,out] data #ExifData
1154  * \param[in] d pointer to raw EXIF data
1155  * \param[in] ds length of data at d
1156  */
1157 static void
interpret_maker_note(ExifData * data,const unsigned char * d,unsigned int ds)1158 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
1159 {
1160 	int mnoteid;
1161 	ExifEntry* e = exif_content_get_huawei_makenote_entry(data->ifd[EXIF_IFD_EXIF]);
1162 	if (!e) {
1163 		e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
1164 	}
1165 
1166 	if (!e)
1167 		return;
1168 
1169 	if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
1170 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
1171 			"ExifData", "Olympus MakerNote variant type %d", mnoteid);
1172 		data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
1173 
1174 	} else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
1175 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
1176 			"ExifData", "Canon MakerNote variant type %d", mnoteid);
1177 		data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
1178 
1179 	} else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
1180 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
1181 			"ExifData", "Fuji MakerNote variant type %d", mnoteid);
1182 		data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
1183 
1184 	/* NOTE: Must do Pentax detection last because some of the
1185 	 * heuristics are pretty general. */
1186 	} else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
1187 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
1188 			"ExifData", "Pentax MakerNote variant type %d", mnoteid);
1189 		data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
1190 	} else if ((mnoteid = exif_mnote_data_apple_identify (data, e)) != 0) {
1191 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
1192 			"ExifData", "Apple MakerNote variant type %d", mnoteid);
1193 		data->priv->md = exif_mnote_data_apple_new (data->priv->mem);
1194 	} else if ((mnoteid = exif_mnote_data_huawei_identify (data, e)) != 0) {
1195 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
1196 			"ExifData", "Huawei MakerNote variant type %d", mnoteid);
1197 		data->priv->md = exif_mnote_data_huawei_new (data->priv->mem);
1198 	}
1199 
1200 	/*
1201 	 * If we are able to interpret the maker note, do so.
1202 	 */
1203 	if (data->priv->md) {
1204 		exif_mnote_data_log (data->priv->md, data->priv->log);
1205 		exif_mnote_data_set_byte_order (data->priv->md,
1206 						data->priv->order);
1207 		exif_mnote_data_set_offset (data->priv->md,
1208 					    data->priv->offset_mnote);
1209 		exif_mnote_data_load (data->priv->md, d, ds);
1210 	}
1211 }
1212 
1213 #define LOG_TOO_SMALL \
1214 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
1215 		_("Size of data too small to allow for EXIF data."));
1216 
1217 void
exif_data_load_data(ExifData * data,const unsigned char * d_orig,unsigned int ds)1218 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
1219 		     unsigned int ds)
1220 {
1221 	unsigned int l;
1222 	ExifLong offset;
1223 	ExifShort n;
1224 	const unsigned char *d = d_orig;
1225 	unsigned int len, fullds;
1226 
1227 	if (!data || !data->priv || !d || !ds)
1228 		return;
1229 
1230 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1231 		  "Parsing %i byte(s) EXIF data...\n", ds);
1232 
1233 	/*
1234 	 * It can be that the data starts with the EXIF header. If it does
1235 	 * not, search the EXIF marker.
1236 	 */
1237 	if (ds < 6) {
1238 		LOG_TOO_SMALL;
1239 		return;
1240 	}
1241 	if (!memcmp (d, ExifHeader, 6)) {
1242 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1243 			  "Found EXIF header at start.");
1244 	} else {
1245 		while (ds >= 3) {
1246 			while (ds && (d[0] == 0xff)) {
1247 				d++;
1248 				ds--;
1249 			}
1250 
1251 			/* JPEG_MARKER_SOI */
1252 			if (ds && d[0] == JPEG_MARKER_SOI) {
1253 				d++;
1254 				ds--;
1255 				continue;
1256 			}
1257 
1258 			/* JPEG_MARKER_APP1 */
1259 			if (ds && d[0] == JPEG_MARKER_APP1) {
1260 				/*
1261 				 * Verify the exif header
1262 				 * (offset 3, length 6).
1263 				 * FF E1 NN NN EXIFHEADER
1264 				 *    ^^ d points here currently
1265 				 */
1266 				if ((ds >= 9) && !memcmp (d+3, ExifHeader, 6))
1267 					break;
1268 				/* fallthrough */
1269 			}
1270 			/* Skip irrelevant APP markers. The branch for APP1 must come before this,
1271 			   otherwise this code block will cause APP1 to be skipped. This code path
1272 			   is only relevant for files that are nonconformant to the EXIF
1273 			   specification. For conformant files, the APP1 code path above will be
1274 			   taken. */
1275 			if (ds >= 3 && d[0] >= 0xe0 && d[0] <= 0xef) {  /* JPEG_MARKER_APPn */
1276 				d++;
1277 				ds--;
1278 				l = (((unsigned int)d[0]) << 8) | d[1];
1279 				if (l > ds)
1280 					return;
1281 				d += l;
1282 				ds -= l;
1283 				continue;
1284 			}
1285 
1286 			/* Unknown marker or data. Give up. */
1287 			exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1288 				  "ExifData", _("EXIF marker not found."));
1289 			return;
1290 		}
1291 		if (ds < 3) {
1292 			LOG_TOO_SMALL;
1293 			return;
1294 		}
1295 		d++;
1296 		ds--;
1297 		len = (((unsigned int)d[0]) << 8) | d[1];
1298 		if (len > ds) {
1299 			exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1300 				  "ExifData", _("Read length %d is longer than data length %d."), len, ds);
1301 			return;
1302 		}
1303 		if (len < 2) {
1304 			exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1305 				  "ExifData", _("APP Tag too short."));
1306 			return;
1307 		}
1308 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1309 			  "We have to deal with %i byte(s) of EXIF data.",
1310 			  len);
1311 		d += 2;
1312 		ds = len - 2;	/* we do not want the full rest size, but only the size of the tag */
1313 	}
1314 
1315 	/*
1316 	 * Verify the exif header
1317 	 * (offset 2, length 6).
1318 	 */
1319 	if (ds < 6) {
1320 		LOG_TOO_SMALL;
1321 		return;
1322 	}
1323 	if (memcmp (d, ExifHeader, 6)) {
1324 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1325 			  "ExifData", _("EXIF header not found."));
1326 		return;
1327 	}
1328 
1329 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1330 		  "Found EXIF header.");
1331 
1332 	/* Sanity check the data length */
1333 	if (ds < 14)
1334 		return;
1335 
1336 	/* The JPEG APP1 section can be no longer than 64 KiB (including a
1337 	   16-bit length), so cap the data length to protect against overflow
1338 	   in future offset calculations */
1339 	fullds = ds;
1340 	if (ds > 0xfffe)
1341 		ds = 0xfffe;
1342 
1343 	/* Byte order (offset 6, length 2) */
1344 	if (!memcmp (d + 6, "II", 2))
1345 		data->priv->order = EXIF_BYTE_ORDER_INTEL;
1346 	else if (!memcmp (d + 6, "MM", 2))
1347 		data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
1348 	else {
1349 		exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1350 			  "ExifData", _("Unknown encoding."));
1351 		return;
1352 	}
1353 
1354 	/* Fixed value */
1355 	if (exif_get_short (d + 8, data->priv->order) != 0x002a)
1356 		return;
1357 
1358 	/* IFD 0 offset */
1359 	offset = exif_get_long (d + 10, data->priv->order);
1360 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1361 		  "IFD 0 at %i.", (int) offset);
1362 
1363 	/* ds is restricted to 16 bit above, so offset is restricted too, and offset+8 should not overflow. */
1364 	if (offset > ds || offset + 6 + 2 > ds)
1365 		return;
1366 
1367 	/* Parse the actual exif data (usually offset 14 from start) */
1368 	exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
1369 
1370 	/* IFD 1 offset */
1371 	n = exif_get_short (d + 6 + offset, data->priv->order);
1372 	/* offset < 2<<16, n is 16 bit at most, so this op will not overflow */
1373 	if (offset + 6 + 2 + 12 * n + 4 > ds)
1374 		return;
1375 
1376 	offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
1377 	if (offset) {
1378 		exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1379 			  "IFD 1 at %i.", (int) offset);
1380 
1381 		/* Sanity check. ds is ensured to be above 6 above, offset is 16bit */
1382 		if (offset > ds - 6) {
1383 			exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1384 				  "ExifData", "Bogus offset of IFD1.");
1385 		} else {
1386 			exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
1387 		}
1388 	}
1389 
1390 	/*
1391 	 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
1392 	 * cameras use pointers in the maker note tag that point to the
1393 	 * space between IFDs. Here is the only place where we have access
1394 	 * to that data.
1395 	 */
1396 	interpret_maker_note(data, d, fullds);
1397 
1398 	/* Fixup tags if requested */
1399 	if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
1400 		exif_data_fix (data);
1401 }
1402 
1403 void
exif_data_load_data_general(ExifData * data,const unsigned char * d_orig,unsigned int ds)1404 exif_data_load_data_general (ExifData* data, const unsigned char* d_orig,
1405 							 unsigned int ds)
1406 {
1407 	ExifLong offset;
1408 	ExifShort n;
1409 	const unsigned char* d = d_orig;
1410 	unsigned int fullds;
1411 
1412 	if (!data || !data->priv || !d || !ds)
1413 		return;
1414 
1415 	exif_log(data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1416 		"Parsing %i byte(s) EXIF data...\n", ds);
1417 
1418 	/* Sanity check the data length */
1419 	if (ds < 14)
1420 		return;
1421 
1422 	/* The JPEG APP1 section can be no longer than 64 KiB (including a
1423 	   16-bit length), so cap the data length to protect against overflow
1424 	   in future offset calculations */
1425 	fullds = ds;
1426 	if (ds > 0xfffe)
1427 		ds = 0xfffe;
1428 
1429 	/* Byte order (offset 6, length 2) */
1430 	if (!memcmp(d + 6 - 6, "II", 2))
1431 		data->priv->order = EXIF_BYTE_ORDER_INTEL;
1432 	else if (!memcmp(d + 6 - 6, "MM", 2))
1433 		data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
1434 	else {
1435 		exif_log(data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1436 			"ExifData", _("Unknown encoding."));
1437 		return;
1438 	}
1439 
1440 	/* Fixed value */
1441 	if (exif_get_short(d + 8 - 6, data->priv->order) != 0x002a)
1442 		return;
1443 
1444 	/* IFD 0 offset */
1445 	offset = exif_get_long(d + 10 - 6, data->priv->order);
1446 	exif_log(data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1447 		"IFD 0 at %i.", (int)offset);
1448 
1449 	/* ds is restricted to 16 bit above, so offset is restricted too, and offset+8 should not overflow. */
1450 	// compare offset and buffer size
1451 	if (offset > ds || offset + 6 - 6 + 2 > ds)
1452 		return;
1453 
1454 	/* Parse the actual exif data (usually offset 14 from start) */
1455 	exif_data_load_data_content(data, EXIF_IFD_0, d + 6 - 6, ds - 6 + 6, offset, 0);
1456 
1457 	/* IFD 1 offset */
1458 	n = exif_get_short(d + 6 - 6 + offset, data->priv->order);
1459 	/* offset < 2<<16, n is 16 bit at most, so this op will not overflow */
1460 	if (offset + 6 - 6 + 2 + 12 * n + 4 > ds)
1461 		return;
1462 
1463 	offset = exif_get_long(d + 6 - 6 + offset + 2 + 12 * n, data->priv->order);
1464 	if (offset) {
1465 		exif_log(data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1466 			"IFD 1 at %i.", (int)offset);
1467 
1468 		/* Sanity check. ds is ensured to be above 6 above, offset is 16bit */
1469 		if (offset > ds - 6 + 6) {
1470 			exif_log(data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
1471 				"ExifData", "Bogus offset of IFD1.");
1472 		} else {
1473 			exif_data_load_data_content(data, EXIF_IFD_1, d + 6 - 6, ds - 6 + 6, offset, 0);
1474 		}
1475 	}
1476 
1477 	/*
1478 	 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
1479 	 * cameras use pointers in the maker note tag that point to the
1480 	 * space between IFDs. Here is the only place where we have access
1481 	 * to that data.
1482 	 */
1483 	interpret_maker_note(data, d, fullds);
1484 
1485 	/* Fixup tags if requested */
1486 	if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
1487 		exif_data_fix(data);
1488 }
1489 
1490 void
exif_data_save_data(ExifData * data,unsigned char ** d,unsigned int * ds)1491 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
1492 {
1493 	if (ds)
1494 		*ds = 0;	/* This means something went wrong */
1495 
1496 	if (!data || !d || !ds)
1497 		return;
1498 
1499 	/* Header */
1500 	*ds = 14;
1501 	*d = exif_data_alloc (data, *ds);
1502 	if (!*d)  {
1503 		*ds = 0;
1504 		return;
1505 	}
1506 	memcpy (*d, ExifHeader, 6);
1507 
1508 	/* Order (offset 6) */
1509 	if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
1510 		memcpy (*d + 6, "II", 2);
1511 	} else {
1512 		memcpy (*d + 6, "MM", 2);
1513 	}
1514 
1515 	/* Fixed value (2 bytes, offset 8) */
1516 	exif_set_short (*d + 8, data->priv->order, 0x002a);
1517 
1518 	/*
1519 	 * IFD 0 offset (4 bytes, offset 10).
1520 	 * We will start 8 bytes after the
1521 	 * EXIF header (2 bytes for order, another 2 for the test, and
1522 	 * 4 bytes for the IFD 0 offset make 8 bytes together).
1523 	 */
1524 	exif_set_long (*d + 10, data->priv->order, 8);
1525 
1526 	/* Now save IFD 0. IFD 1 will be saved automatically. */
1527 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1528 		  "Saving IFDs...");
1529 	exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
1530 				     *ds - 6);
1531 	exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1532 		  "Saved %i byte(s) EXIF data.", *ds);
1533 }
1534 
1535 void
exif_data_save_data_general(ExifData * data,unsigned char ** d,unsigned int * ds)1536 exif_data_save_data_general(ExifData* data, unsigned char** d, unsigned int* ds)
1537 {
1538 	if (ds)
1539 		*ds = 0;	/* This means something went wrong */
1540 
1541 	if (!data || !d || !ds)
1542 		return;
1543 
1544 	/* Header 4D 4D 00 2A 00 00 00 08*/
1545 	*ds = 8;
1546 	*d = exif_data_alloc(data, *ds);
1547 	if (!*d) {
1548 		*ds = 0;
1549 		return;
1550 	}
1551 
1552 	/* Order (offset 6) */
1553 	if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
1554 		memcpy(*d + 6 - 6, "II", 2);
1555 	}
1556 	else {
1557 		memcpy(*d + 6 - 6, "MM", 2);
1558 	}
1559 
1560 	/* Fixed value (2 bytes, offset 8) */
1561 	exif_set_short(*d + 8 - 6, data->priv->order, 0x002a);
1562 
1563 	/*
1564 	 * IFD 0 offset (4 bytes, offset 10).
1565 	 * We will start 8 bytes after the
1566 	 * EXIF header (2 bytes for order, another 2 for the test, and
1567 	 * 4 bytes for the IFD 0 offset make 8 bytes together).
1568 	 */
1569 	exif_set_long(*d + 10 - 6, data->priv->order, 8);
1570 	/* Now save IFD 0. IFD 1 will be saved automatically. */
1571 	exif_log(data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1572 		"Saving IFDs...");
1573 	exif_data_save_data_content_general(data, data->ifd[EXIF_IFD_0], d, ds,
1574 		*ds);
1575 	exif_log(data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1576 		"Saved %i byte(s) EXIF data.", *ds);
1577 }
1578 
1579 ExifData *
exif_data_new_from_file(const char * path)1580 exif_data_new_from_file (const char *path)
1581 {
1582 	ExifData *edata;
1583 	ExifLoader *loader;
1584 
1585 	loader = exif_loader_new ();
1586 	exif_loader_write_file (loader, path);
1587 	edata = exif_loader_get_data (loader);
1588 	exif_loader_unref (loader);
1589 
1590 	return (edata);
1591 }
1592 
1593 void
exif_data_ref(ExifData * data)1594 exif_data_ref (ExifData *data)
1595 {
1596 	if (!data)
1597 		return;
1598 
1599 	data->priv->ref_count++;
1600 }
1601 
1602 void
exif_data_unref(ExifData * data)1603 exif_data_unref (ExifData *data)
1604 {
1605 	if (!data)
1606 		return;
1607 
1608 	data->priv->ref_count--;
1609 	if (!data->priv->ref_count)
1610 		exif_data_free (data);
1611 }
1612 
1613 void
exif_data_free(ExifData * data)1614 exif_data_free (ExifData *data)
1615 {
1616 	unsigned int i;
1617 	ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1618 
1619 	if (!data)
1620 		return;
1621 
1622 	for (i = 0; i < EXIF_IFD_COUNT; i++) {
1623 		if (data->ifd[i]) {
1624 			exif_content_unref (data->ifd[i]);
1625 			data->ifd[i] = NULL;
1626 		}
1627 	}
1628 
1629 	if (data->data) {
1630 		exif_mem_free (mem, data->data);
1631 		data->data = NULL;
1632 	}
1633 
1634 	if (data->priv) {
1635 		if (data->priv->log) {
1636 			exif_log_unref (data->priv->log);
1637 			data->priv->log = NULL;
1638 		}
1639 		if (data->priv->md) {
1640 			exif_mnote_data_unref (data->priv->md);
1641 			data->priv->md = NULL;
1642 		}
1643 		exif_mem_free (mem, data->priv);
1644 		exif_mem_free (mem, data);
1645 	}
1646 
1647 	exif_mem_unref (mem);
1648 }
1649 
1650 void
exif_data_dump(ExifData * data)1651 exif_data_dump (ExifData *data)
1652 {
1653 	unsigned int i;
1654 
1655 	if (!data)
1656 		return;
1657 
1658 	for (i = 0; i < EXIF_IFD_COUNT; i++) {
1659 		if (data->ifd[i] && data->ifd[i]->count) {
1660 			printf ("Dumping IFD '%s'...\n",
1661 				exif_ifd_get_name (i));
1662 			exif_content_dump (data->ifd[i], 0);
1663 		}
1664 	}
1665 
1666 	if (data->data) {
1667 		printf ("%i byte(s) thumbnail data available: ", data->size);
1668 		if (data->size >= 4) {
1669 			printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1670 				data->data[0], data->data[1],
1671 				data->data[data->size - 2],
1672 				data->data[data->size - 1]);
1673 		}
1674 	}
1675 }
1676 
1677 ExifByteOrder
exif_data_get_byte_order(ExifData * data)1678 exif_data_get_byte_order (ExifData *data)
1679 {
1680 	if (!data)
1681 		return (0);
1682 
1683 	return (data->priv->order);
1684 }
1685 
1686 void
exif_data_foreach_content(ExifData * data,ExifDataForeachContentFunc func,void * user_data)1687 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1688 			   void *user_data)
1689 {
1690 	unsigned int i;
1691 
1692 	if (!data || !func)
1693 		return;
1694 
1695 	for (i = 0; i < EXIF_IFD_COUNT; i++)
1696 		func (data->ifd[i], user_data);
1697 }
1698 
1699 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1700 struct _ByteOrderChangeData {
1701 	ExifByteOrder old, new;
1702 };
1703 
1704 static void
entry_set_byte_order(ExifEntry * e,void * data)1705 entry_set_byte_order (ExifEntry *e, void *data)
1706 {
1707 	ByteOrderChangeData *d = data;
1708 
1709 	if (!e)
1710 		return;
1711 
1712 	exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1713 }
1714 
1715 static void
content_set_byte_order(ExifContent * content,void * data)1716 content_set_byte_order (ExifContent *content, void *data)
1717 {
1718 	exif_content_foreach_entry (content, entry_set_byte_order, data);
1719 }
1720 
1721 void
exif_data_set_byte_order(ExifData * data,ExifByteOrder order)1722 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1723 {
1724 	ByteOrderChangeData d;
1725 
1726 	if (!data || (order == data->priv->order))
1727 		return;
1728 
1729 	d.old = data->priv->order;
1730 	d.new = order;
1731 	exif_data_foreach_content (data, content_set_byte_order, &d);
1732 	data->priv->order = order;
1733 	if (data->priv->md)
1734 		exif_mnote_data_set_byte_order (data->priv->md, order);
1735 }
1736 
1737 void
exif_data_log(ExifData * data,ExifLog * log)1738 exif_data_log (ExifData *data, ExifLog *log)
1739 {
1740 	unsigned int i;
1741 
1742 	if (!data || !data->priv)
1743 		return;
1744 	exif_log_unref (data->priv->log);
1745 	data->priv->log = log;
1746 	exif_log_ref (log);
1747 
1748 	for (i = 0; i < EXIF_IFD_COUNT; i++)
1749 		exif_content_log (data->ifd[i], log);
1750 }
1751 
1752 /* Used internally within libexif */
1753 ExifLog *exif_data_get_log (ExifData *);
1754 ExifLog *
exif_data_get_log(ExifData * data)1755 exif_data_get_log (ExifData *data)
1756 {
1757 	if (!data || !data->priv)
1758 		return NULL;
1759 	return data->priv->log;
1760 }
1761 
1762 static const struct {
1763 	ExifDataOption option;
1764 	const char *name;
1765 	const char *description;
1766 } exif_data_option[] = {
1767 	{EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1768 	 N_("Ignore unknown tags when loading EXIF data.")},
1769 	{EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1770 	 N_("Add, correct and remove entries to get EXIF data that follows "
1771 	    "the specification.")},
1772 	{EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1773 	 N_("When loading and resaving Exif data, save the maker note unmodified."
1774 	    " Be aware that the maker note can get corrupted.")},
1775 	{0, NULL, NULL}
1776 };
1777 
1778 const char *
exif_data_option_get_name(ExifDataOption o)1779 exif_data_option_get_name (ExifDataOption o)
1780 {
1781 	unsigned int i;
1782 
1783 	for (i = 0; exif_data_option[i].name; i++)
1784 		if (exif_data_option[i].option == o)
1785 			break;
1786 	return _(exif_data_option[i].name);
1787 }
1788 
1789 const char *
exif_data_option_get_description(ExifDataOption o)1790 exif_data_option_get_description (ExifDataOption o)
1791 {
1792 	unsigned int i;
1793 
1794 	for (i = 0; exif_data_option[i].description; i++)
1795 		if (exif_data_option[i].option == o)
1796 			break;
1797 	return _(exif_data_option[i].description);
1798 }
1799 
1800 void
exif_data_set_option(ExifData * d,ExifDataOption o)1801 exif_data_set_option (ExifData *d, ExifDataOption o)
1802 {
1803 	if (!d)
1804 		return;
1805 
1806 	d->priv->options |= o;
1807 }
1808 
1809 void
exif_data_unset_option(ExifData * d,ExifDataOption o)1810 exif_data_unset_option (ExifData *d, ExifDataOption o)
1811 {
1812 	if (!d)
1813 		return;
1814 
1815 	d->priv->options &= ~o;
1816 }
1817 
1818 static void
fix_func(ExifContent * c,void * UNUSED (data))1819 fix_func (ExifContent *c, void *UNUSED(data))
1820 {
1821 	switch (exif_content_get_ifd (c)) {
1822 	case EXIF_IFD_1:
1823 		if (c->parent->data)
1824 			exif_content_fix (c);
1825 		else if (c->count) {
1826 			exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1827 				  "No thumbnail but entries on thumbnail. These entries have been "
1828 				  "removed.");
1829 			while (c->count) {
1830 				unsigned int cnt = c->count;
1831 				exif_content_remove_entry (c, c->entries[c->count - 1]);
1832 				if (cnt == c->count) {
1833 					/* safety net */
1834 					exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1835 					"failed to remove last entry from entries.");
1836 					c->count--;
1837 				}
1838 			}
1839 		}
1840 		break;
1841 	default:
1842 		exif_content_fix (c);
1843 	}
1844 }
1845 
1846 void
exif_data_fix(ExifData * d)1847 exif_data_fix (ExifData *d)
1848 {
1849 	exif_data_foreach_content (d, fix_func, NULL);
1850 }
1851 
1852 void
exif_data_set_data_type(ExifData * d,ExifDataType dt)1853 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1854 {
1855 	if (!d || !d->priv)
1856 		return;
1857 
1858 	d->priv->data_type = dt;
1859 }
1860 
1861 ExifDataType
exif_data_get_data_type(ExifData * d)1862 exif_data_get_data_type (ExifData *d)
1863 {
1864 	return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;
1865 }
1866