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.
31 #include <sys/types.h>
40 #include "format_raw.h"
42 #include "format_canon.h"
43 #include "format_fuji.h"
44 #include "format_nikon.h"
45 #include "format_olympus.h"
48 typedef struct _FormatRawEntry FormatRawEntry;
49 struct _FormatRawEntry {
50 const gchar *extension;
51 FormatRawMatchType magic_type;
52 const guint magic_offset;
53 gconstpointer magic_pattern;
54 const guint magic_length;
55 const FormatRawExifType exif_type;
56 FormatRawExifParseFunc exif_func;
57 const gchar *description;
58 FormatRawParseFunc func_parse;
61 static FormatRawEntry format_raw_list[] = {
63 FORMAT_RAW_DEBUG_TIFF,
71 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
75 typedef struct _FormatExifEntry FormatExifEntry;
76 struct _FormatExifEntry {
77 FormatExifMatchType header_type;
78 gconstpointer header_pattern;
79 const guint header_length;
80 const gchar *description;
81 FormatExifParseFunc func_parse;
84 static FormatExifEntry format_exif_list[] = {
89 { 0, NULL, 0, NULL, NULL }
93 static guint tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
94 guint tag, ExifFormatType type,
95 guint *result_offset, guint *result_count)
100 if (len < offset + 2) return 0;
101 if (type > EXIF_FORMAT_COUNT) return 0;
103 count = exif_byte_get_int16(data + offset, bo);
105 if (len < offset + count * 12 + 4) return 0;
107 for (i = 0; i < count; i++)
111 segment = offset + i * 12;
112 if (exif_byte_get_int16(data + segment, bo) == tag &&
113 exif_byte_get_int16(data + segment + 2, bo) == type)
119 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
120 chunk_length = ExifFormatList[type].size * chunk_count;
122 if (chunk_length > 4)
124 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
128 chunk_offset = segment + 8;
131 if (chunk_offset + chunk_length <= len)
133 *result_offset = chunk_offset;
134 *result_count = chunk_count;
141 return exif_byte_get_int32(data + offset + count * 12, bo);
144 static gboolean format_tiff_find_tag_data(guchar *data, const guint len,
145 guint tag, ExifFormatType type,
146 guint *result_offset, guint *result_count)
151 if (len < 8) return FALSE;
153 if (memcmp(data, "II", 2) == 0)
155 bo = EXIF_BYTE_ORDER_INTEL;
157 else if (memcmp(data, "MM", 2) == 0)
159 bo = EXIF_BYTE_ORDER_MOTOROLA;
166 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
171 offset = exif_byte_get_int32(data + 4, bo);
178 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
190 static FormatRawEntry *format_raw_find(guchar *data, const guint len)
194 guint make_count = 0;
195 guint make_offset = 0;
198 (memcmp(data, "II\x2a\x00", 4) == 0 ||
199 memcmp(data, "MM\x00\x2a", 4) == 0));
202 while (format_raw_list[n].magic_pattern)
204 FormatRawEntry *entry = &format_raw_list[n];
206 switch (entry->magic_type)
208 case FORMAT_RAW_MATCH_MAGIC:
209 if (entry->magic_length + entry->magic_offset <= len &&
210 memcmp(data + entry->magic_offset,
211 entry->magic_pattern, entry->magic_length) == 0)
216 case FORMAT_RAW_MATCH_TIFF_MAKE:
219 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
220 &make_offset, &make_count))
224 if (make_offset != 0 &&
225 make_count >= entry->magic_offset + entry->magic_length &&
226 memcmp(entry->magic_pattern,
227 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
241 static gboolean format_raw_parse(FormatRawEntry *entry,
242 guchar *data, const guint len,
243 guint *image_offset, guint *exif_offset)
249 if (!entry || !entry->func_parse) return FALSE;
251 DEBUG_1("RAW using file parser for %s", entry->description);
253 found = entry->func_parse(data, len, &io, &eo);
262 if (image_offset) *image_offset = io;
263 if (exif_offset) *exif_offset = eo;
268 gboolean format_raw_img_exif_offsets(guchar *data, const guint len,
269 guint *image_offset, guint *exif_offset)
271 FormatRawEntry *entry;
273 if (!data || len < 1) return FALSE;
275 entry = format_raw_find(data, len);
277 if (!entry || !entry->func_parse) return FALSE;
279 return format_raw_parse(entry, data, len, image_offset, exif_offset);
283 FormatRawExifType format_raw_exif_offset(guchar *data, const guint len, guint *exif_offset,
284 FormatRawExifParseFunc *exif_parse_func)
286 FormatRawEntry *entry;
288 if (!data || len < 1) return FALSE;
290 entry = format_raw_find(data, len);
292 if (!entry || !entry->func_parse) return FALSE;
294 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
296 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
298 *exif_parse_func = entry->exif_func;
301 return entry->exif_type;
305 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
306 guchar *header_data, const guint header_len,
307 guint *image_offset, guint *exif_offset)
309 FormatRawEntry *entry;
310 gpointer map_data = NULL;
315 if (!header_data || fd < 0) return FALSE;
317 /* given image pathname, first do simple (and fast) file extension test */
321 gboolean match = FALSE;
324 ext = strrchr(path, '.');
325 if (!ext) return FALSE;
329 while (!match && format_raw_list[i].magic_pattern)
331 if (format_raw_list[i].extension &&
332 g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
339 if (!match) return FALSE;
341 DEBUG_1("RAW file parser extension match");
345 * when the target is a tiff file it should be mmaped prior to format_raw_find as
346 * the make field data may not always be within header_data + header_len
348 entry = format_raw_find(header_data, header_len);
350 if (!entry || !entry->func_parse) return FALSE;
352 if (fstat(fd, &st) == -1)
354 log_printf("Failed to stat file %d\n", fd);
357 map_len = st.st_size;
358 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
359 if (map_data == MAP_FAILED)
361 log_printf("Failed to mmap file %d\n", fd);
365 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
367 if (munmap(map_data, map_len) == -1)
369 log_printf("Failed to unmap file %d\n", fd);
372 if (success && image_offset)
374 if (lseek(fd, *image_offset, SEEK_SET) != (off_t) *image_offset)
376 log_printf("Failed to seek to embedded image\n");
379 if (*exif_offset) *exif_offset = 0;
388 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, guchar *tiff,
389 guint offset, guint size)
394 make = exif_get_item(exif, "Exif.Image.Make");
397 while (format_exif_list[n].header_pattern)
399 switch (format_exif_list[n].header_type)
401 case FORMAT_EXIF_MATCH_MAKERNOTE:
402 if (format_exif_list[n].header_length + offset < size &&
403 memcmp(tiff + offset, format_exif_list[n].header_pattern,
404 format_exif_list[n].header_length) == 0)
406 return &format_exif_list[n];
409 case FORMAT_EXIF_MATCH_MAKE:
411 make->data_len >= format_exif_list[n].header_length &&
412 memcmp(make->data, format_exif_list[n].header_pattern,
413 format_exif_list[n].header_length) == 0)
415 return &format_exif_list[n];
425 gboolean format_exif_makernote_parse(ExifData *exif, guchar *tiff, guint offset,
426 guint size, ExifByteOrder bo)
428 FormatExifEntry *entry;
430 entry = format_exif_makernote_find(exif, tiff, offset, size);
432 if (!entry || !entry->func_parse) return FALSE;
434 DEBUG_1("EXIF using makernote parser for %s", entry->description);
436 return entry->func_parse(exif, tiff, offset, size, bo);
440 *-----------------------------------------------------------------------------
441 * Basic TIFF debugger, prints all IFD entries within tiff file
442 *-----------------------------------------------------------------------------
446 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
447 ExifByteOrder bo, gint level);
449 static void format_debug_tiff_entry(guchar *data, const guint len, guint offset,
450 ExifByteOrder bo, gint level)
458 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
459 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
460 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
462 seg_len = ExifFormatList[type].size * count;
465 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
466 if (segment + seg_len > len) return;
470 segment = offset + EXIF_TIFD_OFFSET_DATA;
473 log_printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
474 level, "", tag, tag, type,
475 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
476 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
478 if (tag == 0x8769 || tag == 0x14a)
482 log_printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
484 for (i = 0; i < count; i++)
488 subset = exif_byte_get_int32(data + segment + i * 4, bo);
489 format_debug_tiff_table(data, len, subset, bo, level + 1);
492 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
494 log_printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
496 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
498 guint subset = exif_byte_get_int32(data + segment, bo);
499 log_printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
501 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
503 guint subset = exif_byte_get_int32(data + segment, bo);
504 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
508 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
509 ExifByteOrder bo, gint level)
514 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
516 if (len < offset + 2) return FALSE;
518 count = exif_byte_get_int16(data + offset, bo);
520 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
522 log_printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
524 for (i = 0; i < count; i++)
526 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
529 log_printf("%*s----------- end of #%d ------------\n", level, "", level);
531 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
534 gboolean format_debug_tiff_raw(guchar *data, const guint len,
535 guint *image_offset, guint *exif_offset)
541 if (len < 8) return FALSE;
543 /* for debugging, we are more relaxed as to magic header */
544 if (memcmp(data, "II", 2) == 0)
546 bo = EXIF_BYTE_ORDER_INTEL;
548 else if (memcmp(data, "MM", 2) == 0)
550 bo = EXIF_BYTE_ORDER_MOTOROLA;
557 log_printf("*** debug parsing tiff\n");
559 offset = exif_byte_get_int32(data + 4, bo);
561 while (offset && level < EXIF_TIFF_MAX_LEVELS)
563 offset = format_debug_tiff_table(data, len, offset, bo, 0);
567 log_printf("*** end\n");
569 /* we are debugging, not trying to return any data */
576 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */