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) || (doff + s > size))
202 entry->data = exif_data_alloc (data, s);
205 memcpy (entry->data, d + doff, s);
207 /* FIXME: What do our callers do if (entry->data == NULL)? */
208 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
211 /* If this is the MakerNote, remember the offset */
212 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
214 exif_log (data->priv->log,
215 EXIF_LOG_CODE_DEBUG, "ExifData",
216 "MakerNote found with NULL data");
217 } else if (entry->size > 6) {
218 exif_log (data->priv->log,
219 EXIF_LOG_CODE_DEBUG, "ExifData",
220 "MakerNote found (%02x %02x %02x %02x "
221 "%02x %02x %02x...).",
222 entry->data[0], entry->data[1], entry->data[2],
223 entry->data[3], entry->data[4], entry->data[5],
226 data->priv->offset_mnote = doff;
232 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
233 unsigned char **d, unsigned int *ds,
236 unsigned int doff, s;
240 if (!data || !data->priv)
244 * Each entry is 12 bytes long. The memory for the entry has
245 * already been allocated.
247 exif_set_short (*d + 6 + offset + 0,
248 data->priv->order, (ExifShort) e->tag);
249 exif_set_short (*d + 6 + offset + 2,
250 data->priv->order, (ExifShort) e->format);
252 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
253 /* If this is the maker note tag, update it. */
254 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
255 exif_mem_free (data->priv->mem, e->data);
258 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
259 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
260 e->components = e->size;
264 exif_set_long (*d + 6 + offset + 4,
265 data->priv->order, e->components);
268 * Size? If bigger than 4 bytes, the actual data is not in
269 * the entry but somewhere else.
271 s = exif_format_get_size (e->format) * e->components;
277 * According to the TIFF specification,
278 * the offset must be an even number. If we need to introduce
279 * a padding byte, we set it to 0.
283 t = exif_mem_realloc (data->priv->mem, *d, ts);
285 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
290 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
292 *(*d + *ds - 1) = '\0';
297 /* Write the data. Fill unneeded bytes with 0. Do not crash with
300 memcpy (*d + 6 + doff, e->data, s);
302 memset (*d + 6 + doff, 0, s);
305 memset (*d + 6 + doff + s, 0, (4 - s));
309 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
310 unsigned int ds, ExifLong o, ExifLong s)
313 if ((o + s < o) || (o + s < s) || (o + s > ds) || (o > ds)) {
314 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
315 "Bogus thumbnail offset (%u) or size (%u).",
321 exif_mem_free (data->priv->mem, data->data);
322 if (!(data->data = exif_data_alloc (data, s))) {
327 memcpy (data->data, d + o, s);
331 #define CHECK_REC(i) \
333 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
334 "ExifData", "Recursive entry in IFD " \
335 "'%s' detected. Skipping...", \
336 exif_ifd_get_name (i)); \
339 if (data->ifd[(i)]->count) { \
340 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
341 "ExifData", "Attempt to load IFD " \
342 "'%s' multiple times detected. " \
344 exif_ifd_get_name (i)); \
348 /*! Load data for an IFD.
350 * \param[in,out] data #ExifData
351 * \param[in] ifd IFD to load
352 * \param[in] d pointer to buffer containing raw IFD data
353 * \param[in] ds size of raw data in buffer at \c d
354 * \param[in] offset offset into buffer at \c d at which IFD starts
355 * \param[in] recursion_depth number of times this function has been
356 * recursively called without returning
359 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
360 const unsigned char *d,
361 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
363 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
369 if (!data || !data->priv)
372 /* check for valid ExifIfd enum range */
373 if (( ((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
376 if (recursion_depth > 30) {
377 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
378 "Deep recursion detected!");
382 /* Read the number of entries */
383 if (offset >= ds - 1)
385 n = exif_get_short (d + offset, data->priv->order);
386 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
387 "Loading %i entries...", n);
390 /* Check if we have enough data. */
391 if (offset + 12 * n > ds)
392 n = (ds - offset) / 12;
394 for (i = 0; i < n; i++) {
396 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
398 case EXIF_TAG_EXIF_IFD_POINTER:
399 case EXIF_TAG_GPS_INFO_IFD_POINTER:
400 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
401 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
402 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
403 o = exif_get_long (d + offset + 12 * i + 8,
405 /* FIXME: IFD_POINTER tags aren't marked as being in a
406 * specific IFD, so exif_tag_get_name_in_ifd won't work
408 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
409 "IFD entry 0x%x at %u.", tag, o);
411 case EXIF_TAG_EXIF_IFD_POINTER:
412 CHECK_REC (EXIF_IFD_EXIF);
413 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
415 case EXIF_TAG_GPS_INFO_IFD_POINTER:
416 CHECK_REC (EXIF_IFD_GPS);
417 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
419 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
420 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
421 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
423 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
424 thumbnail_offset = o;
425 if (thumbnail_offset && thumbnail_length)
426 exif_data_load_data_thumbnail (data, d,
427 ds, thumbnail_offset,
430 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
431 thumbnail_length = o;
432 if (thumbnail_offset && thumbnail_length)
433 exif_data_load_data_thumbnail (data, d,
434 ds, thumbnail_offset,
444 * If we don't know the tag, don't fail. It could be that new
445 * versions of the standard have defined additional tags. Note that
446 * 0 is a valid tag in the GPS IFD.
448 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
451 * Special case: Tag and format 0. That's against specification.
452 * At least up to 2.2. But Photoshop writes it anyways.
454 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
455 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
456 "Skipping empty entry at position %i in '%s'.", i,
457 exif_ifd_get_name (ifd));
460 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
461 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
462 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
463 exif_ifd_get_name (ifd));
464 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
467 entry = exif_entry_new_mem (data->priv->mem);
468 if (exif_data_load_data_entry (data, entry, d, ds,
470 exif_content_add_entry (data->ifd[ifd], entry);
471 exif_entry_unref (entry);
478 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
480 ExifShort tag1 = exif_get_short (p1, o);
481 ExifShort tag2 = exif_get_short (p2, o);
483 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
487 cmp_func_intel (const void *elem1, const void *elem2)
489 return cmp_func ((const unsigned char *) elem1,
490 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
494 cmp_func_motorola (const void *elem1, const void *elem2)
496 return cmp_func ((const unsigned char *) elem1,
497 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
501 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
502 unsigned char **d, unsigned int *ds,
505 unsigned int j, n_ptr = 0, n_thumb = 0;
510 if (!data || !data->priv || !ifd || !d || !ds)
513 for (i = 0; i < EXIF_IFD_COUNT; i++)
514 if (ifd == data->ifd[i])
516 if (i == EXIF_IFD_COUNT)
520 * Check if we need some extra entries for pointers or the thumbnail.
526 * The pointer to IFD_EXIF is in IFD_0. The pointer to
527 * IFD_INTEROPERABILITY is in IFD_EXIF.
529 if (data->ifd[EXIF_IFD_EXIF]->count ||
530 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
533 /* The pointer to IFD_GPS is in IFD_0. */
534 if (data->ifd[EXIF_IFD_GPS]->count)
543 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
550 * Allocate enough memory for all entries
551 * and the number of entries.
553 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
554 t = exif_mem_realloc (data->priv->mem, *d, ts);
556 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
562 /* Save the number of entries */
563 exif_set_short (*d + 6 + offset, data->priv->order,
564 (ExifShort) (ifd->count + n_ptr + n_thumb));
568 * Save each entry. Make sure that no memcpys from NULL pointers are
571 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
572 "Saving %i entries (IFD '%s', offset: %i)...",
573 ifd->count, exif_ifd_get_name (i), offset);
574 for (j = 0; j < ifd->count; j++) {
575 if (ifd->entries[j]) {
576 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
581 offset += 12 * ifd->count;
583 /* Now save special entries. */
588 * The pointer to IFD_EXIF is in IFD_0.
589 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
590 * therefore, if IFD_INTEROPERABILITY is not empty, we need
591 * IFD_EXIF even if latter is empty.
593 if (data->ifd[EXIF_IFD_EXIF]->count ||
594 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
595 exif_set_short (*d + 6 + offset + 0, data->priv->order,
596 EXIF_TAG_EXIF_IFD_POINTER);
597 exif_set_short (*d + 6 + offset + 2, data->priv->order,
599 exif_set_long (*d + 6 + offset + 4, data->priv->order,
601 exif_set_long (*d + 6 + offset + 8, data->priv->order,
603 exif_data_save_data_content (data,
604 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
608 /* The pointer to IFD_GPS is in IFD_0, too. */
609 if (data->ifd[EXIF_IFD_GPS]->count) {
610 exif_set_short (*d + 6 + offset + 0, data->priv->order,
611 EXIF_TAG_GPS_INFO_IFD_POINTER);
612 exif_set_short (*d + 6 + offset + 2, data->priv->order,
614 exif_set_long (*d + 6 + offset + 4, data->priv->order,
616 exif_set_long (*d + 6 + offset + 8, data->priv->order,
618 exif_data_save_data_content (data,
619 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
627 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
630 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
631 exif_set_short (*d + 6 + offset + 0, data->priv->order,
632 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
633 exif_set_short (*d + 6 + offset + 2, data->priv->order,
635 exif_set_long (*d + 6 + offset + 4, data->priv->order,
637 exif_set_long (*d + 6 + offset + 8, data->priv->order,
639 exif_data_save_data_content (data,
640 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
649 * Information about the thumbnail (if any) is saved in
654 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
655 exif_set_short (*d + 6 + offset + 0, data->priv->order,
656 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
657 exif_set_short (*d + 6 + offset + 2, data->priv->order,
659 exif_set_long (*d + 6 + offset + 4, data->priv->order,
661 exif_set_long (*d + 6 + offset + 8, data->priv->order,
663 ts = *ds + data->size;
664 t = exif_mem_realloc (data->priv->mem, *d, ts);
666 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
672 memcpy (*d + *ds - data->size, data->data, data->size);
675 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
676 exif_set_short (*d + 6 + offset + 0, data->priv->order,
677 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
678 exif_set_short (*d + 6 + offset + 2, data->priv->order,
680 exif_set_long (*d + 6 + offset + 4, data->priv->order,
682 exif_set_long (*d + 6 + offset + 8, data->priv->order,
692 /* Sort the directory according to TIFF specification */
693 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
694 (ifd->count + n_ptr + n_thumb), 12,
695 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
697 /* Correctly terminate the directory */
698 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
702 * We are saving IFD 0. Tell where IFD 1 starts and save
705 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
706 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
709 exif_set_long (*d + 6 + offset, data->priv->order, 0);
713 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
714 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
715 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
716 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
717 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
718 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
719 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6
720 } ExifDataTypeMakerNote;
722 static ExifDataTypeMakerNote
723 exif_data_get_type_maker_note (ExifData *d)
729 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
731 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
733 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
735 /* Olympus & Nikon & Sanyo */
736 if ((e->size >= 8) && ( !memcmp (e->data, "OLYMP", 6) ||
737 !memcmp (e->data, "OLYMPUS", 8) ||
738 !memcmp (e->data, "SANYO", 6) ||
739 !memcmp (e->data, "EPSON", 6) ||
740 !memcmp (e->data, "Nikon", 6)))
741 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
743 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
745 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
748 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
749 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
751 /* Pentax & some variant of Nikon */
752 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
754 exif_entry_get_value (em, value, sizeof(value)),
756 return EXIF_DATA_TYPE_MAKER_NOTE_NIKON;
758 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
760 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
761 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
763 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4)) {
764 return EXIF_DATA_TYPE_MAKER_NOTE_CASIO;
766 if ((e->size >= 12) && !memcmp (e->data, "FUJIFILM", 8)) {
767 return EXIF_DATA_TYPE_MAKER_NOTE_FUJI;
770 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
773 #define LOG_TOO_SMALL \
774 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
775 _("Size of data too small to allow for EXIF data."));
778 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
779 unsigned int ds_orig)
784 const unsigned char *d = d_orig;
785 unsigned int ds = ds_orig, len;
787 if (!data || !data->priv || !d || !ds)
790 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
791 "Parsing %i byte(s) EXIF data...\n", ds);
794 * It can be that the data starts with the EXIF header. If it does
795 * not, search the EXIF marker.
801 if (!memcmp (d, ExifHeader, 6)) {
802 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
803 "Found EXIF header.");
806 while ((d[0] == 0xff) && ds) {
811 /* JPEG_MARKER_SOI */
812 if (d[0] == JPEG_MARKER_SOI) {
818 /* JPEG_MARKER_APP0 */
819 if (d[0] == JPEG_MARKER_APP0) {
822 l = (d[0] << 8) | d[1];
830 /* JPEG_MARKER_APP1 */
831 if (d[0] == JPEG_MARKER_APP1)
834 /* Unknown marker or data. Give up. */
835 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
836 "ExifData", _("EXIF marker not found."));
845 len = (d[0] << 8) | d[1];
846 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
847 "We have to deal with %i byte(s) of EXIF data.",
854 * Verify the exif header
855 * (offset 2, length 6).
861 if (memcmp (d, ExifHeader, 6)) {
862 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
863 "ExifData", _("EXIF header not found."));
867 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
868 "Found EXIF header.");
870 /* Byte order (offset 6, length 2) */
873 if (!memcmp (d + 6, "II", 2))
874 data->priv->order = EXIF_BYTE_ORDER_INTEL;
875 else if (!memcmp (d + 6, "MM", 2))
876 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
878 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
879 "ExifData", _("Unknown encoding."));
884 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
888 offset = exif_get_long (d + 10, data->priv->order);
889 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
890 "IFD 0 at %i.", (int) offset);
892 /* Parse the actual exif data (usually offset 14 from start) */
893 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
896 if (offset + 6 + 2 > ds) {
899 n = exif_get_short (d + 6 + offset, data->priv->order);
900 if (offset + 6 + 2 + 12 * n + 4 > ds) {
903 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
905 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
906 "IFD 1 at %i.", (int) offset);
909 if (offset > ds - 6) {
910 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
911 "ExifData", "Bogus offset of IFD1.");
913 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
918 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
919 * cameras use pointers in the maker note tag that point to the
920 * space between IFDs. Here is the only place where we have access
923 switch (exif_data_get_type_maker_note (data)) {
924 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
925 case EXIF_DATA_TYPE_MAKER_NOTE_NIKON:
926 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
928 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
929 case EXIF_DATA_TYPE_MAKER_NOTE_CASIO:
930 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
932 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
933 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
935 case EXIF_DATA_TYPE_MAKER_NOTE_FUJI:
936 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
943 * If we are able to interpret the maker note, do so.
945 if (data->priv->md) {
946 exif_mnote_data_log (data->priv->md, data->priv->log);
947 exif_mnote_data_set_byte_order (data->priv->md,
949 exif_mnote_data_set_offset (data->priv->md,
950 data->priv->offset_mnote);
951 exif_mnote_data_load (data->priv->md, d, ds);
954 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
955 exif_data_fix (data);
959 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
962 *ds = 0; /* This means something went wrong */
964 if (!data || !d || !ds)
969 *d = exif_data_alloc (data, *ds);
974 memcpy (*d, ExifHeader, 6);
976 /* Order (offset 6) */
977 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
978 memcpy (*d + 6, "II", 2);
980 memcpy (*d + 6, "MM", 2);
983 /* Fixed value (2 bytes, offset 8) */
984 exif_set_short (*d + 8, data->priv->order, 0x002a);
987 * IFD 0 offset (4 bytes, offset 10).
988 * We will start 8 bytes after the
989 * EXIF header (2 bytes for order, another 2 for the test, and
990 * 4 bytes for the IFD 0 offset make 8 bytes together).
992 exif_set_long (*d + 10, data->priv->order, 8);
994 /* Now save IFD 0. IFD 1 will be saved automatically. */
995 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
997 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
999 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1000 "Saved %i byte(s) EXIF data.", *ds);
1004 exif_data_new_from_file (const char *path)
1009 loader = exif_loader_new ();
1010 exif_loader_write_file (loader, path);
1011 edata = exif_loader_get_data (loader);
1012 exif_loader_unref (loader);
1018 exif_data_ref (ExifData *data)
1023 data->priv->ref_count++;
1027 exif_data_unref (ExifData *data)
1032 data->priv->ref_count--;
1033 if (!data->priv->ref_count)
1034 exif_data_free (data);
1038 exif_data_free (ExifData *data)
1041 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1046 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1048 exif_content_unref (data->ifd[i]);
1049 data->ifd[i] = NULL;
1054 exif_mem_free (mem, data->data);
1059 if (data->priv->log) {
1060 exif_log_unref (data->priv->log);
1061 data->priv->log = NULL;
1063 if (data->priv->md) {
1064 exif_mnote_data_unref (data->priv->md);
1065 data->priv->md = NULL;
1067 exif_mem_free (mem, data->priv);
1068 exif_mem_free (mem, data);
1071 exif_mem_unref (mem);
1075 exif_data_dump (ExifData *data)
1082 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1083 if (data->ifd[i] && data->ifd[i]->count) {
1084 printf ("Dumping IFD '%s'...\n",
1085 exif_ifd_get_name (i));
1086 exif_content_dump (data->ifd[i], 0);
1091 printf ("%i byte(s) thumbnail data available.", data->size);
1092 if (data->size >= 4) {
1093 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1094 data->data[0], data->data[1],
1095 data->data[data->size - 2],
1096 data->data[data->size - 1]);
1102 exif_data_get_byte_order (ExifData *data)
1107 return (data->priv->order);
1111 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1119 for (i = 0; i < EXIF_IFD_COUNT; i++)
1120 func (data->ifd[i], user_data);
1123 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1124 struct _ByteOrderChangeData {
1125 ExifByteOrder old, new;
1129 entry_set_byte_order (ExifEntry *e, void *data)
1131 ByteOrderChangeData *d = data;
1136 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1140 content_set_byte_order (ExifContent *content, void *data)
1142 exif_content_foreach_entry (content, entry_set_byte_order, data);
1146 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1148 ByteOrderChangeData d;
1150 if (!data || (order == data->priv->order))
1153 d.old = data->priv->order;
1155 exif_data_foreach_content (data, content_set_byte_order, &d);
1156 data->priv->order = order;
1158 exif_mnote_data_set_byte_order (data->priv->md, order);
1162 exif_data_log (ExifData *data, ExifLog *log)
1166 if (!data || !data->priv)
1168 exif_log_unref (data->priv->log);
1169 data->priv->log = log;
1172 for (i = 0; i < EXIF_IFD_COUNT; i++)
1173 exif_content_log (data->ifd[i], log);
1176 /* Used internally within libexif */
1177 ExifLog *exif_data_get_log (ExifData *);
1179 exif_data_get_log (ExifData *data)
1181 if (!data || !data->priv)
1183 return data->priv->log;
1186 static const struct {
1187 ExifDataOption option;
1189 const char *description;
1190 } exif_data_option[] = {
1191 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1192 N_("Ignore unknown tags when loading EXIF data.")},
1193 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1194 N_("Add, correct and remove entries to get EXIF data that follows "
1195 "the specification.")},
1196 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1197 N_("When loading and resaving Exif data, save the maker note unmodified."
1198 " Be aware that the maker note can get corrupted.")},
1203 exif_data_option_get_name (ExifDataOption o)
1207 for (i = 0; exif_data_option[i].name; i++)
1208 if (exif_data_option[i].option == o)
1210 return _(exif_data_option[i].name);
1214 exif_data_option_get_description (ExifDataOption o)
1218 for (i = 0; exif_data_option[i].description; i++)
1219 if (exif_data_option[i].option == o)
1221 return _(exif_data_option[i].description);
1225 exif_data_set_option (ExifData *d, ExifDataOption o)
1230 d->priv->options |= o;
1234 exif_data_unset_option (ExifData *d, ExifDataOption o)
1239 d->priv->options &= ~o;
1243 fix_func (ExifContent *c, void *UNUSED(data))
1245 switch (exif_content_get_ifd (c)) {
1247 if (c->parent->data)
1248 exif_content_fix (c);
1250 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1251 "No thumbnail but entries on thumbnail. These entries have been "
1254 unsigned int cnt = c->count;
1255 exif_content_remove_entry (c, c->entries[c->count - 1]);
1256 if (cnt == c->count) {
1258 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1259 "failed to remove last entry from entries.");
1266 exif_content_fix (c);
1271 exif_data_fix (ExifData *d)
1273 exif_data_foreach_content (d, fix_func, NULL);
1277 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1282 d->priv->data_type = dt;
1286 exif_data_get_data_type (ExifData *d)
1288 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;