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>
37 #include <libexif/samsung/exif-mnote-data-samsung.h>
43 #undef JPEG_MARKER_SOI
44 #define JPEG_MARKER_SOI 0xd8
45 #undef JPEG_MARKER_APP0
46 #define JPEG_MARKER_APP0 0xe0
47 #undef JPEG_MARKER_APP1
48 #define JPEG_MARKER_APP1 0xe1
50 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
52 struct _ExifDataPrivate
61 unsigned int ref_count;
63 /* Temporarily used while loading data */
64 unsigned int offset_mnote;
66 ExifDataOption options;
67 ExifDataType data_type;
71 exif_data_alloc (ExifData *data, unsigned int i)
78 d = exif_mem_alloc (data->priv->mem, i);
82 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
87 exif_data_get_mnote_data (ExifData *d)
89 return (d && d->priv) ? d->priv->md : NULL;
93 exif_data_get_data_order (ExifData *d)
95 return (d && d->priv) ? d->priv->order : -1;
101 ExifMem *mem = exif_mem_new_default ();
102 ExifData *d = exif_data_new_mem (mem);
104 exif_mem_unref (mem);
110 exif_data_new_mem (ExifMem *mem)
118 data = exif_mem_alloc (mem, sizeof (ExifData));
121 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
123 exif_mem_free (mem, data);
126 data->priv->ref_count = 1;
128 data->priv->mem = mem;
131 for (i = 0; i < EXIF_IFD_COUNT; i++) {
132 data->ifd[i] = exif_content_new_mem (data->priv->mem);
134 exif_data_free (data);
137 data->ifd[i]->parent = data;
140 /* Default options */
141 #ifndef NO_VERBOSE_TAG_STRINGS
143 * When the tag list is compiled away, setting this option prevents
144 * any tags from being loaded
146 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
148 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
150 /* Default data type: none */
151 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
157 exif_data_new_from_data (const unsigned char *data, unsigned int size)
161 edata = exif_data_new ();
162 exif_data_load_data (edata, data, size);
167 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
168 const unsigned char *d,
169 unsigned int size, unsigned int offset)
171 unsigned int s, doff;
173 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
174 entry->format = exif_get_short (d + offset + 2, data->priv->order);
175 entry->components = exif_get_long (d + offset + 4, data->priv->order);
177 /* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent
178 * has not been set yet
180 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
181 "Loading entry 0x%x ('%s')...", entry->tag,
182 exif_tag_get_name (entry->tag));
184 /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff }
185 * -> { 0x000000000 .. 0x7fffffff8 } */
186 s = exif_format_get_size(entry->format) * entry->components;
187 if ((s < entry->components) || (s == 0)){
192 * Size? If bigger than 4 bytes, the actual data is not
193 * in the entry but somewhere else (offset).
196 doff = exif_get_long (d + offset + 8, data->priv->order);
201 if ((doff + s < doff) || (doff + s < s) || (doff + s > size)) {
202 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
203 "Tag data past end of buffer (%u > %u)", doff+s, size);
207 entry->data = exif_data_alloc (data, s);
210 memcpy (entry->data, d + doff, s);
212 /* FIXME: What do our callers do if (entry->data == NULL)? */
213 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
216 /* If this is the MakerNote, remember the offset */
217 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
219 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
220 "MakerNote found with empty data");
221 } else if (entry->size > 6) {
222 exif_log (data->priv->log,
223 EXIF_LOG_CODE_DEBUG, "ExifData",
224 "MakerNote found (%02x %02x %02x %02x "
225 "%02x %02x %02x...).",
226 entry->data[0], entry->data[1], entry->data[2],
227 entry->data[3], entry->data[4], entry->data[5],
230 data->priv->offset_mnote = doff;
236 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
237 unsigned char **d, unsigned int *ds,
240 unsigned int doff, s;
243 if (!data || !data->priv)
247 * Each entry is 12 bytes long. The memory for the entry has
248 * already been allocated.
250 exif_set_short (*d + 6 + offset + 0,
251 data->priv->order, (ExifShort) e->tag);
252 exif_set_short (*d + 6 + offset + 2,
253 data->priv->order, (ExifShort) e->format);
255 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
256 /* If this is the maker note tag, update it. */
257 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
258 /* TODO: this is using the wrong ExifMem to free e->data */
259 exif_mem_free (data->priv->mem, e->data);
262 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
263 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
264 e->components = e->size;
268 exif_set_long (*d + 6 + offset + 4,
269 data->priv->order, e->components);
272 * Size? If bigger than 4 bytes, the actual data is not in
273 * the entry but somewhere else.
275 s = exif_format_get_size (e->format) * e->components;
282 * According to the TIFF specification,
283 * the offset must be an even number. If we need to introduce
284 * a padding byte, we set it to 0.
288 t = exif_mem_realloc (data->priv->mem, *d, ts);
290 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
295 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
297 *(*d + *ds - 1) = '\0';
302 /* Write the data. Fill unneeded bytes with 0. Do not crash with
305 memcpy (*d + 6 + doff, e->data, s);
307 memset (*d + 6 + doff, 0, s);
310 memset (*d + 6 + doff + s, 0, (4 - s));
314 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
315 unsigned int ds, ExifLong o, ExifLong s)
318 if ((o + s < o) || (o + s < s) || (o + s > ds) || (o > ds)) {
319 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
320 "Bogus thumbnail offset (%u) or size (%u).",
326 exif_mem_free (data->priv->mem, data->data);
327 if (!(data->data = exif_data_alloc (data, s))) {
328 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
333 memcpy (data->data, d + o, s);
337 #define CHECK_REC(i) \
339 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
340 "ExifData", "Recursive entry in IFD " \
341 "'%s' detected. Skipping...", \
342 exif_ifd_get_name (i)); \
345 if (data->ifd[(i)]->count) { \
346 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
347 "ExifData", "Attempt to load IFD " \
348 "'%s' multiple times detected. " \
350 exif_ifd_get_name (i)); \
354 /*! Load data for an IFD.
356 * \param[in,out] data #ExifData
357 * \param[in] ifd IFD to load
358 * \param[in] d pointer to buffer containing raw IFD data
359 * \param[in] ds size of raw data in buffer at \c d
360 * \param[in] offset offset into buffer at \c d at which IFD starts
361 * \param[in] recursion_depth number of times this function has been
362 * recursively called without returning
365 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
366 const unsigned char *d,
367 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
369 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
375 if (!data || !data->priv)
378 /* check for valid ExifIfd enum range */
379 if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
382 if (recursion_depth > 30) {
383 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
384 "Deep recursion detected!");
388 /* Read the number of entries */
389 if ((offset + 2 < offset) || (offset + 2 < 2) || (offset + 2 > ds)) {
390 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
391 "Tag data past end of buffer (%u > %u)", offset+2, ds);
394 n = exif_get_short (d + offset, data->priv->order);
395 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
396 "Loading %hu entries...", n);
399 /* Check if we have enough data. */
400 if (offset + 12 * n > ds) {
401 n = (ds - offset) / 12;
402 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
403 "Short data; only loading %hu entries...", n);
406 for (i = 0; i < n; i++) {
408 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
410 case EXIF_TAG_EXIF_IFD_POINTER:
411 case EXIF_TAG_GPS_INFO_IFD_POINTER:
412 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
413 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
414 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
415 o = exif_get_long (d + offset + 12 * i + 8,
417 /* FIXME: IFD_POINTER tags aren't marked as being in a
418 * specific IFD, so exif_tag_get_name_in_ifd won't work
420 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
421 "Sub-IFD entry 0x%x ('%s') at %u.", tag,
422 exif_tag_get_name(tag), o);
424 case EXIF_TAG_EXIF_IFD_POINTER:
425 CHECK_REC (EXIF_IFD_EXIF);
426 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
428 case EXIF_TAG_GPS_INFO_IFD_POINTER:
429 CHECK_REC (EXIF_IFD_GPS);
430 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
432 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
433 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
434 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
436 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
437 thumbnail_offset = o;
438 if (thumbnail_offset && thumbnail_length)
439 exif_data_load_data_thumbnail (data, d,
440 ds, thumbnail_offset,
443 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
444 thumbnail_length = o;
445 if (thumbnail_offset && thumbnail_length)
446 exif_data_load_data_thumbnail (data, d,
447 ds, thumbnail_offset,
457 * If we don't know the tag, don't fail. It could be that new
458 * versions of the standard have defined additional tags. Note that
459 * 0 is a valid tag in the GPS IFD.
461 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
464 * Special case: Tag and format 0. That's against specification
465 * (at least up to 2.2). But Photoshop writes it anyways.
467 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
468 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
469 "Skipping empty entry at position %u in '%s'.", i,
470 exif_ifd_get_name (ifd));
473 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
474 "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
475 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
476 exif_ifd_get_name (ifd));
477 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
480 entry = exif_entry_new_mem (data->priv->mem);
481 if (exif_data_load_data_entry (data, entry, d, ds,
483 exif_content_add_entry (data->ifd[ifd], entry);
484 exif_entry_unref (entry);
491 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
493 ExifShort tag1 = exif_get_short (p1, o);
494 ExifShort tag2 = exif_get_short (p2, o);
496 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
500 cmp_func_intel (const void *elem1, const void *elem2)
502 return cmp_func ((const unsigned char *) elem1,
503 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
507 cmp_func_motorola (const void *elem1, const void *elem2)
509 return cmp_func ((const unsigned char *) elem1,
510 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
514 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
515 unsigned char **d, unsigned int *ds,
518 unsigned int j, n_ptr = 0, n_thumb = 0;
523 if (!data || !data->priv || !ifd || !d || !ds)
526 for (i = 0; i < EXIF_IFD_COUNT; i++)
527 if (ifd == data->ifd[i])
529 if (i == EXIF_IFD_COUNT)
533 * Check if we need some extra entries for pointers or the thumbnail.
539 * The pointer to IFD_EXIF is in IFD_0. The pointer to
540 * IFD_INTEROPERABILITY is in IFD_EXIF.
542 if (data->ifd[EXIF_IFD_EXIF]->count ||
543 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
546 /* The pointer to IFD_GPS is in IFD_0. */
547 if (data->ifd[EXIF_IFD_GPS]->count)
556 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
563 * Allocate enough memory for all entries
564 * and the number of entries.
566 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
567 t = exif_mem_realloc (data->priv->mem, *d, ts);
569 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
575 /* Save the number of entries */
576 exif_set_short (*d + 6 + offset, data->priv->order,
577 (ExifShort) (ifd->count + n_ptr + n_thumb));
581 * Save each entry. Make sure that no memcpys from NULL pointers are
584 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
585 "Saving %i entries (IFD '%s', offset: %i)...",
586 ifd->count, exif_ifd_get_name (i), offset);
587 for (j = 0; j < ifd->count; j++) {
588 if (ifd->entries[j]) {
589 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
594 offset += 12 * ifd->count;
596 /* Now save special entries. */
601 * The pointer to IFD_EXIF is in IFD_0.
602 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
603 * therefore, if IFD_INTEROPERABILITY is not empty, we need
604 * IFD_EXIF even if latter is empty.
606 if (data->ifd[EXIF_IFD_EXIF]->count ||
607 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
608 exif_set_short (*d + 6 + offset + 0, data->priv->order,
609 EXIF_TAG_EXIF_IFD_POINTER);
610 exif_set_short (*d + 6 + offset + 2, data->priv->order,
612 exif_set_long (*d + 6 + offset + 4, data->priv->order,
614 exif_set_long (*d + 6 + offset + 8, data->priv->order,
616 exif_data_save_data_content (data,
617 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
621 /* The pointer to IFD_GPS is in IFD_0, too. */
622 if (data->ifd[EXIF_IFD_GPS]->count) {
623 exif_set_short (*d + 6 + offset + 0, data->priv->order,
624 EXIF_TAG_GPS_INFO_IFD_POINTER);
625 exif_set_short (*d + 6 + offset + 2, data->priv->order,
627 exif_set_long (*d + 6 + offset + 4, data->priv->order,
629 exif_set_long (*d + 6 + offset + 8, data->priv->order,
631 exif_data_save_data_content (data,
632 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
640 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
643 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
644 exif_set_short (*d + 6 + offset + 0, data->priv->order,
645 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
646 exif_set_short (*d + 6 + offset + 2, data->priv->order,
648 exif_set_long (*d + 6 + offset + 4, data->priv->order,
650 exif_set_long (*d + 6 + offset + 8, data->priv->order,
652 exif_data_save_data_content (data,
653 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
662 * Information about the thumbnail (if any) is saved in
667 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
668 exif_set_short (*d + 6 + offset + 0, data->priv->order,
669 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
670 exif_set_short (*d + 6 + offset + 2, data->priv->order,
672 exif_set_long (*d + 6 + offset + 4, data->priv->order,
674 exif_set_long (*d + 6 + offset + 8, data->priv->order,
676 ts = *ds + data->size;
677 t = exif_mem_realloc (data->priv->mem, *d, ts);
679 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
685 memcpy (*d + *ds - data->size, data->data, data->size);
688 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
689 exif_set_short (*d + 6 + offset + 0, data->priv->order,
690 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
691 exif_set_short (*d + 6 + offset + 2, data->priv->order,
693 exif_set_long (*d + 6 + offset + 4, data->priv->order,
695 exif_set_long (*d + 6 + offset + 8, data->priv->order,
705 /* Sort the directory according to TIFF specification */
706 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
707 (ifd->count + n_ptr + n_thumb), 12,
708 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
710 /* Correctly terminate the directory */
711 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
715 * We are saving IFD 0. Tell where IFD 1 starts and save
718 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
719 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
722 exif_set_long (*d + 6 + offset, data->priv->order, 0);
726 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
727 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
728 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
729 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
730 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
731 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
732 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6,
733 EXIF_DATA_TYPE_MAKER_NOTE_SAMSUNG = 7
734 } ExifDataTypeMakerNote;
736 /*! If MakerNote is recognized, load it.
738 * \param[in,out] data #ExifData
739 * \param[in] d pointer to raw EXIF data
740 * \param[in] ds length of data at d
743 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
746 ExifEntry* e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
750 if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
751 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
752 "ExifData", "Olympus MakerNote variant type %d", mnoteid);
753 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
755 } else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
756 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
757 "ExifData", "Canon MakerNote variant type %d", mnoteid);
758 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
760 } else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
761 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
762 "ExifData", "Fuji MakerNote variant type %d", mnoteid);
763 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
765 /* NOTE: Must do Pentax detection last because some of the
766 * heuristics are pretty general. */
767 } else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
768 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
769 "ExifData", "Pentax MakerNote variant type %d", mnoteid);
770 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
771 } else if ((mnoteid = exif_mnote_data_samsung_identify (data, e)) != 0) {
772 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
773 "ExifData", "Samsung MakerNote variant type %d", mnoteid);
774 data->priv->md = exif_mnote_data_samsung_new (data->priv->mem);
777 * If we are able to interpret the maker note, do so.
779 if (data->priv->md) {
780 exif_mnote_data_log (data->priv->md, data->priv->log);
781 exif_mnote_data_set_byte_order (data->priv->md,
783 exif_mnote_data_set_offset (data->priv->md,
784 data->priv->offset_mnote);
785 exif_mnote_data_load (data->priv->md, d, ds);
789 #define LOG_TOO_SMALL \
790 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
791 _("Size of data too small to allow for EXIF data."));
794 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
800 const unsigned char *d = d_orig;
801 unsigned int len, fullds;
803 if (!data || !data->priv || !d || !ds)
806 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
807 "Parsing %i byte(s) EXIF data...\n", ds);
810 * It can be that the data starts with the EXIF header. If it does
811 * not, search the EXIF marker.
817 if (!memcmp (d, ExifHeader, 6)) {
818 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
819 "Found EXIF header.");
822 while (ds && (d[0] == 0xff)) {
827 /* JPEG_MARKER_SOI */
828 if (ds && d[0] == JPEG_MARKER_SOI) {
834 /* JPEG_MARKER_APP0 */
835 if (ds >= 3 && d[0] == JPEG_MARKER_APP0) {
838 l = (d[0] << 8) | d[1];
846 /* JPEG_MARKER_APP1 */
847 if (ds && d[0] == JPEG_MARKER_APP1)
850 /* Unknown marker or data. Give up. */
851 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
852 "ExifData", _("EXIF marker not found."));
861 len = (d[0] << 8) | d[1];
862 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
863 "We have to deal with %i byte(s) of EXIF data.",
870 * Verify the exif header
871 * (offset 2, length 6).
877 if (memcmp (d, ExifHeader, 6)) {
878 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
879 "ExifData", _("EXIF header not found."));
883 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
884 "Found EXIF header.");
886 /* Sanity check the data length */
890 /* The JPEG APP1 section can be no longer than 64 KiB (including a
891 16-bit length), so cap the data length to protect against overflow
892 in future offset calculations */
897 /* Byte order (offset 6, length 2) */
898 if (!memcmp (d + 6, "II", 2))
899 data->priv->order = EXIF_BYTE_ORDER_INTEL;
900 else if (!memcmp (d + 6, "MM", 2))
901 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
903 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
904 "ExifData", _("Unknown encoding."));
909 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
913 offset = exif_get_long (d + 10, data->priv->order);
914 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
915 "IFD 0 at %i.", (int) offset);
917 /* Sanity check the offset, being careful about overflow */
918 if (offset > ds || offset + 6 + 2 > ds)
921 /* Parse the actual exif data (usually offset 14 from start) */
922 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
925 n = exif_get_short (d + 6 + offset, data->priv->order);
926 if (offset + 6 + 2 + 12 * n + 4 > ds)
929 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
931 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
932 "IFD 1 at %i.", (int) offset);
935 if (offset > ds || offset + 6 > ds) {
936 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
937 "ExifData", "Bogus offset of IFD1.");
939 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
944 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
945 * cameras use pointers in the maker note tag that point to the
946 * space between IFDs. Here is the only place where we have access
949 interpret_maker_note(data, d, fullds);
951 /* Fixup tags if requested */
952 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
953 exif_data_fix (data);
957 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
960 *ds = 0; /* This means something went wrong */
962 if (!data || !d || !ds)
967 *d = exif_data_alloc (data, *ds);
972 memcpy (*d, ExifHeader, 6);
974 /* Order (offset 6) */
975 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
976 memcpy (*d + 6, "II", 2);
978 memcpy (*d + 6, "MM", 2);
981 /* Fixed value (2 bytes, offset 8) */
982 exif_set_short (*d + 8, data->priv->order, 0x002a);
985 * IFD 0 offset (4 bytes, offset 10).
986 * We will start 8 bytes after the
987 * EXIF header (2 bytes for order, another 2 for the test, and
988 * 4 bytes for the IFD 0 offset make 8 bytes together).
990 exif_set_long (*d + 10, data->priv->order, 8);
992 /* Now save IFD 0. IFD 1 will be saved automatically. */
993 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
995 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
997 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
998 "Saved %i byte(s) EXIF data.", *ds);
1002 exif_data_new_from_file (const char *path)
1007 loader = exif_loader_new ();
1008 exif_loader_write_file (loader, path);
1009 edata = exif_loader_get_data (loader);
1010 exif_loader_unref (loader);
1016 exif_data_ref (ExifData *data)
1021 data->priv->ref_count++;
1025 exif_data_unref (ExifData *data)
1030 data->priv->ref_count--;
1031 if (!data->priv->ref_count)
1032 exif_data_free (data);
1036 exif_data_free (ExifData *data)
1039 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1044 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1046 exif_content_unref (data->ifd[i]);
1047 data->ifd[i] = NULL;
1052 exif_mem_free (mem, data->data);
1057 if (data->priv->log) {
1058 exif_log_unref (data->priv->log);
1059 data->priv->log = NULL;
1061 if (data->priv->md) {
1062 exif_mnote_data_unref (data->priv->md);
1063 data->priv->md = NULL;
1065 exif_mem_free (mem, data->priv);
1066 exif_mem_free (mem, data);
1069 exif_mem_unref (mem);
1073 exif_data_dump (ExifData *data)
1080 for (i = 0; i < EXIF_IFD_COUNT; i++) {
1081 if (data->ifd[i] && data->ifd[i]->count) {
1082 printf ("Dumping IFD '%s'...\n",
1083 exif_ifd_get_name (i));
1084 exif_content_dump (data->ifd[i], 0);
1089 printf ("%i byte(s) thumbnail data available.", data->size);
1090 if (data->size >= 4) {
1091 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1092 data->data[0], data->data[1],
1093 data->data[data->size - 2],
1094 data->data[data->size - 1]);
1100 exif_data_get_byte_order (ExifData *data)
1105 return (data->priv->order);
1109 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1117 for (i = 0; i < EXIF_IFD_COUNT; i++)
1118 func (data->ifd[i], user_data);
1121 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1122 struct _ByteOrderChangeData {
1123 ExifByteOrder old, new;
1127 entry_set_byte_order (ExifEntry *e, void *data)
1129 ByteOrderChangeData *d = data;
1134 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1138 content_set_byte_order (ExifContent *content, void *data)
1140 exif_content_foreach_entry (content, entry_set_byte_order, data);
1144 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1146 ByteOrderChangeData d;
1148 if (!data || (order == data->priv->order))
1151 d.old = data->priv->order;
1153 exif_data_foreach_content (data, content_set_byte_order, &d);
1154 data->priv->order = order;
1156 exif_mnote_data_set_byte_order (data->priv->md, order);
1160 exif_data_log (ExifData *data, ExifLog *log)
1164 if (!data || !data->priv)
1166 exif_log_unref (data->priv->log);
1167 data->priv->log = log;
1170 for (i = 0; i < EXIF_IFD_COUNT; i++)
1171 exif_content_log (data->ifd[i], log);
1174 /* Used internally within libexif */
1175 ExifLog *exif_data_get_log (ExifData *);
1177 exif_data_get_log (ExifData *data)
1179 if (!data || !data->priv)
1181 return data->priv->log;
1184 static const struct {
1185 ExifDataOption option;
1187 const char *description;
1188 } exif_data_option[] = {
1189 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1190 N_("Ignore unknown tags when loading EXIF data.")},
1191 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1192 N_("Add, correct and remove entries to get EXIF data that follows "
1193 "the specification.")},
1194 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1195 N_("When loading and resaving Exif data, save the maker note unmodified."
1196 " Be aware that the maker note can get corrupted.")},
1201 exif_data_option_get_name (ExifDataOption o)
1205 for (i = 0; exif_data_option[i].name; i++)
1206 if (exif_data_option[i].option == o)
1208 return _(exif_data_option[i].name);
1212 exif_data_option_get_description (ExifDataOption o)
1216 for (i = 0; exif_data_option[i].description; i++)
1217 if (exif_data_option[i].option == o)
1219 return _(exif_data_option[i].description);
1223 exif_data_set_option (ExifData *d, ExifDataOption o)
1228 d->priv->options |= o;
1232 exif_data_unset_option (ExifData *d, ExifDataOption o)
1237 d->priv->options &= ~o;
1241 fix_func (ExifContent *c, void *UNUSED(data))
1243 switch (exif_content_get_ifd (c)) {
1245 if (c->parent->data)
1246 exif_content_fix (c);
1247 else if (c->count) {
1248 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1249 "No thumbnail but entries on thumbnail. These entries have been "
1252 unsigned int cnt = c->count;
1253 exif_content_remove_entry (c, c->entries[c->count - 1]);
1254 if (cnt == c->count) {
1256 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1257 "failed to remove last entry from entries.");
1264 exif_content_fix (c);
1269 exif_data_fix (ExifData *d)
1271 exif_data_foreach_content (d, fix_func, NULL);
1275 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1280 d->priv->data_type = dt;
1284 exif_data_get_data_type (ExifData *d)
1286 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;
1289 /* Used for making maker note data structure from external sources */
1291 exif_data_mnote_data_new(ExifData *d, Manufacturer maker, ExifDataOption o)
1303 md = exif_mnote_data_canon_new(mem, o);
1306 md = exif_mnote_data_fuji_new(mem);
1310 md = exif_mnote_data_olympus_new(mem);
1314 md = exif_mnote_data_pentax_new(mem);
1317 md = exif_mnote_data_samsung_new(mem);
1331 exif_data_mnote_set_mem_for_adding_entry(ExifMnoteData *md, Manufacturer maker)
1335 ExifMnoteDataSamsung *mds;
1353 mds = (ExifMnoteDataSamsung *) md;
1354 count = ++(mds->count);
1356 mds->entries = exif_mem_alloc(mem, sizeof(MnoteSamsungEntry) * count);
1358 EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteSamsung", count);
1364 MnoteSamsungEntry **e;
1365 e = exif_mem_realloc(mem, mds->entries, sizeof(MnoteSamsungEntry) * count);
1367 EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteSamsung", count);
1373 mds->entries[count-1].format = 0;
1382 exif_data_mnote_set_add_entry (ExifMnoteData* md, Manufacturer maker, int tag, ExifFormat fmt, int components, int id)
1385 ExifMnoteDataSamsung *mds;
1401 mds = (ExifMnoteDataSamsung *) md;
1403 mds->entries[count-1].tag = tag;
1404 mds->entries[count-1].format = fmt;
1405 mds->entries[count-1].components = components;
1406 mds->entries[count-1].size = exif_format_get_size(fmt*components);
1407 mds->entries[count-1].data = exif_mem_alloc(md->mem, mds->entries[count-1].size);
1408 mnote_samsung_entry_set_value_by_index(&mds->entries[count-1], id);
1417 exif_data_mnote_set_add_entry_subtag(ExifMnoteData* md, Manufacturer maker, int tag, ExifFormat fmt, int components, int subtag1, int id1, int subtag2, int id2, int val)
1420 ExifMnoteDataSamsung *mds;
1436 mds = (ExifMnoteDataSamsung *) md;
1438 mds->entries[count-1].tag = tag;
1439 mds->entries[count-1].format = fmt;
1440 mds->entries[count-1].components = components;
1441 mds->entries[count-1].size = exif_format_get_size(fmt*components);
1442 mds->entries[count-1].data = exif_mem_alloc(md->mem, mds->entries[count-1].size);
1443 mnote_samsung_entry_set_value_by_subtag(&mds->entries[count-1], subtag1, id1, subtag2, id2, val);
1452 exif_data_mnote_set_add_entry_string(ExifMnoteData* md, Manufacturer maker, int tag, ExifFormat fmt, int components, const char* string)
1455 ExifMnoteDataSamsung *mds;
1471 mds = (ExifMnoteDataSamsung *) md;
1473 mds->entries[count-1].tag = tag;
1474 mds->entries[count-1].format = fmt;
1475 mds->entries[count-1].components = components;
1476 mds->entries[count-1].size = exif_format_get_size(fmt*components);
1477 mds->entries[count-1].data = exif_mem_alloc(md->mem, mds->entries[count-1].size);
1478 mnote_samsung_entry_set_value_by_string(&mds->entries[count-1], string, components);