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