4 * Copyright (C) 2008 The Geeqie Team
7 * Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
9 * This software is released under the GNU General Public License (GNU GPL).
10 * Please read the included file COPYING for more information.
11 * This software comes with no warranty of any kind, use at your own risk!
23 #include <sys/types.h>
32 #include "format_raw.h"
34 #include "format_canon.h"
35 #include "format_fuji.h"
36 #include "format_nikon.h"
37 #include "format_olympus.h"
40 /* so that debugging is honored */
44 typedef struct _FormatRawEntry FormatRawEntry;
45 struct _FormatRawEntry {
46 const gchar *extension;
47 FormatRawMatchType magic_type;
48 const guint magic_offset;
49 const void *magic_pattern;
50 const guint magic_length;
51 const FormatRawExifType exif_type;
52 FormatRawExifParseFunc exif_func;
53 const gchar *description;
54 FormatRawParseFunc func_parse;
57 static FormatRawEntry format_raw_list[] = {
59 FORMAT_RAW_DEBUG_TIFF,
67 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
71 typedef struct _FormatExifEntry FormatExifEntry;
72 struct _FormatExifEntry {
73 FormatExifMatchType header_type;
74 const void *header_pattern;
75 const guint header_length;
76 const gchar *description;
77 FormatExifParseFunc func_parse;
80 static FormatExifEntry format_exif_list[] = {
89 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
90 guint tag, ExifFormatType type,
91 guint *result_offset, guint *result_count)
96 if (len < offset + 2) return 0;
97 if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
99 count = exif_byte_get_int16(data + offset, bo);
101 if (len < offset + count * 12 + 4) return 0;
103 for (i = 0; i < count; i++)
107 segment = offset + i * 12;
108 if (exif_byte_get_int16(data + segment, bo) == tag &&
109 exif_byte_get_int16(data + segment + 2, bo) == type)
115 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
116 chunk_length = ExifFormatList[type].size * chunk_count;
118 if (chunk_length > 4)
120 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
124 chunk_offset = segment + 8;
127 if (chunk_offset + chunk_length <= len)
129 *result_offset = chunk_offset;
130 *result_count = chunk_count;
137 return exif_byte_get_int32(data + offset + count * 12, bo);
140 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
141 guint tag, ExifFormatType type,
142 guint *result_offset, guint *result_count)
147 if (len < 8) return FALSE;
149 if (memcmp(data, "II", 2) == 0)
151 bo = EXIF_BYTE_ORDER_INTEL;
153 else if (memcmp(data, "MM", 2) == 0)
155 bo = EXIF_BYTE_ORDER_MOTOROLA;
162 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
167 offset = exif_byte_get_int32(data + 4, bo);
174 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
186 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
190 guint make_count = 0;
191 guint make_offset = 0;
194 (memcmp(data, "II\x2a\x00", 4) == 0 ||
195 memcmp(data, "MM\x00\x2a", 4) == 0));
198 while (format_raw_list[n].magic_pattern)
200 FormatRawEntry *entry = &format_raw_list[n];
202 switch (entry->magic_type)
204 case FORMAT_RAW_MATCH_MAGIC:
205 if (entry->magic_length + entry->magic_offset <= len &&
206 memcmp(data + entry->magic_offset,
207 entry->magic_pattern, entry->magic_length) == 0)
212 case FORMAT_RAW_MATCH_TIFF_MAKE:
215 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
216 &make_offset, &make_count))
220 if (make_offset != 0 &&
221 make_count >= entry->magic_offset + entry->magic_length &&
222 memcmp(entry->magic_pattern,
223 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
237 static gint format_raw_parse(FormatRawEntry *entry,
238 unsigned char *data, const guint len,
239 guint *image_offset, guint *exif_offset)
245 if (!entry || !entry->func_parse) return FALSE;
247 if (debug) printf("RAW using file parser for %s\n", entry->description);
249 found = entry->func_parse(data, len, &io, &eo);
258 if (image_offset) *image_offset = io;
259 if (exif_offset) *exif_offset = eo;
264 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
265 guint *image_offset, guint *exif_offset)
267 FormatRawEntry *entry;
269 if (!data || len < 1) return FALSE;
271 entry = format_raw_find(data, len);
273 if (!entry || !entry->func_parse) return FALSE;
275 return format_raw_parse(entry, data, len, image_offset, exif_offset);
279 FormatRawExifType format_raw_exif_offset(unsigned char *data, const guint len, guint *exif_offset,
280 FormatRawExifParseFunc *exif_parse_func)
282 FormatRawEntry *entry;
284 if (!data || len < 1) return FALSE;
286 entry = format_raw_find(data, len);
288 if (!entry || !entry->func_parse) return FALSE;
290 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
292 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
294 *exif_parse_func = entry->exif_func;
297 return entry->exif_type;
301 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
302 unsigned char *header_data, const guint header_len,
303 guint *image_offset, guint *exif_offset)
305 FormatRawEntry *entry;
306 void *map_data = NULL;
311 if (!header_data || fd < 0) return FALSE;
313 /* given image pathname, first do simple (and fast) file extension test */
320 ext = strrchr(path, '.');
321 if (!ext) return FALSE;
325 while (!match && format_raw_list[i].magic_pattern)
327 if (format_raw_list[i].extension &&
328 strcasecmp(format_raw_list[i].extension, ext) == 0)
335 if (!match) return FALSE;
337 if (debug) printf("RAW file parser extension match\n");
341 * when the target is a tiff file it should be mmaped prior to format_raw_find as
342 * the make field data may not always be within header_data + header_len
344 entry = format_raw_find(header_data, header_len);
346 if (!entry || !entry->func_parse) return FALSE;
348 if (fstat(fd, &st) == -1)
350 printf("Failed to stat file %d\n", fd);
353 map_len = st.st_size;
354 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
355 if (map_data == MAP_FAILED)
357 printf("Failed to mmap file %d\n", fd);
361 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
363 if (munmap(map_data, map_len) == -1)
365 printf("Failed to unmap file %d\n", fd);
368 if (success && image_offset)
370 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
372 printf("Failed to seek to embedded image\n");
375 if (*exif_offset) *exif_offset = 0;
384 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
385 guint offset, guint size)
390 make = exif_get_item(exif, "Exif.Image.Make");
393 while (format_exif_list[n].header_pattern)
395 switch (format_exif_list[n].header_type)
397 case FORMAT_EXIF_MATCH_MAKERNOTE:
398 if (format_exif_list[n].header_length + offset < size &&
399 memcmp(tiff + offset, format_exif_list[n].header_pattern,
400 format_exif_list[n].header_length) == 0)
402 return &format_exif_list[n];
405 case FORMAT_EXIF_MATCH_MAKE:
407 make->data_len >= format_exif_list[n].header_length &&
408 memcmp(make->data, format_exif_list[n].header_pattern,
409 format_exif_list[n].header_length) == 0)
411 return &format_exif_list[n];
421 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
422 guint size, ExifByteOrder bo)
424 FormatExifEntry *entry;
426 entry = format_exif_makernote_find(exif, tiff, offset, size);
428 if (!entry || !entry->func_parse) return FALSE;
430 if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
432 return entry->func_parse(exif, tiff, offset, size, bo);
436 *-----------------------------------------------------------------------------
437 * Basic TIFF debugger, prints all IFD entries within tiff file
438 *-----------------------------------------------------------------------------
442 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
443 ExifByteOrder bo, gint level);
445 static void format_debug_tiff_entry(unsigned char *data, const guint len, guint offset,
446 ExifByteOrder bo, gint level)
454 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
455 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
456 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
458 seg_len = ExifFormatList[type].size * count;
461 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
462 if (segment + seg_len > len) return;
466 segment = offset + EXIF_TIFD_OFFSET_DATA;
469 printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
470 level, "", tag, tag, type,
471 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
472 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
474 if (tag == 0x8769 || tag == 0x14a)
478 printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
480 for (i = 0; i < count; i++)
484 subset = exif_byte_get_int32(data + segment + i * 4, bo);
485 format_debug_tiff_table(data, len, subset, bo, level + 1);
488 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
490 printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
492 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
494 guint subset = exif_byte_get_int32(data + segment, bo);
495 printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
497 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
499 guint subset = exif_byte_get_int32(data + segment, bo);
500 printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
504 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
505 ExifByteOrder bo, gint level)
510 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
512 if (len < offset + 2) return FALSE;
514 count = exif_byte_get_int16(data + offset, bo);
516 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
518 printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
520 for (i = 0; i < count; i++)
522 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
525 printf("%*s----------- end of #%d ------------\n", level, "", level);
527 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
530 gint format_debug_tiff_raw(unsigned char *data, const guint len,
531 guint *image_offset, guint *exif_offset)
537 if (len < 8) return FALSE;
539 /* for debugging, we are more relaxed as to magic header */
540 if (memcmp(data, "II", 2) == 0)
542 bo = EXIF_BYTE_ORDER_INTEL;
544 else if (memcmp(data, "MM", 2) == 0)
546 bo = EXIF_BYTE_ORDER_MOTOROLA;
553 printf("*** debug parsing tiff\n");
555 offset = exif_byte_get_int32(data + 4, bo);
557 while (offset && level < EXIF_TIFF_MAX_LEVELS)
559 offset = format_debug_tiff_table(data, len, offset, bo, 0);
565 /* we are debugging, not trying to return any data */