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