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