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;
209 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
210 unsigned char **d, unsigned int *ds,
213 unsigned int doff, s;
215 if (!data || !data->priv)
219 * Each entry is 12 bytes long. The memory for the entry has
220 * already been allocated.
222 exif_set_short (*d + 6 + offset + 0,
223 data->priv->order, (ExifShort) e->tag);
224 exif_set_short (*d + 6 + offset + 2,
225 data->priv->order, (ExifShort) e->format);
227 #ifndef EXIF_DONT_CHANGE_MAKER_NOTE
228 /* If this is the maker note tag, update it. */
229 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
230 exif_mem_free (data->priv->mem, e->data);
233 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
234 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
235 e->components = e->size;
239 exif_set_long (*d + 6 + offset + 4,
240 data->priv->order, e->components);
243 * Size? If bigger than 4 bytes, the actual data is not in
244 * the entry but somewhere else.
246 s = exif_format_get_size (e->format) * e->components;
252 * According to the TIFF specification,
253 * the offset must be an even number. If we need to introduce
254 * a padding byte, we set it to 0.
258 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
260 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
263 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
265 *(*d + *ds - 1) = '\0';
270 /* Write the data. Fill unneeded bytes with 0. */
271 memcpy (*d + 6 + doff, e->data, s);
273 memset (*d + 6 + doff + s, 0, (4 - s));
277 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
278 unsigned int ds, ExifLong offset, ExifLong size)
280 if (ds < offset + size) {
281 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
282 "Bogus thumbnail offset and size: %i < %i + %i.",
283 (int) ds, (int) offset, (int) size);
287 exif_mem_free (data->priv->mem, data->data);
289 data->data = exif_data_alloc (data, data->size);
292 memcpy (data->data, d + offset, data->size);
296 #define CHECK_REC(i) \
298 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
299 "ExifData", "Recursive entry in IFD " \
300 "'%s' detected. Skipping...", \
301 exif_ifd_get_name (i)); \
304 if (data->ifd[(i)]->count) { \
305 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
306 "ExifData", "Attemt to load IFD " \
307 "'%s' multiple times detected. " \
309 exif_ifd_get_name (i)); \
314 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
315 const unsigned char *d,
316 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
318 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
324 if (!data || !data->priv)
326 if ((ifd < 0) || (ifd >= EXIF_IFD_COUNT))
329 if (recursion_depth > 150) {
330 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
331 "Deep recursion detected!");
335 /* Read the number of entries */
336 if (offset >= ds - 1)
338 n = exif_get_short (d + offset, data->priv->order);
339 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
340 "Loading %i entries...", n);
343 /* Check if we have enough data. */
344 if (offset + 12 * n > ds)
345 n = (ds - offset) / 12;
347 for (i = 0; i < n; i++) {
349 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
351 case EXIF_TAG_EXIF_IFD_POINTER:
352 case EXIF_TAG_GPS_INFO_IFD_POINTER:
353 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
354 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
355 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
356 o = exif_get_long (d + offset + 12 * i + 8,
359 case EXIF_TAG_EXIF_IFD_POINTER:
360 CHECK_REC (EXIF_IFD_EXIF);
361 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
363 case EXIF_TAG_GPS_INFO_IFD_POINTER:
364 CHECK_REC (EXIF_IFD_GPS);
365 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
367 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
368 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
369 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
371 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
372 thumbnail_offset = o;
373 if (thumbnail_offset && thumbnail_length)
374 exif_data_load_data_thumbnail (data, d,
375 ds, thumbnail_offset,
378 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
379 thumbnail_length = o;
380 if (thumbnail_offset && thumbnail_length)
381 exif_data_load_data_thumbnail (data, d,
382 ds, thumbnail_offset,
392 * If we don't know the tag, don't fail. It could be that new
393 * versions of the standard have defined additional tags. Note that
394 * 0 is a valid tag in the GPS IFD.
396 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
399 * Special case: Tag and format 0. That's against specification.
400 * At least up to 2.2. But Photoshop writes it anyways.
402 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
403 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
404 "Skipping empty entry at position %i in '%s'.", i,
405 exif_ifd_get_name (ifd));
408 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
409 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
410 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
411 exif_ifd_get_name (ifd));
412 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
415 entry = exif_entry_new_mem (data->priv->mem);
416 exif_data_load_data_entry (data, entry, d, ds,
418 exif_content_add_entry (data->ifd[ifd], entry);
419 exif_entry_unref (entry);
426 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
428 ExifShort tag1 = exif_get_short (p1, o);
429 ExifShort tag2 = exif_get_short (p2, o);
431 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
435 cmp_func_intel (const void *elem1, const void *elem2)
437 return cmp_func ((const unsigned char *) elem1,
438 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
442 cmp_func_motorola (const void *elem1, const void *elem2)
444 return cmp_func ((const unsigned char *) elem1,
445 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
449 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
450 unsigned char **d, unsigned int *ds,
453 unsigned int j, n_ptr = 0, n_thumb = 0;
456 if (!data || !data->priv || !ifd || !d || !ds)
459 for (i = 0; i < EXIF_IFD_COUNT; i++)
460 if (ifd == data->ifd[i])
462 if (i == EXIF_IFD_COUNT)
466 * Check if we need some extra entries for pointers or the thumbnail.
472 * The pointer to IFD_EXIF is in IFD_0. The pointer to
473 * IFD_INTEROPERABILITY is in IFD_EXIF.
475 if (data->ifd[EXIF_IFD_EXIF]->count ||
476 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
479 /* The pointer to IFD_GPS is in IFD_0. */
480 if (data->ifd[EXIF_IFD_GPS]->count)
489 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
496 * Allocate enough memory for all entries
497 * and the number of entries.
499 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
500 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
502 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
506 /* Save the number of entries */
507 exif_set_short (*d + 6 + offset, data->priv->order,
508 (ExifShort) (ifd->count + n_ptr + n_thumb));
511 /* Save each entry */
512 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
513 "Saving %i entries (IFD '%s', offset: %i)...",
514 ifd->count, exif_ifd_get_name (i), offset);
515 for (j = 0; j < ifd->count; j++)
516 exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
518 offset += 12 * ifd->count;
520 /* Now save special entries. */
525 * The pointer to IFD_EXIF is in IFD_0.
526 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
527 * therefore, if IFD_INTEROPERABILITY is not empty, we need
528 * IFD_EXIF even if latter is empty.
530 if (data->ifd[EXIF_IFD_EXIF]->count ||
531 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
532 exif_set_short (*d + 6 + offset + 0, data->priv->order,
533 EXIF_TAG_EXIF_IFD_POINTER);
534 exif_set_short (*d + 6 + offset + 2, data->priv->order,
536 exif_set_long (*d + 6 + offset + 4, data->priv->order,
538 exif_set_long (*d + 6 + offset + 8, data->priv->order,
540 exif_data_save_data_content (data,
541 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
545 /* The pointer to IFD_GPS is in IFD_0, too. */
546 if (data->ifd[EXIF_IFD_GPS]->count) {
547 exif_set_short (*d + 6 + offset + 0, data->priv->order,
548 EXIF_TAG_GPS_INFO_IFD_POINTER);
549 exif_set_short (*d + 6 + offset + 2, data->priv->order,
551 exif_set_long (*d + 6 + offset + 4, data->priv->order,
553 exif_set_long (*d + 6 + offset + 8, data->priv->order,
555 exif_data_save_data_content (data,
556 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
564 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
567 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
568 exif_set_short (*d + 6 + offset + 0, data->priv->order,
569 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
570 exif_set_short (*d + 6 + offset + 2, data->priv->order,
572 exif_set_long (*d + 6 + offset + 4, data->priv->order,
574 exif_set_long (*d + 6 + offset + 8, data->priv->order,
576 exif_data_save_data_content (data,
577 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
586 * Information about the thumbnail (if any) is saved in
591 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
592 exif_set_short (*d + 6 + offset + 0, data->priv->order,
593 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
594 exif_set_short (*d + 6 + offset + 2, data->priv->order,
596 exif_set_long (*d + 6 + offset + 4, data->priv->order,
598 exif_set_long (*d + 6 + offset + 8, data->priv->order,
601 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
603 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
607 memcpy (*d + *ds - data->size, data->data, data->size);
610 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
611 exif_set_short (*d + 6 + offset + 0, data->priv->order,
612 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
613 exif_set_short (*d + 6 + offset + 2, data->priv->order,
615 exif_set_long (*d + 6 + offset + 4, data->priv->order,
617 exif_set_long (*d + 6 + offset + 8, data->priv->order,
627 /* Sort the directory according to TIFF specification */
628 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
629 (ifd->count + n_ptr + n_thumb), 12,
630 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
632 /* Correctly terminate the directory */
633 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
637 * We are saving IFD 0. Tell where IFD 1 starts and save
640 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
641 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
644 exif_set_long (*d + 6 + offset, data->priv->order, 0);
648 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
649 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
650 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
651 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3
652 } ExifDataTypeMakerNote;
654 static ExifDataTypeMakerNote
655 exif_data_get_type_maker_note (ExifData *d)
661 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
663 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
665 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
667 /* Olympus & Nikon */
668 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5) ||
669 !memcmp (e->data, "Nikon", 5)))
670 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
672 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
674 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
677 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
678 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
680 /* Pentax & some variant of Nikon */
681 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
683 exif_entry_get_value (em, value, sizeof(value)),
685 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
687 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
690 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
693 #define LOG_TOO_SMALL \
694 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
695 _("Size of data too small to allow for EXIF data."));
698 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
699 unsigned int ds_orig)
704 const unsigned char *d = d_orig;
705 unsigned int ds = ds_orig, len;
707 if (!data || !data->priv || !d || !ds)
710 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
711 "Parsing %i byte(s) EXIF data...\n", ds);
714 * It can be that the data starts with the EXIF header. If it does
715 * not, search the EXIF marker.
721 if (!memcmp (d, ExifHeader, 6)) {
722 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
723 "Found EXIF header.");
726 while ((d[0] == 0xff) && ds) {
731 /* JPEG_MARKER_SOI */
732 if (d[0] == JPEG_MARKER_SOI) {
738 /* JPEG_MARKER_APP0 */
739 if (d[0] == JPEG_MARKER_APP0) {
742 l = (d[0] << 8) | d[1];
750 /* JPEG_MARKER_APP1 */
751 if (d[0] == JPEG_MARKER_APP1)
754 /* Unknown marker or data. Give up. */
755 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
756 "ExifData", _("EXIF marker not found."));
765 len = (d[0] << 8) | d[1];
766 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
767 "We have to deal with %i byte(s) of EXIF data.",
774 * Verify the exif header
775 * (offset 2, length 6).
781 if (memcmp (d, ExifHeader, 6)) {
782 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
783 "ExifData", _("EXIF header not found."));
787 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
788 "Found EXIF header.");
790 /* Byte order (offset 6, length 2) */
793 if (!memcmp (d + 6, "II", 2))
794 data->priv->order = EXIF_BYTE_ORDER_INTEL;
795 else if (!memcmp (d + 6, "MM", 2))
796 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
798 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
799 "ExifData", _("Unknown encoding."));
804 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
808 offset = exif_get_long (d + 10, data->priv->order);
809 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
810 "IFD 0 at %i.", (int) offset);
812 /* Parse the actual exif data (usually offset 14 from start) */
813 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
816 if (offset + 6 + 2 > ds) {
819 n = exif_get_short (d + 6 + offset, data->priv->order);
820 if (offset + 6 + 2 + 12 * n + 4 > ds) {
823 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
825 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
826 "IFD 1 at %i.", (int) offset);
829 if (offset > ds - 6) {
830 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
831 "ExifData", "Bogus offset.");
835 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
839 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
840 * cameras use pointers in the maker note tag that point to the
841 * space between IFDs. Here is the only place where we have access
844 switch (exif_data_get_type_maker_note (data)) {
845 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
846 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
848 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
849 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
851 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
852 data->priv->md = exif_mnote_data_canon_new (data->priv->mem);
859 * If we are able to interpret the maker note, do so.
861 if (data->priv->md) {
862 exif_mnote_data_log (data->priv->md, data->priv->log);
863 exif_mnote_data_set_byte_order (data->priv->md,
865 exif_mnote_data_set_offset (data->priv->md,
866 data->priv->offset_mnote);
867 exif_mnote_data_load (data->priv->md, d, ds);
870 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
871 exif_data_fix (data);
875 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
877 if (!data || !d || !ds)
882 *d = exif_data_alloc (data, *ds);
885 memcpy (*d, ExifHeader, 6);
887 /* Order (offset 6) */
888 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
889 memcpy (*d + 6, "II", 2);
891 memcpy (*d + 6, "MM", 2);
894 /* Fixed value (2 bytes, offset 8) */
895 exif_set_short (*d + 8, data->priv->order, 0x002a);
898 * IFD 0 offset (4 bytes, offset 10).
899 * We will start 8 bytes after the
900 * EXIF header (2 bytes for order, another 2 for the test, and
901 * 4 bytes for the IFD 0 offset make 8 bytes together).
903 exif_set_long (*d + 10, data->priv->order, 8);
905 /* Now save IFD 0. IFD 1 will be saved automatically. */
906 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
908 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
910 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
911 "Saved %i byte(s) EXIF data.", *ds);
915 exif_data_new_from_file (const char *path)
920 loader = exif_loader_new ();
921 exif_loader_write_file (loader, path);
922 edata = exif_loader_get_data (loader);
923 exif_loader_unref (loader);
929 exif_data_ref (ExifData *data)
934 data->priv->ref_count++;
938 exif_data_unref (ExifData *data)
943 data->priv->ref_count--;
944 if (!data->priv->ref_count)
945 exif_data_free (data);
949 exif_data_free (ExifData *data)
952 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
957 for (i = 0; i < EXIF_IFD_COUNT; i++) {
959 exif_content_unref (data->ifd[i]);
965 exif_mem_free (mem, data->data);
970 if (data->priv->log) {
971 exif_log_unref (data->priv->log);
972 data->priv->log = NULL;
974 if (data->priv->md) {
975 exif_mnote_data_unref (data->priv->md);
976 data->priv->md = NULL;
978 exif_mem_free (mem, data->priv);
979 exif_mem_free (mem, data);
982 exif_mem_unref (mem);
986 exif_data_dump (ExifData *data)
993 for (i = 0; i < EXIF_IFD_COUNT; i++) {
994 if (data->ifd[i] && data->ifd[i]->count) {
995 printf ("Dumping IFD '%s'...\n",
996 exif_ifd_get_name (i));
997 exif_content_dump (data->ifd[i], 0);
1002 printf ("%i byte(s) thumbnail data available.", data->size);
1003 if (data->size >= 4) {
1004 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1005 data->data[0], data->data[1],
1006 data->data[data->size - 2],
1007 data->data[data->size - 1]);
1013 exif_data_get_byte_order (ExifData *data)
1018 return (data->priv->order);
1022 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1030 for (i = 0; i < EXIF_IFD_COUNT; i++)
1031 func (data->ifd[i], user_data);
1034 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1035 struct _ByteOrderChangeData {
1036 ExifByteOrder old, new;
1040 entry_set_byte_order (ExifEntry *e, void *data)
1042 ByteOrderChangeData *d = data;
1047 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1051 content_set_byte_order (ExifContent *content, void *data)
1053 exif_content_foreach_entry (content, entry_set_byte_order, data);
1057 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1059 ByteOrderChangeData d;
1061 if (!data || (order == data->priv->order))
1064 d.old = data->priv->order;
1066 exif_data_foreach_content (data, content_set_byte_order, &d);
1067 data->priv->order = order;
1069 exif_mnote_data_set_byte_order (data->priv->md, order);
1073 exif_data_log (ExifData *data, ExifLog *log)
1077 if (!data || !data->priv)
1079 exif_log_unref (data->priv->log);
1080 data->priv->log = log;
1083 for (i = 0; i < EXIF_IFD_COUNT; i++)
1084 exif_content_log (data->ifd[i], log);
1087 /* Used internally within libexif */
1088 ExifLog *exif_data_get_log (ExifData *);
1090 exif_data_get_log (ExifData *data)
1092 if (!data || !data->priv)
1094 return data->priv->log;
1098 ExifDataOption option;
1100 const char *description;
1101 } exif_data_option[] = {
1102 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1103 N_("Ignore unknown tags when loading EXIF data.")},
1104 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1105 N_("Add, correct and remove entries to get EXIF data that follows "
1106 "the specification.")},
1111 exif_data_option_get_name (ExifDataOption o)
1115 for (i = 0; exif_data_option[i].name; i++)
1116 if (exif_data_option[i].option == o)
1118 return _(exif_data_option[i].name);
1122 exif_data_option_get_description (ExifDataOption o)
1126 for (i = 0; exif_data_option[i].description; i++)
1127 if (exif_data_option[i].option == o)
1129 return _(exif_data_option[i].description);
1133 exif_data_set_option (ExifData *d, ExifDataOption o)
1138 d->priv->options |= o;
1142 exif_data_unset_option (ExifData *d, ExifDataOption o)
1147 d->priv->options &= ~o;
1151 fix_func (ExifContent *c, void *data)
1153 switch (exif_content_get_ifd (c)) {
1155 if (c->parent->data)
1156 exif_content_fix (c);
1158 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1159 "No thumbnail but entries on thumbnail. These entries have been "
1162 exif_content_remove_entry (c, c->entries[c->count - 1]);
1166 exif_content_fix (c);
1171 exif_data_fix (ExifData *d)
1173 exif_data_foreach_content (d, fix_func, NULL);
1177 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1182 d->priv->data_type = dt;
1186 exif_data_get_data_type (ExifData *d)
1188 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;