d2d1/effect: Create effect instance properties from builtin effect description.
[wine.git] / libs / tiff / libtiff / tif_dirread.c
blobd84147a0e8260fe78e2897a4efc08423bac12098
1 /*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
26 * TIFF Library.
28 * Directory Read Support Routines.
31 /* Suggested pending improvements:
32 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33 * the pointer to the appropriate TIFFField structure early on in
34 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
37 #include "tiffconf.h"
38 #include "tiffiop.h"
39 #include <float.h>
40 #include <stdlib.h>
42 #define FAILED_FII ((uint32_t) -1)
44 #ifdef HAVE_IEEEFP
45 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
46 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
47 #else
48 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32_t, float*);
49 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32_t, double*);
50 #endif
52 enum TIFFReadDirEntryErr {
53 TIFFReadDirEntryErrOk = 0,
54 TIFFReadDirEntryErrCount = 1,
55 TIFFReadDirEntryErrType = 2,
56 TIFFReadDirEntryErrIo = 3,
57 TIFFReadDirEntryErrRange = 4,
58 TIFFReadDirEntryErrPsdif = 5,
59 TIFFReadDirEntryErrSizesan = 6,
60 TIFFReadDirEntryErrAlloc = 7,
63 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8_t* value);
64 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16_t* value);
65 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32_t* value);
66 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64_t* value);
67 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
68 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
69 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64_t* value);
71 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32_t* count, uint32_t desttypesize, void** value);
72 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8_t** value);
73 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8_t** value);
74 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16_t** value);
75 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16_t** value);
76 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32_t** value);
77 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32_t** value);
78 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64_t** value);
79 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64_t** value);
80 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
81 static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
82 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64_t** value);
84 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16_t* value);
85 #if 0
86 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
87 #endif
89 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8_t* value);
90 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8_t* value);
91 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16_t* value);
92 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16_t* value);
93 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32_t* value);
94 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32_t* value);
95 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64_t* value);
96 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64_t* value);
97 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
98 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
99 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
100 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
102 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
103 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16_t value);
104 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16_t value);
105 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32_t value);
106 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32_t value);
107 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64_t value);
108 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64_t value);
110 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value);
111 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value);
112 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value);
113 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value);
114 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value);
115 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value);
116 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value);
118 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8_t value);
119 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16_t value);
120 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32_t value);
121 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32_t value);
122 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64_t value);
123 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64_t value);
125 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16_t value);
126 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32_t value);
127 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32_t value);
128 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value);
129 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value);
131 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8_t value);
132 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16_t value);
133 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32_t value);
134 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64_t value);
135 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64_t value);
137 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32_t value);
138 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value);
139 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value);
141 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value);
142 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value);
143 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32_t value);
144 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value);
146 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value);
148 static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64_t offset, tmsize_t size, void* dest);
149 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
151 static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16_t dircount);
152 static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16_t dircount, uint16_t tagid);
153 static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16_t tagid, uint32_t* fii);
155 static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16_t dircount);
156 static void MissingRequired(TIFF*, const char*);
157 static int TIFFCheckDirOffset(TIFF* tif, uint64_t diroff);
158 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32_t);
159 static uint16_t TIFFFetchDirectory(TIFF* tif, uint64_t diroff, TIFFDirEntry** pdir, uint64_t* nextdiroff);
160 static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
161 static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32_t nstrips, uint64_t** lpp);
162 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
163 static void ChopUpSingleUncompressedStrip(TIFF*);
164 static void TryChopUpUncompressedBigTiff(TIFF*);
165 static uint64_t TIFFReadUInt64(const uint8_t *value);
166 static int _TIFFGetMaxColorChannels(uint16_t photometric);
168 static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
170 typedef union _UInt64Aligned_t
172 double d;
173 uint64_t l;
174 uint32_t i[2];
175 uint16_t s[4];
176 uint8_t c[8];
177 } UInt64Aligned_t;
180 Unaligned safe copy of a uint64_t value from an octet array.
182 static uint64_t TIFFReadUInt64(const uint8_t *value)
184 UInt64Aligned_t result;
186 result.c[0]=value[0];
187 result.c[1]=value[1];
188 result.c[2]=value[2];
189 result.c[3]=value[3];
190 result.c[4]=value[4];
191 result.c[5]=value[5];
192 result.c[6]=value[6];
193 result.c[7]=value[7];
195 return result.l;
198 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8_t* value)
200 enum TIFFReadDirEntryErr err;
201 if (direntry->tdir_count!=1)
202 return(TIFFReadDirEntryErrCount);
203 switch (direntry->tdir_type)
205 case TIFF_BYTE:
206 case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with field_readcount==1 */
207 TIFFReadDirEntryCheckedByte(tif,direntry,value);
208 return(TIFFReadDirEntryErrOk);
209 case TIFF_SBYTE:
211 int8_t m;
212 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
213 err=TIFFReadDirEntryCheckRangeByteSbyte(m);
214 if (err!=TIFFReadDirEntryErrOk)
215 return(err);
216 *value=(uint8_t)m;
217 return(TIFFReadDirEntryErrOk);
219 case TIFF_SHORT:
221 uint16_t m;
222 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
223 err=TIFFReadDirEntryCheckRangeByteShort(m);
224 if (err!=TIFFReadDirEntryErrOk)
225 return(err);
226 *value=(uint8_t)m;
227 return(TIFFReadDirEntryErrOk);
229 case TIFF_SSHORT:
231 int16_t m;
232 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
233 err=TIFFReadDirEntryCheckRangeByteSshort(m);
234 if (err!=TIFFReadDirEntryErrOk)
235 return(err);
236 *value=(uint8_t)m;
237 return(TIFFReadDirEntryErrOk);
239 case TIFF_LONG:
241 uint32_t m;
242 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
243 err=TIFFReadDirEntryCheckRangeByteLong(m);
244 if (err!=TIFFReadDirEntryErrOk)
245 return(err);
246 *value=(uint8_t)m;
247 return(TIFFReadDirEntryErrOk);
249 case TIFF_SLONG:
251 int32_t m;
252 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
253 err=TIFFReadDirEntryCheckRangeByteSlong(m);
254 if (err!=TIFFReadDirEntryErrOk)
255 return(err);
256 *value=(uint8_t)m;
257 return(TIFFReadDirEntryErrOk);
259 case TIFF_LONG8:
261 uint64_t m;
262 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
263 if (err!=TIFFReadDirEntryErrOk)
264 return(err);
265 err=TIFFReadDirEntryCheckRangeByteLong8(m);
266 if (err!=TIFFReadDirEntryErrOk)
267 return(err);
268 *value=(uint8_t)m;
269 return(TIFFReadDirEntryErrOk);
271 case TIFF_SLONG8:
273 int64_t m;
274 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
275 if (err!=TIFFReadDirEntryErrOk)
276 return(err);
277 err=TIFFReadDirEntryCheckRangeByteSlong8(m);
278 if (err!=TIFFReadDirEntryErrOk)
279 return(err);
280 *value=(uint8_t)m;
281 return(TIFFReadDirEntryErrOk);
283 default:
284 return(TIFFReadDirEntryErrType);
288 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16_t* value)
290 enum TIFFReadDirEntryErr err;
291 if (direntry->tdir_count!=1)
292 return(TIFFReadDirEntryErrCount);
293 switch (direntry->tdir_type)
295 case TIFF_BYTE:
297 uint8_t m;
298 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
299 *value=(uint16_t)m;
300 return(TIFFReadDirEntryErrOk);
302 case TIFF_SBYTE:
304 int8_t m;
305 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
306 err=TIFFReadDirEntryCheckRangeShortSbyte(m);
307 if (err!=TIFFReadDirEntryErrOk)
308 return(err);
309 *value=(uint16_t)m;
310 return(TIFFReadDirEntryErrOk);
312 case TIFF_SHORT:
313 TIFFReadDirEntryCheckedShort(tif,direntry,value);
314 return(TIFFReadDirEntryErrOk);
315 case TIFF_SSHORT:
317 int16_t m;
318 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
319 err=TIFFReadDirEntryCheckRangeShortSshort(m);
320 if (err!=TIFFReadDirEntryErrOk)
321 return(err);
322 *value=(uint16_t)m;
323 return(TIFFReadDirEntryErrOk);
325 case TIFF_LONG:
327 uint32_t m;
328 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
329 err=TIFFReadDirEntryCheckRangeShortLong(m);
330 if (err!=TIFFReadDirEntryErrOk)
331 return(err);
332 *value=(uint16_t)m;
333 return(TIFFReadDirEntryErrOk);
335 case TIFF_SLONG:
337 int32_t m;
338 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
339 err=TIFFReadDirEntryCheckRangeShortSlong(m);
340 if (err!=TIFFReadDirEntryErrOk)
341 return(err);
342 *value=(uint16_t)m;
343 return(TIFFReadDirEntryErrOk);
345 case TIFF_LONG8:
347 uint64_t m;
348 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
349 if (err!=TIFFReadDirEntryErrOk)
350 return(err);
351 err=TIFFReadDirEntryCheckRangeShortLong8(m);
352 if (err!=TIFFReadDirEntryErrOk)
353 return(err);
354 *value=(uint16_t)m;
355 return(TIFFReadDirEntryErrOk);
357 case TIFF_SLONG8:
359 int64_t m;
360 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
361 if (err!=TIFFReadDirEntryErrOk)
362 return(err);
363 err=TIFFReadDirEntryCheckRangeShortSlong8(m);
364 if (err!=TIFFReadDirEntryErrOk)
365 return(err);
366 *value=(uint16_t)m;
367 return(TIFFReadDirEntryErrOk);
369 default:
370 return(TIFFReadDirEntryErrType);
374 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32_t* value)
376 enum TIFFReadDirEntryErr err;
377 if (direntry->tdir_count!=1)
378 return(TIFFReadDirEntryErrCount);
379 switch (direntry->tdir_type)
381 case TIFF_BYTE:
383 uint8_t m;
384 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
385 *value=(uint32_t)m;
386 return(TIFFReadDirEntryErrOk);
388 case TIFF_SBYTE:
390 int8_t m;
391 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
392 err=TIFFReadDirEntryCheckRangeLongSbyte(m);
393 if (err!=TIFFReadDirEntryErrOk)
394 return(err);
395 *value=(uint32_t)m;
396 return(TIFFReadDirEntryErrOk);
398 case TIFF_SHORT:
400 uint16_t m;
401 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
402 *value=(uint32_t)m;
403 return(TIFFReadDirEntryErrOk);
405 case TIFF_SSHORT:
407 int16_t m;
408 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
409 err=TIFFReadDirEntryCheckRangeLongSshort(m);
410 if (err!=TIFFReadDirEntryErrOk)
411 return(err);
412 *value=(uint32_t)m;
413 return(TIFFReadDirEntryErrOk);
415 case TIFF_LONG:
416 TIFFReadDirEntryCheckedLong(tif,direntry,value);
417 return(TIFFReadDirEntryErrOk);
418 case TIFF_SLONG:
420 int32_t m;
421 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
422 err=TIFFReadDirEntryCheckRangeLongSlong(m);
423 if (err!=TIFFReadDirEntryErrOk)
424 return(err);
425 *value=(uint32_t)m;
426 return(TIFFReadDirEntryErrOk);
428 case TIFF_LONG8:
430 uint64_t m;
431 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
432 if (err!=TIFFReadDirEntryErrOk)
433 return(err);
434 err=TIFFReadDirEntryCheckRangeLongLong8(m);
435 if (err!=TIFFReadDirEntryErrOk)
436 return(err);
437 *value=(uint32_t)m;
438 return(TIFFReadDirEntryErrOk);
440 case TIFF_SLONG8:
442 int64_t m;
443 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
444 if (err!=TIFFReadDirEntryErrOk)
445 return(err);
446 err=TIFFReadDirEntryCheckRangeLongSlong8(m);
447 if (err!=TIFFReadDirEntryErrOk)
448 return(err);
449 *value=(uint32_t)m;
450 return(TIFFReadDirEntryErrOk);
452 default:
453 return(TIFFReadDirEntryErrType);
457 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64_t* value)
459 enum TIFFReadDirEntryErr err;
460 if (direntry->tdir_count!=1)
461 return(TIFFReadDirEntryErrCount);
462 switch (direntry->tdir_type)
464 case TIFF_BYTE:
466 uint8_t m;
467 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
468 *value=(uint64_t)m;
469 return(TIFFReadDirEntryErrOk);
471 case TIFF_SBYTE:
473 int8_t m;
474 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
475 err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
476 if (err!=TIFFReadDirEntryErrOk)
477 return(err);
478 *value=(uint64_t)m;
479 return(TIFFReadDirEntryErrOk);
481 case TIFF_SHORT:
483 uint16_t m;
484 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
485 *value=(uint64_t)m;
486 return(TIFFReadDirEntryErrOk);
488 case TIFF_SSHORT:
490 int16_t m;
491 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
492 err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
493 if (err!=TIFFReadDirEntryErrOk)
494 return(err);
495 *value=(uint64_t)m;
496 return(TIFFReadDirEntryErrOk);
498 case TIFF_LONG:
500 uint32_t m;
501 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
502 *value=(uint64_t)m;
503 return(TIFFReadDirEntryErrOk);
505 case TIFF_SLONG:
507 int32_t m;
508 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
509 err=TIFFReadDirEntryCheckRangeLong8Slong(m);
510 if (err!=TIFFReadDirEntryErrOk)
511 return(err);
512 *value=(uint64_t)m;
513 return(TIFFReadDirEntryErrOk);
515 case TIFF_LONG8:
516 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
517 return(err);
518 case TIFF_SLONG8:
520 int64_t m;
521 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
522 if (err!=TIFFReadDirEntryErrOk)
523 return(err);
524 err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
525 if (err!=TIFFReadDirEntryErrOk)
526 return(err);
527 *value=(uint64_t)m;
528 return(TIFFReadDirEntryErrOk);
530 default:
531 return(TIFFReadDirEntryErrType);
535 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
537 enum TIFFReadDirEntryErr err;
538 if (direntry->tdir_count!=1)
539 return(TIFFReadDirEntryErrCount);
540 switch (direntry->tdir_type)
542 case TIFF_BYTE:
544 uint8_t m;
545 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
546 *value=(float)m;
547 return(TIFFReadDirEntryErrOk);
549 case TIFF_SBYTE:
551 int8_t m;
552 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
553 *value=(float)m;
554 return(TIFFReadDirEntryErrOk);
556 case TIFF_SHORT:
558 uint16_t m;
559 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
560 *value=(float)m;
561 return(TIFFReadDirEntryErrOk);
563 case TIFF_SSHORT:
565 int16_t m;
566 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
567 *value=(float)m;
568 return(TIFFReadDirEntryErrOk);
570 case TIFF_LONG:
572 uint32_t m;
573 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
574 *value=(float)m;
575 return(TIFFReadDirEntryErrOk);
577 case TIFF_SLONG:
579 int32_t m;
580 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
581 *value=(float)m;
582 return(TIFFReadDirEntryErrOk);
584 case TIFF_LONG8:
586 uint64_t m;
587 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
588 if (err!=TIFFReadDirEntryErrOk)
589 return(err);
590 #if defined(__WIN32__) && (_MSC_VER < 1500)
592 * XXX: MSVC 6.0 does not support conversion
593 * of 64-bit integers into floating point
594 * values.
596 *value = _TIFFUInt64ToFloat(m);
597 #else
598 *value=(float)m;
599 #endif
600 return(TIFFReadDirEntryErrOk);
602 case TIFF_SLONG8:
604 int64_t m;
605 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
606 if (err!=TIFFReadDirEntryErrOk)
607 return(err);
608 *value=(float)m;
609 return(TIFFReadDirEntryErrOk);
611 case TIFF_RATIONAL:
613 double m;
614 err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
615 if (err!=TIFFReadDirEntryErrOk)
616 return(err);
617 *value=(float)m;
618 return(TIFFReadDirEntryErrOk);
620 case TIFF_SRATIONAL:
622 double m;
623 err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
624 if (err!=TIFFReadDirEntryErrOk)
625 return(err);
626 *value=(float)m;
627 return(TIFFReadDirEntryErrOk);
629 case TIFF_FLOAT:
630 TIFFReadDirEntryCheckedFloat(tif,direntry,value);
631 return(TIFFReadDirEntryErrOk);
632 case TIFF_DOUBLE:
634 double m;
635 err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
636 if (err!=TIFFReadDirEntryErrOk)
637 return(err);
638 if ((m > FLT_MAX) || (m < -FLT_MAX))
639 return(TIFFReadDirEntryErrRange);
640 *value=(float)m;
641 return(TIFFReadDirEntryErrOk);
643 default:
644 return(TIFFReadDirEntryErrType);
648 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
650 enum TIFFReadDirEntryErr err;
651 if (direntry->tdir_count!=1)
652 return(TIFFReadDirEntryErrCount);
653 switch (direntry->tdir_type)
655 case TIFF_BYTE:
657 uint8_t m;
658 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
659 *value=(double)m;
660 return(TIFFReadDirEntryErrOk);
662 case TIFF_SBYTE:
664 int8_t m;
665 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
666 *value=(double)m;
667 return(TIFFReadDirEntryErrOk);
669 case TIFF_SHORT:
671 uint16_t m;
672 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
673 *value=(double)m;
674 return(TIFFReadDirEntryErrOk);
676 case TIFF_SSHORT:
678 int16_t m;
679 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
680 *value=(double)m;
681 return(TIFFReadDirEntryErrOk);
683 case TIFF_LONG:
685 uint32_t m;
686 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
687 *value=(double)m;
688 return(TIFFReadDirEntryErrOk);
690 case TIFF_SLONG:
692 int32_t m;
693 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
694 *value=(double)m;
695 return(TIFFReadDirEntryErrOk);
697 case TIFF_LONG8:
699 uint64_t m;
700 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
701 if (err!=TIFFReadDirEntryErrOk)
702 return(err);
703 #if defined(__WIN32__) && (_MSC_VER < 1500)
705 * XXX: MSVC 6.0 does not support conversion
706 * of 64-bit integers into floating point
707 * values.
709 *value = _TIFFUInt64ToDouble(m);
710 #else
711 *value = (double)m;
712 #endif
713 return(TIFFReadDirEntryErrOk);
715 case TIFF_SLONG8:
717 int64_t m;
718 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
719 if (err!=TIFFReadDirEntryErrOk)
720 return(err);
721 *value=(double)m;
722 return(TIFFReadDirEntryErrOk);
724 case TIFF_RATIONAL:
725 err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
726 return(err);
727 case TIFF_SRATIONAL:
728 err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
729 return(err);
730 case TIFF_FLOAT:
732 float m;
733 TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
734 *value=(double)m;
735 return(TIFFReadDirEntryErrOk);
737 case TIFF_DOUBLE:
738 err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
739 return(err);
740 default:
741 return(TIFFReadDirEntryErrType);
745 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64_t* value)
747 enum TIFFReadDirEntryErr err;
748 if (direntry->tdir_count!=1)
749 return(TIFFReadDirEntryErrCount);
750 switch (direntry->tdir_type)
752 case TIFF_LONG:
753 case TIFF_IFD:
755 uint32_t m;
756 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
757 *value=(uint64_t)m;
758 return(TIFFReadDirEntryErrOk);
760 case TIFF_LONG8:
761 case TIFF_IFD8:
762 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
763 return(err);
764 default:
765 return(TIFFReadDirEntryErrType);
770 #define INITIAL_THRESHOLD (1024 * 1024)
771 #define THRESHOLD_MULTIPLIER 10
772 #define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
774 static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
775 TIFF* tif, uint64_t offset, tmsize_t size, void** pdest)
777 #if SIZEOF_SIZE_T == 8
778 tmsize_t threshold = INITIAL_THRESHOLD;
779 #endif
780 tmsize_t already_read = 0;
782 assert( !isMapped(tif) );
784 if (!SeekOK(tif,offset))
785 return(TIFFReadDirEntryErrIo);
787 /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
788 /* so as to avoid allocating too much memory in case the file is too */
789 /* short. We could ask for the file size, but this might be */
790 /* expensive with some I/O layers (think of reading a gzipped file) */
791 /* Restrict to 64 bit processes, so as to avoid reallocs() */
792 /* on 32 bit processes where virtual memory is scarce. */
793 while( already_read < size )
795 void* new_dest;
796 tmsize_t bytes_read;
797 tmsize_t to_read = size - already_read;
798 #if SIZEOF_SIZE_T == 8
799 if( to_read >= threshold && threshold < MAX_THRESHOLD )
801 to_read = threshold;
802 threshold *= THRESHOLD_MULTIPLIER;
804 #endif
806 new_dest = (uint8_t*) _TIFFrealloc(
807 *pdest, already_read + to_read);
808 if( new_dest == NULL )
810 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
811 "Failed to allocate memory for %s "
812 "(%"TIFF_SSIZE_FORMAT" elements of %"TIFF_SSIZE_FORMAT" bytes each)",
813 "TIFFReadDirEntryArray",
814 (tmsize_t) 1, already_read + to_read);
815 return TIFFReadDirEntryErrAlloc;
817 *pdest = new_dest;
819 bytes_read = TIFFReadFile(tif,
820 (char*)*pdest + already_read, to_read);
821 already_read += bytes_read;
822 if (bytes_read != to_read) {
823 return TIFFReadDirEntryErrIo;
826 return TIFFReadDirEntryErrOk;
829 static enum TIFFReadDirEntryErr TIFFReadDirEntryArrayWithLimit(
830 TIFF* tif, TIFFDirEntry* direntry, uint32_t* count, uint32_t desttypesize,
831 void** value, uint64_t maxcount)
833 int typesize;
834 uint32_t datasize;
835 void* data;
836 uint64_t target_count64;
837 int original_datasize_clamped;
838 typesize=TIFFDataWidth(direntry->tdir_type);
840 target_count64 = (direntry->tdir_count > maxcount) ?
841 maxcount : direntry->tdir_count;
843 if ((target_count64==0)||(typesize==0))
845 *value=0;
846 return(TIFFReadDirEntryErrOk);
848 (void) desttypesize;
850 /* We just want to know if the original tag size is more than 4 bytes
851 * (classic TIFF) or 8 bytes (BigTIFF)
853 original_datasize_clamped =
854 ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) * typesize;
857 * As a sanity check, make sure we have no more than a 2GB tag array
858 * in either the current data type or the dest data type. This also
859 * avoids problems with overflow of tmsize_t on 32bit systems.
861 if ((uint64_t)(2147483647 / typesize) < target_count64)
862 return(TIFFReadDirEntryErrSizesan);
863 if ((uint64_t)(2147483647 / desttypesize) < target_count64)
864 return(TIFFReadDirEntryErrSizesan);
866 *count=(uint32_t)target_count64;
867 datasize=(*count)*typesize;
868 assert((tmsize_t)datasize>0);
870 if( isMapped(tif) && datasize > (uint64_t)tif->tif_size )
871 return TIFFReadDirEntryErrIo;
873 if( !isMapped(tif) &&
874 (((tif->tif_flags&TIFF_BIGTIFF) && datasize > 8) ||
875 (!(tif->tif_flags&TIFF_BIGTIFF) && datasize > 4)) )
877 data = NULL;
879 else
881 data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
882 if (data==0)
883 return(TIFFReadDirEntryErrAlloc);
885 if (!(tif->tif_flags&TIFF_BIGTIFF))
887 /* Only the condition on original_datasize_clamped. The second
888 * one is implied, but Coverity Scan cannot see it. */
889 if (original_datasize_clamped<=4 && datasize <= 4 )
890 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
891 else
893 enum TIFFReadDirEntryErr err;
894 uint32_t offset = direntry->tdir_offset.toff_long;
895 if (tif->tif_flags&TIFF_SWAB)
896 TIFFSwabLong(&offset);
897 if( isMapped(tif) )
898 err=TIFFReadDirEntryData(tif, (uint64_t)offset, (tmsize_t)datasize, data);
899 else
900 err=TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, (tmsize_t)datasize, &data);
901 if (err!=TIFFReadDirEntryErrOk)
903 _TIFFfree(data);
904 return(err);
908 else
910 /* See above comment for the Classic TIFF case */
911 if (original_datasize_clamped<=8 && datasize <= 8 )
912 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
913 else
915 enum TIFFReadDirEntryErr err;
916 uint64_t offset = direntry->tdir_offset.toff_long8;
917 if (tif->tif_flags&TIFF_SWAB)
918 TIFFSwabLong8(&offset);
919 if( isMapped(tif) )
920 err=TIFFReadDirEntryData(tif, (uint64_t)offset, (tmsize_t)datasize, data);
921 else
922 err=TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, (tmsize_t)datasize, &data);
923 if (err!=TIFFReadDirEntryErrOk)
925 _TIFFfree(data);
926 return(err);
930 *value=data;
931 return(TIFFReadDirEntryErrOk);
934 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32_t* count, uint32_t desttypesize, void** value)
936 return TIFFReadDirEntryArrayWithLimit(tif, direntry, count,
937 desttypesize, value, ~((uint64_t)0));
940 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8_t** value)
942 enum TIFFReadDirEntryErr err;
943 uint32_t count;
944 void* origdata;
945 uint8_t* data;
946 switch (direntry->tdir_type)
948 case TIFF_ASCII:
949 case TIFF_UNDEFINED:
950 case TIFF_BYTE:
951 case TIFF_SBYTE:
952 case TIFF_SHORT:
953 case TIFF_SSHORT:
954 case TIFF_LONG:
955 case TIFF_SLONG:
956 case TIFF_LONG8:
957 case TIFF_SLONG8:
958 break;
959 default:
960 return(TIFFReadDirEntryErrType);
962 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
963 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
965 *value=0;
966 return(err);
968 switch (direntry->tdir_type)
970 case TIFF_ASCII:
971 case TIFF_UNDEFINED:
972 case TIFF_BYTE:
973 *value=(uint8_t*)origdata;
974 return(TIFFReadDirEntryErrOk);
975 case TIFF_SBYTE:
977 int8_t* m;
978 uint32_t n;
979 m=(int8_t*)origdata;
980 for (n=0; n<count; n++)
982 err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
983 if (err!=TIFFReadDirEntryErrOk)
985 _TIFFfree(origdata);
986 return(err);
988 m++;
990 *value=(uint8_t*)origdata;
991 return(TIFFReadDirEntryErrOk);
994 data=(uint8_t*)_TIFFmalloc(count);
995 if (data==0)
997 _TIFFfree(origdata);
998 return(TIFFReadDirEntryErrAlloc);
1000 switch (direntry->tdir_type)
1002 case TIFF_SHORT:
1004 uint16_t* ma;
1005 uint8_t* mb;
1006 uint32_t n;
1007 ma=(uint16_t*)origdata;
1008 mb=data;
1009 for (n=0; n<count; n++)
1011 if (tif->tif_flags&TIFF_SWAB)
1012 TIFFSwabShort(ma);
1013 err=TIFFReadDirEntryCheckRangeByteShort(*ma);
1014 if (err!=TIFFReadDirEntryErrOk)
1015 break;
1016 *mb++=(uint8_t)(*ma++);
1019 break;
1020 case TIFF_SSHORT:
1022 int16_t* ma;
1023 uint8_t* mb;
1024 uint32_t n;
1025 ma=(int16_t*)origdata;
1026 mb=data;
1027 for (n=0; n<count; n++)
1029 if (tif->tif_flags&TIFF_SWAB)
1030 TIFFSwabShort((uint16_t*)ma);
1031 err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
1032 if (err!=TIFFReadDirEntryErrOk)
1033 break;
1034 *mb++=(uint8_t)(*ma++);
1037 break;
1038 case TIFF_LONG:
1040 uint32_t* ma;
1041 uint8_t* mb;
1042 uint32_t n;
1043 ma=(uint32_t*)origdata;
1044 mb=data;
1045 for (n=0; n<count; n++)
1047 if (tif->tif_flags&TIFF_SWAB)
1048 TIFFSwabLong(ma);
1049 err=TIFFReadDirEntryCheckRangeByteLong(*ma);
1050 if (err!=TIFFReadDirEntryErrOk)
1051 break;
1052 *mb++=(uint8_t)(*ma++);
1055 break;
1056 case TIFF_SLONG:
1058 int32_t* ma;
1059 uint8_t* mb;
1060 uint32_t n;
1061 ma=(int32_t*)origdata;
1062 mb=data;
1063 for (n=0; n<count; n++)
1065 if (tif->tif_flags&TIFF_SWAB)
1066 TIFFSwabLong((uint32_t*)ma);
1067 err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
1068 if (err!=TIFFReadDirEntryErrOk)
1069 break;
1070 *mb++=(uint8_t)(*ma++);
1073 break;
1074 case TIFF_LONG8:
1076 uint64_t* ma;
1077 uint8_t* mb;
1078 uint32_t n;
1079 ma=(uint64_t*)origdata;
1080 mb=data;
1081 for (n=0; n<count; n++)
1083 if (tif->tif_flags&TIFF_SWAB)
1084 TIFFSwabLong8(ma);
1085 err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
1086 if (err!=TIFFReadDirEntryErrOk)
1087 break;
1088 *mb++=(uint8_t)(*ma++);
1091 break;
1092 case TIFF_SLONG8:
1094 int64_t* ma;
1095 uint8_t* mb;
1096 uint32_t n;
1097 ma=(int64_t*)origdata;
1098 mb=data;
1099 for (n=0; n<count; n++)
1101 if (tif->tif_flags&TIFF_SWAB)
1102 TIFFSwabLong8((uint64_t*)ma);
1103 err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1104 if (err!=TIFFReadDirEntryErrOk)
1105 break;
1106 *mb++=(uint8_t)(*ma++);
1109 break;
1111 _TIFFfree(origdata);
1112 if (err!=TIFFReadDirEntryErrOk)
1114 _TIFFfree(data);
1115 return(err);
1117 *value=data;
1118 return(TIFFReadDirEntryErrOk);
1121 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8_t** value)
1123 enum TIFFReadDirEntryErr err;
1124 uint32_t count;
1125 void* origdata;
1126 int8_t* data;
1127 switch (direntry->tdir_type)
1129 case TIFF_UNDEFINED:
1130 case TIFF_BYTE:
1131 case TIFF_SBYTE:
1132 case TIFF_SHORT:
1133 case TIFF_SSHORT:
1134 case TIFF_LONG:
1135 case TIFF_SLONG:
1136 case TIFF_LONG8:
1137 case TIFF_SLONG8:
1138 break;
1139 default:
1140 return(TIFFReadDirEntryErrType);
1142 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1143 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1145 *value=0;
1146 return(err);
1148 switch (direntry->tdir_type)
1150 case TIFF_UNDEFINED:
1151 case TIFF_BYTE:
1153 uint8_t* m;
1154 uint32_t n;
1155 m=(uint8_t*)origdata;
1156 for (n=0; n<count; n++)
1158 err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1159 if (err!=TIFFReadDirEntryErrOk)
1161 _TIFFfree(origdata);
1162 return(err);
1164 m++;
1166 *value=(int8_t*)origdata;
1167 return(TIFFReadDirEntryErrOk);
1169 case TIFF_SBYTE:
1170 *value=(int8_t*)origdata;
1171 return(TIFFReadDirEntryErrOk);
1173 data=(int8_t*)_TIFFmalloc(count);
1174 if (data==0)
1176 _TIFFfree(origdata);
1177 return(TIFFReadDirEntryErrAlloc);
1179 switch (direntry->tdir_type)
1181 case TIFF_SHORT:
1183 uint16_t* ma;
1184 int8_t* mb;
1185 uint32_t n;
1186 ma=(uint16_t*)origdata;
1187 mb=data;
1188 for (n=0; n<count; n++)
1190 if (tif->tif_flags&TIFF_SWAB)
1191 TIFFSwabShort(ma);
1192 err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1193 if (err!=TIFFReadDirEntryErrOk)
1194 break;
1195 *mb++=(int8_t)(*ma++);
1198 break;
1199 case TIFF_SSHORT:
1201 int16_t* ma;
1202 int8_t* mb;
1203 uint32_t n;
1204 ma=(int16_t*)origdata;
1205 mb=data;
1206 for (n=0; n<count; n++)
1208 if (tif->tif_flags&TIFF_SWAB)
1209 TIFFSwabShort((uint16_t*)ma);
1210 err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1211 if (err!=TIFFReadDirEntryErrOk)
1212 break;
1213 *mb++=(int8_t)(*ma++);
1216 break;
1217 case TIFF_LONG:
1219 uint32_t* ma;
1220 int8_t* mb;
1221 uint32_t n;
1222 ma=(uint32_t*)origdata;
1223 mb=data;
1224 for (n=0; n<count; n++)
1226 if (tif->tif_flags&TIFF_SWAB)
1227 TIFFSwabLong(ma);
1228 err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1229 if (err!=TIFFReadDirEntryErrOk)
1230 break;
1231 *mb++=(int8_t)(*ma++);
1234 break;
1235 case TIFF_SLONG:
1237 int32_t* ma;
1238 int8_t* mb;
1239 uint32_t n;
1240 ma=(int32_t*)origdata;
1241 mb=data;
1242 for (n=0; n<count; n++)
1244 if (tif->tif_flags&TIFF_SWAB)
1245 TIFFSwabLong((uint32_t*)ma);
1246 err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1247 if (err!=TIFFReadDirEntryErrOk)
1248 break;
1249 *mb++=(int8_t)(*ma++);
1252 break;
1253 case TIFF_LONG8:
1255 uint64_t* ma;
1256 int8_t* mb;
1257 uint32_t n;
1258 ma=(uint64_t*)origdata;
1259 mb=data;
1260 for (n=0; n<count; n++)
1262 if (tif->tif_flags&TIFF_SWAB)
1263 TIFFSwabLong8(ma);
1264 err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1265 if (err!=TIFFReadDirEntryErrOk)
1266 break;
1267 *mb++=(int8_t)(*ma++);
1270 break;
1271 case TIFF_SLONG8:
1273 int64_t* ma;
1274 int8_t* mb;
1275 uint32_t n;
1276 ma=(int64_t*)origdata;
1277 mb=data;
1278 for (n=0; n<count; n++)
1280 if (tif->tif_flags&TIFF_SWAB)
1281 TIFFSwabLong8((uint64_t*)ma);
1282 err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1283 if (err!=TIFFReadDirEntryErrOk)
1284 break;
1285 *mb++=(int8_t)(*ma++);
1288 break;
1290 _TIFFfree(origdata);
1291 if (err!=TIFFReadDirEntryErrOk)
1293 _TIFFfree(data);
1294 return(err);
1296 *value=data;
1297 return(TIFFReadDirEntryErrOk);
1300 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16_t** value)
1302 enum TIFFReadDirEntryErr err;
1303 uint32_t count;
1304 void* origdata;
1305 uint16_t* data;
1306 switch (direntry->tdir_type)
1308 case TIFF_BYTE:
1309 case TIFF_SBYTE:
1310 case TIFF_SHORT:
1311 case TIFF_SSHORT:
1312 case TIFF_LONG:
1313 case TIFF_SLONG:
1314 case TIFF_LONG8:
1315 case TIFF_SLONG8:
1316 break;
1317 default:
1318 return(TIFFReadDirEntryErrType);
1320 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1321 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1323 *value=0;
1324 return(err);
1326 switch (direntry->tdir_type)
1328 case TIFF_SHORT:
1329 *value=(uint16_t*)origdata;
1330 if (tif->tif_flags&TIFF_SWAB)
1331 TIFFSwabArrayOfShort(*value,count);
1332 return(TIFFReadDirEntryErrOk);
1333 case TIFF_SSHORT:
1335 int16_t* m;
1336 uint32_t n;
1337 m=(int16_t*)origdata;
1338 for (n=0; n<count; n++)
1340 if (tif->tif_flags&TIFF_SWAB)
1341 TIFFSwabShort((uint16_t*)m);
1342 err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1343 if (err!=TIFFReadDirEntryErrOk)
1345 _TIFFfree(origdata);
1346 return(err);
1348 m++;
1350 *value=(uint16_t*)origdata;
1351 return(TIFFReadDirEntryErrOk);
1354 data=(uint16_t*)_TIFFmalloc(count * 2);
1355 if (data==0)
1357 _TIFFfree(origdata);
1358 return(TIFFReadDirEntryErrAlloc);
1360 switch (direntry->tdir_type)
1362 case TIFF_BYTE:
1364 uint8_t* ma;
1365 uint16_t* mb;
1366 uint32_t n;
1367 ma=(uint8_t*)origdata;
1368 mb=data;
1369 for (n=0; n<count; n++)
1370 *mb++=(uint16_t)(*ma++);
1372 break;
1373 case TIFF_SBYTE:
1375 int8_t* ma;
1376 uint16_t* mb;
1377 uint32_t n;
1378 ma=(int8_t*)origdata;
1379 mb=data;
1380 for (n=0; n<count; n++)
1382 err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1383 if (err!=TIFFReadDirEntryErrOk)
1384 break;
1385 *mb++=(uint16_t)(*ma++);
1388 break;
1389 case TIFF_LONG:
1391 uint32_t* ma;
1392 uint16_t* mb;
1393 uint32_t n;
1394 ma=(uint32_t*)origdata;
1395 mb=data;
1396 for (n=0; n<count; n++)
1398 if (tif->tif_flags&TIFF_SWAB)
1399 TIFFSwabLong(ma);
1400 err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1401 if (err!=TIFFReadDirEntryErrOk)
1402 break;
1403 *mb++=(uint16_t)(*ma++);
1406 break;
1407 case TIFF_SLONG:
1409 int32_t* ma;
1410 uint16_t* mb;
1411 uint32_t n;
1412 ma=(int32_t*)origdata;
1413 mb=data;
1414 for (n=0; n<count; n++)
1416 if (tif->tif_flags&TIFF_SWAB)
1417 TIFFSwabLong((uint32_t*)ma);
1418 err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1419 if (err!=TIFFReadDirEntryErrOk)
1420 break;
1421 *mb++=(uint16_t)(*ma++);
1424 break;
1425 case TIFF_LONG8:
1427 uint64_t* ma;
1428 uint16_t* mb;
1429 uint32_t n;
1430 ma=(uint64_t*)origdata;
1431 mb=data;
1432 for (n=0; n<count; n++)
1434 if (tif->tif_flags&TIFF_SWAB)
1435 TIFFSwabLong8(ma);
1436 err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1437 if (err!=TIFFReadDirEntryErrOk)
1438 break;
1439 *mb++=(uint16_t)(*ma++);
1442 break;
1443 case TIFF_SLONG8:
1445 int64_t* ma;
1446 uint16_t* mb;
1447 uint32_t n;
1448 ma=(int64_t*)origdata;
1449 mb=data;
1450 for (n=0; n<count; n++)
1452 if (tif->tif_flags&TIFF_SWAB)
1453 TIFFSwabLong8((uint64_t*)ma);
1454 err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1455 if (err!=TIFFReadDirEntryErrOk)
1456 break;
1457 *mb++=(uint16_t)(*ma++);
1460 break;
1462 _TIFFfree(origdata);
1463 if (err!=TIFFReadDirEntryErrOk)
1465 _TIFFfree(data);
1466 return(err);
1468 *value=data;
1469 return(TIFFReadDirEntryErrOk);
1472 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16_t** value)
1474 enum TIFFReadDirEntryErr err;
1475 uint32_t count;
1476 void* origdata;
1477 int16_t* data;
1478 switch (direntry->tdir_type)
1480 case TIFF_BYTE:
1481 case TIFF_SBYTE:
1482 case TIFF_SHORT:
1483 case TIFF_SSHORT:
1484 case TIFF_LONG:
1485 case TIFF_SLONG:
1486 case TIFF_LONG8:
1487 case TIFF_SLONG8:
1488 break;
1489 default:
1490 return(TIFFReadDirEntryErrType);
1492 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1493 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1495 *value=0;
1496 return(err);
1498 switch (direntry->tdir_type)
1500 case TIFF_SHORT:
1502 uint16_t* m;
1503 uint32_t n;
1504 m=(uint16_t*)origdata;
1505 for (n=0; n<count; n++)
1507 if (tif->tif_flags&TIFF_SWAB)
1508 TIFFSwabShort(m);
1509 err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1510 if (err!=TIFFReadDirEntryErrOk)
1512 _TIFFfree(origdata);
1513 return(err);
1515 m++;
1517 *value=(int16_t*)origdata;
1518 return(TIFFReadDirEntryErrOk);
1520 case TIFF_SSHORT:
1521 *value=(int16_t*)origdata;
1522 if (tif->tif_flags&TIFF_SWAB)
1523 TIFFSwabArrayOfShort((uint16_t*)(*value), count);
1524 return(TIFFReadDirEntryErrOk);
1526 data=(int16_t*)_TIFFmalloc(count * 2);
1527 if (data==0)
1529 _TIFFfree(origdata);
1530 return(TIFFReadDirEntryErrAlloc);
1532 switch (direntry->tdir_type)
1534 case TIFF_BYTE:
1536 uint8_t* ma;
1537 int16_t* mb;
1538 uint32_t n;
1539 ma=(uint8_t*)origdata;
1540 mb=data;
1541 for (n=0; n<count; n++)
1542 *mb++=(int16_t)(*ma++);
1544 break;
1545 case TIFF_SBYTE:
1547 int8_t* ma;
1548 int16_t* mb;
1549 uint32_t n;
1550 ma=(int8_t*)origdata;
1551 mb=data;
1552 for (n=0; n<count; n++)
1553 *mb++=(int16_t)(*ma++);
1555 break;
1556 case TIFF_LONG:
1558 uint32_t* ma;
1559 int16_t* mb;
1560 uint32_t n;
1561 ma=(uint32_t*)origdata;
1562 mb=data;
1563 for (n=0; n<count; n++)
1565 if (tif->tif_flags&TIFF_SWAB)
1566 TIFFSwabLong(ma);
1567 err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1568 if (err!=TIFFReadDirEntryErrOk)
1569 break;
1570 *mb++=(int16_t)(*ma++);
1573 break;
1574 case TIFF_SLONG:
1576 int32_t* ma;
1577 int16_t* mb;
1578 uint32_t n;
1579 ma=(int32_t*)origdata;
1580 mb=data;
1581 for (n=0; n<count; n++)
1583 if (tif->tif_flags&TIFF_SWAB)
1584 TIFFSwabLong((uint32_t*)ma);
1585 err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1586 if (err!=TIFFReadDirEntryErrOk)
1587 break;
1588 *mb++=(int16_t)(*ma++);
1591 break;
1592 case TIFF_LONG8:
1594 uint64_t* ma;
1595 int16_t* mb;
1596 uint32_t n;
1597 ma=(uint64_t*)origdata;
1598 mb=data;
1599 for (n=0; n<count; n++)
1601 if (tif->tif_flags&TIFF_SWAB)
1602 TIFFSwabLong8(ma);
1603 err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1604 if (err!=TIFFReadDirEntryErrOk)
1605 break;
1606 *mb++=(int16_t)(*ma++);
1609 break;
1610 case TIFF_SLONG8:
1612 int64_t* ma;
1613 int16_t* mb;
1614 uint32_t n;
1615 ma=(int64_t*)origdata;
1616 mb=data;
1617 for (n=0; n<count; n++)
1619 if (tif->tif_flags&TIFF_SWAB)
1620 TIFFSwabLong8((uint64_t*)ma);
1621 err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1622 if (err!=TIFFReadDirEntryErrOk)
1623 break;
1624 *mb++=(int16_t)(*ma++);
1627 break;
1629 _TIFFfree(origdata);
1630 if (err!=TIFFReadDirEntryErrOk)
1632 _TIFFfree(data);
1633 return(err);
1635 *value=data;
1636 return(TIFFReadDirEntryErrOk);
1639 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32_t** value)
1641 enum TIFFReadDirEntryErr err;
1642 uint32_t count;
1643 void* origdata;
1644 uint32_t* data;
1645 switch (direntry->tdir_type)
1647 case TIFF_BYTE:
1648 case TIFF_SBYTE:
1649 case TIFF_SHORT:
1650 case TIFF_SSHORT:
1651 case TIFF_LONG:
1652 case TIFF_SLONG:
1653 case TIFF_LONG8:
1654 case TIFF_SLONG8:
1655 break;
1656 default:
1657 return(TIFFReadDirEntryErrType);
1659 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1660 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1662 *value=0;
1663 return(err);
1665 switch (direntry->tdir_type)
1667 case TIFF_LONG:
1668 *value=(uint32_t*)origdata;
1669 if (tif->tif_flags&TIFF_SWAB)
1670 TIFFSwabArrayOfLong(*value,count);
1671 return(TIFFReadDirEntryErrOk);
1672 case TIFF_SLONG:
1674 int32_t* m;
1675 uint32_t n;
1676 m=(int32_t*)origdata;
1677 for (n=0; n<count; n++)
1679 if (tif->tif_flags&TIFF_SWAB)
1680 TIFFSwabLong((uint32_t*)m);
1681 err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1682 if (err!=TIFFReadDirEntryErrOk)
1684 _TIFFfree(origdata);
1685 return(err);
1687 m++;
1689 *value=(uint32_t*)origdata;
1690 return(TIFFReadDirEntryErrOk);
1693 data=(uint32_t*)_TIFFmalloc(count * 4);
1694 if (data==0)
1696 _TIFFfree(origdata);
1697 return(TIFFReadDirEntryErrAlloc);
1699 switch (direntry->tdir_type)
1701 case TIFF_BYTE:
1703 uint8_t* ma;
1704 uint32_t* mb;
1705 uint32_t n;
1706 ma=(uint8_t*)origdata;
1707 mb=data;
1708 for (n=0; n<count; n++)
1709 *mb++=(uint32_t)(*ma++);
1711 break;
1712 case TIFF_SBYTE:
1714 int8_t* ma;
1715 uint32_t* mb;
1716 uint32_t n;
1717 ma=(int8_t*)origdata;
1718 mb=data;
1719 for (n=0; n<count; n++)
1721 err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1722 if (err!=TIFFReadDirEntryErrOk)
1723 break;
1724 *mb++=(uint32_t)(*ma++);
1727 break;
1728 case TIFF_SHORT:
1730 uint16_t* ma;
1731 uint32_t* mb;
1732 uint32_t n;
1733 ma=(uint16_t*)origdata;
1734 mb=data;
1735 for (n=0; n<count; n++)
1737 if (tif->tif_flags&TIFF_SWAB)
1738 TIFFSwabShort(ma);
1739 *mb++=(uint32_t)(*ma++);
1742 break;
1743 case TIFF_SSHORT:
1745 int16_t* ma;
1746 uint32_t* mb;
1747 uint32_t n;
1748 ma=(int16_t*)origdata;
1749 mb=data;
1750 for (n=0; n<count; n++)
1752 if (tif->tif_flags&TIFF_SWAB)
1753 TIFFSwabShort((uint16_t*)ma);
1754 err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1755 if (err!=TIFFReadDirEntryErrOk)
1756 break;
1757 *mb++=(uint32_t)(*ma++);
1760 break;
1761 case TIFF_LONG8:
1763 uint64_t* ma;
1764 uint32_t* mb;
1765 uint32_t n;
1766 ma=(uint64_t*)origdata;
1767 mb=data;
1768 for (n=0; n<count; n++)
1770 if (tif->tif_flags&TIFF_SWAB)
1771 TIFFSwabLong8(ma);
1772 err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1773 if (err!=TIFFReadDirEntryErrOk)
1774 break;
1775 *mb++=(uint32_t)(*ma++);
1778 break;
1779 case TIFF_SLONG8:
1781 int64_t* ma;
1782 uint32_t* mb;
1783 uint32_t n;
1784 ma=(int64_t*)origdata;
1785 mb=data;
1786 for (n=0; n<count; n++)
1788 if (tif->tif_flags&TIFF_SWAB)
1789 TIFFSwabLong8((uint64_t*)ma);
1790 err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1791 if (err!=TIFFReadDirEntryErrOk)
1792 break;
1793 *mb++=(uint32_t)(*ma++);
1796 break;
1798 _TIFFfree(origdata);
1799 if (err!=TIFFReadDirEntryErrOk)
1801 _TIFFfree(data);
1802 return(err);
1804 *value=data;
1805 return(TIFFReadDirEntryErrOk);
1808 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32_t** value)
1810 enum TIFFReadDirEntryErr err;
1811 uint32_t count;
1812 void* origdata;
1813 int32_t* data;
1814 switch (direntry->tdir_type)
1816 case TIFF_BYTE:
1817 case TIFF_SBYTE:
1818 case TIFF_SHORT:
1819 case TIFF_SSHORT:
1820 case TIFF_LONG:
1821 case TIFF_SLONG:
1822 case TIFF_LONG8:
1823 case TIFF_SLONG8:
1824 break;
1825 default:
1826 return(TIFFReadDirEntryErrType);
1828 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1829 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1831 *value=0;
1832 return(err);
1834 switch (direntry->tdir_type)
1836 case TIFF_LONG:
1838 uint32_t* m;
1839 uint32_t n;
1840 m=(uint32_t*)origdata;
1841 for (n=0; n<count; n++)
1843 if (tif->tif_flags&TIFF_SWAB)
1844 TIFFSwabLong((uint32_t*)m);
1845 err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1846 if (err!=TIFFReadDirEntryErrOk)
1848 _TIFFfree(origdata);
1849 return(err);
1851 m++;
1853 *value=(int32_t*)origdata;
1854 return(TIFFReadDirEntryErrOk);
1856 case TIFF_SLONG:
1857 *value=(int32_t*)origdata;
1858 if (tif->tif_flags&TIFF_SWAB)
1859 TIFFSwabArrayOfLong((uint32_t*)(*value), count);
1860 return(TIFFReadDirEntryErrOk);
1862 data=(int32_t*)_TIFFmalloc(count * 4);
1863 if (data==0)
1865 _TIFFfree(origdata);
1866 return(TIFFReadDirEntryErrAlloc);
1868 switch (direntry->tdir_type)
1870 case TIFF_BYTE:
1872 uint8_t* ma;
1873 int32_t* mb;
1874 uint32_t n;
1875 ma=(uint8_t*)origdata;
1876 mb=data;
1877 for (n=0; n<count; n++)
1878 *mb++=(int32_t)(*ma++);
1880 break;
1881 case TIFF_SBYTE:
1883 int8_t* ma;
1884 int32_t* mb;
1885 uint32_t n;
1886 ma=(int8_t*)origdata;
1887 mb=data;
1888 for (n=0; n<count; n++)
1889 *mb++=(int32_t)(*ma++);
1891 break;
1892 case TIFF_SHORT:
1894 uint16_t* ma;
1895 int32_t* mb;
1896 uint32_t n;
1897 ma=(uint16_t*)origdata;
1898 mb=data;
1899 for (n=0; n<count; n++)
1901 if (tif->tif_flags&TIFF_SWAB)
1902 TIFFSwabShort(ma);
1903 *mb++=(int32_t)(*ma++);
1906 break;
1907 case TIFF_SSHORT:
1909 int16_t* ma;
1910 int32_t* mb;
1911 uint32_t n;
1912 ma=(int16_t*)origdata;
1913 mb=data;
1914 for (n=0; n<count; n++)
1916 if (tif->tif_flags&TIFF_SWAB)
1917 TIFFSwabShort((uint16_t*)ma);
1918 *mb++=(int32_t)(*ma++);
1921 break;
1922 case TIFF_LONG8:
1924 uint64_t* ma;
1925 int32_t* mb;
1926 uint32_t n;
1927 ma=(uint64_t*)origdata;
1928 mb=data;
1929 for (n=0; n<count; n++)
1931 if (tif->tif_flags&TIFF_SWAB)
1932 TIFFSwabLong8(ma);
1933 err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1934 if (err!=TIFFReadDirEntryErrOk)
1935 break;
1936 *mb++=(int32_t)(*ma++);
1939 break;
1940 case TIFF_SLONG8:
1942 int64_t* ma;
1943 int32_t* mb;
1944 uint32_t n;
1945 ma=(int64_t*)origdata;
1946 mb=data;
1947 for (n=0; n<count; n++)
1949 if (tif->tif_flags&TIFF_SWAB)
1950 TIFFSwabLong8((uint64_t*)ma);
1951 err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1952 if (err!=TIFFReadDirEntryErrOk)
1953 break;
1954 *mb++=(int32_t)(*ma++);
1957 break;
1959 _TIFFfree(origdata);
1960 if (err!=TIFFReadDirEntryErrOk)
1962 _TIFFfree(data);
1963 return(err);
1965 *value=data;
1966 return(TIFFReadDirEntryErrOk);
1969 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(
1970 TIFF* tif, TIFFDirEntry* direntry, uint64_t** value, uint64_t maxcount)
1972 enum TIFFReadDirEntryErr err;
1973 uint32_t count;
1974 void* origdata;
1975 uint64_t* data;
1976 switch (direntry->tdir_type)
1978 case TIFF_BYTE:
1979 case TIFF_SBYTE:
1980 case TIFF_SHORT:
1981 case TIFF_SSHORT:
1982 case TIFF_LONG:
1983 case TIFF_SLONG:
1984 case TIFF_LONG8:
1985 case TIFF_SLONG8:
1986 break;
1987 default:
1988 return(TIFFReadDirEntryErrType);
1990 err=TIFFReadDirEntryArrayWithLimit(tif,direntry,&count,8,&origdata,maxcount);
1991 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1993 *value=0;
1994 return(err);
1996 switch (direntry->tdir_type)
1998 case TIFF_LONG8:
1999 *value=(uint64_t*)origdata;
2000 if (tif->tif_flags&TIFF_SWAB)
2001 TIFFSwabArrayOfLong8(*value,count);
2002 return(TIFFReadDirEntryErrOk);
2003 case TIFF_SLONG8:
2005 int64_t* m;
2006 uint32_t n;
2007 m=(int64_t*)origdata;
2008 for (n=0; n<count; n++)
2010 if (tif->tif_flags&TIFF_SWAB)
2011 TIFFSwabLong8((uint64_t*)m);
2012 err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2013 if (err!=TIFFReadDirEntryErrOk)
2015 _TIFFfree(origdata);
2016 return(err);
2018 m++;
2020 *value=(uint64_t*)origdata;
2021 return(TIFFReadDirEntryErrOk);
2024 data=(uint64_t*)_TIFFmalloc(count * 8);
2025 if (data==0)
2027 _TIFFfree(origdata);
2028 return(TIFFReadDirEntryErrAlloc);
2030 switch (direntry->tdir_type)
2032 case TIFF_BYTE:
2034 uint8_t* ma;
2035 uint64_t* mb;
2036 uint32_t n;
2037 ma=(uint8_t*)origdata;
2038 mb=data;
2039 for (n=0; n<count; n++)
2040 *mb++=(uint64_t)(*ma++);
2042 break;
2043 case TIFF_SBYTE:
2045 int8_t* ma;
2046 uint64_t* mb;
2047 uint32_t n;
2048 ma=(int8_t*)origdata;
2049 mb=data;
2050 for (n=0; n<count; n++)
2052 err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2053 if (err!=TIFFReadDirEntryErrOk)
2054 break;
2055 *mb++=(uint64_t)(*ma++);
2058 break;
2059 case TIFF_SHORT:
2061 uint16_t* ma;
2062 uint64_t* mb;
2063 uint32_t n;
2064 ma=(uint16_t*)origdata;
2065 mb=data;
2066 for (n=0; n<count; n++)
2068 if (tif->tif_flags&TIFF_SWAB)
2069 TIFFSwabShort(ma);
2070 *mb++=(uint64_t)(*ma++);
2073 break;
2074 case TIFF_SSHORT:
2076 int16_t* ma;
2077 uint64_t* mb;
2078 uint32_t n;
2079 ma=(int16_t*)origdata;
2080 mb=data;
2081 for (n=0; n<count; n++)
2083 if (tif->tif_flags&TIFF_SWAB)
2084 TIFFSwabShort((uint16_t*)ma);
2085 err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2086 if (err!=TIFFReadDirEntryErrOk)
2087 break;
2088 *mb++=(uint64_t)(*ma++);
2091 break;
2092 case TIFF_LONG:
2094 uint32_t* ma;
2095 uint64_t* mb;
2096 uint32_t n;
2097 ma=(uint32_t*)origdata;
2098 mb=data;
2099 for (n=0; n<count; n++)
2101 if (tif->tif_flags&TIFF_SWAB)
2102 TIFFSwabLong(ma);
2103 *mb++=(uint64_t)(*ma++);
2106 break;
2107 case TIFF_SLONG:
2109 int32_t* ma;
2110 uint64_t* mb;
2111 uint32_t n;
2112 ma=(int32_t*)origdata;
2113 mb=data;
2114 for (n=0; n<count; n++)
2116 if (tif->tif_flags&TIFF_SWAB)
2117 TIFFSwabLong((uint32_t*)ma);
2118 err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2119 if (err!=TIFFReadDirEntryErrOk)
2120 break;
2121 *mb++=(uint64_t)(*ma++);
2124 break;
2126 _TIFFfree(origdata);
2127 if (err!=TIFFReadDirEntryErrOk)
2129 _TIFFfree(data);
2130 return(err);
2132 *value=data;
2133 return(TIFFReadDirEntryErrOk);
2136 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64_t** value)
2138 return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value, ~((uint64_t)0));
2141 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64_t** value)
2143 enum TIFFReadDirEntryErr err;
2144 uint32_t count;
2145 void* origdata;
2146 int64_t* data;
2147 switch (direntry->tdir_type)
2149 case TIFF_BYTE:
2150 case TIFF_SBYTE:
2151 case TIFF_SHORT:
2152 case TIFF_SSHORT:
2153 case TIFF_LONG:
2154 case TIFF_SLONG:
2155 case TIFF_LONG8:
2156 case TIFF_SLONG8:
2157 break;
2158 default:
2159 return(TIFFReadDirEntryErrType);
2161 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2162 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2164 *value=0;
2165 return(err);
2167 switch (direntry->tdir_type)
2169 case TIFF_LONG8:
2171 uint64_t* m;
2172 uint32_t n;
2173 m=(uint64_t*)origdata;
2174 for (n=0; n<count; n++)
2176 if (tif->tif_flags&TIFF_SWAB)
2177 TIFFSwabLong8(m);
2178 err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2179 if (err!=TIFFReadDirEntryErrOk)
2181 _TIFFfree(origdata);
2182 return(err);
2184 m++;
2186 *value=(int64_t*)origdata;
2187 return(TIFFReadDirEntryErrOk);
2189 case TIFF_SLONG8:
2190 *value=(int64_t*)origdata;
2191 if (tif->tif_flags&TIFF_SWAB)
2192 TIFFSwabArrayOfLong8((uint64_t*)(*value), count);
2193 return(TIFFReadDirEntryErrOk);
2195 data=(int64_t*)_TIFFmalloc(count * 8);
2196 if (data==0)
2198 _TIFFfree(origdata);
2199 return(TIFFReadDirEntryErrAlloc);
2201 switch (direntry->tdir_type)
2203 case TIFF_BYTE:
2205 uint8_t* ma;
2206 int64_t* mb;
2207 uint32_t n;
2208 ma=(uint8_t*)origdata;
2209 mb=data;
2210 for (n=0; n<count; n++)
2211 *mb++=(int64_t)(*ma++);
2213 break;
2214 case TIFF_SBYTE:
2216 int8_t* ma;
2217 int64_t* mb;
2218 uint32_t n;
2219 ma=(int8_t*)origdata;
2220 mb=data;
2221 for (n=0; n<count; n++)
2222 *mb++=(int64_t)(*ma++);
2224 break;
2225 case TIFF_SHORT:
2227 uint16_t* ma;
2228 int64_t* mb;
2229 uint32_t n;
2230 ma=(uint16_t*)origdata;
2231 mb=data;
2232 for (n=0; n<count; n++)
2234 if (tif->tif_flags&TIFF_SWAB)
2235 TIFFSwabShort(ma);
2236 *mb++=(int64_t)(*ma++);
2239 break;
2240 case TIFF_SSHORT:
2242 int16_t* ma;
2243 int64_t* mb;
2244 uint32_t n;
2245 ma=(int16_t*)origdata;
2246 mb=data;
2247 for (n=0; n<count; n++)
2249 if (tif->tif_flags&TIFF_SWAB)
2250 TIFFSwabShort((uint16_t*)ma);
2251 *mb++=(int64_t)(*ma++);
2254 break;
2255 case TIFF_LONG:
2257 uint32_t* ma;
2258 int64_t* mb;
2259 uint32_t n;
2260 ma=(uint32_t*)origdata;
2261 mb=data;
2262 for (n=0; n<count; n++)
2264 if (tif->tif_flags&TIFF_SWAB)
2265 TIFFSwabLong(ma);
2266 *mb++=(int64_t)(*ma++);
2269 break;
2270 case TIFF_SLONG:
2272 int32_t* ma;
2273 int64_t* mb;
2274 uint32_t n;
2275 ma=(int32_t*)origdata;
2276 mb=data;
2277 for (n=0; n<count; n++)
2279 if (tif->tif_flags&TIFF_SWAB)
2280 TIFFSwabLong((uint32_t*)ma);
2281 *mb++=(int64_t)(*ma++);
2284 break;
2286 _TIFFfree(origdata);
2287 *value=data;
2288 return(TIFFReadDirEntryErrOk);
2291 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2293 enum TIFFReadDirEntryErr err;
2294 uint32_t count;
2295 void* origdata;
2296 float* data;
2297 switch (direntry->tdir_type)
2299 case TIFF_BYTE:
2300 case TIFF_SBYTE:
2301 case TIFF_SHORT:
2302 case TIFF_SSHORT:
2303 case TIFF_LONG:
2304 case TIFF_SLONG:
2305 case TIFF_LONG8:
2306 case TIFF_SLONG8:
2307 case TIFF_RATIONAL:
2308 case TIFF_SRATIONAL:
2309 case TIFF_FLOAT:
2310 case TIFF_DOUBLE:
2311 break;
2312 default:
2313 return(TIFFReadDirEntryErrType);
2315 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2316 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2318 *value=0;
2319 return(err);
2321 switch (direntry->tdir_type)
2323 case TIFF_FLOAT:
2324 if (tif->tif_flags&TIFF_SWAB)
2325 TIFFSwabArrayOfLong((uint32_t*)origdata, count);
2326 TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2327 *value=(float*)origdata;
2328 return(TIFFReadDirEntryErrOk);
2330 data=(float*)_TIFFmalloc(count*sizeof(float));
2331 if (data==0)
2333 _TIFFfree(origdata);
2334 return(TIFFReadDirEntryErrAlloc);
2336 switch (direntry->tdir_type)
2338 case TIFF_BYTE:
2340 uint8_t* ma;
2341 float* mb;
2342 uint32_t n;
2343 ma=(uint8_t*)origdata;
2344 mb=data;
2345 for (n=0; n<count; n++)
2346 *mb++=(float)(*ma++);
2348 break;
2349 case TIFF_SBYTE:
2351 int8_t* ma;
2352 float* mb;
2353 uint32_t n;
2354 ma=(int8_t*)origdata;
2355 mb=data;
2356 for (n=0; n<count; n++)
2357 *mb++=(float)(*ma++);
2359 break;
2360 case TIFF_SHORT:
2362 uint16_t* ma;
2363 float* mb;
2364 uint32_t n;
2365 ma=(uint16_t*)origdata;
2366 mb=data;
2367 for (n=0; n<count; n++)
2369 if (tif->tif_flags&TIFF_SWAB)
2370 TIFFSwabShort(ma);
2371 *mb++=(float)(*ma++);
2374 break;
2375 case TIFF_SSHORT:
2377 int16_t* ma;
2378 float* mb;
2379 uint32_t n;
2380 ma=(int16_t*)origdata;
2381 mb=data;
2382 for (n=0; n<count; n++)
2384 if (tif->tif_flags&TIFF_SWAB)
2385 TIFFSwabShort((uint16_t*)ma);
2386 *mb++=(float)(*ma++);
2389 break;
2390 case TIFF_LONG:
2392 uint32_t* ma;
2393 float* mb;
2394 uint32_t n;
2395 ma=(uint32_t*)origdata;
2396 mb=data;
2397 for (n=0; n<count; n++)
2399 if (tif->tif_flags&TIFF_SWAB)
2400 TIFFSwabLong(ma);
2401 *mb++=(float)(*ma++);
2404 break;
2405 case TIFF_SLONG:
2407 int32_t* ma;
2408 float* mb;
2409 uint32_t n;
2410 ma=(int32_t*)origdata;
2411 mb=data;
2412 for (n=0; n<count; n++)
2414 if (tif->tif_flags&TIFF_SWAB)
2415 TIFFSwabLong((uint32_t*)ma);
2416 *mb++=(float)(*ma++);
2419 break;
2420 case TIFF_LONG8:
2422 uint64_t* ma;
2423 float* mb;
2424 uint32_t n;
2425 ma=(uint64_t*)origdata;
2426 mb=data;
2427 for (n=0; n<count; n++)
2429 if (tif->tif_flags&TIFF_SWAB)
2430 TIFFSwabLong8(ma);
2431 #if defined(__WIN32__) && (_MSC_VER < 1500)
2433 * XXX: MSVC 6.0 does not support
2434 * conversion of 64-bit integers into
2435 * floating point values.
2437 *mb++ = _TIFFUInt64ToFloat(*ma++);
2438 #else
2439 *mb++ = (float)(*ma++);
2440 #endif
2443 break;
2444 case TIFF_SLONG8:
2446 int64_t* ma;
2447 float* mb;
2448 uint32_t n;
2449 ma=(int64_t*)origdata;
2450 mb=data;
2451 for (n=0; n<count; n++)
2453 if (tif->tif_flags&TIFF_SWAB)
2454 TIFFSwabLong8((uint64_t*)ma);
2455 *mb++=(float)(*ma++);
2458 break;
2459 case TIFF_RATIONAL:
2461 uint32_t* ma;
2462 uint32_t maa;
2463 uint32_t mab;
2464 float* mb;
2465 uint32_t n;
2466 ma=(uint32_t*)origdata;
2467 mb=data;
2468 for (n=0; n<count; n++)
2470 if (tif->tif_flags&TIFF_SWAB)
2471 TIFFSwabLong(ma);
2472 maa=*ma++;
2473 if (tif->tif_flags&TIFF_SWAB)
2474 TIFFSwabLong(ma);
2475 mab=*ma++;
2476 if (mab==0)
2477 *mb++=0.0;
2478 else
2479 *mb++=(float)maa/(float)mab;
2482 break;
2483 case TIFF_SRATIONAL:
2485 uint32_t* ma;
2486 int32_t maa;
2487 uint32_t mab;
2488 float* mb;
2489 uint32_t n;
2490 ma=(uint32_t*)origdata;
2491 mb=data;
2492 for (n=0; n<count; n++)
2494 if (tif->tif_flags&TIFF_SWAB)
2495 TIFFSwabLong(ma);
2496 maa=*(int32_t*)ma;
2497 ma++;
2498 if (tif->tif_flags&TIFF_SWAB)
2499 TIFFSwabLong(ma);
2500 mab=*ma++;
2501 if (mab==0)
2502 *mb++=0.0;
2503 else
2504 *mb++=(float)maa/(float)mab;
2507 break;
2508 case TIFF_DOUBLE:
2510 double* ma;
2511 float* mb;
2512 uint32_t n;
2513 if (tif->tif_flags&TIFF_SWAB)
2514 TIFFSwabArrayOfLong8((uint64_t*)origdata, count);
2515 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2516 ma=(double*)origdata;
2517 mb=data;
2518 for (n=0; n<count; n++)
2520 double val = *ma++;
2521 if( val > FLT_MAX )
2522 val = FLT_MAX;
2523 else if( val < -FLT_MAX )
2524 val = -FLT_MAX;
2525 *mb++=(float)val;
2528 break;
2530 _TIFFfree(origdata);
2531 *value=data;
2532 return(TIFFReadDirEntryErrOk);
2535 static enum TIFFReadDirEntryErr
2536 TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2538 enum TIFFReadDirEntryErr err;
2539 uint32_t count;
2540 void* origdata;
2541 double* data;
2542 switch (direntry->tdir_type)
2544 case TIFF_BYTE:
2545 case TIFF_SBYTE:
2546 case TIFF_SHORT:
2547 case TIFF_SSHORT:
2548 case TIFF_LONG:
2549 case TIFF_SLONG:
2550 case TIFF_LONG8:
2551 case TIFF_SLONG8:
2552 case TIFF_RATIONAL:
2553 case TIFF_SRATIONAL:
2554 case TIFF_FLOAT:
2555 case TIFF_DOUBLE:
2556 break;
2557 default:
2558 return(TIFFReadDirEntryErrType);
2560 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2561 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2563 *value=0;
2564 return(err);
2566 switch (direntry->tdir_type)
2568 case TIFF_DOUBLE:
2569 if (tif->tif_flags&TIFF_SWAB)
2570 TIFFSwabArrayOfLong8((uint64_t*)origdata, count);
2571 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2572 *value=(double*)origdata;
2573 return(TIFFReadDirEntryErrOk);
2575 data=(double*)_TIFFmalloc(count*sizeof(double));
2576 if (data==0)
2578 _TIFFfree(origdata);
2579 return(TIFFReadDirEntryErrAlloc);
2581 switch (direntry->tdir_type)
2583 case TIFF_BYTE:
2585 uint8_t* ma;
2586 double* mb;
2587 uint32_t n;
2588 ma=(uint8_t*)origdata;
2589 mb=data;
2590 for (n=0; n<count; n++)
2591 *mb++=(double)(*ma++);
2593 break;
2594 case TIFF_SBYTE:
2596 int8_t* ma;
2597 double* mb;
2598 uint32_t n;
2599 ma=(int8_t*)origdata;
2600 mb=data;
2601 for (n=0; n<count; n++)
2602 *mb++=(double)(*ma++);
2604 break;
2605 case TIFF_SHORT:
2607 uint16_t* ma;
2608 double* mb;
2609 uint32_t n;
2610 ma=(uint16_t*)origdata;
2611 mb=data;
2612 for (n=0; n<count; n++)
2614 if (tif->tif_flags&TIFF_SWAB)
2615 TIFFSwabShort(ma);
2616 *mb++=(double)(*ma++);
2619 break;
2620 case TIFF_SSHORT:
2622 int16_t* ma;
2623 double* mb;
2624 uint32_t n;
2625 ma=(int16_t*)origdata;
2626 mb=data;
2627 for (n=0; n<count; n++)
2629 if (tif->tif_flags&TIFF_SWAB)
2630 TIFFSwabShort((uint16_t*)ma);
2631 *mb++=(double)(*ma++);
2634 break;
2635 case TIFF_LONG:
2637 uint32_t* ma;
2638 double* mb;
2639 uint32_t n;
2640 ma=(uint32_t*)origdata;
2641 mb=data;
2642 for (n=0; n<count; n++)
2644 if (tif->tif_flags&TIFF_SWAB)
2645 TIFFSwabLong(ma);
2646 *mb++=(double)(*ma++);
2649 break;
2650 case TIFF_SLONG:
2652 int32_t* ma;
2653 double* mb;
2654 uint32_t n;
2655 ma=(int32_t*)origdata;
2656 mb=data;
2657 for (n=0; n<count; n++)
2659 if (tif->tif_flags&TIFF_SWAB)
2660 TIFFSwabLong((uint32_t*)ma);
2661 *mb++=(double)(*ma++);
2664 break;
2665 case TIFF_LONG8:
2667 uint64_t* ma;
2668 double* mb;
2669 uint32_t n;
2670 ma=(uint64_t*)origdata;
2671 mb=data;
2672 for (n=0; n<count; n++)
2674 if (tif->tif_flags&TIFF_SWAB)
2675 TIFFSwabLong8(ma);
2676 #if defined(__WIN32__) && (_MSC_VER < 1500)
2678 * XXX: MSVC 6.0 does not support
2679 * conversion of 64-bit integers into
2680 * floating point values.
2682 *mb++ = _TIFFUInt64ToDouble(*ma++);
2683 #else
2684 *mb++ = (double)(*ma++);
2685 #endif
2688 break;
2689 case TIFF_SLONG8:
2691 int64_t* ma;
2692 double* mb;
2693 uint32_t n;
2694 ma=(int64_t*)origdata;
2695 mb=data;
2696 for (n=0; n<count; n++)
2698 if (tif->tif_flags&TIFF_SWAB)
2699 TIFFSwabLong8((uint64_t*)ma);
2700 *mb++=(double)(*ma++);
2703 break;
2704 case TIFF_RATIONAL:
2706 uint32_t* ma;
2707 uint32_t maa;
2708 uint32_t mab;
2709 double* mb;
2710 uint32_t n;
2711 ma=(uint32_t*)origdata;
2712 mb=data;
2713 for (n=0; n<count; n++)
2715 if (tif->tif_flags&TIFF_SWAB)
2716 TIFFSwabLong(ma);
2717 maa=*ma++;
2718 if (tif->tif_flags&TIFF_SWAB)
2719 TIFFSwabLong(ma);
2720 mab=*ma++;
2721 if (mab==0)
2722 *mb++=0.0;
2723 else
2724 *mb++=(double)maa/(double)mab;
2727 break;
2728 case TIFF_SRATIONAL:
2730 uint32_t* ma;
2731 int32_t maa;
2732 uint32_t mab;
2733 double* mb;
2734 uint32_t n;
2735 ma=(uint32_t*)origdata;
2736 mb=data;
2737 for (n=0; n<count; n++)
2739 if (tif->tif_flags&TIFF_SWAB)
2740 TIFFSwabLong(ma);
2741 maa=*(int32_t*)ma;
2742 ma++;
2743 if (tif->tif_flags&TIFF_SWAB)
2744 TIFFSwabLong(ma);
2745 mab=*ma++;
2746 if (mab==0)
2747 *mb++=0.0;
2748 else
2749 *mb++=(double)maa/(double)mab;
2752 break;
2753 case TIFF_FLOAT:
2755 float* ma;
2756 double* mb;
2757 uint32_t n;
2758 if (tif->tif_flags&TIFF_SWAB)
2759 TIFFSwabArrayOfLong((uint32_t*)origdata, count);
2760 TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
2761 ma=(float*)origdata;
2762 mb=data;
2763 for (n=0; n<count; n++)
2764 *mb++=(double)(*ma++);
2766 break;
2768 _TIFFfree(origdata);
2769 *value=data;
2770 return(TIFFReadDirEntryErrOk);
2773 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64_t** value)
2775 enum TIFFReadDirEntryErr err;
2776 uint32_t count;
2777 void* origdata;
2778 uint64_t* data;
2779 switch (direntry->tdir_type)
2781 case TIFF_LONG:
2782 case TIFF_LONG8:
2783 case TIFF_IFD:
2784 case TIFF_IFD8:
2785 break;
2786 default:
2787 return(TIFFReadDirEntryErrType);
2789 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2790 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2792 *value=0;
2793 return(err);
2795 switch (direntry->tdir_type)
2797 case TIFF_LONG8:
2798 case TIFF_IFD8:
2799 *value=(uint64_t*)origdata;
2800 if (tif->tif_flags&TIFF_SWAB)
2801 TIFFSwabArrayOfLong8(*value,count);
2802 return(TIFFReadDirEntryErrOk);
2804 data=(uint64_t*)_TIFFmalloc(count * 8);
2805 if (data==0)
2807 _TIFFfree(origdata);
2808 return(TIFFReadDirEntryErrAlloc);
2810 switch (direntry->tdir_type)
2812 case TIFF_LONG:
2813 case TIFF_IFD:
2815 uint32_t* ma;
2816 uint64_t* mb;
2817 uint32_t n;
2818 ma=(uint32_t*)origdata;
2819 mb=data;
2820 for (n=0; n<count; n++)
2822 if (tif->tif_flags&TIFF_SWAB)
2823 TIFFSwabLong(ma);
2824 *mb++=(uint64_t)(*ma++);
2827 break;
2829 _TIFFfree(origdata);
2830 *value=data;
2831 return(TIFFReadDirEntryErrOk);
2834 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16_t* value)
2836 enum TIFFReadDirEntryErr err;
2837 uint16_t* m;
2838 uint16_t* na;
2839 uint16_t nb;
2840 if (direntry->tdir_count<(uint64_t)tif->tif_dir.td_samplesperpixel)
2841 return(TIFFReadDirEntryErrCount);
2842 err=TIFFReadDirEntryShortArray(tif,direntry,&m);
2843 if (err!=TIFFReadDirEntryErrOk || m == NULL)
2844 return(err);
2845 na=m;
2846 nb=tif->tif_dir.td_samplesperpixel;
2847 *value=*na++;
2848 nb--;
2849 while (nb>0)
2851 if (*na++!=*value)
2853 err=TIFFReadDirEntryErrPsdif;
2854 break;
2856 nb--;
2858 _TIFFfree(m);
2859 return(err);
2862 #if 0
2863 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2865 enum TIFFReadDirEntryErr err;
2866 double* m;
2867 double* na;
2868 uint16_t nb;
2869 if (direntry->tdir_count<(uint64_t)tif->tif_dir.td_samplesperpixel)
2870 return(TIFFReadDirEntryErrCount);
2871 err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
2872 if (err!=TIFFReadDirEntryErrOk)
2873 return(err);
2874 na=m;
2875 nb=tif->tif_dir.td_samplesperpixel;
2876 *value=*na++;
2877 nb--;
2878 while (nb>0)
2880 if (*na++!=*value)
2882 err=TIFFReadDirEntryErrPsdif;
2883 break;
2885 nb--;
2887 _TIFFfree(m);
2888 return(err);
2890 #endif
2892 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8_t* value)
2894 (void) tif;
2895 *value=*(uint8_t*)(&direntry->tdir_offset);
2898 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8_t* value)
2900 (void) tif;
2901 *value=*(int8_t*)(&direntry->tdir_offset);
2904 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16_t* value)
2906 *value = direntry->tdir_offset.toff_short;
2907 /* *value=*(uint16_t*)(&direntry->tdir_offset); */
2908 if (tif->tif_flags&TIFF_SWAB)
2909 TIFFSwabShort(value);
2912 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16_t* value)
2914 *value=*(int16_t*)(&direntry->tdir_offset);
2915 if (tif->tif_flags&TIFF_SWAB)
2916 TIFFSwabShort((uint16_t*)value);
2919 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32_t* value)
2921 *value=*(uint32_t*)(&direntry->tdir_offset);
2922 if (tif->tif_flags&TIFF_SWAB)
2923 TIFFSwabLong(value);
2926 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32_t* value)
2928 *value=*(int32_t*)(&direntry->tdir_offset);
2929 if (tif->tif_flags&TIFF_SWAB)
2930 TIFFSwabLong((uint32_t*)value);
2933 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64_t* value)
2935 if (!(tif->tif_flags&TIFF_BIGTIFF))
2937 enum TIFFReadDirEntryErr err;
2938 uint32_t offset = direntry->tdir_offset.toff_long;
2939 if (tif->tif_flags&TIFF_SWAB)
2940 TIFFSwabLong(&offset);
2941 err=TIFFReadDirEntryData(tif,offset,8,value);
2942 if (err!=TIFFReadDirEntryErrOk)
2943 return(err);
2945 else
2946 *value = direntry->tdir_offset.toff_long8;
2947 if (tif->tif_flags&TIFF_SWAB)
2948 TIFFSwabLong8(value);
2949 return(TIFFReadDirEntryErrOk);
2952 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64_t* value)
2954 if (!(tif->tif_flags&TIFF_BIGTIFF))
2956 enum TIFFReadDirEntryErr err;
2957 uint32_t offset = direntry->tdir_offset.toff_long;
2958 if (tif->tif_flags&TIFF_SWAB)
2959 TIFFSwabLong(&offset);
2960 err=TIFFReadDirEntryData(tif,offset,8,value);
2961 if (err!=TIFFReadDirEntryErrOk)
2962 return(err);
2964 else
2965 *value=*(int64_t*)(&direntry->tdir_offset);
2966 if (tif->tif_flags&TIFF_SWAB)
2967 TIFFSwabLong8((uint64_t*)value);
2968 return(TIFFReadDirEntryErrOk);
2971 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2973 UInt64Aligned_t m;
2975 assert(sizeof(double)==8);
2976 assert(sizeof(uint64_t) == 8);
2977 assert(sizeof(uint32_t) == 4);
2978 if (!(tif->tif_flags&TIFF_BIGTIFF))
2980 enum TIFFReadDirEntryErr err;
2981 uint32_t offset = direntry->tdir_offset.toff_long;
2982 if (tif->tif_flags&TIFF_SWAB)
2983 TIFFSwabLong(&offset);
2984 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2985 if (err!=TIFFReadDirEntryErrOk)
2986 return(err);
2988 else
2989 m.l = direntry->tdir_offset.toff_long8;
2990 if (tif->tif_flags&TIFF_SWAB)
2991 TIFFSwabArrayOfLong(m.i,2);
2992 /* Not completely sure what we should do when m.i[1]==0, but some */
2993 /* sanitizers do not like division by 0.0: */
2994 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
2995 if (m.i[0]==0 || m.i[1]==0)
2996 *value=0.0;
2997 else
2998 *value=(double)m.i[0]/(double)m.i[1];
2999 return(TIFFReadDirEntryErrOk);
3002 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
3004 UInt64Aligned_t m;
3005 assert(sizeof(double)==8);
3006 assert(sizeof(uint64_t) == 8);
3007 assert(sizeof(int32_t) == 4);
3008 assert(sizeof(uint32_t) == 4);
3009 if (!(tif->tif_flags&TIFF_BIGTIFF))
3011 enum TIFFReadDirEntryErr err;
3012 uint32_t offset = direntry->tdir_offset.toff_long;
3013 if (tif->tif_flags&TIFF_SWAB)
3014 TIFFSwabLong(&offset);
3015 err=TIFFReadDirEntryData(tif,offset,8,m.i);
3016 if (err!=TIFFReadDirEntryErrOk)
3017 return(err);
3019 else
3020 m.l=direntry->tdir_offset.toff_long8;
3021 if (tif->tif_flags&TIFF_SWAB)
3022 TIFFSwabArrayOfLong(m.i,2);
3023 /* Not completely sure what we should do when m.i[1]==0, but some */
3024 /* sanitizers do not like division by 0.0: */
3025 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3026 if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
3027 *value=0.0;
3028 else
3029 *value= (double)((int32_t)m.i[0]) / (double)m.i[1];
3030 return(TIFFReadDirEntryErrOk);
3033 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
3035 union
3037 float f;
3038 uint32_t i;
3039 } float_union;
3040 assert(sizeof(float)==4);
3041 assert(sizeof(uint32_t) == 4);
3042 assert(sizeof(float_union)==4);
3043 float_union.i=*(uint32_t*)(&direntry->tdir_offset);
3044 *value=float_union.f;
3045 if (tif->tif_flags&TIFF_SWAB)
3046 TIFFSwabLong((uint32_t*)value);
3049 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
3051 assert(sizeof(double)==8);
3052 assert(sizeof(uint64_t) == 8);
3053 assert(sizeof(UInt64Aligned_t)==8);
3054 if (!(tif->tif_flags&TIFF_BIGTIFF))
3056 enum TIFFReadDirEntryErr err;
3057 uint32_t offset = direntry->tdir_offset.toff_long;
3058 if (tif->tif_flags&TIFF_SWAB)
3059 TIFFSwabLong(&offset);
3060 err=TIFFReadDirEntryData(tif,offset,8,value);
3061 if (err!=TIFFReadDirEntryErrOk)
3062 return(err);
3064 else
3066 UInt64Aligned_t uint64_union;
3067 uint64_union.l=direntry->tdir_offset.toff_long8;
3068 *value=uint64_union.d;
3070 if (tif->tif_flags&TIFF_SWAB)
3071 TIFFSwabLong8((uint64_t*)value);
3072 return(TIFFReadDirEntryErrOk);
3075 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
3077 if (value<0)
3078 return(TIFFReadDirEntryErrRange);
3079 else
3080 return(TIFFReadDirEntryErrOk);
3083 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
3085 if (value>0xFF)
3086 return(TIFFReadDirEntryErrRange);
3087 else
3088 return(TIFFReadDirEntryErrOk);
3091 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
3093 if ((value<0)||(value>0xFF))
3094 return(TIFFReadDirEntryErrRange);
3095 else
3096 return(TIFFReadDirEntryErrOk);
3099 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
3101 if (value>0xFF)
3102 return(TIFFReadDirEntryErrRange);
3103 else
3104 return(TIFFReadDirEntryErrOk);
3107 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
3109 if ((value<0)||(value>0xFF))
3110 return(TIFFReadDirEntryErrRange);
3111 else
3112 return(TIFFReadDirEntryErrOk);
3115 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
3117 if (value>0xFF)
3118 return(TIFFReadDirEntryErrRange);
3119 else
3120 return(TIFFReadDirEntryErrOk);
3123 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
3125 if ((value<0)||(value>0xFF))
3126 return(TIFFReadDirEntryErrRange);
3127 else
3128 return(TIFFReadDirEntryErrOk);
3131 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
3133 if (value>0x7F)
3134 return(TIFFReadDirEntryErrRange);
3135 else
3136 return(TIFFReadDirEntryErrOk);
3139 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
3141 if (value>0x7F)
3142 return(TIFFReadDirEntryErrRange);
3143 else
3144 return(TIFFReadDirEntryErrOk);
3147 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
3149 if ((value<-0x80)||(value>0x7F))
3150 return(TIFFReadDirEntryErrRange);
3151 else
3152 return(TIFFReadDirEntryErrOk);
3155 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
3157 if (value>0x7F)
3158 return(TIFFReadDirEntryErrRange);
3159 else
3160 return(TIFFReadDirEntryErrOk);
3163 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
3165 if ((value<-0x80)||(value>0x7F))
3166 return(TIFFReadDirEntryErrRange);
3167 else
3168 return(TIFFReadDirEntryErrOk);
3171 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
3173 if (value>0x7F)
3174 return(TIFFReadDirEntryErrRange);
3175 else
3176 return(TIFFReadDirEntryErrOk);
3179 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
3181 if ((value<-0x80)||(value>0x7F))
3182 return(TIFFReadDirEntryErrRange);
3183 else
3184 return(TIFFReadDirEntryErrOk);
3187 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
3189 if (value<0)
3190 return(TIFFReadDirEntryErrRange);
3191 else
3192 return(TIFFReadDirEntryErrOk);
3195 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
3197 if (value<0)
3198 return(TIFFReadDirEntryErrRange);
3199 else
3200 return(TIFFReadDirEntryErrOk);
3203 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
3205 if (value>0xFFFF)
3206 return(TIFFReadDirEntryErrRange);
3207 else
3208 return(TIFFReadDirEntryErrOk);
3211 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
3213 if ((value<0)||(value>0xFFFF))
3214 return(TIFFReadDirEntryErrRange);
3215 else
3216 return(TIFFReadDirEntryErrOk);
3219 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
3221 if (value>0xFFFF)
3222 return(TIFFReadDirEntryErrRange);
3223 else
3224 return(TIFFReadDirEntryErrOk);
3227 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
3229 if ((value<0)||(value>0xFFFF))
3230 return(TIFFReadDirEntryErrRange);
3231 else
3232 return(TIFFReadDirEntryErrOk);
3235 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
3237 if (value>0x7FFF)
3238 return(TIFFReadDirEntryErrRange);
3239 else
3240 return(TIFFReadDirEntryErrOk);
3243 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
3245 if (value>0x7FFF)
3246 return(TIFFReadDirEntryErrRange);
3247 else
3248 return(TIFFReadDirEntryErrOk);
3251 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
3253 if ((value<-0x8000)||(value>0x7FFF))
3254 return(TIFFReadDirEntryErrRange);
3255 else
3256 return(TIFFReadDirEntryErrOk);
3259 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
3261 if (value>0x7FFF)
3262 return(TIFFReadDirEntryErrRange);
3263 else
3264 return(TIFFReadDirEntryErrOk);
3267 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
3269 if ((value<-0x8000)||(value>0x7FFF))
3270 return(TIFFReadDirEntryErrRange);
3271 else
3272 return(TIFFReadDirEntryErrOk);
3275 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
3277 if (value<0)
3278 return(TIFFReadDirEntryErrRange);
3279 else
3280 return(TIFFReadDirEntryErrOk);
3283 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
3285 if (value<0)
3286 return(TIFFReadDirEntryErrRange);
3287 else
3288 return(TIFFReadDirEntryErrOk);
3291 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
3293 if (value<0)
3294 return(TIFFReadDirEntryErrRange);
3295 else
3296 return(TIFFReadDirEntryErrOk);
3299 static enum TIFFReadDirEntryErr
3300 TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
3302 if (value > UINT32_MAX)
3303 return(TIFFReadDirEntryErrRange);
3304 else
3305 return(TIFFReadDirEntryErrOk);
3308 static enum TIFFReadDirEntryErr
3309 TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
3311 if ((value < 0) || (value > (int64_t) UINT32_MAX))
3312 return(TIFFReadDirEntryErrRange);
3313 else
3314 return(TIFFReadDirEntryErrOk);
3317 static enum TIFFReadDirEntryErr
3318 TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
3320 if (value > 0x7FFFFFFFUL)
3321 return(TIFFReadDirEntryErrRange);
3322 else
3323 return(TIFFReadDirEntryErrOk);
3326 /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3327 static enum TIFFReadDirEntryErr
3328 TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
3330 if (value > 0x7FFFFFFF)
3331 return(TIFFReadDirEntryErrRange);
3332 else
3333 return(TIFFReadDirEntryErrOk);
3336 /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3337 static enum TIFFReadDirEntryErr
3338 TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
3340 if ((value < 0-((int64_t) 0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
3341 return(TIFFReadDirEntryErrRange);
3342 else
3343 return(TIFFReadDirEntryErrOk);
3346 static enum TIFFReadDirEntryErr
3347 TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
3349 if (value < 0)
3350 return(TIFFReadDirEntryErrRange);
3351 else
3352 return(TIFFReadDirEntryErrOk);
3355 static enum TIFFReadDirEntryErr
3356 TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
3358 if (value < 0)
3359 return(TIFFReadDirEntryErrRange);
3360 else
3361 return(TIFFReadDirEntryErrOk);
3364 static enum TIFFReadDirEntryErr
3365 TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
3367 if (value < 0)
3368 return(TIFFReadDirEntryErrRange);
3369 else
3370 return(TIFFReadDirEntryErrOk);
3373 static enum TIFFReadDirEntryErr
3374 TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
3376 if (value < 0)
3377 return(TIFFReadDirEntryErrRange);
3378 else
3379 return(TIFFReadDirEntryErrOk);
3382 static enum TIFFReadDirEntryErr
3383 TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
3385 if (value > INT64_MAX)
3386 return(TIFFReadDirEntryErrRange);
3387 else
3388 return(TIFFReadDirEntryErrOk);
3391 static enum TIFFReadDirEntryErr
3392 TIFFReadDirEntryData(TIFF* tif, uint64_t offset, tmsize_t size, void* dest)
3394 assert(size>0);
3395 if (!isMapped(tif)) {
3396 if (!SeekOK(tif,offset))
3397 return(TIFFReadDirEntryErrIo);
3398 if (!ReadOK(tif,dest,size))
3399 return(TIFFReadDirEntryErrIo);
3400 } else {
3401 size_t ma,mb;
3402 ma=(size_t)offset;
3403 if((uint64_t)ma != offset ||
3404 ma > (~(size_t)0) - (size_t)size )
3406 return TIFFReadDirEntryErrIo;
3408 mb=ma+size;
3409 if (mb > (uint64_t)tif->tif_size)
3410 return(TIFFReadDirEntryErrIo);
3411 _TIFFmemcpy(dest,tif->tif_base+ma,size);
3413 return(TIFFReadDirEntryErrOk);
3416 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
3418 if (!recover) {
3419 switch (err) {
3420 case TIFFReadDirEntryErrCount:
3421 TIFFErrorExt(tif->tif_clientdata, module,
3422 "Incorrect count for \"%s\"",
3423 tagname);
3424 break;
3425 case TIFFReadDirEntryErrType:
3426 TIFFErrorExt(tif->tif_clientdata, module,
3427 "Incompatible type for \"%s\"",
3428 tagname);
3429 break;
3430 case TIFFReadDirEntryErrIo:
3431 TIFFErrorExt(tif->tif_clientdata, module,
3432 "IO error during reading of \"%s\"",
3433 tagname);
3434 break;
3435 case TIFFReadDirEntryErrRange:
3436 TIFFErrorExt(tif->tif_clientdata, module,
3437 "Incorrect value for \"%s\"",
3438 tagname);
3439 break;
3440 case TIFFReadDirEntryErrPsdif:
3441 TIFFErrorExt(tif->tif_clientdata, module,
3442 "Cannot handle different values per sample for \"%s\"",
3443 tagname);
3444 break;
3445 case TIFFReadDirEntryErrSizesan:
3446 TIFFErrorExt(tif->tif_clientdata, module,
3447 "Sanity check on size of \"%s\" value failed",
3448 tagname);
3449 break;
3450 case TIFFReadDirEntryErrAlloc:
3451 TIFFErrorExt(tif->tif_clientdata, module,
3452 "Out of memory reading of \"%s\"",
3453 tagname);
3454 break;
3455 default:
3456 assert(0); /* we should never get here */
3457 break;
3459 } else {
3460 switch (err) {
3461 case TIFFReadDirEntryErrCount:
3462 TIFFWarningExt(tif->tif_clientdata, module,
3463 "Incorrect count for \"%s\"; tag ignored",
3464 tagname);
3465 break;
3466 case TIFFReadDirEntryErrType:
3467 TIFFWarningExt(tif->tif_clientdata, module,
3468 "Incompatible type for \"%s\"; tag ignored",
3469 tagname);
3470 break;
3471 case TIFFReadDirEntryErrIo:
3472 TIFFWarningExt(tif->tif_clientdata, module,
3473 "IO error during reading of \"%s\"; tag ignored",
3474 tagname);
3475 break;
3476 case TIFFReadDirEntryErrRange:
3477 TIFFWarningExt(tif->tif_clientdata, module,
3478 "Incorrect value for \"%s\"; tag ignored",
3479 tagname);
3480 break;
3481 case TIFFReadDirEntryErrPsdif:
3482 TIFFWarningExt(tif->tif_clientdata, module,
3483 "Cannot handle different values per sample for \"%s\"; tag ignored",
3484 tagname);
3485 break;
3486 case TIFFReadDirEntryErrSizesan:
3487 TIFFWarningExt(tif->tif_clientdata, module,
3488 "Sanity check on size of \"%s\" value failed; tag ignored",
3489 tagname);
3490 break;
3491 case TIFFReadDirEntryErrAlloc:
3492 TIFFWarningExt(tif->tif_clientdata, module,
3493 "Out of memory reading of \"%s\"; tag ignored",
3494 tagname);
3495 break;
3496 default:
3497 assert(0); /* we should never get here */
3498 break;
3504 * Return the maximum number of color channels specified for a given photometric
3505 * type. 0 is returned if photometric type isn't supported or no default value
3506 * is defined by the specification.
3508 static int _TIFFGetMaxColorChannels(uint16_t photometric )
3510 switch (photometric) {
3511 case PHOTOMETRIC_PALETTE:
3512 case PHOTOMETRIC_MINISWHITE:
3513 case PHOTOMETRIC_MINISBLACK:
3514 return 1;
3515 case PHOTOMETRIC_YCBCR:
3516 case PHOTOMETRIC_RGB:
3517 case PHOTOMETRIC_CIELAB:
3518 case PHOTOMETRIC_LOGLUV:
3519 case PHOTOMETRIC_ITULAB:
3520 case PHOTOMETRIC_ICCLAB:
3521 return 3;
3522 case PHOTOMETRIC_SEPARATED:
3523 case PHOTOMETRIC_MASK:
3524 return 4;
3525 case PHOTOMETRIC_LOGL:
3526 case PHOTOMETRIC_CFA:
3527 default:
3528 return 0;
3532 static int ByteCountLooksBad(TIFF* tif)
3535 * Assume we have wrong StripByteCount value (in case
3536 * of single strip) in following cases:
3537 * - it is equal to zero along with StripOffset;
3538 * - it is larger than file itself (in case of uncompressed
3539 * image);
3540 * - it is smaller than the size of the bytes per row
3541 * multiplied on the number of rows. The last case should
3542 * not be checked in the case of writing new image,
3543 * because we may do not know the exact strip size
3544 * until the whole image will be written and directory
3545 * dumped out.
3547 uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
3548 uint64_t offset = TIFFGetStrileOffset(tif, 0);
3549 uint64_t filesize;
3551 if( offset == 0 )
3552 return 0;
3553 if (bytecount == 0)
3554 return 1;
3555 if ( tif->tif_dir.td_compression != COMPRESSION_NONE )
3556 return 0;
3557 filesize = TIFFGetFileSize(tif);
3558 if( offset <= filesize && bytecount > filesize - offset )
3559 return 1;
3560 if( tif->tif_mode == O_RDONLY )
3562 uint64_t scanlinesize = TIFFScanlineSize64(tif);
3563 if( tif->tif_dir.td_imagelength > 0 &&
3564 scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength )
3566 return 1;
3568 if( bytecount < scanlinesize * tif->tif_dir.td_imagelength)
3569 return 1;
3571 return 0;
3576 * Read the next TIFF directory from a file and convert it to the internal
3577 * format. We read directories sequentially.
3580 TIFFReadDirectory(TIFF* tif)
3582 static const char module[] = "TIFFReadDirectory";
3583 TIFFDirEntry* dir;
3584 uint16_t dircount;
3585 TIFFDirEntry* dp;
3586 uint16_t di;
3587 const TIFFField* fip;
3588 uint32_t fii=FAILED_FII;
3589 toff_t nextdiroff;
3590 int bitspersample_read = FALSE;
3591 int color_channels;
3593 tif->tif_diroff=tif->tif_nextdiroff;
3594 if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
3595 return 0; /* last offset or bad offset (IFD looping) */
3596 (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
3597 tif->tif_curdir++;
3598 nextdiroff = tif->tif_nextdiroff;
3599 dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
3600 if (!dircount)
3602 TIFFErrorExt(tif->tif_clientdata,module,
3603 "Failed to read directory at offset %" PRIu64, nextdiroff);
3604 return 0;
3606 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
3609 * Mark duplicates of any tag to be ignored (bugzilla 1994)
3610 * to avoid certain pathological problems.
3613 TIFFDirEntry* ma;
3614 uint16_t mb;
3615 for (ma=dir, mb=0; mb<dircount; ma++, mb++)
3617 TIFFDirEntry* na;
3618 uint16_t nb;
3619 for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
3621 if (ma->tdir_tag == na->tdir_tag) {
3622 na->tdir_ignore = TRUE;
3628 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
3629 tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
3630 tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
3632 /* free any old stuff and reinit */
3633 TIFFFreeDirectory(tif);
3634 TIFFDefaultDirectory(tif);
3636 * Electronic Arts writes gray-scale TIFF files
3637 * without a PlanarConfiguration directory entry.
3638 * Thus we setup a default value here, even though
3639 * the TIFF spec says there is no default value.
3641 TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3643 * Setup default value and then make a pass over
3644 * the fields to check type and tag information,
3645 * and to extract info required to size data
3646 * structures. A second pass is made afterwards
3647 * to read in everything not taken in the first pass.
3648 * But we must process the Compression tag first
3649 * in order to merge in codec-private tag definitions (otherwise
3650 * we may get complaints about unknown tags). However, the
3651 * Compression tag may be dependent on the SamplesPerPixel
3652 * tag value because older TIFF specs permitted Compression
3653 * to be written as a SamplesPerPixel-count tag entry.
3654 * Thus if we don't first figure out the correct SamplesPerPixel
3655 * tag value then we may end up ignoring the Compression tag
3656 * value because it has an incorrect count value (if the
3657 * true value of SamplesPerPixel is not 1).
3659 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
3660 if (dp)
3662 if (!TIFFFetchNormalTag(tif,dp,0))
3663 goto bad;
3664 dp->tdir_ignore = TRUE;
3666 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
3667 if (dp)
3670 * The 5.0 spec says the Compression tag has one value, while
3671 * earlier specs say it has one value per sample. Because of
3672 * this, we accept the tag if one value is supplied with either
3673 * count.
3675 uint16_t value;
3676 enum TIFFReadDirEntryErr err;
3677 err=TIFFReadDirEntryShort(tif,dp,&value);
3678 if (err==TIFFReadDirEntryErrCount)
3679 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3680 if (err!=TIFFReadDirEntryErrOk)
3682 TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
3683 goto bad;
3685 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
3686 goto bad;
3687 dp->tdir_ignore = TRUE;
3689 else
3691 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
3692 goto bad;
3695 * First real pass over the directory.
3697 for (di=0, dp=dir; di<dircount; di++, dp++)
3699 if (!dp->tdir_ignore)
3701 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3702 if (fii == FAILED_FII)
3704 TIFFWarningExt(tif->tif_clientdata, module,
3705 "Unknown field with tag %"PRIu16" (0x%"PRIx16") encountered",
3706 dp->tdir_tag,dp->tdir_tag);
3707 /* the following knowingly leaks the
3708 anonymous field structure */
3709 if (!_TIFFMergeFields(tif,
3710 _TIFFCreateAnonField(tif,
3711 dp->tdir_tag,
3712 (TIFFDataType) dp->tdir_type),
3713 1)) {
3714 TIFFWarningExt(tif->tif_clientdata,
3715 module,
3716 "Registering anonymous field with tag %"PRIu16" (0x%"PRIx16") failed",
3717 dp->tdir_tag,
3718 dp->tdir_tag);
3719 dp->tdir_ignore = TRUE;
3720 } else {
3721 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3722 assert(fii != FAILED_FII);
3726 if (!dp->tdir_ignore)
3728 fip=tif->tif_fields[fii];
3729 if (fip->field_bit==FIELD_IGNORE)
3730 dp->tdir_ignore = TRUE;
3731 else
3733 switch (dp->tdir_tag)
3735 case TIFFTAG_STRIPOFFSETS:
3736 case TIFFTAG_STRIPBYTECOUNTS:
3737 case TIFFTAG_TILEOFFSETS:
3738 case TIFFTAG_TILEBYTECOUNTS:
3739 TIFFSetFieldBit(tif,fip->field_bit);
3740 break;
3741 case TIFFTAG_IMAGEWIDTH:
3742 case TIFFTAG_IMAGELENGTH:
3743 case TIFFTAG_IMAGEDEPTH:
3744 case TIFFTAG_TILELENGTH:
3745 case TIFFTAG_TILEWIDTH:
3746 case TIFFTAG_TILEDEPTH:
3747 case TIFFTAG_PLANARCONFIG:
3748 case TIFFTAG_ROWSPERSTRIP:
3749 case TIFFTAG_EXTRASAMPLES:
3750 if (!TIFFFetchNormalTag(tif,dp,0))
3751 goto bad;
3752 dp->tdir_ignore = TRUE;
3753 break;
3754 default:
3755 if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )
3756 dp->tdir_ignore = TRUE;
3757 break;
3763 * XXX: OJPEG hack.
3764 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3765 * c) strip offsets/bytecounts tag are both present and
3766 * d) both contain exactly one value, then we consistently find
3767 * that the buggy implementation of the buggy compression scheme
3768 * matches contig planarconfig best. So we 'fix-up' the tag here
3770 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
3771 (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
3773 if (!_TIFFFillStriles(tif))
3774 goto bad;
3775 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
3776 if ((dp!=0)&&(dp->tdir_count==1))
3778 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
3779 TIFFTAG_STRIPBYTECOUNTS);
3780 if ((dp!=0)&&(dp->tdir_count==1))
3782 tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
3783 TIFFWarningExt(tif->tif_clientdata,module,
3784 "Planarconfig tag value assumed incorrect, "
3785 "assuming data is contig instead of chunky");
3790 * Allocate directory structure and setup defaults.
3792 if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
3794 MissingRequired(tif,"ImageLength");
3795 goto bad;
3798 * Setup appropriate structures (by strip or by tile)
3800 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3801 tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
3802 tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3803 tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
3804 tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
3805 tif->tif_flags &= ~TIFF_ISTILED;
3806 } else {
3807 tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
3808 tif->tif_flags |= TIFF_ISTILED;
3810 if (!tif->tif_dir.td_nstrips) {
3811 TIFFErrorExt(tif->tif_clientdata, module,
3812 "Cannot handle zero number of %s",
3813 isTiled(tif) ? "tiles" : "strips");
3814 goto bad;
3816 tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
3817 if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
3818 tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
3819 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
3820 #ifdef OJPEG_SUPPORT
3821 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
3822 (isTiled(tif)==0) &&
3823 (tif->tif_dir.td_nstrips==1)) {
3825 * XXX: OJPEG hack.
3826 * If a) compression is OJPEG, b) it's not a tiled TIFF,
3827 * and c) the number of strips is 1,
3828 * then we tolerate the absence of stripoffsets tag,
3829 * because, presumably, all required data is in the
3830 * JpegInterchangeFormat stream.
3832 TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
3833 } else
3834 #endif
3836 MissingRequired(tif,
3837 isTiled(tif) ? "TileOffsets" : "StripOffsets");
3838 goto bad;
3842 * Second pass: extract other information.
3844 for (di=0, dp=dir; di<dircount; di++, dp++)
3846 if (!dp->tdir_ignore) {
3847 switch (dp->tdir_tag)
3849 case TIFFTAG_MINSAMPLEVALUE:
3850 case TIFFTAG_MAXSAMPLEVALUE:
3851 case TIFFTAG_BITSPERSAMPLE:
3852 case TIFFTAG_DATATYPE:
3853 case TIFFTAG_SAMPLEFORMAT:
3855 * The MinSampleValue, MaxSampleValue, BitsPerSample
3856 * DataType and SampleFormat tags are supposed to be
3857 * written as one value/sample, but some vendors
3858 * incorrectly write one value only -- so we accept
3859 * that as well (yuck). Other vendors write correct
3860 * value for NumberOfSamples, but incorrect one for
3861 * BitsPerSample and friends, and we will read this
3862 * too.
3865 uint16_t value;
3866 enum TIFFReadDirEntryErr err;
3867 err=TIFFReadDirEntryShort(tif,dp,&value);
3868 if (err==TIFFReadDirEntryErrCount)
3869 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3870 if (err!=TIFFReadDirEntryErrOk)
3872 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3873 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3874 goto bad;
3876 if (!TIFFSetField(tif,dp->tdir_tag,value))
3877 goto bad;
3878 if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )
3879 bitspersample_read = TRUE;
3881 break;
3882 case TIFFTAG_SMINSAMPLEVALUE:
3883 case TIFFTAG_SMAXSAMPLEVALUE:
3886 double *data = NULL;
3887 enum TIFFReadDirEntryErr err;
3888 uint32_t saved_flags;
3889 int m;
3890 if (dp->tdir_count != (uint64_t)tif->tif_dir.td_samplesperpixel)
3891 err = TIFFReadDirEntryErrCount;
3892 else
3893 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
3894 if (err!=TIFFReadDirEntryErrOk)
3896 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3897 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3898 goto bad;
3900 saved_flags = tif->tif_flags;
3901 tif->tif_flags |= TIFF_PERSAMPLE;
3902 m = TIFFSetField(tif,dp->tdir_tag,data);
3903 tif->tif_flags = saved_flags;
3904 _TIFFfree(data);
3905 if (!m)
3906 goto bad;
3908 break;
3909 case TIFFTAG_STRIPOFFSETS:
3910 case TIFFTAG_TILEOFFSETS:
3911 switch( dp->tdir_type )
3913 case TIFF_SHORT:
3914 case TIFF_LONG:
3915 case TIFF_LONG8:
3916 break;
3917 default:
3918 /* Warn except if directory typically created with TIFFDeferStrileArrayWriting() */
3919 if( !(tif->tif_mode == O_RDWR &&
3920 dp->tdir_count == 0 &&
3921 dp->tdir_type == 0 &&
3922 dp->tdir_offset.toff_long8 == 0) )
3924 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3925 TIFFWarningExt(tif->tif_clientdata,module,
3926 "Invalid data type for tag %s",
3927 fip ? fip->field_name : "unknown tagname");
3929 break;
3931 _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
3932 dp, sizeof(TIFFDirEntry) );
3933 break;
3934 case TIFFTAG_STRIPBYTECOUNTS:
3935 case TIFFTAG_TILEBYTECOUNTS:
3936 switch( dp->tdir_type )
3938 case TIFF_SHORT:
3939 case TIFF_LONG:
3940 case TIFF_LONG8:
3941 break;
3942 default:
3943 /* Warn except if directory typically created with TIFFDeferStrileArrayWriting() */
3944 if( !(tif->tif_mode == O_RDWR &&
3945 dp->tdir_count == 0 &&
3946 dp->tdir_type == 0 &&
3947 dp->tdir_offset.toff_long8 == 0) )
3949 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3950 TIFFWarningExt(tif->tif_clientdata,module,
3951 "Invalid data type for tag %s",
3952 fip ? fip->field_name : "unknown tagname");
3954 break;
3956 _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
3957 dp, sizeof(TIFFDirEntry) );
3958 break;
3959 case TIFFTAG_COLORMAP:
3960 case TIFFTAG_TRANSFERFUNCTION:
3962 enum TIFFReadDirEntryErr err;
3963 uint32_t countpersample;
3964 uint32_t countrequired;
3965 uint32_t incrementpersample;
3966 uint16_t* value=NULL;
3967 /* It would be dangerous to instantiate those tag values */
3968 /* since if td_bitspersample has not yet been read (due to */
3969 /* unordered tags), it could be read afterwards with a */
3970 /* values greater than the default one (1), which may cause */
3971 /* crashes in user code */
3972 if( !bitspersample_read )
3974 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3975 TIFFWarningExt(tif->tif_clientdata,module,
3976 "Ignoring %s since BitsPerSample tag not found",
3977 fip ? fip->field_name : "unknown tagname");
3978 continue;
3980 /* ColorMap or TransferFunction for high bit */
3981 /* depths do not make much sense and could be */
3982 /* used as a denial of service vector */
3983 if (tif->tif_dir.td_bitspersample > 24)
3985 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3986 TIFFWarningExt(tif->tif_clientdata,module,
3987 "Ignoring %s because BitsPerSample=%"PRIu16">24",
3988 fip ? fip->field_name : "unknown tagname",
3989 tif->tif_dir.td_bitspersample);
3990 continue;
3992 countpersample=(1U<<tif->tif_dir.td_bitspersample);
3993 if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64_t)countpersample))
3995 countrequired=countpersample;
3996 incrementpersample=0;
3998 else
4000 countrequired=3*countpersample;
4001 incrementpersample=countpersample;
4003 if (dp->tdir_count!=(uint64_t)countrequired)
4004 err=TIFFReadDirEntryErrCount;
4005 else
4006 err=TIFFReadDirEntryShortArray(tif,dp,&value);
4007 if (err!=TIFFReadDirEntryErrOk)
4009 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
4010 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
4012 else
4014 TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
4015 _TIFFfree(value);
4018 break;
4019 /* BEGIN REV 4.0 COMPATIBILITY */
4020 case TIFFTAG_OSUBFILETYPE:
4022 uint16_t valueo;
4023 uint32_t value;
4024 if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
4026 switch (valueo)
4028 case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
4029 case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
4030 default: value=0; break;
4032 if (value!=0)
4033 TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
4036 break;
4037 /* END REV 4.0 COMPATIBILITY */
4038 default:
4039 (void) TIFFFetchNormalTag(tif, dp, TRUE);
4040 break;
4042 } /* -- if (!dp->tdir_ignore) */
4043 } /* -- for-loop -- */
4045 if( tif->tif_mode == O_RDWR &&
4046 tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
4047 tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
4048 tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
4049 tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
4050 tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
4051 tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
4052 tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
4053 tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )
4055 /* Directory typically created with TIFFDeferStrileArrayWriting() */
4056 TIFFSetupStrips(tif);
4058 else if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) )
4060 if( tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 )
4062 if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripoffset_entry),
4063 tif->tif_dir.td_nstrips,
4064 &tif->tif_dir.td_stripoffset_p))
4066 goto bad;
4069 if( tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 )
4071 if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripbytecount_entry),
4072 tif->tif_dir.td_nstrips,
4073 &tif->tif_dir.td_stripbytecount_p))
4075 goto bad;
4081 * OJPEG hack:
4082 * - If a) compression is OJPEG, and b) photometric tag is missing,
4083 * then we consistently find that photometric should be YCbCr
4084 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
4085 * then we consistently find that the buggy implementation of the
4086 * buggy compression scheme matches photometric YCbCr instead.
4087 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
4088 * then we consistently find bitspersample should be 8.
4089 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4090 * and c) photometric is RGB or YCbCr, then we consistently find
4091 * samplesperpixel should be 3
4092 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4093 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
4094 * find samplesperpixel should be 3
4096 if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
4098 if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
4100 TIFFWarningExt(tif->tif_clientdata, module,
4101 "Photometric tag is missing, assuming data is YCbCr");
4102 if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
4103 goto bad;
4105 else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
4107 tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
4108 TIFFWarningExt(tif->tif_clientdata, module,
4109 "Photometric tag value assumed incorrect, "
4110 "assuming data is YCbCr instead of RGB");
4112 if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
4114 TIFFWarningExt(tif->tif_clientdata,module,
4115 "BitsPerSample tag is missing, assuming 8 bits per sample");
4116 if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
4117 goto bad;
4119 if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
4121 if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
4123 TIFFWarningExt(tif->tif_clientdata,module,
4124 "SamplesPerPixel tag is missing, "
4125 "assuming correct SamplesPerPixel value is 3");
4126 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
4127 goto bad;
4129 if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
4131 TIFFWarningExt(tif->tif_clientdata,module,
4132 "SamplesPerPixel tag is missing, "
4133 "applying correct SamplesPerPixel value of 3");
4134 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
4135 goto bad;
4137 else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
4138 || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
4141 * SamplesPerPixel tag is missing, but is not required
4142 * by spec. Assume correct SamplesPerPixel value of 1.
4144 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
4145 goto bad;
4151 * Make sure all non-color channels are extrasamples.
4152 * If it's not the case, define them as such.
4154 color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
4155 if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {
4156 uint16_t old_extrasamples;
4157 uint16_t *new_sampleinfo;
4159 TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "
4160 "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4161 "Defining non-color channels as ExtraSamples.");
4163 old_extrasamples = tif->tif_dir.td_extrasamples;
4164 tif->tif_dir.td_extrasamples = (uint16_t) (tif->tif_dir.td_samplesperpixel - color_channels);
4166 // sampleinfo should contain information relative to these new extra samples
4167 new_sampleinfo = (uint16_t*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16_t));
4168 if (!new_sampleinfo) {
4169 TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "
4170 "temporary new sampleinfo array "
4171 "(%"PRIu16" 16 bit elements)",
4172 tif->tif_dir.td_extrasamples);
4173 goto bad;
4176 memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16_t));
4177 _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);
4178 _TIFFfree(new_sampleinfo);
4182 * Verify Palette image has a Colormap.
4184 if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
4185 !TIFFFieldSet(tif, FIELD_COLORMAP)) {
4186 if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
4187 tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
4188 else if (tif->tif_dir.td_bitspersample>=8)
4189 tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
4190 else {
4191 MissingRequired(tif, "Colormap");
4192 goto bad;
4196 * OJPEG hack:
4197 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
4198 * TIFFs
4200 if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
4203 * Attempt to deal with a missing StripByteCounts tag.
4205 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
4207 * Some manufacturers violate the spec by not giving
4208 * the size of the strips. In this case, assume there
4209 * is one uncompressed strip of data.
4211 if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
4212 tif->tif_dir.td_nstrips > 1) ||
4213 (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
4214 tif->tif_dir.td_nstrips != (uint32_t)tif->tif_dir.td_samplesperpixel)) {
4215 MissingRequired(tif, "StripByteCounts");
4216 goto bad;
4218 TIFFWarningExt(tif->tif_clientdata, module,
4219 "TIFF directory is missing required "
4220 "\"StripByteCounts\" field, calculating from imagelength");
4221 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4222 goto bad;
4224 } else if (tif->tif_dir.td_nstrips == 1
4225 && !(tif->tif_flags&TIFF_ISTILED)
4226 && ByteCountLooksBad(tif)) {
4228 * XXX: Plexus (and others) sometimes give a value of
4229 * zero for a tag when they don't know what the
4230 * correct value is! Try and handle the simple case
4231 * of estimating the size of a one strip image.
4233 TIFFWarningExt(tif->tif_clientdata, module,
4234 "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
4235 if(EstimateStripByteCounts(tif, dir, dircount) < 0)
4236 goto bad;
4238 } else if (!(tif->tif_flags&TIFF_DEFERSTRILELOAD)
4239 && tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
4240 && tif->tif_dir.td_nstrips > 2
4241 && tif->tif_dir.td_compression == COMPRESSION_NONE
4242 && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
4243 && TIFFGetStrileByteCount(tif, 0) != 0
4244 && TIFFGetStrileByteCount(tif, 1) != 0 ) {
4246 * XXX: Some vendors fill StripByteCount array with
4247 * absolutely wrong values (it can be equal to
4248 * StripOffset array, for example). Catch this case
4249 * here.
4251 * We avoid this check if deferring strile loading
4252 * as it would always force us to load the strip/tile
4253 * information.
4255 TIFFWarningExt(tif->tif_clientdata, module,
4256 "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
4257 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4258 goto bad;
4261 if (dir)
4263 _TIFFfree(dir);
4264 dir=NULL;
4266 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
4268 if (tif->tif_dir.td_bitspersample>=16)
4269 tif->tif_dir.td_maxsamplevalue=0xFFFF;
4270 else
4271 tif->tif_dir.td_maxsamplevalue = (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
4274 #ifdef STRIPBYTECOUNTSORTED_UNUSED
4276 * XXX: We can optimize checking for the strip bounds using the sorted
4277 * bytecounts array. See also comments for TIFFAppendToStrip()
4278 * function in tif_write.c.
4280 if (!(tif->tif_flags&TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) {
4281 uint32_t strip;
4283 tif->tif_dir.td_stripbytecountsorted = 1;
4284 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
4285 if (TIFFGetStrileOffset(tif, strip - 1) >
4286 TIFFGetStrileOffset(tif, strip)) {
4287 tif->tif_dir.td_stripbytecountsorted = 0;
4288 break;
4292 #endif
4295 * An opportunity for compression mode dependent tag fixup
4297 (*tif->tif_fixuptags)(tif);
4300 * Some manufacturers make life difficult by writing
4301 * large amounts of uncompressed data as a single strip.
4302 * This is contrary to the recommendations of the spec.
4303 * The following makes an attempt at breaking such images
4304 * into strips closer to the recommended 8k bytes. A
4305 * side effect, however, is that the RowsPerStrip tag
4306 * value may be changed.
4308 if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
4309 (tif->tif_dir.td_nstrips==1)&&
4310 (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
4311 ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
4313 ChopUpSingleUncompressedStrip(tif);
4316 /* There are also uncompressed striped files with strips larger than */
4317 /* 2 GB, which make them unfriendly with a lot of code. If possible, */
4318 /* try to expose smaller "virtual" strips. */
4319 if( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
4320 tif->tif_dir.td_compression == COMPRESSION_NONE &&
4321 (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP &&
4322 TIFFStripSize64(tif) > 0x7FFFFFFFUL )
4324 TryChopUpUncompressedBigTiff(tif);
4328 * Clear the dirty directory flag.
4330 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
4331 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
4334 * Reinitialize i/o since we are starting on a new directory.
4336 tif->tif_row = (uint32_t) -1;
4337 tif->tif_curstrip = (uint32_t) -1;
4338 tif->tif_col = (uint32_t) -1;
4339 tif->tif_curtile = (uint32_t) -1;
4340 tif->tif_tilesize = (tmsize_t) -1;
4342 tif->tif_scanlinesize = TIFFScanlineSize(tif);
4343 if (!tif->tif_scanlinesize) {
4344 TIFFErrorExt(tif->tif_clientdata, module,
4345 "Cannot handle zero scanline size");
4346 return (0);
4349 if (isTiled(tif)) {
4350 tif->tif_tilesize = TIFFTileSize(tif);
4351 if (!tif->tif_tilesize) {
4352 TIFFErrorExt(tif->tif_clientdata, module,
4353 "Cannot handle zero tile size");
4354 return (0);
4356 } else {
4357 if (!TIFFStripSize(tif)) {
4358 TIFFErrorExt(tif->tif_clientdata, module,
4359 "Cannot handle zero strip size");
4360 return (0);
4363 return (1);
4364 bad:
4365 if (dir)
4366 _TIFFfree(dir);
4367 return (0);
4370 static void
4371 TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16_t dircount)
4373 static const char module[] = "TIFFReadDirectoryCheckOrder";
4374 uint16_t m;
4375 uint16_t n;
4376 TIFFDirEntry* o;
4377 m=0;
4378 for (n=0, o=dir; n<dircount; n++, o++)
4380 if (o->tdir_tag<m)
4382 TIFFWarningExt(tif->tif_clientdata,module,
4383 "Invalid TIFF directory; tags are not sorted in ascending order");
4384 break;
4386 m=o->tdir_tag+1;
4390 static TIFFDirEntry*
4391 TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16_t dircount, uint16_t tagid)
4393 TIFFDirEntry* m;
4394 uint16_t n;
4395 (void) tif;
4396 for (m=dir, n=0; n<dircount; m++, n++)
4398 if (m->tdir_tag==tagid)
4399 return(m);
4401 return(0);
4404 static void
4405 TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16_t tagid, uint32_t* fii)
4407 int32_t ma,mb,mc;
4408 ma=-1;
4409 mc=(int32_t)tif->tif_nfields;
4410 while (1)
4412 if (ma+1==mc)
4414 *fii = FAILED_FII;
4415 return;
4417 mb=(ma+mc)/2;
4418 if (tif->tif_fields[mb]->field_tag==(uint32_t)tagid)
4419 break;
4420 if (tif->tif_fields[mb]->field_tag<(uint32_t)tagid)
4421 ma=mb;
4422 else
4423 mc=mb;
4425 while (1)
4427 if (mb==0)
4428 break;
4429 if (tif->tif_fields[mb-1]->field_tag!=(uint32_t)tagid)
4430 break;
4431 mb--;
4433 *fii=mb;
4437 * Read custom directory from the arbitrary offset.
4438 * The code is very similar to TIFFReadDirectory().
4441 TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
4442 const TIFFFieldArray* infoarray)
4444 static const char module[] = "TIFFReadCustomDirectory";
4445 TIFFDirEntry* dir;
4446 uint16_t dircount;
4447 TIFFDirEntry* dp;
4448 uint16_t di;
4449 const TIFFField* fip;
4450 uint32_t fii;
4451 (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
4452 _TIFFSetupFields(tif, infoarray);
4453 dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
4454 if (!dircount)
4456 TIFFErrorExt(tif->tif_clientdata,module,
4457 "Failed to read custom directory at offset %" PRIu64,diroff);
4458 return 0;
4460 TIFFFreeDirectory(tif);
4461 _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
4462 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
4463 for (di=0, dp=dir; di<dircount; di++, dp++)
4465 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4466 if (fii == FAILED_FII)
4468 TIFFWarningExt(tif->tif_clientdata, module,
4469 "Unknown field with tag %"PRIu16" (0x%"PRIx16") encountered",
4470 dp->tdir_tag, dp->tdir_tag);
4471 if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
4472 dp->tdir_tag,
4473 (TIFFDataType) dp->tdir_type),
4474 1)) {
4475 TIFFWarningExt(tif->tif_clientdata, module,
4476 "Registering anonymous field with tag %"PRIu16" (0x%"PRIx16") failed",
4477 dp->tdir_tag, dp->tdir_tag);
4478 dp->tdir_ignore = TRUE;
4479 } else {
4480 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4481 assert( fii != FAILED_FII );
4484 if (!dp->tdir_ignore)
4486 fip=tif->tif_fields[fii];
4487 if (fip->field_bit==FIELD_IGNORE)
4488 dp->tdir_ignore = TRUE;
4489 else
4491 /* check data type */
4492 while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
4494 fii++;
4495 if ((fii==tif->tif_nfields)||
4496 (tif->tif_fields[fii]->field_tag!=(uint32_t)dp->tdir_tag))
4498 fii=0xFFFF;
4499 break;
4501 fip=tif->tif_fields[fii];
4503 if (fii==0xFFFF)
4505 TIFFWarningExt(tif->tif_clientdata, module,
4506 "Wrong data type %"PRIu16" for \"%s\"; tag ignored",
4507 dp->tdir_type,fip->field_name);
4508 dp->tdir_ignore = TRUE;
4510 else
4512 /* check count if known in advance */
4513 if ((fip->field_readcount!=TIFF_VARIABLE)&&
4514 (fip->field_readcount!=TIFF_VARIABLE2))
4516 uint32_t expected;
4517 if (fip->field_readcount==TIFF_SPP)
4518 expected=(uint32_t)tif->tif_dir.td_samplesperpixel;
4519 else
4520 expected=(uint32_t)fip->field_readcount;
4521 if (!CheckDirCount(tif,dp,expected))
4522 dp->tdir_ignore = TRUE;
4526 if (!dp->tdir_ignore) {
4527 switch (dp->tdir_tag)
4529 case EXIFTAG_SUBJECTDISTANCE:
4530 (void)TIFFFetchSubjectDistance(tif, dp);
4531 break;
4532 default:
4533 (void)TIFFFetchNormalTag(tif, dp, TRUE);
4534 break;
4536 } /*-- if (!dp->tdir_ignore) */
4539 if (dir)
4540 _TIFFfree(dir);
4541 return 1;
4545 * EXIF is important special case of custom IFD, so we have a special
4546 * function to read it.
4549 TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
4551 const TIFFFieldArray* exifFieldArray;
4552 exifFieldArray = _TIFFGetExifFields();
4553 return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
4557 *--: EXIF-GPS custom directory reading as another special case of custom IFD.
4560 TIFFReadGPSDirectory(TIFF* tif, toff_t diroff)
4562 const TIFFFieldArray* gpsFieldArray;
4563 gpsFieldArray = _TIFFGetGpsFields();
4564 return TIFFReadCustomDirectory(tif, diroff, gpsFieldArray);
4567 static int
4568 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16_t dircount)
4570 static const char module[] = "EstimateStripByteCounts";
4572 TIFFDirEntry *dp;
4573 TIFFDirectory *td = &tif->tif_dir;
4574 uint32_t strip;
4576 /* Do not try to load stripbytecount as we will compute it */
4577 if( !_TIFFFillStrilesInternal( tif, 0 ) )
4578 return -1;
4580 if (td->td_stripbytecount_p)
4581 _TIFFfree(td->td_stripbytecount_p);
4582 td->td_stripbytecount_p = (uint64_t*)
4583 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64_t),
4584 "for \"StripByteCounts\" array");
4585 if( td->td_stripbytecount_p == NULL )
4586 return -1;
4588 if (td->td_compression != COMPRESSION_NONE) {
4589 uint64_t space;
4590 uint64_t filesize;
4591 uint16_t n;
4592 filesize = TIFFGetFileSize(tif);
4593 if (!(tif->tif_flags&TIFF_BIGTIFF))
4594 space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
4595 else
4596 space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
4597 /* calculate amount of space used by indirect values */
4598 for (dp = dir, n = dircount; n > 0; n--, dp++)
4600 uint32_t typewidth;
4601 uint64_t datasize;
4602 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4603 if (typewidth == 0) {
4604 TIFFErrorExt(tif->tif_clientdata, module,
4605 "Cannot determine size of unknown tag type %"PRIu16,
4606 dp->tdir_type);
4607 return -1;
4609 if( dp->tdir_count > UINT64_MAX / typewidth )
4610 return -1;
4611 datasize= (uint64_t)typewidth * dp->tdir_count;
4612 if (!(tif->tif_flags&TIFF_BIGTIFF))
4614 if (datasize<=4)
4615 datasize=0;
4617 else
4619 if (datasize<=8)
4620 datasize=0;
4622 if( space > UINT64_MAX - datasize )
4623 return -1;
4624 space+=datasize;
4626 if( filesize < space )
4627 /* we should perhaps return in error ? */
4628 space = filesize;
4629 else
4630 space = filesize - space;
4631 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
4632 space /= td->td_samplesperpixel;
4633 for (strip = 0; strip < td->td_nstrips; strip++)
4634 td->td_stripbytecount_p[strip] = space;
4636 * This gross hack handles the case were the offset to
4637 * the last strip is past the place where we think the strip
4638 * should begin. Since a strip of data must be contiguous,
4639 * it's safe to assume that we've overestimated the amount
4640 * of data in the strip and trim this number back accordingly.
4642 strip--;
4643 if (td->td_stripoffset_p[strip] > UINT64_MAX - td->td_stripbytecount_p[strip])
4644 return -1;
4645 if (td->td_stripoffset_p[strip]+td->td_stripbytecount_p[strip] > filesize) {
4646 if( td->td_stripoffset_p[strip] >= filesize ) {
4647 /* Not sure what we should in that case... */
4648 td->td_stripbytecount_p[strip] = 0;
4649 } else {
4650 td->td_stripbytecount_p[strip] = filesize - td->td_stripoffset_p[strip];
4653 } else if (isTiled(tif)) {
4654 uint64_t bytespertile = TIFFTileSize64(tif);
4656 for (strip = 0; strip < td->td_nstrips; strip++)
4657 td->td_stripbytecount_p[strip] = bytespertile;
4658 } else {
4659 uint64_t rowbytes = TIFFScanlineSize64(tif);
4660 uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
4661 for (strip = 0; strip < td->td_nstrips; strip++)
4663 if( rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes )
4664 return -1;
4665 td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
4668 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
4669 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
4670 td->td_rowsperstrip = td->td_imagelength;
4671 return 1;
4674 static void
4675 MissingRequired(TIFF* tif, const char* tagname)
4677 static const char module[] = "MissingRequired";
4679 TIFFErrorExt(tif->tif_clientdata, module,
4680 "TIFF directory is missing required \"%s\" field",
4681 tagname);
4685 * Check the directory offset against the list of already seen directory
4686 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4687 * file with looped directory pointers. We will maintain a list of already
4688 * seen directories and check every IFD offset against that list.
4690 static int
4691 TIFFCheckDirOffset(TIFF* tif, uint64_t diroff)
4693 uint16_t n;
4695 if (diroff == 0) /* no more directories */
4696 return 0;
4697 if (tif->tif_dirnumber == 65535) {
4698 TIFFErrorExt(tif->tif_clientdata, "TIFFCheckDirOffset",
4699 "Cannot handle more than 65535 TIFF directories");
4700 return 0;
4703 for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
4704 if (tif->tif_dirlist[n] == diroff)
4705 return 0;
4708 tif->tif_dirnumber++;
4710 if (tif->tif_dirlist == NULL || tif->tif_dirnumber > tif->tif_dirlistsize) {
4711 uint64_t* new_dirlist;
4714 * XXX: Reduce memory allocation granularity of the dirlist
4715 * array.
4717 new_dirlist = (uint64_t*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
4718 tif->tif_dirnumber, 2 * sizeof(uint64_t), "for IFD list");
4719 if (!new_dirlist)
4720 return 0;
4721 if( tif->tif_dirnumber >= 32768 )
4722 tif->tif_dirlistsize = 65535;
4723 else
4724 tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
4725 tif->tif_dirlist = new_dirlist;
4728 tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
4730 return 1;
4734 * Check the count field of a directory entry against a known value. The
4735 * caller is expected to skip/ignore the tag if there is a mismatch.
4737 static int
4738 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32_t count)
4740 if ((uint64_t)count > dir->tdir_count) {
4741 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4742 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4743 "incorrect count for field \"%s\" (%" PRIu64 ", expecting %"PRIu32"); tag ignored",
4744 fip ? fip->field_name : "unknown tagname",
4745 dir->tdir_count, count);
4746 return (0);
4747 } else if ((uint64_t)count < dir->tdir_count) {
4748 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4749 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4750 "incorrect count for field \"%s\" (%" PRIu64 ", expecting %"PRIu32"); tag trimmed",
4751 fip ? fip->field_name : "unknown tagname",
4752 dir->tdir_count, count);
4753 dir->tdir_count = count;
4754 return (1);
4756 return (1);
4760 * Read IFD structure from the specified offset. If the pointer to
4761 * nextdiroff variable has been specified, read it too. Function returns a
4762 * number of fields in the directory or 0 if failed.
4764 static uint16_t
4765 TIFFFetchDirectory(TIFF* tif, uint64_t diroff, TIFFDirEntry** pdir,
4766 uint64_t *nextdiroff)
4768 static const char module[] = "TIFFFetchDirectory";
4770 void* origdir;
4771 uint16_t dircount16;
4772 uint32_t dirsize;
4773 TIFFDirEntry* dir;
4774 uint8_t* ma;
4775 TIFFDirEntry* mb;
4776 uint16_t n;
4778 assert(pdir);
4780 tif->tif_diroff = diroff;
4781 if (nextdiroff)
4782 *nextdiroff = 0;
4783 if (!isMapped(tif)) {
4784 if (!SeekOK(tif, tif->tif_diroff)) {
4785 TIFFErrorExt(tif->tif_clientdata, module,
4786 "%s: Seek error accessing TIFF directory",
4787 tif->tif_name);
4788 return 0;
4790 if (!(tif->tif_flags&TIFF_BIGTIFF))
4792 if (!ReadOK(tif, &dircount16, sizeof (uint16_t))) {
4793 TIFFErrorExt(tif->tif_clientdata, module,
4794 "%s: Can not read TIFF directory count",
4795 tif->tif_name);
4796 return 0;
4798 if (tif->tif_flags & TIFF_SWAB)
4799 TIFFSwabShort(&dircount16);
4800 if (dircount16>4096)
4802 TIFFErrorExt(tif->tif_clientdata, module,
4803 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4804 return 0;
4806 dirsize = 12;
4807 } else {
4808 uint64_t dircount64;
4809 if (!ReadOK(tif, &dircount64, sizeof (uint64_t))) {
4810 TIFFErrorExt(tif->tif_clientdata, module,
4811 "%s: Can not read TIFF directory count",
4812 tif->tif_name);
4813 return 0;
4815 if (tif->tif_flags & TIFF_SWAB)
4816 TIFFSwabLong8(&dircount64);
4817 if (dircount64>4096)
4819 TIFFErrorExt(tif->tif_clientdata, module,
4820 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4821 return 0;
4823 dircount16 = (uint16_t)dircount64;
4824 dirsize = 20;
4826 origdir = _TIFFCheckMalloc(tif, dircount16,
4827 dirsize, "to read TIFF directory");
4828 if (origdir == NULL)
4829 return 0;
4830 if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
4831 TIFFErrorExt(tif->tif_clientdata, module,
4832 "%.100s: Can not read TIFF directory",
4833 tif->tif_name);
4834 _TIFFfree(origdir);
4835 return 0;
4838 * Read offset to next directory for sequential scans if
4839 * needed.
4841 if (nextdiroff)
4843 if (!(tif->tif_flags&TIFF_BIGTIFF))
4845 uint32_t nextdiroff32;
4846 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
4847 nextdiroff32 = 0;
4848 if (tif->tif_flags&TIFF_SWAB)
4849 TIFFSwabLong(&nextdiroff32);
4850 *nextdiroff=nextdiroff32;
4851 } else {
4852 if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
4853 *nextdiroff = 0;
4854 if (tif->tif_flags&TIFF_SWAB)
4855 TIFFSwabLong8(nextdiroff);
4858 } else {
4859 tmsize_t m;
4860 tmsize_t off;
4861 if (tif->tif_diroff > (uint64_t)INT64_MAX)
4863 TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
4864 return(0);
4866 off = (tmsize_t) tif->tif_diroff;
4869 * Check for integer overflow when validating the dir_off,
4870 * otherwise a very high offset may cause an OOB read and
4871 * crash the client. Make two comparisons instead of
4873 * off + sizeof(uint16_t) > tif->tif_size
4875 * to avoid overflow.
4877 if (!(tif->tif_flags&TIFF_BIGTIFF))
4879 m=off+sizeof(uint16_t);
4880 if ((m<off) || (m<(tmsize_t)sizeof(uint16_t)) || (m > tif->tif_size)) {
4881 TIFFErrorExt(tif->tif_clientdata, module,
4882 "Can not read TIFF directory count");
4883 return 0;
4884 } else {
4885 _TIFFmemcpy(&dircount16, tif->tif_base + off,
4886 sizeof(uint16_t));
4888 off += sizeof (uint16_t);
4889 if (tif->tif_flags & TIFF_SWAB)
4890 TIFFSwabShort(&dircount16);
4891 if (dircount16>4096)
4893 TIFFErrorExt(tif->tif_clientdata, module,
4894 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4895 return 0;
4897 dirsize = 12;
4899 else
4901 uint64_t dircount64;
4902 m=off+sizeof(uint64_t);
4903 if ((m<off) || (m<(tmsize_t)sizeof(uint64_t)) || (m > tif->tif_size)) {
4904 TIFFErrorExt(tif->tif_clientdata, module,
4905 "Can not read TIFF directory count");
4906 return 0;
4907 } else {
4908 _TIFFmemcpy(&dircount64, tif->tif_base + off,
4909 sizeof(uint64_t));
4911 off += sizeof (uint64_t);
4912 if (tif->tif_flags & TIFF_SWAB)
4913 TIFFSwabLong8(&dircount64);
4914 if (dircount64>4096)
4916 TIFFErrorExt(tif->tif_clientdata, module,
4917 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4918 return 0;
4920 dircount16 = (uint16_t)dircount64;
4921 dirsize = 20;
4923 if (dircount16 == 0 )
4925 TIFFErrorExt(tif->tif_clientdata, module,
4926 "Sanity check on directory count failed, zero tag directories not supported");
4927 return 0;
4929 origdir = _TIFFCheckMalloc(tif, dircount16,
4930 dirsize,
4931 "to read TIFF directory");
4932 if (origdir == NULL)
4933 return 0;
4934 m=off+dircount16*dirsize;
4935 if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
4936 TIFFErrorExt(tif->tif_clientdata, module,
4937 "Can not read TIFF directory");
4938 _TIFFfree(origdir);
4939 return 0;
4940 } else {
4941 _TIFFmemcpy(origdir, tif->tif_base + off,
4942 dircount16 * dirsize);
4944 if (nextdiroff) {
4945 off += dircount16 * dirsize;
4946 if (!(tif->tif_flags&TIFF_BIGTIFF))
4948 uint32_t nextdiroff32;
4949 m=off+sizeof(uint32_t);
4950 if ((m<off) || (m<(tmsize_t)sizeof(uint32_t)) || (m > tif->tif_size))
4951 nextdiroff32 = 0;
4952 else
4953 _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
4954 sizeof (uint32_t));
4955 if (tif->tif_flags&TIFF_SWAB)
4956 TIFFSwabLong(&nextdiroff32);
4957 *nextdiroff = nextdiroff32;
4959 else
4961 m=off+sizeof(uint64_t);
4962 if ((m<off) || (m<(tmsize_t)sizeof(uint64_t)) || (m > tif->tif_size))
4963 *nextdiroff = 0;
4964 else
4965 _TIFFmemcpy(nextdiroff, tif->tif_base + off,
4966 sizeof (uint64_t));
4967 if (tif->tif_flags&TIFF_SWAB)
4968 TIFFSwabLong8(nextdiroff);
4972 dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
4973 sizeof(TIFFDirEntry),
4974 "to read TIFF directory");
4975 if (dir==0)
4977 _TIFFfree(origdir);
4978 return 0;
4980 ma=(uint8_t*)origdir;
4981 mb=dir;
4982 for (n=0; n<dircount16; n++)
4984 mb->tdir_ignore = FALSE;
4985 if (tif->tif_flags&TIFF_SWAB)
4986 TIFFSwabShort((uint16_t*)ma);
4987 mb->tdir_tag=*(uint16_t*)ma;
4988 ma+=sizeof(uint16_t);
4989 if (tif->tif_flags&TIFF_SWAB)
4990 TIFFSwabShort((uint16_t*)ma);
4991 mb->tdir_type=*(uint16_t*)ma;
4992 ma+=sizeof(uint16_t);
4993 if (!(tif->tif_flags&TIFF_BIGTIFF))
4995 if (tif->tif_flags&TIFF_SWAB)
4996 TIFFSwabLong((uint32_t*)ma);
4997 mb->tdir_count=(uint64_t)(*(uint32_t*)ma);
4998 ma+=sizeof(uint32_t);
4999 mb->tdir_offset.toff_long8=0;
5000 *(uint32_t*)(&mb->tdir_offset)=*(uint32_t*)ma;
5001 ma+=sizeof(uint32_t);
5003 else
5005 if (tif->tif_flags&TIFF_SWAB)
5006 TIFFSwabLong8((uint64_t*)ma);
5007 mb->tdir_count=TIFFReadUInt64(ma);
5008 ma+=sizeof(uint64_t);
5009 mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
5010 ma+=sizeof(uint64_t);
5012 mb++;
5014 _TIFFfree(origdir);
5015 *pdir = dir;
5016 return dircount16;
5020 * Fetch a tag that is not handled by special case code.
5022 static int
5023 TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
5025 static const char module[] = "TIFFFetchNormalTag";
5026 enum TIFFReadDirEntryErr err;
5027 uint32_t fii;
5028 const TIFFField* fip = NULL;
5029 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
5030 if( fii == FAILED_FII )
5032 TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
5033 "No definition found for tag %"PRIu16,
5034 dp->tdir_tag);
5035 return 0;
5037 fip=tif->tif_fields[fii];
5038 assert(fip != NULL); /* should not happen */
5039 assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
5040 assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
5041 err=TIFFReadDirEntryErrOk;
5042 switch (fip->set_field_type)
5044 case TIFF_SETGET_UNDEFINED:
5045 break;
5046 case TIFF_SETGET_ASCII:
5048 uint8_t* data;
5049 assert(fip->field_passcount==0);
5050 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5051 if (err==TIFFReadDirEntryErrOk)
5053 uint32_t mb = 0;
5054 int n;
5055 if (data != NULL)
5057 uint8_t* ma = data;
5058 while (mb<(uint32_t)dp->tdir_count)
5060 if (*ma==0)
5061 break;
5062 ma++;
5063 mb++;
5066 if (mb+1<(uint32_t)dp->tdir_count)
5067 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);
5068 else if (mb+1>(uint32_t)dp->tdir_count)
5070 uint8_t* o;
5071 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
5072 if ((uint32_t)dp->tdir_count + 1 != dp->tdir_count + 1)
5073 o=NULL;
5074 else
5075 o=_TIFFmalloc((uint32_t)dp->tdir_count + 1);
5076 if (o==NULL)
5078 if (data!=NULL)
5079 _TIFFfree(data);
5080 return(0);
5082 _TIFFmemcpy(o,data,(uint32_t)dp->tdir_count);
5083 o[(uint32_t)dp->tdir_count]=0;
5084 if (data!=0)
5085 _TIFFfree(data);
5086 data=o;
5088 n=TIFFSetField(tif,dp->tdir_tag,data);
5089 if (data!=0)
5090 _TIFFfree(data);
5091 if (!n)
5092 return(0);
5095 break;
5096 case TIFF_SETGET_UINT8:
5098 uint8_t data=0;
5099 assert(fip->field_readcount==1);
5100 assert(fip->field_passcount==0);
5101 err=TIFFReadDirEntryByte(tif,dp,&data);
5102 if (err==TIFFReadDirEntryErrOk)
5104 if (!TIFFSetField(tif,dp->tdir_tag,data))
5105 return(0);
5108 break;
5109 case TIFF_SETGET_UINT16:
5111 uint16_t data;
5112 assert(fip->field_readcount==1);
5113 assert(fip->field_passcount==0);
5114 err=TIFFReadDirEntryShort(tif,dp,&data);
5115 if (err==TIFFReadDirEntryErrOk)
5117 if (!TIFFSetField(tif,dp->tdir_tag,data))
5118 return(0);
5121 break;
5122 case TIFF_SETGET_UINT32:
5124 uint32_t data;
5125 assert(fip->field_readcount==1);
5126 assert(fip->field_passcount==0);
5127 err=TIFFReadDirEntryLong(tif,dp,&data);
5128 if (err==TIFFReadDirEntryErrOk)
5130 if (!TIFFSetField(tif,dp->tdir_tag,data))
5131 return(0);
5134 break;
5135 case TIFF_SETGET_UINT64:
5137 uint64_t data;
5138 assert(fip->field_readcount==1);
5139 assert(fip->field_passcount==0);
5140 err=TIFFReadDirEntryLong8(tif,dp,&data);
5141 if (err==TIFFReadDirEntryErrOk)
5143 if (!TIFFSetField(tif,dp->tdir_tag,data))
5144 return(0);
5147 break;
5148 case TIFF_SETGET_FLOAT:
5150 float data;
5151 assert(fip->field_readcount==1);
5152 assert(fip->field_passcount==0);
5153 err=TIFFReadDirEntryFloat(tif,dp,&data);
5154 if (err==TIFFReadDirEntryErrOk)
5156 if (!TIFFSetField(tif,dp->tdir_tag,data))
5157 return(0);
5160 break;
5161 case TIFF_SETGET_DOUBLE:
5163 double data;
5164 assert(fip->field_readcount==1);
5165 assert(fip->field_passcount==0);
5166 err=TIFFReadDirEntryDouble(tif,dp,&data);
5167 if (err==TIFFReadDirEntryErrOk)
5169 if (!TIFFSetField(tif,dp->tdir_tag,data))
5170 return(0);
5173 break;
5174 case TIFF_SETGET_IFD8:
5176 uint64_t data;
5177 assert(fip->field_readcount==1);
5178 assert(fip->field_passcount==0);
5179 err=TIFFReadDirEntryIfd8(tif,dp,&data);
5180 if (err==TIFFReadDirEntryErrOk)
5182 if (!TIFFSetField(tif,dp->tdir_tag,data))
5183 return(0);
5186 break;
5187 case TIFF_SETGET_UINT16_PAIR:
5189 uint16_t* data;
5190 assert(fip->field_readcount==2);
5191 assert(fip->field_passcount==0);
5192 if (dp->tdir_count!=2) {
5193 TIFFWarningExt(tif->tif_clientdata,module,
5194 "incorrect count for field \"%s\", expected 2, got %"PRIu64,
5195 fip->field_name, dp->tdir_count);
5196 return(0);
5198 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5199 if (err==TIFFReadDirEntryErrOk)
5201 int m;
5202 assert(data); /* avoid CLang static Analyzer false positive */
5203 m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
5204 _TIFFfree(data);
5205 if (!m)
5206 return(0);
5209 break;
5210 case TIFF_SETGET_C0_UINT8:
5212 uint8_t* data;
5213 assert(fip->field_readcount>=1);
5214 assert(fip->field_passcount==0);
5215 if (dp->tdir_count!=(uint64_t)fip->field_readcount) {
5216 TIFFWarningExt(tif->tif_clientdata,module,
5217 "incorrect count for field \"%s\", expected %d, got %"PRIu64,
5218 fip->field_name,(int) fip->field_readcount, dp->tdir_count);
5219 return 0;
5221 else
5223 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5224 if (err==TIFFReadDirEntryErrOk)
5226 int m;
5227 m=TIFFSetField(tif,dp->tdir_tag,data);
5228 if (data!=0)
5229 _TIFFfree(data);
5230 if (!m)
5231 return(0);
5235 break;
5236 case TIFF_SETGET_C0_UINT16:
5238 uint16_t* data;
5239 assert(fip->field_readcount>=1);
5240 assert(fip->field_passcount==0);
5241 if (dp->tdir_count!=(uint64_t)fip->field_readcount)
5242 /* corrupt file */;
5243 else
5245 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5246 if (err==TIFFReadDirEntryErrOk)
5248 int m;
5249 m=TIFFSetField(tif,dp->tdir_tag,data);
5250 if (data!=0)
5251 _TIFFfree(data);
5252 if (!m)
5253 return(0);
5257 break;
5258 case TIFF_SETGET_C0_UINT32:
5260 uint32_t* data;
5261 assert(fip->field_readcount>=1);
5262 assert(fip->field_passcount==0);
5263 if (dp->tdir_count!=(uint64_t)fip->field_readcount)
5264 /* corrupt file */;
5265 else
5267 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5268 if (err==TIFFReadDirEntryErrOk)
5270 int m;
5271 m=TIFFSetField(tif,dp->tdir_tag,data);
5272 if (data!=0)
5273 _TIFFfree(data);
5274 if (!m)
5275 return(0);
5279 break;
5280 case TIFF_SETGET_C0_FLOAT:
5282 float* data;
5283 assert(fip->field_readcount>=1);
5284 assert(fip->field_passcount==0);
5285 if (dp->tdir_count!=(uint64_t)fip->field_readcount)
5286 /* corrupt file */;
5287 else
5289 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5290 if (err==TIFFReadDirEntryErrOk)
5292 int m;
5293 m=TIFFSetField(tif,dp->tdir_tag,data);
5294 if (data!=0)
5295 _TIFFfree(data);
5296 if (!m)
5297 return(0);
5301 break;
5302 /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read into Double-Arrays. */
5303 case TIFF_SETGET_C0_DOUBLE:
5305 double* data;
5306 assert(fip->field_readcount>=1);
5307 assert(fip->field_passcount==0);
5308 if (dp->tdir_count!=(uint64_t)fip->field_readcount)
5309 /* corrupt file */;
5310 else
5312 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5313 if (err==TIFFReadDirEntryErrOk)
5315 int m;
5316 m=TIFFSetField(tif,dp->tdir_tag,data);
5317 if (data!=0)
5318 _TIFFfree(data);
5319 if (!m)
5320 return(0);
5324 break;
5325 case TIFF_SETGET_C16_ASCII:
5327 uint8_t* data;
5328 assert(fip->field_readcount==TIFF_VARIABLE);
5329 assert(fip->field_passcount==1);
5330 if (dp->tdir_count>0xFFFF)
5331 err=TIFFReadDirEntryErrCount;
5332 else
5334 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5335 if (err==TIFFReadDirEntryErrOk)
5337 int m;
5338 if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
5340 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
5341 data[dp->tdir_count-1] = '\0';
5343 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5344 if (data!=0)
5345 _TIFFfree(data);
5346 if (!m)
5347 return(0);
5351 break;
5352 case TIFF_SETGET_C16_UINT8:
5354 uint8_t* data;
5355 assert(fip->field_readcount==TIFF_VARIABLE);
5356 assert(fip->field_passcount==1);
5357 if (dp->tdir_count>0xFFFF)
5358 err=TIFFReadDirEntryErrCount;
5359 else
5361 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5362 if (err==TIFFReadDirEntryErrOk)
5364 int m;
5365 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5366 if (data!=0)
5367 _TIFFfree(data);
5368 if (!m)
5369 return(0);
5373 break;
5374 case TIFF_SETGET_C16_UINT16:
5376 uint16_t* data;
5377 assert(fip->field_readcount==TIFF_VARIABLE);
5378 assert(fip->field_passcount==1);
5379 if (dp->tdir_count>0xFFFF)
5380 err=TIFFReadDirEntryErrCount;
5381 else
5383 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5384 if (err==TIFFReadDirEntryErrOk)
5386 int m;
5387 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5388 if (data!=0)
5389 _TIFFfree(data);
5390 if (!m)
5391 return(0);
5395 break;
5396 case TIFF_SETGET_C16_UINT32:
5398 uint32_t* data;
5399 assert(fip->field_readcount==TIFF_VARIABLE);
5400 assert(fip->field_passcount==1);
5401 if (dp->tdir_count>0xFFFF)
5402 err=TIFFReadDirEntryErrCount;
5403 else
5405 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5406 if (err==TIFFReadDirEntryErrOk)
5408 int m;
5409 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5410 if (data!=0)
5411 _TIFFfree(data);
5412 if (!m)
5413 return(0);
5417 break;
5418 case TIFF_SETGET_C16_UINT64:
5420 uint64_t* data;
5421 assert(fip->field_readcount==TIFF_VARIABLE);
5422 assert(fip->field_passcount==1);
5423 if (dp->tdir_count>0xFFFF)
5424 err=TIFFReadDirEntryErrCount;
5425 else
5427 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5428 if (err==TIFFReadDirEntryErrOk)
5430 int m;
5431 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5432 if (data!=0)
5433 _TIFFfree(data);
5434 if (!m)
5435 return(0);
5439 break;
5440 case TIFF_SETGET_C16_FLOAT:
5442 float* data;
5443 assert(fip->field_readcount==TIFF_VARIABLE);
5444 assert(fip->field_passcount==1);
5445 if (dp->tdir_count>0xFFFF)
5446 err=TIFFReadDirEntryErrCount;
5447 else
5449 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5450 if (err==TIFFReadDirEntryErrOk)
5452 int m;
5453 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5454 if (data!=0)
5455 _TIFFfree(data);
5456 if (!m)
5457 return(0);
5461 break;
5462 case TIFF_SETGET_C16_DOUBLE:
5464 double* data;
5465 assert(fip->field_readcount==TIFF_VARIABLE);
5466 assert(fip->field_passcount==1);
5467 if (dp->tdir_count>0xFFFF)
5468 err=TIFFReadDirEntryErrCount;
5469 else
5471 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5472 if (err==TIFFReadDirEntryErrOk)
5474 int m;
5475 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5476 if (data!=0)
5477 _TIFFfree(data);
5478 if (!m)
5479 return(0);
5483 break;
5484 case TIFF_SETGET_C16_IFD8:
5486 uint64_t* data;
5487 assert(fip->field_readcount==TIFF_VARIABLE);
5488 assert(fip->field_passcount==1);
5489 if (dp->tdir_count>0xFFFF)
5490 err=TIFFReadDirEntryErrCount;
5491 else
5493 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5494 if (err==TIFFReadDirEntryErrOk)
5496 int m;
5497 m=TIFFSetField(tif, dp->tdir_tag, (uint16_t)(dp->tdir_count), data);
5498 if (data!=0)
5499 _TIFFfree(data);
5500 if (!m)
5501 return(0);
5505 break;
5506 case TIFF_SETGET_C32_ASCII:
5508 uint8_t* data;
5509 assert(fip->field_readcount==TIFF_VARIABLE2);
5510 assert(fip->field_passcount==1);
5511 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5512 if (err==TIFFReadDirEntryErrOk)
5514 int m;
5515 if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
5517 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
5518 data[dp->tdir_count-1] = '\0';
5520 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5521 if (data!=0)
5522 _TIFFfree(data);
5523 if (!m)
5524 return(0);
5527 break;
5528 case TIFF_SETGET_C32_UINT8:
5530 uint8_t* data;
5531 assert(fip->field_readcount==TIFF_VARIABLE2);
5532 assert(fip->field_passcount==1);
5533 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5534 if (err==TIFFReadDirEntryErrOk)
5536 int m;
5537 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5538 if (data!=0)
5539 _TIFFfree(data);
5540 if (!m)
5541 return(0);
5544 break;
5545 case TIFF_SETGET_C32_SINT8:
5547 int8_t* data = NULL;
5548 assert(fip->field_readcount==TIFF_VARIABLE2);
5549 assert(fip->field_passcount==1);
5550 err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
5551 if (err==TIFFReadDirEntryErrOk)
5553 int m;
5554 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5555 if (data!=0)
5556 _TIFFfree(data);
5557 if (!m)
5558 return(0);
5561 break;
5562 case TIFF_SETGET_C32_UINT16:
5564 uint16_t* data;
5565 assert(fip->field_readcount==TIFF_VARIABLE2);
5566 assert(fip->field_passcount==1);
5567 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5568 if (err==TIFFReadDirEntryErrOk)
5570 int m;
5571 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5572 if (data!=0)
5573 _TIFFfree(data);
5574 if (!m)
5575 return(0);
5578 break;
5579 case TIFF_SETGET_C32_SINT16:
5581 int16_t* data = NULL;
5582 assert(fip->field_readcount==TIFF_VARIABLE2);
5583 assert(fip->field_passcount==1);
5584 err=TIFFReadDirEntrySshortArray(tif,dp,&data);
5585 if (err==TIFFReadDirEntryErrOk)
5587 int m;
5588 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5589 if (data!=0)
5590 _TIFFfree(data);
5591 if (!m)
5592 return(0);
5595 break;
5596 case TIFF_SETGET_C32_UINT32:
5598 uint32_t* data;
5599 assert(fip->field_readcount==TIFF_VARIABLE2);
5600 assert(fip->field_passcount==1);
5601 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5602 if (err==TIFFReadDirEntryErrOk)
5604 int m;
5605 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5606 if (data!=0)
5607 _TIFFfree(data);
5608 if (!m)
5609 return(0);
5612 break;
5613 case TIFF_SETGET_C32_SINT32:
5615 int32_t* data = NULL;
5616 assert(fip->field_readcount==TIFF_VARIABLE2);
5617 assert(fip->field_passcount==1);
5618 err=TIFFReadDirEntrySlongArray(tif,dp,&data);
5619 if (err==TIFFReadDirEntryErrOk)
5621 int m;
5622 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5623 if (data!=0)
5624 _TIFFfree(data);
5625 if (!m)
5626 return(0);
5629 break;
5630 case TIFF_SETGET_C32_UINT64:
5632 uint64_t* data;
5633 assert(fip->field_readcount==TIFF_VARIABLE2);
5634 assert(fip->field_passcount==1);
5635 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5636 if (err==TIFFReadDirEntryErrOk)
5638 int m;
5639 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5640 if (data!=0)
5641 _TIFFfree(data);
5642 if (!m)
5643 return(0);
5646 break;
5647 case TIFF_SETGET_C32_SINT64:
5649 int64_t* data = NULL;
5650 assert(fip->field_readcount==TIFF_VARIABLE2);
5651 assert(fip->field_passcount==1);
5652 err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
5653 if (err==TIFFReadDirEntryErrOk)
5655 int m;
5656 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5657 if (data!=0)
5658 _TIFFfree(data);
5659 if (!m)
5660 return(0);
5663 break;
5664 case TIFF_SETGET_C32_FLOAT:
5666 float* data;
5667 assert(fip->field_readcount==TIFF_VARIABLE2);
5668 assert(fip->field_passcount==1);
5669 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5670 if (err==TIFFReadDirEntryErrOk)
5672 int m;
5673 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5674 if (data!=0)
5675 _TIFFfree(data);
5676 if (!m)
5677 return(0);
5680 break;
5681 case TIFF_SETGET_C32_DOUBLE:
5683 double* data;
5684 assert(fip->field_readcount==TIFF_VARIABLE2);
5685 assert(fip->field_passcount==1);
5686 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5687 if (err==TIFFReadDirEntryErrOk)
5689 int m;
5690 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5691 if (data!=0)
5692 _TIFFfree(data);
5693 if (!m)
5694 return(0);
5697 break;
5698 case TIFF_SETGET_C32_IFD8:
5700 uint64_t* data;
5701 assert(fip->field_readcount==TIFF_VARIABLE2);
5702 assert(fip->field_passcount==1);
5703 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5704 if (err==TIFFReadDirEntryErrOk)
5706 int m;
5707 m=TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), data);
5708 if (data!=0)
5709 _TIFFfree(data);
5710 if (!m)
5711 return(0);
5714 break;
5715 default:
5716 assert(0); /* we should never get here */
5717 break;
5719 if (err!=TIFFReadDirEntryErrOk)
5721 TIFFReadDirEntryOutputErr(tif,err,module,fip->field_name,recover);
5722 return(0);
5724 return(1);
5728 * Fetch a set of offsets or lengths.
5729 * While this routine says "strips", in fact it's also used for tiles.
5731 static int
5732 TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32_t nstrips, uint64_t** lpp)
5734 static const char module[] = "TIFFFetchStripThing";
5735 enum TIFFReadDirEntryErr err;
5736 uint64_t* data;
5737 err=TIFFReadDirEntryLong8ArrayWithLimit(tif,dir,&data,nstrips);
5738 if (err!=TIFFReadDirEntryErrOk)
5740 const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5741 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
5742 return(0);
5744 if (dir->tdir_count<(uint64_t)nstrips)
5746 uint64_t* resizeddata;
5747 const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5748 const char* pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
5749 uint32_t max_nstrips = 1000000;
5750 if( pszMax )
5751 max_nstrips = (uint32_t) atoi(pszMax);
5752 TIFFReadDirEntryOutputErr(tif,TIFFReadDirEntryErrCount,
5753 module,
5754 fip ? fip->field_name : "unknown tagname",
5755 ( nstrips <= max_nstrips ) );
5757 if( nstrips > max_nstrips )
5759 _TIFFfree(data);
5760 return(0);
5763 resizeddata=(uint64_t*)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t), "for strip array");
5764 if (resizeddata==0) {
5765 _TIFFfree(data);
5766 return(0);
5768 _TIFFmemcpy(resizeddata,data, (uint32_t)dir->tdir_count * sizeof(uint64_t));
5769 _TIFFmemset(resizeddata+(uint32_t)dir->tdir_count, 0, (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
5770 _TIFFfree(data);
5771 data=resizeddata;
5773 *lpp=data;
5774 return(1);
5778 * Fetch and set the SubjectDistance EXIF tag.
5780 static int
5781 TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
5783 static const char module[] = "TIFFFetchSubjectDistance";
5784 enum TIFFReadDirEntryErr err;
5785 UInt64Aligned_t m;
5786 m.l=0;
5787 assert(sizeof(double)==8);
5788 assert(sizeof(uint64_t) == 8);
5789 assert(sizeof(uint32_t) == 4);
5790 if (dir->tdir_count!=1)
5791 err=TIFFReadDirEntryErrCount;
5792 else if (dir->tdir_type!=TIFF_RATIONAL)
5793 err=TIFFReadDirEntryErrType;
5794 else
5796 if (!(tif->tif_flags&TIFF_BIGTIFF))
5798 uint32_t offset;
5799 offset=*(uint32_t*)(&dir->tdir_offset);
5800 if (tif->tif_flags&TIFF_SWAB)
5801 TIFFSwabLong(&offset);
5802 err=TIFFReadDirEntryData(tif,offset,8,m.i);
5804 else
5806 m.l=dir->tdir_offset.toff_long8;
5807 err=TIFFReadDirEntryErrOk;
5810 if (err==TIFFReadDirEntryErrOk)
5812 double n;
5813 if (tif->tif_flags&TIFF_SWAB)
5814 TIFFSwabArrayOfLong(m.i,2);
5815 if (m.i[0]==0)
5816 n=0.0;
5817 else if (m.i[0]==0xFFFFFFFF || m.i[1]==0)
5819 * XXX: Numerator 0xFFFFFFFF means that we have infinite
5820 * distance. Indicate that with a negative floating point
5821 * SubjectDistance value.
5823 n=-1.0;
5824 else
5825 n=(double)m.i[0]/(double)m.i[1];
5826 return(TIFFSetField(tif,dir->tdir_tag,n));
5828 else
5830 TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
5831 return(0);
5835 static void allocChoppedUpStripArrays(TIFF* tif, uint32_t nstrips,
5836 uint64_t stripbytes, uint32_t rowsperstrip)
5838 TIFFDirectory *td = &tif->tif_dir;
5839 uint64_t bytecount;
5840 uint64_t offset;
5841 uint64_t last_offset;
5842 uint64_t last_bytecount;
5843 uint32_t i;
5844 uint64_t *newcounts;
5845 uint64_t *newoffsets;
5847 offset = TIFFGetStrileOffset(tif, 0);
5848 last_offset = TIFFGetStrileOffset(tif, td->td_nstrips-1);
5849 last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips-1);
5850 if( last_offset > UINT64_MAX - last_bytecount ||
5851 last_offset + last_bytecount < offset )
5853 return;
5855 bytecount = last_offset + last_bytecount - offset;
5857 newcounts = (uint64_t*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64_t),
5858 "for chopped \"StripByteCounts\" array");
5859 newoffsets = (uint64_t*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64_t),
5860 "for chopped \"StripOffsets\" array");
5861 if (newcounts == NULL || newoffsets == NULL) {
5863 * Unable to allocate new strip information, give up and use
5864 * the original one strip information.
5866 if (newcounts != NULL)
5867 _TIFFfree(newcounts);
5868 if (newoffsets != NULL)
5869 _TIFFfree(newoffsets);
5870 return;
5874 * Fill the strip information arrays with new bytecounts and offsets
5875 * that reflect the broken-up format.
5877 for (i = 0; i < nstrips; i++)
5879 if (stripbytes > bytecount)
5880 stripbytes = bytecount;
5881 newcounts[i] = stripbytes;
5882 newoffsets[i] = stripbytes ? offset : 0;
5883 offset += stripbytes;
5884 bytecount -= stripbytes;
5888 * Replace old single strip info with multi-strip info.
5890 td->td_stripsperimage = td->td_nstrips = nstrips;
5891 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
5893 _TIFFfree(td->td_stripbytecount_p);
5894 _TIFFfree(td->td_stripoffset_p);
5895 td->td_stripbytecount_p = newcounts;
5896 td->td_stripoffset_p = newoffsets;
5897 #ifdef STRIPBYTECOUNTSORTED_UNUSED
5898 td->td_stripbytecountsorted = 1;
5899 #endif
5900 tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
5905 * Replace a single strip (tile) of uncompressed data by multiple strips
5906 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5907 * dealing with large images or for dealing with machines with a limited
5908 * amount memory.
5910 static void
5911 ChopUpSingleUncompressedStrip(TIFF* tif)
5913 register TIFFDirectory *td = &tif->tif_dir;
5914 uint64_t bytecount;
5915 uint64_t offset;
5916 uint32_t rowblock;
5917 uint64_t rowblockbytes;
5918 uint64_t stripbytes;
5919 uint32_t nstrips;
5920 uint32_t rowsperstrip;
5922 bytecount = TIFFGetStrileByteCount(tif, 0);
5923 /* On a newly created file, just re-opened to be filled, we */
5924 /* don't want strip chop to trigger as it is going to cause issues */
5925 /* later ( StripOffsets and StripByteCounts improperly filled) . */
5926 if( bytecount == 0 && tif->tif_mode != O_RDONLY )
5927 return;
5928 offset = TIFFGetStrileByteCount(tif, 0);
5929 assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
5930 if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
5931 (!isUpSampled(tif)))
5932 rowblock = td->td_ycbcrsubsampling[1];
5933 else
5934 rowblock = 1;
5935 rowblockbytes = TIFFVTileSize64(tif, rowblock);
5937 * Make the rows hold at least one scanline, but fill specified amount
5938 * of data if possible.
5940 if (rowblockbytes > STRIP_SIZE_DEFAULT) {
5941 stripbytes = rowblockbytes;
5942 rowsperstrip = rowblock;
5943 } else if (rowblockbytes > 0 ) {
5944 uint32_t rowblocksperstrip;
5945 rowblocksperstrip = (uint32_t) (STRIP_SIZE_DEFAULT / rowblockbytes);
5946 rowsperstrip = rowblocksperstrip * rowblock;
5947 stripbytes = rowblocksperstrip * rowblockbytes;
5949 else
5950 return;
5953 * never increase the number of rows per strip
5955 if (rowsperstrip >= td->td_rowsperstrip)
5956 return;
5957 nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
5958 if( nstrips == 0 )
5959 return;
5961 /* If we are going to allocate a lot of memory, make sure that the */
5962 /* file is as big as needed */
5963 if( tif->tif_mode == O_RDONLY &&
5964 nstrips > 1000000 &&
5965 (offset >= TIFFGetFileSize(tif) ||
5966 stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)) )
5968 return;
5971 allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
5976 * Replace a file with contiguous strips > 2 GB of uncompressed data by
5977 * multiple smaller strips. This is useful for
5978 * dealing with large images or for dealing with machines with a limited
5979 * amount memory.
5981 static void TryChopUpUncompressedBigTiff( TIFF* tif )
5983 TIFFDirectory *td = &tif->tif_dir;
5984 uint32_t rowblock;
5985 uint64_t rowblockbytes;
5986 uint32_t i;
5987 uint64_t stripsize;
5988 uint32_t rowblocksperstrip;
5989 uint32_t rowsperstrip;
5990 uint64_t stripbytes;
5991 uint32_t nstrips;
5993 stripsize = TIFFStripSize64(tif);
5995 assert( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG );
5996 assert( tif->tif_dir.td_compression == COMPRESSION_NONE );
5997 assert( (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP );
5998 assert( stripsize > 0x7FFFFFFFUL );
6000 /* On a newly created file, just re-opened to be filled, we */
6001 /* don't want strip chop to trigger as it is going to cause issues */
6002 /* later ( StripOffsets and StripByteCounts improperly filled) . */
6003 if( TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY )
6004 return;
6006 if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
6007 (!isUpSampled(tif)))
6008 rowblock = td->td_ycbcrsubsampling[1];
6009 else
6010 rowblock = 1;
6011 rowblockbytes = TIFFVStripSize64(tif, rowblock);
6012 if( rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL )
6014 /* In case of file with gigantic width */
6015 return;
6018 /* Check that the strips are contiguous and of the expected size */
6019 for( i = 0; i < td->td_nstrips; i++ )
6021 if( i == td->td_nstrips - 1 )
6023 if( TIFFGetStrileByteCount(tif, i) < TIFFVStripSize64(
6024 tif, td->td_imagelength - i * td->td_rowsperstrip ) )
6026 return;
6029 else
6031 if( TIFFGetStrileByteCount(tif, i) != stripsize )
6033 return;
6035 if( i > 0 && TIFFGetStrileOffset(tif, i) !=
6036 TIFFGetStrileOffset(tif, i-1) + TIFFGetStrileByteCount(tif, i-1) )
6038 return;
6043 /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
6044 rowblocksperstrip = (uint32_t) (512 * 1024 * 1024 / rowblockbytes);
6045 if( rowblocksperstrip == 0 )
6046 rowblocksperstrip = 1;
6047 rowsperstrip = rowblocksperstrip * rowblock;
6048 stripbytes = rowblocksperstrip * rowblockbytes;
6049 assert( stripbytes <= 0x7FFFFFFFUL );
6051 nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
6052 if( nstrips == 0 )
6053 return;
6055 /* If we are going to allocate a lot of memory, make sure that the */
6056 /* file is as big as needed */
6057 if( tif->tif_mode == O_RDONLY &&
6058 nstrips > 1000000 )
6060 uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
6061 uint64_t filesize = TIFFGetFileSize(tif);
6062 uint64_t last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
6063 if( last_offset > filesize ||
6064 last_bytecount > filesize - last_offset )
6066 return;
6070 allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
6074 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
6075 static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
6077 return a + b;
6080 /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
6081 * strip/tile of number strile. Also fetch the neighbouring values using a
6082 * 4096 byte page size.
6084 static
6085 int _TIFFPartialReadStripArray(TIFF* tif, TIFFDirEntry* dirent,
6086 int strile, uint64_t* panVals )
6088 static const char module[] = "_TIFFPartialReadStripArray";
6089 #define IO_CACHE_PAGE_SIZE 4096
6091 size_t sizeofval;
6092 const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
6093 int sizeofvalint;
6094 uint64_t nBaseOffset;
6095 uint64_t nOffset;
6096 uint64_t nOffsetStartPage;
6097 uint64_t nOffsetEndPage;
6098 tmsize_t nToRead;
6099 tmsize_t nRead;
6100 uint64_t nLastStripOffset;
6101 int iStartBefore;
6102 int i;
6103 const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
6104 unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
6106 assert( dirent->tdir_count > 4 );
6108 if( dirent->tdir_type == TIFF_SHORT )
6110 sizeofval = sizeof(uint16_t);
6112 else if( dirent->tdir_type == TIFF_LONG )
6114 sizeofval = sizeof(uint32_t);
6116 else if( dirent->tdir_type == TIFF_LONG8 )
6118 sizeofval = sizeof(uint64_t);
6120 else if( dirent->tdir_type == TIFF_SLONG8 )
6122 /* Non conformant but used by some images as in */
6123 /* https://github.com/OSGeo/gdal/issues/2165 */
6124 sizeofval = sizeof(int64_t);
6126 else
6128 TIFFErrorExt(tif->tif_clientdata, module,
6129 "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
6130 panVals[strile] = 0;
6131 return 0;
6133 sizeofvalint = (int)(sizeofval);
6135 if( tif->tif_flags&TIFF_BIGTIFF )
6137 uint64_t offset = dirent->tdir_offset.toff_long8;
6138 if( bSwab )
6139 TIFFSwabLong8(&offset);
6140 nBaseOffset = offset;
6142 else
6144 uint32_t offset = dirent->tdir_offset.toff_long;
6145 if( bSwab )
6146 TIFFSwabLong(&offset);
6147 nBaseOffset = offset;
6149 /* To avoid later unsigned integer overflows */
6150 if( nBaseOffset > (uint64_t)INT64_MAX )
6152 TIFFErrorExt(tif->tif_clientdata, module,
6153 "Cannot read offset/size for strile %d", strile);
6154 panVals[strile] = 0;
6155 return 0;
6157 nOffset = nBaseOffset + sizeofval * strile;
6158 nOffsetStartPage =
6159 (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
6160 nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
6162 if( nOffset + sizeofval > nOffsetEndPage )
6163 nOffsetEndPage += IO_CACHE_PAGE_SIZE;
6164 #undef IO_CACHE_PAGE_SIZE
6166 nLastStripOffset = nBaseOffset + arraySize * sizeofval;
6167 if( nLastStripOffset < nOffsetEndPage )
6168 nOffsetEndPage = nLastStripOffset;
6169 if( nOffsetStartPage >= nOffsetEndPage )
6171 TIFFErrorExt(tif->tif_clientdata, module,
6172 "Cannot read offset/size for strile %d", strile);
6173 panVals[strile] = 0;
6174 return 0;
6176 if (!SeekOK(tif,nOffsetStartPage))
6178 panVals[strile] = 0;
6179 return 0;
6182 nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
6183 nRead = TIFFReadFile(tif, buffer, nToRead);
6184 if( nRead < nToRead )
6186 TIFFErrorExt(tif->tif_clientdata, module,
6187 "Cannot read offset/size for strile around ~%d", strile);
6188 return 0;
6190 iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
6191 if( strile + iStartBefore < 0 )
6192 iStartBefore = -strile;
6193 for( i = iStartBefore;
6194 (uint32_t)(strile + i) < arraySize &&
6195 _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <= nOffsetEndPage;
6196 ++i )
6198 if( dirent->tdir_type == TIFF_SHORT )
6200 uint16_t val;
6201 memcpy(&val,
6202 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
6203 sizeof(val));
6204 if( bSwab )
6205 TIFFSwabShort(&val);
6206 panVals[strile + i] = val;
6208 else if( dirent->tdir_type == TIFF_LONG )
6210 uint32_t val;
6211 memcpy(&val,
6212 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
6213 sizeof(val));
6214 if( bSwab )
6215 TIFFSwabLong(&val);
6216 panVals[strile + i] = val;
6218 else if( dirent->tdir_type == TIFF_LONG8 )
6220 uint64_t val;
6221 memcpy(&val,
6222 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
6223 sizeof(val));
6224 if( bSwab )
6225 TIFFSwabLong8(&val);
6226 panVals[strile + i] = val;
6228 else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
6230 /* Non conformant data type */
6231 int64_t val;
6232 memcpy(&val,
6233 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
6234 sizeof(val));
6235 if( bSwab )
6236 TIFFSwabLong8((uint64_t*) &val);
6237 panVals[strile + i] = (uint64_t) val;
6240 return 1;
6243 static int _TIFFFetchStrileValue(TIFF* tif,
6244 uint32_t strile,
6245 TIFFDirEntry* dirent,
6246 uint64_t** parray)
6248 static const char module[] = "_TIFFFetchStrileValue";
6249 TIFFDirectory *td = &tif->tif_dir;
6250 if( strile >= dirent->tdir_count )
6252 return 0;
6254 if( strile >= td->td_stripoffsetbyteallocsize )
6256 uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
6257 uint32_t nStripArrayAllocNew;
6258 uint64_t nArraySize64;
6259 size_t nArraySize;
6260 uint64_t* offsetArray;
6261 uint64_t* bytecountArray;
6263 if( strile > 1000000 )
6265 uint64_t filesize = TIFFGetFileSize(tif);
6266 /* Avoid excessive memory allocation attempt */
6267 /* For such a big blockid we need at least a TIFF_LONG per strile */
6268 /* for the offset array. */
6269 if( strile > filesize / sizeof(uint32_t) )
6271 TIFFErrorExt(tif->tif_clientdata, module, "File too short");
6272 return 0;
6276 if( td->td_stripoffsetbyteallocsize == 0 &&
6277 td->td_nstrips < 1024 * 1024 )
6279 nStripArrayAllocNew = td->td_nstrips;
6281 else
6283 #define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))
6284 #define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))
6285 nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );
6286 if( nStripArrayAllocNew < 0xFFFFFFFFU / 2 )
6287 nStripArrayAllocNew *= 2;
6288 nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
6290 assert( strile < nStripArrayAllocNew );
6291 nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
6292 nArraySize = (size_t)(nArraySize64);
6293 #if SIZEOF_SIZE_T == 4
6294 if( nArraySize != nArraySize64 )
6296 TIFFErrorExt(tif->tif_clientdata, module,
6297 "Cannot allocate strip offset and bytecount arrays");
6298 return 0;
6300 #endif
6301 offsetArray = (uint64_t*)(
6302 _TIFFrealloc( td->td_stripoffset_p, nArraySize ) );
6303 bytecountArray = (uint64_t*)(
6304 _TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );
6305 if( offsetArray )
6306 td->td_stripoffset_p = offsetArray;
6307 if( bytecountArray )
6308 td->td_stripbytecount_p = bytecountArray;
6309 if( offsetArray && bytecountArray )
6311 td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
6312 /* Initialize new entries to ~0 / -1 */
6313 memset(td->td_stripoffset_p + nStripArrayAllocBefore,
6314 0xFF,
6315 (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64_t) );
6316 memset(td->td_stripbytecount_p + nStripArrayAllocBefore,
6317 0xFF,
6318 (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64_t) );
6320 else
6322 TIFFErrorExt(tif->tif_clientdata, module,
6323 "Cannot allocate strip offset and bytecount arrays");
6324 _TIFFfree(td->td_stripoffset_p);
6325 td->td_stripoffset_p = NULL;
6326 _TIFFfree(td->td_stripbytecount_p);
6327 td->td_stripbytecount_p = NULL;
6328 td->td_stripoffsetbyteallocsize = 0;
6331 if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )
6332 return 0;
6334 if( ~((*parray)[strile]) == 0 )
6336 if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )
6338 (*parray)[strile] = 0;
6339 return 0;
6343 return 1;
6346 static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
6347 TIFFDirEntry* dirent,
6348 uint64_t** parray,
6349 int *pbErr)
6351 TIFFDirectory *td = &tif->tif_dir;
6352 if( pbErr )
6353 *pbErr = 0;
6354 if( (tif->tif_flags&TIFF_DEFERSTRILELOAD) && !(tif->tif_flags&TIFF_CHOPPEDUPARRAYS) )
6356 if( !(tif->tif_flags&TIFF_LAZYSTRILELOAD) ||
6357 /* If the values may fit in the toff_long/toff_long8 member */
6358 /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
6359 dirent->tdir_count <= 4 )
6361 if( !_TIFFFillStriles(tif) )
6363 if( pbErr )
6364 *pbErr = 1;
6365 /* Do not return, as we want this function to always */
6366 /* return the same value if called several times with */
6367 /* the same arguments */
6370 else
6372 if( !_TIFFFetchStrileValue(tif, strile, dirent, parray) )
6374 if( pbErr )
6375 *pbErr = 1;
6376 return 0;
6380 if( *parray == NULL || strile >= td->td_nstrips )
6382 if( pbErr )
6383 *pbErr = 1;
6384 return 0;
6386 return (*parray)[strile];
6389 /* Return the value of the TileOffsets/StripOffsets array for the specified tile/strile */
6390 uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
6392 return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
6395 /* Return the value of the TileOffsets/StripOffsets array for the specified tile/strile */
6396 uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
6398 TIFFDirectory *td = &tif->tif_dir;
6399 return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
6400 &(td->td_stripoffset_entry),
6401 &(td->td_stripoffset_p), pbErr);
6404 /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
6405 uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
6407 return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
6410 /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
6411 uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
6413 TIFFDirectory *td = &tif->tif_dir;
6414 return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
6415 &(td->td_stripbytecount_entry),
6416 &(td->td_stripbytecount_p), pbErr);
6420 int _TIFFFillStriles( TIFF *tif )
6422 return _TIFFFillStrilesInternal( tif, 1 );
6425 static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount )
6427 register TIFFDirectory *td = &tif->tif_dir;
6428 int return_value = 1;
6430 /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
6431 if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) || (tif->tif_flags&TIFF_CHOPPEDUPARRAYS) != 0 )
6432 return 1;
6434 if( tif->tif_flags&TIFF_LAZYSTRILELOAD )
6436 /* In case of lazy loading, reload completely the arrays */
6437 _TIFFfree(td->td_stripoffset_p);
6438 _TIFFfree(td->td_stripbytecount_p);
6439 td->td_stripoffset_p = NULL;
6440 td->td_stripbytecount_p = NULL;
6441 td->td_stripoffsetbyteallocsize = 0;
6442 tif->tif_flags &= ~TIFF_LAZYSTRILELOAD;
6445 /* If stripoffset array is already loaded, exit with success */
6446 if( td->td_stripoffset_p != NULL )
6447 return 1;
6449 /* If tdir_count was canceled, then we already got there, but in error */
6450 if( td->td_stripoffset_entry.tdir_count == 0 )
6451 return 0;
6453 if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
6454 td->td_nstrips,&td->td_stripoffset_p))
6456 return_value = 0;
6459 if (loadStripByteCount &&
6460 !TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
6461 td->td_nstrips,&td->td_stripbytecount_p))
6463 return_value = 0;
6466 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
6467 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
6469 #ifdef STRIPBYTECOUNTSORTED_UNUSED
6470 if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
6471 uint32_t strip;
6473 tif->tif_dir.td_stripbytecountsorted = 1;
6474 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
6475 if (tif->tif_dir.td_stripoffset_p[strip - 1] >
6476 tif->tif_dir.td_stripoffset_p[strip]) {
6477 tif->tif_dir.td_stripbytecountsorted = 0;
6478 break;
6482 #endif
6484 return return_value;
6488 /* vim: set ts=8 sts=8 sw=8 noet: */
6490 * Local Variables:
6491 * mode: c
6492 * c-basic-offset: 8
6493 * fill-column: 78
6494 * End: