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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 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 #undef JPEG_MARKER_SOI
43 #define JPEG_MARKER_SOI 0xd8
44 #undef JPEG_MARKER_APP0
45 #define JPEG_MARKER_APP0 0xe0
46 #undef JPEG_MARKER_APP1
47 #define JPEG_MARKER_APP1 0xe1
49 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
51 struct _ExifDataPrivate
60 unsigned int ref_count;
62 /* Temporarily used while loading data */
63 unsigned int offset_mnote;
65 ExifDataOption options;
66 ExifDataType data_type;
70 exif_data_alloc (ExifData *data, unsigned int i)
77 d = exif_mem_alloc (data->priv->mem, i);
81 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
86 exif_data_get_mnote_data (ExifData *d)
88 return (d && d->priv) ? d->priv->md : NULL;
94 ExifMem *mem = exif_mem_new_default ();
95 ExifData *d = exif_data_new_mem (mem);
103 exif_data_new_mem (ExifMem *mem)
111 data = exif_mem_alloc (mem, sizeof (ExifData));
114 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
116 exif_mem_free (mem, data);
119 data->priv->ref_count = 1;
121 data->priv->mem = mem;
124 for (i = 0; i < EXIF_IFD_COUNT; i++) {
125 data->ifd[i] = exif_content_new_mem (data->priv->mem);
127 exif_data_free (data);
130 data->ifd[i]->parent = data;
133 /* Default options */
134 #ifndef NO_VERBOSE_TAG_STRINGS
136 * When the tag list is compiled away, setting this option prevents
137 * any tags from being loaded
139 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
141 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
143 /* Default data type: none */
144 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
150 exif_data_new_from_data (const unsigned char *data, unsigned int size)
154 edata = exif_data_new ();
155 exif_data_load_data (edata, data, size);
160 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
161 const unsigned char *d,
162 unsigned int size, unsigned int offset)
164 unsigned int s, doff;
166 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
167 entry->format = exif_get_short (d + offset + 2, data->priv->order);
168 entry->components = exif_get_long (d + offset + 4, data->priv->order);
170 /* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent
171 * has not been set yet
173 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
174 "Loading entry 0x%x ('%s')...", entry->tag,
175 exif_tag_get_name (entry->tag));
177 /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
178 * -> { 0x000000000 .. 0x7fffffff8 } */
179 s = exif_format_get_size(entry->format) * entry->components;
180 if ((s < entry->components) || (s == 0)){
185 * Size? If bigger than 4 bytes, the actual data is not
186 * in the entry but somewhere else (offset).
189 doff = exif_get_long (d + offset + 8, data->priv->order);
194 if ((doff + s < doff) || (doff + s < s) || (doff + s > size)) {
195 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
196 "Tag data past end of buffer (%u > %u)", doff+s, size);
200 entry->data = exif_data_alloc (data, s);
203 memcpy (entry->data, d + doff, s);
205 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
209 /* If this is the MakerNote, remember the offset */
210 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
212 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
213 "MakerNote found with empty data");
214 } else if (entry->size > 6) {
215 exif_log (data->priv->log,
216 EXIF_LOG_CODE_DEBUG, "ExifData",
217 "MakerNote found (%02x %02x %02x %02x "
218 "%02x %02x %02x...).",
219 entry->data[0], entry->data[1], entry->data[2],
220 entry->data[3], entry->data[4], entry->data[5],
223 data->priv->offset_mnote = doff;
229 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
230 unsigned char **d, unsigned int *ds,
233 unsigned int doff, s;
236 if (!data || !data->priv)
240 * Each entry is 12 bytes long. The memory for the entry has
241 * already been allocated.
243 exif_set_short (*d + 6 + offset + 0,
244 data->priv->order, (ExifShort) e->tag);
245 exif_set_short (*d + 6 + offset + 2,
246 data->priv->order, (ExifShort) e->format);
248 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
249 /* If this is the maker note tag, update it. */
250 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
251 /* TODO: this is using the wrong ExifMem to free e->data */
252 exif_mem_free (data->priv->mem, e->data);
255 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
256 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
257 e->components = e->size;
258 if (exif_format_get_size (e->format) != 1) {
259 /* e->format is taken from input code,
260 * but we need to make sure it is a 1 byte
261 * entity due to the multiplication below. */
262 e->format = EXIF_FORMAT_UNDEFINED;
267 exif_set_long (*d + 6 + offset + 4,
268 data->priv->order, e->components);
271 * Size? If bigger than 4 bytes, the actual data is not in
272 * the entry but somewhere else.
274 s = exif_format_get_size (e->format) * e->components;
281 * According to the TIFF specification,
282 * the offset must be an even number. If we need to introduce
283 * a padding byte, we set it to 0.
287 t = exif_mem_realloc (data->priv->mem, *d, ts);
289 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
294 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
296 *(*d + *ds - 1) = '\0';
301 /* Write the data. Fill unneeded bytes with 0. Do not crash with
304 memcpy (*d + 6 + doff, e->data, s);
306 memset (*d + 6 + doff, 0, s);
309 memset (*d + 6 + doff + s, 0, (4 - s));
313 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
314 unsigned int ds, ExifLong o, ExifLong s)
317 if ((o + s < o) || (o + s < s) || (o + s > ds) || (o > ds)) {
318 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
319 "Bogus thumbnail offset (%u) or size (%u).",
325 exif_mem_free (data->priv->mem, data->data);
326 if (!(data->data = exif_data_alloc (data, s))) {
327 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
332 memcpy (data->data, d + o, s);
336 #define CHECK_REC(i) \
338 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
339 "ExifData", "Recursive entry in IFD " \
340 "'%s' detected. Skipping...", \
341 exif_ifd_get_name (i)); \
344 if (data->ifd[(i)]->count) { \
345 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
346 "ExifData", "Attempt to load IFD " \
347 "'%s' multiple times detected. " \
349 exif_ifd_get_name (i)); \
353 /*! Load data for an IFD.
355 * \param[in,out] data #ExifData
356 * \param[in] ifd IFD to load
357 * \param[in] d pointer to buffer containing raw IFD data
358 * \param[in] ds size of raw data in buffer at \c d
359 * \param[in] offset offset into buffer at \c d at which IFD starts
360 * \param[in] recursion_depth number of times this function has been
361 * recursively called without returning
364 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
365 const unsigned char *d,
366 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
368 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
374 if (!data || !data->priv)
377 /* check for valid ExifIfd enum range */
378 if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
381 if (recursion_depth > 12) {
382 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
383 "Deep recursion detected!");
387 /* Read the number of entries */
388 if ((offset + 2 < offset) || (offset + 2 < 2) || (offset + 2 > ds)) {
389 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
390 "Tag data past end of buffer (%u > %u)", offset+2, ds);
393 n = exif_get_short (d + offset, data->priv->order);
394 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
395 "Loading %hu entries...", n);
398 /* Check if we have enough data. */
399 if (offset + 12 * n > ds) {
400 n = (ds - offset) / 12;
401 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
402 "Short data; only loading %hu entries...", n);
405 for (i = 0; i < n; i++) {
407 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
409 case EXIF_TAG_EXIF_IFD_POINTER:
410 case EXIF_TAG_GPS_INFO_IFD_POINTER:
411 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
412 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
413 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
414 o = exif_get_long (d + offset + 12 * i + 8,
416 /* FIXME: IFD_POINTER tags aren't marked as being in a
417 * specific IFD, so exif_tag_get_name_in_ifd won't work
419 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
420 "Sub-IFD entry 0x%x ('%s') at %u.", tag,
421 exif_tag_get_name(tag), o);
423 case EXIF_TAG_EXIF_IFD_POINTER:
424 CHECK_REC (EXIF_IFD_EXIF);
425 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
427 case EXIF_TAG_GPS_INFO_IFD_POINTER:
428 CHECK_REC (EXIF_IFD_GPS);
429 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
431 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
432 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
433 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
435 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
436 thumbnail_offset = o;
437 if (thumbnail_offset && thumbnail_length)
438 exif_data_load_data_thumbnail (data, d,
439 ds, thumbnail_offset,
442 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
443 thumbnail_length = o;
444 if (thumbnail_offset && thumbnail_length)
445 exif_data_load_data_thumbnail (data, d,
446 ds, thumbnail_offset,
456 * If we don't know the tag, don't fail. It could be that new
457 * versions of the standard have defined additional tags. Note that
458 * 0 is a valid tag in the GPS IFD.
460 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
463 * Special case: Tag and format 0. That's against specification
464 * (at least up to 2.2). But Photoshop writes it anyways.
466 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
467 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
468 "Skipping empty entry at position %u in '%s'.", i,
469 exif_ifd_get_name (ifd));
472 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
473 "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
474 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
475 exif_ifd_get_name (ifd));
476 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
479 entry = exif_entry_new_mem (data->priv->mem);
480 if (exif_data_load_data_entry (data, entry, d, ds,
482 exif_content_add_entry (data->ifd[ifd], entry);
483 exif_entry_unref (entry);
490 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
492 ExifShort tag1 = exif_get_short (p1, o);
493 ExifShort tag2 = exif_get_short (p2, o);
495 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
499 cmp_func_intel (const void *elem1, const void *elem2)
501 return cmp_func ((const unsigned char *) elem1,
502 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
506 cmp_func_motorola (const void *elem1, const void *elem2)
508 return cmp_func ((const unsigned char *) elem1,
509 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
513 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
514 unsigned char **d, unsigned int *ds,
517 unsigned int j, n_ptr = 0, n_thumb = 0;
522 if (!data || !data->priv || !ifd || !d || !ds)
525 for (i = 0; i < EXIF_IFD_COUNT; i++)
526 if (ifd == data->ifd[i])
528 if (i == EXIF_IFD_COUNT)
532 * Check if we need some extra entries for pointers or the thumbnail.
538 * The pointer to IFD_EXIF is in IFD_0. The pointer to
539 * IFD_INTEROPERABILITY is in IFD_EXIF.
541 if (data->ifd[EXIF_IFD_EXIF]->count ||
542 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
545 /* The pointer to IFD_GPS is in IFD_0. */
546 if (data->ifd[EXIF_IFD_GPS]->count)
555 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
562 * Allocate enough memory for all entries
563 * and the number of entries.
565 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
566 t = exif_mem_realloc (data->priv->mem, *d, ts);
568 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
574 /* Save the number of entries */
575 exif_set_short (*d + 6 + offset, data->priv->order,
576 (ExifShort) (ifd->count + n_ptr + n_thumb));
580 * Save each entry. Make sure that no memcpys from NULL pointers are
583 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
584 "Saving %i entries (IFD '%s', offset: %i)...",
585 ifd->count, exif_ifd_get_name (i), offset);
586 for (j = 0; j < ifd->count; j++) {
587 if (ifd->entries[j]) {
588 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
593 offset += 12 * ifd->count;
595 /* Now save special entries. */
600 * The pointer to IFD_EXIF is in IFD_0.
601 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
602 * therefore, if IFD_INTEROPERABILITY is not empty, we need
603 * IFD_EXIF even if latter is empty.
605 if (data->ifd[EXIF_IFD_EXIF]->count ||
606 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
607 exif_set_short (*d + 6 + offset + 0, data->priv->order,
608 EXIF_TAG_EXIF_IFD_POINTER);
609 exif_set_short (*d + 6 + offset + 2, data->priv->order,
611 exif_set_long (*d + 6 + offset + 4, data->priv->order,
613 exif_set_long (*d + 6 + offset + 8, data->priv->order,
615 exif_data_save_data_content (data,
616 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
620 /* The pointer to IFD_GPS is in IFD_0, too. */
621 if (data->ifd[EXIF_IFD_GPS]->count) {
622 exif_set_short (*d + 6 + offset + 0, data->priv->order,
623 EXIF_TAG_GPS_INFO_IFD_POINTER);
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,
630 exif_data_save_data_content (data,
631 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
639 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
642 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
643 exif_set_short (*d + 6 + offset + 0, data->priv->order,
644 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
645 exif_set_short (*d + 6 + offset + 2, data->priv->order,
647 exif_set_long (*d + 6 + offset + 4, data->priv->order,
649 exif_set_long (*d + 6 + offset + 8, data->priv->order,
651 exif_data_save_data_content (data,
652 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
661 * Information about the thumbnail (if any) is saved in
666 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
667 exif_set_short (*d + 6 + offset + 0, data->priv->order,
668 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
669 exif_set_short (*d + 6 + offset + 2, data->priv->order,
671 exif_set_long (*d + 6 + offset + 4, data->priv->order,
673 exif_set_long (*d + 6 + offset + 8, data->priv->order,
675 ts = *ds + data->size;
676 t = exif_mem_realloc (data->priv->mem, *d, ts);
678 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
684 memcpy (*d + *ds - data->size, data->data, data->size);
687 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
688 exif_set_short (*d + 6 + offset + 0, data->priv->order,
689 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
690 exif_set_short (*d + 6 + offset + 2, data->priv->order,
692 exif_set_long (*d + 6 + offset + 4, data->priv->order,
694 exif_set_long (*d + 6 + offset + 8, data->priv->order,
704 /* Sort the directory according to TIFF specification */
705 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
706 (ifd->count + n_ptr + n_thumb), 12,
707 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
709 /* Correctly terminate the directory */
710 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
714 * We are saving IFD 0. Tell where IFD 1 starts and save
717 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
718 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
721 exif_set_long (*d + 6 + offset, data->priv->order, 0);
725 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
726 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
727 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
728 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
729 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
730 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
731 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6
732 } ExifDataTypeMakerNote;
734 /*! If MakerNote is recognized, load it.
736 * \param[in,out] data #ExifData
737 * \param[in] d pointer to raw EXIF data
738 * \param[in] ds length of data at d
741 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
744 ExifEntry* e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
748 if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
749 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
750 "ExifData", "Olympus MakerNote variant type %d", mnoteid);
751 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
753 } else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
754 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
755 "ExifData", "Canon MakerNote variant type %d", mnoteid);
756 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
758 } else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
759 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
760 "ExifData", "Fuji MakerNote variant type %d", mnoteid);
761 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
763 /* NOTE: Must do Pentax detection last because some of the
764 * heuristics are pretty general. */
765 } else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
766 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
767 "ExifData", "Pentax MakerNote variant type %d", mnoteid);
768 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
772 * If we are able to interpret the maker note, do so.
774 if (data->priv->md) {
775 exif_mnote_data_log (data->priv->md, data->priv->log);
776 exif_mnote_data_set_byte_order (data->priv->md,
778 exif_mnote_data_set_offset (data->priv->md,
779 data->priv->offset_mnote);
780 exif_mnote_data_load (data->priv->md, d, ds);
784 #define LOG_TOO_SMALL \
785 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
786 _("Size of data too small to allow for EXIF data."));
789 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
795 const unsigned char *d = d_orig;
796 unsigned int len, fullds;
798 if (!data || !data->priv || !d || !ds)
801 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
802 "Parsing %i byte(s) EXIF data...\n", ds);
805 * It can be that the data starts with the EXIF header. If it does
806 * not, search the EXIF marker.
812 if (!memcmp (d, ExifHeader, 6)) {
813 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
814 "Found EXIF header at start.");
817 while (ds && (d[0] == 0xff)) {
822 /* JPEG_MARKER_SOI */
823 if (ds && d[0] == JPEG_MARKER_SOI) {
829 /* JPEG_MARKER_APP1 */
830 if (ds && d[0] == JPEG_MARKER_APP1)
833 /* Skip irrelevant APP markers. The branch for APP1 must come before this,
834 otherwise this code block will cause APP1 to be skipped. This code path
835 is only relevant for files that are nonconformant to the EXIF
836 specification. For conformant files, the APP1 code path above will be
838 if (ds >= 3 && d[0] >= 0xe0 && d[0] <= 0xef) { /* JPEG_MARKER_APPn */
841 l = (d[0] << 8) | d[1];
849 /* Unknown marker or data. Give up. */
850 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
851 "ExifData", _("EXIF marker not found."));
860 len = (d[0] << 8) | d[1];
861 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
862 "We have to deal with %i byte(s) of EXIF data.",
869 * Verify the exif header
870 * (offset 2, length 6).
876 if (memcmp (d, ExifHeader, 6)) {
877 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
878 "ExifData", _("EXIF header not found."));
882 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
883 "Found EXIF header.");
885 /* Sanity check the data length */
889 /* The JPEG APP1 section can be no longer than 64 KiB (including a
890 16-bit length), so cap the data length to protect against overflow
891 in future offset calculations */
896 /* Byte order (offset 6, length 2) */
897 if (!memcmp (d + 6, "II", 2))
898 data->priv->order = EXIF_BYTE_ORDER_INTEL;
899 else if (!memcmp (d + 6, "MM", 2))
900 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
902 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
903 "ExifData", _("Unknown encoding."));
908 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
912 offset = exif_get_long (d + 10, data->priv->order);
913 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
914 "IFD 0 at %i.", (int) offset);
916 /* Sanity check the offset, being careful about overflow */
917 if (offset > ds || offset + 6 + 2 > ds)
920 /* Parse the actual exif data (usually offset 14 from start) */
921 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
924 n = exif_get_short (d + 6 + offset, data->priv->order);
925 if (offset + 6 + 2 + 12 * n + 4 > ds)
928 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
930 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
931 "IFD 1 at %i.", (int) offset);
934 if (offset > ds || offset + 6 > ds) {
935 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
936 "ExifData", "Bogus offset of IFD1.");
938 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
943 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
944 * cameras use pointers in the maker note tag that point to the
945 * space between IFDs. Here is the only place where we have access
948 interpret_maker_note(data, d, fullds);
950 /* Fixup tags if requested */
951 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
952 exif_data_fix (data);
956 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
959 *ds = 0; /* This means something went wrong */
961 if (!data || !d || !ds)
966 *d = exif_data_alloc (data, *ds);
971 memcpy (*d, ExifHeader, 6);
973 /* Order (offset 6) */
974 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
975 memcpy (*d + 6, "II", 2);
977 memcpy (*d + 6, "MM", 2);
980 /* Fixed value (2 bytes, offset 8) */
981 exif_set_short (*d + 8, data->priv->order, 0x002a);
984 * IFD 0 offset (4 bytes, offset 10).
985 * We will start 8 bytes after the
986 * EXIF header (2 bytes for order, another 2 for the test, and
987 * 4 bytes for the IFD 0 offset make 8 bytes together).
989 exif_set_long (*d + 10, data->priv->order, 8);
991 /* Now save IFD 0. IFD 1 will be saved automatically. */
992 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
994 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
996 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
997 "Saved %i byte(s) EXIF data.", *ds);
1001 exif_data_new_from_file (const char *path)
1006 loader = exif_loader_new ();
1007 exif_loader_write_file (loader, path);
1008 edata = exif_loader_get_data (loader);
1009 exif_loader_unref (loader);
1015 exif_data_ref (ExifData *data)
1020 data->priv->ref_count++;
1024 exif_data_unref (ExifData *data)
1029 data->priv->ref_count--;
1030 if (!data->priv->ref_count)
1031 exif_data_free (data);
1035 exif_data_free (ExifData *data)
1038 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1043 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1045 exif_content_unref (data->ifd[i]);
1046 data->ifd[i] = NULL;
1051 exif_mem_free (mem, data->data);
1056 if (data->priv->log) {
1057 exif_log_unref (data->priv->log);
1058 data->priv->log = NULL;
1060 if (data->priv->md) {
1061 exif_mnote_data_unref (data->priv->md);
1062 data->priv->md = NULL;
1064 exif_mem_free (mem, data->priv);
1065 exif_mem_free (mem, data);
1068 exif_mem_unref (mem);
1072 exif_data_dump (ExifData *data)
1079 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1080 if (data->ifd[i] && data->ifd[i]->count) {
1081 printf ("Dumping IFD '%s'...\n",
1082 exif_ifd_get_name (i));
1083 exif_content_dump (data->ifd[i], 0);
1088 printf ("%i byte(s) thumbnail data available: ", data->size);
1089 if (data->size >= 4) {
1090 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1091 data->data[0], data->data[1],
1092 data->data[data->size - 2],
1093 data->data[data->size - 1]);
1099 exif_data_get_byte_order (ExifData *data)
1104 return (data->priv->order);
1108 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1116 for (i = 0; i < EXIF_IFD_COUNT; i++)
1117 func (data->ifd[i], user_data);
1120 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1121 struct _ByteOrderChangeData {
1122 ExifByteOrder old, new;
1126 entry_set_byte_order (ExifEntry *e, void *data)
1128 ByteOrderChangeData *d = data;
1133 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1137 content_set_byte_order (ExifContent *content, void *data)
1139 exif_content_foreach_entry (content, entry_set_byte_order, data);
1143 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1145 ByteOrderChangeData d;
1147 if (!data || (order == data->priv->order))
1150 d.old = data->priv->order;
1152 exif_data_foreach_content (data, content_set_byte_order, &d);
1153 data->priv->order = order;
1155 exif_mnote_data_set_byte_order (data->priv->md, order);
1159 exif_data_log (ExifData *data, ExifLog *log)
1163 if (!data || !data->priv)
1165 exif_log_unref (data->priv->log);
1166 data->priv->log = log;
1169 for (i = 0; i < EXIF_IFD_COUNT; i++)
1170 exif_content_log (data->ifd[i], log);
1173 /* Used internally within libexif */
1174 ExifLog *exif_data_get_log (ExifData *);
1176 exif_data_get_log (ExifData *data)
1178 if (!data || !data->priv)
1180 return data->priv->log;
1183 static const struct {
1184 ExifDataOption option;
1186 const char *description;
1187 } exif_data_option[] = {
1188 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1189 N_("Ignore unknown tags when loading EXIF data.")},
1190 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1191 N_("Add, correct and remove entries to get EXIF data that follows "
1192 "the specification.")},
1193 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1194 N_("When loading and resaving Exif data, save the maker note unmodified."
1195 " Be aware that the maker note can get corrupted.")},
1200 exif_data_option_get_name (ExifDataOption o)
1204 for (i = 0; exif_data_option[i].name; i++)
1205 if (exif_data_option[i].option == o)
1207 return _(exif_data_option[i].name);
1211 exif_data_option_get_description (ExifDataOption o)
1215 for (i = 0; exif_data_option[i].description; i++)
1216 if (exif_data_option[i].option == o)
1218 return _(exif_data_option[i].description);
1222 exif_data_set_option (ExifData *d, ExifDataOption o)
1227 d->priv->options |= o;
1231 exif_data_unset_option (ExifData *d, ExifDataOption o)
1236 d->priv->options &= ~o;
1240 fix_func (ExifContent *c, void *UNUSED(data))
1242 switch (exif_content_get_ifd (c)) {
1244 if (c->parent->data)
1245 exif_content_fix (c);
1246 else if (c->count) {
1247 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1248 "No thumbnail but entries on thumbnail. These entries have been "
1251 unsigned int cnt = c->count;
1252 exif_content_remove_entry (c, c->entries[c->count - 1]);
1253 if (cnt == c->count) {
1255 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1256 "failed to remove last entry from entries.");
1263 exif_content_fix (c);
1268 exif_data_fix (ExifData *d)
1270 exif_data_foreach_content (d, fix_func, NULL);
1274 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1279 d->priv->data_type = dt;
1283 exif_data_get_data_type (ExifData *d)
1285 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;