1 : /* $Id: tif_dirread.c,v 1.172 2011-12-09 03:29:10 fwarmerdam Exp $ */
2 :
3 : /*
4 : * Copyright (c) 1988-1997 Sam Leffler
5 : * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 : *
7 : * Permission to use, copy, modify, distribute, and sell this software and
8 : * its documentation for any purpose is hereby granted without fee, provided
9 : * that (i) the above copyright notices and this permission notice appear in
10 : * all copies of the software and related documentation, and (ii) the names of
11 : * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 : * publicity relating to the software without the specific, prior written
13 : * permission of Sam Leffler and Silicon Graphics.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 : * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 : * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 : *
19 : * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 : * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 : * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 : * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 : * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 : * OF THIS SOFTWARE.
25 : */
26 :
27 : /*
28 : * TIFF Library.
29 : *
30 : * Directory Read Support Routines.
31 : */
32 :
33 : /* Suggested pending improvements:
34 : * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
35 : * eliminating current use of the IGNORE value, and therefore eliminating
36 : * current irrational behaviour on tags with tag id code 0
37 : * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
38 : * the pointer to the appropriate TIFFField structure early on in
39 : * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
40 : */
41 :
42 : #include "tiffiop.h"
43 :
44 : #define IGNORE 0 /* tag placeholder used below */
45 : #define FAILED_FII ((uint32) -1)
46 :
47 : #ifdef HAVE_IEEEFP
48 : # define TIFFCvtIEEEFloatToNative(tif, n, fp)
49 : # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
50 : #else
51 : extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
52 : extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
53 : #endif
54 :
55 : enum TIFFReadDirEntryErr {
56 : TIFFReadDirEntryErrOk = 0,
57 : TIFFReadDirEntryErrCount = 1,
58 : TIFFReadDirEntryErrType = 2,
59 : TIFFReadDirEntryErrIo = 3,
60 : TIFFReadDirEntryErrRange = 4,
61 : TIFFReadDirEntryErrPsdif = 5,
62 : TIFFReadDirEntryErrSizesan = 6,
63 : TIFFReadDirEntryErrAlloc = 7,
64 : };
65 :
66 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
67 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
68 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
69 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
70 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
71 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
72 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
73 :
74 : static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
75 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
76 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
77 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
78 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
79 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
80 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
81 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
82 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
83 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
84 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
85 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
86 :
87 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
88 : #if 0
89 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
90 : #endif
91 :
92 : static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
93 : static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
94 : static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
95 : static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
96 : static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
97 : static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
98 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
99 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
100 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
101 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
102 : static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
103 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
104 :
105 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
106 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
107 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
108 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
109 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
110 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
111 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
112 :
113 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
114 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
115 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
116 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
117 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
118 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
119 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
120 :
121 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
122 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
123 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
124 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
125 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
126 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
127 :
128 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
129 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
130 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
131 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
132 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
133 :
134 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
135 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
136 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
137 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
138 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
139 :
140 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
141 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
142 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
143 :
144 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
145 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
146 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
147 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
148 :
149 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
150 :
151 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
152 : static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
153 :
154 : static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
155 : static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
156 : static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
157 :
158 : static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
159 : static void MissingRequired(TIFF*, const char*);
160 : static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
161 : static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
162 : static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
163 : static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
164 : static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
165 : static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
166 : static void ChopUpSingleUncompressedStrip(TIFF*);
167 : static uint64 TIFFReadUInt64(const uint8 *value);
168 :
169 : typedef union _UInt64Aligned_t
170 : {
171 : double d;
172 : uint64 l;
173 : uint32 i[2];
174 : uint16 s[4];
175 : uint8 c[8];
176 : } UInt64Aligned_t;
177 :
178 : /*
179 : Unaligned safe copy of a uint64 value from an octet array.
180 : */
181 2376 : static uint64 TIFFReadUInt64(const uint8 *value)
182 : {
183 : UInt64Aligned_t result;
184 :
185 2376 : result.c[0]=value[0];
186 2376 : result.c[1]=value[1];
187 2376 : result.c[2]=value[2];
188 2376 : result.c[3]=value[3];
189 2376 : result.c[4]=value[4];
190 2376 : result.c[5]=value[5];
191 2376 : result.c[6]=value[6];
192 2376 : result.c[7]=value[7];
193 :
194 2376 : return result.l;
195 : }
196 :
197 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
198 : {
199 : enum TIFFReadDirEntryErr err;
200 0 : if (direntry->tdir_count!=1)
201 0 : return(TIFFReadDirEntryErrCount);
202 0 : switch (direntry->tdir_type)
203 : {
204 : case TIFF_BYTE:
205 0 : TIFFReadDirEntryCheckedByte(tif,direntry,value);
206 0 : return(TIFFReadDirEntryErrOk);
207 : case TIFF_SBYTE:
208 : {
209 : int8 m;
210 0 : TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
211 0 : err=TIFFReadDirEntryCheckRangeByteSbyte(m);
212 0 : if (err!=TIFFReadDirEntryErrOk)
213 0 : return(err);
214 0 : *value=(uint8)m;
215 0 : return(TIFFReadDirEntryErrOk);
216 : }
217 : case TIFF_SHORT:
218 : {
219 : uint16 m;
220 0 : TIFFReadDirEntryCheckedShort(tif,direntry,&m);
221 0 : err=TIFFReadDirEntryCheckRangeByteShort(m);
222 0 : if (err!=TIFFReadDirEntryErrOk)
223 0 : return(err);
224 0 : *value=(uint8)m;
225 0 : return(TIFFReadDirEntryErrOk);
226 : }
227 : case TIFF_SSHORT:
228 : {
229 : int16 m;
230 0 : TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
231 0 : err=TIFFReadDirEntryCheckRangeByteSshort(m);
232 0 : if (err!=TIFFReadDirEntryErrOk)
233 0 : return(err);
234 0 : *value=(uint8)m;
235 0 : return(TIFFReadDirEntryErrOk);
236 : }
237 : case TIFF_LONG:
238 : {
239 : uint32 m;
240 0 : TIFFReadDirEntryCheckedLong(tif,direntry,&m);
241 0 : err=TIFFReadDirEntryCheckRangeByteLong(m);
242 0 : if (err!=TIFFReadDirEntryErrOk)
243 0 : return(err);
244 0 : *value=(uint8)m;
245 0 : return(TIFFReadDirEntryErrOk);
246 : }
247 : case TIFF_SLONG:
248 : {
249 : int32 m;
250 0 : TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
251 0 : err=TIFFReadDirEntryCheckRangeByteSlong(m);
252 0 : if (err!=TIFFReadDirEntryErrOk)
253 0 : return(err);
254 0 : *value=(uint8)m;
255 0 : return(TIFFReadDirEntryErrOk);
256 : }
257 : case TIFF_LONG8:
258 : {
259 : uint64 m;
260 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
261 0 : if (err!=TIFFReadDirEntryErrOk)
262 0 : return(err);
263 0 : err=TIFFReadDirEntryCheckRangeByteLong8(m);
264 0 : if (err!=TIFFReadDirEntryErrOk)
265 0 : return(err);
266 0 : *value=(uint8)m;
267 0 : return(TIFFReadDirEntryErrOk);
268 : }
269 : case TIFF_SLONG8:
270 : {
271 : int64 m;
272 0 : err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
273 0 : if (err!=TIFFReadDirEntryErrOk)
274 0 : return(err);
275 0 : err=TIFFReadDirEntryCheckRangeByteSlong8(m);
276 0 : if (err!=TIFFReadDirEntryErrOk)
277 0 : return(err);
278 0 : *value=(uint8)m;
279 0 : return(TIFFReadDirEntryErrOk);
280 : }
281 : default:
282 0 : return(TIFFReadDirEntryErrType);
283 : }
284 : }
285 :
286 41731 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
287 : {
288 : enum TIFFReadDirEntryErr err;
289 41731 : if (direntry->tdir_count!=1)
290 2963 : return(TIFFReadDirEntryErrCount);
291 38768 : switch (direntry->tdir_type)
292 : {
293 : case TIFF_BYTE:
294 : {
295 : uint8 m;
296 0 : TIFFReadDirEntryCheckedByte(tif,direntry,&m);
297 0 : *value=(uint16)m;
298 0 : return(TIFFReadDirEntryErrOk);
299 : }
300 : case TIFF_SBYTE:
301 : {
302 : int8 m;
303 0 : TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
304 0 : err=TIFFReadDirEntryCheckRangeShortSbyte(m);
305 0 : if (err!=TIFFReadDirEntryErrOk)
306 0 : return(err);
307 0 : *value=(uint16)m;
308 0 : return(TIFFReadDirEntryErrOk);
309 : }
310 : case TIFF_SHORT:
311 38768 : TIFFReadDirEntryCheckedShort(tif,direntry,value);
312 38768 : return(TIFFReadDirEntryErrOk);
313 : case TIFF_SSHORT:
314 : {
315 : int16 m;
316 0 : TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
317 0 : err=TIFFReadDirEntryCheckRangeShortSshort(m);
318 0 : if (err!=TIFFReadDirEntryErrOk)
319 0 : return(err);
320 0 : *value=(uint16)m;
321 0 : return(TIFFReadDirEntryErrOk);
322 : }
323 : case TIFF_LONG:
324 : {
325 : uint32 m;
326 0 : TIFFReadDirEntryCheckedLong(tif,direntry,&m);
327 0 : err=TIFFReadDirEntryCheckRangeShortLong(m);
328 0 : if (err!=TIFFReadDirEntryErrOk)
329 0 : return(err);
330 0 : *value=(uint16)m;
331 0 : return(TIFFReadDirEntryErrOk);
332 : }
333 : case TIFF_SLONG:
334 : {
335 : int32 m;
336 0 : TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
337 0 : err=TIFFReadDirEntryCheckRangeShortSlong(m);
338 0 : if (err!=TIFFReadDirEntryErrOk)
339 0 : return(err);
340 0 : *value=(uint16)m;
341 0 : return(TIFFReadDirEntryErrOk);
342 : }
343 : case TIFF_LONG8:
344 : {
345 : uint64 m;
346 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
347 0 : if (err!=TIFFReadDirEntryErrOk)
348 0 : return(err);
349 0 : err=TIFFReadDirEntryCheckRangeShortLong8(m);
350 0 : if (err!=TIFFReadDirEntryErrOk)
351 0 : return(err);
352 0 : *value=(uint16)m;
353 0 : return(TIFFReadDirEntryErrOk);
354 : }
355 : case TIFF_SLONG8:
356 : {
357 : int64 m;
358 0 : err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
359 0 : if (err!=TIFFReadDirEntryErrOk)
360 0 : return(err);
361 0 : err=TIFFReadDirEntryCheckRangeShortSlong8(m);
362 0 : if (err!=TIFFReadDirEntryErrOk)
363 0 : return(err);
364 0 : *value=(uint16)m;
365 0 : return(TIFFReadDirEntryErrOk);
366 : }
367 : default:
368 0 : return(TIFFReadDirEntryErrType);
369 : }
370 : }
371 :
372 24669 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
373 : {
374 : enum TIFFReadDirEntryErr err;
375 24669 : if (direntry->tdir_count!=1)
376 0 : return(TIFFReadDirEntryErrCount);
377 24669 : switch (direntry->tdir_type)
378 : {
379 : case TIFF_BYTE:
380 : {
381 : uint8 m;
382 0 : TIFFReadDirEntryCheckedByte(tif,direntry,&m);
383 0 : *value=(uint32)m;
384 0 : return(TIFFReadDirEntryErrOk);
385 : }
386 : case TIFF_SBYTE:
387 : {
388 : int8 m;
389 0 : TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
390 0 : err=TIFFReadDirEntryCheckRangeLongSbyte(m);
391 0 : if (err!=TIFFReadDirEntryErrOk)
392 0 : return(err);
393 0 : *value=(uint32)m;
394 0 : return(TIFFReadDirEntryErrOk);
395 : }
396 : case TIFF_SHORT:
397 : {
398 : uint16 m;
399 22562 : TIFFReadDirEntryCheckedShort(tif,direntry,&m);
400 22562 : *value=(uint32)m;
401 22562 : return(TIFFReadDirEntryErrOk);
402 : }
403 : case TIFF_SSHORT:
404 : {
405 : int16 m;
406 0 : TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
407 0 : err=TIFFReadDirEntryCheckRangeLongSshort(m);
408 0 : if (err!=TIFFReadDirEntryErrOk)
409 0 : return(err);
410 0 : *value=(uint32)m;
411 0 : return(TIFFReadDirEntryErrOk);
412 : }
413 : case TIFF_LONG:
414 2107 : TIFFReadDirEntryCheckedLong(tif,direntry,value);
415 2107 : return(TIFFReadDirEntryErrOk);
416 : case TIFF_SLONG:
417 : {
418 : int32 m;
419 0 : TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
420 0 : err=TIFFReadDirEntryCheckRangeLongSlong(m);
421 0 : if (err!=TIFFReadDirEntryErrOk)
422 0 : return(err);
423 0 : *value=(uint32)m;
424 0 : return(TIFFReadDirEntryErrOk);
425 : }
426 : case TIFF_LONG8:
427 : {
428 : uint64 m;
429 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
430 0 : if (err!=TIFFReadDirEntryErrOk)
431 0 : return(err);
432 0 : err=TIFFReadDirEntryCheckRangeLongLong8(m);
433 0 : if (err!=TIFFReadDirEntryErrOk)
434 0 : return(err);
435 0 : *value=(uint32)m;
436 0 : return(TIFFReadDirEntryErrOk);
437 : }
438 : case TIFF_SLONG8:
439 : {
440 : int64 m;
441 0 : err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
442 0 : if (err!=TIFFReadDirEntryErrOk)
443 0 : return(err);
444 0 : err=TIFFReadDirEntryCheckRangeLongSlong8(m);
445 0 : if (err!=TIFFReadDirEntryErrOk)
446 0 : return(err);
447 0 : *value=(uint32)m;
448 0 : return(TIFFReadDirEntryErrOk);
449 : }
450 : default:
451 0 : return(TIFFReadDirEntryErrType);
452 : }
453 : }
454 :
455 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
456 : {
457 : enum TIFFReadDirEntryErr err;
458 0 : if (direntry->tdir_count!=1)
459 0 : return(TIFFReadDirEntryErrCount);
460 0 : switch (direntry->tdir_type)
461 : {
462 : case TIFF_BYTE:
463 : {
464 : uint8 m;
465 0 : TIFFReadDirEntryCheckedByte(tif,direntry,&m);
466 0 : *value=(uint64)m;
467 0 : return(TIFFReadDirEntryErrOk);
468 : }
469 : case TIFF_SBYTE:
470 : {
471 : int8 m;
472 0 : TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
473 0 : err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
474 0 : if (err!=TIFFReadDirEntryErrOk)
475 0 : return(err);
476 0 : *value=(uint64)m;
477 0 : return(TIFFReadDirEntryErrOk);
478 : }
479 : case TIFF_SHORT:
480 : {
481 : uint16 m;
482 0 : TIFFReadDirEntryCheckedShort(tif,direntry,&m);
483 0 : *value=(uint64)m;
484 0 : return(TIFFReadDirEntryErrOk);
485 : }
486 : case TIFF_SSHORT:
487 : {
488 : int16 m;
489 0 : TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
490 0 : err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
491 0 : if (err!=TIFFReadDirEntryErrOk)
492 0 : return(err);
493 0 : *value=(uint64)m;
494 0 : return(TIFFReadDirEntryErrOk);
495 : }
496 : case TIFF_LONG:
497 : {
498 : uint32 m;
499 0 : TIFFReadDirEntryCheckedLong(tif,direntry,&m);
500 0 : *value=(uint64)m;
501 0 : return(TIFFReadDirEntryErrOk);
502 : }
503 : case TIFF_SLONG:
504 : {
505 : int32 m;
506 0 : TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
507 0 : err=TIFFReadDirEntryCheckRangeLong8Slong(m);
508 0 : if (err!=TIFFReadDirEntryErrOk)
509 0 : return(err);
510 0 : *value=(uint64)m;
511 0 : return(TIFFReadDirEntryErrOk);
512 : }
513 : case TIFF_LONG8:
514 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
515 0 : return(err);
516 : case TIFF_SLONG8:
517 : {
518 : int64 m;
519 0 : err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
520 0 : if (err!=TIFFReadDirEntryErrOk)
521 0 : return(err);
522 0 : err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
523 0 : if (err!=TIFFReadDirEntryErrOk)
524 0 : return(err);
525 0 : *value=(uint64)m;
526 0 : return(TIFFReadDirEntryErrOk);
527 : }
528 : default:
529 0 : return(TIFFReadDirEntryErrType);
530 : }
531 : }
532 :
533 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
534 : {
535 : enum TIFFReadDirEntryErr err;
536 0 : if (direntry->tdir_count!=1)
537 0 : return(TIFFReadDirEntryErrCount);
538 0 : switch (direntry->tdir_type)
539 : {
540 : case TIFF_BYTE:
541 : {
542 : uint8 m;
543 0 : TIFFReadDirEntryCheckedByte(tif,direntry,&m);
544 0 : *value=(float)m;
545 0 : return(TIFFReadDirEntryErrOk);
546 : }
547 : case TIFF_SBYTE:
548 : {
549 : int8 m;
550 0 : TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
551 0 : *value=(float)m;
552 0 : return(TIFFReadDirEntryErrOk);
553 : }
554 : case TIFF_SHORT:
555 : {
556 : uint16 m;
557 0 : TIFFReadDirEntryCheckedShort(tif,direntry,&m);
558 0 : *value=(float)m;
559 0 : return(TIFFReadDirEntryErrOk);
560 : }
561 : case TIFF_SSHORT:
562 : {
563 : int16 m;
564 0 : TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
565 0 : *value=(float)m;
566 0 : return(TIFFReadDirEntryErrOk);
567 : }
568 : case TIFF_LONG:
569 : {
570 : uint32 m;
571 0 : TIFFReadDirEntryCheckedLong(tif,direntry,&m);
572 0 : *value=(float)m;
573 0 : return(TIFFReadDirEntryErrOk);
574 : }
575 : case TIFF_SLONG:
576 : {
577 : int32 m;
578 0 : TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
579 0 : *value=(float)m;
580 0 : return(TIFFReadDirEntryErrOk);
581 : }
582 : case TIFF_LONG8:
583 : {
584 : uint64 m;
585 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
586 0 : if (err!=TIFFReadDirEntryErrOk)
587 0 : return(err);
588 : #if defined(__WIN32__) && (_MSC_VER < 1500)
589 : /*
590 : * XXX: MSVC 6.0 does not support conversion
591 : * of 64-bit integers into floating point
592 : * values.
593 : */
594 : *value = _TIFFUInt64ToFloat(m);
595 : #else
596 0 : *value=(float)m;
597 : #endif
598 0 : return(TIFFReadDirEntryErrOk);
599 : }
600 : case TIFF_SLONG8:
601 : {
602 : int64 m;
603 0 : err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
604 0 : if (err!=TIFFReadDirEntryErrOk)
605 0 : return(err);
606 0 : *value=(float)m;
607 0 : return(TIFFReadDirEntryErrOk);
608 : }
609 : case TIFF_RATIONAL:
610 : {
611 : double m;
612 0 : err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
613 0 : if (err!=TIFFReadDirEntryErrOk)
614 0 : return(err);
615 0 : *value=(float)m;
616 0 : return(TIFFReadDirEntryErrOk);
617 : }
618 : case TIFF_SRATIONAL:
619 : {
620 : double m;
621 0 : err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
622 0 : if (err!=TIFFReadDirEntryErrOk)
623 0 : return(err);
624 0 : *value=(float)m;
625 0 : return(TIFFReadDirEntryErrOk);
626 : }
627 : case TIFF_FLOAT:
628 0 : TIFFReadDirEntryCheckedFloat(tif,direntry,value);
629 0 : return(TIFFReadDirEntryErrOk);
630 : case TIFF_DOUBLE:
631 : {
632 : double m;
633 0 : err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
634 0 : if (err!=TIFFReadDirEntryErrOk)
635 0 : return(err);
636 0 : *value=(float)m;
637 0 : return(TIFFReadDirEntryErrOk);
638 : }
639 : default:
640 0 : return(TIFFReadDirEntryErrType);
641 : }
642 : }
643 :
644 92 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
645 : {
646 : enum TIFFReadDirEntryErr err;
647 92 : if (direntry->tdir_count!=1)
648 0 : return(TIFFReadDirEntryErrCount);
649 92 : switch (direntry->tdir_type)
650 : {
651 : case TIFF_BYTE:
652 : {
653 : uint8 m;
654 0 : TIFFReadDirEntryCheckedByte(tif,direntry,&m);
655 0 : *value=(double)m;
656 0 : return(TIFFReadDirEntryErrOk);
657 : }
658 : case TIFF_SBYTE:
659 : {
660 : int8 m;
661 0 : TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
662 0 : *value=(double)m;
663 0 : return(TIFFReadDirEntryErrOk);
664 : }
665 : case TIFF_SHORT:
666 : {
667 : uint16 m;
668 0 : TIFFReadDirEntryCheckedShort(tif,direntry,&m);
669 0 : *value=(double)m;
670 0 : return(TIFFReadDirEntryErrOk);
671 : }
672 : case TIFF_SSHORT:
673 : {
674 : int16 m;
675 0 : TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
676 0 : *value=(double)m;
677 0 : return(TIFFReadDirEntryErrOk);
678 : }
679 : case TIFF_LONG:
680 : {
681 : uint32 m;
682 0 : TIFFReadDirEntryCheckedLong(tif,direntry,&m);
683 0 : *value=(double)m;
684 0 : return(TIFFReadDirEntryErrOk);
685 : }
686 : case TIFF_SLONG:
687 : {
688 : int32 m;
689 0 : TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
690 0 : *value=(double)m;
691 0 : return(TIFFReadDirEntryErrOk);
692 : }
693 : case TIFF_LONG8:
694 : {
695 : uint64 m;
696 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
697 0 : if (err!=TIFFReadDirEntryErrOk)
698 0 : return(err);
699 : #if defined(__WIN32__) && (_MSC_VER < 1500)
700 : /*
701 : * XXX: MSVC 6.0 does not support conversion
702 : * of 64-bit integers into floating point
703 : * values.
704 : */
705 : *value = _TIFFUInt64ToDouble(m);
706 : #else
707 0 : *value = (double)m;
708 : #endif
709 0 : return(TIFFReadDirEntryErrOk);
710 : }
711 : case TIFF_SLONG8:
712 : {
713 : int64 m;
714 0 : err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
715 0 : if (err!=TIFFReadDirEntryErrOk)
716 0 : return(err);
717 0 : *value=(double)m;
718 0 : return(TIFFReadDirEntryErrOk);
719 : }
720 : case TIFF_RATIONAL:
721 92 : err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
722 92 : return(err);
723 : case TIFF_SRATIONAL:
724 0 : err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
725 0 : return(err);
726 : case TIFF_FLOAT:
727 : {
728 : float m;
729 0 : TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
730 0 : *value=(double)m;
731 0 : return(TIFFReadDirEntryErrOk);
732 : }
733 : case TIFF_DOUBLE:
734 0 : err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
735 0 : return(err);
736 : default:
737 0 : return(TIFFReadDirEntryErrType);
738 : }
739 : }
740 :
741 1 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
742 : {
743 : enum TIFFReadDirEntryErr err;
744 1 : if (direntry->tdir_count!=1)
745 0 : return(TIFFReadDirEntryErrCount);
746 1 : switch (direntry->tdir_type)
747 : {
748 : case TIFF_LONG:
749 : case TIFF_IFD:
750 : {
751 : uint32 m;
752 1 : TIFFReadDirEntryCheckedLong(tif,direntry,&m);
753 1 : *value=(uint64)m;
754 1 : return(TIFFReadDirEntryErrOk);
755 : }
756 : case TIFF_LONG8:
757 : case TIFF_IFD8:
758 0 : err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
759 0 : return(err);
760 : default:
761 0 : return(TIFFReadDirEntryErrType);
762 : }
763 : }
764 :
765 34067 : static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
766 : {
767 : int typesize;
768 : uint32 datasize;
769 : void* data;
770 34067 : typesize=TIFFDataWidth(direntry->tdir_type);
771 34067 : if ((direntry->tdir_count==0)||(typesize==0))
772 : {
773 0 : *value=0;
774 0 : return(TIFFReadDirEntryErrOk);
775 : }
776 : (void) desttypesize;
777 :
778 : /*
779 : * As a sanity check, make sure we have no more than a 2GB tag array
780 : * in either the current data type or the dest data type. This also
781 : * avoids problems with overflow of tmsize_t on 32bit systems.
782 : */
783 34067 : if ((uint64)(2147483647/typesize)<direntry->tdir_count)
784 0 : return(TIFFReadDirEntryErrSizesan);
785 34067 : if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
786 0 : return(TIFFReadDirEntryErrSizesan);
787 :
788 34067 : *count=(uint32)direntry->tdir_count;
789 34067 : datasize=(*count)*typesize;
790 34067 : assert((tmsize_t)datasize>0);
791 34067 : data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
792 34067 : if (data==0)
793 0 : return(TIFFReadDirEntryErrAlloc);
794 34067 : if (!(tif->tif_flags&TIFF_BIGTIFF))
795 : {
796 33987 : if (datasize<=4)
797 11541 : _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
798 : else
799 : {
800 : enum TIFFReadDirEntryErr err;
801 22446 : uint32 offset = direntry->tdir_offset.toff_long;
802 22446 : if (tif->tif_flags&TIFF_SWAB)
803 559 : TIFFSwabLong(&offset);
804 22446 : err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
805 22446 : if (err!=TIFFReadDirEntryErrOk)
806 : {
807 1 : _TIFFfree(data);
808 1 : return(err);
809 : }
810 : }
811 : }
812 : else
813 : {
814 80 : if (datasize<=8)
815 78 : _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
816 : else
817 : {
818 : enum TIFFReadDirEntryErr err;
819 2 : uint64 offset = direntry->tdir_offset.toff_long8;
820 2 : if (tif->tif_flags&TIFF_SWAB)
821 0 : TIFFSwabLong8(&offset);
822 2 : err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
823 2 : if (err!=TIFFReadDirEntryErrOk)
824 : {
825 0 : _TIFFfree(data);
826 0 : return(err);
827 : }
828 : }
829 : }
830 34066 : *value=data;
831 34066 : return(TIFFReadDirEntryErrOk);
832 : }
833 :
834 4586 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
835 : {
836 : enum TIFFReadDirEntryErr err;
837 : uint32 count;
838 : void* origdata;
839 : uint8* data;
840 4586 : switch (direntry->tdir_type)
841 : {
842 : case TIFF_ASCII:
843 : case TIFF_UNDEFINED:
844 : case TIFF_BYTE:
845 : case TIFF_SBYTE:
846 : case TIFF_SHORT:
847 : case TIFF_SSHORT:
848 : case TIFF_LONG:
849 : case TIFF_SLONG:
850 : case TIFF_LONG8:
851 : case TIFF_SLONG8:
852 : break;
853 : default:
854 0 : return(TIFFReadDirEntryErrType);
855 : }
856 4586 : err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
857 9171 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
858 : {
859 1 : *value=0;
860 1 : return(err);
861 : }
862 4585 : switch (direntry->tdir_type)
863 : {
864 : case TIFF_ASCII:
865 : case TIFF_UNDEFINED:
866 : case TIFF_BYTE:
867 4585 : *value=(uint8*)origdata;
868 4585 : return(TIFFReadDirEntryErrOk);
869 : case TIFF_SBYTE:
870 : {
871 : int8* m;
872 : uint32 n;
873 0 : m=(int8*)origdata;
874 0 : for (n=0; n<count; n++)
875 : {
876 0 : err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
877 0 : if (err!=TIFFReadDirEntryErrOk)
878 : {
879 0 : _TIFFfree(origdata);
880 0 : return(err);
881 : }
882 0 : m++;
883 : }
884 0 : *value=(uint8*)origdata;
885 0 : return(TIFFReadDirEntryErrOk);
886 : }
887 : }
888 0 : data=(uint8*)_TIFFmalloc(count);
889 0 : if (data==0)
890 : {
891 0 : _TIFFfree(origdata);
892 0 : return(TIFFReadDirEntryErrAlloc);
893 : }
894 0 : switch (direntry->tdir_type)
895 : {
896 : case TIFF_SHORT:
897 : {
898 : uint16* ma;
899 : uint8* mb;
900 : uint32 n;
901 0 : ma=(uint16*)origdata;
902 0 : mb=data;
903 0 : for (n=0; n<count; n++)
904 : {
905 0 : if (tif->tif_flags&TIFF_SWAB)
906 0 : TIFFSwabShort(ma);
907 0 : err=TIFFReadDirEntryCheckRangeByteShort(*ma);
908 0 : if (err!=TIFFReadDirEntryErrOk)
909 0 : break;
910 0 : *mb++=(uint8)(*ma++);
911 : }
912 : }
913 0 : break;
914 : case TIFF_SSHORT:
915 : {
916 : int16* ma;
917 : uint8* mb;
918 : uint32 n;
919 0 : ma=(int16*)origdata;
920 0 : mb=data;
921 0 : for (n=0; n<count; n++)
922 : {
923 0 : if (tif->tif_flags&TIFF_SWAB)
924 0 : TIFFSwabShort((uint16*)ma);
925 0 : err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
926 0 : if (err!=TIFFReadDirEntryErrOk)
927 0 : break;
928 0 : *mb++=(uint8)(*ma++);
929 : }
930 : }
931 0 : break;
932 : case TIFF_LONG:
933 : {
934 : uint32* ma;
935 : uint8* mb;
936 : uint32 n;
937 0 : ma=(uint32*)origdata;
938 0 : mb=data;
939 0 : for (n=0; n<count; n++)
940 : {
941 0 : if (tif->tif_flags&TIFF_SWAB)
942 0 : TIFFSwabLong(ma);
943 0 : err=TIFFReadDirEntryCheckRangeByteLong(*ma);
944 0 : if (err!=TIFFReadDirEntryErrOk)
945 0 : break;
946 0 : *mb++=(uint8)(*ma++);
947 : }
948 : }
949 0 : break;
950 : case TIFF_SLONG:
951 : {
952 : int32* ma;
953 : uint8* mb;
954 : uint32 n;
955 0 : ma=(int32*)origdata;
956 0 : mb=data;
957 0 : for (n=0; n<count; n++)
958 : {
959 0 : if (tif->tif_flags&TIFF_SWAB)
960 0 : TIFFSwabLong((uint32*)ma);
961 0 : err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
962 0 : if (err!=TIFFReadDirEntryErrOk)
963 0 : break;
964 0 : *mb++=(uint8)(*ma++);
965 : }
966 : }
967 0 : break;
968 : case TIFF_LONG8:
969 : {
970 : uint64* ma;
971 : uint8* mb;
972 : uint32 n;
973 0 : ma=(uint64*)origdata;
974 0 : mb=data;
975 0 : for (n=0; n<count; n++)
976 : {
977 0 : if (tif->tif_flags&TIFF_SWAB)
978 0 : TIFFSwabLong8(ma);
979 0 : err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
980 0 : if (err!=TIFFReadDirEntryErrOk)
981 0 : break;
982 0 : *mb++=(uint8)(*ma++);
983 : }
984 : }
985 0 : break;
986 : case TIFF_SLONG8:
987 : {
988 : int64* ma;
989 : uint8* mb;
990 : uint32 n;
991 0 : ma=(int64*)origdata;
992 0 : mb=data;
993 0 : for (n=0; n<count; n++)
994 : {
995 0 : if (tif->tif_flags&TIFF_SWAB)
996 0 : TIFFSwabLong8((uint64*)ma);
997 0 : err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
998 0 : if (err!=TIFFReadDirEntryErrOk)
999 0 : break;
1000 0 : *mb++=(uint8)(*ma++);
1001 : }
1002 : }
1003 : break;
1004 : }
1005 0 : _TIFFfree(origdata);
1006 0 : if (err!=TIFFReadDirEntryErrOk)
1007 : {
1008 0 : _TIFFfree(data);
1009 0 : return(err);
1010 : }
1011 0 : *value=data;
1012 0 : return(TIFFReadDirEntryErrOk);
1013 : }
1014 :
1015 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
1016 : {
1017 : enum TIFFReadDirEntryErr err;
1018 : uint32 count;
1019 : void* origdata;
1020 : int8* data;
1021 0 : switch (direntry->tdir_type)
1022 : {
1023 : case TIFF_UNDEFINED:
1024 : case TIFF_BYTE:
1025 : case TIFF_SBYTE:
1026 : case TIFF_SHORT:
1027 : case TIFF_SSHORT:
1028 : case TIFF_LONG:
1029 : case TIFF_SLONG:
1030 : case TIFF_LONG8:
1031 : case TIFF_SLONG8:
1032 : break;
1033 : default:
1034 0 : return(TIFFReadDirEntryErrType);
1035 : }
1036 0 : err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1037 0 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1038 : {
1039 0 : *value=0;
1040 0 : return(err);
1041 : }
1042 0 : switch (direntry->tdir_type)
1043 : {
1044 : case TIFF_UNDEFINED:
1045 : case TIFF_BYTE:
1046 : {
1047 : uint8* m;
1048 : uint32 n;
1049 0 : m=(uint8*)origdata;
1050 0 : for (n=0; n<count; n++)
1051 : {
1052 0 : err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1053 0 : if (err!=TIFFReadDirEntryErrOk)
1054 : {
1055 0 : _TIFFfree(origdata);
1056 0 : return(err);
1057 : }
1058 0 : m++;
1059 : }
1060 0 : *value=(int8*)origdata;
1061 0 : return(TIFFReadDirEntryErrOk);
1062 : }
1063 : case TIFF_SBYTE:
1064 0 : *value=(int8*)origdata;
1065 0 : return(TIFFReadDirEntryErrOk);
1066 : }
1067 0 : data=(int8*)_TIFFmalloc(count);
1068 0 : if (data==0)
1069 : {
1070 0 : _TIFFfree(origdata);
1071 0 : return(TIFFReadDirEntryErrAlloc);
1072 : }
1073 0 : switch (direntry->tdir_type)
1074 : {
1075 : case TIFF_SHORT:
1076 : {
1077 : uint16* ma;
1078 : int8* mb;
1079 : uint32 n;
1080 0 : ma=(uint16*)origdata;
1081 0 : mb=data;
1082 0 : for (n=0; n<count; n++)
1083 : {
1084 0 : if (tif->tif_flags&TIFF_SWAB)
1085 0 : TIFFSwabShort(ma);
1086 0 : err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1087 0 : if (err!=TIFFReadDirEntryErrOk)
1088 0 : break;
1089 0 : *mb++=(int8)(*ma++);
1090 : }
1091 : }
1092 0 : break;
1093 : case TIFF_SSHORT:
1094 : {
1095 : int16* ma;
1096 : int8* mb;
1097 : uint32 n;
1098 0 : ma=(int16*)origdata;
1099 0 : mb=data;
1100 0 : for (n=0; n<count; n++)
1101 : {
1102 0 : if (tif->tif_flags&TIFF_SWAB)
1103 0 : TIFFSwabShort((uint16*)ma);
1104 0 : err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1105 0 : if (err!=TIFFReadDirEntryErrOk)
1106 0 : break;
1107 0 : *mb++=(int8)(*ma++);
1108 : }
1109 : }
1110 0 : break;
1111 : case TIFF_LONG:
1112 : {
1113 : uint32* ma;
1114 : int8* mb;
1115 : uint32 n;
1116 0 : ma=(uint32*)origdata;
1117 0 : mb=data;
1118 0 : for (n=0; n<count; n++)
1119 : {
1120 0 : if (tif->tif_flags&TIFF_SWAB)
1121 0 : TIFFSwabLong(ma);
1122 0 : err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1123 0 : if (err!=TIFFReadDirEntryErrOk)
1124 0 : break;
1125 0 : *mb++=(int8)(*ma++);
1126 : }
1127 : }
1128 0 : break;
1129 : case TIFF_SLONG:
1130 : {
1131 : int32* ma;
1132 : int8* mb;
1133 : uint32 n;
1134 0 : ma=(int32*)origdata;
1135 0 : mb=data;
1136 0 : for (n=0; n<count; n++)
1137 : {
1138 0 : if (tif->tif_flags&TIFF_SWAB)
1139 0 : TIFFSwabLong((uint32*)ma);
1140 0 : err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1141 0 : if (err!=TIFFReadDirEntryErrOk)
1142 0 : break;
1143 0 : *mb++=(int8)(*ma++);
1144 : }
1145 : }
1146 0 : break;
1147 : case TIFF_LONG8:
1148 : {
1149 : uint64* ma;
1150 : int8* mb;
1151 : uint32 n;
1152 0 : ma=(uint64*)origdata;
1153 0 : mb=data;
1154 0 : for (n=0; n<count; n++)
1155 : {
1156 0 : if (tif->tif_flags&TIFF_SWAB)
1157 0 : TIFFSwabLong8(ma);
1158 0 : err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1159 0 : if (err!=TIFFReadDirEntryErrOk)
1160 0 : break;
1161 0 : *mb++=(int8)(*ma++);
1162 : }
1163 : }
1164 0 : break;
1165 : case TIFF_SLONG8:
1166 : {
1167 : int64* ma;
1168 : int8* mb;
1169 : uint32 n;
1170 0 : ma=(int64*)origdata;
1171 0 : mb=data;
1172 0 : for (n=0; n<count; n++)
1173 : {
1174 0 : if (tif->tif_flags&TIFF_SWAB)
1175 0 : TIFFSwabLong8((uint64*)ma);
1176 0 : err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1177 0 : if (err!=TIFFReadDirEntryErrOk)
1178 0 : break;
1179 0 : *mb++=(int8)(*ma++);
1180 : }
1181 : }
1182 : break;
1183 : }
1184 0 : _TIFFfree(origdata);
1185 0 : if (err!=TIFFReadDirEntryErrOk)
1186 : {
1187 0 : _TIFFfree(data);
1188 0 : return(err);
1189 : }
1190 0 : *value=data;
1191 0 : return(TIFFReadDirEntryErrOk);
1192 : }
1193 :
1194 7306 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
1195 : {
1196 : enum TIFFReadDirEntryErr err;
1197 : uint32 count;
1198 : void* origdata;
1199 : uint16* data;
1200 7306 : switch (direntry->tdir_type)
1201 : {
1202 : case TIFF_BYTE:
1203 : case TIFF_SBYTE:
1204 : case TIFF_SHORT:
1205 : case TIFF_SSHORT:
1206 : case TIFF_LONG:
1207 : case TIFF_SLONG:
1208 : case TIFF_LONG8:
1209 : case TIFF_SLONG8:
1210 : break;
1211 : default:
1212 0 : return(TIFFReadDirEntryErrType);
1213 : }
1214 7306 : err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1215 14612 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1216 : {
1217 0 : *value=0;
1218 0 : return(err);
1219 : }
1220 7306 : switch (direntry->tdir_type)
1221 : {
1222 : case TIFF_SHORT:
1223 7306 : *value=(uint16*)origdata;
1224 7306 : if (tif->tif_flags&TIFF_SWAB)
1225 349 : TIFFSwabArrayOfShort(*value,count);
1226 7306 : return(TIFFReadDirEntryErrOk);
1227 : case TIFF_SSHORT:
1228 : {
1229 : int16* m;
1230 : uint32 n;
1231 0 : m=(int16*)origdata;
1232 0 : for (n=0; n<count; n++)
1233 : {
1234 0 : if (tif->tif_flags&TIFF_SWAB)
1235 0 : TIFFSwabShort((uint16*)m);
1236 0 : err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1237 0 : if (err!=TIFFReadDirEntryErrOk)
1238 : {
1239 0 : _TIFFfree(origdata);
1240 0 : return(err);
1241 : }
1242 0 : m++;
1243 : }
1244 0 : *value=(uint16*)origdata;
1245 0 : return(TIFFReadDirEntryErrOk);
1246 : }
1247 : }
1248 0 : data=(uint16*)_TIFFmalloc(count*2);
1249 0 : if (data==0)
1250 : {
1251 0 : _TIFFfree(origdata);
1252 0 : return(TIFFReadDirEntryErrAlloc);
1253 : }
1254 0 : switch (direntry->tdir_type)
1255 : {
1256 : case TIFF_BYTE:
1257 : {
1258 : uint8* ma;
1259 : uint16* mb;
1260 : uint32 n;
1261 0 : ma=(uint8*)origdata;
1262 0 : mb=data;
1263 0 : for (n=0; n<count; n++)
1264 0 : *mb++=(uint16)(*ma++);
1265 : }
1266 0 : break;
1267 : case TIFF_SBYTE:
1268 : {
1269 : int8* ma;
1270 : uint16* mb;
1271 : uint32 n;
1272 0 : ma=(int8*)origdata;
1273 0 : mb=data;
1274 0 : for (n=0; n<count; n++)
1275 : {
1276 0 : err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1277 0 : if (err!=TIFFReadDirEntryErrOk)
1278 0 : break;
1279 0 : *mb++=(uint16)(*ma++);
1280 : }
1281 : }
1282 0 : break;
1283 : case TIFF_LONG:
1284 : {
1285 : uint32* ma;
1286 : uint16* mb;
1287 : uint32 n;
1288 0 : ma=(uint32*)origdata;
1289 0 : mb=data;
1290 0 : for (n=0; n<count; n++)
1291 : {
1292 0 : if (tif->tif_flags&TIFF_SWAB)
1293 0 : TIFFSwabLong(ma);
1294 0 : err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1295 0 : if (err!=TIFFReadDirEntryErrOk)
1296 0 : break;
1297 0 : *mb++=(uint16)(*ma++);
1298 : }
1299 : }
1300 0 : break;
1301 : case TIFF_SLONG:
1302 : {
1303 : int32* ma;
1304 : uint16* mb;
1305 : uint32 n;
1306 0 : ma=(int32*)origdata;
1307 0 : mb=data;
1308 0 : for (n=0; n<count; n++)
1309 : {
1310 0 : if (tif->tif_flags&TIFF_SWAB)
1311 0 : TIFFSwabLong((uint32*)ma);
1312 0 : err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1313 0 : if (err!=TIFFReadDirEntryErrOk)
1314 0 : break;
1315 0 : *mb++=(uint16)(*ma++);
1316 : }
1317 : }
1318 0 : break;
1319 : case TIFF_LONG8:
1320 : {
1321 : uint64* ma;
1322 : uint16* mb;
1323 : uint32 n;
1324 0 : ma=(uint64*)origdata;
1325 0 : mb=data;
1326 0 : for (n=0; n<count; n++)
1327 : {
1328 0 : if (tif->tif_flags&TIFF_SWAB)
1329 0 : TIFFSwabLong8(ma);
1330 0 : err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1331 0 : if (err!=TIFFReadDirEntryErrOk)
1332 0 : break;
1333 0 : *mb++=(uint16)(*ma++);
1334 : }
1335 : }
1336 0 : break;
1337 : case TIFF_SLONG8:
1338 : {
1339 : int64* ma;
1340 : uint16* mb;
1341 : uint32 n;
1342 0 : ma=(int64*)origdata;
1343 0 : mb=data;
1344 0 : for (n=0; n<count; n++)
1345 : {
1346 0 : if (tif->tif_flags&TIFF_SWAB)
1347 0 : TIFFSwabLong8((uint64*)ma);
1348 0 : err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1349 0 : if (err!=TIFFReadDirEntryErrOk)
1350 0 : break;
1351 0 : *mb++=(uint16)(*ma++);
1352 : }
1353 : }
1354 : break;
1355 : }
1356 0 : _TIFFfree(origdata);
1357 0 : if (err!=TIFFReadDirEntryErrOk)
1358 : {
1359 0 : _TIFFfree(data);
1360 0 : return(err);
1361 : }
1362 0 : *value=data;
1363 0 : return(TIFFReadDirEntryErrOk);
1364 : }
1365 :
1366 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
1367 : {
1368 : enum TIFFReadDirEntryErr err;
1369 : uint32 count;
1370 : void* origdata;
1371 : int16* data;
1372 0 : switch (direntry->tdir_type)
1373 : {
1374 : case TIFF_BYTE:
1375 : case TIFF_SBYTE:
1376 : case TIFF_SHORT:
1377 : case TIFF_SSHORT:
1378 : case TIFF_LONG:
1379 : case TIFF_SLONG:
1380 : case TIFF_LONG8:
1381 : case TIFF_SLONG8:
1382 : break;
1383 : default:
1384 0 : return(TIFFReadDirEntryErrType);
1385 : }
1386 0 : err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1387 0 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1388 : {
1389 0 : *value=0;
1390 0 : return(err);
1391 : }
1392 0 : switch (direntry->tdir_type)
1393 : {
1394 : case TIFF_SHORT:
1395 : {
1396 : uint16* m;
1397 : uint32 n;
1398 0 : m=(uint16*)origdata;
1399 0 : for (n=0; n<count; n++)
1400 : {
1401 0 : if (tif->tif_flags&TIFF_SWAB)
1402 0 : TIFFSwabShort(m);
1403 0 : err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1404 0 : if (err!=TIFFReadDirEntryErrOk)
1405 : {
1406 0 : _TIFFfree(origdata);
1407 0 : return(err);
1408 : }
1409 0 : m++;
1410 : }
1411 0 : *value=(int16*)origdata;
1412 0 : return(TIFFReadDirEntryErrOk);
1413 : }
1414 : case TIFF_SSHORT:
1415 0 : *value=(int16*)origdata;
1416 0 : if (tif->tif_flags&TIFF_SWAB)
1417 0 : TIFFSwabArrayOfShort((uint16*)(*value),count);
1418 0 : return(TIFFReadDirEntryErrOk);
1419 : }
1420 0 : data=(int16*)_TIFFmalloc(count*2);
1421 0 : if (data==0)
1422 : {
1423 0 : _TIFFfree(origdata);
1424 0 : return(TIFFReadDirEntryErrAlloc);
1425 : }
1426 0 : switch (direntry->tdir_type)
1427 : {
1428 : case TIFF_BYTE:
1429 : {
1430 : uint8* ma;
1431 : int16* mb;
1432 : uint32 n;
1433 0 : ma=(uint8*)origdata;
1434 0 : mb=data;
1435 0 : for (n=0; n<count; n++)
1436 0 : *mb++=(int16)(*ma++);
1437 : }
1438 0 : break;
1439 : case TIFF_SBYTE:
1440 : {
1441 : int8* ma;
1442 : int16* mb;
1443 : uint32 n;
1444 0 : ma=(int8*)origdata;
1445 0 : mb=data;
1446 0 : for (n=0; n<count; n++)
1447 0 : *mb++=(int16)(*ma++);
1448 : }
1449 0 : break;
1450 : case TIFF_LONG:
1451 : {
1452 : uint32* ma;
1453 : int16* mb;
1454 : uint32 n;
1455 0 : ma=(uint32*)origdata;
1456 0 : mb=data;
1457 0 : for (n=0; n<count; n++)
1458 : {
1459 0 : if (tif->tif_flags&TIFF_SWAB)
1460 0 : TIFFSwabLong(ma);
1461 0 : err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1462 0 : if (err!=TIFFReadDirEntryErrOk)
1463 0 : break;
1464 0 : *mb++=(int16)(*ma++);
1465 : }
1466 : }
1467 0 : break;
1468 : case TIFF_SLONG:
1469 : {
1470 : int32* ma;
1471 : int16* mb;
1472 : uint32 n;
1473 0 : ma=(int32*)origdata;
1474 0 : mb=data;
1475 0 : for (n=0; n<count; n++)
1476 : {
1477 0 : if (tif->tif_flags&TIFF_SWAB)
1478 0 : TIFFSwabLong((uint32*)ma);
1479 0 : err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1480 0 : if (err!=TIFFReadDirEntryErrOk)
1481 0 : break;
1482 0 : *mb++=(int16)(*ma++);
1483 : }
1484 : }
1485 0 : break;
1486 : case TIFF_LONG8:
1487 : {
1488 : uint64* ma;
1489 : int16* mb;
1490 : uint32 n;
1491 0 : ma=(uint64*)origdata;
1492 0 : mb=data;
1493 0 : for (n=0; n<count; n++)
1494 : {
1495 0 : if (tif->tif_flags&TIFF_SWAB)
1496 0 : TIFFSwabLong8(ma);
1497 0 : err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1498 0 : if (err!=TIFFReadDirEntryErrOk)
1499 0 : break;
1500 0 : *mb++=(int16)(*ma++);
1501 : }
1502 : }
1503 0 : break;
1504 : case TIFF_SLONG8:
1505 : {
1506 : int64* ma;
1507 : int16* mb;
1508 : uint32 n;
1509 0 : ma=(int64*)origdata;
1510 0 : mb=data;
1511 0 : for (n=0; n<count; n++)
1512 : {
1513 0 : if (tif->tif_flags&TIFF_SWAB)
1514 0 : TIFFSwabLong8((uint64*)ma);
1515 0 : err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1516 0 : if (err!=TIFFReadDirEntryErrOk)
1517 0 : break;
1518 0 : *mb++=(int16)(*ma++);
1519 : }
1520 : }
1521 : break;
1522 : }
1523 0 : _TIFFfree(origdata);
1524 0 : if (err!=TIFFReadDirEntryErrOk)
1525 : {
1526 0 : _TIFFfree(data);
1527 0 : return(err);
1528 : }
1529 0 : *value=data;
1530 0 : return(TIFFReadDirEntryErrOk);
1531 : }
1532 :
1533 1 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
1534 : {
1535 : enum TIFFReadDirEntryErr err;
1536 : uint32 count;
1537 : void* origdata;
1538 : uint32* data;
1539 1 : switch (direntry->tdir_type)
1540 : {
1541 : case TIFF_BYTE:
1542 : case TIFF_SBYTE:
1543 : case TIFF_SHORT:
1544 : case TIFF_SSHORT:
1545 : case TIFF_LONG:
1546 : case TIFF_SLONG:
1547 : case TIFF_LONG8:
1548 : case TIFF_SLONG8:
1549 : break;
1550 : default:
1551 0 : return(TIFFReadDirEntryErrType);
1552 : }
1553 1 : err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1554 2 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1555 : {
1556 0 : *value=0;
1557 0 : return(err);
1558 : }
1559 1 : switch (direntry->tdir_type)
1560 : {
1561 : case TIFF_LONG:
1562 1 : *value=(uint32*)origdata;
1563 1 : if (tif->tif_flags&TIFF_SWAB)
1564 0 : TIFFSwabArrayOfLong(*value,count);
1565 1 : return(TIFFReadDirEntryErrOk);
1566 : case TIFF_SLONG:
1567 : {
1568 : int32* m;
1569 : uint32 n;
1570 0 : m=(int32*)origdata;
1571 0 : for (n=0; n<count; n++)
1572 : {
1573 0 : if (tif->tif_flags&TIFF_SWAB)
1574 0 : TIFFSwabLong((uint32*)m);
1575 0 : err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1576 0 : if (err!=TIFFReadDirEntryErrOk)
1577 : {
1578 0 : _TIFFfree(origdata);
1579 0 : return(err);
1580 : }
1581 0 : m++;
1582 : }
1583 0 : *value=(uint32*)origdata;
1584 0 : return(TIFFReadDirEntryErrOk);
1585 : }
1586 : }
1587 0 : data=(uint32*)_TIFFmalloc(count*4);
1588 0 : if (data==0)
1589 : {
1590 0 : _TIFFfree(origdata);
1591 0 : return(TIFFReadDirEntryErrAlloc);
1592 : }
1593 0 : switch (direntry->tdir_type)
1594 : {
1595 : case TIFF_BYTE:
1596 : {
1597 : uint8* ma;
1598 : uint32* mb;
1599 : uint32 n;
1600 0 : ma=(uint8*)origdata;
1601 0 : mb=data;
1602 0 : for (n=0; n<count; n++)
1603 0 : *mb++=(uint32)(*ma++);
1604 : }
1605 0 : break;
1606 : case TIFF_SBYTE:
1607 : {
1608 : int8* ma;
1609 : uint32* mb;
1610 : uint32 n;
1611 0 : ma=(int8*)origdata;
1612 0 : mb=data;
1613 0 : for (n=0; n<count; n++)
1614 : {
1615 0 : err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1616 0 : if (err!=TIFFReadDirEntryErrOk)
1617 0 : break;
1618 0 : *mb++=(uint32)(*ma++);
1619 : }
1620 : }
1621 0 : break;
1622 : case TIFF_SHORT:
1623 : {
1624 : uint16* ma;
1625 : uint32* mb;
1626 : uint32 n;
1627 0 : ma=(uint16*)origdata;
1628 0 : mb=data;
1629 0 : for (n=0; n<count; n++)
1630 : {
1631 0 : if (tif->tif_flags&TIFF_SWAB)
1632 0 : TIFFSwabShort(ma);
1633 0 : *mb++=(uint32)(*ma++);
1634 : }
1635 : }
1636 0 : break;
1637 : case TIFF_SSHORT:
1638 : {
1639 : int16* ma;
1640 : uint32* mb;
1641 : uint32 n;
1642 0 : ma=(int16*)origdata;
1643 0 : mb=data;
1644 0 : for (n=0; n<count; n++)
1645 : {
1646 0 : if (tif->tif_flags&TIFF_SWAB)
1647 0 : TIFFSwabShort((uint16*)ma);
1648 0 : err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1649 0 : if (err!=TIFFReadDirEntryErrOk)
1650 0 : break;
1651 0 : *mb++=(uint32)(*ma++);
1652 : }
1653 : }
1654 0 : break;
1655 : case TIFF_LONG8:
1656 : {
1657 : uint64* ma;
1658 : uint32* mb;
1659 : uint32 n;
1660 0 : ma=(uint64*)origdata;
1661 0 : mb=data;
1662 0 : for (n=0; n<count; n++)
1663 : {
1664 0 : if (tif->tif_flags&TIFF_SWAB)
1665 0 : TIFFSwabLong8(ma);
1666 0 : err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1667 0 : if (err!=TIFFReadDirEntryErrOk)
1668 0 : break;
1669 0 : *mb++=(uint32)(*ma++);
1670 : }
1671 : }
1672 0 : break;
1673 : case TIFF_SLONG8:
1674 : {
1675 : int64* ma;
1676 : uint32* mb;
1677 : uint32 n;
1678 0 : ma=(int64*)origdata;
1679 0 : mb=data;
1680 0 : for (n=0; n<count; n++)
1681 : {
1682 0 : if (tif->tif_flags&TIFF_SWAB)
1683 0 : TIFFSwabLong8((uint64*)ma);
1684 0 : err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1685 0 : if (err!=TIFFReadDirEntryErrOk)
1686 0 : break;
1687 0 : *mb++=(uint32)(*ma++);
1688 : }
1689 : }
1690 : break;
1691 : }
1692 0 : _TIFFfree(origdata);
1693 0 : if (err!=TIFFReadDirEntryErrOk)
1694 : {
1695 0 : _TIFFfree(data);
1696 0 : return(err);
1697 : }
1698 0 : *value=data;
1699 0 : return(TIFFReadDirEntryErrOk);
1700 : }
1701 :
1702 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
1703 : {
1704 : enum TIFFReadDirEntryErr err;
1705 : uint32 count;
1706 : void* origdata;
1707 : int32* data;
1708 0 : switch (direntry->tdir_type)
1709 : {
1710 : case TIFF_BYTE:
1711 : case TIFF_SBYTE:
1712 : case TIFF_SHORT:
1713 : case TIFF_SSHORT:
1714 : case TIFF_LONG:
1715 : case TIFF_SLONG:
1716 : case TIFF_LONG8:
1717 : case TIFF_SLONG8:
1718 : break;
1719 : default:
1720 0 : return(TIFFReadDirEntryErrType);
1721 : }
1722 0 : err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1723 0 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1724 : {
1725 0 : *value=0;
1726 0 : return(err);
1727 : }
1728 0 : switch (direntry->tdir_type)
1729 : {
1730 : case TIFF_LONG:
1731 : {
1732 : uint32* m;
1733 : uint32 n;
1734 0 : m=(uint32*)origdata;
1735 0 : for (n=0; n<count; n++)
1736 : {
1737 0 : if (tif->tif_flags&TIFF_SWAB)
1738 0 : TIFFSwabLong((uint32*)m);
1739 0 : err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1740 0 : if (err!=TIFFReadDirEntryErrOk)
1741 : {
1742 0 : _TIFFfree(origdata);
1743 0 : return(err);
1744 : }
1745 0 : m++;
1746 : }
1747 0 : *value=(int32*)origdata;
1748 0 : return(TIFFReadDirEntryErrOk);
1749 : }
1750 : case TIFF_SLONG:
1751 0 : *value=(int32*)origdata;
1752 0 : if (tif->tif_flags&TIFF_SWAB)
1753 0 : TIFFSwabArrayOfLong((uint32*)(*value),count);
1754 0 : return(TIFFReadDirEntryErrOk);
1755 : }
1756 0 : data=(int32*)_TIFFmalloc(count*4);
1757 0 : if (data==0)
1758 : {
1759 0 : _TIFFfree(origdata);
1760 0 : return(TIFFReadDirEntryErrAlloc);
1761 : }
1762 0 : switch (direntry->tdir_type)
1763 : {
1764 : case TIFF_BYTE:
1765 : {
1766 : uint8* ma;
1767 : int32* mb;
1768 : uint32 n;
1769 0 : ma=(uint8*)origdata;
1770 0 : mb=data;
1771 0 : for (n=0; n<count; n++)
1772 0 : *mb++=(int32)(*ma++);
1773 : }
1774 0 : break;
1775 : case TIFF_SBYTE:
1776 : {
1777 : int8* ma;
1778 : int32* mb;
1779 : uint32 n;
1780 0 : ma=(int8*)origdata;
1781 0 : mb=data;
1782 0 : for (n=0; n<count; n++)
1783 0 : *mb++=(int32)(*ma++);
1784 : }
1785 0 : break;
1786 : case TIFF_SHORT:
1787 : {
1788 : uint16* ma;
1789 : int32* mb;
1790 : uint32 n;
1791 0 : ma=(uint16*)origdata;
1792 0 : mb=data;
1793 0 : for (n=0; n<count; n++)
1794 : {
1795 0 : if (tif->tif_flags&TIFF_SWAB)
1796 0 : TIFFSwabShort(ma);
1797 0 : *mb++=(int32)(*ma++);
1798 : }
1799 : }
1800 0 : break;
1801 : case TIFF_SSHORT:
1802 : {
1803 : int16* ma;
1804 : int32* mb;
1805 : uint32 n;
1806 0 : ma=(int16*)origdata;
1807 0 : mb=data;
1808 0 : for (n=0; n<count; n++)
1809 : {
1810 0 : if (tif->tif_flags&TIFF_SWAB)
1811 0 : TIFFSwabShort((uint16*)ma);
1812 0 : *mb++=(int32)(*ma++);
1813 : }
1814 : }
1815 0 : break;
1816 : case TIFF_LONG8:
1817 : {
1818 : uint64* ma;
1819 : int32* mb;
1820 : uint32 n;
1821 0 : ma=(uint64*)origdata;
1822 0 : mb=data;
1823 0 : for (n=0; n<count; n++)
1824 : {
1825 0 : if (tif->tif_flags&TIFF_SWAB)
1826 0 : TIFFSwabLong8(ma);
1827 0 : err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1828 0 : if (err!=TIFFReadDirEntryErrOk)
1829 0 : break;
1830 0 : *mb++=(int32)(*ma++);
1831 : }
1832 : }
1833 0 : break;
1834 : case TIFF_SLONG8:
1835 : {
1836 : int64* ma;
1837 : int32* mb;
1838 : uint32 n;
1839 0 : ma=(int64*)origdata;
1840 0 : mb=data;
1841 0 : for (n=0; n<count; n++)
1842 : {
1843 0 : if (tif->tif_flags&TIFF_SWAB)
1844 0 : TIFFSwabLong8((uint64*)ma);
1845 0 : err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1846 0 : if (err!=TIFFReadDirEntryErrOk)
1847 0 : break;
1848 0 : *mb++=(int32)(*ma++);
1849 : }
1850 : }
1851 : break;
1852 : }
1853 0 : _TIFFfree(origdata);
1854 0 : if (err!=TIFFReadDirEntryErrOk)
1855 : {
1856 0 : _TIFFfree(data);
1857 0 : return(err);
1858 : }
1859 0 : *value=data;
1860 0 : return(TIFFReadDirEntryErrOk);
1861 : }
1862 :
1863 12579 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
1864 : {
1865 : enum TIFFReadDirEntryErr err;
1866 : uint32 count;
1867 : void* origdata;
1868 : uint64* data;
1869 12579 : switch (direntry->tdir_type)
1870 : {
1871 : case TIFF_BYTE:
1872 : case TIFF_SBYTE:
1873 : case TIFF_SHORT:
1874 : case TIFF_SSHORT:
1875 : case TIFF_LONG:
1876 : case TIFF_SLONG:
1877 : case TIFF_LONG8:
1878 : case TIFF_SLONG8:
1879 : break;
1880 : default:
1881 0 : return(TIFFReadDirEntryErrType);
1882 : }
1883 12579 : err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
1884 25158 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1885 : {
1886 0 : *value=0;
1887 0 : return(err);
1888 : }
1889 12579 : switch (direntry->tdir_type)
1890 : {
1891 : case TIFF_LONG8:
1892 80 : *value=(uint64*)origdata;
1893 80 : if (tif->tif_flags&TIFF_SWAB)
1894 0 : TIFFSwabArrayOfLong8(*value,count);
1895 80 : return(TIFFReadDirEntryErrOk);
1896 : case TIFF_SLONG8:
1897 : {
1898 : int64* m;
1899 : uint32 n;
1900 0 : m=(int64*)origdata;
1901 0 : for (n=0; n<count; n++)
1902 : {
1903 0 : if (tif->tif_flags&TIFF_SWAB)
1904 0 : TIFFSwabLong8((uint64*)m);
1905 0 : err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
1906 0 : if (err!=TIFFReadDirEntryErrOk)
1907 : {
1908 0 : _TIFFfree(origdata);
1909 0 : return(err);
1910 : }
1911 0 : m++;
1912 : }
1913 0 : *value=(uint64*)origdata;
1914 0 : return(TIFFReadDirEntryErrOk);
1915 : }
1916 : }
1917 12499 : data=(uint64*)_TIFFmalloc(count*8);
1918 12499 : if (data==0)
1919 : {
1920 0 : _TIFFfree(origdata);
1921 0 : return(TIFFReadDirEntryErrAlloc);
1922 : }
1923 12499 : switch (direntry->tdir_type)
1924 : {
1925 : case TIFF_BYTE:
1926 : {
1927 : uint8* ma;
1928 : uint64* mb;
1929 : uint32 n;
1930 0 : ma=(uint8*)origdata;
1931 0 : mb=data;
1932 0 : for (n=0; n<count; n++)
1933 0 : *mb++=(uint64)(*ma++);
1934 : }
1935 0 : break;
1936 : case TIFF_SBYTE:
1937 : {
1938 : int8* ma;
1939 : uint64* mb;
1940 : uint32 n;
1941 0 : ma=(int8*)origdata;
1942 0 : mb=data;
1943 0 : for (n=0; n<count; n++)
1944 : {
1945 0 : err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
1946 0 : if (err!=TIFFReadDirEntryErrOk)
1947 0 : break;
1948 0 : *mb++=(uint64)(*ma++);
1949 : }
1950 : }
1951 0 : break;
1952 : case TIFF_SHORT:
1953 : {
1954 : uint16* ma;
1955 : uint64* mb;
1956 : uint32 n;
1957 0 : ma=(uint16*)origdata;
1958 0 : mb=data;
1959 0 : for (n=0; n<count; n++)
1960 : {
1961 0 : if (tif->tif_flags&TIFF_SWAB)
1962 0 : TIFFSwabShort(ma);
1963 0 : *mb++=(uint64)(*ma++);
1964 : }
1965 : }
1966 0 : break;
1967 : case TIFF_SSHORT:
1968 : {
1969 : int16* ma;
1970 : uint64* mb;
1971 : uint32 n;
1972 0 : ma=(int16*)origdata;
1973 0 : mb=data;
1974 0 : for (n=0; n<count; n++)
1975 : {
1976 0 : if (tif->tif_flags&TIFF_SWAB)
1977 0 : TIFFSwabShort((uint16*)ma);
1978 0 : err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
1979 0 : if (err!=TIFFReadDirEntryErrOk)
1980 0 : break;
1981 0 : *mb++=(uint64)(*ma++);
1982 : }
1983 : }
1984 0 : break;
1985 : case TIFF_LONG:
1986 : {
1987 : uint32* ma;
1988 : uint64* mb;
1989 : uint32 n;
1990 12499 : ma=(uint32*)origdata;
1991 12499 : mb=data;
1992 507526 : for (n=0; n<count; n++)
1993 : {
1994 495027 : if (tif->tif_flags&TIFF_SWAB)
1995 627 : TIFFSwabLong(ma);
1996 495027 : *mb++=(uint64)(*ma++);
1997 : }
1998 : }
1999 12499 : break;
2000 : case TIFF_SLONG:
2001 : {
2002 : int32* ma;
2003 : uint64* mb;
2004 : uint32 n;
2005 0 : ma=(int32*)origdata;
2006 0 : mb=data;
2007 0 : for (n=0; n<count; n++)
2008 : {
2009 0 : if (tif->tif_flags&TIFF_SWAB)
2010 0 : TIFFSwabLong((uint32*)ma);
2011 0 : err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2012 0 : if (err!=TIFFReadDirEntryErrOk)
2013 0 : break;
2014 0 : *mb++=(uint64)(*ma++);
2015 : }
2016 : }
2017 : break;
2018 : }
2019 12499 : _TIFFfree(origdata);
2020 12499 : if (err!=TIFFReadDirEntryErrOk)
2021 : {
2022 0 : _TIFFfree(data);
2023 0 : return(err);
2024 : }
2025 12499 : *value=data;
2026 12499 : return(TIFFReadDirEntryErrOk);
2027 : }
2028 :
2029 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
2030 : {
2031 : enum TIFFReadDirEntryErr err;
2032 : uint32 count;
2033 : void* origdata;
2034 : int64* data;
2035 0 : switch (direntry->tdir_type)
2036 : {
2037 : case TIFF_BYTE:
2038 : case TIFF_SBYTE:
2039 : case TIFF_SHORT:
2040 : case TIFF_SSHORT:
2041 : case TIFF_LONG:
2042 : case TIFF_SLONG:
2043 : case TIFF_LONG8:
2044 : case TIFF_SLONG8:
2045 : break;
2046 : default:
2047 0 : return(TIFFReadDirEntryErrType);
2048 : }
2049 0 : err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2050 0 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2051 : {
2052 0 : *value=0;
2053 0 : return(err);
2054 : }
2055 0 : switch (direntry->tdir_type)
2056 : {
2057 : case TIFF_LONG8:
2058 : {
2059 : uint64* m;
2060 : uint32 n;
2061 0 : m=(uint64*)origdata;
2062 0 : for (n=0; n<count; n++)
2063 : {
2064 0 : if (tif->tif_flags&TIFF_SWAB)
2065 0 : TIFFSwabLong8(m);
2066 0 : err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2067 0 : if (err!=TIFFReadDirEntryErrOk)
2068 : {
2069 0 : _TIFFfree(origdata);
2070 0 : return(err);
2071 : }
2072 0 : m++;
2073 : }
2074 0 : *value=(int64*)origdata;
2075 0 : return(TIFFReadDirEntryErrOk);
2076 : }
2077 : case TIFF_SLONG8:
2078 0 : *value=(int64*)origdata;
2079 0 : if (tif->tif_flags&TIFF_SWAB)
2080 0 : TIFFSwabArrayOfLong8((uint64*)(*value),count);
2081 0 : return(TIFFReadDirEntryErrOk);
2082 : }
2083 0 : data=(int64*)_TIFFmalloc(count*8);
2084 0 : if (data==0)
2085 : {
2086 0 : _TIFFfree(origdata);
2087 0 : return(TIFFReadDirEntryErrAlloc);
2088 : }
2089 0 : switch (direntry->tdir_type)
2090 : {
2091 : case TIFF_BYTE:
2092 : {
2093 : uint8* ma;
2094 : int64* mb;
2095 : uint32 n;
2096 0 : ma=(uint8*)origdata;
2097 0 : mb=data;
2098 0 : for (n=0; n<count; n++)
2099 0 : *mb++=(int64)(*ma++);
2100 : }
2101 0 : break;
2102 : case TIFF_SBYTE:
2103 : {
2104 : int8* ma;
2105 : int64* mb;
2106 : uint32 n;
2107 0 : ma=(int8*)origdata;
2108 0 : mb=data;
2109 0 : for (n=0; n<count; n++)
2110 0 : *mb++=(int64)(*ma++);
2111 : }
2112 0 : break;
2113 : case TIFF_SHORT:
2114 : {
2115 : uint16* ma;
2116 : int64* mb;
2117 : uint32 n;
2118 0 : ma=(uint16*)origdata;
2119 0 : mb=data;
2120 0 : for (n=0; n<count; n++)
2121 : {
2122 0 : if (tif->tif_flags&TIFF_SWAB)
2123 0 : TIFFSwabShort(ma);
2124 0 : *mb++=(int64)(*ma++);
2125 : }
2126 : }
2127 0 : break;
2128 : case TIFF_SSHORT:
2129 : {
2130 : int16* ma;
2131 : int64* mb;
2132 : uint32 n;
2133 0 : ma=(int16*)origdata;
2134 0 : mb=data;
2135 0 : for (n=0; n<count; n++)
2136 : {
2137 0 : if (tif->tif_flags&TIFF_SWAB)
2138 0 : TIFFSwabShort((uint16*)ma);
2139 0 : *mb++=(int64)(*ma++);
2140 : }
2141 : }
2142 0 : break;
2143 : case TIFF_LONG:
2144 : {
2145 : uint32* ma;
2146 : int64* mb;
2147 : uint32 n;
2148 0 : ma=(uint32*)origdata;
2149 0 : mb=data;
2150 0 : for (n=0; n<count; n++)
2151 : {
2152 0 : if (tif->tif_flags&TIFF_SWAB)
2153 0 : TIFFSwabLong(ma);
2154 0 : *mb++=(int64)(*ma++);
2155 : }
2156 : }
2157 0 : break;
2158 : case TIFF_SLONG:
2159 : {
2160 : int32* ma;
2161 : int64* mb;
2162 : uint32 n;
2163 0 : ma=(int32*)origdata;
2164 0 : mb=data;
2165 0 : for (n=0; n<count; n++)
2166 : {
2167 0 : if (tif->tif_flags&TIFF_SWAB)
2168 0 : TIFFSwabLong((uint32*)ma);
2169 0 : *mb++=(int64)(*ma++);
2170 : }
2171 : }
2172 : break;
2173 : }
2174 0 : _TIFFfree(origdata);
2175 0 : if (err!=TIFFReadDirEntryErrOk)
2176 : {
2177 0 : _TIFFfree(data);
2178 0 : return(err);
2179 : }
2180 0 : *value=data;
2181 0 : return(TIFFReadDirEntryErrOk);
2182 : }
2183 :
2184 168 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2185 : {
2186 : enum TIFFReadDirEntryErr err;
2187 : uint32 count;
2188 : void* origdata;
2189 : float* data;
2190 168 : switch (direntry->tdir_type)
2191 : {
2192 : case TIFF_BYTE:
2193 : case TIFF_SBYTE:
2194 : case TIFF_SHORT:
2195 : case TIFF_SSHORT:
2196 : case TIFF_LONG:
2197 : case TIFF_SLONG:
2198 : case TIFF_LONG8:
2199 : case TIFF_SLONG8:
2200 : case TIFF_RATIONAL:
2201 : case TIFF_SRATIONAL:
2202 : case TIFF_FLOAT:
2203 : case TIFF_DOUBLE:
2204 : break;
2205 : default:
2206 0 : return(TIFFReadDirEntryErrType);
2207 : }
2208 168 : err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2209 336 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2210 : {
2211 0 : *value=0;
2212 0 : return(err);
2213 : }
2214 168 : switch (direntry->tdir_type)
2215 : {
2216 : case TIFF_FLOAT:
2217 0 : if (tif->tif_flags&TIFF_SWAB)
2218 0 : TIFFSwabArrayOfLong((uint32*)origdata,count);
2219 : TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2220 0 : *value=(float*)origdata;
2221 0 : return(TIFFReadDirEntryErrOk);
2222 : }
2223 168 : data=(float*)_TIFFmalloc(count*sizeof(float));
2224 168 : if (data==0)
2225 : {
2226 0 : _TIFFfree(origdata);
2227 0 : return(TIFFReadDirEntryErrAlloc);
2228 : }
2229 168 : switch (direntry->tdir_type)
2230 : {
2231 : case TIFF_BYTE:
2232 : {
2233 : uint8* ma;
2234 : float* mb;
2235 : uint32 n;
2236 0 : ma=(uint8*)origdata;
2237 0 : mb=data;
2238 0 : for (n=0; n<count; n++)
2239 0 : *mb++=(float)(*ma++);
2240 : }
2241 0 : break;
2242 : case TIFF_SBYTE:
2243 : {
2244 : int8* ma;
2245 : float* mb;
2246 : uint32 n;
2247 0 : ma=(int8*)origdata;
2248 0 : mb=data;
2249 0 : for (n=0; n<count; n++)
2250 0 : *mb++=(float)(*ma++);
2251 : }
2252 0 : break;
2253 : case TIFF_SHORT:
2254 : {
2255 : uint16* ma;
2256 : float* mb;
2257 : uint32 n;
2258 0 : ma=(uint16*)origdata;
2259 0 : mb=data;
2260 0 : for (n=0; n<count; n++)
2261 : {
2262 0 : if (tif->tif_flags&TIFF_SWAB)
2263 0 : TIFFSwabShort(ma);
2264 0 : *mb++=(float)(*ma++);
2265 : }
2266 : }
2267 0 : break;
2268 : case TIFF_SSHORT:
2269 : {
2270 : int16* ma;
2271 : float* mb;
2272 : uint32 n;
2273 0 : ma=(int16*)origdata;
2274 0 : mb=data;
2275 0 : for (n=0; n<count; n++)
2276 : {
2277 0 : if (tif->tif_flags&TIFF_SWAB)
2278 0 : TIFFSwabShort((uint16*)ma);
2279 0 : *mb++=(float)(*ma++);
2280 : }
2281 : }
2282 0 : break;
2283 : case TIFF_LONG:
2284 : {
2285 : uint32* ma;
2286 : float* mb;
2287 : uint32 n;
2288 1 : ma=(uint32*)origdata;
2289 1 : mb=data;
2290 7 : for (n=0; n<count; n++)
2291 : {
2292 6 : if (tif->tif_flags&TIFF_SWAB)
2293 6 : TIFFSwabLong(ma);
2294 6 : *mb++=(float)(*ma++);
2295 : }
2296 : }
2297 1 : break;
2298 : case TIFF_SLONG:
2299 : {
2300 : int32* ma;
2301 : float* mb;
2302 : uint32 n;
2303 0 : ma=(int32*)origdata;
2304 0 : mb=data;
2305 0 : for (n=0; n<count; n++)
2306 : {
2307 0 : if (tif->tif_flags&TIFF_SWAB)
2308 0 : TIFFSwabLong((uint32*)ma);
2309 0 : *mb++=(float)(*ma++);
2310 : }
2311 : }
2312 0 : break;
2313 : case TIFF_LONG8:
2314 : {
2315 : uint64* ma;
2316 : float* mb;
2317 : uint32 n;
2318 0 : ma=(uint64*)origdata;
2319 0 : mb=data;
2320 0 : for (n=0; n<count; n++)
2321 : {
2322 0 : if (tif->tif_flags&TIFF_SWAB)
2323 0 : TIFFSwabLong8(ma);
2324 : #if defined(__WIN32__) && (_MSC_VER < 1500)
2325 : /*
2326 : * XXX: MSVC 6.0 does not support
2327 : * conversion of 64-bit integers into
2328 : * floating point values.
2329 : */
2330 : *mb++ = _TIFFUInt64ToFloat(*ma++);
2331 : #else
2332 0 : *mb++ = (float)(*ma++);
2333 : #endif
2334 : }
2335 : }
2336 0 : break;
2337 : case TIFF_SLONG8:
2338 : {
2339 : int64* ma;
2340 : float* mb;
2341 : uint32 n;
2342 0 : ma=(int64*)origdata;
2343 0 : mb=data;
2344 0 : for (n=0; n<count; n++)
2345 : {
2346 0 : if (tif->tif_flags&TIFF_SWAB)
2347 0 : TIFFSwabLong8((uint64*)ma);
2348 0 : *mb++=(float)(*ma++);
2349 : }
2350 : }
2351 0 : break;
2352 : case TIFF_RATIONAL:
2353 : {
2354 : uint32* ma;
2355 : uint32 maa;
2356 : uint32 mab;
2357 : float* mb;
2358 : uint32 n;
2359 167 : ma=(uint32*)origdata;
2360 167 : mb=data;
2361 1166 : for (n=0; n<count; n++)
2362 : {
2363 999 : if (tif->tif_flags&TIFF_SWAB)
2364 33 : TIFFSwabLong(ma);
2365 999 : maa=*ma++;
2366 999 : if (tif->tif_flags&TIFF_SWAB)
2367 33 : TIFFSwabLong(ma);
2368 999 : mab=*ma++;
2369 999 : if (mab==0)
2370 0 : *mb++=0.0;
2371 : else
2372 999 : *mb++=(float)maa/(float)mab;
2373 : }
2374 : }
2375 167 : break;
2376 : case TIFF_SRATIONAL:
2377 : {
2378 : uint32* ma;
2379 : int32 maa;
2380 : uint32 mab;
2381 : float* mb;
2382 : uint32 n;
2383 0 : ma=(uint32*)origdata;
2384 0 : mb=data;
2385 0 : for (n=0; n<count; n++)
2386 : {
2387 0 : if (tif->tif_flags&TIFF_SWAB)
2388 0 : TIFFSwabLong(ma);
2389 0 : maa=*(int32*)ma;
2390 0 : ma++;
2391 0 : if (tif->tif_flags&TIFF_SWAB)
2392 0 : TIFFSwabLong(ma);
2393 0 : mab=*ma++;
2394 0 : if (mab==0)
2395 0 : *mb++=0.0;
2396 : else
2397 0 : *mb++=(float)maa/(float)mab;
2398 : }
2399 : }
2400 0 : break;
2401 : case TIFF_DOUBLE:
2402 : {
2403 : double* ma;
2404 : float* mb;
2405 : uint32 n;
2406 0 : if (tif->tif_flags&TIFF_SWAB)
2407 0 : TIFFSwabArrayOfLong8((uint64*)origdata,count);
2408 : TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2409 0 : ma=(double*)origdata;
2410 0 : mb=data;
2411 0 : for (n=0; n<count; n++)
2412 0 : *mb++=(float)(*ma++);
2413 : }
2414 : break;
2415 : }
2416 168 : _TIFFfree(origdata);
2417 168 : if (err!=TIFFReadDirEntryErrOk)
2418 : {
2419 0 : _TIFFfree(data);
2420 0 : return(err);
2421 : }
2422 168 : *value=data;
2423 168 : return(TIFFReadDirEntryErrOk);
2424 : }
2425 :
2426 : static enum TIFFReadDirEntryErr
2427 9427 : TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2428 : {
2429 : enum TIFFReadDirEntryErr err;
2430 : uint32 count;
2431 : void* origdata;
2432 : double* data;
2433 9427 : switch (direntry->tdir_type)
2434 : {
2435 : case TIFF_BYTE:
2436 : case TIFF_SBYTE:
2437 : case TIFF_SHORT:
2438 : case TIFF_SSHORT:
2439 : case TIFF_LONG:
2440 : case TIFF_SLONG:
2441 : case TIFF_LONG8:
2442 : case TIFF_SLONG8:
2443 : case TIFF_RATIONAL:
2444 : case TIFF_SRATIONAL:
2445 : case TIFF_FLOAT:
2446 : case TIFF_DOUBLE:
2447 : break;
2448 : default:
2449 0 : return(TIFFReadDirEntryErrType);
2450 : }
2451 9427 : err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2452 18854 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2453 : {
2454 0 : *value=0;
2455 0 : return(err);
2456 : }
2457 9427 : switch (direntry->tdir_type)
2458 : {
2459 : case TIFF_DOUBLE:
2460 9427 : if (tif->tif_flags&TIFF_SWAB)
2461 107 : TIFFSwabArrayOfLong8((uint64*)origdata,count);
2462 : TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2463 9427 : *value=(double*)origdata;
2464 9427 : return(TIFFReadDirEntryErrOk);
2465 : }
2466 0 : data=(double*)_TIFFmalloc(count*sizeof(double));
2467 0 : if (data==0)
2468 : {
2469 0 : _TIFFfree(origdata);
2470 0 : return(TIFFReadDirEntryErrAlloc);
2471 : }
2472 0 : switch (direntry->tdir_type)
2473 : {
2474 : case TIFF_BYTE:
2475 : {
2476 : uint8* ma;
2477 : double* mb;
2478 : uint32 n;
2479 0 : ma=(uint8*)origdata;
2480 0 : mb=data;
2481 0 : for (n=0; n<count; n++)
2482 0 : *mb++=(double)(*ma++);
2483 : }
2484 0 : break;
2485 : case TIFF_SBYTE:
2486 : {
2487 : int8* ma;
2488 : double* mb;
2489 : uint32 n;
2490 0 : ma=(int8*)origdata;
2491 0 : mb=data;
2492 0 : for (n=0; n<count; n++)
2493 0 : *mb++=(double)(*ma++);
2494 : }
2495 0 : break;
2496 : case TIFF_SHORT:
2497 : {
2498 : uint16* ma;
2499 : double* mb;
2500 : uint32 n;
2501 0 : ma=(uint16*)origdata;
2502 0 : mb=data;
2503 0 : for (n=0; n<count; n++)
2504 : {
2505 0 : if (tif->tif_flags&TIFF_SWAB)
2506 0 : TIFFSwabShort(ma);
2507 0 : *mb++=(double)(*ma++);
2508 : }
2509 : }
2510 0 : break;
2511 : case TIFF_SSHORT:
2512 : {
2513 : int16* ma;
2514 : double* mb;
2515 : uint32 n;
2516 0 : ma=(int16*)origdata;
2517 0 : mb=data;
2518 0 : for (n=0; n<count; n++)
2519 : {
2520 0 : if (tif->tif_flags&TIFF_SWAB)
2521 0 : TIFFSwabShort((uint16*)ma);
2522 0 : *mb++=(double)(*ma++);
2523 : }
2524 : }
2525 0 : break;
2526 : case TIFF_LONG:
2527 : {
2528 : uint32* ma;
2529 : double* mb;
2530 : uint32 n;
2531 0 : ma=(uint32*)origdata;
2532 0 : mb=data;
2533 0 : for (n=0; n<count; n++)
2534 : {
2535 0 : if (tif->tif_flags&TIFF_SWAB)
2536 0 : TIFFSwabLong(ma);
2537 0 : *mb++=(double)(*ma++);
2538 : }
2539 : }
2540 0 : break;
2541 : case TIFF_SLONG:
2542 : {
2543 : int32* ma;
2544 : double* mb;
2545 : uint32 n;
2546 0 : ma=(int32*)origdata;
2547 0 : mb=data;
2548 0 : for (n=0; n<count; n++)
2549 : {
2550 0 : if (tif->tif_flags&TIFF_SWAB)
2551 0 : TIFFSwabLong((uint32*)ma);
2552 0 : *mb++=(double)(*ma++);
2553 : }
2554 : }
2555 0 : break;
2556 : case TIFF_LONG8:
2557 : {
2558 : uint64* ma;
2559 : double* mb;
2560 : uint32 n;
2561 0 : ma=(uint64*)origdata;
2562 0 : mb=data;
2563 0 : for (n=0; n<count; n++)
2564 : {
2565 0 : if (tif->tif_flags&TIFF_SWAB)
2566 0 : TIFFSwabLong8(ma);
2567 : #if defined(__WIN32__) && (_MSC_VER < 1500)
2568 : /*
2569 : * XXX: MSVC 6.0 does not support
2570 : * conversion of 64-bit integers into
2571 : * floating point values.
2572 : */
2573 : *mb++ = _TIFFUInt64ToDouble(*ma++);
2574 : #else
2575 0 : *mb++ = (double)(*ma++);
2576 : #endif
2577 : }
2578 : }
2579 0 : break;
2580 : case TIFF_SLONG8:
2581 : {
2582 : int64* ma;
2583 : double* mb;
2584 : uint32 n;
2585 0 : ma=(int64*)origdata;
2586 0 : mb=data;
2587 0 : for (n=0; n<count; n++)
2588 : {
2589 0 : if (tif->tif_flags&TIFF_SWAB)
2590 0 : TIFFSwabLong8((uint64*)ma);
2591 0 : *mb++=(double)(*ma++);
2592 : }
2593 : }
2594 0 : break;
2595 : case TIFF_RATIONAL:
2596 : {
2597 : uint32* ma;
2598 : uint32 maa;
2599 : uint32 mab;
2600 : double* mb;
2601 : uint32 n;
2602 0 : ma=(uint32*)origdata;
2603 0 : mb=data;
2604 0 : for (n=0; n<count; n++)
2605 : {
2606 0 : if (tif->tif_flags&TIFF_SWAB)
2607 0 : TIFFSwabLong(ma);
2608 0 : maa=*ma++;
2609 0 : if (tif->tif_flags&TIFF_SWAB)
2610 0 : TIFFSwabLong(ma);
2611 0 : mab=*ma++;
2612 0 : if (mab==0)
2613 0 : *mb++=0.0;
2614 : else
2615 0 : *mb++=(double)maa/(double)mab;
2616 : }
2617 : }
2618 0 : break;
2619 : case TIFF_SRATIONAL:
2620 : {
2621 : uint32* ma;
2622 : int32 maa;
2623 : uint32 mab;
2624 : double* mb;
2625 : uint32 n;
2626 0 : ma=(uint32*)origdata;
2627 0 : mb=data;
2628 0 : for (n=0; n<count; n++)
2629 : {
2630 0 : if (tif->tif_flags&TIFF_SWAB)
2631 0 : TIFFSwabLong(ma);
2632 0 : maa=*(int32*)ma;
2633 0 : ma++;
2634 0 : if (tif->tif_flags&TIFF_SWAB)
2635 0 : TIFFSwabLong(ma);
2636 0 : mab=*ma++;
2637 0 : if (mab==0)
2638 0 : *mb++=0.0;
2639 : else
2640 0 : *mb++=(double)maa/(double)mab;
2641 : }
2642 : }
2643 0 : break;
2644 : case TIFF_FLOAT:
2645 : {
2646 : float* ma;
2647 : double* mb;
2648 : uint32 n;
2649 0 : if (tif->tif_flags&TIFF_SWAB)
2650 0 : TIFFSwabArrayOfLong((uint32*)origdata,count);
2651 : TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
2652 0 : ma=(float*)origdata;
2653 0 : mb=data;
2654 0 : for (n=0; n<count; n++)
2655 0 : *mb++=(double)(*ma++);
2656 : }
2657 : break;
2658 : }
2659 0 : _TIFFfree(origdata);
2660 0 : if (err!=TIFFReadDirEntryErrOk)
2661 : {
2662 0 : _TIFFfree(data);
2663 0 : return(err);
2664 : }
2665 0 : *value=data;
2666 0 : return(TIFFReadDirEntryErrOk);
2667 : }
2668 :
2669 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
2670 : {
2671 : enum TIFFReadDirEntryErr err;
2672 : uint32 count;
2673 : void* origdata;
2674 : uint64* data;
2675 0 : switch (direntry->tdir_type)
2676 : {
2677 : case TIFF_LONG:
2678 : case TIFF_LONG8:
2679 : case TIFF_IFD:
2680 : case TIFF_IFD8:
2681 : break;
2682 : default:
2683 0 : return(TIFFReadDirEntryErrType);
2684 : }
2685 0 : err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2686 0 : if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2687 : {
2688 0 : *value=0;
2689 0 : return(err);
2690 : }
2691 0 : switch (direntry->tdir_type)
2692 : {
2693 : case TIFF_LONG8:
2694 : case TIFF_IFD8:
2695 0 : *value=(uint64*)origdata;
2696 0 : if (tif->tif_flags&TIFF_SWAB)
2697 0 : TIFFSwabArrayOfLong8(*value,count);
2698 0 : return(TIFFReadDirEntryErrOk);
2699 : }
2700 0 : data=(uint64*)_TIFFmalloc(count*8);
2701 0 : if (data==0)
2702 : {
2703 0 : _TIFFfree(origdata);
2704 0 : return(TIFFReadDirEntryErrAlloc);
2705 : }
2706 0 : switch (direntry->tdir_type)
2707 : {
2708 : case TIFF_LONG:
2709 : case TIFF_IFD:
2710 : {
2711 : uint32* ma;
2712 : uint64* mb;
2713 : uint32 n;
2714 0 : ma=(uint32*)origdata;
2715 0 : mb=data;
2716 0 : for (n=0; n<count; n++)
2717 : {
2718 0 : if (tif->tif_flags&TIFF_SWAB)
2719 0 : TIFFSwabLong(ma);
2720 0 : *mb++=(uint64)(*ma++);
2721 : }
2722 : }
2723 : break;
2724 : }
2725 0 : _TIFFfree(origdata);
2726 0 : if (err!=TIFFReadDirEntryErrOk)
2727 : {
2728 0 : _TIFFfree(data);
2729 0 : return(err);
2730 : }
2731 0 : *value=data;
2732 0 : return(TIFFReadDirEntryErrOk);
2733 : }
2734 :
2735 2963 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2736 : {
2737 : enum TIFFReadDirEntryErr err;
2738 : uint16* m;
2739 : uint16* na;
2740 : uint16 nb;
2741 2963 : if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2742 0 : return(TIFFReadDirEntryErrCount);
2743 2963 : err=TIFFReadDirEntryShortArray(tif,direntry,&m);
2744 2963 : if (err!=TIFFReadDirEntryErrOk)
2745 0 : return(err);
2746 2963 : na=m;
2747 2963 : nb=tif->tif_dir.td_samplesperpixel;
2748 2963 : *value=*na++;
2749 2963 : nb--;
2750 798360 : while (nb>0)
2751 : {
2752 792434 : if (*na++!=*value)
2753 : {
2754 0 : err=TIFFReadDirEntryErrPsdif;
2755 0 : break;
2756 : }
2757 792434 : nb--;
2758 : }
2759 2963 : _TIFFfree(m);
2760 2963 : return(err);
2761 : }
2762 :
2763 : #if 0
2764 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2765 : {
2766 : enum TIFFReadDirEntryErr err;
2767 : double* m;
2768 : double* na;
2769 : uint16 nb;
2770 : if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2771 : return(TIFFReadDirEntryErrCount);
2772 : err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
2773 : if (err!=TIFFReadDirEntryErrOk)
2774 : return(err);
2775 : na=m;
2776 : nb=tif->tif_dir.td_samplesperpixel;
2777 : *value=*na++;
2778 : nb--;
2779 : while (nb>0)
2780 : {
2781 : if (*na++!=*value)
2782 : {
2783 : err=TIFFReadDirEntryErrPsdif;
2784 : break;
2785 : }
2786 : nb--;
2787 : }
2788 : _TIFFfree(m);
2789 : return(err);
2790 : }
2791 : #endif
2792 :
2793 0 : static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
2794 : {
2795 : (void) tif;
2796 0 : *value=*(uint8*)(&direntry->tdir_offset);
2797 0 : }
2798 :
2799 0 : static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
2800 : {
2801 : (void) tif;
2802 0 : *value=*(int8*)(&direntry->tdir_offset);
2803 0 : }
2804 :
2805 61330 : static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2806 : {
2807 61330 : *value = direntry->tdir_offset.toff_short;
2808 : /* *value=*(uint16*)(&direntry->tdir_offset); */
2809 61330 : if (tif->tif_flags&TIFF_SWAB)
2810 2597 : TIFFSwabShort(value);
2811 61330 : }
2812 :
2813 0 : static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
2814 : {
2815 0 : *value=*(int16*)(&direntry->tdir_offset);
2816 0 : if (tif->tif_flags&TIFF_SWAB)
2817 0 : TIFFSwabShort((uint16*)value);
2818 0 : }
2819 :
2820 2108 : static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
2821 : {
2822 2108 : *value=*(uint32*)(&direntry->tdir_offset);
2823 2108 : if (tif->tif_flags&TIFF_SWAB)
2824 172 : TIFFSwabLong(value);
2825 2108 : }
2826 :
2827 0 : static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
2828 : {
2829 0 : *value=*(int32*)(&direntry->tdir_offset);
2830 0 : if (tif->tif_flags&TIFF_SWAB)
2831 0 : TIFFSwabLong((uint32*)value);
2832 0 : }
2833 :
2834 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
2835 : {
2836 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
2837 : {
2838 : enum TIFFReadDirEntryErr err;
2839 0 : uint32 offset = direntry->tdir_offset.toff_long;
2840 0 : if (tif->tif_flags&TIFF_SWAB)
2841 0 : TIFFSwabLong(&offset);
2842 0 : err=TIFFReadDirEntryData(tif,offset,8,value);
2843 0 : if (err!=TIFFReadDirEntryErrOk)
2844 0 : return(err);
2845 : }
2846 : else
2847 0 : *value = direntry->tdir_offset.toff_long8;
2848 0 : if (tif->tif_flags&TIFF_SWAB)
2849 0 : TIFFSwabLong8(value);
2850 0 : return(TIFFReadDirEntryErrOk);
2851 : }
2852 :
2853 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
2854 : {
2855 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
2856 : {
2857 : enum TIFFReadDirEntryErr err;
2858 0 : uint32 offset = direntry->tdir_offset.toff_long;
2859 0 : if (tif->tif_flags&TIFF_SWAB)
2860 0 : TIFFSwabLong(&offset);
2861 0 : err=TIFFReadDirEntryData(tif,offset,8,value);
2862 0 : if (err!=TIFFReadDirEntryErrOk)
2863 0 : return(err);
2864 : }
2865 : else
2866 0 : *value=*(int64*)(&direntry->tdir_offset);
2867 0 : if (tif->tif_flags&TIFF_SWAB)
2868 0 : TIFFSwabLong8((uint64*)value);
2869 0 : return(TIFFReadDirEntryErrOk);
2870 : }
2871 :
2872 92 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2873 : {
2874 : UInt64Aligned_t m;
2875 :
2876 : assert(sizeof(double)==8);
2877 : assert(sizeof(uint64)==8);
2878 : assert(sizeof(uint32)==4);
2879 92 : if (!(tif->tif_flags&TIFF_BIGTIFF))
2880 : {
2881 : enum TIFFReadDirEntryErr err;
2882 92 : uint32 offset = direntry->tdir_offset.toff_long;
2883 92 : if (tif->tif_flags&TIFF_SWAB)
2884 8 : TIFFSwabLong(&offset);
2885 92 : err=TIFFReadDirEntryData(tif,offset,8,m.i);
2886 92 : if (err!=TIFFReadDirEntryErrOk)
2887 0 : return(err);
2888 : }
2889 : else
2890 0 : m.l = direntry->tdir_offset.toff_long8;
2891 92 : if (tif->tif_flags&TIFF_SWAB)
2892 8 : TIFFSwabArrayOfLong(m.i,2);
2893 92 : if (m.i[0]==0)
2894 14 : *value=0.0;
2895 : else
2896 78 : *value=(double)m.i[0]/(double)m.i[1];
2897 92 : return(TIFFReadDirEntryErrOk);
2898 : }
2899 :
2900 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2901 : {
2902 : UInt64Aligned_t m;
2903 : assert(sizeof(double)==8);
2904 : assert(sizeof(uint64)==8);
2905 : assert(sizeof(int32)==4);
2906 : assert(sizeof(uint32)==4);
2907 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
2908 : {
2909 : enum TIFFReadDirEntryErr err;
2910 0 : uint32 offset = direntry->tdir_offset.toff_long;
2911 0 : if (tif->tif_flags&TIFF_SWAB)
2912 0 : TIFFSwabLong(&offset);
2913 0 : err=TIFFReadDirEntryData(tif,offset,8,m.i);
2914 0 : if (err!=TIFFReadDirEntryErrOk)
2915 0 : return(err);
2916 : }
2917 : else
2918 0 : m.l=direntry->tdir_offset.toff_long8;
2919 0 : if (tif->tif_flags&TIFF_SWAB)
2920 0 : TIFFSwabArrayOfLong(m.i,2);
2921 0 : if ((int32)m.i[0]==0)
2922 0 : *value=0.0;
2923 : else
2924 0 : *value=(double)((int32)m.i[0])/(double)m.i[1];
2925 0 : return(TIFFReadDirEntryErrOk);
2926 : }
2927 :
2928 0 : static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
2929 : {
2930 : union
2931 : {
2932 : float f;
2933 : uint32 i;
2934 : } float_union;
2935 : assert(sizeof(float)==4);
2936 : assert(sizeof(uint32)==4);
2937 : assert(sizeof(float_union)==4);
2938 0 : float_union.i=*(uint32*)(&direntry->tdir_offset);
2939 0 : *value=float_union.f;
2940 0 : if (tif->tif_flags&TIFF_SWAB)
2941 0 : TIFFSwabLong((uint32*)value);
2942 0 : }
2943 :
2944 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2945 : {
2946 : assert(sizeof(double)==8);
2947 : assert(sizeof(uint64)==8);
2948 : assert(sizeof(UInt64Aligned_t)==8);
2949 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
2950 : {
2951 : enum TIFFReadDirEntryErr err;
2952 0 : uint32 offset = direntry->tdir_offset.toff_long;
2953 0 : if (tif->tif_flags&TIFF_SWAB)
2954 0 : TIFFSwabLong(&offset);
2955 0 : err=TIFFReadDirEntryData(tif,offset,8,value);
2956 0 : if (err!=TIFFReadDirEntryErrOk)
2957 0 : return(err);
2958 : }
2959 : else
2960 : {
2961 : UInt64Aligned_t uint64_union;
2962 0 : uint64_union.l=direntry->tdir_offset.toff_long8;
2963 0 : *value=uint64_union.d;
2964 : }
2965 0 : if (tif->tif_flags&TIFF_SWAB)
2966 0 : TIFFSwabLong8((uint64*)value);
2967 0 : return(TIFFReadDirEntryErrOk);
2968 : }
2969 :
2970 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
2971 : {
2972 0 : if (value<0)
2973 0 : return(TIFFReadDirEntryErrRange);
2974 : else
2975 0 : return(TIFFReadDirEntryErrOk);
2976 : }
2977 :
2978 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
2979 : {
2980 0 : if (value>0xFF)
2981 0 : return(TIFFReadDirEntryErrRange);
2982 : else
2983 0 : return(TIFFReadDirEntryErrOk);
2984 : }
2985 :
2986 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
2987 : {
2988 0 : if ((value<0)||(value>0xFF))
2989 0 : return(TIFFReadDirEntryErrRange);
2990 : else
2991 0 : return(TIFFReadDirEntryErrOk);
2992 : }
2993 :
2994 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
2995 : {
2996 0 : if (value>0xFF)
2997 0 : return(TIFFReadDirEntryErrRange);
2998 : else
2999 0 : return(TIFFReadDirEntryErrOk);
3000 : }
3001 :
3002 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
3003 : {
3004 0 : if ((value<0)||(value>0xFF))
3005 0 : return(TIFFReadDirEntryErrRange);
3006 : else
3007 0 : return(TIFFReadDirEntryErrOk);
3008 : }
3009 :
3010 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
3011 : {
3012 0 : if (value>0xFF)
3013 0 : return(TIFFReadDirEntryErrRange);
3014 : else
3015 0 : return(TIFFReadDirEntryErrOk);
3016 : }
3017 :
3018 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
3019 : {
3020 0 : if ((value<0)||(value>0xFF))
3021 0 : return(TIFFReadDirEntryErrRange);
3022 : else
3023 0 : return(TIFFReadDirEntryErrOk);
3024 : }
3025 :
3026 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
3027 : {
3028 0 : if (value>0x7F)
3029 0 : return(TIFFReadDirEntryErrRange);
3030 : else
3031 0 : return(TIFFReadDirEntryErrOk);
3032 : }
3033 :
3034 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
3035 : {
3036 0 : if (value>0x7F)
3037 0 : return(TIFFReadDirEntryErrRange);
3038 : else
3039 0 : return(TIFFReadDirEntryErrOk);
3040 : }
3041 :
3042 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
3043 : {
3044 0 : if ((value<-0x80)||(value>0x7F))
3045 0 : return(TIFFReadDirEntryErrRange);
3046 : else
3047 0 : return(TIFFReadDirEntryErrOk);
3048 : }
3049 :
3050 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
3051 : {
3052 0 : if (value>0x7F)
3053 0 : return(TIFFReadDirEntryErrRange);
3054 : else
3055 0 : return(TIFFReadDirEntryErrOk);
3056 : }
3057 :
3058 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
3059 : {
3060 0 : if ((value<-0x80)||(value>0x7F))
3061 0 : return(TIFFReadDirEntryErrRange);
3062 : else
3063 0 : return(TIFFReadDirEntryErrOk);
3064 : }
3065 :
3066 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
3067 : {
3068 0 : if (value>0x7F)
3069 0 : return(TIFFReadDirEntryErrRange);
3070 : else
3071 0 : return(TIFFReadDirEntryErrOk);
3072 : }
3073 :
3074 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
3075 : {
3076 0 : if ((value<-0x80)||(value>0x7F))
3077 0 : return(TIFFReadDirEntryErrRange);
3078 : else
3079 0 : return(TIFFReadDirEntryErrOk);
3080 : }
3081 :
3082 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
3083 : {
3084 0 : if (value<0)
3085 0 : return(TIFFReadDirEntryErrRange);
3086 : else
3087 0 : return(TIFFReadDirEntryErrOk);
3088 : }
3089 :
3090 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
3091 : {
3092 0 : if (value<0)
3093 0 : return(TIFFReadDirEntryErrRange);
3094 : else
3095 0 : return(TIFFReadDirEntryErrOk);
3096 : }
3097 :
3098 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
3099 : {
3100 0 : if (value>0xFFFF)
3101 0 : return(TIFFReadDirEntryErrRange);
3102 : else
3103 0 : return(TIFFReadDirEntryErrOk);
3104 : }
3105 :
3106 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
3107 : {
3108 0 : if ((value<0)||(value>0xFFFF))
3109 0 : return(TIFFReadDirEntryErrRange);
3110 : else
3111 0 : return(TIFFReadDirEntryErrOk);
3112 : }
3113 :
3114 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
3115 : {
3116 0 : if (value>0xFFFF)
3117 0 : return(TIFFReadDirEntryErrRange);
3118 : else
3119 0 : return(TIFFReadDirEntryErrOk);
3120 : }
3121 :
3122 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
3123 : {
3124 0 : if ((value<0)||(value>0xFFFF))
3125 0 : return(TIFFReadDirEntryErrRange);
3126 : else
3127 0 : return(TIFFReadDirEntryErrOk);
3128 : }
3129 :
3130 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
3131 : {
3132 0 : if (value>0x7FFF)
3133 0 : return(TIFFReadDirEntryErrRange);
3134 : else
3135 0 : return(TIFFReadDirEntryErrOk);
3136 : }
3137 :
3138 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
3139 : {
3140 0 : if (value>0x7FFF)
3141 0 : return(TIFFReadDirEntryErrRange);
3142 : else
3143 0 : return(TIFFReadDirEntryErrOk);
3144 : }
3145 :
3146 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
3147 : {
3148 0 : if ((value<-0x8000)||(value>0x7FFF))
3149 0 : return(TIFFReadDirEntryErrRange);
3150 : else
3151 0 : return(TIFFReadDirEntryErrOk);
3152 : }
3153 :
3154 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
3155 : {
3156 0 : if (value>0x7FFF)
3157 0 : return(TIFFReadDirEntryErrRange);
3158 : else
3159 0 : return(TIFFReadDirEntryErrOk);
3160 : }
3161 :
3162 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
3163 : {
3164 0 : if ((value<-0x8000)||(value>0x7FFF))
3165 0 : return(TIFFReadDirEntryErrRange);
3166 : else
3167 0 : return(TIFFReadDirEntryErrOk);
3168 : }
3169 :
3170 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
3171 : {
3172 0 : if (value<0)
3173 0 : return(TIFFReadDirEntryErrRange);
3174 : else
3175 0 : return(TIFFReadDirEntryErrOk);
3176 : }
3177 :
3178 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
3179 : {
3180 0 : if (value<0)
3181 0 : return(TIFFReadDirEntryErrRange);
3182 : else
3183 0 : return(TIFFReadDirEntryErrOk);
3184 : }
3185 :
3186 0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
3187 : {
3188 0 : if (value<0)
3189 0 : return(TIFFReadDirEntryErrRange);
3190 : else
3191 0 : return(TIFFReadDirEntryErrOk);
3192 : }
3193 :
3194 : /*
3195 : * Largest 32-bit unsigned integer value.
3196 : */
3197 : #if defined(__WIN32__) && defined(_MSC_VER)
3198 : # define TIFF_UINT32_MAX 0xFFFFFFFFI64
3199 : #else
3200 : # define TIFF_UINT32_MAX 0xFFFFFFFFLL
3201 : #endif
3202 :
3203 : static enum TIFFReadDirEntryErr
3204 0 : TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
3205 : {
3206 0 : if (value > TIFF_UINT32_MAX)
3207 0 : return(TIFFReadDirEntryErrRange);
3208 : else
3209 0 : return(TIFFReadDirEntryErrOk);
3210 : }
3211 :
3212 : static enum TIFFReadDirEntryErr
3213 0 : TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
3214 : {
3215 0 : if ((value<0) || (value > TIFF_UINT32_MAX))
3216 0 : return(TIFFReadDirEntryErrRange);
3217 : else
3218 0 : return(TIFFReadDirEntryErrOk);
3219 : }
3220 :
3221 : #undef TIFF_UINT32_MAX
3222 :
3223 : static enum TIFFReadDirEntryErr
3224 0 : TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
3225 : {
3226 0 : if (value > 0x7FFFFFFFUL)
3227 0 : return(TIFFReadDirEntryErrRange);
3228 : else
3229 0 : return(TIFFReadDirEntryErrOk);
3230 : }
3231 :
3232 : static enum TIFFReadDirEntryErr
3233 0 : TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
3234 : {
3235 0 : if (value > 0x7FFFFFFFUL)
3236 0 : return(TIFFReadDirEntryErrRange);
3237 : else
3238 0 : return(TIFFReadDirEntryErrOk);
3239 : }
3240 :
3241 : static enum TIFFReadDirEntryErr
3242 0 : TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
3243 : {
3244 0 : if ((value < 0L-0x80000000L) || (value > 0x7FFFFFFFL))
3245 0 : return(TIFFReadDirEntryErrRange);
3246 : else
3247 0 : return(TIFFReadDirEntryErrOk);
3248 : }
3249 :
3250 : static enum TIFFReadDirEntryErr
3251 0 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
3252 : {
3253 0 : if (value < 0)
3254 0 : return(TIFFReadDirEntryErrRange);
3255 : else
3256 0 : return(TIFFReadDirEntryErrOk);
3257 : }
3258 :
3259 : static enum TIFFReadDirEntryErr
3260 0 : TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
3261 : {
3262 0 : if (value < 0)
3263 0 : return(TIFFReadDirEntryErrRange);
3264 : else
3265 0 : return(TIFFReadDirEntryErrOk);
3266 : }
3267 :
3268 : static enum TIFFReadDirEntryErr
3269 0 : TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
3270 : {
3271 0 : if (value < 0)
3272 0 : return(TIFFReadDirEntryErrRange);
3273 : else
3274 0 : return(TIFFReadDirEntryErrOk);
3275 : }
3276 :
3277 : static enum TIFFReadDirEntryErr
3278 0 : TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
3279 : {
3280 0 : if (value < 0)
3281 0 : return(TIFFReadDirEntryErrRange);
3282 : else
3283 0 : return(TIFFReadDirEntryErrOk);
3284 : }
3285 :
3286 : /*
3287 : * Largest 64-bit signed integer value.
3288 : */
3289 : #if defined(__WIN32__) && defined(_MSC_VER)
3290 : # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFI64
3291 : #else
3292 : # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFLL
3293 : #endif
3294 :
3295 : static enum TIFFReadDirEntryErr
3296 0 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
3297 : {
3298 0 : if (value > TIFF_INT64_MAX)
3299 0 : return(TIFFReadDirEntryErrRange);
3300 : else
3301 0 : return(TIFFReadDirEntryErrOk);
3302 : }
3303 :
3304 : #undef TIFF_INT64_MAX
3305 :
3306 : static enum TIFFReadDirEntryErr
3307 22540 : TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
3308 : {
3309 22540 : assert(size>0);
3310 22540 : if (!isMapped(tif)) {
3311 22540 : if (!SeekOK(tif,offset))
3312 0 : return(TIFFReadDirEntryErrIo);
3313 22540 : if (!ReadOK(tif,dest,size))
3314 1 : return(TIFFReadDirEntryErrIo);
3315 : } else {
3316 : tmsize_t ma,mb;
3317 0 : ma=(tmsize_t)offset;
3318 0 : mb=ma+size;
3319 0 : if (((uint64)ma!=offset)||(mb<ma)||(mb<size)||(mb>tif->tif_size))
3320 0 : return(TIFFReadDirEntryErrIo);
3321 0 : _TIFFmemcpy(dest,tif->tif_base+ma,size);
3322 : }
3323 22539 : return(TIFFReadDirEntryErrOk);
3324 : }
3325 :
3326 1 : static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
3327 : {
3328 1 : if (!recover) {
3329 0 : switch (err) {
3330 : case TIFFReadDirEntryErrCount:
3331 0 : TIFFErrorExt(tif->tif_clientdata, module,
3332 : "Incorrect count for \"%s\"",
3333 : tagname);
3334 0 : break;
3335 : case TIFFReadDirEntryErrType:
3336 0 : TIFFErrorExt(tif->tif_clientdata, module,
3337 : "Incompatible type for \"%s\"",
3338 : tagname);
3339 0 : break;
3340 : case TIFFReadDirEntryErrIo:
3341 0 : TIFFErrorExt(tif->tif_clientdata, module,
3342 : "IO error during reading of \"%s\"",
3343 : tagname);
3344 0 : break;
3345 : case TIFFReadDirEntryErrRange:
3346 0 : TIFFErrorExt(tif->tif_clientdata, module,
3347 : "Incorrect value for \"%s\"",
3348 : tagname);
3349 0 : break;
3350 : case TIFFReadDirEntryErrPsdif:
3351 0 : TIFFErrorExt(tif->tif_clientdata, module,
3352 : "Cannot handle different values per sample for \"%s\"",
3353 : tagname);
3354 0 : break;
3355 : case TIFFReadDirEntryErrSizesan:
3356 0 : TIFFErrorExt(tif->tif_clientdata, module,
3357 : "Sanity check on size of \"%s\" value failed",
3358 : tagname);
3359 0 : break;
3360 : case TIFFReadDirEntryErrAlloc:
3361 0 : TIFFErrorExt(tif->tif_clientdata, module,
3362 : "Out of memory reading of \"%s\"",
3363 : tagname);
3364 0 : break;
3365 : default:
3366 0 : assert(0); /* we should never get here */
3367 : break;
3368 : }
3369 : } else {
3370 1 : switch (err) {
3371 : case TIFFReadDirEntryErrCount:
3372 0 : TIFFErrorExt(tif->tif_clientdata, module,
3373 : "Incorrect count for \"%s\"; tag ignored",
3374 : tagname);
3375 0 : break;
3376 : case TIFFReadDirEntryErrType:
3377 0 : TIFFWarningExt(tif->tif_clientdata, module,
3378 : "Incompatible type for \"%s\"; tag ignored",
3379 : tagname);
3380 0 : break;
3381 : case TIFFReadDirEntryErrIo:
3382 1 : TIFFWarningExt(tif->tif_clientdata, module,
3383 : "IO error during reading of \"%s\"; tag ignored",
3384 : tagname);
3385 1 : break;
3386 : case TIFFReadDirEntryErrRange:
3387 0 : TIFFWarningExt(tif->tif_clientdata, module,
3388 : "Incorrect value for \"%s\"; tag ignored",
3389 : tagname);
3390 0 : break;
3391 : case TIFFReadDirEntryErrPsdif:
3392 0 : TIFFWarningExt(tif->tif_clientdata, module,
3393 : "Cannot handle different values per sample for \"%s\"; tag ignored",
3394 : tagname);
3395 0 : break;
3396 : case TIFFReadDirEntryErrSizesan:
3397 0 : TIFFWarningExt(tif->tif_clientdata, module,
3398 : "Sanity check on size of \"%s\" value failed; tag ignored",
3399 : tagname);
3400 0 : break;
3401 : case TIFFReadDirEntryErrAlloc:
3402 0 : TIFFWarningExt(tif->tif_clientdata, module,
3403 : "Out of memory reading of \"%s\"; tag ignored",
3404 : tagname);
3405 0 : break;
3406 : default:
3407 0 : assert(0); /* we should never get here */
3408 : break;
3409 : }
3410 : }
3411 1 : }
3412 :
3413 : /*
3414 : * Read the next TIFF directory from a file and convert it to the internal
3415 : * format. We read directories sequentially.
3416 : */
3417 : int
3418 6976 : TIFFReadDirectory(TIFF* tif)
3419 : {
3420 : static const char module[] = "TIFFReadDirectory";
3421 : TIFFDirEntry* dir;
3422 : uint16 dircount;
3423 : TIFFDirEntry* dp;
3424 : uint16 di;
3425 : const TIFFField* fip;
3426 6976 : uint32 fii=FAILED_FII;
3427 : toff_t nextdiroff;
3428 6976 : tif->tif_diroff=tif->tif_nextdiroff;
3429 6976 : if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
3430 95 : return 0; /* last offset or bad offset (IFD looping) */
3431 6881 : (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
3432 6881 : tif->tif_curdir++;
3433 6881 : nextdiroff = tif->tif_nextdiroff;
3434 6881 : dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
3435 6881 : if (!dircount)
3436 : {
3437 0 : TIFFErrorExt(tif->tif_clientdata,module,
3438 : "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
3439 0 : return 0;
3440 : }
3441 6881 : TIFFReadDirectoryCheckOrder(tif,dir,dircount);
3442 : {
3443 : TIFFDirEntry* ma;
3444 : uint16 mb;
3445 105664 : for (ma=dir, mb=0; mb<dircount; ma++, mb++)
3446 : {
3447 : TIFFDirEntry* na;
3448 : uint16 nb;
3449 768587 : for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
3450 : {
3451 669804 : if (ma->tdir_tag==na->tdir_tag)
3452 0 : na->tdir_tag=IGNORE;
3453 : }
3454 : }
3455 : }
3456 6881 : tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
3457 6881 : tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
3458 : /* free any old stuff and reinit */
3459 6881 : TIFFFreeDirectory(tif);
3460 6881 : TIFFDefaultDirectory(tif);
3461 : /*
3462 : * Electronic Arts writes gray-scale TIFF files
3463 : * without a PlanarConfiguration directory entry.
3464 : * Thus we setup a default value here, even though
3465 : * the TIFF spec says there is no default value.
3466 : */
3467 6881 : TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3468 : /*
3469 : * Setup default value and then make a pass over
3470 : * the fields to check type and tag information,
3471 : * and to extract info required to size data
3472 : * structures. A second pass is made afterwards
3473 : * to read in everthing not taken in the first pass.
3474 : * But we must process the Compression tag first
3475 : * in order to merge in codec-private tag definitions (otherwise
3476 : * we may get complaints about unknown tags). However, the
3477 : * Compression tag may be dependent on the SamplesPerPixel
3478 : * tag value because older TIFF specs permited Compression
3479 : * to be written as a SamplesPerPixel-count tag entry.
3480 : * Thus if we don't first figure out the correct SamplesPerPixel
3481 : * tag value then we may end up ignoring the Compression tag
3482 : * value because it has an incorrect count value (if the
3483 : * true value of SamplesPerPixel is not 1).
3484 : */
3485 6881 : dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
3486 6881 : if (dp)
3487 : {
3488 6877 : if (!TIFFFetchNormalTag(tif,dp,0))
3489 0 : goto bad;
3490 6877 : dp->tdir_tag=IGNORE;
3491 : }
3492 6881 : dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
3493 6881 : if (dp)
3494 : {
3495 : /*
3496 : * The 5.0 spec says the Compression tag has one value, while
3497 : * earlier specs say it has one value per sample. Because of
3498 : * this, we accept the tag if one value is supplied with either
3499 : * count.
3500 : */
3501 : uint16 value;
3502 : enum TIFFReadDirEntryErr err;
3503 6881 : err=TIFFReadDirEntryShort(tif,dp,&value);
3504 6881 : if (err==TIFFReadDirEntryErrCount)
3505 0 : err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3506 6881 : if (err!=TIFFReadDirEntryErrOk)
3507 : {
3508 0 : TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
3509 0 : goto bad;
3510 : }
3511 6881 : if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
3512 0 : goto bad;
3513 6881 : dp->tdir_tag=IGNORE;
3514 : }
3515 : else
3516 : {
3517 0 : if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
3518 0 : goto bad;
3519 : }
3520 : /*
3521 : * First real pass over the directory.
3522 : */
3523 105664 : for (di=0, dp=dir; di<dircount; di++, dp++)
3524 : {
3525 98783 : if (dp->tdir_tag!=IGNORE)
3526 : {
3527 85025 : TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3528 85025 : if (fii == FAILED_FII)
3529 : {
3530 0 : TIFFWarningExt(tif->tif_clientdata, module,
3531 : "Unknown field with tag %d (0x%x) encountered",
3532 0 : dp->tdir_tag,dp->tdir_tag);
3533 : /* the following knowingly leaks the
3534 : anonymous field structure */
3535 0 : if (!_TIFFMergeFields(tif,
3536 0 : _TIFFCreateAnonField(tif,
3537 0 : dp->tdir_tag,
3538 : (TIFFDataType) dp->tdir_type),
3539 : 1)) {
3540 0 : TIFFWarningExt(tif->tif_clientdata,
3541 : module,
3542 : "Registering anonymous field with tag %d (0x%x) failed",
3543 0 : dp->tdir_tag,
3544 0 : dp->tdir_tag);
3545 0 : dp->tdir_tag=IGNORE;
3546 : } else {
3547 0 : TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3548 0 : assert(fii != FAILED_FII);
3549 : }
3550 : }
3551 : }
3552 98783 : if (dp->tdir_tag!=IGNORE)
3553 : {
3554 85025 : fip=tif->tif_fields[fii];
3555 85025 : if (fip->field_bit==FIELD_IGNORE)
3556 0 : dp->tdir_tag=IGNORE;
3557 : else
3558 : {
3559 85025 : switch (dp->tdir_tag)
3560 : {
3561 : case TIFFTAG_STRIPOFFSETS:
3562 : case TIFFTAG_STRIPBYTECOUNTS:
3563 : case TIFFTAG_TILEOFFSETS:
3564 : case TIFFTAG_TILEBYTECOUNTS:
3565 13762 : TIFFSetFieldBit(tif,fip->field_bit);
3566 13762 : break;
3567 : case TIFFTAG_IMAGEWIDTH:
3568 : case TIFFTAG_IMAGELENGTH:
3569 : case TIFFTAG_IMAGEDEPTH:
3570 : case TIFFTAG_TILELENGTH:
3571 : case TIFFTAG_TILEWIDTH:
3572 : case TIFFTAG_TILEDEPTH:
3573 : case TIFFTAG_PLANARCONFIG:
3574 : case TIFFTAG_ROWSPERSTRIP:
3575 : case TIFFTAG_EXTRASAMPLES:
3576 29979 : if (!TIFFFetchNormalTag(tif,dp,0))
3577 0 : goto bad;
3578 29979 : dp->tdir_tag=IGNORE;
3579 : break;
3580 : }
3581 : }
3582 : }
3583 : }
3584 : /*
3585 : * XXX: OJPEG hack.
3586 : * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3587 : * c) strip offsets/bytecounts tag are both present and
3588 : * d) both contain exactly one value, then we consistently find
3589 : * that the buggy implementation of the buggy compression scheme
3590 : * matches contig planarconfig best. So we 'fix-up' the tag here
3591 : */
3592 6882 : if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
3593 1 : (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
3594 : {
3595 0 : if (!_TIFFFillStriles(tif))
3596 0 : goto bad;
3597 0 : dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
3598 0 : if ((dp!=0)&&(dp->tdir_count==1))
3599 : {
3600 0 : dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
3601 : TIFFTAG_STRIPBYTECOUNTS);
3602 0 : if ((dp!=0)&&(dp->tdir_count==1))
3603 : {
3604 0 : tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
3605 0 : TIFFWarningExt(tif->tif_clientdata,module,
3606 : "Planarconfig tag value assumed incorrect, "
3607 : "assuming data is contig instead of chunky");
3608 : }
3609 : }
3610 : }
3611 : /*
3612 : * Allocate directory structure and setup defaults.
3613 : */
3614 6881 : if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
3615 : {
3616 0 : MissingRequired(tif,"ImageLength");
3617 0 : goto bad;
3618 : }
3619 : /*
3620 : * Setup appropriate structures (by strip or by tile)
3621 : */
3622 6881 : if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3623 4858 : tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
3624 4858 : tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3625 4858 : tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
3626 4858 : tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
3627 4858 : tif->tif_flags &= ~TIFF_ISTILED;
3628 : } else {
3629 2023 : tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
3630 2023 : tif->tif_flags |= TIFF_ISTILED;
3631 : }
3632 6881 : if (!tif->tif_dir.td_nstrips) {
3633 0 : TIFFErrorExt(tif->tif_clientdata, module,
3634 : "Cannot handle zero number of %s",
3635 0 : isTiled(tif) ? "tiles" : "strips");
3636 0 : goto bad;
3637 : }
3638 6881 : tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
3639 6881 : if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
3640 452 : tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
3641 6881 : if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
3642 0 : if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
3643 0 : (isTiled(tif)==0) &&
3644 0 : (tif->tif_dir.td_nstrips==1)) {
3645 : /*
3646 : * XXX: OJPEG hack.
3647 : * If a) compression is OJPEG, b) it's not a tiled TIFF,
3648 : * and c) the number of strips is 1,
3649 : * then we tolerate the absence of stripoffsets tag,
3650 : * because, presumably, all required data is in the
3651 : * JpegInterchangeFormat stream.
3652 : */
3653 0 : TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
3654 : } else {
3655 0 : MissingRequired(tif,
3656 0 : isTiled(tif) ? "TileOffsets" : "StripOffsets");
3657 0 : goto bad;
3658 : }
3659 : }
3660 : /*
3661 : * Second pass: extract other information.
3662 : */
3663 105664 : for (di=0, dp=dir; di<dircount; di++, dp++)
3664 : {
3665 98783 : switch (dp->tdir_tag)
3666 : {
3667 : case IGNORE:
3668 43737 : break;
3669 : case TIFFTAG_MINSAMPLEVALUE:
3670 : case TIFFTAG_MAXSAMPLEVALUE:
3671 : case TIFFTAG_BITSPERSAMPLE:
3672 : case TIFFTAG_DATATYPE:
3673 : case TIFFTAG_SAMPLEFORMAT:
3674 : /*
3675 : * The MinSampleValue, MaxSampleValue, BitsPerSample
3676 : * DataType and SampleFormat tags are supposed to be
3677 : * written as one value/sample, but some vendors
3678 : * incorrectly write one value only -- so we accept
3679 : * that as well (yech). Other vendors write correct
3680 : * value for NumberOfSamples, but incorrect one for
3681 : * BitsPerSample and friends, and we will read this
3682 : * too.
3683 : */
3684 : {
3685 : uint16 value;
3686 : enum TIFFReadDirEntryErr err;
3687 13668 : err=TIFFReadDirEntryShort(tif,dp,&value);
3688 13668 : if (err==TIFFReadDirEntryErrCount)
3689 2963 : err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3690 13668 : if (err!=TIFFReadDirEntryErrOk)
3691 : {
3692 0 : fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3693 0 : TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3694 0 : goto bad;
3695 : }
3696 13668 : if (!TIFFSetField(tif,dp->tdir_tag,value))
3697 0 : goto bad;
3698 : }
3699 13668 : break;
3700 : case TIFFTAG_SMINSAMPLEVALUE:
3701 : case TIFFTAG_SMAXSAMPLEVALUE:
3702 : {
3703 :
3704 : double *data;
3705 : enum TIFFReadDirEntryErr err;
3706 : uint32 saved_flags;
3707 : int m;
3708 0 : if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
3709 0 : err = TIFFReadDirEntryErrCount;
3710 : else
3711 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
3712 0 : if (err!=TIFFReadDirEntryErrOk)
3713 : {
3714 0 : fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3715 0 : TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3716 0 : goto bad;
3717 : }
3718 0 : saved_flags = tif->tif_flags;
3719 0 : tif->tif_flags |= TIFF_PERSAMPLE;
3720 0 : m = TIFFSetField(tif,dp->tdir_tag,data);
3721 0 : tif->tif_flags = saved_flags;
3722 0 : _TIFFfree(data);
3723 0 : if (!m)
3724 0 : goto bad;
3725 : }
3726 0 : break;
3727 : case TIFFTAG_STRIPOFFSETS:
3728 : case TIFFTAG_TILEOFFSETS:
3729 : #if defined(DEFER_STRILE_LOAD)
3730 6881 : _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
3731 : dp, sizeof(TIFFDirEntry) );
3732 : #else
3733 : if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
3734 : goto bad;
3735 : #endif
3736 6881 : break;
3737 : case TIFFTAG_STRIPBYTECOUNTS:
3738 : case TIFFTAG_TILEBYTECOUNTS:
3739 : #if defined(DEFER_STRILE_LOAD)
3740 6881 : _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
3741 : dp, sizeof(TIFFDirEntry) );
3742 : #else
3743 : if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
3744 : goto bad;
3745 : #endif
3746 6881 : break;
3747 : case TIFFTAG_COLORMAP:
3748 : case TIFFTAG_TRANSFERFUNCTION:
3749 : {
3750 : enum TIFFReadDirEntryErr err;
3751 : uint32 countpersample;
3752 : uint32 countrequired;
3753 : uint32 incrementpersample;
3754 153 : uint16* value=NULL;
3755 153 : countpersample=(1L<<tif->tif_dir.td_bitspersample);
3756 153 : if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
3757 : {
3758 0 : countrequired=countpersample;
3759 0 : incrementpersample=0;
3760 : }
3761 : else
3762 : {
3763 153 : countrequired=3*countpersample;
3764 153 : incrementpersample=countpersample;
3765 : }
3766 153 : if (dp->tdir_count!=(uint64)countrequired)
3767 0 : err=TIFFReadDirEntryErrCount;
3768 : else
3769 153 : err=TIFFReadDirEntryShortArray(tif,dp,&value);
3770 153 : if (err!=TIFFReadDirEntryErrOk)
3771 : {
3772 0 : fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3773 0 : TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
3774 : }
3775 : else
3776 : {
3777 153 : TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
3778 153 : _TIFFfree(value);
3779 : }
3780 : }
3781 153 : break;
3782 : /* BEGIN REV 4.0 COMPATIBILITY */
3783 : case TIFFTAG_OSUBFILETYPE:
3784 : {
3785 : uint16 valueo;
3786 : uint32 value;
3787 0 : if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
3788 : {
3789 0 : switch (valueo)
3790 : {
3791 0 : case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
3792 0 : case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
3793 0 : default: value=0; break;
3794 : }
3795 0 : if (value!=0)
3796 0 : TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
3797 : }
3798 : }
3799 0 : break;
3800 : /* END REV 4.0 COMPATIBILITY */
3801 : default:
3802 27463 : (void) TIFFFetchNormalTag(tif, dp, TRUE);
3803 : break;
3804 : }
3805 : }
3806 : /*
3807 : * OJPEG hack:
3808 : * - If a) compression is OJPEG, and b) photometric tag is missing,
3809 : * then we consistently find that photometric should be YCbCr
3810 : * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
3811 : * then we consistently find that the buggy implementation of the
3812 : * buggy compression scheme matches photometric YCbCr instead.
3813 : * - If a) compression is OJPEG, and b) bitspersample tag is missing,
3814 : * then we consistently find bitspersample should be 8.
3815 : * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3816 : * and c) photometric is RGB or YCbCr, then we consistently find
3817 : * samplesperpixel should be 3
3818 : * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3819 : * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
3820 : * find samplesperpixel should be 3
3821 : */
3822 6881 : if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
3823 : {
3824 1 : if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
3825 : {
3826 0 : TIFFWarningExt(tif->tif_clientdata, module,
3827 : "Photometric tag is missing, assuming data is YCbCr");
3828 0 : if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
3829 0 : goto bad;
3830 : }
3831 1 : else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
3832 : {
3833 0 : tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
3834 0 : TIFFWarningExt(tif->tif_clientdata, module,
3835 : "Photometric tag value assumed incorrect, "
3836 : "assuming data is YCbCr instead of RGB");
3837 : }
3838 1 : if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
3839 : {
3840 0 : TIFFWarningExt(tif->tif_clientdata,module,
3841 : "BitsPerSample tag is missing, assuming 8 bits per sample");
3842 0 : if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
3843 0 : goto bad;
3844 : }
3845 1 : if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
3846 : {
3847 0 : if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
3848 : {
3849 0 : TIFFWarningExt(tif->tif_clientdata,module,
3850 : "SamplesPerPixel tag is missing, "
3851 : "assuming correct SamplesPerPixel value is 3");
3852 0 : if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
3853 0 : goto bad;
3854 : }
3855 0 : if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
3856 : {
3857 0 : TIFFWarningExt(tif->tif_clientdata,module,
3858 : "SamplesPerPixel tag is missing, "
3859 : "applying correct SamplesPerPixel value of 3");
3860 0 : if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
3861 0 : goto bad;
3862 : }
3863 0 : else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
3864 0 : || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
3865 : {
3866 : /*
3867 : * SamplesPerPixel tag is missing, but is not required
3868 : * by spec. Assume correct SamplesPerPixel value of 1.
3869 : */
3870 0 : if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
3871 0 : goto bad;
3872 : }
3873 : }
3874 : }
3875 : /*
3876 : * Verify Palette image has a Colormap.
3877 : */
3878 7028 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
3879 147 : !TIFFFieldSet(tif, FIELD_COLORMAP)) {
3880 0 : if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
3881 0 : tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
3882 0 : else if (tif->tif_dir.td_bitspersample>=8)
3883 0 : tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
3884 : else {
3885 0 : MissingRequired(tif, "Colormap");
3886 0 : goto bad;
3887 : }
3888 : }
3889 : /*
3890 : * OJPEG hack:
3891 : * We do no further messing with strip/tile offsets/bytecounts in OJPEG
3892 : * TIFFs
3893 : */
3894 6881 : if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
3895 : {
3896 : /*
3897 : * Attempt to deal with a missing StripByteCounts tag.
3898 : */
3899 6880 : if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
3900 : /*
3901 : * Some manufacturers violate the spec by not giving
3902 : * the size of the strips. In this case, assume there
3903 : * is one uncompressed strip of data.
3904 : */
3905 0 : if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
3906 0 : tif->tif_dir.td_nstrips > 1) ||
3907 0 : (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
3908 0 : tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
3909 0 : MissingRequired(tif, "StripByteCounts");
3910 0 : goto bad;
3911 : }
3912 0 : TIFFWarningExt(tif->tif_clientdata, module,
3913 : "TIFF directory is missing required "
3914 : "\"%s\" field, calculating from imagelength",
3915 0 : TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
3916 0 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
3917 0 : goto bad;
3918 : /*
3919 : * Assume we have wrong StripByteCount value (in case
3920 : * of single strip) in following cases:
3921 : * - it is equal to zero along with StripOffset;
3922 : * - it is larger than file itself (in case of uncompressed
3923 : * image);
3924 : * - it is smaller than the size of the bytes per row
3925 : * multiplied on the number of rows. The last case should
3926 : * not be checked in the case of writing new image,
3927 : * because we may do not know the exact strip size
3928 : * until the whole image will be written and directory
3929 : * dumped out.
3930 : */
3931 : #define BYTECOUNTLOOKSBAD \
3932 : ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
3933 : (tif->tif_dir.td_compression == COMPRESSION_NONE && \
3934 : tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
3935 : (tif->tif_mode == O_RDONLY && \
3936 : tif->tif_dir.td_compression == COMPRESSION_NONE && \
3937 : tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
3938 :
3939 37806 : } else if (tif->tif_dir.td_nstrips == 1
3940 6880 : && _TIFFFillStriles(tif)
3941 10568 : && tif->tif_dir.td_stripoffset[0] != 0
3942 20358 : && BYTECOUNTLOOKSBAD) {
3943 : /*
3944 : * XXX: Plexus (and others) sometimes give a value of
3945 : * zero for a tag when they don't know what the
3946 : * correct value is! Try and handle the simple case
3947 : * of estimating the size of a one strip image.
3948 : */
3949 0 : TIFFWarningExt(tif->tif_clientdata, module,
3950 : "Bogus \"%s\" field, ignoring and calculating from imagelength",
3951 0 : TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
3952 0 : if(EstimateStripByteCounts(tif, dir, dircount) < 0)
3953 0 : goto bad;
3954 :
3955 : #if !defined(DEFER_STRILE_LOAD)
3956 : } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
3957 : && tif->tif_dir.td_nstrips > 2
3958 : && tif->tif_dir.td_compression == COMPRESSION_NONE
3959 : && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
3960 : && tif->tif_dir.td_stripbytecount[0] != 0
3961 : && tif->tif_dir.td_stripbytecount[1] != 0 ) {
3962 : /*
3963 : * XXX: Some vendors fill StripByteCount array with
3964 : * absolutely wrong values (it can be equal to
3965 : * StripOffset array, for example). Catch this case
3966 : * here.
3967 : *
3968 : * We avoid this check if deferring strile loading
3969 : * as it would always force us to load the strip/tile
3970 : * information.
3971 : */
3972 : TIFFWarningExt(tif->tif_clientdata, module,
3973 : "Wrong \"%s\" field, ignoring and calculating from imagelength",
3974 : TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
3975 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
3976 : goto bad;
3977 : #endif /* !defined(DEFER_STRILE_LOAD) */
3978 : }
3979 : }
3980 6881 : if (dir)
3981 : {
3982 6881 : _TIFFfree(dir);
3983 6881 : dir=NULL;
3984 : }
3985 6881 : if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
3986 : {
3987 6881 : if (tif->tif_dir.td_bitspersample>=16)
3988 2634 : tif->tif_dir.td_maxsamplevalue=0xFFFF;
3989 : else
3990 4247 : tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
3991 : }
3992 : /*
3993 : * XXX: We can optimize checking for the strip bounds using the sorted
3994 : * bytecounts array. See also comments for TIFFAppendToStrip()
3995 : * function in tif_write.c.
3996 : */
3997 : #if !defined(DEFER_STRILE_LOAD)
3998 : if (tif->tif_dir.td_nstrips > 1) {
3999 : uint32 strip;
4000 :
4001 : tif->tif_dir.td_stripbytecountsorted = 1;
4002 : for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
4003 : if (tif->tif_dir.td_stripoffset[strip - 1] >
4004 : tif->tif_dir.td_stripoffset[strip]) {
4005 : tif->tif_dir.td_stripbytecountsorted = 0;
4006 : break;
4007 : }
4008 : }
4009 : }
4010 : #endif /* !defined(DEFER_STRILE_LOAD) */
4011 :
4012 : /*
4013 : * An opportunity for compression mode dependent tag fixup
4014 : */
4015 6881 : (*tif->tif_fixuptags)(tif);
4016 :
4017 : /*
4018 : * Some manufacturers make life difficult by writing
4019 : * large amounts of uncompressed data as a single strip.
4020 : * This is contrary to the recommendations of the spec.
4021 : * The following makes an attempt at breaking such images
4022 : * into strips closer to the recommended 8k bytes. A
4023 : * side effect, however, is that the RowsPerStrip tag
4024 : * value may be changed.
4025 : */
4026 23421 : if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
4027 6429 : (tif->tif_dir.td_nstrips==1)&&
4028 5275 : (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
4029 4836 : ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
4030 : {
4031 3124 : if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
4032 0 : return 0;
4033 3124 : ChopUpSingleUncompressedStrip(tif);
4034 : }
4035 :
4036 : /*
4037 : * Clear the dirty directory flag.
4038 : */
4039 6881 : tif->tif_flags &= ~TIFF_DIRTYDIRECT;
4040 6881 : tif->tif_flags &= ~TIFF_DIRTYSTRIP;
4041 :
4042 : /*
4043 : * Reinitialize i/o since we are starting on a new directory.
4044 : */
4045 6881 : tif->tif_row = (uint32) -1;
4046 6881 : tif->tif_curstrip = (uint32) -1;
4047 6881 : tif->tif_col = (uint32) -1;
4048 6881 : tif->tif_curtile = (uint32) -1;
4049 6881 : tif->tif_tilesize = (tmsize_t) -1;
4050 :
4051 6881 : tif->tif_scanlinesize = TIFFScanlineSize(tif);
4052 6881 : if (!tif->tif_scanlinesize) {
4053 0 : TIFFErrorExt(tif->tif_clientdata, module,
4054 : "Cannot handle zero scanline size");
4055 0 : return (0);
4056 : }
4057 :
4058 6881 : if (isTiled(tif)) {
4059 2023 : tif->tif_tilesize = TIFFTileSize(tif);
4060 2023 : if (!tif->tif_tilesize) {
4061 0 : TIFFErrorExt(tif->tif_clientdata, module,
4062 : "Cannot handle zero tile size");
4063 0 : return (0);
4064 : }
4065 : } else {
4066 4858 : if (!TIFFStripSize(tif)) {
4067 0 : TIFFErrorExt(tif->tif_clientdata, module,
4068 : "Cannot handle zero strip size");
4069 0 : return (0);
4070 : }
4071 : }
4072 6881 : return (1);
4073 : bad:
4074 0 : if (dir)
4075 0 : _TIFFfree(dir);
4076 0 : return (0);
4077 : }
4078 :
4079 : static void
4080 6881 : TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4081 : {
4082 : static const char module[] = "TIFFReadDirectoryCheckOrder";
4083 : uint16 m;
4084 : uint16 n;
4085 : TIFFDirEntry* o;
4086 6881 : m=0;
4087 105664 : for (n=0, o=dir; n<dircount; n++, o++)
4088 : {
4089 98783 : if (o->tdir_tag<m)
4090 : {
4091 0 : TIFFWarningExt(tif->tif_clientdata,module,
4092 : "Invalid TIFF directory; tags are not sorted in ascending order");
4093 0 : break;
4094 : }
4095 98783 : m=o->tdir_tag+1;
4096 : }
4097 6881 : }
4098 :
4099 : static TIFFDirEntry*
4100 13762 : TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
4101 : {
4102 : TIFFDirEntry* m;
4103 : uint16 n;
4104 : (void) tif;
4105 77812 : for (m=dir, n=0; n<dircount; m++, n++)
4106 : {
4107 77808 : if (m->tdir_tag==tagid)
4108 13758 : return(m);
4109 : }
4110 4 : return(0);
4111 : }
4112 :
4113 : static void
4114 149344 : TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
4115 : {
4116 : int32 ma,mb,mc;
4117 149344 : ma=-1;
4118 149344 : mc=(int32)tif->tif_nfields;
4119 : while (1)
4120 : {
4121 951738 : if (ma+1==mc)
4122 : {
4123 0 : *fii = FAILED_FII;
4124 0 : return;
4125 : }
4126 951738 : mb=(ma+mc)/2;
4127 951738 : if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
4128 149344 : break;
4129 802394 : if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
4130 262864 : ma=mb;
4131 : else
4132 539530 : mc=mb;
4133 802394 : }
4134 : while (1)
4135 : {
4136 149344 : if (mb==0)
4137 4062 : break;
4138 145282 : if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
4139 145282 : break;
4140 0 : mb--;
4141 0 : }
4142 149344 : *fii=mb;
4143 : }
4144 :
4145 : /*
4146 : * Read custom directory from the arbitarry offset.
4147 : * The code is very similar to TIFFReadDirectory().
4148 : */
4149 : int
4150 0 : TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
4151 : const TIFFFieldArray* infoarray)
4152 : {
4153 : static const char module[] = "TIFFReadCustomDirectory";
4154 : TIFFDirEntry* dir;
4155 : uint16 dircount;
4156 : TIFFDirEntry* dp;
4157 : uint16 di;
4158 : const TIFFField* fip;
4159 : uint32 fii;
4160 0 : _TIFFSetupFields(tif, infoarray);
4161 0 : dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
4162 0 : if (!dircount)
4163 : {
4164 0 : TIFFErrorExt(tif->tif_clientdata,module,
4165 : "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
4166 0 : return 0;
4167 : }
4168 0 : TIFFFreeDirectory(tif);
4169 0 : _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
4170 0 : TIFFReadDirectoryCheckOrder(tif,dir,dircount);
4171 0 : for (di=0, dp=dir; di<dircount; di++, dp++)
4172 : {
4173 0 : TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4174 0 : if (fii == FAILED_FII)
4175 : {
4176 0 : TIFFWarningExt(tif->tif_clientdata, module,
4177 : "Unknown field with tag %d (0x%x) encountered",
4178 0 : dp->tdir_tag, dp->tdir_tag);
4179 0 : if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
4180 0 : dp->tdir_tag,
4181 : (TIFFDataType) dp->tdir_type),
4182 : 1)) {
4183 0 : TIFFWarningExt(tif->tif_clientdata, module,
4184 : "Registering anonymous field with tag %d (0x%x) failed",
4185 0 : dp->tdir_tag, dp->tdir_tag);
4186 0 : dp->tdir_tag=IGNORE;
4187 : } else {
4188 0 : TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4189 0 : assert( fii != FAILED_FII );
4190 : }
4191 : }
4192 0 : if (dp->tdir_tag!=IGNORE)
4193 : {
4194 0 : fip=tif->tif_fields[fii];
4195 0 : if (fip->field_bit==FIELD_IGNORE)
4196 0 : dp->tdir_tag=IGNORE;
4197 : else
4198 : {
4199 : /* check data type */
4200 0 : while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
4201 : {
4202 0 : fii++;
4203 0 : if ((fii==tif->tif_nfields)||
4204 0 : (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
4205 : {
4206 0 : fii=0xFFFF;
4207 0 : break;
4208 : }
4209 0 : fip=tif->tif_fields[fii];
4210 : }
4211 0 : if (fii==0xFFFF)
4212 : {
4213 0 : TIFFWarningExt(tif->tif_clientdata, module,
4214 : "Wrong data type %d for \"%s\"; tag ignored",
4215 0 : dp->tdir_type,fip->field_name);
4216 0 : dp->tdir_tag=IGNORE;
4217 : }
4218 : else
4219 : {
4220 : /* check count if known in advance */
4221 0 : if ((fip->field_readcount!=TIFF_VARIABLE)&&
4222 0 : (fip->field_readcount!=TIFF_VARIABLE2))
4223 : {
4224 : uint32 expected;
4225 0 : if (fip->field_readcount==TIFF_SPP)
4226 0 : expected=(uint32)tif->tif_dir.td_samplesperpixel;
4227 : else
4228 0 : expected=(uint32)fip->field_readcount;
4229 0 : if (!CheckDirCount(tif,dp,expected))
4230 0 : dp->tdir_tag=IGNORE;
4231 : }
4232 : }
4233 : }
4234 0 : switch (dp->tdir_tag)
4235 : {
4236 : case IGNORE:
4237 0 : break;
4238 : case EXIFTAG_SUBJECTDISTANCE:
4239 0 : (void) TIFFFetchSubjectDistance(tif,dp);
4240 0 : break;
4241 : default:
4242 0 : (void) TIFFFetchNormalTag(tif, dp, TRUE);
4243 : break;
4244 : }
4245 : }
4246 : }
4247 0 : if (dir)
4248 0 : _TIFFfree(dir);
4249 0 : return 1;
4250 : }
4251 :
4252 : /*
4253 : * EXIF is important special case of custom IFD, so we have a special
4254 : * function to read it.
4255 : */
4256 : int
4257 0 : TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
4258 : {
4259 : const TIFFFieldArray* exifFieldArray;
4260 0 : exifFieldArray = _TIFFGetExifFields();
4261 0 : return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
4262 : }
4263 :
4264 : static int
4265 0 : EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4266 : {
4267 : static const char module[] = "EstimateStripByteCounts";
4268 :
4269 : TIFFDirEntry *dp;
4270 0 : TIFFDirectory *td = &tif->tif_dir;
4271 : uint32 strip;
4272 :
4273 0 : _TIFFFillStriles( tif );
4274 :
4275 0 : if (td->td_stripbytecount)
4276 0 : _TIFFfree(td->td_stripbytecount);
4277 0 : td->td_stripbytecount = (uint64*)
4278 0 : _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
4279 : "for \"StripByteCounts\" array");
4280 0 : if( td->td_stripbytecount == NULL )
4281 0 : return -1;
4282 :
4283 0 : if (td->td_compression != COMPRESSION_NONE) {
4284 : uint64 space;
4285 : uint64 filesize;
4286 : uint16 n;
4287 0 : filesize = TIFFGetFileSize(tif);
4288 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4289 0 : space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
4290 : else
4291 0 : space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
4292 : /* calculate amount of space used by indirect values */
4293 0 : for (dp = dir, n = dircount; n > 0; n--, dp++)
4294 : {
4295 0 : uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4296 : uint64 datasize;
4297 0 : typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4298 0 : if (typewidth == 0) {
4299 0 : TIFFErrorExt(tif->tif_clientdata, module,
4300 : "Cannot determine size of unknown tag type %d",
4301 0 : dp->tdir_type);
4302 0 : return -1;
4303 : }
4304 0 : datasize=(uint64)typewidth*dp->tdir_count;
4305 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4306 : {
4307 0 : if (datasize<=4)
4308 0 : datasize=0;
4309 : }
4310 : else
4311 : {
4312 0 : if (datasize<=8)
4313 0 : datasize=0;
4314 : }
4315 0 : space+=datasize;
4316 : }
4317 0 : space = filesize - space;
4318 0 : if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
4319 0 : space /= td->td_samplesperpixel;
4320 0 : for (strip = 0; strip < td->td_nstrips; strip++)
4321 0 : td->td_stripbytecount[strip] = space;
4322 : /*
4323 : * This gross hack handles the case were the offset to
4324 : * the last strip is past the place where we think the strip
4325 : * should begin. Since a strip of data must be contiguous,
4326 : * it's safe to assume that we've overestimated the amount
4327 : * of data in the strip and trim this number back accordingly.
4328 : */
4329 0 : strip--;
4330 0 : if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
4331 0 : td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
4332 0 : } else if (isTiled(tif)) {
4333 0 : uint64 bytespertile = TIFFTileSize64(tif);
4334 :
4335 0 : for (strip = 0; strip < td->td_nstrips; strip++)
4336 0 : td->td_stripbytecount[strip] = bytespertile;
4337 : } else {
4338 0 : uint64 rowbytes = TIFFScanlineSize64(tif);
4339 0 : uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
4340 0 : for (strip = 0; strip < td->td_nstrips; strip++)
4341 0 : td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
4342 : }
4343 0 : TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
4344 0 : if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
4345 0 : td->td_rowsperstrip = td->td_imagelength;
4346 0 : return 1;
4347 : }
4348 :
4349 : static void
4350 0 : MissingRequired(TIFF* tif, const char* tagname)
4351 : {
4352 : static const char module[] = "MissingRequired";
4353 :
4354 0 : TIFFErrorExt(tif->tif_clientdata, module,
4355 : "TIFF directory is missing required \"%s\" field",
4356 : tagname);
4357 0 : }
4358 :
4359 : /*
4360 : * Check the directory offset against the list of already seen directory
4361 : * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4362 : * file with looped directory pointers. We will maintain a list of already
4363 : * seen directories and check every IFD offset against that list.
4364 : */
4365 : static int
4366 6976 : TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
4367 : {
4368 : uint16 n;
4369 :
4370 6976 : if (diroff == 0) /* no more directories */
4371 95 : return 0;
4372 :
4373 7255 : for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
4374 374 : if (tif->tif_dirlist[n] == diroff)
4375 0 : return 0;
4376 : }
4377 :
4378 6881 : tif->tif_dirnumber++;
4379 :
4380 6881 : if (tif->tif_dirnumber > tif->tif_dirlistsize) {
4381 : uint64* new_dirlist;
4382 :
4383 : /*
4384 : * XXX: Reduce memory allocation granularity of the dirlist
4385 : * array.
4386 : */
4387 4822 : new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
4388 4822 : tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
4389 4822 : if (!new_dirlist)
4390 0 : return 0;
4391 4822 : tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
4392 4822 : tif->tif_dirlist = new_dirlist;
4393 : }
4394 :
4395 6881 : tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
4396 :
4397 6881 : return 1;
4398 : }
4399 :
4400 : /*
4401 : * Check the count field of a directory entry against a known value. The
4402 : * caller is expected to skip/ignore the tag if there is a mismatch.
4403 : */
4404 : static int
4405 0 : CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
4406 : {
4407 0 : if ((uint64)count > dir->tdir_count) {
4408 0 : const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4409 0 : TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4410 : "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
4411 : fip ? fip->field_name : "unknown tagname",
4412 : dir->tdir_count, count);
4413 0 : return (0);
4414 0 : } else if ((uint64)count < dir->tdir_count) {
4415 0 : const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4416 0 : TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4417 : "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
4418 : fip ? fip->field_name : "unknown tagname",
4419 : dir->tdir_count, count);
4420 0 : dir->tdir_count = count;
4421 0 : return (1);
4422 : }
4423 0 : return (1);
4424 : }
4425 :
4426 : /*
4427 : * Read IFD structure from the specified offset. If the pointer to
4428 : * nextdiroff variable has been specified, read it too. Function returns a
4429 : * number of fields in the directory or 0 if failed.
4430 : */
4431 : static uint16
4432 6881 : TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
4433 : uint64 *nextdiroff)
4434 : {
4435 : static const char module[] = "TIFFFetchDirectory";
4436 :
4437 : void* origdir;
4438 : uint16 dircount16;
4439 : uint32 dirsize;
4440 : TIFFDirEntry* dir;
4441 : uint8* ma;
4442 : TIFFDirEntry* mb;
4443 : uint16 n;
4444 :
4445 6881 : assert(pdir);
4446 :
4447 6881 : tif->tif_diroff = diroff;
4448 6881 : if (nextdiroff)
4449 6881 : *nextdiroff = 0;
4450 6881 : if (!isMapped(tif)) {
4451 6881 : if (!SeekOK(tif, tif->tif_diroff)) {
4452 0 : TIFFErrorExt(tif->tif_clientdata, module,
4453 : "%s: Seek error accessing TIFF directory",
4454 : tif->tif_name);
4455 0 : return 0;
4456 : }
4457 6881 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4458 : {
4459 6784 : if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
4460 0 : TIFFErrorExt(tif->tif_clientdata, module,
4461 : "%s: Can not read TIFF directory count",
4462 : tif->tif_name);
4463 0 : return 0;
4464 : }
4465 6784 : if (tif->tif_flags & TIFF_SWAB)
4466 284 : TIFFSwabShort(&dircount16);
4467 6784 : if (dircount16>4096)
4468 : {
4469 0 : TIFFErrorExt(tif->tif_clientdata, module,
4470 : "Sanity check on directory count failed, this is probably not a valid IFD offset");
4471 0 : return 0;
4472 : }
4473 6784 : dirsize = 12;
4474 : } else {
4475 : uint64 dircount64;
4476 97 : if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
4477 0 : TIFFErrorExt(tif->tif_clientdata, module,
4478 : "%s: Can not read TIFF directory count",
4479 : tif->tif_name);
4480 0 : return 0;
4481 : }
4482 97 : if (tif->tif_flags & TIFF_SWAB)
4483 12 : TIFFSwabLong8(&dircount64);
4484 97 : if (dircount64>4096)
4485 : {
4486 0 : TIFFErrorExt(tif->tif_clientdata, module,
4487 : "Sanity check on directory count failed, this is probably not a valid IFD offset");
4488 0 : return 0;
4489 : }
4490 97 : dircount16 = (uint16)dircount64;
4491 97 : dirsize = 20;
4492 : }
4493 6881 : origdir = _TIFFCheckMalloc(tif, dircount16,
4494 : dirsize, "to read TIFF directory");
4495 6881 : if (origdir == NULL)
4496 0 : return 0;
4497 6881 : if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
4498 0 : TIFFErrorExt(tif->tif_clientdata, module,
4499 : "%.100s: Can not read TIFF directory",
4500 : tif->tif_name);
4501 0 : _TIFFfree(origdir);
4502 0 : return 0;
4503 : }
4504 : /*
4505 : * Read offset to next directory for sequential scans if
4506 : * needed.
4507 : */
4508 6881 : if (nextdiroff)
4509 : {
4510 6881 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4511 : {
4512 : uint32 nextdiroff32;
4513 6784 : if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
4514 0 : nextdiroff32 = 0;
4515 6784 : if (tif->tif_flags&TIFF_SWAB)
4516 284 : TIFFSwabLong(&nextdiroff32);
4517 6784 : *nextdiroff=nextdiroff32;
4518 : } else {
4519 97 : if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
4520 0 : *nextdiroff = 0;
4521 97 : if (tif->tif_flags&TIFF_SWAB)
4522 12 : TIFFSwabLong8(nextdiroff);
4523 : }
4524 : }
4525 : } else {
4526 : tmsize_t m;
4527 0 : tmsize_t off = (tmsize_t) tif->tif_diroff;
4528 0 : if ((uint64)off!=tif->tif_diroff)
4529 : {
4530 0 : TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
4531 0 : return(0);
4532 : }
4533 :
4534 : /*
4535 : * Check for integer overflow when validating the dir_off,
4536 : * otherwise a very high offset may cause an OOB read and
4537 : * crash the client. Make two comparisons instead of
4538 : *
4539 : * off + sizeof(uint16) > tif->tif_size
4540 : *
4541 : * to avoid overflow.
4542 : */
4543 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4544 : {
4545 0 : m=off+sizeof(uint16);
4546 0 : if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
4547 0 : TIFFErrorExt(tif->tif_clientdata, module,
4548 : "Can not read TIFF directory count");
4549 0 : return 0;
4550 : } else {
4551 0 : _TIFFmemcpy(&dircount16, tif->tif_base + off,
4552 : sizeof(uint16));
4553 : }
4554 0 : off += sizeof (uint16);
4555 0 : if (tif->tif_flags & TIFF_SWAB)
4556 0 : TIFFSwabShort(&dircount16);
4557 0 : if (dircount16>4096)
4558 : {
4559 0 : TIFFErrorExt(tif->tif_clientdata, module,
4560 : "Sanity check on directory count failed, this is probably not a valid IFD offset");
4561 0 : return 0;
4562 : }
4563 0 : dirsize = 12;
4564 : }
4565 : else
4566 : {
4567 : tmsize_t m;
4568 : uint64 dircount64;
4569 0 : m=off+sizeof(uint64);
4570 0 : if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
4571 0 : TIFFErrorExt(tif->tif_clientdata, module,
4572 : "Can not read TIFF directory count");
4573 0 : return 0;
4574 : } else {
4575 0 : _TIFFmemcpy(&dircount64, tif->tif_base + off,
4576 : sizeof(uint64));
4577 : }
4578 0 : off += sizeof (uint64);
4579 0 : if (tif->tif_flags & TIFF_SWAB)
4580 0 : TIFFSwabLong8(&dircount64);
4581 0 : if (dircount64>4096)
4582 : {
4583 0 : TIFFErrorExt(tif->tif_clientdata, module,
4584 : "Sanity check on directory count failed, this is probably not a valid IFD offset");
4585 0 : return 0;
4586 : }
4587 0 : dircount16 = (uint16)dircount64;
4588 0 : dirsize = 20;
4589 : }
4590 0 : if (dircount16 == 0 )
4591 : {
4592 0 : TIFFErrorExt(tif->tif_clientdata, module,
4593 : "Sanity check on directory count failed, zero tag directories not supported");
4594 0 : return 0;
4595 : }
4596 0 : origdir = _TIFFCheckMalloc(tif, dircount16,
4597 : dirsize,
4598 : "to read TIFF directory");
4599 0 : if (origdir == NULL)
4600 0 : return 0;
4601 0 : m=off+dircount16*dirsize;
4602 0 : if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
4603 0 : TIFFErrorExt(tif->tif_clientdata, module,
4604 : "Can not read TIFF directory");
4605 0 : _TIFFfree(origdir);
4606 0 : return 0;
4607 : } else {
4608 0 : _TIFFmemcpy(origdir, tif->tif_base + off,
4609 0 : dircount16 * dirsize);
4610 : }
4611 0 : if (nextdiroff) {
4612 0 : off += dircount16 * dirsize;
4613 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4614 : {
4615 : uint32 nextdiroff32;
4616 0 : m=off+sizeof(uint32);
4617 0 : if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
4618 0 : nextdiroff32 = 0;
4619 : else
4620 0 : _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
4621 : sizeof (uint32));
4622 0 : if (tif->tif_flags&TIFF_SWAB)
4623 0 : TIFFSwabLong(&nextdiroff32);
4624 0 : *nextdiroff = nextdiroff32;
4625 : }
4626 : else
4627 : {
4628 0 : m=off+sizeof(uint64);
4629 0 : if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
4630 0 : *nextdiroff = 0;
4631 : else
4632 0 : _TIFFmemcpy(nextdiroff, tif->tif_base + off,
4633 : sizeof (uint64));
4634 0 : if (tif->tif_flags&TIFF_SWAB)
4635 0 : TIFFSwabLong8(nextdiroff);
4636 : }
4637 : }
4638 : }
4639 6881 : dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
4640 : sizeof(TIFFDirEntry),
4641 : "to read TIFF directory");
4642 6881 : if (dir==0)
4643 : {
4644 0 : _TIFFfree(origdir);
4645 0 : return 0;
4646 : }
4647 6881 : ma=(uint8*)origdir;
4648 6881 : mb=dir;
4649 105664 : for (n=0; n<dircount16; n++)
4650 : {
4651 98783 : if (tif->tif_flags&TIFF_SWAB)
4652 3969 : TIFFSwabShort((uint16*)ma);
4653 98783 : mb->tdir_tag=*(uint16*)ma;
4654 98783 : ma+=sizeof(uint16);
4655 98783 : if (tif->tif_flags&TIFF_SWAB)
4656 3969 : TIFFSwabShort((uint16*)ma);
4657 98783 : mb->tdir_type=*(uint16*)ma;
4658 98783 : ma+=sizeof(uint16);
4659 98783 : if (!(tif->tif_flags&TIFF_BIGTIFF))
4660 : {
4661 97595 : if (tif->tif_flags&TIFF_SWAB)
4662 3837 : TIFFSwabLong((uint32*)ma);
4663 97595 : mb->tdir_count=(uint64)(*(uint32*)ma);
4664 97595 : ma+=sizeof(uint32);
4665 97595 : *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
4666 97595 : ma+=sizeof(uint32);
4667 : }
4668 : else
4669 : {
4670 1188 : if (tif->tif_flags&TIFF_SWAB)
4671 132 : TIFFSwabLong8((uint64*)ma);
4672 1188 : mb->tdir_count=TIFFReadUInt64(ma);
4673 1188 : ma+=sizeof(uint64);
4674 1188 : mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
4675 1188 : ma+=sizeof(uint64);
4676 : }
4677 98783 : mb++;
4678 : }
4679 6881 : _TIFFfree(origdir);
4680 6881 : *pdir = dir;
4681 6881 : return dircount16;
4682 : }
4683 :
4684 : /*
4685 : * Fetch a tag that is not handled by special case code.
4686 : */
4687 : static int
4688 64319 : TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
4689 : {
4690 : static const char module[] = "TIFFFetchNormalTag";
4691 : enum TIFFReadDirEntryErr err;
4692 : uint32 fii;
4693 64319 : const TIFFField* fip = NULL;
4694 64319 : TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4695 64319 : if( fii == FAILED_FII )
4696 : {
4697 0 : TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
4698 : "No definition found for tag %d",
4699 0 : dp->tdir_tag);
4700 0 : return 0;
4701 : }
4702 64319 : fip=tif->tif_fields[fii];
4703 64319 : assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
4704 64319 : assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
4705 64319 : err=TIFFReadDirEntryErrOk;
4706 64319 : switch (fip->set_field_type)
4707 : {
4708 : case TIFF_SETGET_UNDEFINED:
4709 0 : break;
4710 : case TIFF_SETGET_ASCII:
4711 : {
4712 : uint8* data;
4713 4359 : assert(fip->field_passcount==0);
4714 4359 : err=TIFFReadDirEntryByteArray(tif,dp,&data);
4715 4359 : if (err==TIFFReadDirEntryErrOk)
4716 : {
4717 : uint8* ma;
4718 : uint32 mb;
4719 : int n;
4720 4358 : ma=data;
4721 4358 : mb=0;
4722 140039 : while (mb<(uint32)dp->tdir_count)
4723 : {
4724 135680 : if (*ma==0)
4725 4357 : break;
4726 131323 : ma++;
4727 131323 : mb++;
4728 : }
4729 4358 : if (mb+1<(uint32)dp->tdir_count)
4730 0 : TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip->field_name);
4731 4358 : else if (mb+1>(uint32)dp->tdir_count)
4732 : {
4733 : uint8* o;
4734 1 : TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
4735 1 : if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
4736 0 : o=NULL;
4737 : else
4738 1 : o=_TIFFmalloc((uint32)dp->tdir_count+1);
4739 1 : if (o==NULL)
4740 : {
4741 0 : if (data!=NULL)
4742 0 : _TIFFfree(data);
4743 0 : return(0);
4744 : }
4745 1 : _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
4746 1 : o[(uint32)dp->tdir_count]=0;
4747 1 : if (data!=0)
4748 1 : _TIFFfree(data);
4749 1 : data=o;
4750 : }
4751 4358 : n=TIFFSetField(tif,dp->tdir_tag,data);
4752 4358 : if (data!=0)
4753 4358 : _TIFFfree(data);
4754 4358 : if (!n)
4755 0 : return(0);
4756 : }
4757 : }
4758 4359 : break;
4759 : case TIFF_SETGET_UINT8:
4760 : {
4761 : uint8 data;
4762 0 : assert(fip->field_readcount==1);
4763 0 : assert(fip->field_passcount==0);
4764 0 : err=TIFFReadDirEntryByte(tif,dp,&data);
4765 0 : if (err==TIFFReadDirEntryErrOk)
4766 : {
4767 0 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4768 0 : return(0);
4769 : }
4770 : }
4771 0 : break;
4772 : case TIFF_SETGET_UINT16:
4773 : {
4774 : uint16 data;
4775 21182 : assert(fip->field_readcount==1);
4776 21182 : assert(fip->field_passcount==0);
4777 21182 : err=TIFFReadDirEntryShort(tif,dp,&data);
4778 21182 : if (err==TIFFReadDirEntryErrOk)
4779 : {
4780 21182 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4781 0 : return(0);
4782 : }
4783 : }
4784 21182 : break;
4785 : case TIFF_SETGET_UINT32:
4786 : {
4787 : uint32 data;
4788 24669 : assert(fip->field_readcount==1);
4789 24669 : assert(fip->field_passcount==0);
4790 24669 : err=TIFFReadDirEntryLong(tif,dp,&data);
4791 24669 : if (err==TIFFReadDirEntryErrOk)
4792 : {
4793 24669 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4794 0 : return(0);
4795 : }
4796 : }
4797 24669 : break;
4798 : case TIFF_SETGET_UINT64:
4799 : {
4800 : uint64 data;
4801 0 : assert(fip->field_readcount==1);
4802 0 : assert(fip->field_passcount==0);
4803 0 : err=TIFFReadDirEntryLong8(tif,dp,&data);
4804 0 : if (err==TIFFReadDirEntryErrOk)
4805 : {
4806 0 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4807 0 : return(0);
4808 : }
4809 : }
4810 0 : break;
4811 : case TIFF_SETGET_FLOAT:
4812 : {
4813 : float data;
4814 0 : assert(fip->field_readcount==1);
4815 0 : assert(fip->field_passcount==0);
4816 0 : err=TIFFReadDirEntryFloat(tif,dp,&data);
4817 0 : if (err==TIFFReadDirEntryErrOk)
4818 : {
4819 0 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4820 0 : return(0);
4821 : }
4822 : }
4823 0 : break;
4824 : case TIFF_SETGET_DOUBLE:
4825 : {
4826 : double data;
4827 92 : assert(fip->field_readcount==1);
4828 92 : assert(fip->field_passcount==0);
4829 92 : err=TIFFReadDirEntryDouble(tif,dp,&data);
4830 92 : if (err==TIFFReadDirEntryErrOk)
4831 : {
4832 92 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4833 0 : return(0);
4834 : }
4835 : }
4836 92 : break;
4837 : case TIFF_SETGET_IFD8:
4838 : {
4839 : uint64 data;
4840 1 : assert(fip->field_readcount==1);
4841 1 : assert(fip->field_passcount==0);
4842 1 : err=TIFFReadDirEntryIfd8(tif,dp,&data);
4843 1 : if (err==TIFFReadDirEntryErrOk)
4844 : {
4845 1 : if (!TIFFSetField(tif,dp->tdir_tag,data))
4846 0 : return(0);
4847 : }
4848 : }
4849 1 : break;
4850 : case TIFF_SETGET_UINT16_PAIR:
4851 : {
4852 : uint16* data;
4853 1 : assert(fip->field_readcount==2);
4854 1 : assert(fip->field_passcount==0);
4855 1 : if (dp->tdir_count!=2)
4856 0 : return(0);
4857 1 : err=TIFFReadDirEntryShortArray(tif,dp,&data);
4858 1 : if (err==TIFFReadDirEntryErrOk)
4859 : {
4860 : int m;
4861 1 : m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
4862 1 : _TIFFfree(data);
4863 1 : if (!m)
4864 0 : return(0);
4865 : }
4866 : }
4867 1 : break;
4868 : case TIFF_SETGET_C0_UINT8:
4869 : {
4870 : uint8* data;
4871 0 : assert(fip->field_readcount>=1);
4872 0 : assert(fip->field_passcount==0);
4873 0 : if (dp->tdir_count!=(uint64)fip->field_readcount)
4874 : /* corrupt file */;
4875 : else
4876 : {
4877 0 : err=TIFFReadDirEntryByteArray(tif,dp,&data);
4878 0 : if (err==TIFFReadDirEntryErrOk)
4879 : {
4880 : int m;
4881 0 : m=TIFFSetField(tif,dp->tdir_tag,data);
4882 0 : if (data!=0)
4883 0 : _TIFFfree(data);
4884 0 : if (!m)
4885 0 : return(0);
4886 : }
4887 : }
4888 : }
4889 0 : break;
4890 : case TIFF_SETGET_C0_UINT16:
4891 : {
4892 : uint16* data;
4893 0 : assert(fip->field_readcount>=1);
4894 0 : assert(fip->field_passcount==0);
4895 0 : if (dp->tdir_count!=(uint64)fip->field_readcount)
4896 : /* corrupt file */;
4897 : else
4898 : {
4899 0 : err=TIFFReadDirEntryShortArray(tif,dp,&data);
4900 0 : if (err==TIFFReadDirEntryErrOk)
4901 : {
4902 : int m;
4903 0 : m=TIFFSetField(tif,dp->tdir_tag,data);
4904 0 : if (data!=0)
4905 0 : _TIFFfree(data);
4906 0 : if (!m)
4907 0 : return(0);
4908 : }
4909 : }
4910 : }
4911 0 : break;
4912 : case TIFF_SETGET_C0_UINT32:
4913 : {
4914 : uint32* data;
4915 0 : assert(fip->field_readcount>=1);
4916 0 : assert(fip->field_passcount==0);
4917 0 : if (dp->tdir_count!=(uint64)fip->field_readcount)
4918 : /* corrupt file */;
4919 : else
4920 : {
4921 0 : err=TIFFReadDirEntryLongArray(tif,dp,&data);
4922 0 : if (err==TIFFReadDirEntryErrOk)
4923 : {
4924 : int m;
4925 0 : m=TIFFSetField(tif,dp->tdir_tag,data);
4926 0 : if (data!=0)
4927 0 : _TIFFfree(data);
4928 0 : if (!m)
4929 0 : return(0);
4930 : }
4931 : }
4932 : }
4933 0 : break;
4934 : case TIFF_SETGET_C0_FLOAT:
4935 : {
4936 : float* data;
4937 168 : assert(fip->field_readcount>=1);
4938 168 : assert(fip->field_passcount==0);
4939 168 : if (dp->tdir_count!=(uint64)fip->field_readcount)
4940 : /* corrupt file */;
4941 : else
4942 : {
4943 168 : err=TIFFReadDirEntryFloatArray(tif,dp,&data);
4944 168 : if (err==TIFFReadDirEntryErrOk)
4945 : {
4946 : int m;
4947 168 : m=TIFFSetField(tif,dp->tdir_tag,data);
4948 168 : if (data!=0)
4949 168 : _TIFFfree(data);
4950 168 : if (!m)
4951 0 : return(0);
4952 : }
4953 : }
4954 : }
4955 168 : break;
4956 : case TIFF_SETGET_C16_ASCII:
4957 : {
4958 : uint8* data;
4959 0 : assert(fip->field_readcount==TIFF_VARIABLE);
4960 0 : assert(fip->field_passcount==1);
4961 0 : if (dp->tdir_count>0xFFFF)
4962 0 : err=TIFFReadDirEntryErrCount;
4963 : else
4964 : {
4965 0 : err=TIFFReadDirEntryByteArray(tif,dp,&data);
4966 0 : if (err==TIFFReadDirEntryErrOk)
4967 : {
4968 : int m;
4969 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
4970 0 : if (data!=0)
4971 0 : _TIFFfree(data);
4972 0 : if (!m)
4973 0 : return(0);
4974 : }
4975 : }
4976 : }
4977 0 : break;
4978 : case TIFF_SETGET_C16_UINT8:
4979 : {
4980 : uint8* data;
4981 0 : assert(fip->field_readcount==TIFF_VARIABLE);
4982 0 : assert(fip->field_passcount==1);
4983 0 : if (dp->tdir_count>0xFFFF)
4984 0 : err=TIFFReadDirEntryErrCount;
4985 : else
4986 : {
4987 0 : err=TIFFReadDirEntryByteArray(tif,dp,&data);
4988 0 : if (err==TIFFReadDirEntryErrOk)
4989 : {
4990 : int m;
4991 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
4992 0 : if (data!=0)
4993 0 : _TIFFfree(data);
4994 0 : if (!m)
4995 0 : return(0);
4996 : }
4997 : }
4998 : }
4999 0 : break;
5000 : case TIFF_SETGET_C16_UINT16:
5001 : {
5002 : uint16* data;
5003 4189 : assert(fip->field_readcount==TIFF_VARIABLE);
5004 4189 : assert(fip->field_passcount==1);
5005 4189 : if (dp->tdir_count>0xFFFF)
5006 0 : err=TIFFReadDirEntryErrCount;
5007 : else
5008 : {
5009 4189 : err=TIFFReadDirEntryShortArray(tif,dp,&data);
5010 4189 : if (err==TIFFReadDirEntryErrOk)
5011 : {
5012 : int m;
5013 4189 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5014 4189 : if (data!=0)
5015 4189 : _TIFFfree(data);
5016 4189 : if (!m)
5017 0 : return(0);
5018 : }
5019 : }
5020 : }
5021 4189 : break;
5022 : case TIFF_SETGET_C16_UINT32:
5023 : {
5024 : uint32* data;
5025 0 : assert(fip->field_readcount==TIFF_VARIABLE);
5026 0 : assert(fip->field_passcount==1);
5027 0 : if (dp->tdir_count>0xFFFF)
5028 0 : err=TIFFReadDirEntryErrCount;
5029 : else
5030 : {
5031 0 : err=TIFFReadDirEntryLongArray(tif,dp,&data);
5032 0 : if (err==TIFFReadDirEntryErrOk)
5033 : {
5034 : int m;
5035 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5036 0 : if (data!=0)
5037 0 : _TIFFfree(data);
5038 0 : if (!m)
5039 0 : return(0);
5040 : }
5041 : }
5042 : }
5043 0 : break;
5044 : case TIFF_SETGET_C16_UINT64:
5045 : {
5046 : uint64* data;
5047 0 : assert(fip->field_readcount==TIFF_VARIABLE);
5048 0 : assert(fip->field_passcount==1);
5049 0 : if (dp->tdir_count>0xFFFF)
5050 0 : err=TIFFReadDirEntryErrCount;
5051 : else
5052 : {
5053 0 : err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5054 0 : if (err==TIFFReadDirEntryErrOk)
5055 : {
5056 : int m;
5057 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5058 0 : if (data!=0)
5059 0 : _TIFFfree(data);
5060 0 : if (!m)
5061 0 : return(0);
5062 : }
5063 : }
5064 : }
5065 0 : break;
5066 : case TIFF_SETGET_C16_FLOAT:
5067 : {
5068 : float* data;
5069 0 : assert(fip->field_readcount==TIFF_VARIABLE);
5070 0 : assert(fip->field_passcount==1);
5071 0 : if (dp->tdir_count>0xFFFF)
5072 0 : err=TIFFReadDirEntryErrCount;
5073 : else
5074 : {
5075 0 : err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5076 0 : if (err==TIFFReadDirEntryErrOk)
5077 : {
5078 : int m;
5079 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5080 0 : if (data!=0)
5081 0 : _TIFFfree(data);
5082 0 : if (!m)
5083 0 : return(0);
5084 : }
5085 : }
5086 : }
5087 0 : break;
5088 : case TIFF_SETGET_C16_DOUBLE:
5089 : {
5090 : double* data;
5091 9427 : assert(fip->field_readcount==TIFF_VARIABLE);
5092 9427 : assert(fip->field_passcount==1);
5093 9427 : if (dp->tdir_count>0xFFFF)
5094 0 : err=TIFFReadDirEntryErrCount;
5095 : else
5096 : {
5097 9427 : err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5098 9427 : if (err==TIFFReadDirEntryErrOk)
5099 : {
5100 : int m;
5101 9427 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5102 9427 : if (data!=0)
5103 9427 : _TIFFfree(data);
5104 9427 : if (!m)
5105 0 : return(0);
5106 : }
5107 : }
5108 : }
5109 9427 : break;
5110 : case TIFF_SETGET_C16_IFD8:
5111 : {
5112 : uint64* data;
5113 0 : assert(fip->field_readcount==TIFF_VARIABLE);
5114 0 : assert(fip->field_passcount==1);
5115 0 : if (dp->tdir_count>0xFFFF)
5116 0 : err=TIFFReadDirEntryErrCount;
5117 : else
5118 : {
5119 0 : err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5120 0 : if (err==TIFFReadDirEntryErrOk)
5121 : {
5122 : int m;
5123 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5124 0 : if (data!=0)
5125 0 : _TIFFfree(data);
5126 0 : if (!m)
5127 0 : return(0);
5128 : }
5129 : }
5130 : }
5131 0 : break;
5132 : case TIFF_SETGET_C32_ASCII:
5133 : {
5134 : uint8* data;
5135 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5136 0 : assert(fip->field_passcount==1);
5137 0 : err=TIFFReadDirEntryByteArray(tif,dp,&data);
5138 0 : if (err==TIFFReadDirEntryErrOk)
5139 : {
5140 : int m;
5141 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5142 0 : if (data!=0)
5143 0 : _TIFFfree(data);
5144 0 : if (!m)
5145 0 : return(0);
5146 : }
5147 : }
5148 0 : break;
5149 : case TIFF_SETGET_C32_UINT8:
5150 : {
5151 : uint8* data;
5152 227 : assert(fip->field_readcount==TIFF_VARIABLE2);
5153 227 : assert(fip->field_passcount==1);
5154 227 : err=TIFFReadDirEntryByteArray(tif,dp,&data);
5155 227 : if (err==TIFFReadDirEntryErrOk)
5156 : {
5157 : int m;
5158 227 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5159 227 : if (data!=0)
5160 227 : _TIFFfree(data);
5161 227 : if (!m)
5162 0 : return(0);
5163 : }
5164 : }
5165 227 : break;
5166 : case TIFF_SETGET_C32_SINT8:
5167 : {
5168 0 : int8* data = NULL;
5169 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5170 0 : assert(fip->field_passcount==1);
5171 0 : err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
5172 0 : if (err==TIFFReadDirEntryErrOk)
5173 : {
5174 : int m;
5175 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5176 0 : if (data!=0)
5177 0 : _TIFFfree(data);
5178 0 : if (!m)
5179 0 : return(0);
5180 : }
5181 : }
5182 0 : break;
5183 : case TIFF_SETGET_C32_UINT16:
5184 : {
5185 : uint16* data;
5186 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5187 0 : assert(fip->field_passcount==1);
5188 0 : err=TIFFReadDirEntryShortArray(tif,dp,&data);
5189 0 : if (err==TIFFReadDirEntryErrOk)
5190 : {
5191 : int m;
5192 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5193 0 : if (data!=0)
5194 0 : _TIFFfree(data);
5195 0 : if (!m)
5196 0 : return(0);
5197 : }
5198 : }
5199 0 : break;
5200 : case TIFF_SETGET_C32_SINT16:
5201 : {
5202 0 : int16* data = NULL;
5203 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5204 0 : assert(fip->field_passcount==1);
5205 0 : err=TIFFReadDirEntrySshortArray(tif,dp,&data);
5206 0 : if (err==TIFFReadDirEntryErrOk)
5207 : {
5208 : int m;
5209 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5210 0 : if (data!=0)
5211 0 : _TIFFfree(data);
5212 0 : if (!m)
5213 0 : return(0);
5214 : }
5215 : }
5216 0 : break;
5217 : case TIFF_SETGET_C32_UINT32:
5218 : {
5219 : uint32* data;
5220 1 : assert(fip->field_readcount==TIFF_VARIABLE2);
5221 1 : assert(fip->field_passcount==1);
5222 1 : err=TIFFReadDirEntryLongArray(tif,dp,&data);
5223 1 : if (err==TIFFReadDirEntryErrOk)
5224 : {
5225 : int m;
5226 1 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5227 1 : if (data!=0)
5228 1 : _TIFFfree(data);
5229 1 : if (!m)
5230 0 : return(0);
5231 : }
5232 : }
5233 1 : break;
5234 : case TIFF_SETGET_C32_SINT32:
5235 : {
5236 0 : int32* data = NULL;
5237 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5238 0 : assert(fip->field_passcount==1);
5239 0 : err=TIFFReadDirEntrySlongArray(tif,dp,&data);
5240 0 : if (err==TIFFReadDirEntryErrOk)
5241 : {
5242 : int m;
5243 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5244 0 : if (data!=0)
5245 0 : _TIFFfree(data);
5246 0 : if (!m)
5247 0 : return(0);
5248 : }
5249 : }
5250 0 : break;
5251 : case TIFF_SETGET_C32_UINT64:
5252 : {
5253 : uint64* data;
5254 3 : assert(fip->field_readcount==TIFF_VARIABLE2);
5255 3 : assert(fip->field_passcount==1);
5256 3 : err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5257 3 : if (err==TIFFReadDirEntryErrOk)
5258 : {
5259 : int m;
5260 3 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5261 3 : if (data!=0)
5262 3 : _TIFFfree(data);
5263 3 : if (!m)
5264 0 : return(0);
5265 : }
5266 : }
5267 3 : break;
5268 : case TIFF_SETGET_C32_SINT64:
5269 : {
5270 0 : int64* data = NULL;
5271 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5272 0 : assert(fip->field_passcount==1);
5273 0 : err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
5274 0 : if (err==TIFFReadDirEntryErrOk)
5275 : {
5276 : int m;
5277 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5278 0 : if (data!=0)
5279 0 : _TIFFfree(data);
5280 0 : if (!m)
5281 0 : return(0);
5282 : }
5283 : }
5284 0 : break;
5285 : case TIFF_SETGET_C32_FLOAT:
5286 : {
5287 : float* data;
5288 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5289 0 : assert(fip->field_passcount==1);
5290 0 : err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5291 0 : if (err==TIFFReadDirEntryErrOk)
5292 : {
5293 : int m;
5294 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5295 0 : if (data!=0)
5296 0 : _TIFFfree(data);
5297 0 : if (!m)
5298 0 : return(0);
5299 : }
5300 : }
5301 0 : break;
5302 : case TIFF_SETGET_C32_DOUBLE:
5303 : {
5304 : double* data;
5305 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5306 0 : assert(fip->field_passcount==1);
5307 0 : err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5308 0 : if (err==TIFFReadDirEntryErrOk)
5309 : {
5310 : int m;
5311 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5312 0 : if (data!=0)
5313 0 : _TIFFfree(data);
5314 0 : if (!m)
5315 0 : return(0);
5316 : }
5317 : }
5318 0 : break;
5319 : case TIFF_SETGET_C32_IFD8:
5320 : {
5321 : uint64* data;
5322 0 : assert(fip->field_readcount==TIFF_VARIABLE2);
5323 0 : assert(fip->field_passcount==1);
5324 0 : err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5325 0 : if (err==TIFFReadDirEntryErrOk)
5326 : {
5327 : int m;
5328 0 : m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5329 0 : if (data!=0)
5330 0 : _TIFFfree(data);
5331 0 : if (!m)
5332 0 : return(0);
5333 : }
5334 : }
5335 0 : break;
5336 : default:
5337 0 : assert(0); /* we should never get here */
5338 : break;
5339 : }
5340 64319 : if (err!=TIFFReadDirEntryErrOk)
5341 : {
5342 1 : TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",recover);
5343 1 : return(0);
5344 : }
5345 64318 : return(1);
5346 : }
5347 :
5348 : /*
5349 : * Fetch a set of offsets or lengths.
5350 : * While this routine says "strips", in fact it's also used for tiles.
5351 : */
5352 : static int
5353 12576 : TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
5354 : {
5355 : static const char module[] = "TIFFFetchStripThing";
5356 : enum TIFFReadDirEntryErr err;
5357 : uint64* data;
5358 12576 : err=TIFFReadDirEntryLong8Array(tif,dir,&data);
5359 12576 : if (err!=TIFFReadDirEntryErrOk)
5360 : {
5361 0 : const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5362 0 : TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
5363 0 : return(0);
5364 : }
5365 12576 : if (dir->tdir_count!=(uint64)nstrips)
5366 : {
5367 : uint64* resizeddata;
5368 0 : resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
5369 0 : if (resizeddata==0) {
5370 0 : _TIFFfree(data);
5371 0 : return(0);
5372 : }
5373 0 : if (dir->tdir_count<(uint64)nstrips)
5374 : {
5375 0 : _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
5376 0 : _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
5377 : }
5378 : else
5379 0 : _TIFFmemcpy(resizeddata,data,nstrips*sizeof(uint64));
5380 0 : _TIFFfree(data);
5381 0 : data=resizeddata;
5382 : }
5383 12576 : *lpp=data;
5384 12576 : return(1);
5385 : }
5386 :
5387 : /*
5388 : * Fetch and set the SubjectDistance EXIF tag.
5389 : */
5390 : static int
5391 0 : TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
5392 : {
5393 : static const char module[] = "TIFFFetchSubjectDistance";
5394 : enum TIFFReadDirEntryErr err;
5395 : UInt64Aligned_t m;
5396 0 : m.l=0;
5397 : assert(sizeof(double)==8);
5398 : assert(sizeof(uint64)==8);
5399 : assert(sizeof(uint32)==4);
5400 0 : if (dir->tdir_count!=1)
5401 0 : err=TIFFReadDirEntryErrCount;
5402 0 : else if (dir->tdir_type!=TIFF_RATIONAL)
5403 0 : err=TIFFReadDirEntryErrType;
5404 : else
5405 : {
5406 0 : if (!(tif->tif_flags&TIFF_BIGTIFF))
5407 : {
5408 : uint32 offset;
5409 0 : offset=*(uint32*)(&dir->tdir_offset);
5410 0 : if (tif->tif_flags&TIFF_SWAB)
5411 0 : TIFFSwabLong(&offset);
5412 0 : err=TIFFReadDirEntryData(tif,offset,8,m.i);
5413 : }
5414 : else
5415 : {
5416 0 : m.l=dir->tdir_offset.toff_long8;
5417 0 : err=TIFFReadDirEntryErrOk;
5418 : }
5419 : }
5420 0 : if (err==TIFFReadDirEntryErrOk)
5421 : {
5422 : double n;
5423 0 : if (tif->tif_flags&TIFF_SWAB)
5424 0 : TIFFSwabArrayOfLong(m.i,2);
5425 0 : if (m.i[0]==0)
5426 0 : n=0.0;
5427 0 : else if (m.i[0]==0xFFFFFFFF)
5428 : /*
5429 : * XXX: Numerator 0xFFFFFFFF means that we have infinite
5430 : * distance. Indicate that with a negative floating point
5431 : * SubjectDistance value.
5432 : */
5433 0 : n=-1.0;
5434 : else
5435 0 : n=(double)m.i[0]/(double)m.i[1];
5436 0 : return(TIFFSetField(tif,dir->tdir_tag,n));
5437 : }
5438 : else
5439 : {
5440 0 : TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
5441 0 : return(0);
5442 : }
5443 : }
5444 :
5445 : /*
5446 : * Replace a single strip (tile) of uncompressed data by multiple strips
5447 : * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5448 : * dealing with large images or for dealing with machines with a limited
5449 : * amount memory.
5450 : */
5451 : static void
5452 3124 : ChopUpSingleUncompressedStrip(TIFF* tif)
5453 : {
5454 3124 : register TIFFDirectory *td = &tif->tif_dir;
5455 : uint64 bytecount;
5456 : uint64 offset;
5457 : uint32 rowblock;
5458 : uint64 rowblockbytes;
5459 : uint64 stripbytes;
5460 : uint32 strip;
5461 : uint64 nstrips64;
5462 : uint32 nstrips32;
5463 : uint32 rowsperstrip;
5464 : uint64* newcounts;
5465 : uint64* newoffsets;
5466 :
5467 3124 : bytecount = td->td_stripbytecount[0];
5468 3124 : offset = td->td_stripoffset[0];
5469 3124 : assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
5470 3124 : if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
5471 0 : (!isUpSampled(tif)))
5472 0 : rowblock = td->td_ycbcrsubsampling[1];
5473 : else
5474 3124 : rowblock = 1;
5475 3124 : rowblockbytes = TIFFVTileSize64(tif, rowblock);
5476 : /*
5477 : * Make the rows hold at least one scanline, but fill specified amount
5478 : * of data if possible.
5479 : */
5480 3124 : if (rowblockbytes > STRIP_SIZE_DEFAULT) {
5481 2 : stripbytes = rowblockbytes;
5482 2 : rowsperstrip = rowblock;
5483 3122 : } else if (rowblockbytes > 0 ) {
5484 : uint32 rowblocksperstrip;
5485 3122 : rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
5486 3122 : rowsperstrip = rowblocksperstrip * rowblock;
5487 3122 : stripbytes = rowblocksperstrip * rowblockbytes;
5488 : }
5489 : else
5490 0 : return;
5491 :
5492 : /*
5493 : * never increase the number of strips in an image
5494 : */
5495 3124 : if (rowsperstrip >= td->td_rowsperstrip)
5496 3082 : return;
5497 42 : nstrips64 = TIFFhowmany_64(bytecount, stripbytes);
5498 42 : if ((nstrips64==0)||(nstrips64>0xFFFFFFFF)) /* something is wonky, do nothing. */
5499 25 : return;
5500 17 : nstrips32 = (uint32)nstrips64;
5501 :
5502 17 : newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
5503 : "for chopped \"StripByteCounts\" array");
5504 17 : newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
5505 : "for chopped \"StripOffsets\" array");
5506 17 : if (newcounts == NULL || newoffsets == NULL) {
5507 : /*
5508 : * Unable to allocate new strip information, give up and use
5509 : * the original one strip information.
5510 : */
5511 0 : if (newcounts != NULL)
5512 0 : _TIFFfree(newcounts);
5513 0 : if (newoffsets != NULL)
5514 0 : _TIFFfree(newoffsets);
5515 0 : return;
5516 : }
5517 : /*
5518 : * Fill the strip information arrays with new bytecounts and offsets
5519 : * that reflect the broken-up format.
5520 : */
5521 441 : for (strip = 0; strip < nstrips32; strip++) {
5522 424 : if (stripbytes > bytecount)
5523 4 : stripbytes = bytecount;
5524 424 : newcounts[strip] = stripbytes;
5525 424 : newoffsets[strip] = offset;
5526 424 : offset += stripbytes;
5527 424 : bytecount -= stripbytes;
5528 : }
5529 : /*
5530 : * Replace old single strip info with multi-strip info.
5531 : */
5532 17 : td->td_stripsperimage = td->td_nstrips = nstrips32;
5533 17 : TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
5534 :
5535 17 : _TIFFfree(td->td_stripbytecount);
5536 17 : _TIFFfree(td->td_stripoffset);
5537 17 : td->td_stripbytecount = newcounts;
5538 17 : td->td_stripoffset = newoffsets;
5539 17 : td->td_stripbytecountsorted = 1;
5540 : }
5541 :
5542 304319 : int _TIFFFillStriles( TIFF *tif )
5543 : {
5544 : #if defined(DEFER_STRILE_LOAD)
5545 304319 : register TIFFDirectory *td = &tif->tif_dir;
5546 304319 : int return_value = 1;
5547 :
5548 304319 : if( td->td_stripoffset != NULL )
5549 296604 : return 1;
5550 :
5551 7715 : if( td->td_stripoffset_entry.tdir_count == 0 )
5552 1427 : return 0;
5553 :
5554 6288 : if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
5555 : td->td_nstrips,&td->td_stripoffset))
5556 : {
5557 0 : return_value = 0;
5558 : }
5559 :
5560 6288 : if (!TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
5561 : td->td_nstrips,&td->td_stripbytecount))
5562 : {
5563 0 : return_value = 0;
5564 : }
5565 :
5566 6288 : _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
5567 6288 : _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
5568 :
5569 6288 : if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
5570 : uint32 strip;
5571 :
5572 1003 : tif->tif_dir.td_stripbytecountsorted = 1;
5573 327119 : for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
5574 652432 : if (tif->tif_dir.td_stripoffset[strip - 1] >
5575 326216 : tif->tif_dir.td_stripoffset[strip]) {
5576 100 : tif->tif_dir.td_stripbytecountsorted = 0;
5577 100 : break;
5578 : }
5579 : }
5580 : }
5581 :
5582 6288 : return return_value;
5583 : #else /* !defined(DEFER_STRILE_LOAD) */
5584 : (void) tif;
5585 : return 1;
5586 : #endif
5587 : }
5588 :
5589 :
5590 : /* vim: set ts=8 sts=8 sw=8 noet: */
5591 : /*
5592 : * Local Variables:
5593 : * mode: c
5594 : * c-basic-offset: 8
5595 : * fill-column: 78
5596 : * End:
5597 : */
|