Remove commented out code.
[geeqie.git] / src / format_raw.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  *  Authors:
7  *    Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
8  *
9  * This software is released under the GNU General Public License (GNU GPL).
10  * Please read the included file COPYING for more information.
11  * This software comes with no warranty of any kind, use at your own risk!
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #  include "config.h"
16 #endif
17
18 #ifndef HAVE_EXIV2
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26
27 #include <glib.h>
28
29 #include "intl.h"
30
31 #include "main.h"
32 #include "format_raw.h"
33
34 #include "format_canon.h"
35 #include "format_fuji.h"
36 #include "format_nikon.h"
37 #include "format_olympus.h"
38
39
40 typedef struct _FormatRawEntry FormatRawEntry;
41 struct _FormatRawEntry {
42         const gchar *extension;
43         FormatRawMatchType magic_type;
44         const guint magic_offset;
45         gconstpointer magic_pattern;
46         const guint magic_length;
47         const FormatRawExifType exif_type;
48         FormatRawExifParseFunc exif_func;
49         const gchar *description;
50         FormatRawParseFunc func_parse;
51 };
52
53 static FormatRawEntry format_raw_list[] = {
54 #if DEBUG_RAW_TIFF
55         FORMAT_RAW_DEBUG_TIFF,
56 #endif
57         FORMAT_RAW_CANON,
58         FORMAT_RAW_FUJI,
59         FORMAT_RAW_NIKON,
60         FORMAT_RAW_OLYMPUS,
61         FORMAT_RAW_PENTAX,
62         FORMAT_RAW_SAMSUNG,
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         gconstpointer 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, NULL }
82 };
83
84
85 static guint tiff_table(guchar *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 > 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 gboolean format_tiff_find_tag_data(guchar *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(guchar *data, const guint len)
183 {
184         gint n;
185         gboolean 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 gboolean format_raw_parse(FormatRawEntry *entry,
234                                  guchar *data, const guint len,
235                                  guint *image_offset, guint *exif_offset)
236 {
237         guint io = 0;
238         guint eo = 0;
239         gboolean found;
240
241         if (!entry || !entry->func_parse) return FALSE;
242
243         DEBUG_1("RAW using file parser for %s", 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 gboolean format_raw_img_exif_offsets(guchar *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(guchar *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 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
298                                         guchar *header_data, const guint header_len,
299                                         guint *image_offset, guint *exif_offset)
300 {
301         FormatRawEntry *entry;
302         gpointer map_data = NULL;
303         size_t map_len = 0;
304         struct stat st;
305         gboolean 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                 gboolean 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                             g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
325                                 {
326                                 match = TRUE;
327                                 }
328                         i++;
329                         }
330
331                 if (!match) return FALSE;
332
333                 DEBUG_1("RAW file parser extension match");
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                 log_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                 log_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                 log_printf("Failed to unmap file %d\n", fd);
362                 }
363
364         if (success && image_offset)
365                 {
366                 if (lseek(fd, *image_offset, SEEK_SET) != (off_t) *image_offset)
367                         {
368                         log_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, guchar *tiff,
381                                                    guint offset, guint size)
382 {
383         ExifItem *make;
384         gint n;
385
386         make = exif_get_item(exif, "Exif.Image.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 gboolean format_exif_makernote_parse(ExifData *exif, guchar *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         DEBUG_1("EXIF using makernote parser for %s", 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(guchar *data, const guint len, guint offset,
439                                      ExifByteOrder bo, gint level);
440
441 static void format_debug_tiff_entry(guchar *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         log_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                 log_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 == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
485                 {
486                 log_printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
487                 }
488         else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
489                 {
490                 guint subset = exif_byte_get_int32(data + segment, bo);
491                 log_printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
492                 }
493         else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
494                 {
495                 guint subset = exif_byte_get_int32(data + segment, bo);
496                 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
497                 }
498 }
499
500 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
501                                      ExifByteOrder bo, gint level)
502 {
503         guint count;
504         guint i;
505
506         if (level > EXIF_TIFF_MAX_LEVELS) return 0;
507
508         if (len < offset + 2) return FALSE;
509
510         count = exif_byte_get_int16(data + offset, bo);
511         offset += 2;
512         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
513
514         log_printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
515
516         for (i = 0; i < count; i++)
517                 {
518                 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
519                 }
520
521         log_printf("%*s----------- end of #%d ------------\n", level, "", level);
522
523         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
524 }
525
526 gboolean format_debug_tiff_raw(guchar *data, const guint len,
527                                guint *image_offset, guint *exif_offset)
528 {
529         ExifByteOrder bo;
530         gint level;
531         guint offset;
532
533         if (len < 8) return FALSE;
534
535         /* for debugging, we are more relaxed as to magic header */
536         if (memcmp(data, "II", 2) == 0)
537                 {
538                 bo = EXIF_BYTE_ORDER_INTEL;
539                 }
540         else if (memcmp(data, "MM", 2) == 0)
541                 {
542                 bo = EXIF_BYTE_ORDER_MOTOROLA;
543                 }
544         else
545                 {
546                 return FALSE;
547                 }
548
549         log_printf("*** debug parsing tiff\n");
550
551         offset = exif_byte_get_int32(data + 4, bo);
552         level = 0;
553         while (offset && level < EXIF_TIFF_MAX_LEVELS)
554                 {
555                 offset = format_debug_tiff_table(data, len, offset, bo, 0);
556                 level++;
557                 }
558
559         log_printf("*** end\n");
560
561         /* we are debugging, not trying to return any data */
562         return FALSE;
563 }
564 #endif
565
566 #endif
567 /* not HAVE_EXIV2 */
568 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */