00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <libexif/exif-mnote-data.h>
00024 #include <libexif/exif-data.h>
00025 #include <libexif/exif-ifd.h>
00026 #include <libexif/exif-mnote-data-priv.h>
00027 #include <libexif/exif-utils.h>
00028 #include <libexif/exif-loader.h>
00029 #include <libexif/exif-log.h>
00030 #include <libexif/i18n.h>
00031 #include <libexif/exif-system.h>
00032
00033 #include <libexif/canon/exif-mnote-data-canon.h>
00034 #include <libexif/fuji/exif-mnote-data-fuji.h>
00035 #include <libexif/olympus/exif-mnote-data-olympus.h>
00036 #include <libexif/pentax/exif-mnote-data-pentax.h>
00037
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <string.h>
00041
00042 #undef JPEG_MARKER_SOI
00043 #define JPEG_MARKER_SOI 0xd8
00044 #undef JPEG_MARKER_APP0
00045 #define JPEG_MARKER_APP0 0xe0
00046 #undef JPEG_MARKER_APP1
00047 #define JPEG_MARKER_APP1 0xe1
00048
00049 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
00050
00051 struct _ExifDataPrivate
00052 {
00053 ExifByteOrder order;
00054
00055 ExifMnoteData *md;
00056
00057 ExifLog *log;
00058 ExifMem *mem;
00059
00060 unsigned int ref_count;
00061
00062
00063 unsigned int offset_mnote;
00064
00065 ExifDataOption options;
00066 ExifDataType data_type;
00067 };
00068
00069 static void *
00070 exif_data_alloc (ExifData *data, unsigned int i)
00071 {
00072 void *d;
00073
00074 if (!data || !i)
00075 return NULL;
00076
00077 d = exif_mem_alloc (data->priv->mem, i);
00078 if (d)
00079 return d;
00080
00081 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
00082 return NULL;
00083 }
00084
00085 ExifMnoteData *
00086 exif_data_get_mnote_data (ExifData *d)
00087 {
00088 return (d && d->priv) ? d->priv->md : NULL;
00089 }
00090
00091 ExifData *
00092 exif_data_new (void)
00093 {
00094 ExifMem *mem = exif_mem_new_default ();
00095 ExifData *d = exif_data_new_mem (mem);
00096
00097 exif_mem_unref (mem);
00098
00099 return d;
00100 }
00101
00102 ExifData *
00103 exif_data_new_mem (ExifMem *mem)
00104 {
00105 ExifData *data;
00106 unsigned int i;
00107
00108 if (!mem)
00109 return NULL;
00110
00111 data = exif_mem_alloc (mem, sizeof (ExifData));
00112 if (!data)
00113 return (NULL);
00114 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
00115 if (!data->priv) {
00116 exif_mem_free (mem, data);
00117 return (NULL);
00118 }
00119 data->priv->ref_count = 1;
00120
00121 data->priv->mem = mem;
00122 exif_mem_ref (mem);
00123
00124 for (i = 0; i < EXIF_IFD_COUNT; i++) {
00125 data->ifd[i] = exif_content_new_mem (data->priv->mem);
00126 if (!data->ifd[i]) {
00127 exif_data_free (data);
00128 return (NULL);
00129 }
00130 data->ifd[i]->parent = data;
00131 }
00132
00133
00134 #ifndef NO_VERBOSE_TAG_STRINGS
00135
00136
00137
00138
00139 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
00140 #endif
00141 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
00142
00143
00144 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
00145
00146 return (data);
00147 }
00148
00149 ExifData *
00150 exif_data_new_from_data (const unsigned char *data, unsigned int size)
00151 {
00152 ExifData *edata;
00153
00154 edata = exif_data_new ();
00155 exif_data_load_data (edata, data, size);
00156 return (edata);
00157 }
00158
00159 static int
00160 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
00161 const unsigned char *d,
00162 unsigned int size, unsigned int offset)
00163 {
00164 unsigned int s, doff;
00165
00166 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
00167 entry->format = exif_get_short (d + offset + 2, data->priv->order);
00168 entry->components = exif_get_long (d + offset + 4, data->priv->order);
00169
00170
00171
00172
00173 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00174 "Loading entry 0x%x ('%s')...", entry->tag,
00175 exif_tag_get_name (entry->tag));
00176
00177
00178
00179 s = exif_format_get_size(entry->format) * entry->components;
00180 if ((s < entry->components) || (s == 0)){
00181 return 0;
00182 }
00183
00184
00185
00186
00187
00188 if (s > 4)
00189 doff = exif_get_long (d + offset + 8, data->priv->order);
00190 else
00191 doff = offset + 8;
00192
00193
00194 if ((doff + s < doff) || (doff + s < s) || (doff + s > size)) {
00195 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00196 "Tag data past end of buffer (%u > %u)", doff+s, size);
00197 return 0;
00198 }
00199
00200 entry->data = exif_data_alloc (data, s);
00201 if (entry->data) {
00202 entry->size = s;
00203 memcpy (entry->data, d + doff, s);
00204 } else {
00205
00206 EXIF_LOG_NO_MEMORY(data->priv->log, "ExifData", s);
00207 }
00208
00209
00210 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
00211 if (!entry->data) {
00212 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00213 "MakerNote found with empty data");
00214 } else if (entry->size > 6) {
00215 exif_log (data->priv->log,
00216 EXIF_LOG_CODE_DEBUG, "ExifData",
00217 "MakerNote found (%02x %02x %02x %02x "
00218 "%02x %02x %02x...).",
00219 entry->data[0], entry->data[1], entry->data[2],
00220 entry->data[3], entry->data[4], entry->data[5],
00221 entry->data[6]);
00222 }
00223 data->priv->offset_mnote = doff;
00224 }
00225 return 1;
00226 }
00227
00228 static void
00229 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
00230 unsigned char **d, unsigned int *ds,
00231 unsigned int offset)
00232 {
00233 unsigned int doff, s;
00234 unsigned int ts;
00235
00236 if (!data || !data->priv)
00237 return;
00238
00239
00240
00241
00242
00243 exif_set_short (*d + 6 + offset + 0,
00244 data->priv->order, (ExifShort) e->tag);
00245 exif_set_short (*d + 6 + offset + 2,
00246 data->priv->order, (ExifShort) e->format);
00247
00248 if (!(data->priv->options & EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE)) {
00249
00250 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
00251
00252 exif_mem_free (data->priv->mem, e->data);
00253 e->data = NULL;
00254 e->size = 0;
00255 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
00256 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
00257 e->components = e->size;
00258 }
00259 }
00260
00261 exif_set_long (*d + 6 + offset + 4,
00262 data->priv->order, e->components);
00263
00264
00265
00266
00267
00268 s = exif_format_get_size (e->format) * e->components;
00269 if (s > 4) {
00270 unsigned char *t;
00271 doff = *ds - 6;
00272 ts = *ds + s;
00273
00274
00275
00276
00277
00278
00279 if (s & 1)
00280 ts++;
00281 t = exif_mem_realloc (data->priv->mem, *d, ts);
00282 if (!t) {
00283 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
00284 return;
00285 }
00286 *d = t;
00287 *ds = ts;
00288 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
00289 if (s & 1)
00290 *(*d + *ds - 1) = '\0';
00291
00292 } else
00293 doff = offset + 8;
00294
00295
00296
00297 if (e->data) {
00298 memcpy (*d + 6 + doff, e->data, s);
00299 } else {
00300 memset (*d + 6 + doff, 0, s);
00301 }
00302 if (s < 4)
00303 memset (*d + 6 + doff + s, 0, (4 - s));
00304 }
00305
00306 static void
00307 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
00308 unsigned int ds, ExifLong o, ExifLong s)
00309 {
00310
00311 if ((o + s < o) || (o + s < s) || (o + s > ds) || (o > ds)) {
00312 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00313 "Bogus thumbnail offset (%u) or size (%u).",
00314 o, s);
00315 return;
00316 }
00317
00318 if (data->data)
00319 exif_mem_free (data->priv->mem, data->data);
00320 if (!(data->data = exif_data_alloc (data, s))) {
00321 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", s);
00322 data->size = 0;
00323 return;
00324 }
00325 data->size = s;
00326 memcpy (data->data, d + o, s);
00327 }
00328
00329 #undef CHECK_REC
00330 #define CHECK_REC(i) \
00331 if ((i) == ifd) { \
00332 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
00333 "ExifData", "Recursive entry in IFD " \
00334 "'%s' detected. Skipping...", \
00335 exif_ifd_get_name (i)); \
00336 break; \
00337 } \
00338 if (data->ifd[(i)]->count) { \
00339 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
00340 "ExifData", "Attempt to load IFD " \
00341 "'%s' multiple times detected. " \
00342 "Skipping...", \
00343 exif_ifd_get_name (i)); \
00344 break; \
00345 }
00346
00357 static void
00358 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
00359 const unsigned char *d,
00360 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
00361 {
00362 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
00363 ExifShort n;
00364 ExifEntry *entry;
00365 unsigned int i;
00366 ExifTag tag;
00367
00368 if (!data || !data->priv)
00369 return;
00370
00371
00372 if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
00373 return;
00374
00375 if (recursion_depth > 30) {
00376 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
00377 "Deep recursion detected!");
00378 return;
00379 }
00380
00381
00382 if ((offset + 2 < offset) || (offset + 2 < 2) || (offset + 2 > ds)) {
00383 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
00384 "Tag data past end of buffer (%u > %u)", offset+2, ds);
00385 return;
00386 }
00387 n = exif_get_short (d + offset, data->priv->order);
00388 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00389 "Loading %hu entries...", n);
00390 offset += 2;
00391
00392
00393 if (offset + 12 * n > ds) {
00394 n = (ds - offset) / 12;
00395 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00396 "Short data; only loading %hu entries...", n);
00397 }
00398
00399 for (i = 0; i < n; i++) {
00400
00401 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
00402 switch (tag) {
00403 case EXIF_TAG_EXIF_IFD_POINTER:
00404 case EXIF_TAG_GPS_INFO_IFD_POINTER:
00405 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
00406 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
00407 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
00408 o = exif_get_long (d + offset + 12 * i + 8,
00409 data->priv->order);
00410
00411
00412
00413 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00414 "Sub-IFD entry 0x%x ('%s') at %u.", tag,
00415 exif_tag_get_name(tag), o);
00416 switch (tag) {
00417 case EXIF_TAG_EXIF_IFD_POINTER:
00418 CHECK_REC (EXIF_IFD_EXIF);
00419 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
00420 break;
00421 case EXIF_TAG_GPS_INFO_IFD_POINTER:
00422 CHECK_REC (EXIF_IFD_GPS);
00423 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
00424 break;
00425 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
00426 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
00427 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
00428 break;
00429 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
00430 thumbnail_offset = o;
00431 if (thumbnail_offset && thumbnail_length)
00432 exif_data_load_data_thumbnail (data, d,
00433 ds, thumbnail_offset,
00434 thumbnail_length);
00435 break;
00436 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
00437 thumbnail_length = o;
00438 if (thumbnail_offset && thumbnail_length)
00439 exif_data_load_data_thumbnail (data, d,
00440 ds, thumbnail_offset,
00441 thumbnail_length);
00442 break;
00443 default:
00444 return;
00445 }
00446 break;
00447 default:
00448
00449
00450
00451
00452
00453
00454 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
00455
00456
00457
00458
00459
00460 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
00461 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00462 "Skipping empty entry at position %u in '%s'.", i,
00463 exif_ifd_get_name (ifd));
00464 break;
00465 }
00466 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00467 "Unknown tag 0x%04x (entry %u in '%s'). Please report this tag "
00468 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
00469 exif_ifd_get_name (ifd));
00470 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
00471 break;
00472 }
00473 entry = exif_entry_new_mem (data->priv->mem);
00474 if (exif_data_load_data_entry (data, entry, d, ds,
00475 offset + 12 * i))
00476 exif_content_add_entry (data->ifd[ifd], entry);
00477 exif_entry_unref (entry);
00478 break;
00479 }
00480 }
00481 }
00482
00483 static int
00484 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
00485 {
00486 ExifShort tag1 = exif_get_short (p1, o);
00487 ExifShort tag2 = exif_get_short (p2, o);
00488
00489 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
00490 }
00491
00492 static int
00493 cmp_func_intel (const void *elem1, const void *elem2)
00494 {
00495 return cmp_func ((const unsigned char *) elem1,
00496 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
00497 }
00498
00499 static int
00500 cmp_func_motorola (const void *elem1, const void *elem2)
00501 {
00502 return cmp_func ((const unsigned char *) elem1,
00503 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
00504 }
00505
00506 static void
00507 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
00508 unsigned char **d, unsigned int *ds,
00509 unsigned int offset)
00510 {
00511 unsigned int j, n_ptr = 0, n_thumb = 0;
00512 ExifIfd i;
00513 unsigned char *t;
00514 unsigned int ts;
00515
00516 if (!data || !data->priv || !ifd || !d || !ds)
00517 return;
00518
00519 for (i = 0; i < EXIF_IFD_COUNT; i++)
00520 if (ifd == data->ifd[i])
00521 break;
00522 if (i == EXIF_IFD_COUNT)
00523 return;
00524
00525
00526
00527
00528 switch (i) {
00529 case EXIF_IFD_0:
00530
00531
00532
00533
00534
00535 if (data->ifd[EXIF_IFD_EXIF]->count ||
00536 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
00537 n_ptr++;
00538
00539
00540 if (data->ifd[EXIF_IFD_GPS]->count)
00541 n_ptr++;
00542
00543 break;
00544 case EXIF_IFD_1:
00545 if (data->size)
00546 n_thumb = 2;
00547 break;
00548 case EXIF_IFD_EXIF:
00549 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
00550 n_ptr++;
00551 default:
00552 break;
00553 }
00554
00555
00556
00557
00558
00559 ts = *ds + (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
00560 t = exif_mem_realloc (data->priv->mem, *d, ts);
00561 if (!t) {
00562 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", ts);
00563 return;
00564 }
00565 *d = t;
00566 *ds = ts;
00567
00568
00569 exif_set_short (*d + 6 + offset, data->priv->order,
00570 (ExifShort) (ifd->count + n_ptr + n_thumb));
00571 offset += 2;
00572
00573
00574
00575
00576
00577 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00578 "Saving %i entries (IFD '%s', offset: %i)...",
00579 ifd->count, exif_ifd_get_name (i), offset);
00580 for (j = 0; j < ifd->count; j++) {
00581 if (ifd->entries[j]) {
00582 exif_data_save_data_entry (data, ifd->entries[j], d, ds,
00583 offset + 12 * j);
00584 }
00585 }
00586
00587 offset += 12 * ifd->count;
00588
00589
00590 switch (i) {
00591 case EXIF_IFD_0:
00592
00593
00594
00595
00596
00597
00598
00599 if (data->ifd[EXIF_IFD_EXIF]->count ||
00600 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
00601 exif_set_short (*d + 6 + offset + 0, data->priv->order,
00602 EXIF_TAG_EXIF_IFD_POINTER);
00603 exif_set_short (*d + 6 + offset + 2, data->priv->order,
00604 EXIF_FORMAT_LONG);
00605 exif_set_long (*d + 6 + offset + 4, data->priv->order,
00606 1);
00607 exif_set_long (*d + 6 + offset + 8, data->priv->order,
00608 *ds - 6);
00609 exif_data_save_data_content (data,
00610 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
00611 offset += 12;
00612 }
00613
00614
00615 if (data->ifd[EXIF_IFD_GPS]->count) {
00616 exif_set_short (*d + 6 + offset + 0, data->priv->order,
00617 EXIF_TAG_GPS_INFO_IFD_POINTER);
00618 exif_set_short (*d + 6 + offset + 2, data->priv->order,
00619 EXIF_FORMAT_LONG);
00620 exif_set_long (*d + 6 + offset + 4, data->priv->order,
00621 1);
00622 exif_set_long (*d + 6 + offset + 8, data->priv->order,
00623 *ds - 6);
00624 exif_data_save_data_content (data,
00625 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
00626 offset += 12;
00627 }
00628
00629 break;
00630 case EXIF_IFD_EXIF:
00631
00632
00633
00634
00635
00636 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
00637 exif_set_short (*d + 6 + offset + 0, data->priv->order,
00638 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
00639 exif_set_short (*d + 6 + offset + 2, data->priv->order,
00640 EXIF_FORMAT_LONG);
00641 exif_set_long (*d + 6 + offset + 4, data->priv->order,
00642 1);
00643 exif_set_long (*d + 6 + offset + 8, data->priv->order,
00644 *ds - 6);
00645 exif_data_save_data_content (data,
00646 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
00647 *ds - 6);
00648 offset += 12;
00649 }
00650
00651 break;
00652 case EXIF_IFD_1:
00653
00654
00655
00656
00657
00658 if (data->size) {
00659
00660
00661 exif_set_short (*d + 6 + offset + 0, data->priv->order,
00662 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
00663 exif_set_short (*d + 6 + offset + 2, data->priv->order,
00664 EXIF_FORMAT_LONG);
00665 exif_set_long (*d + 6 + offset + 4, data->priv->order,
00666 1);
00667 exif_set_long (*d + 6 + offset + 8, data->priv->order,
00668 *ds - 6);
00669 ts = *ds + data->size;
00670 t = exif_mem_realloc (data->priv->mem, *d, ts);
00671 if (!t) {
00672 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
00673 ts);
00674 return;
00675 }
00676 *d = t;
00677 *ds = ts;
00678 memcpy (*d + *ds - data->size, data->data, data->size);
00679 offset += 12;
00680
00681
00682 exif_set_short (*d + 6 + offset + 0, data->priv->order,
00683 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
00684 exif_set_short (*d + 6 + offset + 2, data->priv->order,
00685 EXIF_FORMAT_LONG);
00686 exif_set_long (*d + 6 + offset + 4, data->priv->order,
00687 1);
00688 exif_set_long (*d + 6 + offset + 8, data->priv->order,
00689 data->size);
00690 offset += 12;
00691 }
00692
00693 break;
00694 default:
00695 break;
00696 }
00697
00698
00699 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
00700 (ifd->count + n_ptr + n_thumb), 12,
00701 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
00702
00703
00704 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
00705 data->size)) {
00706
00707
00708
00709
00710
00711 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
00712 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
00713 *ds - 6);
00714 } else
00715 exif_set_long (*d + 6 + offset, data->priv->order, 0);
00716 }
00717
00718 typedef enum {
00719 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
00720 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
00721 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
00722 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3,
00723 EXIF_DATA_TYPE_MAKER_NOTE_NIKON = 4,
00724 EXIF_DATA_TYPE_MAKER_NOTE_CASIO = 5,
00725 EXIF_DATA_TYPE_MAKER_NOTE_FUJI = 6
00726 } ExifDataTypeMakerNote;
00727
00734 static void
00735 interpret_maker_note(ExifData *data, const unsigned char *d, unsigned int ds)
00736 {
00737 int mnoteid;
00738 ExifEntry* e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
00739 if (!e)
00740 return;
00741
00742 if ((mnoteid = exif_mnote_data_olympus_identify (data, e)) != 0) {
00743 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
00744 "ExifData", "Olympus MakerNote variant type %d", mnoteid);
00745 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
00746
00747 } else if ((mnoteid = exif_mnote_data_canon_identify (data, e)) != 0) {
00748 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
00749 "ExifData", "Canon MakerNote variant type %d", mnoteid);
00750 data->priv->md = exif_mnote_data_canon_new (data->priv->mem, data->priv->options);
00751
00752 } else if ((mnoteid = exif_mnote_data_fuji_identify (data, e)) != 0) {
00753 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
00754 "ExifData", "Fuji MakerNote variant type %d", mnoteid);
00755 data->priv->md = exif_mnote_data_fuji_new (data->priv->mem);
00756
00757
00758
00759 } else if ((mnoteid = exif_mnote_data_pentax_identify (data, e)) != 0) {
00760 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
00761 "ExifData", "Pentax MakerNote variant type %d", mnoteid);
00762 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
00763 }
00764
00765
00766
00767
00768 if (data->priv->md) {
00769 exif_mnote_data_log (data->priv->md, data->priv->log);
00770 exif_mnote_data_set_byte_order (data->priv->md,
00771 data->priv->order);
00772 exif_mnote_data_set_offset (data->priv->md,
00773 data->priv->offset_mnote);
00774 exif_mnote_data_load (data->priv->md, d, ds);
00775 }
00776 }
00777
00778 #define LOG_TOO_SMALL \
00779 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
00780 _("Size of data too small to allow for EXIF data."));
00781
00782 void
00783 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
00784 unsigned int ds)
00785 {
00786 unsigned int l;
00787 ExifLong offset;
00788 ExifShort n;
00789 const unsigned char *d = d_orig;
00790 unsigned int len, fullds;
00791
00792 if (!data || !data->priv || !d || !ds)
00793 return;
00794
00795 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00796 "Parsing %i byte(s) EXIF data...\n", ds);
00797
00798
00799
00800
00801
00802 if (ds < 6) {
00803 LOG_TOO_SMALL;
00804 return;
00805 }
00806 if (!memcmp (d, ExifHeader, 6)) {
00807 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00808 "Found EXIF header.");
00809 } else {
00810 while (ds >= 3) {
00811 while (ds && (d[0] == 0xff)) {
00812 d++;
00813 ds--;
00814 }
00815
00816
00817 if (ds && d[0] == JPEG_MARKER_SOI) {
00818 d++;
00819 ds--;
00820 continue;
00821 }
00822
00823
00824 if (ds >= 3 && d[0] == JPEG_MARKER_APP0) {
00825 d++;
00826 ds--;
00827 l = (d[0] << 8) | d[1];
00828 if (l > ds)
00829 return;
00830 d += l;
00831 ds -= l;
00832 continue;
00833 }
00834
00835
00836 if (ds && d[0] == JPEG_MARKER_APP1)
00837 break;
00838
00839
00840 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
00841 "ExifData", _("EXIF marker not found."));
00842 return;
00843 }
00844 if (ds < 3) {
00845 LOG_TOO_SMALL;
00846 return;
00847 }
00848 d++;
00849 ds--;
00850 len = (d[0] << 8) | d[1];
00851 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00852 "We have to deal with %i byte(s) of EXIF data.",
00853 len);
00854 d += 2;
00855 ds -= 2;
00856 }
00857
00858
00859
00860
00861
00862 if (ds < 6) {
00863 LOG_TOO_SMALL;
00864 return;
00865 }
00866 if (memcmp (d, ExifHeader, 6)) {
00867 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
00868 "ExifData", _("EXIF header not found."));
00869 return;
00870 }
00871
00872 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00873 "Found EXIF header.");
00874
00875
00876 if (ds < 14)
00877 return;
00878
00879
00880
00881
00882 fullds = ds;
00883 if (ds > 0xfffe)
00884 ds = 0xfffe;
00885
00886
00887 if (!memcmp (d + 6, "II", 2))
00888 data->priv->order = EXIF_BYTE_ORDER_INTEL;
00889 else if (!memcmp (d + 6, "MM", 2))
00890 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
00891 else {
00892 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
00893 "ExifData", _("Unknown encoding."));
00894 return;
00895 }
00896
00897
00898 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
00899 return;
00900
00901
00902 offset = exif_get_long (d + 10, data->priv->order);
00903 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00904 "IFD 0 at %i.", (int) offset);
00905
00906
00907 if (offset > ds || offset + 6 + 2 > ds)
00908 return;
00909
00910
00911 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
00912
00913
00914 n = exif_get_short (d + 6 + offset, data->priv->order);
00915 if (offset + 6 + 2 + 12 * n + 4 > ds)
00916 return;
00917
00918 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
00919 if (offset) {
00920 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00921 "IFD 1 at %i.", (int) offset);
00922
00923
00924 if (offset > ds || offset + 6 > ds) {
00925 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
00926 "ExifData", "Bogus offset of IFD1.");
00927 } else {
00928 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
00929 }
00930 }
00931
00932
00933
00934
00935
00936
00937
00938 interpret_maker_note(data, d, fullds);
00939
00940
00941 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
00942 exif_data_fix (data);
00943 }
00944
00945 void
00946 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
00947 {
00948 if (ds)
00949 *ds = 0;
00950
00951 if (!data || !d || !ds)
00952 return;
00953
00954
00955 *ds = 14;
00956 *d = exif_data_alloc (data, *ds);
00957 if (!*d) {
00958 *ds = 0;
00959 return;
00960 }
00961 memcpy (*d, ExifHeader, 6);
00962
00963
00964 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
00965 memcpy (*d + 6, "II", 2);
00966 } else {
00967 memcpy (*d + 6, "MM", 2);
00968 }
00969
00970
00971 exif_set_short (*d + 8, data->priv->order, 0x002a);
00972
00973
00974
00975
00976
00977
00978
00979 exif_set_long (*d + 10, data->priv->order, 8);
00980
00981
00982 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00983 "Saving IFDs...");
00984 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
00985 *ds - 6);
00986 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
00987 "Saved %i byte(s) EXIF data.", *ds);
00988 }
00989
00990 ExifData *
00991 exif_data_new_from_file (const char *path)
00992 {
00993 ExifData *edata;
00994 ExifLoader *loader;
00995
00996 loader = exif_loader_new ();
00997 exif_loader_write_file (loader, path);
00998 edata = exif_loader_get_data (loader);
00999 exif_loader_unref (loader);
01000
01001 return (edata);
01002 }
01003
01004 void
01005 exif_data_ref (ExifData *data)
01006 {
01007 if (!data)
01008 return;
01009
01010 data->priv->ref_count++;
01011 }
01012
01013 void
01014 exif_data_unref (ExifData *data)
01015 {
01016 if (!data)
01017 return;
01018
01019 data->priv->ref_count--;
01020 if (!data->priv->ref_count)
01021 exif_data_free (data);
01022 }
01023
01024 void
01025 exif_data_free (ExifData *data)
01026 {
01027 unsigned int i;
01028 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
01029
01030 if (!data)
01031 return;
01032
01033 for (i = 0; i < EXIF_IFD_COUNT; i++) {
01034 if (data->ifd[i]) {
01035 exif_content_unref (data->ifd[i]);
01036 data->ifd[i] = NULL;
01037 }
01038 }
01039
01040 if (data->data) {
01041 exif_mem_free (mem, data->data);
01042 data->data = NULL;
01043 }
01044
01045 if (data->priv) {
01046 if (data->priv->log) {
01047 exif_log_unref (data->priv->log);
01048 data->priv->log = NULL;
01049 }
01050 if (data->priv->md) {
01051 exif_mnote_data_unref (data->priv->md);
01052 data->priv->md = NULL;
01053 }
01054 exif_mem_free (mem, data->priv);
01055 exif_mem_free (mem, data);
01056 }
01057
01058 exif_mem_unref (mem);
01059 }
01060
01061 void
01062 exif_data_dump (ExifData *data)
01063 {
01064 unsigned int i;
01065
01066 if (!data)
01067 return;
01068
01069 for (i = 0; i < EXIF_IFD_COUNT; i++) {
01070 if (data->ifd[i] && data->ifd[i]->count) {
01071 printf ("Dumping IFD '%s'...\n",
01072 exif_ifd_get_name (i));
01073 exif_content_dump (data->ifd[i], 0);
01074 }
01075 }
01076
01077 if (data->data) {
01078 printf ("%i byte(s) thumbnail data available.", data->size);
01079 if (data->size >= 4) {
01080 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
01081 data->data[0], data->data[1],
01082 data->data[data->size - 2],
01083 data->data[data->size - 1]);
01084 }
01085 }
01086 }
01087
01088 ExifByteOrder
01089 exif_data_get_byte_order (ExifData *data)
01090 {
01091 if (!data)
01092 return (0);
01093
01094 return (data->priv->order);
01095 }
01096
01097 void
01098 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
01099 void *user_data)
01100 {
01101 unsigned int i;
01102
01103 if (!data || !func)
01104 return;
01105
01106 for (i = 0; i < EXIF_IFD_COUNT; i++)
01107 func (data->ifd[i], user_data);
01108 }
01109
01110 typedef struct _ByteOrderChangeData ByteOrderChangeData;
01111 struct _ByteOrderChangeData {
01112 ExifByteOrder old, new;
01113 };
01114
01115 static void
01116 entry_set_byte_order (ExifEntry *e, void *data)
01117 {
01118 ByteOrderChangeData *d = data;
01119
01120 if (!e)
01121 return;
01122
01123 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
01124 }
01125
01126 static void
01127 content_set_byte_order (ExifContent *content, void *data)
01128 {
01129 exif_content_foreach_entry (content, entry_set_byte_order, data);
01130 }
01131
01132 void
01133 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
01134 {
01135 ByteOrderChangeData d;
01136
01137 if (!data || (order == data->priv->order))
01138 return;
01139
01140 d.old = data->priv->order;
01141 d.new = order;
01142 exif_data_foreach_content (data, content_set_byte_order, &d);
01143 data->priv->order = order;
01144 if (data->priv->md)
01145 exif_mnote_data_set_byte_order (data->priv->md, order);
01146 }
01147
01148 void
01149 exif_data_log (ExifData *data, ExifLog *log)
01150 {
01151 unsigned int i;
01152
01153 if (!data || !data->priv)
01154 return;
01155 exif_log_unref (data->priv->log);
01156 data->priv->log = log;
01157 exif_log_ref (log);
01158
01159 for (i = 0; i < EXIF_IFD_COUNT; i++)
01160 exif_content_log (data->ifd[i], log);
01161 }
01162
01163
01164 ExifLog *exif_data_get_log (ExifData *);
01165 ExifLog *
01166 exif_data_get_log (ExifData *data)
01167 {
01168 if (!data || !data->priv)
01169 return NULL;
01170 return data->priv->log;
01171 }
01172
01173 static const struct {
01174 ExifDataOption option;
01175 const char *name;
01176 const char *description;
01177 } exif_data_option[] = {
01178 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
01179 N_("Ignore unknown tags when loading EXIF data.")},
01180 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
01181 N_("Add, correct and remove entries to get EXIF data that follows "
01182 "the specification.")},
01183 {EXIF_DATA_OPTION_DONT_CHANGE_MAKER_NOTE, N_("Do not change maker note"),
01184 N_("When loading and resaving Exif data, save the maker note unmodified."
01185 " Be aware that the maker note can get corrupted.")},
01186 {0, NULL, NULL}
01187 };
01188
01189 const char *
01190 exif_data_option_get_name (ExifDataOption o)
01191 {
01192 unsigned int i;
01193
01194 for (i = 0; exif_data_option[i].name; i++)
01195 if (exif_data_option[i].option == o)
01196 break;
01197 return _(exif_data_option[i].name);
01198 }
01199
01200 const char *
01201 exif_data_option_get_description (ExifDataOption o)
01202 {
01203 unsigned int i;
01204
01205 for (i = 0; exif_data_option[i].description; i++)
01206 if (exif_data_option[i].option == o)
01207 break;
01208 return _(exif_data_option[i].description);
01209 }
01210
01211 void
01212 exif_data_set_option (ExifData *d, ExifDataOption o)
01213 {
01214 if (!d)
01215 return;
01216
01217 d->priv->options |= o;
01218 }
01219
01220 void
01221 exif_data_unset_option (ExifData *d, ExifDataOption o)
01222 {
01223 if (!d)
01224 return;
01225
01226 d->priv->options &= ~o;
01227 }
01228
01229 static void
01230 fix_func (ExifContent *c, void *UNUSED(data))
01231 {
01232 switch (exif_content_get_ifd (c)) {
01233 case EXIF_IFD_1:
01234 if (c->parent->data)
01235 exif_content_fix (c);
01236 else if (c->count) {
01237 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
01238 "No thumbnail but entries on thumbnail. These entries have been "
01239 "removed.");
01240 while (c->count) {
01241 unsigned int cnt = c->count;
01242 exif_content_remove_entry (c, c->entries[c->count - 1]);
01243 if (cnt == c->count) {
01244
01245 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
01246 "failed to remove last entry from entries.");
01247 c->count--;
01248 }
01249 }
01250 }
01251 break;
01252 default:
01253 exif_content_fix (c);
01254 }
01255 }
01256
01257 void
01258 exif_data_fix (ExifData *d)
01259 {
01260 exif_data_foreach_content (d, fix_func, NULL);
01261 }
01262
01263 void
01264 exif_data_set_data_type (ExifData *d, ExifDataType dt)
01265 {
01266 if (!d || !d->priv)
01267 return;
01268
01269 d->priv->data_type = dt;
01270 }
01271
01272 ExifDataType
01273 exif_data_get_data_type (ExifData *d)
01274 {
01275 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_UNKNOWN;
01276 }