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));
170 /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
171 * -> { 0x000000000 .. 0x7fffffff8 } */
172 s = exif_format_get_size(entry->format) * entry->components;
173 if (s < entry->components) {
179 * Size? If bigger than 4 bytes, the actual data is not
180 * in the entry but somewhere else (offset).
183 doff = exif_get_long (d + offset + 8, data->priv->order);
188 if ((doff + s < doff) || (doff + s < s))
193 entry->data = exif_data_alloc (data, s);
196 memcpy (entry->data, d + doff, s);
199 /* If this is the MakerNote, remember the offset */
200 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
202 exif_log (data->priv->log,
203 EXIF_LOG_CODE_DEBUG, "ExifData",
204 "MakerNote found with NULL data");
206 else if (entry->size > 6)
207 exif_log (data->priv->log,
208 EXIF_LOG_CODE_DEBUG, "ExifData",
209 "MakerNote found (%02x %02x %02x %02x "
210 "%02x %02x %02x...).",
211 entry->data[0], entry->data[1], entry->data[2],
212 entry->data[3], entry->data[4], entry->data[5],
214 data->priv->offset_mnote = doff;
220 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
221 unsigned char **d, unsigned int *ds,
224 unsigned int doff, s;
226 if (!data || !data->priv)
230 * Each entry is 12 bytes long. The memory for the entry has
231 * already been allocated.
233 exif_set_short (*d + 6 + offset + 0,
234 data->priv->order, (ExifShort) e->tag);
235 exif_set_short (*d + 6 + offset + 2,
236 data->priv->order, (ExifShort) e->format);
238 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
239 /* If this is the maker note tag, update it. */
240 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
241 exif_mem_free (data->priv->mem, e->data);
244 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
245 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
246 e->components = e->size;
250 exif_set_long (*d + 6 + offset + 4,
251 data->priv->order, e->components);
254 * Size? If bigger than 4 bytes, the actual data is not in
255 * the entry but somewhere else.
257 s = exif_format_get_size (e->format) * e->components;
263 * According to the TIFF specification,
264 * the offset must be an even number. If we need to introduce
265 * a padding byte, we set it to 0.
269 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
271 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
274 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
276 *(*d + *ds - 1) = '\0';
281 /* Write the data. Fill unneeded bytes with 0. */
282 memcpy (*d + 6 + doff, e->data, s);
284 memset (*d + 6 + doff + s, 0, (4 - s));
288 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
289 unsigned int ds, ExifLong offset, ExifLong size)
291 if (ds < offset + size) {
292 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
293 "Bogus thumbnail offset and size: %i < %i + %i.",
294 (int) ds, (int) offset, (int) size);
298 exif_mem_free (data->priv->mem, data->data);
300 data->data = exif_data_alloc (data, data->size);
303 memcpy (data->data, d + offset, data->size);
307 #define CHECK_REC(i) \
309 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
310 "ExifData", "Recursive entry in IFD " \
311 "'%s' detected. Skipping...", \
312 exif_ifd_get_name (i)); \
315 if (data->ifd[(i)]->count) { \
316 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
317 "ExifData", "Attemt to load IFD " \
318 "'%s' multiple times detected. " \
320 exif_ifd_get_name (i)); \
325 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
326 const unsigned char *d,
327 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
329 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
335 if (!data || !data->priv)
337 if ((ifd < 0) || (ifd >= EXIF_IFD_COUNT))
340 if (recursion_depth > 150) {
341 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
342 "Deep recursion detected!");
346 /* Read the number of entries */
347 if (offset >= ds - 1)
349 n = exif_get_short (d + offset, data->priv->order);
350 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
351 "Loading %i entries...", n);
354 /* Check if we have enough data. */
355 if (offset + 12 * n > ds)
356 n = (ds - offset) / 12;
358 for (i = 0; i < n; i++) {
360 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
362 case EXIF_TAG_EXIF_IFD_POINTER:
363 case EXIF_TAG_GPS_INFO_IFD_POINTER:
364 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
365 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
366 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
367 o = exif_get_long (d + offset + 12 * i + 8,
370 case EXIF_TAG_EXIF_IFD_POINTER:
371 CHECK_REC (EXIF_IFD_EXIF);
372 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
374 case EXIF_TAG_GPS_INFO_IFD_POINTER:
375 CHECK_REC (EXIF_IFD_GPS);
376 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
378 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
379 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
380 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
382 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
383 thumbnail_offset = o;
384 if (thumbnail_offset && thumbnail_length)
385 exif_data_load_data_thumbnail (data, d,
386 ds, thumbnail_offset,
389 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
390 thumbnail_length = o;
391 if (thumbnail_offset && thumbnail_length)
392 exif_data_load_data_thumbnail (data, d,
393 ds, thumbnail_offset,
403 * If we don't know the tag, don't fail. It could be that new
404 * versions of the standard have defined additional tags. Note that
405 * 0 is a valid tag in the GPS IFD.
407 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
410 * Special case: Tag and format 0. That's against specification.
411 * At least up to 2.2. But Photoshop writes it anyways.
413 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
414 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
415 "Skipping empty entry at position %i in '%s'.", i,
416 exif_ifd_get_name (ifd));
419 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
420 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
421 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
422 exif_ifd_get_name (ifd));
423 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
426 entry = exif_entry_new_mem (data->priv->mem);
427 if (exif_data_load_data_entry (data, entry, d, ds,
429 exif_content_add_entry (data->ifd[ifd], entry);
430 exif_entry_unref (entry);
437 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
439 ExifShort tag1 = exif_get_short (p1, o);
440 ExifShort tag2 = exif_get_short (p2, o);
442 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
446 cmp_func_intel (const void *elem1, const void *elem2)
448 return cmp_func ((const unsigned char *) elem1,
449 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
453 cmp_func_motorola (const void *elem1, const void *elem2)
455 return cmp_func ((const unsigned char *) elem1,
456 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
460 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
461 unsigned char **d, unsigned int *ds,
464 unsigned int j, n_ptr = 0, n_thumb = 0;
467 if (!data || !data->priv || !ifd || !d || !ds)
470 for (i = 0; i < EXIF_IFD_COUNT; i++)
471 if (ifd == data->ifd[i])
473 if (i == EXIF_IFD_COUNT)
477 * Check if we need some extra entries for pointers or the thumbnail.
483 * The pointer to IFD_EXIF is in IFD_0. The pointer to
484 * IFD_INTEROPERABILITY is in IFD_EXIF.
486 if (data->ifd[EXIF_IFD_EXIF]->count ||
487 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
490 /* The pointer to IFD_GPS is in IFD_0. */
491 if (data->ifd[EXIF_IFD_GPS]->count)
500 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
507 * Allocate enough memory for all entries
508 * and the number of entries.
510 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
511 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
513 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
517 /* Save the number of entries */
518 exif_set_short (*d + 6 + offset, data->priv->order,
519 (ExifShort) (ifd->count + n_ptr + n_thumb));
522 /* Save each entry */
523 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
524 "Saving %i entries (IFD '%s', offset: %i)...",
525 ifd->count, exif_ifd_get_name (i), offset);
526 for (j = 0; j < ifd->count; j++)
527 exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
529 offset += 12 * ifd->count;
531 /* Now save special entries. */
536 * The pointer to IFD_EXIF is in IFD_0.
537 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
538 * therefore, if IFD_INTEROPERABILITY is not empty, we need
539 * IFD_EXIF even if latter is empty.
541 if (data->ifd[EXIF_IFD_EXIF]->count ||
542 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
543 exif_set_short (*d + 6 + offset + 0, data->priv->order,
544 EXIF_TAG_EXIF_IFD_POINTER);
545 exif_set_short (*d + 6 + offset + 2, data->priv->order,
547 exif_set_long (*d + 6 + offset + 4, data->priv->order,
549 exif_set_long (*d + 6 + offset + 8, data->priv->order,
551 exif_data_save_data_content (data,
552 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
556 /* The pointer to IFD_GPS is in IFD_0, too. */
557 if (data->ifd[EXIF_IFD_GPS]->count) {
558 exif_set_short (*d + 6 + offset + 0, data->priv->order,
559 EXIF_TAG_GPS_INFO_IFD_POINTER);
560 exif_set_short (*d + 6 + offset + 2, data->priv->order,
562 exif_set_long (*d + 6 + offset + 4, data->priv->order,
564 exif_set_long (*d + 6 + offset + 8, data->priv->order,
566 exif_data_save_data_content (data,
567 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
575 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
578 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
579 exif_set_short (*d + 6 + offset + 0, data->priv->order,
580 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
581 exif_set_short (*d + 6 + offset + 2, data->priv->order,
583 exif_set_long (*d + 6 + offset + 4, data->priv->order,
585 exif_set_long (*d + 6 + offset + 8, data->priv->order,
587 exif_data_save_data_content (data,
588 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
597 * Information about the thumbnail (if any) is saved in
602 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
603 exif_set_short (*d + 6 + offset + 0, data->priv->order,
604 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
605 exif_set_short (*d + 6 + offset + 2, data->priv->order,
607 exif_set_long (*d + 6 + offset + 4, data->priv->order,
609 exif_set_long (*d + 6 + offset + 8, data->priv->order,
612 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
614 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
618 memcpy (*d + *ds - data->size, data->data, data->size);
621 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
622 exif_set_short (*d + 6 + offset + 0, data->priv->order,
623 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
624 exif_set_short (*d + 6 + offset + 2, data->priv->order,
626 exif_set_long (*d + 6 + offset + 4, data->priv->order,
628 exif_set_long (*d + 6 + offset + 8, data->priv->order,
638 /* Sort the directory according to TIFF specification */
639 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
640 (ifd->count + n_ptr + n_thumb), 12,
641 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
643 /* Correctly terminate the directory */
644 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
648 * We are saving IFD 0. Tell where IFD 1 starts and save
651 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
652 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
655 exif_set_long (*d + 6 + offset, data->priv->order, 0);
659 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
660 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
661 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
662 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
663 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
664 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5
665 } ExifDataTypeMakerNote;
667 static ExifDataTypeMakerNote
668 exif_data_get_type_maker_note (ExifData *d)
674 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
676 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
678 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
680 /* Olympus & Nikon */
681 if ((e->size >= 8) && (!memcmp (e->data, "OLYMP", 6) ||
682 !memcmp (e->data, "OLYMPUS", 8) || !memcmp (e->data, "Nikon", 6)))
683 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
685 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
687 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
690 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
691 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
693 /* Pentax & some variant of Nikon */
694 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
696 exif_entry_get_value (em, value, sizeof(value)),
698 return EXIF_DATA_TYPE_MAKER_NOTE_NIKON;
700 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
702 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
703 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
705 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4)) {
706 return EXIF_DATA_TYPE_MAKER_NOTE_CASIO;
709 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
712 #define LOG_TOO_SMALL \
713 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
714 _("Size of data too small to allow for EXIF data."));
717 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
718 unsigned int ds_orig)
723 const unsigned char *d = d_orig;
724 unsigned int ds = ds_orig, len;
726 if (!data || !data->priv || !d || !ds)
729 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
730 "Parsing %i byte(s) EXIF data...\n", ds);
733 * It can be that the data starts with the EXIF header. If it does
734 * not, search the EXIF marker.
740 if (!memcmp (d, ExifHeader, 6)) {
741 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
742 "Found EXIF header.");
745 while ((d[0] == 0xff) && ds) {
750 /* JPEG_MARKER_SOI */
751 if (d[0] == JPEG_MARKER_SOI) {
757 /* JPEG_MARKER_APP0 */
758 if (d[0] == JPEG_MARKER_APP0) {
761 l = (d[0] << 8) | d[1];
769 /* JPEG_MARKER_APP1 */
770 if (d[0] == JPEG_MARKER_APP1)
773 /* Unknown marker or data. Give up. */
774 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
775 "ExifData", _("EXIF marker not found."));
784 len = (d[0] << 8) | d[1];
785 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
786 "We have to deal with %i byte(s) of EXIF data.",
793 * Verify the exif header
794 * (offset 2, length 6).
800 if (memcmp (d, ExifHeader, 6)) {
801 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
802 "ExifData", _("EXIF header not found."));
806 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
807 "Found EXIF header.");
809 /* Byte order (offset 6, length 2) */
812 if (!memcmp (d + 6, "II", 2))
813 data->priv->order = EXIF_BYTE_ORDER_INTEL;
814 else if (!memcmp (d + 6, "MM", 2))
815 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
817 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
818 "ExifData", _("Unknown encoding."));
823 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
827 offset = exif_get_long (d + 10, data->priv->order);
828 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
829 "IFD 0 at %i.", (int) offset);
831 /* Parse the actual exif data (usually offset 14 from start) */
832 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
835 if (offset + 6 + 2 > ds) {
838 n = exif_get_short (d + 6 + offset, data->priv->order);
839 if (offset + 6 + 2 + 12 * n + 4 > ds) {
842 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
844 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
845 "IFD 1 at %i.", (int) offset);
848 if (offset > ds - 6) {
849 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
850 "ExifData", "Bogus offset of IFD1.");
852 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
857 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
858 * cameras use pointers in the maker note tag that point to the
859 * space between IFDs. Here is the only place where we have access
862 switch (exif_data_get_type_maker_note (data)) {
863 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
864 case EXIF_DATA_TYPE_MAKER_NOTE_NIKON:
865 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
867 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
868 case EXIF_DATA_TYPE_MAKER_NOTE_CASIO:
869 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
871 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
872 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
879 * If we are able to interpret the maker note, do so.
881 if (data->priv->md) {
882 exif_mnote_data_log (data->priv->md, data->priv->log);
883 exif_mnote_data_set_byte_order (data->priv->md,
885 exif_mnote_data_set_offset (data->priv->md,
886 data->priv->offset_mnote);
887 exif_mnote_data_load (data->priv->md, d, ds);
890 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
891 exif_data_fix (data);
895 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
897 if (!data || !d || !ds)
902 *d = exif_data_alloc (data, *ds);
905 memcpy (*d, ExifHeader, 6);
907 /* Order (offset 6) */
908 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
909 memcpy (*d + 6, "II", 2);
911 memcpy (*d + 6, "MM", 2);
914 /* Fixed value (2 bytes, offset 8) */
915 exif_set_short (*d + 8, data->priv->order, 0x002a);
918 * IFD 0 offset (4 bytes, offset 10).
919 * We will start 8 bytes after the
920 * EXIF header (2 bytes for order, another 2 for the test, and
921 * 4 bytes for the IFD 0 offset make 8 bytes together).
923 exif_set_long (*d + 10, data->priv->order, 8);
925 /* Now save IFD 0. IFD 1 will be saved automatically. */
926 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
928 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
930 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
931 "Saved %i byte(s) EXIF data.", *ds);
935 exif_data_new_from_file (const char *path)
940 loader = exif_loader_new ();
941 exif_loader_write_file (loader, path);
942 edata = exif_loader_get_data (loader);
943 exif_loader_unref (loader);
949 exif_data_ref (ExifData *data)
954 data->priv->ref_count++;
958 exif_data_unref (ExifData *data)
963 data->priv->ref_count--;
964 if (!data->priv->ref_count)
965 exif_data_free (data);
969 exif_data_free (ExifData *data)
972 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
977 for (i = 0; i < EXIF_IFD_COUNT; i++) {
979 exif_content_unref (data->ifd[i]);
985 exif_mem_free (mem, data->data);
990 if (data->priv->log) {
991 exif_log_unref (data->priv->log);
992 data->priv->log = NULL;
994 if (data->priv->md) {
995 exif_mnote_data_unref (data->priv->md);
996 data->priv->md = NULL;
998 exif_mem_free (mem, data->priv);
999 exif_mem_free (mem, data);
1002 exif_mem_unref (mem);
1006 exif_data_dump (ExifData *data)
1013 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1014 if (data->ifd[i] && data->ifd[i]->count) {
1015 printf ("Dumping IFD '%s'...\n",
1016 exif_ifd_get_name (i));
1017 exif_content_dump (data->ifd[i], 0);
1022 printf ("%i byte(s) thumbnail data available.", data->size);
1023 if (data->size >= 4) {
1024 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1025 data->data[0], data->data[1],
1026 data->data[data->size - 2],
1027 data->data[data->size - 1]);
1033 exif_data_get_byte_order (ExifData *data)
1038 return (data->priv->order);
1042 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1050 for (i = 0; i < EXIF_IFD_COUNT; i++)
1051 func (data->ifd[i], user_data);
1054 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1055 struct _ByteOrderChangeData {
1056 ExifByteOrder old, new;
1060 entry_set_byte_order (ExifEntry *e, void *data)
1062 ByteOrderChangeData *d = data;
1067 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1071 content_set_byte_order (ExifContent *content, void *data)
1073 exif_content_foreach_entry (content, entry_set_byte_order, data);
1077 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1079 ByteOrderChangeData d;
1081 if (!data || (order == data->priv->order))
1084 d.old = data->priv->order;
1086 exif_data_foreach_content (data, content_set_byte_order, &d);
1087 data->priv->order = order;
1089 exif_mnote_data_set_byte_order (data->priv->md, order);
1093 exif_data_log (ExifData *data, ExifLog *log)
1097 if (!data || !data->priv)
1099 exif_log_unref (data->priv->log);
1100 data->priv->log = log;
1103 for (i = 0; i < EXIF_IFD_COUNT; i++)
1104 exif_content_log (data->ifd[i], log);
1107 /* Used internally within libexif */
1108 ExifLog *exif_data_get_log (ExifData *);
1110 exif_data_get_log (ExifData *data)
1112 if (!data || !data->priv)
1114 return data->priv->log;
1118 ExifDataOption option;
1120 const char *description;
1121 } exif_data_option[] = {
1122 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1123 N_("Ignore unknown tags when loading EXIF data.")},
1124 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1125 N_("Add, correct and remove entries to get EXIF data that follows "
1126 "the specification.")},
1127 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Don not change maker note"),
1128 N_("When loading and resaving Exif data, save the maker note unmodified."
1129 " Be aware that the maker note can get corrupted.")},
1134 exif_data_option_get_name (ExifDataOption o)
1138 for (i = 0; exif_data_option[i].name; i++)
1139 if (exif_data_option[i].option == o)
1141 return _(exif_data_option[i].name);
1145 exif_data_option_get_description (ExifDataOption o)
1149 for (i = 0; exif_data_option[i].description; i++)
1150 if (exif_data_option[i].option == o)
1152 return _(exif_data_option[i].description);
1156 exif_data_set_option (ExifData *d, ExifDataOption o)
1161 d->priv->options |= o;
1165 exif_data_unset_option (ExifData *d, ExifDataOption o)
1170 d->priv->options &= ~o;
1174 fix_func (ExifContent *c, void *data)
1176 switch (exif_content_get_ifd (c)) {
1178 if (c->parent->data)
1179 exif_content_fix (c);
1181 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1182 "No thumbnail but entries on thumbnail. These entries have been "
1185 exif_content_remove_entry (c, c->entries[c->count - 1]);
1189 exif_content_fix (c);
1194 exif_data_fix (ExifData *d)
1196 exif_data_foreach_content (d, fix_func, NULL);
1200 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1205 d->priv->data_type = dt;
1209 exif_data_get_data_type (ExifData *d)
1211 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;