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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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>
42 #if defined(__WATCOMC__) || defined(_MSC_VER)
43 # define strncasecmp strnicmp
46 #undef JPEG_MARKER_SOI
47 #define JPEG_MARKER_SOI 0xd8
48 #undef JPEG_MARKER_APP0
49 #define JPEG_MARKER_APP0 0xe0
50 #undef JPEG_MARKER_APP1
51 #define JPEG_MARKER_APP1 0xe1
53 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
55 struct _ExifDataPrivate
64 unsigned int ref_count;
66 /* Temporarily used while loading data */
67 unsigned int offset_mnote;
69 ExifDataOption options;
70 ExifDataType data_type;
74 exif_data_alloc (ExifData *data, unsigned int i)
81 d = exif_mem_alloc (data->priv->mem, i);
85 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
90 exif_data_get_mnote_data (ExifData *d)
92 return (d && d->priv) ? d->priv->md : NULL;
98 ExifMem *mem = exif_mem_new_default ();
99 ExifData *d = exif_data_new_mem (mem);
101 exif_mem_unref (mem);
107 exif_data_new_mem (ExifMem *mem)
115 data = exif_mem_alloc (mem, sizeof (ExifData));
118 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
120 exif_mem_free (mem, data);
123 data->priv->ref_count = 1;
125 data->priv->mem = mem;
128 for (i = 0; i < EXIF_IFD_COUNT; i++) {
129 data->ifd[i] = exif_content_new_mem (data->priv->mem);
131 exif_data_free (data);
134 data->ifd[i]->parent = data;
137 /* Default options */
138 #ifndef NO_VERBOSE_TAG_STRINGS
140 * When the tag list is compiled away, setting this option prevents
141 * any tags from being loaded
143 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
145 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
147 /* Default data type: none */
148 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
154 exif_data_new_from_data (const unsigned char *data, unsigned int size)
158 edata = exif_data_new ();
159 exif_data_load_data (edata, data, size);
164 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
165 const unsigned char *d,
166 unsigned int size, unsigned int offset)
168 unsigned int s, doff;
170 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
171 entry->format = exif_get_short (d + offset + 2, data->priv->order);
172 entry->components = exif_get_long (d + offset + 4, data->priv->order);
174 /* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent
175 * has not been set yet
177 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
178 "Loading entry 0x%x ('%s')...", entry->tag,
179 exif_tag_get_name (entry->tag));
181 /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
182 * -> { 0x000000000 .. 0x7fffffff8 } */
183 s = exif_format_get_size(entry->format) * entry->components;
184 if (s < entry->components) {
190 * Size? If bigger than 4 bytes, the actual data is not
191 * in the entry but somewhere else (offset).
194 doff = exif_get_long (d + offset + 8, data->priv->order);
199 if ((doff + s < doff) || (doff + s < s))
204 entry->data = exif_data_alloc (data, s);
207 memcpy (entry->data, d + doff, s);
209 /* FIXME: What do our callers do if (entry->data == NULL)? */
210 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
213 /* If this is the MakerNote, remember the offset */
214 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
216 exif_log (data->priv->log,
217 EXIF_LOG_CODE_DEBUG, "ExifData",
218 "MakerNote found with NULL data");
219 } else if (entry->size > 6) {
220 exif_log (data->priv->log,
221 EXIF_LOG_CODE_DEBUG, "ExifData",
222 "MakerNote found (%02x %02x %02x %02x "
223 "%02x %02x %02x...).",
224 entry->data[0], entry->data[1], entry->data[2],
225 entry->data[3], entry->data[4], entry->data[5],
228 data->priv->offset_mnote = doff;
234 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
235 unsigned char **d, unsigned int *ds,
238 unsigned int doff, s;
242 if (!data || !data->priv)
246 * Each entry is 12 bytes long. The memory for the entry has
247 * already been allocated.
249 exif_set_short (*d + 6 + offset + 0,
250 data->priv->order, (ExifShort) e->tag);
251 exif_set_short (*d + 6 + offset + 2,
252 data->priv->order, (ExifShort) e->format);
254 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
255 /* If this is the maker note tag, update it. */
256 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
257 exif_mem_free (data->priv->mem, e->data);
260 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
261 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
262 e->components = e->size;
266 exif_set_long (*d + 6 + offset + 4,
267 data->priv->order, e->components);
270 * Size? If bigger than 4 bytes, the actual data is not in
271 * the entry but somewhere else.
273 s = exif_format_get_size (e->format) * e->components;
279 * According to the TIFF specification,
280 * the offset must be an even number. If we need to introduce
281 * a padding byte, we set it to 0.
285 t = exif_mem_realloc (data->priv->mem, *d, ts);
287 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
292 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
294 *(*d + *ds - 1) = '\0';
299 /* Write the data. Fill unneeded bytes with 0. Do not crash with
302 memcpy (*d + 6 + doff, e->data, s);
304 memset (*d + 6 + doff, 0, s);
307 memset (*d + 6 + doff + s, 0, (4 - s));
311 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
312 unsigned int ds, ExifLong offset, ExifLong size)
314 if ((ds < offset + size) || (offset > ds)) {
315 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
316 "Bogus thumbnail offset (%u) or size (%u).",
321 exif_mem_free (data->priv->mem, data->data);
323 data->data = exif_data_alloc (data, data->size);
326 memcpy (data->data, d + offset, data->size);
330 #define CHECK_REC(i) \
332 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
333 "ExifData", "Recursive entry in IFD " \
334 "'%s' detected. Skipping...", \
335 exif_ifd_get_name (i)); \
338 if (data->ifd[(i)]->count) { \
339 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
340 "ExifData", "Attempt to load IFD " \
341 "'%s' multiple times detected. " \
343 exif_ifd_get_name (i)); \
347 /*! Load data for an IFD.
349 * \param[in/out] data #ExifData
350 * \param[in] ifd IFD to load
351 * \param[in] d pointer to buffer containing raw IFD data
352 * \param[in] ds size of raw data in buffer at \c d
353 * \param[in] offset offset into buffer at \c d at which IFD starts
354 * \param[in] recursion_depth number of times this function has been
355 * recursively called without returning
358 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
359 const unsigned char *d,
360 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
362 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
368 if (!data || !data->priv)
371 /* check for valid ExifIfd enum range */
372 if (( ((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
375 if (recursion_depth > 150) {
376 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
377 "Deep recursion detected!");
381 /* Read the number of entries */
382 if (offset >= ds - 1)
384 n = exif_get_short (d + offset, data->priv->order);
385 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
386 "Loading %i entries...", n);
389 /* Check if we have enough data. */
390 if (offset + 12 * n > ds)
391 n = (ds - offset) / 12;
393 for (i = 0; i < n; i++) {
395 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
397 case EXIF_TAG_EXIF_IFD_POINTER:
398 case EXIF_TAG_GPS_INFO_IFD_POINTER:
399 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
400 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
401 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
402 o = exif_get_long (d + offset + 12 * i + 8,
404 /* FIXME: IFD_POINTER tags aren't marked as being in a
405 * specific IFD, so exif_tag_get_name_in_ifd won't work
407 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
408 "IFD entry 0x%x at %u.", tag, o);
410 case EXIF_TAG_EXIF_IFD_POINTER:
411 CHECK_REC (EXIF_IFD_EXIF);
412 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
414 case EXIF_TAG_GPS_INFO_IFD_POINTER:
415 CHECK_REC (EXIF_IFD_GPS);
416 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
418 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
419 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
420 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
422 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
423 thumbnail_offset = o;
424 if (thumbnail_offset && thumbnail_length)
425 exif_data_load_data_thumbnail (data, d,
426 ds, thumbnail_offset,
429 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
430 thumbnail_length = o;
431 if (thumbnail_offset && thumbnail_length)
432 exif_data_load_data_thumbnail (data, d,
433 ds, thumbnail_offset,
443 * If we don't know the tag, don't fail. It could be that new
444 * versions of the standard have defined additional tags. Note that
445 * 0 is a valid tag in the GPS IFD.
447 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
450 * Special case: Tag and format 0. That's against specification.
451 * At least up to 2.2. But Photoshop writes it anyways.
453 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
454 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
455 "Skipping empty entry at position %i in '%s'.", i,
456 exif_ifd_get_name (ifd));
459 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
460 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
461 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
462 exif_ifd_get_name (ifd));
463 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
466 entry = exif_entry_new_mem (data->priv->mem);
467 if (exif_data_load_data_entry (data, entry, d, ds,
469 exif_content_add_entry (data->ifd[ifd], entry);
470 exif_entry_unref (entry);
477 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
479 ExifShort tag1 = exif_get_short (p1, o);
480 ExifShort tag2 = exif_get_short (p2, o);
482 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
486 cmp_func_intel (const void *elem1, const void *elem2)
488 return cmp_func ((const unsigned char *) elem1,
489 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
493 cmp_func_motorola (const void *elem1, const void *elem2)
495 return cmp_func ((const unsigned char *) elem1,
496 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
500 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
501 unsigned char **d, unsigned int *ds,
504 unsigned int j, n_ptr = 0, n_thumb = 0;
509 if (!data || !data->priv || !ifd || !d || !ds)
512 for (i = 0; i < EXIF_IFD_COUNT; i++)
513 if (ifd == data->ifd[i])
515 if (i == EXIF_IFD_COUNT)
519 * Check if we need some extra entries for pointers or the thumbnail.
525 * The pointer to IFD_EXIF is in IFD_0. The pointer to
526 * IFD_INTEROPERABILITY is in IFD_EXIF.
528 if (data->ifd[EXIF_IFD_EXIF]->count ||
529 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
532 /* The pointer to IFD_GPS is in IFD_0. */
533 if (data->ifd[EXIF_IFD_GPS]->count)
542 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
549 * Allocate enough memory for all entries
550 * and the number of entries.
552 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
553 t = exif_mem_realloc (data->priv->mem, *d, ts);
555 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
561 /* Save the number of entries */
562 exif_set_short (*d + 6 + offset, data->priv->order,
563 (ExifShort) (ifd->count + n_ptr + n_thumb));
567 * Save each entry. Make sure that no memcpys from NULL pointers are
570 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
571 "Saving %i entries (IFD '%s', offset: %i)...",
572 ifd->count, exif_ifd_get_name (i), offset);
573 for (j = 0; j < ifd->count; j++) {
574 if (ifd->entries[j]) {
575 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
580 offset += 12 * ifd->count;
582 /* Now save special entries. */
587 * The pointer to IFD_EXIF is in IFD_0.
588 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
589 * therefore, if IFD_INTEROPERABILITY is not empty, we need
590 * IFD_EXIF even if latter is empty.
592 if (data->ifd[EXIF_IFD_EXIF]->count ||
593 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
594 exif_set_short (*d + 6 + offset + 0, data->priv->order,
595 EXIF_TAG_EXIF_IFD_POINTER);
596 exif_set_short (*d + 6 + offset + 2, data->priv->order,
598 exif_set_long (*d + 6 + offset + 4, data->priv->order,
600 exif_set_long (*d + 6 + offset + 8, data->priv->order,
602 exif_data_save_data_content (data,
603 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
607 /* The pointer to IFD_GPS is in IFD_0, too. */
608 if (data->ifd[EXIF_IFD_GPS]->count) {
609 exif_set_short (*d + 6 + offset + 0, data->priv->order,
610 EXIF_TAG_GPS_INFO_IFD_POINTER);
611 exif_set_short (*d + 6 + offset + 2, data->priv->order,
613 exif_set_long (*d + 6 + offset + 4, data->priv->order,
615 exif_set_long (*d + 6 + offset + 8, data->priv->order,
617 exif_data_save_data_content (data,
618 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
626 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
629 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
630 exif_set_short (*d + 6 + offset + 0, data->priv->order,
631 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
632 exif_set_short (*d + 6 + offset + 2, data->priv->order,
634 exif_set_long (*d + 6 + offset + 4, data->priv->order,
636 exif_set_long (*d + 6 + offset + 8, data->priv->order,
638 exif_data_save_data_content (data,
639 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
648 * Information about the thumbnail (if any) is saved in
653 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
654 exif_set_short (*d + 6 + offset + 0, data->priv->order,
655 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
656 exif_set_short (*d + 6 + offset + 2, data->priv->order,
658 exif_set_long (*d + 6 + offset + 4, data->priv->order,
660 exif_set_long (*d + 6 + offset + 8, data->priv->order,
662 ts = *ds + data->size;
663 t = exif_mem_realloc (data->priv->mem, *d, ts);
665 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
671 memcpy (*d + *ds - data->size, data->data, data->size);
674 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
675 exif_set_short (*d + 6 + offset + 0, data->priv->order,
676 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
677 exif_set_short (*d + 6 + offset + 2, data->priv->order,
679 exif_set_long (*d + 6 + offset + 4, data->priv->order,
681 exif_set_long (*d + 6 + offset + 8, data->priv->order,
691 /* Sort the directory according to TIFF specification */
692 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
693 (ifd->count + n_ptr + n_thumb), 12,
694 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
696 /* Correctly terminate the directory */
697 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
701 * We are saving IFD 0. Tell where IFD 1 starts and save
704 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
705 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
708 exif_set_long (*d + 6 + offset, data->priv->order, 0);
712 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
713 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
714 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
715 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
716 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
717 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
718 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6
719 } ExifDataTypeMakerNote;
721 static ExifDataTypeMakerNote
722 exif_data_get_type_maker_note (ExifData *d)
728 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
730 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
732 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
734 /* Olympus & Nikon & Sanyo */
735 if ((e->size >= 8) && ( !memcmp (e->data, "OLYMP", 6) ||
736 !memcmp (e->data, "OLYMPUS", 8) ||
737 !memcmp (e->data, "SANYO", 6) ||
738 !memcmp (e->data, "EPSON", 6) ||
739 !memcmp (e->data, "Nikon", 6)))
740 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
742 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
744 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
747 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
748 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
750 /* Pentax & some variant of Nikon */
751 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
753 exif_entry_get_value (em, value, sizeof(value)),
755 return EXIF_DATA_TYPE_MAKER_NOTE_NIKON;
757 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
759 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
760 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
762 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4)) {
763 return EXIF_DATA_TYPE_MAKER_NOTE_CASIO;
765 if ((e->size >= 12) && !memcmp (e->data, "FUJIFILM", 8)) {
766 return EXIF_DATA_TYPE_MAKER_NOTE_FUJI;
769 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
772 #define LOG_TOO_SMALL \
773 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
774 _("Size of data too small to allow for EXIF data."));
777 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
778 unsigned int ds_orig)
783 const unsigned char *d = d_orig;
784 unsigned int ds = ds_orig, len;
786 if (!data || !data->priv || !d || !ds)
789 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
790 "Parsing %i byte(s) EXIF data...\n", ds);
793 * It can be that the data starts with the EXIF header. If it does
794 * not, search the EXIF marker.
800 if (!memcmp (d, ExifHeader, 6)) {
801 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
802 "Found EXIF header.");
805 while ((d[0] == 0xff) && ds) {
810 /* JPEG_MARKER_SOI */
811 if (d[0] == JPEG_MARKER_SOI) {
817 /* JPEG_MARKER_APP0 */
818 if (d[0] == JPEG_MARKER_APP0) {
821 l = (d[0] << 8) | d[1];
829 /* JPEG_MARKER_APP1 */
830 if (d[0] == JPEG_MARKER_APP1)
833 /* Unknown marker or data. Give up. */
834 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
835 "ExifData", _("EXIF marker not found."));
844 len = (d[0] << 8) | d[1];
845 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
846 "We have to deal with %i byte(s) of EXIF data.",
853 * Verify the exif header
854 * (offset 2, length 6).
860 if (memcmp (d, ExifHeader, 6)) {
861 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
862 "ExifData", _("EXIF header not found."));
866 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
867 "Found EXIF header.");
869 /* Byte order (offset 6, length 2) */
872 if (!memcmp (d + 6, "II", 2))
873 data->priv->order = EXIF_BYTE_ORDER_INTEL;
874 else if (!memcmp (d + 6, "MM", 2))
875 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
877 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
878 "ExifData", _("Unknown encoding."));
883 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
887 offset = exif_get_long (d + 10, data->priv->order);
888 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
889 "IFD 0 at %i.", (int) offset);
891 /* Parse the actual exif data (usually offset 14 from start) */
892 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
895 if (offset + 6 + 2 > ds) {
898 n = exif_get_short (d + 6 + offset, data->priv->order);
899 if (offset + 6 + 2 + 12 * n + 4 > ds) {
902 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
904 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
905 "IFD 1 at %i.", (int) offset);
908 if (offset > ds - 6) {
909 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
910 "ExifData", "Bogus offset of IFD1.");
912 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
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
922 switch (exif_data_get_type_maker_note (data)) {
923 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
924 case EXIF_DATA_TYPE_MAKER_NOTE_NIKON:
925 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
927 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
928 case EXIF_DATA_TYPE_MAKER_NOTE_CASIO:
929 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
931 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
932 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
934 case EXIF_DATA_TYPE_MAKER_NOTE_FUJI:
935 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
942 * If we are able to interpret the maker note, do so.
944 if (data->priv->md) {
945 exif_mnote_data_log (data->priv->md, data->priv->log);
946 exif_mnote_data_set_byte_order (data->priv->md,
948 exif_mnote_data_set_offset (data->priv->md,
949 data->priv->offset_mnote);
950 exif_mnote_data_load (data->priv->md, d, ds);
953 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
954 exif_data_fix (data);
958 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
961 *ds = 0; /* This means something went wrong */
963 if (!data || !d || !ds)
968 *d = exif_data_alloc (data, *ds);
973 memcpy (*d, ExifHeader, 6);
975 /* Order (offset 6) */
976 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
977 memcpy (*d + 6, "II", 2);
979 memcpy (*d + 6, "MM", 2);
982 /* Fixed value (2 bytes, offset 8) */
983 exif_set_short (*d + 8, data->priv->order, 0x002a);
986 * IFD 0 offset (4 bytes, offset 10).
987 * We will start 8 bytes after the
988 * EXIF header (2 bytes for order, another 2 for the test, and
989 * 4 bytes for the IFD 0 offset make 8 bytes together).
991 exif_set_long (*d + 10, data->priv->order, 8);
993 /* Now save IFD 0. IFD 1 will be saved automatically. */
994 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
996 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
998 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
999 "Saved %i byte(s) EXIF data.", *ds);
1003 exif_data_new_from_file (const char *path)
1008 loader = exif_loader_new ();
1009 exif_loader_write_file (loader, path);
1010 edata = exif_loader_get_data (loader);
1011 exif_loader_unref (loader);
1017 exif_data_ref (ExifData *data)
1022 data->priv->ref_count++;
1026 exif_data_unref (ExifData *data)
1031 data->priv->ref_count--;
1032 if (!data->priv->ref_count)
1033 exif_data_free (data);
1037 exif_data_free (ExifData *data)
1040 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1045 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1047 exif_content_unref (data->ifd[i]);
1048 data->ifd[i] = NULL;
1053 exif_mem_free (mem, data->data);
1058 if (data->priv->log) {
1059 exif_log_unref (data->priv->log);
1060 data->priv->log = NULL;
1062 if (data->priv->md) {
1063 exif_mnote_data_unref (data->priv->md);
1064 data->priv->md = NULL;
1066 exif_mem_free (mem, data->priv);
1067 exif_mem_free (mem, data);
1070 exif_mem_unref (mem);
1074 exif_data_dump (ExifData *data)
1081 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1082 if (data->ifd[i] && data->ifd[i]->count) {
1083 printf ("Dumping IFD '%s'...\n",
1084 exif_ifd_get_name (i));
1085 exif_content_dump (data->ifd[i], 0);
1090 printf ("%i byte(s) thumbnail data available.", data->size);
1091 if (data->size >= 4) {
1092 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1093 data->data[0], data->data[1],
1094 data->data[data->size - 2],
1095 data->data[data->size - 1]);
1101 exif_data_get_byte_order (ExifData *data)
1106 return (data->priv->order);
1110 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1118 for (i = 0; i < EXIF_IFD_COUNT; i++)
1119 func (data->ifd[i], user_data);
1122 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1123 struct _ByteOrderChangeData {
1124 ExifByteOrder old, new;
1128 entry_set_byte_order (ExifEntry *e, void *data)
1130 ByteOrderChangeData *d = data;
1135 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1139 content_set_byte_order (ExifContent *content, void *data)
1141 exif_content_foreach_entry (content, entry_set_byte_order, data);
1145 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1147 ByteOrderChangeData d;
1149 if (!data || (order == data->priv->order))
1152 d.old = data->priv->order;
1154 exif_data_foreach_content (data, content_set_byte_order, &d);
1155 data->priv->order = order;
1157 exif_mnote_data_set_byte_order (data->priv->md, order);
1161 exif_data_log (ExifData *data, ExifLog *log)
1165 if (!data || !data->priv)
1167 exif_log_unref (data->priv->log);
1168 data->priv->log = log;
1171 for (i = 0; i < EXIF_IFD_COUNT; i++)
1172 exif_content_log (data->ifd[i], log);
1175 /* Used internally within libexif */
1176 ExifLog *exif_data_get_log (ExifData *);
1178 exif_data_get_log (ExifData *data)
1180 if (!data || !data->priv)
1182 return data->priv->log;
1185 static const struct {
1186 ExifDataOption option;
1188 const char *description;
1189 } exif_data_option[] = {
1190 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1191 N_("Ignore unknown tags when loading EXIF data.")},
1192 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1193 N_("Add, correct and remove entries to get EXIF data that follows "
1194 "the specification.")},
1195 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1196 N_("When loading and resaving Exif data, save the maker note unmodified."
1197 " Be aware that the maker note can get corrupted.")},
1202 exif_data_option_get_name (ExifDataOption o)
1206 for (i = 0; exif_data_option[i].name; i++)
1207 if (exif_data_option[i].option == o)
1209 return _(exif_data_option[i].name);
1213 exif_data_option_get_description (ExifDataOption o)
1217 for (i = 0; exif_data_option[i].description; i++)
1218 if (exif_data_option[i].option == o)
1220 return _(exif_data_option[i].description);
1224 exif_data_set_option (ExifData *d, ExifDataOption o)
1229 d->priv->options |= o;
1233 exif_data_unset_option (ExifData *d, ExifDataOption o)
1238 d->priv->options &= ~o;
1242 fix_func (ExifContent *c, void *UNUSED(data))
1244 switch (exif_content_get_ifd (c)) {
1246 if (c->parent->data)
1247 exif_content_fix (c);
1249 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1250 "No thumbnail but entries on thumbnail. These entries have been "
1253 unsigned int cnt = c->count;
1254 exif_content_remove_entry (c, c->entries[c->count - 1]);
1255 if (cnt == c->count) {
1257 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1258 "failed to remove last entry from entries.");
1265 exif_content_fix (c);
1270 exif_data_fix (ExifData *d)
1272 exif_data_foreach_content (d, fix_func, NULL);
1276 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1281 d->priv->data_type = dt;
1285 exif_data_get_data_type (ExifData *d)
1287 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;