6332cd1ae3b0f6cc6db3694b25d3554ea81132c9
[platform/upstream/libexif.git] / libexif / exif-data.c
1 /* exif-data.c
2  *
3  * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <config.h>
22
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>
32
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
38 #include <math.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
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
49
50 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
51
52 struct _ExifDataPrivate
53 {
54         ExifByteOrder order;
55
56         ExifMnoteData *md;
57
58         ExifLog *log;
59         ExifMem *mem;
60
61         unsigned int ref_count;
62
63         /* Temporarily used while loading data */
64         unsigned int offset_mnote;
65
66         ExifDataOption options;
67         ExifDataType data_type;
68 };
69
70 static void *
71 exif_data_alloc (ExifData *data, unsigned int i)
72 {
73         void *d;
74
75         if (!data || !i) 
76                 return NULL;
77
78         d = exif_mem_alloc (data->priv->mem, i);
79         if (d) 
80                 return d;
81
82         EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
83         return NULL;
84 }
85
86 ExifMnoteData *
87 exif_data_get_mnote_data (ExifData *d)
88 {
89         return (d && d->priv) ? d->priv->md : NULL;
90 }
91
92 ExifData *
93 exif_data_new (void)
94 {
95         ExifMem *mem = exif_mem_new_default ();
96         ExifData *d = exif_data_new_mem (mem);
97
98         exif_mem_unref (mem);
99
100         return d;
101 }
102
103 ExifData *
104 exif_data_new_mem (ExifMem *mem)
105 {
106         ExifData *data;
107         unsigned int i;
108
109         if (!mem) 
110                 return NULL;
111
112         data = exif_mem_alloc (mem, sizeof (ExifData));
113         if (!data) 
114                 return (NULL);
115         data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
116         if (!data->priv) { 
117                 exif_mem_free (mem, data); 
118                 return (NULL); 
119         }
120         data->priv->ref_count = 1;
121
122         data->priv->mem = mem;
123         exif_mem_ref (mem);
124
125         for (i = 0; i < EXIF_IFD_COUNT; i++) {
126                 data->ifd[i] = exif_content_new_mem (data->priv->mem);
127                 if (!data->ifd[i]) {
128                         exif_data_free (data);
129                         return (NULL);
130                 }
131                 data->ifd[i]->parent = data;
132         }
133
134         /* Default options */
135 #ifndef NO_VERBOSE_TAG_STRINGS
136         /*
137          * When the tag list is compiled away, setting this option prevents
138          * any tags from being loaded
139          */
140         exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
141 #endif
142         exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
143
144         /* Default data type: none */
145         exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
146
147         return (data);
148 }
149
150 ExifData *
151 exif_data_new_from_data (const unsigned char *data, unsigned int size)
152 {
153         ExifData *edata;
154
155         edata = exif_data_new ();
156         exif_data_load_data (edata, data, size);
157         return (edata);
158 }
159
160 static int
161 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
162                            const unsigned char *d,
163                            unsigned int size, unsigned int offset)
164 {
165         unsigned int s, doff;
166
167         entry->tag        = exif_get_short (d + offset + 0, data->priv->order);
168         entry->format     = exif_get_short (d + offset + 2, data->priv->order);
169         entry->components = exif_get_long  (d + offset + 4, data->priv->order);
170
171         /* FIXME: should use exif_tag_get_name_in_ifd here but entry->parent 
172          * has not been set yet
173          */
174         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
175                   "Loading entry 0x%x ('%s')...", entry->tag,
176                   exif_tag_get_name (entry->tag));
177
178         /* {0,1,2,4,8} x { 0x00000000 .. 0xffffffff } 
179          *   -> { 0x000000000 .. 0x7fffffff8 } */
180         s = exif_format_get_size(entry->format) * entry->components;
181         if ((s < entry->components) || (s == 0)){
182                 return 0;
183         }
184
185         /*
186          * Size? If bigger than 4 bytes, the actual data is not
187          * in the entry but somewhere else (offset).
188          */
189         if (s > 4)
190                 doff = exif_get_long (d + offset + 8, data->priv->order);
191         else
192                 doff = offset + 8;
193
194         /* Sanity checks */
195         if (doff >= size) {
196                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
197                                   "Tag starts past end of buffer (%u > %u)", doff, size);
198                 return 0;
199         }
200
201         if (s > size - doff) {
202                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
203                                   "Tag data goes past end of buffer (%u > %u)", doff+s, size);
204                 return 0;
205         }
206
207         entry->data = exif_data_alloc (data, s);
208         if (entry->data) {
209                 entry->size = s;
210                 memcpy (entry->data, d + doff, s);
211         } else {
212                 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
213                 return 0;
214         }
215
216         /* If this is the MakerNote, remember the offset */
217         if (entry->tag == EXIF_TAG_MAKER_NOTE) {
218                 if (!entry->data) {
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],
228                                                entry->data[6]);
229                 }
230                 data->priv->offset_mnote = doff;
231         }
232         return 1;
233 }
234
235 static void
236 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
237                            unsigned char **d, unsigned int *ds,
238                            unsigned int offset)
239 {
240         unsigned int doff, s;
241         unsigned int ts;
242
243         if (!data || !data->priv) 
244                 return;
245
246         /*
247          * Each entry is 12 bytes long. The memory for the entry has
248          * already been allocated.
249          */
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);
254
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);
260                         e->data = NULL;
261                         e->size = 0;
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;
265                         if (exif_format_get_size (e->format) != 1) {
266                                 /* e->format is taken from input code,
267                                  * but we need to make sure it is a 1 byte
268                                  * entity due to the multiplication below. */
269                                 e->format = EXIF_FORMAT_UNDEFINED;
270                         }
271                 }
272         }
273
274         exif_set_long  (*d + 6 + offset + 4,
275                         data->priv->order, e->components);
276
277         /*
278          * Size? If bigger than 4 bytes, the actual data is not in
279          * the entry but somewhere else.
280          */
281         s = exif_format_get_size (e->format) * e->components;
282         if (s > 4) {
283                 unsigned char *t;
284                 doff = *ds - 6;
285                 ts = *ds + s;
286
287                 /*
288                  * According to the TIFF specification,
289                  * the offset must be an even number. If we need to introduce
290                  * a padding byte, we set it to 0.
291                  */
292                 if (s & 1)
293                         ts++;
294                 t = exif_mem_realloc (data->priv->mem, *d, ts);
295                 if (!t) {
296                         EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
297                         return;
298                 }
299                 *d = t;
300                 *ds = ts;
301                 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
302                 if (s & 1) 
303                         *(*d + *ds - 1) = '\0';
304
305         } else
306                 doff = offset + 8;
307
308         /* Write the data. Fill unneeded bytes with 0. Do not crash with
309          * e->data is NULL */
310         if (e->data) {
311                 memcpy (*d + 6 + doff, e->data, s);
312         } else {
313                 memset (*d + 6 + doff, 0, s);
314         }
315         if (s < 4) 
316                 memset (*d + 6 + doff + s, 0, (4 - s));
317 }
318
319 static void
320 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
321                                unsigned int ds, ExifLong o, ExifLong s)
322 {
323         /* Sanity checks */
324         if (o >= ds) {
325                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", "Bogus thumbnail offset (%u).", o);
326                 return;
327         }
328         if (s > ds - o) {
329                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", "Bogus thumbnail size (%u), max would be %u.", s, ds-o);
330                 return;
331         }
332         if (data->data) 
333                 exif_mem_free (data->priv->mem, data->data);
334         if (!(data->data = exif_data_alloc (data, s))) {
335                 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
336                 data->size = 0;
337                 return;
338         }
339         data->size = s;
340         memcpy (data->data, d + o, s);
341 }
342
343 #undef CHECK_REC
344 #define CHECK_REC(i)                                    \
345 if ((i) == ifd) {                               \
346         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
347                 "ExifData", "Recursive entry in IFD "   \
348                 "'%s' detected. Skipping...",           \
349                 exif_ifd_get_name (i));                 \
350         break;                                          \
351 }                                                       \
352 if (data->ifd[(i)]->count) {                            \
353         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
354                 "ExifData", "Attempt to load IFD "      \
355                 "'%s' multiple times detected. "        \
356                 "Skipping...",                          \
357                 exif_ifd_get_name (i));                 \
358         break;                                          \
359 }
360
361 /*! Calculate the recursion cost added by one level of IFD loading.
362  *
363  * The work performed is related to the cost in the exponential relation
364  *   work=1.1**cost
365  */
366 static unsigned int
367 level_cost(unsigned int n)
368 {
369     static const double log_1_1 = 0.09531017980432493;
370
371         /* Adding 0.1 protects against the case where n==1 */
372         return ceil(log(n + 0.1)/log_1_1);
373 }
374
375 /*! Load data for an IFD.
376  *
377  * \param[in,out] data #ExifData
378  * \param[in] ifd IFD to load
379  * \param[in] d pointer to buffer containing raw IFD data
380  * \param[in] ds size of raw data in buffer at \c d
381  * \param[in] offset offset into buffer at \c d at which IFD starts
382  * \param[in] recursion_cost factor indicating how expensive this recursive
383  * call could be
384  */
385 static void
386 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
387                              const unsigned char *d,
388                              unsigned int ds, unsigned int offset, unsigned int recursion_cost)
389 {
390         ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
391         ExifShort n;
392         ExifEntry *entry;
393         unsigned int i;
394         ExifTag tag;
395
396         if (!data || !data->priv) 
397                 return;
398
399         /* check for valid ExifIfd enum range */
400         if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
401           return;
402
403         if (recursion_cost > 170) {
404                 /*
405                  * recursion_cost is a logarithmic-scale indicator of how expensive this
406                  * recursive call might end up being. It is an indicator of the depth of
407                  * recursion as well as the potential for worst-case future recursive
408                  * calls. Since it's difficult to tell ahead of time how often recursion
409                  * will occur, this assumes the worst by assuming every tag could end up
410                  * causing recursion.
411                  * The value of 170 was chosen to limit typical EXIF structures to a
412                  * recursive depth of about 6, but pathological ones (those with very
413                  * many tags) to only 2.
414                  */
415                 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
416                           "Deep/expensive recursion detected!");
417                 return;
418         }
419
420         /* Read the number of entries */
421         if ((offset + 2 < offset) || (offset + 2 < 2) || (offset + 2 > ds)) {
422                 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
423                           "Tag data past end of buffer (%u > %u)", offset+2, ds);
424                 return;
425         }
426         n = exif_get_short (d + offset, data->priv->order);
427         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
428                   "Loading %hu entries...", n);
429         offset += 2;
430
431         /* Check if we have enough data. */
432         if (offset + 12 * n > ds) {
433                 n = (ds - offset) / 12;
434                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
435                                   "Short data; only loading %hu entries...", n);
436         }
437
438         for (i = 0; i < n; i++) {
439
440                 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
441                 switch (tag) {
442                 case EXIF_TAG_EXIF_IFD_POINTER:
443                 case EXIF_TAG_GPS_INFO_IFD_POINTER:
444                 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
445                 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
446                 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
447                         o = exif_get_long (d + offset + 12 * i + 8,
448                                            data->priv->order);
449                         /* FIXME: IFD_POINTER tags aren't marked as being in a
450                          * specific IFD, so exif_tag_get_name_in_ifd won't work
451                          */
452                         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
453                                   "Sub-IFD entry 0x%x ('%s') at %u.", tag,
454                                   exif_tag_get_name(tag), o);
455                         switch (tag) {
456                         case EXIF_TAG_EXIF_IFD_POINTER:
457                                 CHECK_REC (EXIF_IFD_EXIF);
458                                 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o,
459                                         recursion_cost + level_cost(n));
460                                 break;
461                         case EXIF_TAG_GPS_INFO_IFD_POINTER:
462                                 CHECK_REC (EXIF_IFD_GPS);
463                                 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o,
464                                         recursion_cost + level_cost(n));
465                                 break;
466                         case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
467                                 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
468                                 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o,
469                                         recursion_cost + level_cost(n));
470                                 break;
471                         case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
472                                 thumbnail_offset = o;
473                                 if (thumbnail_offset && thumbnail_length)
474                                         exif_data_load_data_thumbnail (data, d,
475                                                                        ds, thumbnail_offset,
476                                                                        thumbnail_length);
477                                 break;
478                         case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
479                                 thumbnail_length = o;
480                                 if (thumbnail_offset && thumbnail_length)
481                                         exif_data_load_data_thumbnail (data, d,
482                                                                        ds, thumbnail_offset,
483                                                                        thumbnail_length);
484                                 break;
485                         default:
486                                 return;
487                         }
488                         break;
489                 default:
490
491                         /*
492                          * If we don't know the tag, don't fail. It could be that new 
493                          * versions of the standard have defined additional tags. Note that
494                          * 0 is a valid tag in the GPS IFD.
495                          */
496                         if (!exif_tag_get_name_in_ifd (tag, ifd)) {
497
498                                 /*
499                                  * Special case: Tag and format 0. That's against specification
500                                  * (at least up to 2.2). But Photoshop writes it anyways.
501                                  */
502                                 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
503                                         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
504                                                   "Skipping empty entry at position %u in '%s'.", i, 
505                                                   exif_ifd_get_name (ifd));
506                                         break;
507                                 }
508                                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
509                                           "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
510                                           "to <libexif-devel@lists.sourceforge.net>.", tag, i,
511                                           exif_ifd_get_name (ifd));
512                                 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
513                                         break;
514                         }
515                         entry = exif_entry_new_mem (data->priv->mem);
516                         if (!entry) {
517                                   exif_log (data->priv->log, EXIF_LOG_CODE_NO_MEMORY, "ExifData",
518                                           "Could not allocate memory");
519                                   return;
520                         }
521                         if (exif_data_load_data_entry (data, entry, d, ds,
522                                                    offset + 12 * i))
523                                 exif_content_add_entry (data->ifd[ifd], entry);
524                         exif_entry_unref (entry);
525                         break;
526                 }
527         }
528 }
529
530 static int
531 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
532 {
533         ExifShort tag1 = exif_get_short (p1, o);
534         ExifShort tag2 = exif_get_short (p2, o);
535
536         return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
537 }
538
539 static int
540 cmp_func_intel (const void *elem1, const void *elem2)
541 {
542         return cmp_func ((const unsigned char *) elem1,
543                          (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
544 }
545
546 static int
547 cmp_func_motorola (const void *elem1, const void *elem2)
548 {
549         return cmp_func ((const unsigned char *) elem1,
550                          (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
551 }
552
553 static void
554 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
555                              unsigned char **d, unsigned int *ds,
556                              unsigned int offset)
557 {
558         unsigned int j, n_ptr = 0, n_thumb = 0;
559         ExifIfd i;
560         unsigned char *t;
561         unsigned int ts;
562
563         if (!data || !data->priv || !ifd || !d || !ds) 
564                 return;
565
566         for (i = 0; i < EXIF_IFD_COUNT; i++)
567                 if (ifd == data->ifd[i])
568                         break;
569         if (i == EXIF_IFD_COUNT)
570                 return; /* error */
571
572         /*
573          * Check if we need some extra entries for pointers or the thumbnail.
574          */
575         switch (i) {
576         case EXIF_IFD_0:
577
578                 /*
579                  * The pointer to IFD_EXIF is in IFD_0. The pointer to
580                  * IFD_INTEROPERABILITY is in IFD_EXIF.
581                  */
582                 if (data->ifd[EXIF_IFD_EXIF]->count ||
583                     data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
584                         n_ptr++;
585
586                 /* The pointer to IFD_GPS is in IFD_0. */
587                 if (data->ifd[EXIF_IFD_GPS]->count)
588                         n_ptr++;
589
590                 break;
591         case EXIF_IFD_1:
592                 if (data->size)
593                         n_thumb = 2;
594                 break;
595         case EXIF_IFD_EXIF:
596                 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
597                         n_ptr++;
598         default:
599                 break;
600         }
601
602         /*
603          * Allocate enough memory for all entries
604          * and the number of entries.
605          */
606         ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
607         t = exif_mem_realloc (data->priv->mem, *d, ts);
608         if (!t) {
609                 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
610                 return;
611         }
612         *d = t;
613         *ds = ts;
614
615         /* Save the number of entries */
616         exif_set_short (*d + 6 + offset, data->priv->order,
617                         (ExifShort) (ifd->count + n_ptr + n_thumb));
618         offset += 2;
619
620         /*
621          * Save each entry. Make sure that no memcpys from NULL pointers are
622          * performed
623          */
624         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
625                   "Saving %i entries (IFD '%s', offset: %i)...",
626                   ifd->count, exif_ifd_get_name (i), offset);
627         for (j = 0; j < ifd->count; j++) {
628                 if (ifd->entries[j]) {
629                         exif_data_save_data_entry (data, ifd->entries[j], d, ds,
630                                 offset + 12 * j);
631                 }
632         }
633
634         offset += 12 * ifd->count;
635
636         /* Now save special entries. */
637         switch (i) {
638         case EXIF_IFD_0:
639
640                 /*
641                  * The pointer to IFD_EXIF is in IFD_0.
642                  * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
643                  * therefore, if IFD_INTEROPERABILITY is not empty, we need
644                  * IFD_EXIF even if latter is empty.
645                  */
646                 if (data->ifd[EXIF_IFD_EXIF]->count ||
647                     data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
648                         exif_set_short (*d + 6 + offset + 0, data->priv->order,
649                                         EXIF_TAG_EXIF_IFD_POINTER);
650                         exif_set_short (*d + 6 + offset + 2, data->priv->order,
651                                         EXIF_FORMAT_LONG);
652                         exif_set_long  (*d + 6 + offset + 4, data->priv->order,
653                                         1);
654                         exif_set_long  (*d + 6 + offset + 8, data->priv->order,
655                                         *ds - 6);
656                         exif_data_save_data_content (data,
657                                                      data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
658                         offset += 12;
659                 }
660
661                 /* The pointer to IFD_GPS is in IFD_0, too. */
662                 if (data->ifd[EXIF_IFD_GPS]->count) {
663                         exif_set_short (*d + 6 + offset + 0, data->priv->order,
664                                         EXIF_TAG_GPS_INFO_IFD_POINTER);
665                         exif_set_short (*d + 6 + offset + 2, data->priv->order,
666                                         EXIF_FORMAT_LONG);
667                         exif_set_long  (*d + 6 + offset + 4, data->priv->order,
668                                         1);
669                         exif_set_long  (*d + 6 + offset + 8, data->priv->order,
670                                         *ds - 6);
671                         exif_data_save_data_content (data,
672                                                      data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
673                         offset += 12;
674                 }
675
676                 break;
677         case EXIF_IFD_EXIF:
678
679                 /*
680                  * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
681                  * See note above.
682                  */
683                 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
684                         exif_set_short (*d + 6 + offset + 0, data->priv->order,
685                                         EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
686                         exif_set_short (*d + 6 + offset + 2, data->priv->order,
687                                         EXIF_FORMAT_LONG);
688                         exif_set_long  (*d + 6 + offset + 4, data->priv->order,
689                                         1);
690                         exif_set_long  (*d + 6 + offset + 8, data->priv->order,
691                                         *ds - 6);
692                         exif_data_save_data_content (data,
693                                                      data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
694                                                      *ds - 6);
695                         offset += 12;
696                 }
697
698                 break;
699         case EXIF_IFD_1:
700
701                 /*
702                  * Information about the thumbnail (if any) is saved in
703                  * IFD_1.
704                  */
705                 if (data->size) {
706
707                         /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
708                         exif_set_short (*d + 6 + offset + 0, data->priv->order,
709                                         EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
710                         exif_set_short (*d + 6 + offset + 2, data->priv->order,
711                                         EXIF_FORMAT_LONG);
712                         exif_set_long  (*d + 6 + offset + 4, data->priv->order,
713                                         1);
714                         exif_set_long  (*d + 6 + offset + 8, data->priv->order,
715                                         *ds - 6);
716                         ts = *ds + data->size;
717                         t = exif_mem_realloc (data->priv->mem, *d, ts);
718                         if (!t) {
719                                 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
720                                                     ts);
721                                 return;
722                         }
723                         *d = t;
724                         *ds = ts;
725                         memcpy (*d + *ds - data->size, data->data, data->size);
726                         offset += 12;
727
728                         /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
729                         exif_set_short (*d + 6 + offset + 0, data->priv->order,
730                                         EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
731                         exif_set_short (*d + 6 + offset + 2, data->priv->order,
732                                         EXIF_FORMAT_LONG);
733                         exif_set_long  (*d + 6 + offset + 4, data->priv->order,
734                                         1);
735                         exif_set_long  (*d + 6 + offset + 8, data->priv->order,
736                                         data->size);
737                         offset += 12;
738                 }
739
740                 break;
741         default:
742                 break;
743         }
744
745         /* Sort the directory according to TIFF specification */
746         qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
747                (ifd->count + n_ptr + n_thumb), 12,
748                (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
749
750         /* Correctly terminate the directory */
751         if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
752                                 data->size)) {
753
754                 /*
755                  * We are saving IFD 0. Tell where IFD 1 starts and save
756                  * IFD 1.
757                  */
758                 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
759                 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
760                                              *ds - 6);
761         } else
762                 exif_set_long (*d + 6 + offset, data->priv->order, 0);
763 }
764
765 typedef enum {
766         EXIF_DATA_TYPE_MAKER_NOTE_NONE          = 0,
767         EXIF_DATA_TYPE_MAKER_NOTE_CANON         = 1,
768         EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS       = 2,
769         EXIF_DATA_TYPE_MAKER_NOTE_PENTAX        = 3,
770         EXIF_DATA_TYPE_MAKER_NOTE_NIKON         = 4,
771         EXIF_DATA_TYPE_MAKER_NOTE_CASIO         = 5,
772         EXIF_DATA_TYPE_MAKER_NOTE_FUJI          = 6
773 } ExifDataTypeMakerNote;
774
775 /*! If MakerNote is recognized, load it.
776  *
777  * \param[in,out] data #ExifData
778  * \param[in] d pointer to raw EXIF data
779  * \param[in] ds length of data at d
780  */
781 static void
782 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
783 {
784         int mnoteid;
785         ExifEntry* e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
786         if (!e)
787                 return;
788         
789         if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
790                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
791                         "ExifData", "Olympus MakerNote variant type %d", mnoteid);
792                 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
793
794         } else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
795                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
796                         "ExifData", "Canon MakerNote variant type %d", mnoteid);
797                 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
798
799         } else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
800                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
801                         "ExifData", "Fuji MakerNote variant type %d", mnoteid);
802                 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
803
804         /* NOTE: Must do Pentax detection last because some of the
805          * heuristics are pretty general. */
806         } else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
807                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
808                         "ExifData", "Pentax MakerNote variant type %d", mnoteid);
809                 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
810         }
811
812         /* 
813          * If we are able to interpret the maker note, do so.
814          */
815         if (data->priv->md) {
816                 exif_mnote_data_log (data->priv->md, data->priv->log);
817                 exif_mnote_data_set_byte_order (data->priv->md,
818                                                 data->priv->order);
819                 exif_mnote_data_set_offset (data->priv->md,
820                                             data->priv->offset_mnote);
821                 exif_mnote_data_load (data->priv->md, d, ds);
822         }
823 }
824
825 #define LOG_TOO_SMALL \
826 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
827                 _("Size of data too small to allow for EXIF data."));
828
829 void
830 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
831                      unsigned int ds)
832 {
833         unsigned int l;
834         ExifLong offset;
835         ExifShort n;
836         const unsigned char *d = d_orig;
837         unsigned int len, fullds;
838
839         if (!data || !data->priv || !d || !ds)
840                 return;
841
842         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
843                   "Parsing %i byte(s) EXIF data...\n", ds);
844
845         /*
846          * It can be that the data starts with the EXIF header. If it does
847          * not, search the EXIF marker.
848          */
849         if (ds < 6) {
850                 LOG_TOO_SMALL;
851                 return;
852         }
853         if (!memcmp (d, ExifHeader, 6)) {
854                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
855                           "Found EXIF header at start.");
856         } else {
857                 while (ds >= 3) {
858                         while (ds && (d[0] == 0xff)) {
859                                 d++;
860                                 ds--;
861                         }
862
863                         /* JPEG_MARKER_SOI */
864                         if (ds && d[0] == JPEG_MARKER_SOI) {
865                                 d++;
866                                 ds--;
867                                 continue;
868                         }
869
870                         /* JPEG_MARKER_APP1 */
871                         if (ds && d[0] == JPEG_MARKER_APP1)
872                                 break;
873
874                         /* Skip irrelevant APP markers. The branch for APP1 must come before this,
875                            otherwise this code block will cause APP1 to be skipped. This code path
876                            is only relevant for files that are nonconformant to the EXIF
877                            specification. For conformant files, the APP1 code path above will be
878                            taken. */
879                         if (ds >= 3 && d[0] >= 0xe0 && d[0] <= 0xef) {  /* JPEG_MARKER_APPn */
880                                 d++;
881                                 ds--;
882                                 l = (d[0] << 8) | d[1];
883                                 if (l > ds)
884                                         return;
885                                 d += l;
886                                 ds -= l;
887                                 continue;
888                         }
889
890                         /* Unknown marker or data. Give up. */
891                         exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
892                                   "ExifData", _("EXIF marker not found."));
893                         return;
894                 }
895                 if (ds < 3) {
896                         LOG_TOO_SMALL;
897                         return;
898                 }
899                 d++;
900                 ds--;
901                 len = (d[0] << 8) | d[1];
902                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
903                           "We have to deal with %i byte(s) of EXIF data.",
904                           len);
905                 d += 2;
906                 ds -= 2;
907         }
908
909         /*
910          * Verify the exif header
911          * (offset 2, length 6).
912          */
913         if (ds < 6) {
914                 LOG_TOO_SMALL;
915                 return;
916         }
917         if (memcmp (d, ExifHeader, 6)) {
918                 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
919                           "ExifData", _("EXIF header not found."));
920                 return;
921         }
922
923         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
924                   "Found EXIF header.");
925
926         /* Sanity check the data length */
927         if (ds < 14)
928                 return;
929
930         /* The JPEG APP1 section can be no longer than 64 KiB (including a
931            16-bit length), so cap the data length to protect against overflow
932            in future offset calculations */
933         fullds = ds;
934         if (ds > 0xfffe)
935                 ds = 0xfffe;
936
937         /* Byte order (offset 6, length 2) */
938         if (!memcmp (d + 6, "II", 2))
939                 data->priv->order = EXIF_BYTE_ORDER_INTEL;
940         else if (!memcmp (d + 6, "MM", 2))
941                 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
942         else {
943                 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
944                           "ExifData", _("Unknown encoding."));
945                 return;
946         }
947
948         /* Fixed value */
949         if (exif_get_short (d + 8, data->priv->order) != 0x002a)
950                 return;
951
952         /* IFD 0 offset */
953         offset = exif_get_long (d + 10, data->priv->order);
954         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData", 
955                   "IFD 0 at %i.", (int) offset);
956
957         /* ds is restricted to 16 bit above, so offset is restricted too, and offset+8 should not overflow. */
958         if (offset > ds || offset + 6 + 2 > ds)
959                 return;
960
961         /* Parse the actual exif data (usually offset 14 from start) */
962         exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
963
964         /* IFD 1 offset */
965         n = exif_get_short (d + 6 + offset, data->priv->order);
966         /* offset < 2<<16, n is 16 bit at most, so this op will not overflow */
967         if (offset + 6 + 2 + 12 * n + 4 > ds)
968                 return;
969
970         offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
971         if (offset) {
972                 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
973                           "IFD 1 at %i.", (int) offset);
974
975                 /* Sanity check. ds is ensured to be above 6 above, offset is 16bit */
976                 if (offset > ds - 6) {
977                         exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
978                                   "ExifData", "Bogus offset of IFD1.");
979                 } else {
980                    exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
981                 }
982         }
983
984         /*
985          * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
986          * cameras use pointers in the maker note tag that point to the
987          * space between IFDs. Here is the only place where we have access
988          * to that data.
989          */
990         interpret_maker_note(data, d, fullds);
991
992         /* Fixup tags if requested */
993         if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
994                 exif_data_fix (data);
995 }
996
997 void
998 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
999 {
1000         if (ds)
1001                 *ds = 0;        /* This means something went wrong */
1002
1003         if (!data || !d || !ds)
1004                 return;
1005
1006         /* Header */
1007         *ds = 14;
1008         *d = exif_data_alloc (data, *ds);
1009         if (!*d)  {
1010                 *ds = 0;
1011                 return;
1012         }
1013         memcpy (*d, ExifHeader, 6);
1014
1015         /* Order (offset 6) */
1016         if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
1017                 memcpy (*d + 6, "II", 2);
1018         } else {
1019                 memcpy (*d + 6, "MM", 2);
1020         }
1021
1022         /* Fixed value (2 bytes, offset 8) */
1023         exif_set_short (*d + 8, data->priv->order, 0x002a);
1024
1025         /*
1026          * IFD 0 offset (4 bytes, offset 10).
1027          * We will start 8 bytes after the
1028          * EXIF header (2 bytes for order, another 2 for the test, and
1029          * 4 bytes for the IFD 0 offset make 8 bytes together).
1030          */
1031         exif_set_long (*d + 10, data->priv->order, 8);
1032
1033         /* Now save IFD 0. IFD 1 will be saved automatically. */
1034         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1035                   "Saving IFDs...");
1036         exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
1037                                      *ds - 6);
1038         exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
1039                   "Saved %i byte(s) EXIF data.", *ds);
1040 }
1041
1042 ExifData *
1043 exif_data_new_from_file (const char *path)
1044 {
1045         ExifData *edata;
1046         ExifLoader *loader;
1047
1048         loader = exif_loader_new ();
1049         exif_loader_write_file (loader, path);
1050         edata = exif_loader_get_data (loader);
1051         exif_loader_unref (loader);
1052
1053         return (edata);
1054 }
1055
1056 void
1057 exif_data_ref (ExifData *data)
1058 {
1059         if (!data)
1060                 return;
1061
1062         data->priv->ref_count++;
1063 }
1064
1065 void
1066 exif_data_unref (ExifData *data)
1067 {
1068         if (!data) 
1069                 return;
1070
1071         data->priv->ref_count--;
1072         if (!data->priv->ref_count) 
1073                 exif_data_free (data);
1074 }
1075
1076 void
1077 exif_data_free (ExifData *data)
1078 {
1079         unsigned int i;
1080         ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
1081
1082         if (!data) 
1083                 return;
1084
1085         for (i = 0; i < EXIF_IFD_COUNT; i++) {
1086                 if (data->ifd[i]) {
1087                         exif_content_unref (data->ifd[i]);
1088                         data->ifd[i] = NULL;
1089                 }
1090         }
1091
1092         if (data->data) {
1093                 exif_mem_free (mem, data->data);
1094                 data->data = NULL;
1095         }
1096
1097         if (data->priv) {
1098                 if (data->priv->log) {
1099                         exif_log_unref (data->priv->log);
1100                         data->priv->log = NULL;
1101                 }
1102                 if (data->priv->md) {
1103                         exif_mnote_data_unref (data->priv->md);
1104                         data->priv->md = NULL;
1105                 }
1106                 exif_mem_free (mem, data->priv);
1107                 exif_mem_free (mem, data);
1108         }
1109
1110         exif_mem_unref (mem);
1111 }
1112
1113 void
1114 exif_data_dump (ExifData *data)
1115 {
1116         unsigned int i;
1117
1118         if (!data)
1119                 return;
1120
1121         for (i = 0; i < EXIF_IFD_COUNT; i++) {
1122                 if (data->ifd[i] && data->ifd[i]->count) {
1123                         printf ("Dumping IFD '%s'...\n",
1124                                 exif_ifd_get_name (i));
1125                         exif_content_dump (data->ifd[i], 0);
1126                 }
1127         }
1128
1129         if (data->data) {
1130                 printf ("%i byte(s) thumbnail data available: ", data->size);
1131                 if (data->size >= 4) {
1132                         printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
1133                                 data->data[0], data->data[1],
1134                                 data->data[data->size - 2],
1135                                 data->data[data->size - 1]);
1136                 }
1137         }
1138 }
1139
1140 ExifByteOrder
1141 exif_data_get_byte_order (ExifData *data)
1142 {
1143         if (!data)
1144                 return (0);
1145
1146         return (data->priv->order);
1147 }
1148
1149 void
1150 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1151                            void *user_data)
1152 {
1153         unsigned int i;
1154
1155         if (!data || !func)
1156                 return;
1157
1158         for (i = 0; i < EXIF_IFD_COUNT; i++)
1159                 func (data->ifd[i], user_data);
1160 }
1161
1162 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1163 struct _ByteOrderChangeData {
1164         ExifByteOrder old, new;
1165 };
1166
1167 static void
1168 entry_set_byte_order (ExifEntry *e, void *data)
1169 {
1170         ByteOrderChangeData *d = data;
1171
1172         if (!e)
1173                 return;
1174
1175         exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1176 }
1177
1178 static void
1179 content_set_byte_order (ExifContent *content, void *data)
1180 {
1181         exif_content_foreach_entry (content, entry_set_byte_order, data);
1182 }
1183
1184 void
1185 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1186 {
1187         ByteOrderChangeData d;
1188
1189         if (!data || (order == data->priv->order))
1190                 return;
1191
1192         d.old = data->priv->order;
1193         d.new = order;
1194         exif_data_foreach_content (data, content_set_byte_order, &d);
1195         data->priv->order = order;
1196         if (data->priv->md)
1197                 exif_mnote_data_set_byte_order (data->priv->md, order);
1198 }
1199
1200 void
1201 exif_data_log (ExifData *data, ExifLog *log)
1202 {
1203         unsigned int i;
1204
1205         if (!data || !data->priv) 
1206                 return;
1207         exif_log_unref (data->priv->log);
1208         data->priv->log = log;
1209         exif_log_ref (log);
1210
1211         for (i = 0; i < EXIF_IFD_COUNT; i++)
1212                 exif_content_log (data->ifd[i], log);
1213 }
1214
1215 /* Used internally within libexif */
1216 ExifLog *exif_data_get_log (ExifData *);
1217 ExifLog *
1218 exif_data_get_log (ExifData *data)
1219 {
1220         if (!data || !data->priv) 
1221                 return NULL;
1222         return data->priv->log;
1223 }
1224
1225 static const struct {
1226         ExifDataOption option;
1227         const char *name;
1228         const char *description;
1229 } exif_data_option[] = {
1230         {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1231          N_("Ignore unknown tags when loading EXIF data.")},
1232         {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1233          N_("Add, correct and remove entries to get EXIF data that follows "
1234             "the specification.")},
1235         {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
1236          N_("When loading and resaving Exif data, save the maker note unmodified."
1237             " Be aware that the maker note can get corrupted.")},
1238         {0, NULL, NULL}
1239 };
1240
1241 const char *
1242 exif_data_option_get_name (ExifDataOption o)
1243 {
1244         unsigned int i;
1245
1246         for (i = 0; exif_data_option[i].name; i++)
1247                 if (exif_data_option[i].option == o) 
1248                         break;
1249         return _(exif_data_option[i].name);
1250 }
1251
1252 const char *
1253 exif_data_option_get_description (ExifDataOption o)
1254 {
1255         unsigned int i;
1256
1257         for (i = 0; exif_data_option[i].description; i++)
1258                 if (exif_data_option[i].option == o) 
1259                         break;
1260         return _(exif_data_option[i].description);
1261 }
1262
1263 void
1264 exif_data_set_option (ExifData *d, ExifDataOption o)
1265 {
1266         if (!d) 
1267                 return;
1268
1269         d->priv->options |= o;
1270 }
1271
1272 void
1273 exif_data_unset_option (ExifData *d, ExifDataOption o)
1274 {
1275         if (!d) 
1276                 return;
1277
1278         d->priv->options &= ~o;
1279 }
1280
1281 static void
1282 fix_func (ExifContent *c, void *UNUSED(data))
1283 {
1284         switch (exif_content_get_ifd (c)) {
1285         case EXIF_IFD_1:
1286                 if (c->parent->data)
1287                         exif_content_fix (c);
1288                 else if (c->count) {
1289                         exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1290                                   "No thumbnail but entries on thumbnail. These entries have been "
1291                                   "removed.");
1292                         while (c->count) {
1293                                 unsigned int cnt = c->count;
1294                                 exif_content_remove_entry (c, c->entries[c->count - 1]);
1295                                 if (cnt == c->count) {
1296                                         /* safety net */
1297                                         exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1298                                         "failed to remove last entry from entries.");
1299                                         c->count--;
1300                                 }
1301                         }
1302                 }
1303                 break;
1304         default:
1305                 exif_content_fix (c);
1306         }
1307 }
1308
1309 void
1310 exif_data_fix (ExifData *d)
1311 {
1312         exif_data_foreach_content (d, fix_func, NULL);
1313 }
1314
1315 void
1316 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1317 {
1318         if (!d || !d->priv) 
1319                 return;
1320
1321         d->priv->data_type = dt;
1322 }
1323
1324 ExifDataType
1325 exif_data_get_data_type (ExifData *d)
1326 {
1327         return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;
1328 }