Coverage Report

Created: 2026-01-17 06:59

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