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.
22 #include "format-raw.h"
35 #include "format-canon.h"
36 #include "format-fuji.h"
37 #include "format-nikon.h"
38 #include "format-olympus.h"
41 struct FormatRawEntry {
42 const gchar *extension;
43 FormatRawMatchType magic_type;
45 gconstpointer magic_pattern;
47 FormatRawExifType exif_type;
48 FormatRawExifParseFunc exif_func;
49 const gchar *description;
50 FormatRawParseFunc func_parse;
53 static FormatRawEntry format_raw_list[] = {
55 FORMAT_RAW_DEBUG_TIFF,
63 { nullptr, static_cast<FormatRawMatchType>(0), 0, nullptr, 0, static_cast<FormatRawExifType>(0), nullptr, nullptr, nullptr }
67 struct FormatExifEntry {
68 FormatExifMatchType header_type;
69 gconstpointer header_pattern;
71 const gchar *description;
72 FormatExifParseFunc func_parse;
75 static FormatExifEntry format_exif_list[] = {
80 { static_cast<FormatExifMatchType>(0), nullptr, 0, nullptr, nullptr }
84 static guint tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
85 guint tag, ExifFormatType type,
86 guint *result_offset, guint *result_count)
91 if (len < offset + 2) return 0;
92 if (type > EXIF_FORMAT_COUNT) return 0;
94 count = exif_byte_get_int16(data + offset, bo);
96 if (len < offset + count * 12 + 4) return 0;
98 for (i = 0; i < count; i++)
102 segment = offset + i * 12;
103 if (exif_byte_get_int16(data + segment, bo) == tag &&
104 exif_byte_get_int16(data + segment + 2, bo) == type)
110 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
111 chunk_length = ExifFormatList[type].size * chunk_count;
113 if (chunk_length > 4)
115 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
119 chunk_offset = segment + 8;
122 if (chunk_offset + chunk_length <= len)
124 *result_offset = chunk_offset;
125 *result_count = chunk_count;
132 return exif_byte_get_int32(data + offset + count * 12, bo);
135 static gboolean format_tiff_find_tag_data(guchar *data, const guint len,
136 guint tag, ExifFormatType type,
137 guint *result_offset, guint *result_count)
142 if (len < 8) return FALSE;
144 if (memcmp(data, "II", 2) == 0)
146 bo = EXIF_BYTE_ORDER_INTEL;
148 else if (memcmp(data, "MM", 2) == 0)
150 bo = EXIF_BYTE_ORDER_MOTOROLA;
157 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
162 offset = exif_byte_get_int32(data + 4, bo);
169 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
181 static FormatRawEntry *format_raw_find(guchar *data, const guint len)
185 guint make_count = 0;
186 guint make_offset = 0;
189 (memcmp(data, "II\x2a\x00", 4) == 0 ||
190 memcmp(data, "MM\x00\x2a", 4) == 0));
193 while (format_raw_list[n].magic_pattern)
195 FormatRawEntry *entry = &format_raw_list[n];
197 switch (entry->magic_type)
199 case FORMAT_RAW_MATCH_MAGIC:
200 if (entry->magic_length + entry->magic_offset <= len &&
201 memcmp(data + entry->magic_offset,
202 entry->magic_pattern, entry->magic_length) == 0)
207 case FORMAT_RAW_MATCH_TIFF_MAKE:
210 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
211 &make_offset, &make_count))
215 if (make_offset != 0 &&
216 make_count >= entry->magic_offset + entry->magic_length &&
217 memcmp(entry->magic_pattern,
218 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
232 static gboolean format_raw_parse(FormatRawEntry *entry,
233 guchar *data, const guint len,
234 guint *image_offset, guint *exif_offset)
240 if (!entry || !entry->func_parse) return FALSE;
242 DEBUG_1("RAW using file parser for %s", entry->description);
244 found = entry->func_parse(data, len, &io, &eo);
253 if (image_offset) *image_offset = io;
254 if (exif_offset) *exif_offset = eo;
259 gboolean format_raw_img_exif_offsets(guchar *data, const guint len,
260 guint *image_offset, guint *exif_offset)
262 FormatRawEntry *entry;
264 if (!data || len < 1) return FALSE;
266 entry = format_raw_find(data, len);
268 if (!entry || !entry->func_parse) return FALSE;
270 return format_raw_parse(entry, data, len, image_offset, exif_offset);
274 FormatRawExifType format_raw_exif_offset(guchar *data, const guint len, guint *exif_offset,
275 FormatRawExifParseFunc *exif_parse_func)
277 FormatRawEntry *entry;
279 if (!data || len < 1) return static_cast<FormatRawExifType>FALSE;
281 entry = format_raw_find(data, len);
283 if (!entry || !entry->func_parse) return static_cast<FormatRawExifType>FALSE;
285 if (!format_raw_parse(entry, data, len, nullptr, exif_offset)) return FORMAT_RAW_EXIF_NONE;
287 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
289 *exif_parse_func = entry->exif_func;
292 return entry->exif_type;
296 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
297 guchar *header_data, const guint header_len,
298 guint *image_offset, guint *exif_offset)
300 FormatRawEntry *entry;
301 gpointer map_data = nullptr;
306 if (!header_data || fd < 0) return FALSE;
308 /* given image pathname, first do simple (and fast) file extension test */
312 gboolean match = FALSE;
315 ext = strrchr(path, '.');
316 if (!ext) return FALSE;
320 while (!match && format_raw_list[i].magic_pattern)
322 if (format_raw_list[i].extension &&
323 g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
330 if (!match) return FALSE;
332 DEBUG_1("RAW file parser extension match");
336 * @FIXME when the target is a tiff file it should be mmaped prior to format_raw_find as
337 * the make field data may not always be within header_data + header_len
339 entry = format_raw_find(header_data, header_len);
341 if (!entry || !entry->func_parse) return FALSE;
343 if (fstat(fd, &st) == -1)
345 log_printf("Failed to stat file %d\n", fd);
348 map_len = st.st_size;
349 map_data = mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
350 if (map_data == MAP_FAILED)
352 log_printf("Failed to mmap file %d\n", fd);
356 success = format_raw_parse(entry, static_cast<guchar*>(map_data), map_len, image_offset, exif_offset);
358 if (munmap(map_data, map_len) == -1)
360 log_printf("Failed to unmap file %d\n", fd);
363 if (success && image_offset)
365 if (lseek(fd, *image_offset, SEEK_SET) != static_cast<off_t>(*image_offset))
367 log_printf("Failed to seek to embedded image\n");
370 if (*exif_offset) *exif_offset = 0;
379 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, guchar *tiff,
380 guint offset, guint size)
385 make = exif_get_item(exif, "Exif.Image.Make");
388 while (format_exif_list[n].header_pattern)
390 switch (format_exif_list[n].header_type)
392 case FORMAT_EXIF_MATCH_MAKERNOTE:
393 if (format_exif_list[n].header_length + offset < size &&
394 memcmp(tiff + offset, format_exif_list[n].header_pattern,
395 format_exif_list[n].header_length) == 0)
397 return &format_exif_list[n];
400 case FORMAT_EXIF_MATCH_MAKE:
402 make->data_len >= format_exif_list[n].header_length &&
403 memcmp(make->data, format_exif_list[n].header_pattern,
404 format_exif_list[n].header_length) == 0)
406 return &format_exif_list[n];
416 gboolean format_exif_makernote_parse(ExifData *exif, guchar *tiff, guint offset,
417 guint size, ExifByteOrder bo)
419 FormatExifEntry *entry;
421 entry = format_exif_makernote_find(exif, tiff, offset, size);
423 if (!entry || !entry->func_parse) return FALSE;
425 DEBUG_1("EXIF using makernote parser for %s", entry->description);
427 return entry->func_parse(exif, tiff, offset, size, bo);
431 *-----------------------------------------------------------------------------
432 * Basic TIFF debugger, prints all IFD entries within tiff file
433 *-----------------------------------------------------------------------------
437 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
438 ExifByteOrder bo, gint level);
440 static void format_debug_tiff_entry(guchar *data, const guint len, guint offset,
441 ExifByteOrder bo, gint level)
449 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
450 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
451 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
453 seg_len = ExifFormatList[type].size * count;
456 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
457 if (segment + seg_len > len) return;
461 segment = offset + EXIF_TIFD_OFFSET_DATA;
464 log_printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
465 level, "", tag, tag, type,
466 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
467 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
469 if (tag == 0x8769 || tag == 0x14a)
473 log_printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
475 for (i = 0; i < count; i++)
479 subset = exif_byte_get_int32(data + segment + i * 4, bo);
480 format_debug_tiff_table(data, len, subset, bo, level + 1);
483 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
485 log_printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
487 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
489 guint subset = exif_byte_get_int32(data + segment, bo);
490 log_printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
492 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
494 guint subset = exif_byte_get_int32(data + segment, bo);
495 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
499 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
500 ExifByteOrder bo, gint level)
505 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
507 if (len < offset + 2) return FALSE;
509 count = exif_byte_get_int16(data + offset, bo);
511 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
513 log_printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
515 for (i = 0; i < count; i++)
517 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
520 log_printf("%*s----------- end of #%d ------------\n", level, "", level);
522 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
525 gboolean format_debug_tiff_raw(guchar *data, const guint len,
526 guint *image_offset, guint *exif_offset)
532 if (len < 8) return FALSE;
534 /* for debugging, we are more relaxed as to magic header */
535 if (memcmp(data, "II", 2) == 0)
537 bo = EXIF_BYTE_ORDER_INTEL;
539 else if (memcmp(data, "MM", 2) == 0)
541 bo = EXIF_BYTE_ORDER_MOTOROLA;
548 log_printf("*** debug parsing tiff\n");
550 offset = exif_byte_get_int32(data + 4, bo);
552 while (offset && level < EXIF_TIFF_MAX_LEVELS)
554 offset = format_debug_tiff_table(data, len, offset, bo, 0);
558 log_printf("*** end\n");
560 /* we are debugging, not trying to return any data */
564 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */