2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * Author: Lars Ellenberg
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/types.h>
38 #include "format-raw.h"
40 #include "format-canon.h"
41 #include "format-fuji.h"
42 #include "format-nikon.h"
43 #include "format-olympus.h"
46 typedef struct _FormatRawEntry FormatRawEntry;
47 struct _FormatRawEntry {
48 const gchar *extension;
49 FormatRawMatchType magic_type;
50 const guint magic_offset;
51 gconstpointer magic_pattern;
52 const guint magic_length;
53 const FormatRawExifType exif_type;
54 FormatRawExifParseFunc exif_func;
55 const gchar *description;
56 FormatRawParseFunc func_parse;
59 static FormatRawEntry format_raw_list[] = {
61 FORMAT_RAW_DEBUG_TIFF,
69 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
73 typedef struct _FormatExifEntry FormatExifEntry;
74 struct _FormatExifEntry {
75 FormatExifMatchType header_type;
76 gconstpointer header_pattern;
77 const guint header_length;
78 const gchar *description;
79 FormatExifParseFunc func_parse;
82 static FormatExifEntry format_exif_list[] = {
87 { 0, NULL, 0, NULL, NULL }
91 static guint tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
92 guint tag, ExifFormatType type,
93 guint *result_offset, guint *result_count)
98 if (len < offset + 2) return 0;
99 if (type > EXIF_FORMAT_COUNT) return 0;
101 count = exif_byte_get_int16(data + offset, bo);
103 if (len < offset + count * 12 + 4) return 0;
105 for (i = 0; i < count; i++)
109 segment = offset + i * 12;
110 if (exif_byte_get_int16(data + segment, bo) == tag &&
111 exif_byte_get_int16(data + segment + 2, bo) == type)
117 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
118 chunk_length = ExifFormatList[type].size * chunk_count;
120 if (chunk_length > 4)
122 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
126 chunk_offset = segment + 8;
129 if (chunk_offset + chunk_length <= len)
131 *result_offset = chunk_offset;
132 *result_count = chunk_count;
139 return exif_byte_get_int32(data + offset + count * 12, bo);
142 static gboolean format_tiff_find_tag_data(guchar *data, const guint len,
143 guint tag, ExifFormatType type,
144 guint *result_offset, guint *result_count)
149 if (len < 8) return FALSE;
151 if (memcmp(data, "II", 2) == 0)
153 bo = EXIF_BYTE_ORDER_INTEL;
155 else if (memcmp(data, "MM", 2) == 0)
157 bo = EXIF_BYTE_ORDER_MOTOROLA;
164 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
169 offset = exif_byte_get_int32(data + 4, bo);
176 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
188 static FormatRawEntry *format_raw_find(guchar *data, const guint len)
192 guint make_count = 0;
193 guint make_offset = 0;
196 (memcmp(data, "II\x2a\x00", 4) == 0 ||
197 memcmp(data, "MM\x00\x2a", 4) == 0));
200 while (format_raw_list[n].magic_pattern)
202 FormatRawEntry *entry = &format_raw_list[n];
204 switch (entry->magic_type)
206 case FORMAT_RAW_MATCH_MAGIC:
207 if (entry->magic_length + entry->magic_offset <= len &&
208 memcmp(data + entry->magic_offset,
209 entry->magic_pattern, entry->magic_length) == 0)
214 case FORMAT_RAW_MATCH_TIFF_MAKE:
217 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
218 &make_offset, &make_count))
222 if (make_offset != 0 &&
223 make_count >= entry->magic_offset + entry->magic_length &&
224 memcmp(entry->magic_pattern,
225 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
239 static gboolean format_raw_parse(FormatRawEntry *entry,
240 guchar *data, const guint len,
241 guint *image_offset, guint *exif_offset)
247 if (!entry || !entry->func_parse) return FALSE;
249 DEBUG_1("RAW using file parser for %s", entry->description);
251 found = entry->func_parse(data, len, &io, &eo);
260 if (image_offset) *image_offset = io;
261 if (exif_offset) *exif_offset = eo;
266 gboolean format_raw_img_exif_offsets(guchar *data, const guint len,
267 guint *image_offset, guint *exif_offset)
269 FormatRawEntry *entry;
271 if (!data || len < 1) return FALSE;
273 entry = format_raw_find(data, len);
275 if (!entry || !entry->func_parse) return FALSE;
277 return format_raw_parse(entry, data, len, image_offset, exif_offset);
281 FormatRawExifType format_raw_exif_offset(guchar *data, const guint len, guint *exif_offset,
282 FormatRawExifParseFunc *exif_parse_func)
284 FormatRawEntry *entry;
286 if (!data || len < 1) return FALSE;
288 entry = format_raw_find(data, len);
290 if (!entry || !entry->func_parse) return FALSE;
292 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
294 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
296 *exif_parse_func = entry->exif_func;
299 return entry->exif_type;
303 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
304 guchar *header_data, const guint header_len,
305 guint *image_offset, guint *exif_offset)
307 FormatRawEntry *entry;
308 gpointer map_data = NULL;
313 if (!header_data || fd < 0) return FALSE;
315 /* given image pathname, first do simple (and fast) file extension test */
319 gboolean match = FALSE;
322 ext = strrchr(path, '.');
323 if (!ext) return FALSE;
327 while (!match && format_raw_list[i].magic_pattern)
329 if (format_raw_list[i].extension &&
330 g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
337 if (!match) return FALSE;
339 DEBUG_1("RAW file parser extension match");
343 * @FIXME when the target is a tiff file it should be mmaped prior to format_raw_find as
344 * the make field data may not always be within header_data + header_len
346 entry = format_raw_find(header_data, header_len);
348 if (!entry || !entry->func_parse) return FALSE;
350 if (fstat(fd, &st) == -1)
352 log_printf("Failed to stat file %d\n", fd);
355 map_len = st.st_size;
356 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
357 if (map_data == MAP_FAILED)
359 log_printf("Failed to mmap file %d\n", fd);
363 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
365 if (munmap(map_data, map_len) == -1)
367 log_printf("Failed to unmap file %d\n", fd);
370 if (success && image_offset)
372 if (lseek(fd, *image_offset, SEEK_SET) != (off_t) *image_offset)
374 log_printf("Failed to seek to embedded image\n");
377 if (*exif_offset) *exif_offset = 0;
386 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, guchar *tiff,
387 guint offset, guint size)
392 make = exif_get_item(exif, "Exif.Image.Make");
395 while (format_exif_list[n].header_pattern)
397 switch (format_exif_list[n].header_type)
399 case FORMAT_EXIF_MATCH_MAKERNOTE:
400 if (format_exif_list[n].header_length + offset < size &&
401 memcmp(tiff + offset, format_exif_list[n].header_pattern,
402 format_exif_list[n].header_length) == 0)
404 return &format_exif_list[n];
407 case FORMAT_EXIF_MATCH_MAKE:
409 make->data_len >= format_exif_list[n].header_length &&
410 memcmp(make->data, format_exif_list[n].header_pattern,
411 format_exif_list[n].header_length) == 0)
413 return &format_exif_list[n];
423 gboolean format_exif_makernote_parse(ExifData *exif, guchar *tiff, guint offset,
424 guint size, ExifByteOrder bo)
426 FormatExifEntry *entry;
428 entry = format_exif_makernote_find(exif, tiff, offset, size);
430 if (!entry || !entry->func_parse) return FALSE;
432 DEBUG_1("EXIF using makernote parser for %s", entry->description);
434 return entry->func_parse(exif, tiff, offset, size, bo);
438 *-----------------------------------------------------------------------------
439 * Basic TIFF debugger, prints all IFD entries within tiff file
440 *-----------------------------------------------------------------------------
444 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
445 ExifByteOrder bo, gint level);
447 static void format_debug_tiff_entry(guchar *data, const guint len, guint offset,
448 ExifByteOrder bo, gint level)
456 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
457 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
458 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
460 seg_len = ExifFormatList[type].size * count;
463 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
464 if (segment + seg_len > len) return;
468 segment = offset + EXIF_TIFD_OFFSET_DATA;
471 log_printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
472 level, "", tag, tag, type,
473 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
474 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
476 if (tag == 0x8769 || tag == 0x14a)
480 log_printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
482 for (i = 0; i < count; i++)
486 subset = exif_byte_get_int32(data + segment + i * 4, bo);
487 format_debug_tiff_table(data, len, subset, bo, level + 1);
490 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
492 log_printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
494 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
496 guint subset = exif_byte_get_int32(data + segment, bo);
497 log_printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
499 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
501 guint subset = exif_byte_get_int32(data + segment, bo);
502 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
506 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
507 ExifByteOrder bo, gint level)
512 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
514 if (len < offset + 2) return FALSE;
516 count = exif_byte_get_int16(data + offset, bo);
518 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
520 log_printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
522 for (i = 0; i < count; i++)
524 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
527 log_printf("%*s----------- end of #%d ------------\n", level, "", level);
529 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
532 gboolean format_debug_tiff_raw(guchar *data, const guint len,
533 guint *image_offset, guint *exif_offset)
539 if (len < 8) return FALSE;
541 /* for debugging, we are more relaxed as to magic header */
542 if (memcmp(data, "II", 2) == 0)
544 bo = EXIF_BYTE_ORDER_INTEL;
546 else if (memcmp(data, "MM", 2) == 0)
548 bo = EXIF_BYTE_ORDER_MOTOROLA;
555 log_printf("*** debug parsing tiff\n");
557 offset = exif_byte_get_int32(data + 4, bo);
559 while (offset && level < EXIF_TIFF_MAX_LEVELS)
561 offset = format_debug_tiff_table(data, len, offset, bo, 0);
565 log_printf("*** end\n");
567 /* we are debugging, not trying to return any data */
572 typedef int dummy_variable;
575 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */