6 * Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
21 #include <sys/types.h>
29 #include "format_raw.h"
31 #include "format_canon.h"
32 #include "format_fuji.h"
33 #include "format_nikon.h"
36 /* so that debugging is honored */
40 typedef struct _FormatRawEntry FormatRawEntry;
41 struct _FormatRawEntry {
42 const gchar *extension;
43 FormatRawMatchType magic_type;
44 const guint magic_offset;
45 const void *magic_pattern;
46 const guint magic_length;
47 const gchar *description;
48 FormatRawParseFunc func_parse;
51 static FormatRawEntry format_raw_list[] = {
55 { NULL, 0, 0, NULL, 0, NULL, NULL }
59 typedef struct _FormatExifEntry FormatExifEntry;
60 struct _FormatExifEntry {
61 FormatExifMatchType header_type;
62 const void *header_pattern;
63 const guint header_length;
64 const gchar *description;
65 FormatExifParseFunc func_parse;
68 static FormatExifEntry format_exif_list[] = {
76 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
77 guint tag, ExifFormatType type,
78 guint *result_offset, guint *result_count)
83 if (len < offset + 2) return 0;
84 if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
86 count = exif_byte_get_int16(data + offset, bo);
88 if (len < offset + count * 12 + 4) return 0;
90 for (i = 0; i < count; i++)
94 segment = offset + i * 12;
95 if (exif_byte_get_int16(data + segment, bo) == tag &&
96 exif_byte_get_int16(data + segment + 2, bo) == type)
102 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
103 chunk_length = ExifFormatList[type].size * chunk_count;
105 if (chunk_length > 4)
107 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
111 chunk_offset = segment + 8;
114 if (chunk_offset + chunk_length <= len)
116 *result_offset = chunk_offset;
117 *result_count = chunk_count;
124 return exif_byte_get_int32(data + offset + count * 12, bo);
127 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
128 guint tag, ExifFormatType type,
129 guint *result_offset, guint *result_count)
134 if (len < 8) return FALSE;
136 if (memcmp(data, "II", 2) == 0)
138 bo = EXIF_BYTE_ORDER_INTEL;
140 else if (memcmp(data, "MM", 2) == 0)
142 bo = EXIF_BYTE_ORDER_MOTOROLA;
149 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
154 offset = exif_byte_get_int32(data + 4, bo);
161 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
173 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
177 guint make_count = 0;
178 guint make_offset = 0;
181 (memcmp(data, "II\x2a\x00", 4) == 0 ||
182 memcmp(data, "MM\x00\x2a", 4) == 0));
185 while (format_raw_list[n].magic_pattern)
187 FormatRawEntry *entry = &format_raw_list[n];
189 switch (entry->magic_type)
191 case FORMAT_RAW_MATCH_MAGIC:
192 if (entry->magic_length + entry->magic_offset <= len &&
193 memcmp(data + entry->magic_offset,
194 entry->magic_pattern, entry->magic_length) == 0)
199 case FORMAT_RAW_MATCH_TIFF_MAKE:
202 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
203 &make_offset, &make_count))
207 if (make_offset != 0 &&
208 make_count >= entry->magic_offset + entry->magic_length &&
209 memcmp(entry->magic_pattern,
210 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
224 static gint format_raw_parse(FormatRawEntry *entry,
225 unsigned char *data, const guint len,
226 guint *image_offset, guint *exif_offset)
232 if (!entry || !entry->func_parse) return FALSE;
234 if (debug) printf("RAW using file parser for %s\n", entry->description);
236 found = entry->func_parse(data, len, &io, &eo);
245 if (image_offset) *image_offset = io;
246 if (exif_offset) *exif_offset = eo;
251 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
252 guint *image_offset, guint *exif_offset)
254 FormatRawEntry *entry;
256 if (!data || len < 1) return FALSE;
258 entry = format_raw_find(data, len);
260 if (!entry || !entry->func_parse) return FALSE;
262 return format_raw_parse(entry, data, len, image_offset, exif_offset);
266 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
267 unsigned char *header_data, const guint header_len,
268 guint *image_offset, guint *exif_offset)
270 FormatRawEntry *entry;
271 void *map_data = NULL;
276 if (!header_data || fd < 0) return FALSE;
278 /* given image pathname, first do simple (and fast) file extension test */
285 ext = strrchr(path, '.');
286 if (!ext) return FALSE;
290 while (!match && format_raw_list[i].magic_pattern)
292 if (format_raw_list[i].extension &&
293 strcasecmp(format_raw_list[i].extension, ext) == 0)
300 if (!match) return FALSE;
302 if (debug) printf("RAW file parser extension match\n");
305 entry = format_raw_find(header_data, header_len);
307 if (!entry || !entry->func_parse) return FALSE;
309 if (fstat(fd, &st) == -1)
311 printf("Failed to stat file %d\n", fd);
314 map_len = st.st_size;
315 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
316 if (map_data == MAP_FAILED)
318 printf("Failed to mmap file %d\n", fd);
322 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
324 if (munmap(map_data, map_len) == -1)
326 printf("Failed to unmap file %d\n", fd);
329 if (success && image_offset)
331 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
333 printf("Failed to seek to embedded image\n");
336 if (*exif_offset) *exif_offset = 0;
345 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
346 guint offset, guint size)
351 make = exif_get_item(exif, "Make");
354 while (format_exif_list[n].header_pattern)
356 switch (format_exif_list[n].header_type)
358 case FORMAT_EXIF_MATCH_MAKERNOTE:
359 if (format_exif_list[n].header_length + offset < size &&
360 memcmp(tiff + offset, format_exif_list[n].header_pattern,
361 format_exif_list[n].header_length) == 0)
363 return &format_exif_list[n];
366 case FORMAT_EXIF_MATCH_MAKE:
368 make->data_len >= format_exif_list[n].header_length &&
369 memcmp(make->data, format_exif_list[n].header_pattern,
370 format_exif_list[n].header_length) == 0)
372 return &format_exif_list[n];
382 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
383 guint size, ExifByteOrder bo)
385 FormatExifEntry *entry;
387 entry = format_exif_makernote_find(exif, tiff, offset, size);
389 if (!entry || !entry->func_parse) return FALSE;
391 if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
393 return entry->func_parse(exif, tiff, offset, size, bo);