3 * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
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.
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.
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.
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>
33 #include <libexif/canon/exif-mnote-data-canon.h>
34 #include <libexif/fuji/exif-mnote-data-fuji.h>
35 #include <libexif/olympus/exif-mnote-data-olympus.h>
36 #include <libexif/pentax/exif-mnote-data-pentax.h>
43 #undef JPEG_MARKER_SOI
44 #define JPEG_MARKER_SOI 0xd8
45 #undef JPEG_MARKER_APP0
46 #define JPEG_MARKER_APP0 0xe0
47 #undef JPEG_MARKER_APP1
48 #define JPEG_MARKER_APP1 0xe1
50 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
52 struct _ExifDataPrivate
61 unsigned int ref_count;
63 /* Temporarily used while loading data */
64 unsigned int offset_mnote;
66 ExifDataOption options;
67 ExifDataType data_type;
71 exif_data_alloc (ExifData *data, unsigned int i)
78 d = exif_mem_alloc (data->priv->mem, i);
82 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
87 exif_data_get_mnote_data (ExifData *d)
89 return (d && d->priv) ? d->priv->md : NULL;
95 ExifMem *mem = exif_mem_new_default ();
96 ExifData *d = exif_data_new_mem (mem);
104 exif_data_new_mem (ExifMem *mem)
112 data = exif_mem_alloc (mem, sizeof (ExifData));
115 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
117 exif_mem_free (mem, data);
120 data->priv->ref_count = 1;
122 data->priv->mem = mem;
125 for (i = 0; i < EXIF_IFD_COUNT; i++) {
126 data->ifd[i] = exif_content_new_mem (data->priv->mem);
128 exif_data_free (data);
131 data->ifd[i]->parent = data;
134 /* Default options */
135 #ifndef NO_VERBOSE_TAG_STRINGS
137 * When the tag list is compiled away, setting this option prevents
138 * any tags from being loaded
140 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
142 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
144 /* Default data type: none */
145 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
151 exif_data_new_from_data (const unsigned char *data, unsigned int size)
155 edata = exif_data_new ();
156 exif_data_load_data (edata, data, size);
161 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
162 const unsigned char *d,
163 unsigned int size, unsigned int offset)
165 unsigned int s, doff;
167 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
168 entry->format = exif_get_short (d + offset + 2, data->priv->order);
169 entry->components = exif_get_long (d + offset + 4, data->priv->order);
171 /* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent
172 * has not been set yet
174 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
175 "Loading entry 0x%x ('%s')...", entry->tag,
176 exif_tag_get_name (entry->tag));
178 /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
179 * -> { 0x000000000 .. 0x7fffffff8 } */
180 s = exif_format_get_size(entry->format) * entry->components;
181 if ((s < entry->components) || (s == 0)){
186 * Size? If bigger than 4 bytes, the actual data is not
187 * in the entry but somewhere else (offset).
190 doff = exif_get_long (d + offset + 8, data->priv->order);
196 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
197 "Tag starts past end of buffer (%u > %u)", doff, size);
201 if (s > size - doff) {
202 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
203 "Tag data goes past end of buffer (%u > %u)", doff+s, size);
207 entry->data = exif_data_alloc (data, s);
210 memcpy (entry->data, d + doff, s);
212 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
216 /* If this is the MakerNote, remember the offset */
217 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
219 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
220 "MakerNote found with empty data");
221 } else if (entry->size > 6) {
222 exif_log (data->priv->log,
223 EXIF_LOG_CODE_DEBUG, "ExifData",
224 "MakerNote found (%02x %02x %02x %02x "
225 "%02x %02x %02x...).",
226 entry->data[0], entry->data[1], entry->data[2],
227 entry->data[3], entry->data[4], entry->data[5],
230 data->priv->offset_mnote = doff;
236 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
237 unsigned char **d, unsigned int *ds,
240 unsigned int doff, s;
243 if (!data || !data->priv)
247 * Each entry is 12 bytes long. The memory for the entry has
248 * already been allocated.
250 exif_set_short (*d + 6 + offset + 0,
251 data->priv->order, (ExifShort) e->tag);
252 exif_set_short (*d + 6 + offset + 2,
253 data->priv->order, (ExifShort) e->format);
255 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
256 /* If this is the maker note tag, update it. */
257 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
258 /* TODO: this is using the wrong ExifMem to free e->data */
259 exif_mem_free (data->priv->mem, e->data);
262 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
263 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
264 e->components = e->size;
265 if (exif_format_get_size (e->format) != 1) {
266 /* e->format is taken from input code,
267 * but we need to make sure it is a 1 byte
268 * entity due to the multiplication below. */
269 e->format = EXIF_FORMAT_UNDEFINED;
274 exif_set_long (*d + 6 + offset + 4,
275 data->priv->order, e->components);
278 * Size? If bigger than 4 bytes, the actual data is not in
279 * the entry but somewhere else.
281 s = exif_format_get_size (e->format) * e->components;
288 * According to the TIFF specification,
289 * the offset must be an even number. If we need to introduce
290 * a padding byte, we set it to 0.
294 t = exif_mem_realloc (data->priv->mem, *d, ts);
296 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
301 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
303 *(*d + *ds - 1) = '\0';
308 /* Write the data. Fill unneeded bytes with 0. Do not crash with
311 unsigned int len = s;
312 if (e->size < s) len = e->size;
313 memcpy (*d + 6 + doff, e->data, len);
315 memset (*d + 6 + doff, 0, s);
318 memset (*d + 6 + doff + s, 0, (4 - s));
322 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
323 unsigned int ds, ExifLong o, ExifLong s)
327 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", "Bogus thumbnail offset (%u).", o);
331 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", "Bogus thumbnail size (%u), max would be %u.", s, ds-o);
335 exif_mem_free (data->priv->mem, data->data);
336 if (!(data->data = exif_data_alloc (data, s))) {
337 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
342 memcpy (data->data, d + o, s);
346 #define CHECK_REC(i) \
348 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
349 "ExifData", "Recursive entry in IFD " \
350 "'%s' detected. Skipping...", \
351 exif_ifd_get_name (i)); \
354 if (data->ifd[(i)]->count) { \
355 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
356 "ExifData", "Attempt to load IFD " \
357 "'%s' multiple times detected. " \
359 exif_ifd_get_name (i)); \
363 /*! Calculate the recursion cost added by one level of IFD loading.
365 * The work performed is related to the cost in the exponential relation
369 level_cost(unsigned int n)
371 static const double log_1_1 = 0.09531017980432493;
373 /* Adding 0.1 protects against the case where n==1 */
374 return ceil(log(n + 0.1)/log_1_1);
377 /*! Load data for an IFD.
379 * \param[in,out] data #ExifData
380 * \param[in] ifd IFD to load
381 * \param[in] d pointer to buffer containing raw IFD data
382 * \param[in] ds size of raw data in buffer at \c d
383 * \param[in] offset offset into buffer at \c d at which IFD starts
384 * \param[in] recursion_cost factor indicating how expensive this recursive
388 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
389 const unsigned char *d,
390 unsigned int ds, unsigned int offset, unsigned int recursion_cost)
392 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
398 if (!data || !data->priv)
401 /* check for valid ExifIfd enum range */
402 if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
405 if (recursion_cost > 170) {
407 * recursion_cost is a logarithmic-scale indicator of how expensive this
408 * recursive call might end up being. It is an indicator of the depth of
409 * recursion as well as the potential for worst-case future recursive
410 * calls. Since it's difficult to tell ahead of time how often recursion
411 * will occur, this assumes the worst by assuming every tag could end up
413 * The value of 170 was chosen to limit typical EXIF structures to a
414 * recursive depth of about 6, but pathological ones (those with very
415 * many tags) to only 2.
417 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
418 "Deep/expensive recursion detected!");
422 /* Read the number of entries */
423 if ((offset + 2 < offset) || (offset + 2 < 2) || (offset + 2 > ds)) {
424 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
425 "Tag data past end of buffer (%u > %u)", offset+2, ds);
428 n = exif_get_short (d + offset, data->priv->order);
429 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
430 "Loading %hu entries...", n);
433 /* Check if we have enough data. */
434 if (offset + 12 * n > ds) {
435 n = (ds - offset) / 12;
436 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
437 "Short data; only loading %hu entries...", n);
440 for (i = 0; i < n; i++) {
442 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
444 case EXIF_TAG_EXIF_IFD_POINTER:
445 case EXIF_TAG_GPS_INFO_IFD_POINTER:
446 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
447 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
448 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
449 o = exif_get_long (d + offset + 12 * i + 8,
451 /* FIXME: IFD_POINTER tags aren't marked as being in a
452 * specific IFD, so exif_tag_get_name_in_ifd won't work
454 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
455 "Sub-IFD entry 0x%x ('%s') at %u.", tag,
456 exif_tag_get_name(tag), o);
458 case EXIF_TAG_EXIF_IFD_POINTER:
459 CHECK_REC (EXIF_IFD_EXIF);
460 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o,
461 recursion_cost + level_cost(n));
463 case EXIF_TAG_GPS_INFO_IFD_POINTER:
464 CHECK_REC (EXIF_IFD_GPS);
465 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o,
466 recursion_cost + level_cost(n));
468 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
469 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
470 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o,
471 recursion_cost + level_cost(n));
473 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
474 thumbnail_offset = o;
475 if (thumbnail_offset && thumbnail_length)
476 exif_data_load_data_thumbnail (data, d,
477 ds, thumbnail_offset,
480 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
481 thumbnail_length = o;
482 if (thumbnail_offset && thumbnail_length)
483 exif_data_load_data_thumbnail (data, d,
484 ds, thumbnail_offset,
494 * If we don't know the tag, don't fail. It could be that new
495 * versions of the standard have defined additional tags. Note that
496 * 0 is a valid tag in the GPS IFD.
498 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
501 * Special case: Tag and format 0. That's against specification
502 * (at least up to 2.2). But Photoshop writes it anyways.
504 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
505 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
506 "Skipping empty entry at position %u in '%s'.", i,
507 exif_ifd_get_name (ifd));
510 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
511 "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
512 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
513 exif_ifd_get_name (ifd));
514 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
517 entry = exif_entry_new_mem (data->priv->mem);
519 exif_log (data->priv->log, EXIF_LOG_CODE_NO_MEMORY, "ExifData",
520 "Could not allocate memory");
523 if (exif_data_load_data_entry (data, entry, d, ds,
525 exif_content_add_entry (data->ifd[ifd], entry);
526 exif_entry_unref (entry);
533 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
535 ExifShort tag1 = exif_get_short (p1, o);
536 ExifShort tag2 = exif_get_short (p2, o);
538 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
542 cmp_func_intel (const void *elem1, const void *elem2)
544 return cmp_func ((const unsigned char *) elem1,
545 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
549 cmp_func_motorola (const void *elem1, const void *elem2)
551 return cmp_func ((const unsigned char *) elem1,
552 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
556 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
557 unsigned char **d, unsigned int *ds,
560 unsigned int j, n_ptr = 0, n_thumb = 0;
565 if (!data || !data->priv || !ifd || !d || !ds)
568 for (i = 0; i < EXIF_IFD_COUNT; i++)
569 if (ifd == data->ifd[i])
571 if (i == EXIF_IFD_COUNT)
575 * Check if we need some extra entries for pointers or the thumbnail.
581 * The pointer to IFD_EXIF is in IFD_0. The pointer to
582 * IFD_INTEROPERABILITY is in IFD_EXIF.
584 if (data->ifd[EXIF_IFD_EXIF]->count ||
585 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
588 /* The pointer to IFD_GPS is in IFD_0. */
589 if (data->ifd[EXIF_IFD_GPS]->count)
598 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
605 * Allocate enough memory for all entries
606 * and the number of entries.
608 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
609 t = exif_mem_realloc (data->priv->mem, *d, ts);
611 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
617 /* Save the number of entries */
618 exif_set_short (*d + 6 + offset, data->priv->order,
619 (ExifShort) (ifd->count + n_ptr + n_thumb));
623 * Save each entry. Make sure that no memcpys from NULL pointers are
626 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
627 "Saving %i entries (IFD '%s', offset: %i)...",
628 ifd->count, exif_ifd_get_name (i), offset);
629 for (j = 0; j < ifd->count; j++) {
630 if (ifd->entries[j]) {
631 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
636 offset += 12 * ifd->count;
638 /* Now save special entries. */
643 * The pointer to IFD_EXIF is in IFD_0.
644 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
645 * therefore, if IFD_INTEROPERABILITY is not empty, we need
646 * IFD_EXIF even if latter is empty.
648 if (data->ifd[EXIF_IFD_EXIF]->count ||
649 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
650 exif_set_short (*d + 6 + offset + 0, data->priv->order,
651 EXIF_TAG_EXIF_IFD_POINTER);
652 exif_set_short (*d + 6 + offset + 2, data->priv->order,
654 exif_set_long (*d + 6 + offset + 4, data->priv->order,
656 exif_set_long (*d + 6 + offset + 8, data->priv->order,
658 exif_data_save_data_content (data,
659 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
663 /* The pointer to IFD_GPS is in IFD_0, too. */
664 if (data->ifd[EXIF_IFD_GPS]->count) {
665 exif_set_short (*d + 6 + offset + 0, data->priv->order,
666 EXIF_TAG_GPS_INFO_IFD_POINTER);
667 exif_set_short (*d + 6 + offset + 2, data->priv->order,
669 exif_set_long (*d + 6 + offset + 4, data->priv->order,
671 exif_set_long (*d + 6 + offset + 8, data->priv->order,
673 exif_data_save_data_content (data,
674 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
682 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
685 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
686 exif_set_short (*d + 6 + offset + 0, data->priv->order,
687 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
688 exif_set_short (*d + 6 + offset + 2, data->priv->order,
690 exif_set_long (*d + 6 + offset + 4, data->priv->order,
692 exif_set_long (*d + 6 + offset + 8, data->priv->order,
694 exif_data_save_data_content (data,
695 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
704 * Information about the thumbnail (if any) is saved in
709 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
710 exif_set_short (*d + 6 + offset + 0, data->priv->order,
711 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
712 exif_set_short (*d + 6 + offset + 2, data->priv->order,
714 exif_set_long (*d + 6 + offset + 4, data->priv->order,
716 exif_set_long (*d + 6 + offset + 8, data->priv->order,
718 ts = *ds + data->size;
719 t = exif_mem_realloc (data->priv->mem, *d, ts);
721 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
727 memcpy (*d + *ds - data->size, data->data, data->size);
730 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
731 exif_set_short (*d + 6 + offset + 0, data->priv->order,
732 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
733 exif_set_short (*d + 6 + offset + 2, data->priv->order,
735 exif_set_long (*d + 6 + offset + 4, data->priv->order,
737 exif_set_long (*d + 6 + offset + 8, data->priv->order,
747 /* Sort the directory according to TIFF specification */
748 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
749 (ifd->count + n_ptr + n_thumb), 12,
750 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
752 /* Correctly terminate the directory */
753 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
757 * We are saving IFD 0. Tell where IFD 1 starts and save
760 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
761 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
764 exif_set_long (*d + 6 + offset, data->priv->order, 0);
768 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
769 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
770 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
771 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
772 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
773 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
774 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6
775 } ExifDataTypeMakerNote;
777 /*! If MakerNote is recognized, load it.
779 * \param[in,out] data #ExifData
780 * \param[in] d pointer to raw EXIF data
781 * \param[in] ds length of data at d
784 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
787 ExifEntry* e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
791 if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
792 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
793 "ExifData", "Olympus MakerNote variant type %d", mnoteid);
794 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
796 } else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
797 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
798 "ExifData", "Canon MakerNote variant type %d", mnoteid);
799 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
801 } else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
802 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
803 "ExifData", "Fuji MakerNote variant type %d", mnoteid);
804 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
806 /* NOTE: Must do Pentax detection last because some of the
807 * heuristics are pretty general. */
808 } else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
809 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
810 "ExifData", "Pentax MakerNote variant type %d", mnoteid);
811 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
815 * If we are able to interpret the maker note, do so.
817 if (data->priv->md) {
818 exif_mnote_data_log (data->priv->md, data->priv->log);
819 exif_mnote_data_set_byte_order (data->priv->md,
821 exif_mnote_data_set_offset (data->priv->md,
822 data->priv->offset_mnote);
823 exif_mnote_data_load (data->priv->md, d, ds);
827 #define LOG_TOO_SMALL \
828 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
829 _("Size of data too small to allow for EXIF data."));
832 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
838 const unsigned char *d = d_orig;
839 unsigned int len, fullds;
841 if (!data || !data->priv || !d || !ds)
844 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
845 "Parsing %i byte(s) EXIF data...\n", ds);
848 * It can be that the data starts with the EXIF header. If it does
849 * not, search the EXIF marker.
855 if (!memcmp (d, ExifHeader, 6)) {
856 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
857 "Found EXIF header at start.");
860 while (ds && (d[0] == 0xff)) {
865 /* JPEG_MARKER_SOI */
866 if (ds && d[0] == JPEG_MARKER_SOI) {
872 /* JPEG_MARKER_APP1 */
873 if (ds && d[0] == JPEG_MARKER_APP1)
876 /* Skip irrelevant APP markers. The branch for APP1 must come before this,
877 otherwise this code block will cause APP1 to be skipped. This code path
878 is only relevant for files that are nonconformant to the EXIF
879 specification. For conformant files, the APP1 code path above will be
881 if (ds >= 3 && d[0] >= 0xe0 && d[0] <= 0xef) { /* JPEG_MARKER_APPn */
884 l = (d[0] << 8) | d[1];
892 /* Unknown marker or data. Give up. */
893 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
894 "ExifData", _("EXIF marker not found."));
903 len = (d[0] << 8) | d[1];
904 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
905 "We have to deal with %i byte(s) of EXIF data.",
912 * Verify the exif header
913 * (offset 2, length 6).
919 if (memcmp (d, ExifHeader, 6)) {
920 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
921 "ExifData", _("EXIF header not found."));
925 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
926 "Found EXIF header.");
928 /* Sanity check the data length */
932 /* The JPEG APP1 section can be no longer than 64 KiB (including a
933 16-bit length), so cap the data length to protect against overflow
934 in future offset calculations */
939 /* Byte order (offset 6, length 2) */
940 if (!memcmp (d + 6, "II", 2))
941 data->priv->order = EXIF_BYTE_ORDER_INTEL;
942 else if (!memcmp (d + 6, "MM", 2))
943 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
945 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
946 "ExifData", _("Unknown encoding."));
951 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
955 offset = exif_get_long (d + 10, data->priv->order);
956 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
957 "IFD 0 at %i.", (int) offset);
959 /* ds is restricted to 16 bit above, so offset is restricted too, and offset+8 should not overflow. */
960 if (offset > ds || offset + 6 + 2 > ds)
963 /* Parse the actual exif data (usually offset 14 from start) */
964 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
967 n = exif_get_short (d + 6 + offset, data->priv->order);
968 /* offset < 2<<16, n is 16 bit at most, so this op will not overflow */
969 if (offset + 6 + 2 + 12 * n + 4 > ds)
972 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
974 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
975 "IFD 1 at %i.", (int) offset);
977 /* Sanity check. ds is ensured to be above 6 above, offset is 16bit */
978 if (offset > ds - 6) {
979 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
980 "ExifData", "Bogus offset of IFD1.");
982 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
987 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
988 * cameras use pointers in the maker note tag that point to the
989 * space between IFDs. Here is the only place where we have access
992 interpret_maker_note(data, d, fullds);
994 /* Fixup tags if requested */
995 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
996 exif_data_fix (data);
1000 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
1003 *ds = 0; /* This means something went wrong */
1005 if (!data || !d || !ds)
1010 *d = exif_data_alloc (data, *ds);
1015 memcpy (*d, ExifHeader, 6);
1017 /* Order (offset 6) */
1018 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
1019 memcpy (*d + 6, "II", 2);
1021 memcpy (*d + 6, "MM", 2);
1024 /* Fixed value (2 bytes, offset 8) */
1025 exif_set_short (*d + 8, data->priv->order, 0x002a);
1028 * IFD 0 offset (4 bytes, offset 10).
1029 * We will start 8 bytes after the
1030 * EXIF header (2 bytes for order, another 2 for the test, and
1031 * 4 bytes for the IFD 0 offset make 8 bytes together).
1033 exif_set_long (*d + 10, data->priv->order, 8);
1035 /* Now save IFD 0. IFD 1 will be saved automatically. */
1036 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1038 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
1040 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1041 "Saved %i byte(s) EXIF data.", *ds);
1045 exif_data_new_from_file (const char *path)
1050 loader = exif_loader_new ();
1051 exif_loader_write_file (loader, path);
1052 edata = exif_loader_get_data (loader);
1053 exif_loader_unref (loader);
1059 exif_data_ref (ExifData *data)
1064 data->priv->ref_count++;
1068 exif_data_unref (ExifData *data)
1073 data->priv->ref_count--;
1074 if (!data->priv->ref_count)
1075 exif_data_free (data);
1079 exif_data_free (ExifData *data)
1082 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1087 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1089 exif_content_unref (data->ifd[i]);
1090 data->ifd[i] = NULL;
1095 exif_mem_free (mem, data->data);
1100 if (data->priv->log) {
1101 exif_log_unref (data->priv->log);
1102 data->priv->log = NULL;
1104 if (data->priv->md) {
1105 exif_mnote_data_unref (data->priv->md);
1106 data->priv->md = NULL;
1108 exif_mem_free (mem, data->priv);
1109 exif_mem_free (mem, data);
1112 exif_mem_unref (mem);
1116 exif_data_dump (ExifData *data)
1123 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1124 if (data->ifd[i] && data->ifd[i]->count) {
1125 printf ("Dumping IFD '%s'...\n",
1126 exif_ifd_get_name (i));
1127 exif_content_dump (data->ifd[i], 0);
1132 printf ("%i byte(s) thumbnail data available: ", data->size);
1133 if (data->size >= 4) {
1134 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1135 data->data[0], data->data[1],
1136 data->data[data->size - 2],
1137 data->data[data->size - 1]);
1143 exif_data_get_byte_order (ExifData *data)
1148 return (data->priv->order);
1152 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1160 for (i = 0; i < EXIF_IFD_COUNT; i++)
1161 func (data->ifd[i], user_data);
1164 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1165 struct _ByteOrderChangeData {
1166 ExifByteOrder old, new;
1170 entry_set_byte_order (ExifEntry *e, void *data)
1172 ByteOrderChangeData *d = data;
1177 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1181 content_set_byte_order (ExifContent *content, void *data)
1183 exif_content_foreach_entry (content, entry_set_byte_order, data);
1187 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1189 ByteOrderChangeData d;
1191 if (!data || (order == data->priv->order))
1194 d.old = data->priv->order;
1196 exif_data_foreach_content (data, content_set_byte_order, &d);
1197 data->priv->order = order;
1199 exif_mnote_data_set_byte_order (data->priv->md, order);
1203 exif_data_log (ExifData *data, ExifLog *log)
1207 if (!data || !data->priv)
1209 exif_log_unref (data->priv->log);
1210 data->priv->log = log;
1213 for (i = 0; i < EXIF_IFD_COUNT; i++)
1214 exif_content_log (data->ifd[i], log);
1217 /* Used internally within libexif */
1218 ExifLog *exif_data_get_log (ExifData *);
1220 exif_data_get_log (ExifData *data)
1222 if (!data || !data->priv)
1224 return data->priv->log;
1227 static const struct {
1228 ExifDataOption option;
1230 const char *description;
1231 } exif_data_option[] = {
1232 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1233 N_("Ignore unknown tags when loading EXIF data.")},
1234 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1235 N_("Add, correct and remove entries to get EXIF data that follows "
1236 "the specification.")},
1237 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1238 N_("When loading and resaving Exif data, save the maker note unmodified."
1239 " Be aware that the maker note can get corrupted.")},
1244 exif_data_option_get_name (ExifDataOption o)
1248 for (i = 0; exif_data_option[i].name; i++)
1249 if (exif_data_option[i].option == o)
1251 return _(exif_data_option[i].name);
1255 exif_data_option_get_description (ExifDataOption o)
1259 for (i = 0; exif_data_option[i].description; i++)
1260 if (exif_data_option[i].option == o)
1262 return _(exif_data_option[i].description);
1266 exif_data_set_option (ExifData *d, ExifDataOption o)
1271 d->priv->options |= o;
1275 exif_data_unset_option (ExifData *d, ExifDataOption o)
1280 d->priv->options &= ~o;
1284 fix_func (ExifContent *c, void *UNUSED(data))
1286 switch (exif_content_get_ifd (c)) {
1288 if (c->parent->data)
1289 exif_content_fix (c);
1290 else if (c->count) {
1291 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1292 "No thumbnail but entries on thumbnail. These entries have been "
1295 unsigned int cnt = c->count;
1296 exif_content_remove_entry (c, c->entries[c->count - 1]);
1297 if (cnt == c->count) {
1299 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1300 "failed to remove last entry from entries.");
1307 exif_content_fix (c);
1312 exif_data_fix (ExifData *d)
1314 exif_data_foreach_content (d, fix_func, NULL);
1318 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1323 d->priv->data_type = dt;
1327 exif_data_get_data_type (ExifData *d)
1329 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;