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"
34 #include "format_olympus.h"
37 /* so that debugging is honored */
41 typedef struct _FormatRawEntry FormatRawEntry;
42 struct _FormatRawEntry {
43 const gchar *extension;
44 FormatRawMatchType magic_type;
45 const guint magic_offset;
46 const void *magic_pattern;
47 const guint magic_length;
48 const gchar *description;
49 FormatRawParseFunc func_parse;
52 static FormatRawEntry format_raw_list[] = {
56 { NULL, 0, 0, NULL, 0, NULL, NULL }
60 typedef struct _FormatExifEntry FormatExifEntry;
61 struct _FormatExifEntry {
62 FormatExifMatchType header_type;
63 const void *header_pattern;
64 const guint header_length;
65 const gchar *description;
66 FormatExifParseFunc func_parse;
69 static FormatExifEntry format_exif_list[] = {
78 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
79 guint tag, ExifFormatType type,
80 guint *result_offset, guint *result_count)
85 if (len < offset + 2) return 0;
86 if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
88 count = exif_byte_get_int16(data + offset, bo);
90 if (len < offset + count * 12 + 4) return 0;
92 for (i = 0; i < count; i++)
96 segment = offset + i * 12;
97 if (exif_byte_get_int16(data + segment, bo) == tag &&
98 exif_byte_get_int16(data + segment + 2, bo) == type)
104 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
105 chunk_length = ExifFormatList[type].size * chunk_count;
107 if (chunk_length > 4)
109 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
113 chunk_offset = segment + 8;
116 if (chunk_offset + chunk_length <= len)
118 *result_offset = chunk_offset;
119 *result_count = chunk_count;
126 return exif_byte_get_int32(data + offset + count * 12, bo);
129 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
130 guint tag, ExifFormatType type,
131 guint *result_offset, guint *result_count)
136 if (len < 8) return FALSE;
138 if (memcmp(data, "II", 2) == 0)
140 bo = EXIF_BYTE_ORDER_INTEL;
142 else if (memcmp(data, "MM", 2) == 0)
144 bo = EXIF_BYTE_ORDER_MOTOROLA;
151 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
156 offset = exif_byte_get_int32(data + 4, bo);
163 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
175 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
179 guint make_count = 0;
180 guint make_offset = 0;
183 (memcmp(data, "II\x2a\x00", 4) == 0 ||
184 memcmp(data, "MM\x00\x2a", 4) == 0));
187 while (format_raw_list[n].magic_pattern)
189 FormatRawEntry *entry = &format_raw_list[n];
191 switch (entry->magic_type)
193 case FORMAT_RAW_MATCH_MAGIC:
194 if (entry->magic_length + entry->magic_offset <= len &&
195 memcmp(data + entry->magic_offset,
196 entry->magic_pattern, entry->magic_length) == 0)
201 case FORMAT_RAW_MATCH_TIFF_MAKE:
204 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
205 &make_offset, &make_count))
209 if (make_offset != 0 &&
210 make_count >= entry->magic_offset + entry->magic_length &&
211 memcmp(entry->magic_pattern,
212 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
226 static gint format_raw_parse(FormatRawEntry *entry,
227 unsigned char *data, const guint len,
228 guint *image_offset, guint *exif_offset)
234 if (!entry || !entry->func_parse) return FALSE;
236 if (debug) printf("RAW using file parser for %s\n", entry->description);
238 found = entry->func_parse(data, len, &io, &eo);
247 if (image_offset) *image_offset = io;
248 if (exif_offset) *exif_offset = eo;
253 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
254 guint *image_offset, guint *exif_offset)
256 FormatRawEntry *entry;
258 if (!data || len < 1) return FALSE;
260 entry = format_raw_find(data, len);
262 if (!entry || !entry->func_parse) return FALSE;
264 return format_raw_parse(entry, data, len, image_offset, exif_offset);
268 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
269 unsigned char *header_data, const guint header_len,
270 guint *image_offset, guint *exif_offset)
272 FormatRawEntry *entry;
273 void *map_data = NULL;
278 if (!header_data || fd < 0) return FALSE;
280 /* given image pathname, first do simple (and fast) file extension test */
287 ext = strrchr(path, '.');
288 if (!ext) return FALSE;
292 while (!match && format_raw_list[i].magic_pattern)
294 if (format_raw_list[i].extension &&
295 strcasecmp(format_raw_list[i].extension, ext) == 0)
302 if (!match) return FALSE;
304 if (debug) printf("RAW file parser extension match\n");
307 entry = format_raw_find(header_data, header_len);
309 if (!entry || !entry->func_parse) return FALSE;
311 if (fstat(fd, &st) == -1)
313 printf("Failed to stat file %d\n", fd);
316 map_len = st.st_size;
317 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
318 if (map_data == MAP_FAILED)
320 printf("Failed to mmap file %d\n", fd);
324 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
326 if (munmap(map_data, map_len) == -1)
328 printf("Failed to unmap file %d\n", fd);
331 if (success && image_offset)
333 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
335 printf("Failed to seek to embedded image\n");
338 if (*exif_offset) *exif_offset = 0;
347 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
348 guint offset, guint size)
353 make = exif_get_item(exif, "Make");
356 while (format_exif_list[n].header_pattern)
358 switch (format_exif_list[n].header_type)
360 case FORMAT_EXIF_MATCH_MAKERNOTE:
361 if (format_exif_list[n].header_length + offset < size &&
362 memcmp(tiff + offset, format_exif_list[n].header_pattern,
363 format_exif_list[n].header_length) == 0)
365 return &format_exif_list[n];
368 case FORMAT_EXIF_MATCH_MAKE:
370 make->data_len >= format_exif_list[n].header_length &&
371 memcmp(make->data, format_exif_list[n].header_pattern,
372 format_exif_list[n].header_length) == 0)
374 return &format_exif_list[n];
384 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
385 guint size, ExifByteOrder bo)
387 FormatExifEntry *entry;
389 entry = format_exif_makernote_find(exif, tiff, offset, size);
391 if (!entry || !entry->func_parse) return FALSE;
393 if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
395 return entry->func_parse(exif, tiff, offset, size, bo);