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.
26 #include "format-raw.h"
32 #include <sys/types.h>
38 #include "format-canon.h"
39 #include "format-fuji.h"
40 #include "format-nikon.h"
41 #include "format-olympus.h"
44 struct FormatRawEntry {
45 const gchar *extension;
46 FormatRawMatchType magic_type;
48 gconstpointer magic_pattern;
50 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 { nullptr, static_cast<FormatRawMatchType>(0), 0, nullptr, 0, static_cast<FormatRawExifType>(0), nullptr, nullptr, nullptr }
70 struct FormatExifEntry {
71 FormatExifMatchType header_type;
72 gconstpointer header_pattern;
74 const gchar *description;
75 FormatExifParseFunc func_parse;
78 static FormatExifEntry format_exif_list[] = {
83 { static_cast<FormatExifMatchType>(0), nullptr, 0, nullptr, nullptr }
87 static guint tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
88 guint tag, ExifFormatType type,
89 guint *result_offset, guint *result_count)
94 if (len < offset + 2) return 0;
95 if (type > EXIF_FORMAT_COUNT) return 0;
97 count = exif_byte_get_int16(data + offset, bo);
99 if (len < offset + count * 12 + 4) return 0;
101 for (i = 0; i < count; i++)
105 segment = offset + i * 12;
106 if (exif_byte_get_int16(data + segment, bo) == tag &&
107 exif_byte_get_int16(data + segment + 2, bo) == type)
113 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
114 chunk_length = ExifFormatList[type].size * chunk_count;
116 if (chunk_length > 4)
118 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
122 chunk_offset = segment + 8;
125 if (chunk_offset + chunk_length <= len)
127 *result_offset = chunk_offset;
128 *result_count = chunk_count;
135 return exif_byte_get_int32(data + offset + count * 12, bo);
138 static gboolean format_tiff_find_tag_data(guchar *data, const guint len,
139 guint tag, ExifFormatType type,
140 guint *result_offset, guint *result_count)
145 if (len < 8) return FALSE;
147 if (memcmp(data, "II", 2) == 0)
149 bo = EXIF_BYTE_ORDER_INTEL;
151 else if (memcmp(data, "MM", 2) == 0)
153 bo = EXIF_BYTE_ORDER_MOTOROLA;
160 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
165 offset = exif_byte_get_int32(data + 4, bo);
172 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
184 static FormatRawEntry *format_raw_find(guchar *data, const guint len)
188 guint make_count = 0;
189 guint make_offset = 0;
192 (memcmp(data, "II\x2a\x00", 4) == 0 ||
193 memcmp(data, "MM\x00\x2a", 4) == 0));
196 while (format_raw_list[n].magic_pattern)
198 FormatRawEntry *entry = &format_raw_list[n];
200 switch (entry->magic_type)
202 case FORMAT_RAW_MATCH_MAGIC:
203 if (entry->magic_length + entry->magic_offset <= len &&
204 memcmp(data + entry->magic_offset,
205 entry->magic_pattern, entry->magic_length) == 0)
210 case FORMAT_RAW_MATCH_TIFF_MAKE:
213 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
214 &make_offset, &make_count))
218 if (make_offset != 0 &&
219 make_count >= entry->magic_offset + entry->magic_length &&
220 memcmp(entry->magic_pattern,
221 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
235 static gboolean format_raw_parse(FormatRawEntry *entry,
236 guchar *data, const guint len,
237 guint *image_offset, guint *exif_offset)
243 if (!entry || !entry->func_parse) return FALSE;
245 DEBUG_1("RAW using file parser for %s", entry->description);
247 found = entry->func_parse(data, len, &io, &eo);
256 if (image_offset) *image_offset = io;
257 if (exif_offset) *exif_offset = eo;
262 gboolean format_raw_img_exif_offsets(guchar *data, const guint len,
263 guint *image_offset, guint *exif_offset)
265 FormatRawEntry *entry;
267 if (!data || len < 1) return FALSE;
269 entry = format_raw_find(data, len);
271 if (!entry || !entry->func_parse) return FALSE;
273 return format_raw_parse(entry, data, len, image_offset, exif_offset);
277 FormatRawExifType format_raw_exif_offset(guchar *data, const guint len, guint *exif_offset,
278 FormatRawExifParseFunc *exif_parse_func)
280 FormatRawEntry *entry;
282 if (!data || len < 1) return static_cast<FormatRawExifType>FALSE;
284 entry = format_raw_find(data, len);
286 if (!entry || !entry->func_parse) return static_cast<FormatRawExifType>FALSE;
288 if (!format_raw_parse(entry, data, len, nullptr, exif_offset)) return FORMAT_RAW_EXIF_NONE;
290 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
292 *exif_parse_func = entry->exif_func;
295 return entry->exif_type;
299 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
300 guchar *header_data, const guint header_len,
301 guint *image_offset, guint *exif_offset)
303 FormatRawEntry *entry;
304 gpointer map_data = nullptr;
309 if (!header_data || fd < 0) return FALSE;
311 /* given image pathname, first do simple (and fast) file extension test */
315 gboolean match = FALSE;
318 ext = strrchr(path, '.');
319 if (!ext) return FALSE;
323 while (!match && format_raw_list[i].magic_pattern)
325 if (format_raw_list[i].extension &&
326 g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
333 if (!match) return FALSE;
335 DEBUG_1("RAW file parser extension match");
339 * @FIXME when the target is a tiff file it should be mmaped prior to format_raw_find as
340 * the make field data may not always be within header_data + header_len
342 entry = format_raw_find(header_data, header_len);
344 if (!entry || !entry->func_parse) return FALSE;
346 if (fstat(fd, &st) == -1)
348 log_printf("Failed to stat file %d\n", fd);
351 map_len = st.st_size;
352 map_data = mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
353 if (map_data == MAP_FAILED)
355 log_printf("Failed to mmap file %d\n", fd);
359 success = format_raw_parse(entry, static_cast<guchar*>(map_data), map_len, image_offset, exif_offset);
361 if (munmap(map_data, map_len) == -1)
363 log_printf("Failed to unmap file %d\n", fd);
366 if (success && image_offset)
368 if (lseek(fd, *image_offset, SEEK_SET) != static_cast<off_t>(*image_offset))
370 log_printf("Failed to seek to embedded image\n");
373 if (*exif_offset) *exif_offset = 0;
382 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, guchar *tiff,
383 guint offset, guint size)
388 make = exif_get_item(exif, "Exif.Image.Make");
391 while (format_exif_list[n].header_pattern)
393 switch (format_exif_list[n].header_type)
395 case FORMAT_EXIF_MATCH_MAKERNOTE:
396 if (format_exif_list[n].header_length + offset < size &&
397 memcmp(tiff + offset, format_exif_list[n].header_pattern,
398 format_exif_list[n].header_length) == 0)
400 return &format_exif_list[n];
403 case FORMAT_EXIF_MATCH_MAKE:
405 make->data_len >= format_exif_list[n].header_length &&
406 memcmp(make->data, format_exif_list[n].header_pattern,
407 format_exif_list[n].header_length) == 0)
409 return &format_exif_list[n];
419 gboolean format_exif_makernote_parse(ExifData *exif, guchar *tiff, guint offset,
420 guint size, ExifByteOrder bo)
422 FormatExifEntry *entry;
424 entry = format_exif_makernote_find(exif, tiff, offset, size);
426 if (!entry || !entry->func_parse) return FALSE;
428 DEBUG_1("EXIF using makernote parser for %s", entry->description);
430 return entry->func_parse(exif, tiff, offset, size, bo);
434 *-----------------------------------------------------------------------------
435 * Basic TIFF debugger, prints all IFD entries within tiff file
436 *-----------------------------------------------------------------------------
440 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
441 ExifByteOrder bo, gint level);
443 static void format_debug_tiff_entry(guchar *data, const guint len, guint offset,
444 ExifByteOrder bo, gint level)
452 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
453 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
454 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
456 seg_len = ExifFormatList[type].size * count;
459 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
460 if (segment + seg_len > len) return;
464 segment = offset + EXIF_TIFD_OFFSET_DATA;
467 log_printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
468 level, "", tag, tag, type,
469 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
470 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
472 if (tag == 0x8769 || tag == 0x14a)
476 log_printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
478 for (i = 0; i < count; i++)
482 subset = exif_byte_get_int32(data + segment + i * 4, bo);
483 format_debug_tiff_table(data, len, subset, bo, level + 1);
486 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
488 log_printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
490 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
492 guint subset = exif_byte_get_int32(data + segment, bo);
493 log_printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
495 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
497 guint subset = exif_byte_get_int32(data + segment, bo);
498 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
502 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
503 ExifByteOrder bo, gint level)
508 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
510 if (len < offset + 2) return FALSE;
512 count = exif_byte_get_int16(data + offset, bo);
514 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
516 log_printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
518 for (i = 0; i < count; i++)
520 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
523 log_printf("%*s----------- end of #%d ------------\n", level, "", level);
525 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
528 gboolean format_debug_tiff_raw(guchar *data, const guint len,
529 guint *image_offset, guint *exif_offset)
535 if (len < 8) return FALSE;
537 /* for debugging, we are more relaxed as to magic header */
538 if (memcmp(data, "II", 2) == 0)
540 bo = EXIF_BYTE_ORDER_INTEL;
542 else if (memcmp(data, "MM", 2) == 0)
544 bo = EXIF_BYTE_ORDER_MOTOROLA;
551 log_printf("*** debug parsing tiff\n");
553 offset = exif_byte_get_int32(data + 4, bo);
555 while (offset && level < EXIF_TIFF_MAX_LEVELS)
557 offset = format_debug_tiff_table(data, len, offset, bo, 0);
561 log_printf("*** end\n");
563 /* we are debugging, not trying to return any data */
568 using dummy_variable = int;
571 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */