3 * Copyright © 2001 Lutz Müller <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/olympus/exif-mnote-data-olympus.h>
34 #include <libexif/canon/exif-mnote-data-canon.h>
35 #include <libexif/pentax/exif-mnote-data-pentax.h>
41 #if defined(__WATCOMC__) || defined(_MSC_VER)
42 # define strncasecmp strnicmp
45 #undef JPEG_MARKER_SOI
46 #define JPEG_MARKER_SOI 0xd8
47 #undef JPEG_MARKER_APP0
48 #define JPEG_MARKER_APP0 0xe0
49 #undef JPEG_MARKER_APP1
50 #define JPEG_MARKER_APP1 0xe1
52 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
54 struct _ExifDataPrivate
63 unsigned int ref_count;
65 /* Temporarily used while loading data */
66 unsigned int offset_mnote;
68 ExifDataOption options;
69 ExifDataType data_type;
73 exif_data_alloc (ExifData *data, unsigned int i)
80 d = exif_mem_alloc (data->priv->mem, i);
84 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
89 exif_data_get_mnote_data (ExifData *d)
91 return (d && d->priv) ? d->priv->md : NULL;
97 ExifMem *mem = exif_mem_new_default ();
98 ExifData *d = exif_data_new_mem (mem);
100 exif_mem_unref (mem);
106 exif_data_new_mem (ExifMem *mem)
114 data = exif_mem_alloc (mem, sizeof (ExifData));
117 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
119 exif_mem_free (mem, data);
122 data->priv->ref_count = 1;
124 data->priv->mem = mem;
127 for (i = 0; i < EXIF_IFD_COUNT; i++) {
128 data->ifd[i] = exif_content_new_mem (data->priv->mem);
130 exif_data_free (data);
133 data->ifd[i]->parent = data;
136 /* Default options */
137 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
138 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
140 /* Default data type: none */
141 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
147 exif_data_new_from_data (const unsigned char *data, unsigned int size)
151 edata = exif_data_new ();
152 exif_data_load_data (edata, data, size);
157 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
158 const unsigned char *d,
159 unsigned int size, unsigned int offset)
161 unsigned int s, doff;
163 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
164 entry->format = exif_get_short (d + offset + 2, data->priv->order);
165 entry->components = exif_get_long (d + offset + 4, data->priv->order);
167 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
168 "Loading entry 0x%x ('%s')...", entry->tag,
169 exif_tag_get_name (entry->tag));
171 /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
172 * -> { 0x000000000 .. 0x7fffffff8 } */
173 s = exif_format_get_size(entry->format) * entry->components;
174 if (s < entry->components) {
180 * Size? If bigger than 4 bytes, the actual data is not
181 * in the entry but somewhere else (offset).
184 doff = exif_get_long (d + offset + 8, data->priv->order);
189 if ((doff + s < doff) || (doff + s < s))
194 entry->data = exif_data_alloc (data, s);
197 memcpy (entry->data, d + doff, s);
199 /* FIXME: What do our callers do if (entry->data == NULL)? */
200 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
203 /* If this is the MakerNote, remember the offset */
204 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
206 exif_log (data->priv->log,
207 EXIF_LOG_CODE_DEBUG, "ExifData",
208 "MakerNote found with NULL data");
209 } else if (entry->size > 6) {
210 exif_log (data->priv->log,
211 EXIF_LOG_CODE_DEBUG, "ExifData",
212 "MakerNote found (%02x %02x %02x %02x "
213 "%02x %02x %02x...).",
214 entry->data[0], entry->data[1], entry->data[2],
215 entry->data[3], entry->data[4], entry->data[5],
218 data->priv->offset_mnote = doff;
224 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
225 unsigned char **d, unsigned int *ds,
228 unsigned int doff, s;
230 if (!data || !data->priv)
234 * Each entry is 12 bytes long. The memory for the entry has
235 * already been allocated.
237 exif_set_short (*d + 6 + offset + 0,
238 data->priv->order, (ExifShort) e->tag);
239 exif_set_short (*d + 6 + offset + 2,
240 data->priv->order, (ExifShort) e->format);
242 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
243 /* If this is the maker note tag, update it. */
244 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
245 exif_mem_free (data->priv->mem, e->data);
248 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
249 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
250 e->components = e->size;
254 exif_set_long (*d + 6 + offset + 4,
255 data->priv->order, e->components);
258 * Size? If bigger than 4 bytes, the actual data is not in
259 * the entry but somewhere else.
261 s = exif_format_get_size (e->format) * e->components;
267 * According to the TIFF specification,
268 * the offset must be an even number. If we need to introduce
269 * a padding byte, we set it to 0.
273 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
275 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
278 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
280 *(*d + *ds - 1) = '\0';
285 /* Write the data. Fill unneeded bytes with 0. */
286 memcpy (*d + 6 + doff, e->data, s);
288 memset (*d + 6 + doff + s, 0, (4 - s));
292 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
293 unsigned int ds, ExifLong offset, ExifLong size)
295 if (ds < offset + size) {
296 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
297 "Bogus thumbnail offset and size: %i < %i + %i.",
298 (int) ds, (int) offset, (int) size);
302 exif_mem_free (data->priv->mem, data->data);
304 data->data = exif_data_alloc (data, data->size);
307 memcpy (data->data, d + offset, data->size);
311 #define CHECK_REC(i) \
313 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
314 "ExifData", "Recursive entry in IFD " \
315 "'%s' detected. Skipping...", \
316 exif_ifd_get_name (i)); \
319 if (data->ifd[(i)]->count) { \
320 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
321 "ExifData", "Attemt to load IFD " \
322 "'%s' multiple times detected. " \
324 exif_ifd_get_name (i)); \
329 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
330 const unsigned char *d,
331 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
333 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
339 if (!data || !data->priv)
341 /* check for valid ExifIfd enum range
342 * if ((((int)ifd) < 0) || (ifd >= EXIF_IFD_COUNT))
350 case EXIF_IFD_INTEROPERABILITY:
357 if (recursion_depth > 150) {
358 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
359 "Deep recursion detected!");
363 /* Read the number of entries */
364 if (offset >= ds - 1)
366 n = exif_get_short (d + offset, data->priv->order);
367 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
368 "Loading %i entries...", n);
371 /* Check if we have enough data. */
372 if (offset + 12 * n > ds)
373 n = (ds - offset) / 12;
375 for (i = 0; i < n; i++) {
377 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
379 case EXIF_TAG_EXIF_IFD_POINTER:
380 case EXIF_TAG_GPS_INFO_IFD_POINTER:
381 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
382 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
383 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
384 o = exif_get_long (d + offset + 12 * i + 8,
387 case EXIF_TAG_EXIF_IFD_POINTER:
388 CHECK_REC (EXIF_IFD_EXIF);
389 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
391 case EXIF_TAG_GPS_INFO_IFD_POINTER:
392 CHECK_REC (EXIF_IFD_GPS);
393 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
395 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
396 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
397 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
399 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
400 thumbnail_offset = o;
401 if (thumbnail_offset && thumbnail_length)
402 exif_data_load_data_thumbnail (data, d,
403 ds, thumbnail_offset,
406 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
407 thumbnail_length = o;
408 if (thumbnail_offset && thumbnail_length)
409 exif_data_load_data_thumbnail (data, d,
410 ds, thumbnail_offset,
420 * If we don't know the tag, don't fail. It could be that new
421 * versions of the standard have defined additional tags. Note that
422 * 0 is a valid tag in the GPS IFD.
424 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
427 * Special case: Tag and format 0. That's against specification.
428 * At least up to 2.2. But Photoshop writes it anyways.
430 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
431 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
432 "Skipping empty entry at position %i in '%s'.", i,
433 exif_ifd_get_name (ifd));
436 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
437 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
438 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
439 exif_ifd_get_name (ifd));
440 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
443 entry = exif_entry_new_mem (data->priv->mem);
444 if (exif_data_load_data_entry (data, entry, d, ds,
446 exif_content_add_entry (data->ifd[ifd], entry);
447 exif_entry_unref (entry);
454 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
456 ExifShort tag1 = exif_get_short (p1, o);
457 ExifShort tag2 = exif_get_short (p2, o);
459 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
463 cmp_func_intel (const void *elem1, const void *elem2)
465 return cmp_func ((const unsigned char *) elem1,
466 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
470 cmp_func_motorola (const void *elem1, const void *elem2)
472 return cmp_func ((const unsigned char *) elem1,
473 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
477 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
478 unsigned char **d, unsigned int *ds,
481 unsigned int j, n_ptr = 0, n_thumb = 0;
484 if (!data || !data->priv || !ifd || !d || !ds)
487 for (i = 0; i < EXIF_IFD_COUNT; i++)
488 if (ifd == data->ifd[i])
490 if (i == EXIF_IFD_COUNT)
494 * Check if we need some extra entries for pointers or the thumbnail.
500 * The pointer to IFD_EXIF is in IFD_0. The pointer to
501 * IFD_INTEROPERABILITY is in IFD_EXIF.
503 if (data->ifd[EXIF_IFD_EXIF]->count ||
504 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
507 /* The pointer to IFD_GPS is in IFD_0. */
508 if (data->ifd[EXIF_IFD_GPS]->count)
517 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
524 * Allocate enough memory for all entries
525 * and the number of entries.
527 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
528 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
530 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
534 /* Save the number of entries */
535 exif_set_short (*d + 6 + offset, data->priv->order,
536 (ExifShort) (ifd->count + n_ptr + n_thumb));
539 /* Save each entry */
540 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
541 "Saving %i entries (IFD '%s', offset: %i)...",
542 ifd->count, exif_ifd_get_name (i), offset);
543 for (j = 0; j < ifd->count; j++)
544 exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
546 offset += 12 * ifd->count;
548 /* Now save special entries. */
553 * The pointer to IFD_EXIF is in IFD_0.
554 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
555 * therefore, if IFD_INTEROPERABILITY is not empty, we need
556 * IFD_EXIF even if latter is empty.
558 if (data->ifd[EXIF_IFD_EXIF]->count ||
559 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
560 exif_set_short (*d + 6 + offset + 0, data->priv->order,
561 EXIF_TAG_EXIF_IFD_POINTER);
562 exif_set_short (*d + 6 + offset + 2, data->priv->order,
564 exif_set_long (*d + 6 + offset + 4, data->priv->order,
566 exif_set_long (*d + 6 + offset + 8, data->priv->order,
568 exif_data_save_data_content (data,
569 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
573 /* The pointer to IFD_GPS is in IFD_0, too. */
574 if (data->ifd[EXIF_IFD_GPS]->count) {
575 exif_set_short (*d + 6 + offset + 0, data->priv->order,
576 EXIF_TAG_GPS_INFO_IFD_POINTER);
577 exif_set_short (*d + 6 + offset + 2, data->priv->order,
579 exif_set_long (*d + 6 + offset + 4, data->priv->order,
581 exif_set_long (*d + 6 + offset + 8, data->priv->order,
583 exif_data_save_data_content (data,
584 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
592 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
595 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
596 exif_set_short (*d + 6 + offset + 0, data->priv->order,
597 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
598 exif_set_short (*d + 6 + offset + 2, data->priv->order,
600 exif_set_long (*d + 6 + offset + 4, data->priv->order,
602 exif_set_long (*d + 6 + offset + 8, data->priv->order,
604 exif_data_save_data_content (data,
605 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
614 * Information about the thumbnail (if any) is saved in
619 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
620 exif_set_short (*d + 6 + offset + 0, data->priv->order,
621 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
622 exif_set_short (*d + 6 + offset + 2, data->priv->order,
624 exif_set_long (*d + 6 + offset + 4, data->priv->order,
626 exif_set_long (*d + 6 + offset + 8, data->priv->order,
629 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
631 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
635 memcpy (*d + *ds - data->size, data->data, data->size);
638 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
639 exif_set_short (*d + 6 + offset + 0, data->priv->order,
640 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
641 exif_set_short (*d + 6 + offset + 2, data->priv->order,
643 exif_set_long (*d + 6 + offset + 4, data->priv->order,
645 exif_set_long (*d + 6 + offset + 8, data->priv->order,
655 /* Sort the directory according to TIFF specification */
656 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
657 (ifd->count + n_ptr + n_thumb), 12,
658 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
660 /* Correctly terminate the directory */
661 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
665 * We are saving IFD 0. Tell where IFD 1 starts and save
668 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
669 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
672 exif_set_long (*d + 6 + offset, data->priv->order, 0);
676 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
677 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
678 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
679 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
680 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
681 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5
682 } ExifDataTypeMakerNote;
684 static ExifDataTypeMakerNote
685 exif_data_get_type_maker_note (ExifData *d)
691 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
693 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
695 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
697 /* Olympus & Nikon */
698 if ((e->size >= 8) && (!memcmp (e->data, "OLYMP", 6) ||
699 !memcmp (e->data, "OLYMPUS", 8) || !memcmp (e->data, "Nikon", 6)))
700 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
702 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
704 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
707 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
708 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
710 /* Pentax & some variant of Nikon */
711 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
713 exif_entry_get_value (em, value, sizeof(value)),
715 return EXIF_DATA_TYPE_MAKER_NOTE_NIKON;
717 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
719 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
720 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
722 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4)) {
723 return EXIF_DATA_TYPE_MAKER_NOTE_CASIO;
726 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
729 #define LOG_TOO_SMALL \
730 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
731 _("Size of data too small to allow for EXIF data."));
734 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
735 unsigned int ds_orig)
740 const unsigned char *d = d_orig;
741 unsigned int ds = ds_orig, len;
743 if (!data || !data->priv || !d || !ds)
746 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
747 "Parsing %i byte(s) EXIF data...\n", ds);
750 * It can be that the data starts with the EXIF header. If it does
751 * not, search the EXIF marker.
757 if (!memcmp (d, ExifHeader, 6)) {
758 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
759 "Found EXIF header.");
762 while ((d[0] == 0xff) && ds) {
767 /* JPEG_MARKER_SOI */
768 if (d[0] == JPEG_MARKER_SOI) {
774 /* JPEG_MARKER_APP0 */
775 if (d[0] == JPEG_MARKER_APP0) {
778 l = (d[0] << 8) | d[1];
786 /* JPEG_MARKER_APP1 */
787 if (d[0] == JPEG_MARKER_APP1)
790 /* Unknown marker or data. Give up. */
791 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
792 "ExifData", _("EXIF marker not found."));
801 len = (d[0] << 8) | d[1];
802 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
803 "We have to deal with %i byte(s) of EXIF data.",
810 * Verify the exif header
811 * (offset 2, length 6).
817 if (memcmp (d, ExifHeader, 6)) {
818 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
819 "ExifData", _("EXIF header not found."));
823 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
824 "Found EXIF header.");
826 /* Byte order (offset 6, length 2) */
829 if (!memcmp (d + 6, "II", 2))
830 data->priv->order = EXIF_BYTE_ORDER_INTEL;
831 else if (!memcmp (d + 6, "MM", 2))
832 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
834 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
835 "ExifData", _("Unknown encoding."));
840 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
844 offset = exif_get_long (d + 10, data->priv->order);
845 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
846 "IFD 0 at %i.", (int) offset);
848 /* Parse the actual exif data (usually offset 14 from start) */
849 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
852 if (offset + 6 + 2 > ds) {
855 n = exif_get_short (d + 6 + offset, data->priv->order);
856 if (offset + 6 + 2 + 12 * n + 4 > ds) {
859 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
861 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
862 "IFD 1 at %i.", (int) offset);
865 if (offset > ds - 6) {
866 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
867 "ExifData", "Bogus offset of IFD1.");
869 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
874 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
875 * cameras use pointers in the maker note tag that point to the
876 * space between IFDs. Here is the only place where we have access
879 switch (exif_data_get_type_maker_note (data)) {
880 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
881 case EXIF_DATA_TYPE_MAKER_NOTE_NIKON:
882 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
884 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
885 case EXIF_DATA_TYPE_MAKER_NOTE_CASIO:
886 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
888 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
889 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
896 * If we are able to interpret the maker note, do so.
898 if (data->priv->md) {
899 exif_mnote_data_log (data->priv->md, data->priv->log);
900 exif_mnote_data_set_byte_order (data->priv->md,
902 exif_mnote_data_set_offset (data->priv->md,
903 data->priv->offset_mnote);
904 exif_mnote_data_load (data->priv->md, d, ds);
907 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
908 exif_data_fix (data);
912 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
914 if (!data || !d || !ds)
919 *d = exif_data_alloc (data, *ds);
922 memcpy (*d, ExifHeader, 6);
924 /* Order (offset 6) */
925 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
926 memcpy (*d + 6, "II", 2);
928 memcpy (*d + 6, "MM", 2);
931 /* Fixed value (2 bytes, offset 8) */
932 exif_set_short (*d + 8, data->priv->order, 0x002a);
935 * IFD 0 offset (4 bytes, offset 10).
936 * We will start 8 bytes after the
937 * EXIF header (2 bytes for order, another 2 for the test, and
938 * 4 bytes for the IFD 0 offset make 8 bytes together).
940 exif_set_long (*d + 10, data->priv->order, 8);
942 /* Now save IFD 0. IFD 1 will be saved automatically. */
943 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
945 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
947 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
948 "Saved %i byte(s) EXIF data.", *ds);
952 exif_data_new_from_file (const char *path)
957 loader = exif_loader_new ();
958 exif_loader_write_file (loader, path);
959 edata = exif_loader_get_data (loader);
960 exif_loader_unref (loader);
966 exif_data_ref (ExifData *data)
971 data->priv->ref_count++;
975 exif_data_unref (ExifData *data)
980 data->priv->ref_count--;
981 if (!data->priv->ref_count)
982 exif_data_free (data);
986 exif_data_free (ExifData *data)
989 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
994 for (i = 0; i < EXIF_IFD_COUNT; i++) {
996 exif_content_unref (data->ifd[i]);
1002 exif_mem_free (mem, data->data);
1007 if (data->priv->log) {
1008 exif_log_unref (data->priv->log);
1009 data->priv->log = NULL;
1011 if (data->priv->md) {
1012 exif_mnote_data_unref (data->priv->md);
1013 data->priv->md = NULL;
1015 exif_mem_free (mem, data->priv);
1016 exif_mem_free (mem, data);
1019 exif_mem_unref (mem);
1023 exif_data_dump (ExifData *data)
1030 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1031 if (data->ifd[i] && data->ifd[i]->count) {
1032 printf ("Dumping IFD '%s'...\n",
1033 exif_ifd_get_name (i));
1034 exif_content_dump (data->ifd[i], 0);
1039 printf ("%i byte(s) thumbnail data available.", data->size);
1040 if (data->size >= 4) {
1041 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1042 data->data[0], data->data[1],
1043 data->data[data->size - 2],
1044 data->data[data->size - 1]);
1050 exif_data_get_byte_order (ExifData *data)
1055 return (data->priv->order);
1059 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1067 for (i = 0; i < EXIF_IFD_COUNT; i++)
1068 func (data->ifd[i], user_data);
1071 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1072 struct _ByteOrderChangeData {
1073 ExifByteOrder old, new;
1077 entry_set_byte_order (ExifEntry *e, void *data)
1079 ByteOrderChangeData *d = data;
1084 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1088 content_set_byte_order (ExifContent *content, void *data)
1090 exif_content_foreach_entry (content, entry_set_byte_order, data);
1094 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1096 ByteOrderChangeData d;
1098 if (!data || (order == data->priv->order))
1101 d.old = data->priv->order;
1103 exif_data_foreach_content (data, content_set_byte_order, &d);
1104 data->priv->order = order;
1106 exif_mnote_data_set_byte_order (data->priv->md, order);
1110 exif_data_log (ExifData *data, ExifLog *log)
1114 if (!data || !data->priv)
1116 exif_log_unref (data->priv->log);
1117 data->priv->log = log;
1120 for (i = 0; i < EXIF_IFD_COUNT; i++)
1121 exif_content_log (data->ifd[i], log);
1124 /* Used internally within libexif */
1125 ExifLog *exif_data_get_log (ExifData *);
1127 exif_data_get_log (ExifData *data)
1129 if (!data || !data->priv)
1131 return data->priv->log;
1135 ExifDataOption option;
1137 const char *description;
1138 } exif_data_option[] = {
1139 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1140 N_("Ignore unknown tags when loading EXIF data.")},
1141 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1142 N_("Add, correct and remove entries to get EXIF data that follows "
1143 "the specification.")},
1144 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1145 N_("When loading and resaving Exif data, save the maker note unmodified."
1146 " Be aware that the maker note can get corrupted.")},
1151 exif_data_option_get_name (ExifDataOption o)
1155 for (i = 0; exif_data_option[i].name; i++)
1156 if (exif_data_option[i].option == o)
1158 return _(exif_data_option[i].name);
1162 exif_data_option_get_description (ExifDataOption o)
1166 for (i = 0; exif_data_option[i].description; i++)
1167 if (exif_data_option[i].option == o)
1169 return _(exif_data_option[i].description);
1173 exif_data_set_option (ExifData *d, ExifDataOption o)
1178 d->priv->options |= o;
1182 exif_data_unset_option (ExifData *d, ExifDataOption o)
1187 d->priv->options &= ~o;
1191 fix_func (ExifContent *c, void *UNUSED(data))
1193 switch (exif_content_get_ifd (c)) {
1195 if (c->parent->data)
1196 exif_content_fix (c);
1198 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1199 "No thumbnail but entries on thumbnail. These entries have been "
1202 exif_content_remove_entry (c, c->entries[c->count - 1]);
1206 exif_content_fix (c);
1211 exif_data_fix (ExifData *d)
1213 exif_data_foreach_content (d, fix_func, NULL);
1217 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1222 d->priv->data_type = dt;
1226 exif_data_get_data_type (ExifData *d)
1228 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;