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,
66 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
70 typedef struct _FormatExifEntry FormatExifEntry;
71 struct _FormatExifEntry {
72 FormatExifMatchType header_type;
73 const void *header_pattern;
74 const guint header_length;
75 const gchar *description;
76 FormatExifParseFunc func_parse;
79 static FormatExifEntry format_exif_list[] = {
88 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
89 guint tag, ExifFormatType type,
90 guint *result_offset, guint *result_count)
95 if (len < offset + 2) return 0;
96 if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
98 count = exif_byte_get_int16(data + offset, bo);
100 if (len < offset + count * 12 + 4) return 0;
102 for (i = 0; i < count; i++)
106 segment = offset + i * 12;
107 if (exif_byte_get_int16(data + segment, bo) == tag &&
108 exif_byte_get_int16(data + segment + 2, bo) == type)
114 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
115 chunk_length = ExifFormatList[type].size * chunk_count;
117 if (chunk_length > 4)
119 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
123 chunk_offset = segment + 8;
126 if (chunk_offset + chunk_length <= len)
128 *result_offset = chunk_offset;
129 *result_count = chunk_count;
136 return exif_byte_get_int32(data + offset + count * 12, bo);
139 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
140 guint tag, ExifFormatType type,
141 guint *result_offset, guint *result_count)
146 if (len < 8) return FALSE;
148 if (memcmp(data, "II", 2) == 0)
150 bo = EXIF_BYTE_ORDER_INTEL;
152 else if (memcmp(data, "MM", 2) == 0)
154 bo = EXIF_BYTE_ORDER_MOTOROLA;
161 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
166 offset = exif_byte_get_int32(data + 4, bo);
173 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
185 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
189 guint make_count = 0;
190 guint make_offset = 0;
193 (memcmp(data, "II\x2a\x00", 4) == 0 ||
194 memcmp(data, "MM\x00\x2a", 4) == 0));
197 while (format_raw_list[n].magic_pattern)
199 FormatRawEntry *entry = &format_raw_list[n];
201 switch (entry->magic_type)
203 case FORMAT_RAW_MATCH_MAGIC:
204 if (entry->magic_length + entry->magic_offset <= len &&
205 memcmp(data + entry->magic_offset,
206 entry->magic_pattern, entry->magic_length) == 0)
211 case FORMAT_RAW_MATCH_TIFF_MAKE:
214 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
215 &make_offset, &make_count))
219 if (make_offset != 0 &&
220 make_count >= entry->magic_offset + entry->magic_length &&
221 memcmp(entry->magic_pattern,
222 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
236 static gint format_raw_parse(FormatRawEntry *entry,
237 unsigned char *data, const guint len,
238 guint *image_offset, guint *exif_offset)
244 if (!entry || !entry->func_parse) return FALSE;
246 if (debug) printf("RAW using file parser for %s\n", entry->description);
248 found = entry->func_parse(data, len, &io, &eo);
257 if (image_offset) *image_offset = io;
258 if (exif_offset) *exif_offset = eo;
263 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
264 guint *image_offset, guint *exif_offset)
266 FormatRawEntry *entry;
268 if (!data || len < 1) return FALSE;
270 entry = format_raw_find(data, len);
272 if (!entry || !entry->func_parse) return FALSE;
274 return format_raw_parse(entry, data, len, image_offset, exif_offset);
278 FormatRawExifType format_raw_exif_offset(unsigned char *data, const guint len, guint *exif_offset,
279 FormatRawExifParseFunc *exif_parse_func)
281 FormatRawEntry *entry;
283 if (!data || len < 1) return FALSE;
285 entry = format_raw_find(data, len);
287 if (!entry || !entry->func_parse) return FALSE;
289 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
291 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
293 *exif_parse_func = entry->exif_func;
296 return entry->exif_type;
300 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
301 unsigned char *header_data, const guint header_len,
302 guint *image_offset, guint *exif_offset)
304 FormatRawEntry *entry;
305 void *map_data = NULL;
310 if (!header_data || fd < 0) return FALSE;
312 /* given image pathname, first do simple (and fast) file extension test */
319 ext = strrchr(path, '.');
320 if (!ext) return FALSE;
324 while (!match && format_raw_list[i].magic_pattern)
326 if (format_raw_list[i].extension &&
327 strcasecmp(format_raw_list[i].extension, ext) == 0)
334 if (!match) return FALSE;
336 if (debug) printf("RAW file parser extension match\n");
340 * when the target is a tiff file it should be mmaped prior to format_raw_find as
341 * the make field data may not always be within header_data + header_len
343 entry = format_raw_find(header_data, header_len);
345 if (!entry || !entry->func_parse) return FALSE;
347 if (fstat(fd, &st) == -1)
349 printf("Failed to stat file %d\n", fd);
352 map_len = st.st_size;
353 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
354 if (map_data == MAP_FAILED)
356 printf("Failed to mmap file %d\n", fd);
360 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
362 if (munmap(map_data, map_len) == -1)
364 printf("Failed to unmap file %d\n", fd);
367 if (success && image_offset)
369 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
371 printf("Failed to seek to embedded image\n");
374 if (*exif_offset) *exif_offset = 0;
383 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
384 guint offset, guint size)
389 make = exif_get_item(exif, "Exif.Image.Make");
392 while (format_exif_list[n].header_pattern)
394 switch (format_exif_list[n].header_type)
396 case FORMAT_EXIF_MATCH_MAKERNOTE:
397 if (format_exif_list[n].header_length + offset < size &&
398 memcmp(tiff + offset, format_exif_list[n].header_pattern,
399 format_exif_list[n].header_length) == 0)
401 return &format_exif_list[n];
404 case FORMAT_EXIF_MATCH_MAKE:
406 make->data_len >= format_exif_list[n].header_length &&
407 memcmp(make->data, format_exif_list[n].header_pattern,
408 format_exif_list[n].header_length) == 0)
410 return &format_exif_list[n];
420 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
421 guint size, ExifByteOrder bo)
423 FormatExifEntry *entry;
425 entry = format_exif_makernote_find(exif, tiff, offset, size);
427 if (!entry || !entry->func_parse) return FALSE;
429 if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
431 return entry->func_parse(exif, tiff, offset, size, bo);
435 *-----------------------------------------------------------------------------
436 * Basic TIFF debugger, prints all IFD entries within tiff file
437 *-----------------------------------------------------------------------------
441 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
442 ExifByteOrder bo, gint level);
444 static void format_debug_tiff_entry(unsigned char *data, const guint len, guint offset,
445 ExifByteOrder bo, gint level)
453 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
454 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
455 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
457 seg_len = ExifFormatList[type].size * count;
460 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
461 if (segment + seg_len > len) return;
465 segment = offset + EXIF_TIFD_OFFSET_DATA;
468 printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
469 level, "", tag, tag, type,
470 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
471 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
473 if (tag == 0x8769 || tag == 0x14a)
477 printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
479 for (i = 0; i < count; i++)
483 subset = exif_byte_get_int32(data + segment + i * 4, bo);
484 format_debug_tiff_table(data, len, subset, bo, level + 1);
487 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
489 printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
491 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
493 guint subset = exif_byte_get_int32(data + segment, bo);
494 printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
496 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
498 guint subset = exif_byte_get_int32(data + segment, bo);
499 printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
503 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
504 ExifByteOrder bo, gint level)
509 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
511 if (len < offset + 2) return FALSE;
513 count = exif_byte_get_int16(data + offset, bo);
515 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
517 printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
519 for (i = 0; i < count; i++)
521 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
524 printf("%*s----------- end of #%d ------------\n", level, "", level);
526 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
529 gint format_debug_tiff_raw(unsigned char *data, const guint len,
530 guint *image_offset, guint *exif_offset)
536 if (len < 8) return FALSE;
538 /* for debugging, we are more relaxed as to magic header */
539 if (memcmp(data, "II", 2) == 0)
541 bo = EXIF_BYTE_ORDER_INTEL;
543 else if (memcmp(data, "MM", 2) == 0)
545 bo = EXIF_BYTE_ORDER_MOTOROLA;
552 printf("*** debug parsing tiff\n");
554 offset = exif_byte_get_int32(data + 4, bo);
556 while (offset && level < EXIF_TIFF_MAX_LEVELS)
558 offset = format_debug_tiff_table(data, len, offset, bo, 0);
564 /* we are debugging, not trying to return any data */