1 /*
2 
3     File: file_tiff.c
4 
5     Copyright (C) 1998-2005,2007-2009 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software 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 the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #ifdef HAVE_TIME_H
30 #include <time.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #include <stdio.h>
36 #include "types.h"
37 #include "filegen.h"
38 #include "common.h"
39 #include "file_tiff.h"
40 #include "log.h"
41 
42 static void register_header_check_tiff(file_stat_t *file_stat);
43 
44 const file_hint_t file_hint_tiff= {
45   .extension="tif",
46   .description="Tag Image File Format and some raw file formats (pef/nef/dcr/sr2/cr2)",
47   .max_filesize=100*1024*1024,
48   .recover=1,
49   .enable_by_default=1,
50   .register_header_check=&register_header_check_tiff
51 };
52 
tiff_type2size(const unsigned int type)53 unsigned int tiff_type2size(const unsigned int type)
54 {
55   switch(type)
56   {
57     case 1:	/* TIFF_BYTE	*/
58     case 2:	/* TIFF_ASCII	*/
59     case 6:	/* TIFF_SBYTE	*/
60     case 7:	/* TIFF_UNDEFINED */
61       return 1;
62     case 3:	/* TIFF_SHORT	*/
63     case 8:	/* TIFF_SSHORT	*/
64       return 2;
65     case 4:	/* TIFF_LONG	*/
66     case 9:	/* TIFF_SLONG	*/
67     case 11:	/* TIFF_FLOAT	*/
68     case 13:	/* TIFF_IFD	*/
69       return 4;
70     case 5:	/* TIFF_RATIONAL */
71     case 10:	/* TIFF_SRATIONAL */
72     case 12:	/* TIFF_DOUBLE	*/
73     case 16:	/* TIFF_LONG8	*/
74     case 17:	/* TIFF_SLONG8	*/
75     case 18:	/* TIFF_IFD8	*/
76       return 8;
77 #if 0
78     case 14:	/* TIFF_UNICODE	*/
79     case 15:	/* TIFF_COMPLEX */
80 #endif
81     default:
82       return 1;
83   }
84 }
85 
86 #ifdef DEBUG_TIFF
tag_name(unsigned int tag)87 const char *tag_name(unsigned int tag)
88 {
89   switch(tag)
90   {
91     case TIFFTAG_IMAGEDESCRIPTION:
92       return "IMAGEDESCRIPTION";
93     case TIFFTAG_MAKE:
94       return "MAKE";
95     case TIFFTAG_MODEL:
96       return "TIFFTAG_MODEL";
97     case TIFFTAG_SUBIFD:
98       return "SUBIFD";
99     case TIFFTAG_EXIFIFD:
100        return "EXIFIFD";
101     case TIFFTAG_STRIPOFFSETS:
102        return "STRIPOFFSETS";
103     case TIFFTAG_STRIPBYTECOUNTS:
104        return "STRIPBYTECOUNTS";
105     case TIFFTAG_KODAKIFD:
106        return "KODAKIFD";
107     case TIFFTAG_JPEGIFOFFSET:
108        return "JPEGIFOFFSET";
109     case TIFFTAG_JPEGIFBYTECOUNT:
110        return "JPEGIFBYTECOUNT";
111     case TIFFTAG_DNGPRIVATEDATA:
112        return "DNGPRIVATEDATA";
113     case EXIFTAG_MAKERNOTE:
114        return "EXIFTAG_MAKERNOTE";
115     case TIFFTAG_PRINTIM:
116        return "PrintIM";
117     case TIFFTAG_IMAGEOFFSET:
118        return "IMAGEOFFSET";
119     case TIFFTAG_IMAGEBYTECOUNT:
120        return "IMAGEBYTECOUNT";
121     case TIFFTAG_ALPHAOFFSET:
122        return "ALPHAOFFSET";
123     case TIFFTAG_ALPHABYTECOUNT:
124        return "ALPHABYTECOUNT";
125     case TIFFTAG_TILEOFFSETS:
126        return "TileOffsets";
127     case TIFFTAG_TILEBYTECOUNTS:
128        return "TileByteCounts";
129     default:
130       return "";
131   }
132 }
133 #endif
134 
find_tag_from_tiff_header(const TIFFHeader * tiff,const unsigned int tiff_size,const unsigned int tag,const char ** potential_error)135 const char *find_tag_from_tiff_header(const TIFFHeader *tiff, const unsigned int tiff_size, const unsigned int tag, const char **potential_error)
136 {
137   if(tiff->tiff_magic==TIFF_BIGENDIAN)
138     return find_tag_from_tiff_header_be(tiff, tiff_size, tag, potential_error);
139   else if(tiff->tiff_magic==TIFF_LITTLEENDIAN)
140     return find_tag_from_tiff_header_le(tiff, tiff_size, tag, potential_error);
141   return NULL;
142 }
143 
get_date_from_tiff_header(const TIFFHeader * tiff,const unsigned int tiff_size)144 time_t get_date_from_tiff_header(const TIFFHeader *tiff, const unsigned int tiff_size)
145 {
146   const char *potential_error=NULL;
147   const char *date_asc;
148   /* DateTimeOriginal */
149   date_asc=find_tag_from_tiff_header(tiff, tiff_size, 0x9003, &potential_error);
150   /* DateTimeDigitalized*/
151   if(date_asc==NULL || date_asc < (const char *)tiff || &date_asc[18] >= (const char *)tiff + tiff_size)
152     date_asc=find_tag_from_tiff_header(tiff, tiff_size, 0x9004, &potential_error);
153   if(date_asc==NULL || date_asc < (const char *)tiff || &date_asc[18] >= (const char *)tiff + tiff_size)
154     date_asc=find_tag_from_tiff_header(tiff, tiff_size, 0x132, &potential_error);
155   if(date_asc==NULL || date_asc < (const char *)tiff || &date_asc[18] >= (const char *)tiff + tiff_size)
156     return (time_t)0;
157   return get_time_from_YYYY_MM_DD_HH_MM_SS(date_asc);
158 }
159 
register_header_check_tiff(file_stat_t * file_stat)160 static void register_header_check_tiff(file_stat_t *file_stat)
161 {
162   static const unsigned char tiff_header_be[4]= { 'M','M',0x00, 0x2a};
163   static const unsigned char tiff_header_le[4]= { 'I','I',0x2a, 0x00};
164   register_header_check(0, tiff_header_be, sizeof(tiff_header_be), &header_check_tiff_be_new, file_stat);
165   register_header_check(0, tiff_header_le, sizeof(tiff_header_le), &header_check_tiff_le_new, file_stat);
166 }
167 
file_check_tiff(file_recovery_t * fr)168 void file_check_tiff(file_recovery_t *fr)
169 {
170   static uint64_t calculated_file_size=0;
171   TIFFHeader header;
172   calculated_file_size = 0;
173   if(fseek(fr->handle, 0, SEEK_SET) < 0 ||
174       fread(&header, sizeof(TIFFHeader), 1, fr->handle) != 1)
175   {
176     fr->file_size=0;
177     return;
178   }
179   if(header.tiff_magic==TIFF_LITTLEENDIAN)
180     calculated_file_size=header_check_tiff_le(fr, le32(header.tiff_diroff), 0, 0);
181   else if(header.tiff_magic==TIFF_BIGENDIAN)
182     calculated_file_size=header_check_tiff_be(fr, be32(header.tiff_diroff), 0, 0);
183 #ifdef DEBUG_TIFF
184   log_info("TIFF Current   %llu\n", (unsigned long long)fr->file_size);
185   log_info("TIFF Estimated %llu %llx\n", (unsigned long long)calculated_file_size, (unsigned long long)calculated_file_size);
186 #endif
187   if(fr->file_size < calculated_file_size || calculated_file_size==0)
188     fr->file_size=0;
189     /* PhotoRec isn't yet capable to find the correct filesize for
190      * Sony arw and dng,
191      * Panasonic raw/rw2,
192      * Minolta tif
193      * Sony sr2
194      * so don't truncate them */
195   else if(strcmp(fr->extension,"cr2")==0 ||
196       strcmp(fr->extension,"dcr")==0 ||
197       strcmp(fr->extension,"nef")==0 ||
198       strcmp(fr->extension,"orf")==0 ||
199       strcmp(fr->extension,"pef")==0 ||
200       (strcmp(fr->extension,"tif")==0 && calculated_file_size>1024*1024*1024) ||
201       strcmp(fr->extension,"wdp")==0)
202     fr->file_size=calculated_file_size;
203 }
204