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 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) {
187 * Size? If bigger than 4 bytes, the actual data is not
188 * in the entry but somewhere else (offset).
191 doff = exif_get_long (d + offset + 8, data->priv->order);
196 if ((doff + s < doff) || (doff + s < s))
201 entry->data = exif_data_alloc (data, s);
204 memcpy (entry->data, d + doff, s);
206 /* FIXME: What do our callers do if (entry->data == NULL)? */
207 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
210 /* If this is the MakerNote, remember the offset */
211 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
213 exif_log (data->priv->log,
214 EXIF_LOG_CODE_DEBUG, "ExifData",
215 "MakerNote found with NULL data");
216 } else if (entry->size > 6) {
217 exif_log (data->priv->log,
218 EXIF_LOG_CODE_DEBUG, "ExifData",
219 "MakerNote found (%02x %02x %02x %02x "
220 "%02x %02x %02x...).",
221 entry->data[0], entry->data[1], entry->data[2],
222 entry->data[3], entry->data[4], entry->data[5],
225 data->priv->offset_mnote = doff;
231 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
232 unsigned char **d, unsigned int *ds,
235 unsigned int doff, s;
239 if (!data || !data->priv)
243 * Each entry is 12 bytes long. The memory for the entry has
244 * already been allocated.
246 exif_set_short (*d + 6 + offset + 0,
247 data->priv->order, (ExifShort) e->tag);
248 exif_set_short (*d + 6 + offset + 2,
249 data->priv->order, (ExifShort) e->format);
251 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
252 /* If this is the maker note tag, update it. */
253 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
254 exif_mem_free (data->priv->mem, e->data);
257 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
258 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
259 e->components = e->size;
263 exif_set_long (*d + 6 + offset + 4,
264 data->priv->order, e->components);
267 * Size? If bigger than 4 bytes, the actual data is not in
268 * the entry but somewhere else.
270 s = exif_format_get_size (e->format) * e->components;
276 * According to the TIFF specification,
277 * the offset must be an even number. If we need to introduce
278 * a padding byte, we set it to 0.
282 t = exif_mem_realloc (data->priv->mem, *d, ts);
284 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
289 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
291 *(*d + *ds - 1) = '\0';
296 /* Write the data. Fill unneeded bytes with 0. Do not crash with
299 memcpy (*d + 6 + doff, e->data, s);
301 memset (*d + 6 + doff, 0, s);
304 memset (*d + 6 + doff + s, 0, (4 - s));
308 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
309 unsigned int ds, ExifLong offset, ExifLong size)
311 if ((ds < offset + size) || (offset > ds)) {
312 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
313 "Bogus thumbnail offset (%u) or size (%u).",
318 exif_mem_free (data->priv->mem, data->data);
320 data->data = exif_data_alloc (data, data->size);
323 memcpy (data->data, d + offset, data->size);
327 #define CHECK_REC(i) \
329 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
330 "ExifData", "Recursive entry in IFD " \
331 "'%s' detected. Skipping...", \
332 exif_ifd_get_name (i)); \
335 if (data->ifd[(i)]->count) { \
336 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
337 "ExifData", "Attempt to load IFD " \
338 "'%s' multiple times detected. " \
340 exif_ifd_get_name (i)); \
345 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
346 const unsigned char *d,
347 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
349 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
355 if (!data || !data->priv)
358 /* check for valid ExifIfd enum range */
359 if (( ((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
362 if (recursion_depth > 150) {
363 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
364 "Deep recursion detected!");
368 /* Read the number of entries */
369 if (offset >= ds - 1)
371 n = exif_get_short (d + offset, data->priv->order);
372 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
373 "Loading %i entries...", n);
376 /* Check if we have enough data. */
377 if (offset + 12 * n > ds)
378 n = (ds - offset) / 12;
380 for (i = 0; i < n; i++) {
382 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
384 case EXIF_TAG_EXIF_IFD_POINTER:
385 case EXIF_TAG_GPS_INFO_IFD_POINTER:
386 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
387 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
388 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
389 o = exif_get_long (d + offset + 12 * i + 8,
392 case EXIF_TAG_EXIF_IFD_POINTER:
393 CHECK_REC (EXIF_IFD_EXIF);
394 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
396 case EXIF_TAG_GPS_INFO_IFD_POINTER:
397 CHECK_REC (EXIF_IFD_GPS);
398 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
400 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
401 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
402 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
404 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
405 thumbnail_offset = o;
406 if (thumbnail_offset && thumbnail_length)
407 exif_data_load_data_thumbnail (data, d,
408 ds, thumbnail_offset,
411 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
412 thumbnail_length = o;
413 if (thumbnail_offset && thumbnail_length)
414 exif_data_load_data_thumbnail (data, d,
415 ds, thumbnail_offset,
425 * If we don't know the tag, don't fail. It could be that new
426 * versions of the standard have defined additional tags. Note that
427 * 0 is a valid tag in the GPS IFD.
429 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
432 * Special case: Tag and format 0. That's against specification.
433 * At least up to 2.2. But Photoshop writes it anyways.
435 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
436 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
437 "Skipping empty entry at position %i in '%s'.", i,
438 exif_ifd_get_name (ifd));
441 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
442 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
443 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
444 exif_ifd_get_name (ifd));
445 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
448 entry = exif_entry_new_mem (data->priv->mem);
449 if (exif_data_load_data_entry (data, entry, d, ds,
451 exif_content_add_entry (data->ifd[ifd], entry);
452 exif_entry_unref (entry);
459 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
461 ExifShort tag1 = exif_get_short (p1, o);
462 ExifShort tag2 = exif_get_short (p2, o);
464 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
468 cmp_func_intel (const void *elem1, const void *elem2)
470 return cmp_func ((const unsigned char *) elem1,
471 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
475 cmp_func_motorola (const void *elem1, const void *elem2)
477 return cmp_func ((const unsigned char *) elem1,
478 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
482 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
483 unsigned char **d, unsigned int *ds,
486 unsigned int j, n_ptr = 0, n_thumb = 0;
491 if (!data || !data->priv || !ifd || !d || !ds)
494 for (i = 0; i < EXIF_IFD_COUNT; i++)
495 if (ifd == data->ifd[i])
497 if (i == EXIF_IFD_COUNT)
501 * Check if we need some extra entries for pointers or the thumbnail.
507 * The pointer to IFD_EXIF is in IFD_0. The pointer to
508 * IFD_INTEROPERABILITY is in IFD_EXIF.
510 if (data->ifd[EXIF_IFD_EXIF]->count ||
511 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
514 /* The pointer to IFD_GPS is in IFD_0. */
515 if (data->ifd[EXIF_IFD_GPS]->count)
524 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
531 * Allocate enough memory for all entries
532 * and the number of entries.
534 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
535 t = exif_mem_realloc (data->priv->mem, *d, ts);
537 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
543 /* Save the number of entries */
544 exif_set_short (*d + 6 + offset, data->priv->order,
545 (ExifShort) (ifd->count + n_ptr + n_thumb));
549 * Save each entry. Make sure that no memcpys from NULL pointers are
552 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
553 "Saving %i entries (IFD '%s', offset: %i)...",
554 ifd->count, exif_ifd_get_name (i), offset);
555 for (j = 0; j < ifd->count; j++) {
556 if (ifd->entries[j]) {
557 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
562 offset += 12 * ifd->count;
564 /* Now save special entries. */
569 * The pointer to IFD_EXIF is in IFD_0.
570 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
571 * therefore, if IFD_INTEROPERABILITY is not empty, we need
572 * IFD_EXIF even if latter is empty.
574 if (data->ifd[EXIF_IFD_EXIF]->count ||
575 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
576 exif_set_short (*d + 6 + offset + 0, data->priv->order,
577 EXIF_TAG_EXIF_IFD_POINTER);
578 exif_set_short (*d + 6 + offset + 2, data->priv->order,
580 exif_set_long (*d + 6 + offset + 4, data->priv->order,
582 exif_set_long (*d + 6 + offset + 8, data->priv->order,
584 exif_data_save_data_content (data,
585 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
589 /* The pointer to IFD_GPS is in IFD_0, too. */
590 if (data->ifd[EXIF_IFD_GPS]->count) {
591 exif_set_short (*d + 6 + offset + 0, data->priv->order,
592 EXIF_TAG_GPS_INFO_IFD_POINTER);
593 exif_set_short (*d + 6 + offset + 2, data->priv->order,
595 exif_set_long (*d + 6 + offset + 4, data->priv->order,
597 exif_set_long (*d + 6 + offset + 8, data->priv->order,
599 exif_data_save_data_content (data,
600 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
608 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
611 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
612 exif_set_short (*d + 6 + offset + 0, data->priv->order,
613 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
614 exif_set_short (*d + 6 + offset + 2, data->priv->order,
616 exif_set_long (*d + 6 + offset + 4, data->priv->order,
618 exif_set_long (*d + 6 + offset + 8, data->priv->order,
620 exif_data_save_data_content (data,
621 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
630 * Information about the thumbnail (if any) is saved in
635 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
636 exif_set_short (*d + 6 + offset + 0, data->priv->order,
637 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
638 exif_set_short (*d + 6 + offset + 2, data->priv->order,
640 exif_set_long (*d + 6 + offset + 4, data->priv->order,
642 exif_set_long (*d + 6 + offset + 8, data->priv->order,
644 ts = *ds + data->size;
645 t = exif_mem_realloc (data->priv->mem, *d, ts);
647 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
653 memcpy (*d + *ds - data->size, data->data, data->size);
656 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
657 exif_set_short (*d + 6 + offset + 0, data->priv->order,
658 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
659 exif_set_short (*d + 6 + offset + 2, data->priv->order,
661 exif_set_long (*d + 6 + offset + 4, data->priv->order,
663 exif_set_long (*d + 6 + offset + 8, data->priv->order,
673 /* Sort the directory according to TIFF specification */
674 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
675 (ifd->count + n_ptr + n_thumb), 12,
676 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
678 /* Correctly terminate the directory */
679 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
683 * We are saving IFD 0. Tell where IFD 1 starts and save
686 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
687 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
690 exif_set_long (*d + 6 + offset, data->priv->order, 0);
694 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
695 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
696 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
697 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
698 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
699 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
700 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6
701 } ExifDataTypeMakerNote;
703 static ExifDataTypeMakerNote
704 exif_data_get_type_maker_note (ExifData *d)
710 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
712 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
714 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
716 /* Olympus & Nikon & Sanyo */
717 if ((e->size >= 8) && ( !memcmp (e->data, "OLYMP", 6) ||
718 !memcmp (e->data, "OLYMPUS", 8) ||
719 !memcmp (e->data, "SANYO", 6) ||
720 !memcmp (e->data, "Nikon", 6)))
721 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
723 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
725 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
728 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
729 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
731 /* Pentax & some variant of Nikon */
732 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
734 exif_entry_get_value (em, value, sizeof(value)),
736 return EXIF_DATA_TYPE_MAKER_NOTE_NIKON;
738 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
740 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
741 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
743 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4)) {
744 return EXIF_DATA_TYPE_MAKER_NOTE_CASIO;
746 if ((e->size >= 12) && !memcmp (e->data, "FUJIFILM", 8)) {
747 return EXIF_DATA_TYPE_MAKER_NOTE_FUJI;
750 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
753 #define LOG_TOO_SMALL \
754 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
755 _("Size of data too small to allow for EXIF data."));
758 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
759 unsigned int ds_orig)
764 const unsigned char *d = d_orig;
765 unsigned int ds = ds_orig, len;
767 if (!data || !data->priv || !d || !ds)
770 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
771 "Parsing %i byte(s) EXIF data...\n", ds);
774 * It can be that the data starts with the EXIF header. If it does
775 * not, search the EXIF marker.
781 if (!memcmp (d, ExifHeader, 6)) {
782 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
783 "Found EXIF header.");
786 while ((d[0] == 0xff) && ds) {
791 /* JPEG_MARKER_SOI */
792 if (d[0] == JPEG_MARKER_SOI) {
798 /* JPEG_MARKER_APP0 */
799 if (d[0] == JPEG_MARKER_APP0) {
802 l = (d[0] << 8) | d[1];
810 /* JPEG_MARKER_APP1 */
811 if (d[0] == JPEG_MARKER_APP1)
814 /* Unknown marker or data. Give up. */
815 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
816 "ExifData", _("EXIF marker not found."));
825 len = (d[0] << 8) | d[1];
826 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
827 "We have to deal with %i byte(s) of EXIF data.",
834 * Verify the exif header
835 * (offset 2, length 6).
841 if (memcmp (d, ExifHeader, 6)) {
842 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
843 "ExifData", _("EXIF header not found."));
847 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
848 "Found EXIF header.");
850 /* Byte order (offset 6, length 2) */
853 if (!memcmp (d + 6, "II", 2))
854 data->priv->order = EXIF_BYTE_ORDER_INTEL;
855 else if (!memcmp (d + 6, "MM", 2))
856 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
858 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
859 "ExifData", _("Unknown encoding."));
864 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
868 offset = exif_get_long (d + 10, data->priv->order);
869 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
870 "IFD 0 at %i.", (int) offset);
872 /* Parse the actual exif data (usually offset 14 from start) */
873 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
876 if (offset + 6 + 2 > ds) {
879 n = exif_get_short (d + 6 + offset, data->priv->order);
880 if (offset + 6 + 2 + 12 * n + 4 > ds) {
883 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
885 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
886 "IFD 1 at %i.", (int) offset);
889 if (offset > ds - 6) {
890 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
891 "ExifData", "Bogus offset of IFD1.");
893 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
898 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
899 * cameras use pointers in the maker note tag that point to the
900 * space between IFDs. Here is the only place where we have access
903 switch (exif_data_get_type_maker_note (data)) {
904 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
905 case EXIF_DATA_TYPE_MAKER_NOTE_NIKON:
906 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
908 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
909 case EXIF_DATA_TYPE_MAKER_NOTE_CASIO:
910 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
912 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
913 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
915 case EXIF_DATA_TYPE_MAKER_NOTE_FUJI:
916 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
923 * If we are able to interpret the maker note, do so.
925 if (data->priv->md) {
926 exif_mnote_data_log (data->priv->md, data->priv->log);
927 exif_mnote_data_set_byte_order (data->priv->md,
929 exif_mnote_data_set_offset (data->priv->md,
930 data->priv->offset_mnote);
931 exif_mnote_data_load (data->priv->md, d, ds);
934 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
935 exif_data_fix (data);
939 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
942 *ds = 0; /* This means something went wrong */
944 if (!data || !d || !ds)
949 *d = exif_data_alloc (data, *ds);
954 memcpy (*d, ExifHeader, 6);
956 /* Order (offset 6) */
957 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
958 memcpy (*d + 6, "II", 2);
960 memcpy (*d + 6, "MM", 2);
963 /* Fixed value (2 bytes, offset 8) */
964 exif_set_short (*d + 8, data->priv->order, 0x002a);
967 * IFD 0 offset (4 bytes, offset 10).
968 * We will start 8 bytes after the
969 * EXIF header (2 bytes for order, another 2 for the test, and
970 * 4 bytes for the IFD 0 offset make 8 bytes together).
972 exif_set_long (*d + 10, data->priv->order, 8);
974 /* Now save IFD 0. IFD 1 will be saved automatically. */
975 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
977 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
979 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
980 "Saved %i byte(s) EXIF data.", *ds);
984 exif_data_new_from_file (const char *path)
989 loader = exif_loader_new ();
990 exif_loader_write_file (loader, path);
991 edata = exif_loader_get_data (loader);
992 exif_loader_unref (loader);
998 exif_data_ref (ExifData *data)
1003 data->priv->ref_count++;
1007 exif_data_unref (ExifData *data)
1012 data->priv->ref_count--;
1013 if (!data->priv->ref_count)
1014 exif_data_free (data);
1018 exif_data_free (ExifData *data)
1021 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1026 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1028 exif_content_unref (data->ifd[i]);
1029 data->ifd[i] = NULL;
1034 exif_mem_free (mem, data->data);
1039 if (data->priv->log) {
1040 exif_log_unref (data->priv->log);
1041 data->priv->log = NULL;
1043 if (data->priv->md) {
1044 exif_mnote_data_unref (data->priv->md);
1045 data->priv->md = NULL;
1047 exif_mem_free (mem, data->priv);
1048 exif_mem_free (mem, data);
1051 exif_mem_unref (mem);
1055 exif_data_dump (ExifData *data)
1062 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1063 if (data->ifd[i] && data->ifd[i]->count) {
1064 printf ("Dumping IFD '%s'...\n",
1065 exif_ifd_get_name (i));
1066 exif_content_dump (data->ifd[i], 0);
1071 printf ("%i byte(s) thumbnail data available.", data->size);
1072 if (data->size >= 4) {
1073 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1074 data->data[0], data->data[1],
1075 data->data[data->size - 2],
1076 data->data[data->size - 1]);
1082 exif_data_get_byte_order (ExifData *data)
1087 return (data->priv->order);
1091 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1099 for (i = 0; i < EXIF_IFD_COUNT; i++)
1100 func (data->ifd[i], user_data);
1103 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1104 struct _ByteOrderChangeData {
1105 ExifByteOrder old, new;
1109 entry_set_byte_order (ExifEntry *e, void *data)
1111 ByteOrderChangeData *d = data;
1116 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1120 content_set_byte_order (ExifContent *content, void *data)
1122 exif_content_foreach_entry (content, entry_set_byte_order, data);
1126 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1128 ByteOrderChangeData d;
1130 if (!data || (order == data->priv->order))
1133 d.old = data->priv->order;
1135 exif_data_foreach_content (data, content_set_byte_order, &d);
1136 data->priv->order = order;
1138 exif_mnote_data_set_byte_order (data->priv->md, order);
1142 exif_data_log (ExifData *data, ExifLog *log)
1146 if (!data || !data->priv)
1148 exif_log_unref (data->priv->log);
1149 data->priv->log = log;
1152 for (i = 0; i < EXIF_IFD_COUNT; i++)
1153 exif_content_log (data->ifd[i], log);
1156 /* Used internally within libexif */
1157 ExifLog *exif_data_get_log (ExifData *);
1159 exif_data_get_log (ExifData *data)
1161 if (!data || !data->priv)
1163 return data->priv->log;
1166 static const struct {
1167 ExifDataOption option;
1169 const char *description;
1170 } exif_data_option[] = {
1171 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1172 N_("Ignore unknown tags when loading EXIF data.")},
1173 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1174 N_("Add, correct and remove entries to get EXIF data that follows "
1175 "the specification.")},
1176 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1177 N_("When loading and resaving Exif data, save the maker note unmodified."
1178 " Be aware that the maker note can get corrupted.")},
1183 exif_data_option_get_name (ExifDataOption o)
1187 for (i = 0; exif_data_option[i].name; i++)
1188 if (exif_data_option[i].option == o)
1190 return _(exif_data_option[i].name);
1194 exif_data_option_get_description (ExifDataOption o)
1198 for (i = 0; exif_data_option[i].description; i++)
1199 if (exif_data_option[i].option == o)
1201 return _(exif_data_option[i].description);
1205 exif_data_set_option (ExifData *d, ExifDataOption o)
1210 d->priv->options |= o;
1214 exif_data_unset_option (ExifData *d, ExifDataOption o)
1219 d->priv->options &= ~o;
1223 fix_func (ExifContent *c, void *UNUSED(data))
1225 switch (exif_content_get_ifd (c)) {
1227 if (c->parent->data)
1228 exif_content_fix (c);
1230 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1231 "No thumbnail but entries on thumbnail. These entries have been "
1234 unsigned int cnt = c->count;
1235 exif_content_remove_entry (c, c->entries[c->count - 1]);
1236 if (cnt == c->count) {
1238 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1239 "failed to remove last entry from entries.");
1246 exif_content_fix (c);
1251 exif_data_fix (ExifData *d)
1253 exif_data_foreach_content (d, fix_func, NULL);
1257 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1262 d->priv->data_type = dt;
1266 exif_data_get_data_type (ExifData *d)
1268 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;