Tue Nov 7 15:35:59 2006 John Ellis <johne@verizon.net>
[geeqie.git] / src / format_raw.c
1 /*
2  *  GQView
3  *  (C) 2006 John Ellis
4  *
5  *  Authors:
6  *    Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
7  *
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!
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #  include "config.h"
15 #endif
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24
25 #include <glib.h>
26
27 #include "intl.h"
28
29 #include "format_raw.h"
30
31 #include "format_canon.h"
32 #include "format_fuji.h"
33 #include "format_nikon.h"
34 #include "format_olympus.h"
35
36
37 /* so that debugging is honored */
38 extern gint debug;
39
40
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 FormatRawExifType exif_type;
49         FormatRawExifParseFunc exif_func;
50         const gchar *description;
51         FormatRawParseFunc func_parse;
52 };
53
54 static FormatRawEntry format_raw_list[] = {
55 #if DEBUG_RAW_TIFF
56         FORMAT_RAW_DEBUG_TIFF,
57 #endif
58         FORMAT_RAW_CANON,
59         FORMAT_RAW_FUJI,
60         FORMAT_RAW_NIKON,
61         FORMAT_RAW_OLYMPUS,
62         FORMAT_RAW_PENTAX,
63         { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
64 };
65
66
67 typedef struct _FormatExifEntry FormatExifEntry;
68 struct _FormatExifEntry {
69         FormatExifMatchType header_type;
70         const void *header_pattern;
71         const guint header_length;
72         const gchar *description;
73         FormatExifParseFunc func_parse;
74 };
75
76 static FormatExifEntry format_exif_list[] = {
77         FORMAT_EXIF_CANON,
78         FORMAT_EXIF_FUJI,
79         FORMAT_EXIF_NIKON,
80         FORMAT_EXIF_OLYMPUS,
81         { 0, NULL, 0, NULL }
82 };
83
84
85 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
86                         guint tag, ExifFormatType type,
87                         guint *result_offset, guint *result_count)
88 {
89         guint count;
90         guint i;
91
92         if (len < offset + 2) return 0;
93         if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
94
95         count = exif_byte_get_int16(data + offset, bo);
96         offset += 2;
97         if (len < offset + count * 12 + 4) return 0;
98
99         for (i = 0; i < count; i++)
100                 {
101                 guint segment;
102
103                 segment = offset + i * 12;
104                 if (exif_byte_get_int16(data + segment, bo) == tag &&
105                     exif_byte_get_int16(data + segment + 2, bo) == type)
106                         {
107                         guint chunk_count;
108                         guint chunk_offset;
109                         guint chunk_length;
110
111                         chunk_count = exif_byte_get_int32(data + segment + 4, bo);
112                         chunk_length = ExifFormatList[type].size * chunk_count;
113
114                         if (chunk_length > 4)
115                                 {
116                                 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
117                                 }
118                         else
119                                 {
120                                 chunk_offset = segment + 8;
121                                 }
122
123                         if (chunk_offset + chunk_length <= len)
124                                 {
125                                 *result_offset = chunk_offset;
126                                 *result_count = chunk_count;
127                                 }
128
129                         return 0;
130                         }
131                 }
132
133         return exif_byte_get_int32(data + offset + count * 12, bo);
134 }
135
136 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
137                                       guint tag, ExifFormatType type,
138                                       guint *result_offset, guint *result_count)
139 {
140         ExifByteOrder bo;
141         guint offset;
142
143         if (len < 8) return FALSE;
144
145         if (memcmp(data, "II", 2) == 0)
146                 {
147                 bo = EXIF_BYTE_ORDER_INTEL;
148                 }
149         else if (memcmp(data, "MM", 2) == 0)
150                 {
151                 bo = EXIF_BYTE_ORDER_MOTOROLA;
152                 }
153         else
154                 {
155                 return FALSE;
156                 }
157
158         if (exif_byte_get_int16(data + 2, bo) != 0x002A)
159                 {
160                 return FALSE;
161                 }
162
163         offset = exif_byte_get_int32(data + 4, bo);
164
165         while (offset != 0)
166                 {
167                 guint ro = 0;
168                 guint rc = 0;
169
170                 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
171                 if (ro != 0)
172                         {
173                         *result_offset = ro;
174                         *result_count = rc;
175                         return TRUE;
176                         }
177                 }
178
179         return FALSE;
180 }
181
182 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
183 {
184         gint n;
185         gint tiff;
186         guint make_count = 0;
187         guint make_offset = 0;
188
189         tiff = (len > 8 &&
190                 (memcmp(data, "II\x2a\x00", 4) == 0 ||
191                  memcmp(data, "MM\x00\x2a", 4) == 0));
192
193         n = 0;
194         while (format_raw_list[n].magic_pattern)
195                 {
196                 FormatRawEntry *entry = &format_raw_list[n];
197
198                 switch (entry->magic_type)
199                         {
200                         case FORMAT_RAW_MATCH_MAGIC:
201                                 if (entry->magic_length + entry->magic_offset <= len &&
202                                     memcmp(data + entry->magic_offset,
203                                            entry->magic_pattern, entry->magic_length) == 0)
204                                         {
205                                         return entry;
206                                         }
207                                 break;
208                         case FORMAT_RAW_MATCH_TIFF_MAKE:
209                                 if (tiff &&
210                                     make_offset == 0 &&
211                                     !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
212                                                                &make_offset, &make_count))
213                                         {
214                                         tiff = FALSE;
215                                         }
216                                 if (make_offset != 0 &&
217                                     make_count >= entry->magic_offset + entry->magic_length &&
218                                     memcmp(entry->magic_pattern,
219                                            data + make_offset + entry->magic_offset, entry->magic_length) == 0)
220                                         {
221                                         return entry;
222                                         }
223                                 break;
224                         default:
225                                 break;
226                         }
227                 n++;
228                 }
229
230         return NULL;
231 }
232
233 static gint format_raw_parse(FormatRawEntry *entry,
234                              unsigned char *data, const guint len,
235                              guint *image_offset, guint *exif_offset)
236 {
237         guint io = 0;
238         guint eo = 0;
239         gint found;
240
241         if (!entry || !entry->func_parse) return FALSE;
242
243         if (debug) printf("RAW using file parser for %s\n", entry->description);
244
245         found = entry->func_parse(data, len, &io, &eo);
246
247         if (!found ||
248             io >= len - 4 ||
249             eo >= len)
250                 {
251                 return FALSE;
252                 }
253
254         if (image_offset) *image_offset = io;
255         if (exif_offset) *exif_offset = eo;
256
257         return TRUE;
258 }
259
260 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
261                                  guint *image_offset, guint *exif_offset)
262 {
263         FormatRawEntry *entry;
264
265         if (!data || len < 1) return FALSE;
266
267         entry = format_raw_find(data, len);
268
269         if (!entry || !entry->func_parse) return FALSE;
270
271         return format_raw_parse(entry, data, len, image_offset, exif_offset);
272 }
273
274
275 FormatRawExifType format_raw_exif_offset(unsigned char *data, const guint len, guint *exif_offset,
276                                          FormatRawExifParseFunc *exif_parse_func)
277 {
278         FormatRawEntry *entry;
279
280         if (!data || len < 1) return FALSE;
281
282         entry = format_raw_find(data, len);
283
284         if (!entry || !entry->func_parse) return FALSE;
285
286         if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
287
288         if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
289                 {
290                 *exif_parse_func = entry->exif_func;
291                 }
292
293         return entry->exif_type;
294 }
295
296
297 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
298                                     unsigned char *header_data, const guint header_len,
299                                     guint *image_offset, guint *exif_offset)
300 {
301         FormatRawEntry *entry;
302         void *map_data = NULL;
303         size_t map_len = 0;
304         struct stat st;
305         gint success;
306
307         if (!header_data || fd < 0) return FALSE;
308
309         /* given image pathname, first do simple (and fast) file extension test */
310         if (path)
311                 {
312                 const gchar *ext;
313                 gint match = FALSE;
314                 gint i;
315
316                 ext = strrchr(path, '.');
317                 if (!ext) return FALSE;
318                 ext++;
319
320                 i = 0;
321                 while (!match && format_raw_list[i].magic_pattern)
322                         {
323                         if (format_raw_list[i].extension &&
324                             strcasecmp(format_raw_list[i].extension, ext) == 0)
325                                 {
326                                 match = TRUE;
327                                 }
328                         i++;
329                         }
330
331                 if (!match) return FALSE;
332
333                 if (debug) printf("RAW file parser extension match\n");
334                 }
335
336         /* FIXME:
337          * when the target is a tiff file it should be mmaped prior to format_raw_find as
338          * the make field data may not always be within header_data + header_len
339          */ 
340         entry = format_raw_find(header_data, header_len);
341
342         if (!entry || !entry->func_parse) return FALSE;
343
344         if (fstat(fd, &st) == -1)
345                 {
346                 printf("Failed to stat file %d\n", fd);
347                 return FALSE;
348                 }
349         map_len = st.st_size;
350         map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
351         if (map_data == MAP_FAILED)
352                 {
353                 printf("Failed to mmap file %d\n", fd);
354                 return FALSE;
355                 }
356
357         success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
358
359         if (munmap(map_data, map_len) == -1)
360                 {
361                 printf("Failed to unmap file %d\n", fd);
362                 }
363
364         if (success && image_offset)
365                 {
366                 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
367                         {
368                         printf("Failed to seek to embedded image\n");
369
370                         *image_offset = 0;
371                         if (*exif_offset) *exif_offset = 0;
372                         success = FALSE;
373                         }
374                 }
375
376         return success;
377 }
378
379
380 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
381                                                    guint offset, guint size)
382 {
383         ExifItem *make;
384         gint n;
385
386         make = exif_get_item(exif, "Make");
387
388         n = 0;
389         while (format_exif_list[n].header_pattern)
390                 {
391                 switch (format_exif_list[n].header_type)
392                         {
393                         case FORMAT_EXIF_MATCH_MAKERNOTE:
394                                 if (format_exif_list[n].header_length + offset < size &&
395                                     memcmp(tiff + offset, format_exif_list[n].header_pattern,
396                                                           format_exif_list[n].header_length) == 0)
397                                         {
398                                         return &format_exif_list[n];
399                                         }
400                                 break;
401                         case FORMAT_EXIF_MATCH_MAKE:
402                                 if (make &&
403                                     make->data_len >= format_exif_list[n].header_length &&
404                                     memcmp(make->data, format_exif_list[n].header_pattern,
405                                                        format_exif_list[n].header_length) == 0)
406                                         {
407                                         return &format_exif_list[n];
408                                         }
409                                 break;
410                         }
411                 n++;
412                 }
413
414         return FALSE;
415 }
416
417 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
418                                  guint size, ExifByteOrder bo)
419 {
420         FormatExifEntry *entry;
421
422         entry = format_exif_makernote_find(exif, tiff, offset, size);
423
424         if (!entry || !entry->func_parse) return FALSE;
425
426         if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
427
428         return entry->func_parse(exif, tiff, offset, size, bo);
429 }
430
431 /*
432  *-----------------------------------------------------------------------------
433  * Basic TIFF debugger, prints all IFD entries within tiff file
434  *-----------------------------------------------------------------------------
435  */
436 #if DEBUG_RAW_TIFF
437
438 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
439                                      ExifByteOrder bo, gint level);
440
441 static void format_debug_tiff_entry(unsigned char *data, const guint len, guint offset,
442                                     ExifByteOrder bo, gint level)
443 {
444         guint tag;
445         guint type;
446         guint count;
447         guint segment;
448         guint seg_len;
449
450         tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
451         type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
452         count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
453
454         seg_len = ExifFormatList[type].size * count;
455         if (seg_len > 4)
456                 {
457                 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
458                 if (segment + seg_len > len) return;
459                 }
460         else
461                 {
462                 segment = offset + EXIF_TIFD_OFFSET_DATA;
463                 }
464
465         printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
466                 level, "", tag, tag, type,
467                 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
468                 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
469
470         if (tag == 0x8769 || tag == 0x14a)
471                 {
472                 gint i;
473
474                 printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
475
476                 for (i = 0; i < count; i++)
477                         {
478                         guint subset;
479
480                         subset = exif_byte_get_int32(data + segment + i * 4, bo);
481                         format_debug_tiff_table(data, len, subset, bo, level + 1);
482                         }
483                 }
484         else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
485                 {
486                 guint subset = exif_byte_get_int32(data + segment, bo);
487                 printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
488                 }
489         else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
490                 {
491                 guint subset = exif_byte_get_int32(data + segment, bo);
492                 printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
493                 }
494 }
495
496 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
497                                      ExifByteOrder bo, gint level)
498 {
499         guint count;
500         guint i;
501
502         if (level > EXIF_TIFF_MAX_LEVELS) return 0;
503
504         if (len < offset + 2) return FALSE;
505
506         count = exif_byte_get_int16(data + offset, bo);
507         offset += 2;
508         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
509
510         printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
511
512         for (i = 0; i < count; i++)
513                 {
514                 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
515                 }
516
517         printf("%*s----------- end of #%d ------------\n", level, "", level);
518
519         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
520 }
521
522 gint format_debug_tiff_raw(unsigned char *data, const guint len,
523                            guint *image_offset, guint *exif_offset)
524 {
525         ExifByteOrder bo;
526         gint level;
527         guint offset;
528
529         if (len < 8) return FALSE;
530
531         /* for debugging, we are more relaxed as to magic header */
532         if (memcmp(data, "II", 2) == 0)
533                 {
534                 bo = EXIF_BYTE_ORDER_INTEL;
535                 }
536         else if (memcmp(data, "MM", 2) == 0)
537                 {
538                 bo = EXIF_BYTE_ORDER_MOTOROLA;
539                 }
540         else
541                 {
542                 return FALSE;
543                 }
544
545         printf("*** debug parsing tiff\n");
546
547         offset = exif_byte_get_int32(data + 4, bo);
548         level = 0;
549         while (offset && level < EXIF_TIFF_MAX_LEVELS)
550                 {
551                 offset = format_debug_tiff_table(data, len, offset, bo, 0);
552                 level++;
553                 }
554
555         printf("*** end\n");
556
557         /* we are debugging, not trying to return any data */
558         return FALSE;
559 }
560 #endif
561
562