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.
30 #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 struct FormatRawEntry {
47 const gchar *extension;
48 FormatRawMatchType magic_type;
50 gconstpointer magic_pattern;
52 FormatRawExifType exif_type;
53 FormatRawExifParseFunc exif_func;
54 const gchar *description;
55 FormatRawParseFunc func_parse;
58 static FormatRawEntry format_raw_list[] = {
60 FORMAT_RAW_DEBUG_TIFF,
68 { nullptr, static_cast<FormatRawMatchType>(0), 0, nullptr, 0, static_cast<FormatRawExifType>(0), nullptr, nullptr, nullptr }
72 struct FormatExifEntry {
73 FormatExifMatchType header_type;
74 gconstpointer header_pattern;
76 const gchar *description;
77 FormatExifParseFunc func_parse;
80 static FormatExifEntry format_exif_list[] = {
85 { static_cast<FormatExifMatchType>(0), nullptr, 0, nullptr, nullptr }
89 static guint tiff_table(guchar *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 > 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 gboolean format_tiff_find_tag_data(guchar *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(guchar *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 gboolean format_raw_parse(FormatRawEntry *entry,
238 guchar *data, const guint len,
239 guint *image_offset, guint *exif_offset)
245 if (!entry || !entry->func_parse) return FALSE;
247 DEBUG_1("RAW using file parser for %s", 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 gboolean format_raw_img_exif_offsets(guchar *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(guchar *data, const guint len, guint *exif_offset,
280 FormatRawExifParseFunc *exif_parse_func)
282 FormatRawEntry *entry;
284 if (!data || len < 1) return static_cast<FormatRawExifType>FALSE;
286 entry = format_raw_find(data, len);
288 if (!entry || !entry->func_parse) return static_cast<FormatRawExifType>FALSE;
290 if (!format_raw_parse(entry, data, len, nullptr, 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 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
302 guchar *header_data, const guint header_len,
303 guint *image_offset, guint *exif_offset)
305 FormatRawEntry *entry;
306 gpointer map_data = nullptr;
311 if (!header_data || fd < 0) return FALSE;
313 /* given image pathname, first do simple (and fast) file extension test */
317 gboolean match = FALSE;
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 g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
335 if (!match) return FALSE;
337 DEBUG_1("RAW file parser extension match");
341 * @FIXME 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 log_printf("Failed to stat file %d\n", fd);
353 map_len = st.st_size;
354 map_data = mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
355 if (map_data == MAP_FAILED)
357 log_printf("Failed to mmap file %d\n", fd);
361 success = format_raw_parse(entry, static_cast<guchar*>(map_data), map_len, image_offset, exif_offset);
363 if (munmap(map_data, map_len) == -1)
365 log_printf("Failed to unmap file %d\n", fd);
368 if (success && image_offset)
370 if (lseek(fd, *image_offset, SEEK_SET) != static_cast<off_t>(*image_offset))
372 log_printf("Failed to seek to embedded image\n");
375 if (*exif_offset) *exif_offset = 0;
384 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, guchar *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 gboolean format_exif_makernote_parse(ExifData *exif, guchar *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 DEBUG_1("EXIF using makernote parser for %s", 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(guchar *data, const guint len, guint offset,
443 ExifByteOrder bo, gint level);
445 static void format_debug_tiff_entry(guchar *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 log_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 log_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 log_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 log_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 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
504 static guint format_debug_tiff_table(guchar *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 log_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 log_printf("%*s----------- end of #%d ------------\n", level, "", level);
527 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
530 gboolean format_debug_tiff_raw(guchar *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 log_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);
563 log_printf("*** end\n");
565 /* we are debugging, not trying to return any data */
570 using dummy_variable = int;
573 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */