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>
32 #include <libexif/olympus/exif-mnote-data-olympus.h>
33 #include <libexif/canon/exif-mnote-data-canon.h>
34 #include <libexif/pentax/exif-mnote-data-pentax.h>
40 #if defined(__WATCOMC__) || defined(_MSC_VER)
41 # define strncasecmp strnicmp
44 #undef JPEG_MARKER_SOI
45 #define JPEG_MARKER_SOI 0xd8
46 #undef JPEG_MARKER_APP0
47 #define JPEG_MARKER_APP0 0xe0
48 #undef JPEG_MARKER_APP1
49 #define JPEG_MARKER_APP1 0xe1
51 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
53 struct _ExifDataPrivate
62 unsigned int ref_count;
64 /* Temporarily used while loading data */
65 unsigned int offset_mnote;
67 ExifDataOption options;
68 ExifDataType data_type;
72 exif_data_alloc (ExifData *data, unsigned int i)
79 d = exif_mem_alloc (data->priv->mem, i);
83 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
88 exif_data_get_mnote_data (ExifData *d)
90 return (d && d->priv) ? d->priv->md : NULL;
96 ExifMem *mem = exif_mem_new_default ();
97 ExifData *d = exif_data_new_mem (mem);
105 exif_data_new_mem (ExifMem *mem)
113 data = exif_mem_alloc (mem, sizeof (ExifData));
116 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
118 exif_mem_free (mem, data);
121 data->priv->ref_count = 1;
123 data->priv->mem = mem;
126 for (i = 0; i < EXIF_IFD_COUNT; i++) {
127 data->ifd[i] = exif_content_new_mem (data->priv->mem);
129 exif_data_free (data);
132 data->ifd[i]->parent = data;
135 /* Default options */
136 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
137 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
139 /* Default data type: none */
140 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
146 exif_data_new_from_data (const unsigned char *data, unsigned int size)
150 edata = exif_data_new ();
151 exif_data_load_data (edata, data, size);
156 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
157 const unsigned char *d,
158 unsigned int size, unsigned int offset)
160 unsigned int s, doff;
162 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
163 entry->format = exif_get_short (d + offset + 2, data->priv->order);
164 entry->components = exif_get_long (d + offset + 4, data->priv->order);
166 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
167 "Loading entry 0x%x ('%s')...", entry->tag,
168 exif_tag_get_name (entry->tag));
171 * Size? If bigger than 4 bytes, the actual data is not
172 * in the entry but somewhere else (offset).
174 s = exif_format_get_size (entry->format) * entry->components;
178 doff = exif_get_long (d + offset + 8, data->priv->order);
183 if ((doff + s < doff) || (doff + s < s))
188 entry->data = exif_data_alloc (data, s);
191 memcpy (entry->data, d + doff, s);
194 /* If this is the MakerNote, remember the offset */
195 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
197 exif_log (data->priv->log,
198 EXIF_LOG_CODE_DEBUG, "ExifData",
199 "MakerNote found with NULL data");
201 else if (entry->size > 6)
202 exif_log (data->priv->log,
203 EXIF_LOG_CODE_DEBUG, "ExifData",
204 "MakerNote found (%02x %02x %02x %02x "
205 "%02x %02x %02x...).",
206 entry->data[0], entry->data[1], entry->data[2],
207 entry->data[3], entry->data[4], entry->data[5],
209 data->priv->offset_mnote = doff;
215 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
216 unsigned char **d, unsigned int *ds,
219 unsigned int doff, s;
221 if (!data || !data->priv)
225 * Each entry is 12 bytes long. The memory for the entry has
226 * already been allocated.
228 exif_set_short (*d + 6 + offset + 0,
229 data->priv->order, (ExifShort) e->tag);
230 exif_set_short (*d + 6 + offset + 2,
231 data->priv->order, (ExifShort) e->format);
233 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
234 /* If this is the maker note tag, update it. */
235 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
236 exif_mem_free (data->priv->mem, e->data);
239 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
240 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
241 e->components = e->size;
245 exif_set_long (*d + 6 + offset + 4,
246 data->priv->order, e->components);
249 * Size? If bigger than 4 bytes, the actual data is not in
250 * the entry but somewhere else.
252 s = exif_format_get_size (e->format) * e->components;
258 * According to the TIFF specification,
259 * the offset must be an even number. If we need to introduce
260 * a padding byte, we set it to 0.
264 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
266 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
269 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
271 *(*d + *ds - 1) = '\0';
276 /* Write the data. Fill unneeded bytes with 0. */
277 memcpy (*d + 6 + doff, e->data, s);
279 memset (*d + 6 + doff + s, 0, (4 - s));
283 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
284 unsigned int ds, ExifLong offset, ExifLong size)
286 if (ds < offset + size) {
287 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
288 "Bogus thumbnail offset and size: %i < %i + %i.",
289 (int) ds, (int) offset, (int) size);
293 exif_mem_free (data->priv->mem, data->data);
295 data->data = exif_data_alloc (data, data->size);
298 memcpy (data->data, d + offset, data->size);
302 #define CHECK_REC(i) \
304 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
305 "ExifData", "Recursive entry in IFD " \
306 "'%s' detected. Skipping...", \
307 exif_ifd_get_name (i)); \
310 if (data->ifd[(i)]->count) { \
311 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
312 "ExifData", "Attemt to load IFD " \
313 "'%s' multiple times detected. " \
315 exif_ifd_get_name (i)); \
320 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
321 const unsigned char *d,
322 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
324 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
330 if (!data || !data->priv)
332 if ((ifd < 0) || (ifd >= EXIF_IFD_COUNT))
335 if (recursion_depth > 150) {
336 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
337 "Deep recursion detected!");
341 /* Read the number of entries */
342 if (offset >= ds - 1)
344 n = exif_get_short (d + offset, data->priv->order);
345 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
346 "Loading %i entries...", n);
349 /* Check if we have enough data. */
350 if (offset + 12 * n > ds)
351 n = (ds - offset) / 12;
353 for (i = 0; i < n; i++) {
355 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
357 case EXIF_TAG_EXIF_IFD_POINTER:
358 case EXIF_TAG_GPS_INFO_IFD_POINTER:
359 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
360 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
361 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
362 o = exif_get_long (d + offset + 12 * i + 8,
365 case EXIF_TAG_EXIF_IFD_POINTER:
366 CHECK_REC (EXIF_IFD_EXIF);
367 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
369 case EXIF_TAG_GPS_INFO_IFD_POINTER:
370 CHECK_REC (EXIF_IFD_GPS);
371 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
373 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
374 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
375 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
377 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
378 thumbnail_offset = o;
379 if (thumbnail_offset && thumbnail_length)
380 exif_data_load_data_thumbnail (data, d,
381 ds, thumbnail_offset,
384 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
385 thumbnail_length = o;
386 if (thumbnail_offset && thumbnail_length)
387 exif_data_load_data_thumbnail (data, d,
388 ds, thumbnail_offset,
398 * If we don't know the tag, don't fail. It could be that new
399 * versions of the standard have defined additional tags. Note that
400 * 0 is a valid tag in the GPS IFD.
402 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
405 * Special case: Tag and format 0. That's against specification.
406 * At least up to 2.2. But Photoshop writes it anyways.
408 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
409 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
410 "Skipping empty entry at position %i in '%s'.", i,
411 exif_ifd_get_name (ifd));
414 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
415 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
416 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
417 exif_ifd_get_name (ifd));
418 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
421 entry = exif_entry_new_mem (data->priv->mem);
422 if (exif_data_load_data_entry (data, entry, d, ds,
424 exif_content_add_entry (data->ifd[ifd], entry);
425 exif_entry_unref (entry);
432 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
434 ExifShort tag1 = exif_get_short (p1, o);
435 ExifShort tag2 = exif_get_short (p2, o);
437 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
441 cmp_func_intel (const void *elem1, const void *elem2)
443 return cmp_func ((const unsigned char *) elem1,
444 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
448 cmp_func_motorola (const void *elem1, const void *elem2)
450 return cmp_func ((const unsigned char *) elem1,
451 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
455 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
456 unsigned char **d, unsigned int *ds,
459 unsigned int j, n_ptr = 0, n_thumb = 0;
462 if (!data || !data->priv || !ifd || !d || !ds)
465 for (i = 0; i < EXIF_IFD_COUNT; i++)
466 if (ifd == data->ifd[i])
468 if (i == EXIF_IFD_COUNT)
472 * Check if we need some extra entries for pointers or the thumbnail.
478 * The pointer to IFD_EXIF is in IFD_0. The pointer to
479 * IFD_INTEROPERABILITY is in IFD_EXIF.
481 if (data->ifd[EXIF_IFD_EXIF]->count ||
482 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
485 /* The pointer to IFD_GPS is in IFD_0. */
486 if (data->ifd[EXIF_IFD_GPS]->count)
495 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
502 * Allocate enough memory for all entries
503 * and the number of entries.
505 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
506 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
508 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
512 /* Save the number of entries */
513 exif_set_short (*d + 6 + offset, data->priv->order,
514 (ExifShort) (ifd->count + n_ptr + n_thumb));
517 /* Save each entry */
518 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
519 "Saving %i entries (IFD '%s', offset: %i)...",
520 ifd->count, exif_ifd_get_name (i), offset);
521 for (j = 0; j < ifd->count; j++)
522 exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
524 offset += 12 * ifd->count;
526 /* Now save special entries. */
531 * The pointer to IFD_EXIF is in IFD_0.
532 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
533 * therefore, if IFD_INTEROPERABILITY is not empty, we need
534 * IFD_EXIF even if latter is empty.
536 if (data->ifd[EXIF_IFD_EXIF]->count ||
537 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
538 exif_set_short (*d + 6 + offset + 0, data->priv->order,
539 EXIF_TAG_EXIF_IFD_POINTER);
540 exif_set_short (*d + 6 + offset + 2, data->priv->order,
542 exif_set_long (*d + 6 + offset + 4, data->priv->order,
544 exif_set_long (*d + 6 + offset + 8, data->priv->order,
546 exif_data_save_data_content (data,
547 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
551 /* The pointer to IFD_GPS is in IFD_0, too. */
552 if (data->ifd[EXIF_IFD_GPS]->count) {
553 exif_set_short (*d + 6 + offset + 0, data->priv->order,
554 EXIF_TAG_GPS_INFO_IFD_POINTER);
555 exif_set_short (*d + 6 + offset + 2, data->priv->order,
557 exif_set_long (*d + 6 + offset + 4, data->priv->order,
559 exif_set_long (*d + 6 + offset + 8, data->priv->order,
561 exif_data_save_data_content (data,
562 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
570 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
573 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
574 exif_set_short (*d + 6 + offset + 0, data->priv->order,
575 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
576 exif_set_short (*d + 6 + offset + 2, data->priv->order,
578 exif_set_long (*d + 6 + offset + 4, data->priv->order,
580 exif_set_long (*d + 6 + offset + 8, data->priv->order,
582 exif_data_save_data_content (data,
583 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
592 * Information about the thumbnail (if any) is saved in
597 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
598 exif_set_short (*d + 6 + offset + 0, data->priv->order,
599 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
600 exif_set_short (*d + 6 + offset + 2, data->priv->order,
602 exif_set_long (*d + 6 + offset + 4, data->priv->order,
604 exif_set_long (*d + 6 + offset + 8, data->priv->order,
607 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
609 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
613 memcpy (*d + *ds - data->size, data->data, data->size);
616 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
617 exif_set_short (*d + 6 + offset + 0, data->priv->order,
618 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
619 exif_set_short (*d + 6 + offset + 2, data->priv->order,
621 exif_set_long (*d + 6 + offset + 4, data->priv->order,
623 exif_set_long (*d + 6 + offset + 8, data->priv->order,
633 /* Sort the directory according to TIFF specification */
634 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
635 (ifd->count + n_ptr + n_thumb), 12,
636 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
638 /* Correctly terminate the directory */
639 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
643 * We are saving IFD 0. Tell where IFD 1 starts and save
646 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
647 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
650 exif_set_long (*d + 6 + offset, data->priv->order, 0);
654 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
655 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
656 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
657 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
658 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
659 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5
660 } ExifDataTypeMakerNote;
662 static ExifDataTypeMakerNote
663 exif_data_get_type_maker_note (ExifData *d)
669 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
671 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
673 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
675 /* Olympus & Nikon */
676 if ((e->size >= 8) && (!memcmp (e->data, "OLYMP", 6) ||
677 !memcmp (e->data, "OLYMPUS", 8) || !memcmp (e->data, "Nikon", 6)))
678 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
680 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
682 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
685 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
686 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
688 /* Pentax & some variant of Nikon */
689 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
691 exif_entry_get_value (em, value, sizeof(value)),
693 return EXIF_DATA_TYPE_MAKER_NOTE_NIKON;
695 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
697 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
698 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
700 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4)) {
701 return EXIF_DATA_TYPE_MAKER_NOTE_CASIO;
704 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
707 #define LOG_TOO_SMALL \
708 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
709 _("Size of data too small to allow for EXIF data."));
712 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
713 unsigned int ds_orig)
718 const unsigned char *d = d_orig;
719 unsigned int ds = ds_orig, len;
721 if (!data || !data->priv || !d || !ds)
724 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
725 "Parsing %i byte(s) EXIF data...\n", ds);
728 * It can be that the data starts with the EXIF header. If it does
729 * not, search the EXIF marker.
735 if (!memcmp (d, ExifHeader, 6)) {
736 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
737 "Found EXIF header.");
740 while ((d[0] == 0xff) && ds) {
745 /* JPEG_MARKER_SOI */
746 if (d[0] == JPEG_MARKER_SOI) {
752 /* JPEG_MARKER_APP0 */
753 if (d[0] == JPEG_MARKER_APP0) {
756 l = (d[0] << 8) | d[1];
764 /* JPEG_MARKER_APP1 */
765 if (d[0] == JPEG_MARKER_APP1)
768 /* Unknown marker or data. Give up. */
769 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
770 "ExifData", _("EXIF marker not found."));
779 len = (d[0] << 8) | d[1];
780 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
781 "We have to deal with %i byte(s) of EXIF data.",
788 * Verify the exif header
789 * (offset 2, length 6).
795 if (memcmp (d, ExifHeader, 6)) {
796 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
797 "ExifData", _("EXIF header not found."));
801 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
802 "Found EXIF header.");
804 /* Byte order (offset 6, length 2) */
807 if (!memcmp (d + 6, "II", 2))
808 data->priv->order = EXIF_BYTE_ORDER_INTEL;
809 else if (!memcmp (d + 6, "MM", 2))
810 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
812 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
813 "ExifData", _("Unknown encoding."));
818 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
822 offset = exif_get_long (d + 10, data->priv->order);
823 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
824 "IFD 0 at %i.", (int) offset);
826 /* Parse the actual exif data (usually offset 14 from start) */
827 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
830 if (offset + 6 + 2 > ds) {
833 n = exif_get_short (d + 6 + offset, data->priv->order);
834 if (offset + 6 + 2 + 12 * n + 4 > ds) {
837 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
839 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
840 "IFD 1 at %i.", (int) offset);
843 if (offset > ds - 6) {
844 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
845 "ExifData", "Bogus offset of IFD1.");
847 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
852 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
853 * cameras use pointers in the maker note tag that point to the
854 * space between IFDs. Here is the only place where we have access
857 switch (exif_data_get_type_maker_note (data)) {
858 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
859 case EXIF_DATA_TYPE_MAKER_NOTE_NIKON:
860 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
862 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
863 case EXIF_DATA_TYPE_MAKER_NOTE_CASIO:
864 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
866 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
867 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
874 * If we are able to interpret the maker note, do so.
876 if (data->priv->md) {
877 exif_mnote_data_log (data->priv->md, data->priv->log);
878 exif_mnote_data_set_byte_order (data->priv->md,
880 exif_mnote_data_set_offset (data->priv->md,
881 data->priv->offset_mnote);
882 exif_mnote_data_load (data->priv->md, d, ds);
885 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
886 exif_data_fix (data);
890 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
892 if (!data || !d || !ds)
897 *d = exif_data_alloc (data, *ds);
900 memcpy (*d, ExifHeader, 6);
902 /* Order (offset 6) */
903 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
904 memcpy (*d + 6, "II", 2);
906 memcpy (*d + 6, "MM", 2);
909 /* Fixed value (2 bytes, offset 8) */
910 exif_set_short (*d + 8, data->priv->order, 0x002a);
913 * IFD 0 offset (4 bytes, offset 10).
914 * We will start 8 bytes after the
915 * EXIF header (2 bytes for order, another 2 for the test, and
916 * 4 bytes for the IFD 0 offset make 8 bytes together).
918 exif_set_long (*d + 10, data->priv->order, 8);
920 /* Now save IFD 0. IFD 1 will be saved automatically. */
921 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
923 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
925 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
926 "Saved %i byte(s) EXIF data.", *ds);
930 exif_data_new_from_file (const char *path)
935 loader = exif_loader_new ();
936 exif_loader_write_file (loader, path);
937 edata = exif_loader_get_data (loader);
938 exif_loader_unref (loader);
944 exif_data_ref (ExifData *data)
949 data->priv->ref_count++;
953 exif_data_unref (ExifData *data)
958 data->priv->ref_count--;
959 if (!data->priv->ref_count)
960 exif_data_free (data);
964 exif_data_free (ExifData *data)
967 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
972 for (i = 0; i < EXIF_IFD_COUNT; i++) {
974 exif_content_unref (data->ifd[i]);
980 exif_mem_free (mem, data->data);
985 if (data->priv->log) {
986 exif_log_unref (data->priv->log);
987 data->priv->log = NULL;
989 if (data->priv->md) {
990 exif_mnote_data_unref (data->priv->md);
991 data->priv->md = NULL;
993 exif_mem_free (mem, data->priv);
994 exif_mem_free (mem, data);
997 exif_mem_unref (mem);
1001 exif_data_dump (ExifData *data)
1008 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1009 if (data->ifd[i] && data->ifd[i]->count) {
1010 printf ("Dumping IFD '%s'...\n",
1011 exif_ifd_get_name (i));
1012 exif_content_dump (data->ifd[i], 0);
1017 printf ("%i byte(s) thumbnail data available.", data->size);
1018 if (data->size >= 4) {
1019 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1020 data->data[0], data->data[1],
1021 data->data[data->size - 2],
1022 data->data[data->size - 1]);
1028 exif_data_get_byte_order (ExifData *data)
1033 return (data->priv->order);
1037 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1045 for (i = 0; i < EXIF_IFD_COUNT; i++)
1046 func (data->ifd[i], user_data);
1049 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1050 struct _ByteOrderChangeData {
1051 ExifByteOrder old, new;
1055 entry_set_byte_order (ExifEntry *e, void *data)
1057 ByteOrderChangeData *d = data;
1062 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1066 content_set_byte_order (ExifContent *content, void *data)
1068 exif_content_foreach_entry (content, entry_set_byte_order, data);
1072 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1074 ByteOrderChangeData d;
1076 if (!data || (order == data->priv->order))
1079 d.old = data->priv->order;
1081 exif_data_foreach_content (data, content_set_byte_order, &d);
1082 data->priv->order = order;
1084 exif_mnote_data_set_byte_order (data->priv->md, order);
1088 exif_data_log (ExifData *data, ExifLog *log)
1092 if (!data || !data->priv)
1094 exif_log_unref (data->priv->log);
1095 data->priv->log = log;
1098 for (i = 0; i < EXIF_IFD_COUNT; i++)
1099 exif_content_log (data->ifd[i], log);
1102 /* Used internally within libexif */
1103 ExifLog *exif_data_get_log (ExifData *);
1105 exif_data_get_log (ExifData *data)
1107 if (!data || !data->priv)
1109 return data->priv->log;
1113 ExifDataOption option;
1115 const char *description;
1116 } exif_data_option[] = {
1117 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1118 N_("Ignore unknown tags when loading EXIF data.")},
1119 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1120 N_("Add, correct and remove entries to get EXIF data that follows "
1121 "the specification.")},
1122 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Don not change maker note"),
1123 N_("When loading and resaving Exif data, save the maker note unmodified."
1124 " Be aware that the maker note can get corrupted.")},
1129 exif_data_option_get_name (ExifDataOption o)
1133 for (i = 0; exif_data_option[i].name; i++)
1134 if (exif_data_option[i].option == o)
1136 return _(exif_data_option[i].name);
1140 exif_data_option_get_description (ExifDataOption o)
1144 for (i = 0; exif_data_option[i].description; i++)
1145 if (exif_data_option[i].option == o)
1147 return _(exif_data_option[i].description);
1151 exif_data_set_option (ExifData *d, ExifDataOption o)
1156 d->priv->options |= o;
1160 exif_data_unset_option (ExifData *d, ExifDataOption o)
1165 d->priv->options &= ~o;
1169 fix_func (ExifContent *c, void *data)
1171 switch (exif_content_get_ifd (c)) {
1173 if (c->parent->data)
1174 exif_content_fix (c);
1176 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1177 "No thumbnail but entries on thumbnail. These entries have been "
1180 exif_content_remove_entry (c, c->entries[c->count - 1]);
1184 exif_content_fix (c);
1189 exif_data_fix (ExifData *d)
1191 exif_data_foreach_content (d, fix_func, NULL);
1195 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1200 d->priv->data_type = dt;
1204 exif_data_get_data_type (ExifData *d)
1206 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;