6 * Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
22 #include <sys/types.h>
31 #include "format_raw.h"
33 #include "format_canon.h"
34 #include "format_fuji.h"
35 #include "format_nikon.h"
36 #include "format_olympus.h"
39 /* so that debugging is honored */
43 typedef struct _FormatRawEntry FormatRawEntry;
44 struct _FormatRawEntry {
45 const gchar *extension;
46 FormatRawMatchType magic_type;
47 const guint magic_offset;
48 const void *magic_pattern;
49 const guint magic_length;
50 const FormatRawExifType exif_type;
51 FormatRawExifParseFunc exif_func;
52 const gchar *description;
53 FormatRawParseFunc func_parse;
56 static FormatRawEntry format_raw_list[] = {
58 FORMAT_RAW_DEBUG_TIFF,
65 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
69 typedef struct _FormatExifEntry FormatExifEntry;
70 struct _FormatExifEntry {
71 FormatExifMatchType header_type;
72 const void *header_pattern;
73 const guint header_length;
74 const gchar *description;
75 FormatExifParseFunc func_parse;
78 static FormatExifEntry format_exif_list[] = {
87 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
88 guint tag, ExifFormatType type,
89 guint *result_offset, guint *result_count)
94 if (len < offset + 2) return 0;
95 if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
97 count = exif_byte_get_int16(data + offset, bo);
99 if (len < offset + count * 12 + 4) return 0;
101 for (i = 0; i < count; i++)
105 segment = offset + i * 12;
106 if (exif_byte_get_int16(data + segment, bo) == tag &&
107 exif_byte_get_int16(data + segment + 2, bo) == type)
113 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
114 chunk_length = ExifFormatList[type].size * chunk_count;
116 if (chunk_length > 4)
118 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
122 chunk_offset = segment + 8;
125 if (chunk_offset + chunk_length <= len)
127 *result_offset = chunk_offset;
128 *result_count = chunk_count;
135 return exif_byte_get_int32(data + offset + count * 12, bo);
138 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
139 guint tag, ExifFormatType type,
140 guint *result_offset, guint *result_count)
145 if (len < 8) return FALSE;
147 if (memcmp(data, "II", 2) == 0)
149 bo = EXIF_BYTE_ORDER_INTEL;
151 else if (memcmp(data, "MM", 2) == 0)
153 bo = EXIF_BYTE_ORDER_MOTOROLA;
160 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
165 offset = exif_byte_get_int32(data + 4, bo);
172 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
184 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
188 guint make_count = 0;
189 guint make_offset = 0;
192 (memcmp(data, "II\x2a\x00", 4) == 0 ||
193 memcmp(data, "MM\x00\x2a", 4) == 0));
196 while (format_raw_list[n].magic_pattern)
198 FormatRawEntry *entry = &format_raw_list[n];
200 switch (entry->magic_type)
202 case FORMAT_RAW_MATCH_MAGIC:
203 if (entry->magic_length + entry->magic_offset <= len &&
204 memcmp(data + entry->magic_offset,
205 entry->magic_pattern, entry->magic_length) == 0)
210 case FORMAT_RAW_MATCH_TIFF_MAKE:
213 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
214 &make_offset, &make_count))
218 if (make_offset != 0 &&
219 make_count >= entry->magic_offset + entry->magic_length &&
220 memcmp(entry->magic_pattern,
221 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
235 static gint format_raw_parse(FormatRawEntry *entry,
236 unsigned char *data, const guint len,
237 guint *image_offset, guint *exif_offset)
243 if (!entry || !entry->func_parse) return FALSE;
245 if (debug) printf("RAW using file parser for %s\n", entry->description);
247 found = entry->func_parse(data, len, &io, &eo);
256 if (image_offset) *image_offset = io;
257 if (exif_offset) *exif_offset = eo;
262 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
263 guint *image_offset, guint *exif_offset)
265 FormatRawEntry *entry;
267 if (!data || len < 1) return FALSE;
269 entry = format_raw_find(data, len);
271 if (!entry || !entry->func_parse) return FALSE;
273 return format_raw_parse(entry, data, len, image_offset, exif_offset);
277 FormatRawExifType format_raw_exif_offset(unsigned char *data, const guint len, guint *exif_offset,
278 FormatRawExifParseFunc *exif_parse_func)
280 FormatRawEntry *entry;
282 if (!data || len < 1) return FALSE;
284 entry = format_raw_find(data, len);
286 if (!entry || !entry->func_parse) return FALSE;
288 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
290 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
292 *exif_parse_func = entry->exif_func;
295 return entry->exif_type;
299 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
300 unsigned char *header_data, const guint header_len,
301 guint *image_offset, guint *exif_offset)
303 FormatRawEntry *entry;
304 void *map_data = NULL;
309 if (!header_data || fd < 0) return FALSE;
311 /* given image pathname, first do simple (and fast) file extension test */
318 ext = strrchr(path, '.');
319 if (!ext) return FALSE;
323 while (!match && format_raw_list[i].magic_pattern)
325 if (format_raw_list[i].extension &&
326 strcasecmp(format_raw_list[i].extension, ext) == 0)
333 if (!match) return FALSE;
335 if (debug) printf("RAW file parser extension match\n");
339 * when the target is a tiff file it should be mmaped prior to format_raw_find as
340 * the make field data may not always be within header_data + header_len
342 entry = format_raw_find(header_data, header_len);
344 if (!entry || !entry->func_parse) return FALSE;
346 if (fstat(fd, &st) == -1)
348 printf("Failed to stat file %d\n", fd);
351 map_len = st.st_size;
352 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
353 if (map_data == MAP_FAILED)
355 printf("Failed to mmap file %d\n", fd);
359 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
361 if (munmap(map_data, map_len) == -1)
363 printf("Failed to unmap file %d\n", fd);
366 if (success && image_offset)
368 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
370 printf("Failed to seek to embedded image\n");
373 if (*exif_offset) *exif_offset = 0;
382 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
383 guint offset, guint size)
388 make = exif_get_item(exif, "Exif.Image.Make");
391 while (format_exif_list[n].header_pattern)
393 switch (format_exif_list[n].header_type)
395 case FORMAT_EXIF_MATCH_MAKERNOTE:
396 if (format_exif_list[n].header_length + offset < size &&
397 memcmp(tiff + offset, format_exif_list[n].header_pattern,
398 format_exif_list[n].header_length) == 0)
400 return &format_exif_list[n];
403 case FORMAT_EXIF_MATCH_MAKE:
405 make->data_len >= format_exif_list[n].header_length &&
406 memcmp(make->data, format_exif_list[n].header_pattern,
407 format_exif_list[n].header_length) == 0)
409 return &format_exif_list[n];
419 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
420 guint size, ExifByteOrder bo)
422 FormatExifEntry *entry;
424 entry = format_exif_makernote_find(exif, tiff, offset, size);
426 if (!entry || !entry->func_parse) return FALSE;
428 if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
430 return entry->func_parse(exif, tiff, offset, size, bo);
434 *-----------------------------------------------------------------------------
435 * Basic TIFF debugger, prints all IFD entries within tiff file
436 *-----------------------------------------------------------------------------
440 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
441 ExifByteOrder bo, gint level);
443 static void format_debug_tiff_entry(unsigned char *data, const guint len, guint offset,
444 ExifByteOrder bo, gint level)
452 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
453 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
454 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
456 seg_len = ExifFormatList[type].size * count;
459 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
460 if (segment + seg_len > len) return;
464 segment = offset + EXIF_TIFD_OFFSET_DATA;
467 printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
468 level, "", tag, tag, type,
469 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
470 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
472 if (tag == 0x8769 || tag == 0x14a)
476 printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
478 for (i = 0; i < count; i++)
482 subset = exif_byte_get_int32(data + segment + i * 4, bo);
483 format_debug_tiff_table(data, len, subset, bo, level + 1);
486 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
488 printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
490 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
492 guint subset = exif_byte_get_int32(data + segment, bo);
493 printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
495 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
497 guint subset = exif_byte_get_int32(data + segment, bo);
498 printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
502 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
503 ExifByteOrder bo, gint level)
508 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
510 if (len < offset + 2) return FALSE;
512 count = exif_byte_get_int16(data + offset, bo);
514 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
516 printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
518 for (i = 0; i < count; i++)
520 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
523 printf("%*s----------- end of #%d ------------\n", level, "", level);
525 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
528 gint format_debug_tiff_raw(unsigned char *data, const guint len,
529 guint *image_offset, guint *exif_offset)
535 if (len < 8) return FALSE;
537 /* for debugging, we are more relaxed as to magic header */
538 if (memcmp(data, "II", 2) == 0)
540 bo = EXIF_BYTE_ORDER_INTEL;
542 else if (memcmp(data, "MM", 2) == 0)
544 bo = EXIF_BYTE_ORDER_MOTOROLA;
551 printf("*** debug parsing tiff\n");
553 offset = exif_byte_get_int32(data + 4, bo);
555 while (offset && level < EXIF_TIFF_MAX_LEVELS)
557 offset = format_debug_tiff_table(data, len, offset, bo, 0);
563 /* we are debugging, not trying to return any data */