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>
41 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
43 #if defined(__WATCOMC__) || defined(_MSC_VER)
44 # define strncasecmp strnicmp
47 #undef JPEG_MARKER_SOI
48 #define JPEG_MARKER_SOI 0xd8
49 #undef JPEG_MARKER_APP0
50 #define JPEG_MARKER_APP0 0xe0
51 #undef JPEG_MARKER_APP1
52 #define JPEG_MARKER_APP1 0xe1
54 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
56 struct _ExifDataPrivate
65 unsigned int ref_count;
67 /* Temporarily used while loading data */
68 unsigned int offset_mnote;
70 ExifDataOption options;
71 ExifDataType data_type;
75 exif_data_alloc (ExifData *data, unsigned int i)
82 d = exif_mem_alloc (data->priv->mem, i);
86 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
91 exif_data_get_mnote_data (ExifData *d)
93 return (d && d->priv) ? d->priv->md : NULL;
99 ExifMem *mem = exif_mem_new_default ();
100 ExifData *d = exif_data_new_mem (mem);
102 exif_mem_unref (mem);
108 exif_data_new_mem (ExifMem *mem)
116 data = exif_mem_alloc (mem, sizeof (ExifData));
119 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
121 exif_mem_free (mem, data);
124 data->priv->ref_count = 1;
126 data->priv->mem = mem;
129 for (i = 0; i < EXIF_IFD_COUNT; i++) {
130 data->ifd[i] = exif_content_new_mem (data->priv->mem);
132 exif_data_free (data);
135 data->ifd[i]->parent = data;
138 /* Default options */
139 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
140 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
142 /* Default data type: none */
143 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
149 exif_data_new_from_data (const unsigned char *data, unsigned int size)
153 edata = exif_data_new ();
154 exif_data_load_data (edata, data, size);
159 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
160 const unsigned char *d,
161 unsigned int size, unsigned int offset)
163 unsigned int s, doff;
165 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
166 entry->format = exif_get_short (d + offset + 2, data->priv->order);
167 entry->components = exif_get_long (d + offset + 4, data->priv->order);
169 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
170 "Loading entry 0x%x ('%s')...", entry->tag,
171 exif_tag_get_name (entry->tag));
174 * Size? If bigger than 4 bytes, the actual data is not
175 * in the entry but somewhere else (offset).
177 s = exif_format_get_size (entry->format) * entry->components;
181 doff = exif_get_long (d + offset + 8, data->priv->order);
189 entry->data = exif_data_alloc (data, s);
192 memcpy (entry->data, d + doff, s);
195 /* If this is the MakerNote, remember the offset */
196 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
197 if (entry->size > 6) exif_log (data->priv->log,
198 EXIF_LOG_CODE_DEBUG, "ExifData",
199 "MakerNote found (%02x %02x %02x %02x "
200 "%02x %02x %02x...).",
201 entry->data[0], entry->data[1], entry->data[2],
202 entry->data[3], entry->data[4], entry->data[5],
204 data->priv->offset_mnote = doff;
210 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
211 unsigned char **d, unsigned int *ds,
214 unsigned int doff, s;
216 if (!data || !data->priv)
220 * Each entry is 12 bytes long. The memory for the entry has
221 * already been allocated.
223 exif_set_short (*d + 6 + offset + 0,
224 data->priv->order, (ExifShort) e->tag);
225 exif_set_short (*d + 6 + offset + 2,
226 data->priv->order, (ExifShort) e->format);
228 #ifndef EXIF_DONT_CHANGE_MAKER_NOTE
229 /* If this is the maker note tag, update it. */
230 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
231 exif_mem_free (data->priv->mem, e->data);
234 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
235 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
236 e->components = e->size;
240 exif_set_long (*d + 6 + offset + 4,
241 data->priv->order, e->components);
244 * Size? If bigger than 4 bytes, the actual data is not in
245 * the entry but somewhere else.
247 s = exif_format_get_size (e->format) * e->components;
253 * According to the TIFF specification,
254 * the offset must be an even number. If we need to introduce
255 * a padding byte, we set it to 0.
259 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
261 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
264 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
266 *(*d + *ds - 1) = '\0';
271 /* Write the data. Fill unneeded bytes with 0. */
272 memcpy (*d + 6 + doff, e->data, s);
274 memset (*d + 6 + doff + s, 0, (4 - s));
278 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
279 unsigned int ds, ExifLong offset, ExifLong size)
281 if (ds < offset + size) {
282 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
283 "Bogus thumbnail offset and size: %i < %i + %i.",
284 (int) ds, (int) offset, (int) size);
288 exif_mem_free (data->priv->mem, data->data);
290 data->data = exif_data_alloc (data, data->size);
293 memcpy (data->data, d + offset, data->size);
297 #define CHECK_REC(i) \
299 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
300 "ExifData", "Recursive entry in IFD " \
301 "'%s' detected. Skipping...", \
302 exif_ifd_get_name (i)); \
305 if (data->ifd[(i)]->count) { \
306 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
307 "ExifData", "Attemt to load IFD " \
308 "'%s' multiple times detected. " \
310 exif_ifd_get_name (i)); \
315 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
316 const unsigned char *d,
317 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
319 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
325 if (!data || !data->priv)
327 if ((ifd < 0) || (ifd >= EXIF_IFD_COUNT))
330 if (recursion_depth > 150) {
331 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
332 "Deep recursion detected!");
336 /* Read the number of entries */
337 if (offset >= ds - 1)
339 n = exif_get_short (d + offset, data->priv->order);
340 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
341 "Loading %i entries...", n);
344 /* Check if we have enough data. */
345 if (offset + 12 * n > ds)
346 n = (ds - offset) / 12;
348 for (i = 0; i < n; i++) {
350 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
352 case EXIF_TAG_EXIF_IFD_POINTER:
353 case EXIF_TAG_GPS_INFO_IFD_POINTER:
354 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
355 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
356 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
357 o = exif_get_long (d + offset + 12 * i + 8,
360 case EXIF_TAG_EXIF_IFD_POINTER:
361 CHECK_REC (EXIF_IFD_EXIF);
362 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
364 case EXIF_TAG_GPS_INFO_IFD_POINTER:
365 CHECK_REC (EXIF_IFD_GPS);
366 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
368 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
369 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
370 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
372 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
373 thumbnail_offset = o;
374 if (thumbnail_offset && thumbnail_length)
375 exif_data_load_data_thumbnail (data, d,
376 ds, thumbnail_offset,
379 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
380 thumbnail_length = o;
381 if (thumbnail_offset && thumbnail_length)
382 exif_data_load_data_thumbnail (data, d,
383 ds, thumbnail_offset,
393 * If we don't know the tag, don't fail. It could be that new
394 * versions of the standard have defined additional tags. Note that
395 * 0 is a valid tag in the GPS IFD.
397 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
400 * Special case: Tag and format 0. That's against specification.
401 * At least up to 2.2. But Photoshop writes it anyways.
403 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
404 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
405 "Skipping empty entry at position %i in '%s'.", i,
406 exif_ifd_get_name (ifd));
409 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
410 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
411 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
412 exif_ifd_get_name (ifd));
413 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
416 entry = exif_entry_new_mem (data->priv->mem);
417 if (exif_data_load_data_entry (data, entry, d, ds,
419 exif_content_add_entry (data->ifd[ifd], entry);
420 exif_entry_unref (entry);
427 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
429 ExifShort tag1 = exif_get_short (p1, o);
430 ExifShort tag2 = exif_get_short (p2, o);
432 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
436 cmp_func_intel (const void *elem1, const void *elem2)
438 return cmp_func ((const unsigned char *) elem1,
439 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
443 cmp_func_motorola (const void *elem1, const void *elem2)
445 return cmp_func ((const unsigned char *) elem1,
446 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
450 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
451 unsigned char **d, unsigned int *ds,
454 unsigned int j, n_ptr = 0, n_thumb = 0;
457 if (!data || !data->priv || !ifd || !d || !ds)
460 for (i = 0; i < EXIF_IFD_COUNT; i++)
461 if (ifd == data->ifd[i])
463 if (i == EXIF_IFD_COUNT)
467 * Check if we need some extra entries for pointers or the thumbnail.
473 * The pointer to IFD_EXIF is in IFD_0. The pointer to
474 * IFD_INTEROPERABILITY is in IFD_EXIF.
476 if (data->ifd[EXIF_IFD_EXIF]->count ||
477 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
480 /* The pointer to IFD_GPS is in IFD_0. */
481 if (data->ifd[EXIF_IFD_GPS]->count)
490 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
497 * Allocate enough memory for all entries
498 * and the number of entries.
500 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
501 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
503 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
507 /* Save the number of entries */
508 exif_set_short (*d + 6 + offset, data->priv->order,
509 (ExifShort) (ifd->count + n_ptr + n_thumb));
512 /* Save each entry */
513 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
514 "Saving %i entries (IFD '%s', offset: %i)...",
515 ifd->count, exif_ifd_get_name (i), offset);
516 for (j = 0; j < ifd->count; j++)
517 exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
519 offset += 12 * ifd->count;
521 /* Now save special entries. */
526 * The pointer to IFD_EXIF is in IFD_0.
527 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
528 * therefore, if IFD_INTEROPERABILITY is not empty, we need
529 * IFD_EXIF even if latter is empty.
531 if (data->ifd[EXIF_IFD_EXIF]->count ||
532 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
533 exif_set_short (*d + 6 + offset + 0, data->priv->order,
534 EXIF_TAG_EXIF_IFD_POINTER);
535 exif_set_short (*d + 6 + offset + 2, data->priv->order,
537 exif_set_long (*d + 6 + offset + 4, data->priv->order,
539 exif_set_long (*d + 6 + offset + 8, data->priv->order,
541 exif_data_save_data_content (data,
542 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
546 /* The pointer to IFD_GPS is in IFD_0, too. */
547 if (data->ifd[EXIF_IFD_GPS]->count) {
548 exif_set_short (*d + 6 + offset + 0, data->priv->order,
549 EXIF_TAG_GPS_INFO_IFD_POINTER);
550 exif_set_short (*d + 6 + offset + 2, data->priv->order,
552 exif_set_long (*d + 6 + offset + 4, data->priv->order,
554 exif_set_long (*d + 6 + offset + 8, data->priv->order,
556 exif_data_save_data_content (data,
557 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
565 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
568 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
569 exif_set_short (*d + 6 + offset + 0, data->priv->order,
570 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
571 exif_set_short (*d + 6 + offset + 2, data->priv->order,
573 exif_set_long (*d + 6 + offset + 4, data->priv->order,
575 exif_set_long (*d + 6 + offset + 8, data->priv->order,
577 exif_data_save_data_content (data,
578 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
587 * Information about the thumbnail (if any) is saved in
592 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
593 exif_set_short (*d + 6 + offset + 0, data->priv->order,
594 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
595 exif_set_short (*d + 6 + offset + 2, data->priv->order,
597 exif_set_long (*d + 6 + offset + 4, data->priv->order,
599 exif_set_long (*d + 6 + offset + 8, data->priv->order,
602 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
604 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
608 memcpy (*d + *ds - data->size, data->data, data->size);
611 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
612 exif_set_short (*d + 6 + offset + 0, data->priv->order,
613 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
614 exif_set_short (*d + 6 + offset + 2, data->priv->order,
616 exif_set_long (*d + 6 + offset + 4, data->priv->order,
618 exif_set_long (*d + 6 + offset + 8, data->priv->order,
628 /* Sort the directory according to TIFF specification */
629 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
630 (ifd->count + n_ptr + n_thumb), 12,
631 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
633 /* Correctly terminate the directory */
634 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
638 * We are saving IFD 0. Tell where IFD 1 starts and save
641 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
642 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
645 exif_set_long (*d + 6 + offset, data->priv->order, 0);
649 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
650 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
651 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
652 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3
653 } ExifDataTypeMakerNote;
655 static ExifDataTypeMakerNote
656 exif_data_get_type_maker_note (ExifData *d)
662 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
664 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
666 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
668 /* Olympus & Nikon */
669 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5) ||
670 !memcmp (e->data, "Nikon", 5)))
671 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
673 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
675 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
678 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
679 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
681 /* Pentax & some variant of Nikon */
682 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
684 exif_entry_get_value (em, value, sizeof(value)),
686 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
688 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
691 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
694 #define LOG_TOO_SMALL \
695 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
696 _("Size of data too small to allow for EXIF data."));
699 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
700 unsigned int ds_orig)
705 const unsigned char *d = d_orig;
706 unsigned int ds = ds_orig, len;
708 if (!data || !data->priv || !d || !ds)
711 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
712 "Parsing %i byte(s) EXIF data...\n", ds);
715 * It can be that the data starts with the EXIF header. If it does
716 * not, search the EXIF marker.
722 if (!memcmp (d, ExifHeader, 6)) {
723 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
724 "Found EXIF header.");
727 while ((d[0] == 0xff) && ds) {
732 /* JPEG_MARKER_SOI */
733 if (d[0] == JPEG_MARKER_SOI) {
739 /* JPEG_MARKER_APP0 */
740 if (d[0] == JPEG_MARKER_APP0) {
743 l = (d[0] << 8) | d[1];
751 /* JPEG_MARKER_APP1 */
752 if (d[0] == JPEG_MARKER_APP1)
755 /* Unknown marker or data. Give up. */
756 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
757 "ExifData", _("EXIF marker not found."));
766 len = (d[0] << 8) | d[1];
767 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
768 "We have to deal with %i byte(s) of EXIF data.",
775 * Verify the exif header
776 * (offset 2, length 6).
782 if (memcmp (d, ExifHeader, 6)) {
783 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
784 "ExifData", _("EXIF header not found."));
788 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
789 "Found EXIF header.");
791 /* Byte order (offset 6, length 2) */
794 if (!memcmp (d + 6, "II", 2))
795 data->priv->order = EXIF_BYTE_ORDER_INTEL;
796 else if (!memcmp (d + 6, "MM", 2))
797 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
799 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
800 "ExifData", _("Unknown encoding."));
805 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
809 offset = exif_get_long (d + 10, data->priv->order);
810 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
811 "IFD 0 at %i.", (int) offset);
813 /* Parse the actual exif data (usually offset 14 from start) */
814 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
817 if (offset + 6 + 2 > ds) {
820 n = exif_get_short (d + 6 + offset, data->priv->order);
821 if (offset + 6 + 2 + 12 * n + 4 > ds) {
824 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
826 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
827 "IFD 1 at %i.", (int) offset);
830 if (offset > ds - 6) {
831 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
832 "ExifData", "Bogus offset.");
836 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
840 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
841 * cameras use pointers in the maker note tag that point to the
842 * space between IFDs. Here is the only place where we have access
845 switch (exif_data_get_type_maker_note (data)) {
846 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
847 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
849 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
850 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
852 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
853 data->priv->md = exif_mnote_data_canon_new (data->priv->mem);
860 * If we are able to interpret the maker note, do so.
862 if (data->priv->md) {
863 exif_mnote_data_log (data->priv->md, data->priv->log);
864 exif_mnote_data_set_byte_order (data->priv->md,
866 exif_mnote_data_set_offset (data->priv->md,
867 data->priv->offset_mnote);
868 exif_mnote_data_load (data->priv->md, d, ds);
871 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
872 exif_data_fix (data);
876 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
878 if (!data || !d || !ds)
883 *d = exif_data_alloc (data, *ds);
886 memcpy (*d, ExifHeader, 6);
888 /* Order (offset 6) */
889 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
890 memcpy (*d + 6, "II", 2);
892 memcpy (*d + 6, "MM", 2);
895 /* Fixed value (2 bytes, offset 8) */
896 exif_set_short (*d + 8, data->priv->order, 0x002a);
899 * IFD 0 offset (4 bytes, offset 10).
900 * We will start 8 bytes after the
901 * EXIF header (2 bytes for order, another 2 for the test, and
902 * 4 bytes for the IFD 0 offset make 8 bytes together).
904 exif_set_long (*d + 10, data->priv->order, 8);
906 /* Now save IFD 0. IFD 1 will be saved automatically. */
907 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
909 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
911 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
912 "Saved %i byte(s) EXIF data.", *ds);
916 exif_data_new_from_file (const char *path)
921 loader = exif_loader_new ();
922 exif_loader_write_file (loader, path);
923 edata = exif_loader_get_data (loader);
924 exif_loader_unref (loader);
930 exif_data_ref (ExifData *data)
935 data->priv->ref_count++;
939 exif_data_unref (ExifData *data)
944 data->priv->ref_count--;
945 if (!data->priv->ref_count)
946 exif_data_free (data);
950 exif_data_free (ExifData *data)
953 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
958 for (i = 0; i < EXIF_IFD_COUNT; i++) {
960 exif_content_unref (data->ifd[i]);
966 exif_mem_free (mem, data->data);
971 if (data->priv->log) {
972 exif_log_unref (data->priv->log);
973 data->priv->log = NULL;
975 if (data->priv->md) {
976 exif_mnote_data_unref (data->priv->md);
977 data->priv->md = NULL;
979 exif_mem_free (mem, data->priv);
980 exif_mem_free (mem, data);
983 exif_mem_unref (mem);
987 exif_data_dump (ExifData *data)
994 for (i = 0; i < EXIF_IFD_COUNT; i++) {
995 if (data->ifd[i] && data->ifd[i]->count) {
996 printf ("Dumping IFD '%s'...\n",
997 exif_ifd_get_name (i));
998 exif_content_dump (data->ifd[i], 0);
1003 printf ("%i byte(s) thumbnail data available.", data->size);
1004 if (data->size >= 4) {
1005 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1006 data->data[0], data->data[1],
1007 data->data[data->size - 2],
1008 data->data[data->size - 1]);
1014 exif_data_get_byte_order (ExifData *data)
1019 return (data->priv->order);
1023 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1031 for (i = 0; i < EXIF_IFD_COUNT; i++)
1032 func (data->ifd[i], user_data);
1035 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1036 struct _ByteOrderChangeData {
1037 ExifByteOrder old, new;
1041 entry_set_byte_order (ExifEntry *e, void *data)
1043 ByteOrderChangeData *d = data;
1048 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1052 content_set_byte_order (ExifContent *content, void *data)
1054 exif_content_foreach_entry (content, entry_set_byte_order, data);
1058 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1060 ByteOrderChangeData d;
1062 if (!data || (order == data->priv->order))
1065 d.old = data->priv->order;
1067 exif_data_foreach_content (data, content_set_byte_order, &d);
1068 data->priv->order = order;
1070 exif_mnote_data_set_byte_order (data->priv->md, order);
1074 exif_data_log (ExifData *data, ExifLog *log)
1078 if (!data || !data->priv)
1080 exif_log_unref (data->priv->log);
1081 data->priv->log = log;
1084 for (i = 0; i < EXIF_IFD_COUNT; i++)
1085 exif_content_log (data->ifd[i], log);
1088 /* Used internally within libexif */
1089 ExifLog *exif_data_get_log (ExifData *);
1091 exif_data_get_log (ExifData *data)
1093 if (!data || !data->priv)
1095 return data->priv->log;
1099 ExifDataOption option;
1101 const char *description;
1102 } exif_data_option[] = {
1103 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1104 N_("Ignore unknown tags when loading EXIF data.")},
1105 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1106 N_("Add, correct and remove entries to get EXIF data that follows "
1107 "the specification.")},
1112 exif_data_option_get_name (ExifDataOption o)
1116 for (i = 0; exif_data_option[i].name; i++)
1117 if (exif_data_option[i].option == o)
1119 return _(exif_data_option[i].name);
1123 exif_data_option_get_description (ExifDataOption o)
1127 for (i = 0; exif_data_option[i].description; i++)
1128 if (exif_data_option[i].option == o)
1130 return _(exif_data_option[i].description);
1134 exif_data_set_option (ExifData *d, ExifDataOption o)
1139 d->priv->options |= o;
1143 exif_data_unset_option (ExifData *d, ExifDataOption o)
1148 d->priv->options &= ~o;
1152 fix_func (ExifContent *c, void *data)
1154 switch (exif_content_get_ifd (c)) {
1156 if (c->parent->data)
1157 exif_content_fix (c);
1159 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1160 "No thumbnail but entries on thumbnail. These entries have been "
1163 exif_content_remove_entry (c, c->entries[c->count - 1]);
1167 exif_content_fix (c);
1172 exif_data_fix (ExifData *d)
1174 exif_data_foreach_content (d, fix_func, NULL);
1178 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1183 d->priv->data_type = dt;
1187 exif_data_get_data_type (ExifData *d)
1189 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;