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"
35 typedef struct _FormatEntry FormatEntry;
37 const void *header_pattern;
38 const guint header_length;
39 const gchar *description;
40 FormatRawParseFunc func_parse;
44 static FormatEntry format_list[] = {
47 { NULL, 0, NULL, NULL }
51 static FormatEntry *format_raw_find(const void *data, const guint len)
56 while (format_list[n].header_pattern)
58 if (format_list[n].header_length <= len &&
59 memcmp(data, format_list[n].header_pattern, format_list[n].header_length) == 0)
61 return &format_list[n];
69 static gint format_raw_parse(FormatEntry *entry,
70 const void *data, const guint len,
71 guint *image_offset, guint *exif_offset)
77 if (!entry || !entry->func_parse) return FALSE;
79 found = entry->func_parse(data, len, &io, &eo);
88 if (image_offset) *image_offset = io;
89 if (exif_offset) *exif_offset = eo;
94 gint format_raw_img_exif_offsets(const void *data, const guint len,
95 guint *image_offset, guint *exif_offset)
99 if (!data || len < 1) return FALSE;
101 entry = format_raw_find(data, len);
103 if (!entry || !entry->func_parse) return FALSE;
105 return format_raw_parse(entry, data, len, image_offset, exif_offset);
109 gint format_raw_img_exif_offsets_fd(int fd, const void *header_data, const guint header_len,
110 guint *image_offset, guint *exif_offset)
113 void *map_data = NULL;
118 if (!header_data || fd < 0) return FALSE;
120 entry = format_raw_find(header_data, header_len);
122 if (!entry || !entry->func_parse) return FALSE;
124 if (fstat(fd, &st) == -1)
126 printf("Failed to stat file %d\n", fd);
129 map_len = st.st_size;
130 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
131 if (map_data == MAP_FAILED)
133 printf("Failed to mmap file %d\n", fd);
137 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
139 if (munmap(map_data, map_len) == -1)
141 printf("Failed to unmap file %d\n", fd);
144 if (success && image_offset)
146 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
148 printf("Failed to seek to embedded image\n");
151 if (*exif_offset) *exif_offset = 0;