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
28 * Directory Read Support Routines.
31 /* Suggested pending improvements:
32 * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
33 * eliminating current use of the IGNORE value, and therefore eliminating
34 * current irrational behaviour on tags with tag id code 0
35 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
36 * the pointer to the appropriate TIFFField structure early on in
37 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
44 #define IGNORE 0 /* tag placeholder used below */
45 #define FAILED_FII ((uint32) -1)
48 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
49 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
51 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
52 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
55 enum TIFFReadDirEntryErr {
56 TIFFReadDirEntryErrOk = 0,
57 TIFFReadDirEntryErrCount = 1,
58 TIFFReadDirEntryErrType = 2,
59 TIFFReadDirEntryErrIo = 3,
60 TIFFReadDirEntryErrRange = 4,
61 TIFFReadDirEntryErrPsdif = 5,
62 TIFFReadDirEntryErrSizesan = 6,
63 TIFFReadDirEntryErrAlloc = 7,
66 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
67 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
68 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
69 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
70 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
71 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
72 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
74 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
75 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
76 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
77 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
78 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
79 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
80 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
81 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
82 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
83 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
84 static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
85 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
87 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
89 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
92 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
93 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
94 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
95 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
96 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
97 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
98 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
99 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
100 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
101 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
102 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
103 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
105 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
106 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
107 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
108 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
109 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
110 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
111 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
113 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
114 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
115 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
116 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
117 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
118 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
119 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
121 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
122 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
123 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
124 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
125 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
126 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
128 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
129 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
130 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
131 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
132 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
134 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
135 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
136 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
137 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
138 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
140 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
141 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
142 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
144 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
145 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
146 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
147 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
149 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
151 static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
152 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
154 static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
155 static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
156 static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
158 static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
159 static void MissingRequired(TIFF*, const char*);
160 static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
161 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
162 static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
163 static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
164 static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
165 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
166 static void ChopUpSingleUncompressedStrip(TIFF*);
167 static uint64 TIFFReadUInt64(const uint8 *value);
168 static int _TIFFGetMaxColorChannels(uint16 photometric);
170 static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
172 typedef union _UInt64Aligned_t
182 Unaligned safe copy of a uint64 value from an octet array.
184 static uint64 TIFFReadUInt64(const uint8 *value)
186 UInt64Aligned_t result;
188 result.c[0]=value[0];
189 result.c[1]=value[1];
190 result.c[2]=value[2];
191 result.c[3]=value[3];
192 result.c[4]=value[4];
193 result.c[5]=value[5];
194 result.c[6]=value[6];
195 result.c[7]=value[7];
200 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
202 enum TIFFReadDirEntryErr err;
203 if (direntry->tdir_count!=1)
204 return(TIFFReadDirEntryErrCount);
205 switch (direntry->tdir_type)
208 TIFFReadDirEntryCheckedByte(tif,direntry,value);
209 return(TIFFReadDirEntryErrOk);
213 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
214 err=TIFFReadDirEntryCheckRangeByteSbyte(m);
215 if (err!=TIFFReadDirEntryErrOk)
218 return(TIFFReadDirEntryErrOk);
223 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
224 err=TIFFReadDirEntryCheckRangeByteShort(m);
225 if (err!=TIFFReadDirEntryErrOk)
228 return(TIFFReadDirEntryErrOk);
233 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
234 err=TIFFReadDirEntryCheckRangeByteSshort(m);
235 if (err!=TIFFReadDirEntryErrOk)
238 return(TIFFReadDirEntryErrOk);
243 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
244 err=TIFFReadDirEntryCheckRangeByteLong(m);
245 if (err!=TIFFReadDirEntryErrOk)
248 return(TIFFReadDirEntryErrOk);
253 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
254 err=TIFFReadDirEntryCheckRangeByteSlong(m);
255 if (err!=TIFFReadDirEntryErrOk)
258 return(TIFFReadDirEntryErrOk);
263 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
264 if (err!=TIFFReadDirEntryErrOk)
266 err=TIFFReadDirEntryCheckRangeByteLong8(m);
267 if (err!=TIFFReadDirEntryErrOk)
270 return(TIFFReadDirEntryErrOk);
275 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
276 if (err!=TIFFReadDirEntryErrOk)
278 err=TIFFReadDirEntryCheckRangeByteSlong8(m);
279 if (err!=TIFFReadDirEntryErrOk)
282 return(TIFFReadDirEntryErrOk);
285 return(TIFFReadDirEntryErrType);
289 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
291 enum TIFFReadDirEntryErr err;
292 if (direntry->tdir_count!=1)
293 return(TIFFReadDirEntryErrCount);
294 switch (direntry->tdir_type)
299 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
301 return(TIFFReadDirEntryErrOk);
306 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
307 err=TIFFReadDirEntryCheckRangeShortSbyte(m);
308 if (err!=TIFFReadDirEntryErrOk)
311 return(TIFFReadDirEntryErrOk);
314 TIFFReadDirEntryCheckedShort(tif,direntry,value);
315 return(TIFFReadDirEntryErrOk);
319 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
320 err=TIFFReadDirEntryCheckRangeShortSshort(m);
321 if (err!=TIFFReadDirEntryErrOk)
324 return(TIFFReadDirEntryErrOk);
329 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
330 err=TIFFReadDirEntryCheckRangeShortLong(m);
331 if (err!=TIFFReadDirEntryErrOk)
334 return(TIFFReadDirEntryErrOk);
339 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
340 err=TIFFReadDirEntryCheckRangeShortSlong(m);
341 if (err!=TIFFReadDirEntryErrOk)
344 return(TIFFReadDirEntryErrOk);
349 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
350 if (err!=TIFFReadDirEntryErrOk)
352 err=TIFFReadDirEntryCheckRangeShortLong8(m);
353 if (err!=TIFFReadDirEntryErrOk)
356 return(TIFFReadDirEntryErrOk);
361 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
362 if (err!=TIFFReadDirEntryErrOk)
364 err=TIFFReadDirEntryCheckRangeShortSlong8(m);
365 if (err!=TIFFReadDirEntryErrOk)
368 return(TIFFReadDirEntryErrOk);
371 return(TIFFReadDirEntryErrType);
375 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
377 enum TIFFReadDirEntryErr err;
378 if (direntry->tdir_count!=1)
379 return(TIFFReadDirEntryErrCount);
380 switch (direntry->tdir_type)
385 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
387 return(TIFFReadDirEntryErrOk);
392 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
393 err=TIFFReadDirEntryCheckRangeLongSbyte(m);
394 if (err!=TIFFReadDirEntryErrOk)
397 return(TIFFReadDirEntryErrOk);
402 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
404 return(TIFFReadDirEntryErrOk);
409 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
410 err=TIFFReadDirEntryCheckRangeLongSshort(m);
411 if (err!=TIFFReadDirEntryErrOk)
414 return(TIFFReadDirEntryErrOk);
417 TIFFReadDirEntryCheckedLong(tif,direntry,value);
418 return(TIFFReadDirEntryErrOk);
422 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
423 err=TIFFReadDirEntryCheckRangeLongSlong(m);
424 if (err!=TIFFReadDirEntryErrOk)
427 return(TIFFReadDirEntryErrOk);
432 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
433 if (err!=TIFFReadDirEntryErrOk)
435 err=TIFFReadDirEntryCheckRangeLongLong8(m);
436 if (err!=TIFFReadDirEntryErrOk)
439 return(TIFFReadDirEntryErrOk);
444 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
445 if (err!=TIFFReadDirEntryErrOk)
447 err=TIFFReadDirEntryCheckRangeLongSlong8(m);
448 if (err!=TIFFReadDirEntryErrOk)
451 return(TIFFReadDirEntryErrOk);
454 return(TIFFReadDirEntryErrType);
458 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
460 enum TIFFReadDirEntryErr err;
461 if (direntry->tdir_count!=1)
462 return(TIFFReadDirEntryErrCount);
463 switch (direntry->tdir_type)
468 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
470 return(TIFFReadDirEntryErrOk);
475 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
476 err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
477 if (err!=TIFFReadDirEntryErrOk)
480 return(TIFFReadDirEntryErrOk);
485 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
487 return(TIFFReadDirEntryErrOk);
492 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
493 err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
494 if (err!=TIFFReadDirEntryErrOk)
497 return(TIFFReadDirEntryErrOk);
502 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
504 return(TIFFReadDirEntryErrOk);
509 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
510 err=TIFFReadDirEntryCheckRangeLong8Slong(m);
511 if (err!=TIFFReadDirEntryErrOk)
514 return(TIFFReadDirEntryErrOk);
517 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
522 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
523 if (err!=TIFFReadDirEntryErrOk)
525 err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
526 if (err!=TIFFReadDirEntryErrOk)
529 return(TIFFReadDirEntryErrOk);
532 return(TIFFReadDirEntryErrType);
536 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
538 enum TIFFReadDirEntryErr err;
539 if (direntry->tdir_count!=1)
540 return(TIFFReadDirEntryErrCount);
541 switch (direntry->tdir_type)
546 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
548 return(TIFFReadDirEntryErrOk);
553 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
555 return(TIFFReadDirEntryErrOk);
560 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
562 return(TIFFReadDirEntryErrOk);
567 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
569 return(TIFFReadDirEntryErrOk);
574 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
576 return(TIFFReadDirEntryErrOk);
581 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
583 return(TIFFReadDirEntryErrOk);
588 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
589 if (err!=TIFFReadDirEntryErrOk)
591 #if defined(__WIN32__) && (_MSC_VER < 1500)
593 * XXX: MSVC 6.0 does not support conversion
594 * of 64-bit integers into floating point
597 *value = _TIFFUInt64ToFloat(m);
601 return(TIFFReadDirEntryErrOk);
606 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
607 if (err!=TIFFReadDirEntryErrOk)
610 return(TIFFReadDirEntryErrOk);
615 err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
616 if (err!=TIFFReadDirEntryErrOk)
619 return(TIFFReadDirEntryErrOk);
624 err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
625 if (err!=TIFFReadDirEntryErrOk)
628 return(TIFFReadDirEntryErrOk);
631 TIFFReadDirEntryCheckedFloat(tif,direntry,value);
632 return(TIFFReadDirEntryErrOk);
636 err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
637 if (err!=TIFFReadDirEntryErrOk)
639 if ((m > FLT_MAX) || (m < FLT_MIN))
640 return(TIFFReadDirEntryErrRange);
642 return(TIFFReadDirEntryErrOk);
645 return(TIFFReadDirEntryErrType);
649 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
651 enum TIFFReadDirEntryErr err;
652 if (direntry->tdir_count!=1)
653 return(TIFFReadDirEntryErrCount);
654 switch (direntry->tdir_type)
659 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
661 return(TIFFReadDirEntryErrOk);
666 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
668 return(TIFFReadDirEntryErrOk);
673 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
675 return(TIFFReadDirEntryErrOk);
680 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
682 return(TIFFReadDirEntryErrOk);
687 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
689 return(TIFFReadDirEntryErrOk);
694 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
696 return(TIFFReadDirEntryErrOk);
701 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
702 if (err!=TIFFReadDirEntryErrOk)
704 #if defined(__WIN32__) && (_MSC_VER < 1500)
706 * XXX: MSVC 6.0 does not support conversion
707 * of 64-bit integers into floating point
710 *value = _TIFFUInt64ToDouble(m);
714 return(TIFFReadDirEntryErrOk);
719 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
720 if (err!=TIFFReadDirEntryErrOk)
723 return(TIFFReadDirEntryErrOk);
726 err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
729 err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
734 TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
736 return(TIFFReadDirEntryErrOk);
739 err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
742 return(TIFFReadDirEntryErrType);
746 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
748 enum TIFFReadDirEntryErr err;
749 if (direntry->tdir_count!=1)
750 return(TIFFReadDirEntryErrCount);
751 switch (direntry->tdir_type)
757 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
759 return(TIFFReadDirEntryErrOk);
763 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
766 return(TIFFReadDirEntryErrType);
771 #define INITIAL_THRESHOLD (1024 * 1024)
772 #define THRESHOLD_MULTIPLIER 10
773 #define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
775 static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
776 TIFF* tif, uint64 offset, tmsize_t size, void** pdest)
778 #if SIZEOF_SIZE_T == 8
779 tmsize_t threshold = INITIAL_THRESHOLD;
781 tmsize_t already_read = 0;
783 assert( !isMapped(tif) );
785 if (!SeekOK(tif,offset))
786 return(TIFFReadDirEntryErrIo);
788 /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
789 /* so as to avoid allocating too much memory in case the file is too */
790 /* short. We could ask for the file size, but this might be */
791 /* expensive with some I/O layers (think of reading a gzipped file) */
792 /* Restrict to 64 bit processes, so as to avoid reallocs() */
793 /* on 32 bit processes where virtual memory is scarce. */
794 while( already_read < size )
798 tmsize_t to_read = size - already_read;
799 #if SIZEOF_SIZE_T == 8
800 if( to_read >= threshold && threshold < MAX_THRESHOLD )
803 threshold *= THRESHOLD_MULTIPLIER;
807 new_dest = (uint8*) _TIFFrealloc(
808 *pdest, already_read + to_read);
809 if( new_dest == NULL )
811 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
812 "Failed to allocate memory for %s "
813 "(%ld elements of %ld bytes each)",
814 "TIFFReadDirEntryArray",
815 (long) 1, (long) (already_read + to_read));
816 return TIFFReadDirEntryErrAlloc;
820 bytes_read = TIFFReadFile(tif,
821 (char*)*pdest + already_read, to_read);
822 already_read += bytes_read;
823 if (bytes_read != to_read) {
824 return TIFFReadDirEntryErrIo;
827 return TIFFReadDirEntryErrOk;
830 static enum TIFFReadDirEntryErr TIFFReadDirEntryArrayWithLimit(
831 TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize,
832 void** value, uint64 maxcount)
837 uint64 target_count64;
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))
846 return(TIFFReadDirEntryErrOk);
851 * As a sanity check, make sure we have no more than a 2GB tag array
852 * in either the current data type or the dest data type. This also
853 * avoids problems with overflow of tmsize_t on 32bit systems.
855 if ((uint64)(2147483647/typesize)<target_count64)
856 return(TIFFReadDirEntryErrSizesan);
857 if ((uint64)(2147483647/desttypesize)<target_count64)
858 return(TIFFReadDirEntryErrSizesan);
860 *count=(uint32)target_count64;
861 datasize=(*count)*typesize;
862 assert((tmsize_t)datasize>0);
864 if( isMapped(tif) && datasize > (uint32)tif->tif_size )
865 return TIFFReadDirEntryErrIo;
867 if( !isMapped(tif) &&
868 (((tif->tif_flags&TIFF_BIGTIFF) && datasize > 8) ||
869 (!(tif->tif_flags&TIFF_BIGTIFF) && datasize > 4)) )
875 data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
877 return(TIFFReadDirEntryErrAlloc);
879 if (!(tif->tif_flags&TIFF_BIGTIFF))
882 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
885 enum TIFFReadDirEntryErr err;
886 uint32 offset = direntry->tdir_offset.toff_long;
887 if (tif->tif_flags&TIFF_SWAB)
888 TIFFSwabLong(&offset);
890 err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
892 err=TIFFReadDirEntryDataAndRealloc(tif,(uint64)offset,(tmsize_t)datasize,&data);
893 if (err!=TIFFReadDirEntryErrOk)
903 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
906 enum TIFFReadDirEntryErr err;
907 uint64 offset = direntry->tdir_offset.toff_long8;
908 if (tif->tif_flags&TIFF_SWAB)
909 TIFFSwabLong8(&offset);
911 err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
913 err=TIFFReadDirEntryDataAndRealloc(tif,(uint64)offset,(tmsize_t)datasize,&data);
914 if (err!=TIFFReadDirEntryErrOk)
922 return(TIFFReadDirEntryErrOk);
925 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
927 return TIFFReadDirEntryArrayWithLimit(tif, direntry, count,
928 desttypesize, value, ~((uint64)0));
931 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
933 enum TIFFReadDirEntryErr err;
937 switch (direntry->tdir_type)
951 return(TIFFReadDirEntryErrType);
953 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
954 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
959 switch (direntry->tdir_type)
964 *value=(uint8*)origdata;
965 return(TIFFReadDirEntryErrOk);
971 for (n=0; n<count; n++)
973 err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
974 if (err!=TIFFReadDirEntryErrOk)
981 *value=(uint8*)origdata;
982 return(TIFFReadDirEntryErrOk);
985 data=(uint8*)_TIFFmalloc(count);
989 return(TIFFReadDirEntryErrAlloc);
991 switch (direntry->tdir_type)
998 ma=(uint16*)origdata;
1000 for (n=0; n<count; n++)
1002 if (tif->tif_flags&TIFF_SWAB)
1004 err=TIFFReadDirEntryCheckRangeByteShort(*ma);
1005 if (err!=TIFFReadDirEntryErrOk)
1007 *mb++=(uint8)(*ma++);
1016 ma=(int16*)origdata;
1018 for (n=0; n<count; n++)
1020 if (tif->tif_flags&TIFF_SWAB)
1021 TIFFSwabShort((uint16*)ma);
1022 err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
1023 if (err!=TIFFReadDirEntryErrOk)
1025 *mb++=(uint8)(*ma++);
1034 ma=(uint32*)origdata;
1036 for (n=0; n<count; n++)
1038 if (tif->tif_flags&TIFF_SWAB)
1040 err=TIFFReadDirEntryCheckRangeByteLong(*ma);
1041 if (err!=TIFFReadDirEntryErrOk)
1043 *mb++=(uint8)(*ma++);
1052 ma=(int32*)origdata;
1054 for (n=0; n<count; n++)
1056 if (tif->tif_flags&TIFF_SWAB)
1057 TIFFSwabLong((uint32*)ma);
1058 err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
1059 if (err!=TIFFReadDirEntryErrOk)
1061 *mb++=(uint8)(*ma++);
1070 ma=(uint64*)origdata;
1072 for (n=0; n<count; n++)
1074 if (tif->tif_flags&TIFF_SWAB)
1076 err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
1077 if (err!=TIFFReadDirEntryErrOk)
1079 *mb++=(uint8)(*ma++);
1088 ma=(int64*)origdata;
1090 for (n=0; n<count; n++)
1092 if (tif->tif_flags&TIFF_SWAB)
1093 TIFFSwabLong8((uint64*)ma);
1094 err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1095 if (err!=TIFFReadDirEntryErrOk)
1097 *mb++=(uint8)(*ma++);
1102 _TIFFfree(origdata);
1103 if (err!=TIFFReadDirEntryErrOk)
1109 return(TIFFReadDirEntryErrOk);
1112 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
1114 enum TIFFReadDirEntryErr err;
1118 switch (direntry->tdir_type)
1120 case TIFF_UNDEFINED:
1131 return(TIFFReadDirEntryErrType);
1133 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1134 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1139 switch (direntry->tdir_type)
1141 case TIFF_UNDEFINED:
1147 for (n=0; n<count; n++)
1149 err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1150 if (err!=TIFFReadDirEntryErrOk)
1152 _TIFFfree(origdata);
1157 *value=(int8*)origdata;
1158 return(TIFFReadDirEntryErrOk);
1161 *value=(int8*)origdata;
1162 return(TIFFReadDirEntryErrOk);
1164 data=(int8*)_TIFFmalloc(count);
1167 _TIFFfree(origdata);
1168 return(TIFFReadDirEntryErrAlloc);
1170 switch (direntry->tdir_type)
1177 ma=(uint16*)origdata;
1179 for (n=0; n<count; n++)
1181 if (tif->tif_flags&TIFF_SWAB)
1183 err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1184 if (err!=TIFFReadDirEntryErrOk)
1186 *mb++=(int8)(*ma++);
1195 ma=(int16*)origdata;
1197 for (n=0; n<count; n++)
1199 if (tif->tif_flags&TIFF_SWAB)
1200 TIFFSwabShort((uint16*)ma);
1201 err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1202 if (err!=TIFFReadDirEntryErrOk)
1204 *mb++=(int8)(*ma++);
1213 ma=(uint32*)origdata;
1215 for (n=0; n<count; n++)
1217 if (tif->tif_flags&TIFF_SWAB)
1219 err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1220 if (err!=TIFFReadDirEntryErrOk)
1222 *mb++=(int8)(*ma++);
1231 ma=(int32*)origdata;
1233 for (n=0; n<count; n++)
1235 if (tif->tif_flags&TIFF_SWAB)
1236 TIFFSwabLong((uint32*)ma);
1237 err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1238 if (err!=TIFFReadDirEntryErrOk)
1240 *mb++=(int8)(*ma++);
1249 ma=(uint64*)origdata;
1251 for (n=0; n<count; n++)
1253 if (tif->tif_flags&TIFF_SWAB)
1255 err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1256 if (err!=TIFFReadDirEntryErrOk)
1258 *mb++=(int8)(*ma++);
1267 ma=(int64*)origdata;
1269 for (n=0; n<count; n++)
1271 if (tif->tif_flags&TIFF_SWAB)
1272 TIFFSwabLong8((uint64*)ma);
1273 err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1274 if (err!=TIFFReadDirEntryErrOk)
1276 *mb++=(int8)(*ma++);
1281 _TIFFfree(origdata);
1282 if (err!=TIFFReadDirEntryErrOk)
1288 return(TIFFReadDirEntryErrOk);
1291 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
1293 enum TIFFReadDirEntryErr err;
1297 switch (direntry->tdir_type)
1309 return(TIFFReadDirEntryErrType);
1311 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1312 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1317 switch (direntry->tdir_type)
1320 *value=(uint16*)origdata;
1321 if (tif->tif_flags&TIFF_SWAB)
1322 TIFFSwabArrayOfShort(*value,count);
1323 return(TIFFReadDirEntryErrOk);
1329 for (n=0; n<count; n++)
1331 if (tif->tif_flags&TIFF_SWAB)
1332 TIFFSwabShort((uint16*)m);
1333 err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1334 if (err!=TIFFReadDirEntryErrOk)
1336 _TIFFfree(origdata);
1341 *value=(uint16*)origdata;
1342 return(TIFFReadDirEntryErrOk);
1345 data=(uint16*)_TIFFmalloc(count*2);
1348 _TIFFfree(origdata);
1349 return(TIFFReadDirEntryErrAlloc);
1351 switch (direntry->tdir_type)
1358 ma=(uint8*)origdata;
1360 for (n=0; n<count; n++)
1361 *mb++=(uint16)(*ma++);
1371 for (n=0; n<count; n++)
1373 err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1374 if (err!=TIFFReadDirEntryErrOk)
1376 *mb++=(uint16)(*ma++);
1385 ma=(uint32*)origdata;
1387 for (n=0; n<count; n++)
1389 if (tif->tif_flags&TIFF_SWAB)
1391 err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1392 if (err!=TIFFReadDirEntryErrOk)
1394 *mb++=(uint16)(*ma++);
1403 ma=(int32*)origdata;
1405 for (n=0; n<count; n++)
1407 if (tif->tif_flags&TIFF_SWAB)
1408 TIFFSwabLong((uint32*)ma);
1409 err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1410 if (err!=TIFFReadDirEntryErrOk)
1412 *mb++=(uint16)(*ma++);
1421 ma=(uint64*)origdata;
1423 for (n=0; n<count; n++)
1425 if (tif->tif_flags&TIFF_SWAB)
1427 err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1428 if (err!=TIFFReadDirEntryErrOk)
1430 *mb++=(uint16)(*ma++);
1439 ma=(int64*)origdata;
1441 for (n=0; n<count; n++)
1443 if (tif->tif_flags&TIFF_SWAB)
1444 TIFFSwabLong8((uint64*)ma);
1445 err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1446 if (err!=TIFFReadDirEntryErrOk)
1448 *mb++=(uint16)(*ma++);
1453 _TIFFfree(origdata);
1454 if (err!=TIFFReadDirEntryErrOk)
1460 return(TIFFReadDirEntryErrOk);
1463 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
1465 enum TIFFReadDirEntryErr err;
1469 switch (direntry->tdir_type)
1481 return(TIFFReadDirEntryErrType);
1483 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1484 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1489 switch (direntry->tdir_type)
1495 m=(uint16*)origdata;
1496 for (n=0; n<count; n++)
1498 if (tif->tif_flags&TIFF_SWAB)
1500 err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1501 if (err!=TIFFReadDirEntryErrOk)
1503 _TIFFfree(origdata);
1508 *value=(int16*)origdata;
1509 return(TIFFReadDirEntryErrOk);
1512 *value=(int16*)origdata;
1513 if (tif->tif_flags&TIFF_SWAB)
1514 TIFFSwabArrayOfShort((uint16*)(*value),count);
1515 return(TIFFReadDirEntryErrOk);
1517 data=(int16*)_TIFFmalloc(count*2);
1520 _TIFFfree(origdata);
1521 return(TIFFReadDirEntryErrAlloc);
1523 switch (direntry->tdir_type)
1530 ma=(uint8*)origdata;
1532 for (n=0; n<count; n++)
1533 *mb++=(int16)(*ma++);
1543 for (n=0; n<count; n++)
1544 *mb++=(int16)(*ma++);
1552 ma=(uint32*)origdata;
1554 for (n=0; n<count; n++)
1556 if (tif->tif_flags&TIFF_SWAB)
1558 err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1559 if (err!=TIFFReadDirEntryErrOk)
1561 *mb++=(int16)(*ma++);
1570 ma=(int32*)origdata;
1572 for (n=0; n<count; n++)
1574 if (tif->tif_flags&TIFF_SWAB)
1575 TIFFSwabLong((uint32*)ma);
1576 err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1577 if (err!=TIFFReadDirEntryErrOk)
1579 *mb++=(int16)(*ma++);
1588 ma=(uint64*)origdata;
1590 for (n=0; n<count; n++)
1592 if (tif->tif_flags&TIFF_SWAB)
1594 err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1595 if (err!=TIFFReadDirEntryErrOk)
1597 *mb++=(int16)(*ma++);
1606 ma=(int64*)origdata;
1608 for (n=0; n<count; n++)
1610 if (tif->tif_flags&TIFF_SWAB)
1611 TIFFSwabLong8((uint64*)ma);
1612 err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1613 if (err!=TIFFReadDirEntryErrOk)
1615 *mb++=(int16)(*ma++);
1620 _TIFFfree(origdata);
1621 if (err!=TIFFReadDirEntryErrOk)
1627 return(TIFFReadDirEntryErrOk);
1630 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
1632 enum TIFFReadDirEntryErr err;
1636 switch (direntry->tdir_type)
1648 return(TIFFReadDirEntryErrType);
1650 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1651 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1656 switch (direntry->tdir_type)
1659 *value=(uint32*)origdata;
1660 if (tif->tif_flags&TIFF_SWAB)
1661 TIFFSwabArrayOfLong(*value,count);
1662 return(TIFFReadDirEntryErrOk);
1668 for (n=0; n<count; n++)
1670 if (tif->tif_flags&TIFF_SWAB)
1671 TIFFSwabLong((uint32*)m);
1672 err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1673 if (err!=TIFFReadDirEntryErrOk)
1675 _TIFFfree(origdata);
1680 *value=(uint32*)origdata;
1681 return(TIFFReadDirEntryErrOk);
1684 data=(uint32*)_TIFFmalloc(count*4);
1687 _TIFFfree(origdata);
1688 return(TIFFReadDirEntryErrAlloc);
1690 switch (direntry->tdir_type)
1697 ma=(uint8*)origdata;
1699 for (n=0; n<count; n++)
1700 *mb++=(uint32)(*ma++);
1710 for (n=0; n<count; n++)
1712 err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1713 if (err!=TIFFReadDirEntryErrOk)
1715 *mb++=(uint32)(*ma++);
1724 ma=(uint16*)origdata;
1726 for (n=0; n<count; n++)
1728 if (tif->tif_flags&TIFF_SWAB)
1730 *mb++=(uint32)(*ma++);
1739 ma=(int16*)origdata;
1741 for (n=0; n<count; n++)
1743 if (tif->tif_flags&TIFF_SWAB)
1744 TIFFSwabShort((uint16*)ma);
1745 err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1746 if (err!=TIFFReadDirEntryErrOk)
1748 *mb++=(uint32)(*ma++);
1757 ma=(uint64*)origdata;
1759 for (n=0; n<count; n++)
1761 if (tif->tif_flags&TIFF_SWAB)
1763 err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1764 if (err!=TIFFReadDirEntryErrOk)
1766 *mb++=(uint32)(*ma++);
1775 ma=(int64*)origdata;
1777 for (n=0; n<count; n++)
1779 if (tif->tif_flags&TIFF_SWAB)
1780 TIFFSwabLong8((uint64*)ma);
1781 err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1782 if (err!=TIFFReadDirEntryErrOk)
1784 *mb++=(uint32)(*ma++);
1789 _TIFFfree(origdata);
1790 if (err!=TIFFReadDirEntryErrOk)
1796 return(TIFFReadDirEntryErrOk);
1799 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
1801 enum TIFFReadDirEntryErr err;
1805 switch (direntry->tdir_type)
1817 return(TIFFReadDirEntryErrType);
1819 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1820 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1825 switch (direntry->tdir_type)
1831 m=(uint32*)origdata;
1832 for (n=0; n<count; n++)
1834 if (tif->tif_flags&TIFF_SWAB)
1835 TIFFSwabLong((uint32*)m);
1836 err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1837 if (err!=TIFFReadDirEntryErrOk)
1839 _TIFFfree(origdata);
1844 *value=(int32*)origdata;
1845 return(TIFFReadDirEntryErrOk);
1848 *value=(int32*)origdata;
1849 if (tif->tif_flags&TIFF_SWAB)
1850 TIFFSwabArrayOfLong((uint32*)(*value),count);
1851 return(TIFFReadDirEntryErrOk);
1853 data=(int32*)_TIFFmalloc(count*4);
1856 _TIFFfree(origdata);
1857 return(TIFFReadDirEntryErrAlloc);
1859 switch (direntry->tdir_type)
1866 ma=(uint8*)origdata;
1868 for (n=0; n<count; n++)
1869 *mb++=(int32)(*ma++);
1879 for (n=0; n<count; n++)
1880 *mb++=(int32)(*ma++);
1888 ma=(uint16*)origdata;
1890 for (n=0; n<count; n++)
1892 if (tif->tif_flags&TIFF_SWAB)
1894 *mb++=(int32)(*ma++);
1903 ma=(int16*)origdata;
1905 for (n=0; n<count; n++)
1907 if (tif->tif_flags&TIFF_SWAB)
1908 TIFFSwabShort((uint16*)ma);
1909 *mb++=(int32)(*ma++);
1918 ma=(uint64*)origdata;
1920 for (n=0; n<count; n++)
1922 if (tif->tif_flags&TIFF_SWAB)
1924 err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1925 if (err!=TIFFReadDirEntryErrOk)
1927 *mb++=(int32)(*ma++);
1936 ma=(int64*)origdata;
1938 for (n=0; n<count; n++)
1940 if (tif->tif_flags&TIFF_SWAB)
1941 TIFFSwabLong8((uint64*)ma);
1942 err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1943 if (err!=TIFFReadDirEntryErrOk)
1945 *mb++=(int32)(*ma++);
1950 _TIFFfree(origdata);
1951 if (err!=TIFFReadDirEntryErrOk)
1957 return(TIFFReadDirEntryErrOk);
1960 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(
1961 TIFF* tif, TIFFDirEntry* direntry, uint64** value, uint64 maxcount)
1963 enum TIFFReadDirEntryErr err;
1967 switch (direntry->tdir_type)
1979 return(TIFFReadDirEntryErrType);
1981 err=TIFFReadDirEntryArrayWithLimit(tif,direntry,&count,8,&origdata,maxcount);
1982 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1987 switch (direntry->tdir_type)
1990 *value=(uint64*)origdata;
1991 if (tif->tif_flags&TIFF_SWAB)
1992 TIFFSwabArrayOfLong8(*value,count);
1993 return(TIFFReadDirEntryErrOk);
1999 for (n=0; n<count; n++)
2001 if (tif->tif_flags&TIFF_SWAB)
2002 TIFFSwabLong8((uint64*)m);
2003 err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2004 if (err!=TIFFReadDirEntryErrOk)
2006 _TIFFfree(origdata);
2011 *value=(uint64*)origdata;
2012 return(TIFFReadDirEntryErrOk);
2015 data=(uint64*)_TIFFmalloc(count*8);
2018 _TIFFfree(origdata);
2019 return(TIFFReadDirEntryErrAlloc);
2021 switch (direntry->tdir_type)
2028 ma=(uint8*)origdata;
2030 for (n=0; n<count; n++)
2031 *mb++=(uint64)(*ma++);
2041 for (n=0; n<count; n++)
2043 err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2044 if (err!=TIFFReadDirEntryErrOk)
2046 *mb++=(uint64)(*ma++);
2055 ma=(uint16*)origdata;
2057 for (n=0; n<count; n++)
2059 if (tif->tif_flags&TIFF_SWAB)
2061 *mb++=(uint64)(*ma++);
2070 ma=(int16*)origdata;
2072 for (n=0; n<count; n++)
2074 if (tif->tif_flags&TIFF_SWAB)
2075 TIFFSwabShort((uint16*)ma);
2076 err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2077 if (err!=TIFFReadDirEntryErrOk)
2079 *mb++=(uint64)(*ma++);
2088 ma=(uint32*)origdata;
2090 for (n=0; n<count; n++)
2092 if (tif->tif_flags&TIFF_SWAB)
2094 *mb++=(uint64)(*ma++);
2103 ma=(int32*)origdata;
2105 for (n=0; n<count; n++)
2107 if (tif->tif_flags&TIFF_SWAB)
2108 TIFFSwabLong((uint32*)ma);
2109 err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2110 if (err!=TIFFReadDirEntryErrOk)
2112 *mb++=(uint64)(*ma++);
2117 _TIFFfree(origdata);
2118 if (err!=TIFFReadDirEntryErrOk)
2124 return(TIFFReadDirEntryErrOk);
2127 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
2129 return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value, ~((uint64)0));
2132 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
2134 enum TIFFReadDirEntryErr err;
2138 switch (direntry->tdir_type)
2150 return(TIFFReadDirEntryErrType);
2152 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2153 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2158 switch (direntry->tdir_type)
2164 m=(uint64*)origdata;
2165 for (n=0; n<count; n++)
2167 if (tif->tif_flags&TIFF_SWAB)
2169 err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2170 if (err!=TIFFReadDirEntryErrOk)
2172 _TIFFfree(origdata);
2177 *value=(int64*)origdata;
2178 return(TIFFReadDirEntryErrOk);
2181 *value=(int64*)origdata;
2182 if (tif->tif_flags&TIFF_SWAB)
2183 TIFFSwabArrayOfLong8((uint64*)(*value),count);
2184 return(TIFFReadDirEntryErrOk);
2186 data=(int64*)_TIFFmalloc(count*8);
2189 _TIFFfree(origdata);
2190 return(TIFFReadDirEntryErrAlloc);
2192 switch (direntry->tdir_type)
2199 ma=(uint8*)origdata;
2201 for (n=0; n<count; n++)
2202 *mb++=(int64)(*ma++);
2212 for (n=0; n<count; n++)
2213 *mb++=(int64)(*ma++);
2221 ma=(uint16*)origdata;
2223 for (n=0; n<count; n++)
2225 if (tif->tif_flags&TIFF_SWAB)
2227 *mb++=(int64)(*ma++);
2236 ma=(int16*)origdata;
2238 for (n=0; n<count; n++)
2240 if (tif->tif_flags&TIFF_SWAB)
2241 TIFFSwabShort((uint16*)ma);
2242 *mb++=(int64)(*ma++);
2251 ma=(uint32*)origdata;
2253 for (n=0; n<count; n++)
2255 if (tif->tif_flags&TIFF_SWAB)
2257 *mb++=(int64)(*ma++);
2266 ma=(int32*)origdata;
2268 for (n=0; n<count; n++)
2270 if (tif->tif_flags&TIFF_SWAB)
2271 TIFFSwabLong((uint32*)ma);
2272 *mb++=(int64)(*ma++);
2277 _TIFFfree(origdata);
2279 return(TIFFReadDirEntryErrOk);
2282 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2284 enum TIFFReadDirEntryErr err;
2288 switch (direntry->tdir_type)
2299 case TIFF_SRATIONAL:
2304 return(TIFFReadDirEntryErrType);
2306 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2307 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2312 switch (direntry->tdir_type)
2315 if (tif->tif_flags&TIFF_SWAB)
2316 TIFFSwabArrayOfLong((uint32*)origdata,count);
2317 TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2318 *value=(float*)origdata;
2319 return(TIFFReadDirEntryErrOk);
2321 data=(float*)_TIFFmalloc(count*sizeof(float));
2324 _TIFFfree(origdata);
2325 return(TIFFReadDirEntryErrAlloc);
2327 switch (direntry->tdir_type)
2334 ma=(uint8*)origdata;
2336 for (n=0; n<count; n++)
2337 *mb++=(float)(*ma++);
2347 for (n=0; n<count; n++)
2348 *mb++=(float)(*ma++);
2356 ma=(uint16*)origdata;
2358 for (n=0; n<count; n++)
2360 if (tif->tif_flags&TIFF_SWAB)
2362 *mb++=(float)(*ma++);
2371 ma=(int16*)origdata;
2373 for (n=0; n<count; n++)
2375 if (tif->tif_flags&TIFF_SWAB)
2376 TIFFSwabShort((uint16*)ma);
2377 *mb++=(float)(*ma++);
2386 ma=(uint32*)origdata;
2388 for (n=0; n<count; n++)
2390 if (tif->tif_flags&TIFF_SWAB)
2392 *mb++=(float)(*ma++);
2401 ma=(int32*)origdata;
2403 for (n=0; n<count; n++)
2405 if (tif->tif_flags&TIFF_SWAB)
2406 TIFFSwabLong((uint32*)ma);
2407 *mb++=(float)(*ma++);
2416 ma=(uint64*)origdata;
2418 for (n=0; n<count; n++)
2420 if (tif->tif_flags&TIFF_SWAB)
2422 #if defined(__WIN32__) && (_MSC_VER < 1500)
2424 * XXX: MSVC 6.0 does not support
2425 * conversion of 64-bit integers into
2426 * floating point values.
2428 *mb++ = _TIFFUInt64ToFloat(*ma++);
2430 *mb++ = (float)(*ma++);
2440 ma=(int64*)origdata;
2442 for (n=0; n<count; n++)
2444 if (tif->tif_flags&TIFF_SWAB)
2445 TIFFSwabLong8((uint64*)ma);
2446 *mb++=(float)(*ma++);
2457 ma=(uint32*)origdata;
2459 for (n=0; n<count; n++)
2461 if (tif->tif_flags&TIFF_SWAB)
2464 if (tif->tif_flags&TIFF_SWAB)
2470 *mb++=(float)maa/(float)mab;
2474 case TIFF_SRATIONAL:
2481 ma=(uint32*)origdata;
2483 for (n=0; n<count; n++)
2485 if (tif->tif_flags&TIFF_SWAB)
2489 if (tif->tif_flags&TIFF_SWAB)
2495 *mb++=(float)maa/(float)mab;
2504 if (tif->tif_flags&TIFF_SWAB)
2505 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2506 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2507 ma=(double*)origdata;
2509 for (n=0; n<count; n++)
2514 else if( val < -FLT_MAX )
2521 _TIFFfree(origdata);
2523 return(TIFFReadDirEntryErrOk);
2526 static enum TIFFReadDirEntryErr
2527 TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2529 enum TIFFReadDirEntryErr err;
2533 switch (direntry->tdir_type)
2544 case TIFF_SRATIONAL:
2549 return(TIFFReadDirEntryErrType);
2551 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2552 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2557 switch (direntry->tdir_type)
2560 if (tif->tif_flags&TIFF_SWAB)
2561 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2562 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2563 *value=(double*)origdata;
2564 return(TIFFReadDirEntryErrOk);
2566 data=(double*)_TIFFmalloc(count*sizeof(double));
2569 _TIFFfree(origdata);
2570 return(TIFFReadDirEntryErrAlloc);
2572 switch (direntry->tdir_type)
2579 ma=(uint8*)origdata;
2581 for (n=0; n<count; n++)
2582 *mb++=(double)(*ma++);
2592 for (n=0; n<count; n++)
2593 *mb++=(double)(*ma++);
2601 ma=(uint16*)origdata;
2603 for (n=0; n<count; n++)
2605 if (tif->tif_flags&TIFF_SWAB)
2607 *mb++=(double)(*ma++);
2616 ma=(int16*)origdata;
2618 for (n=0; n<count; n++)
2620 if (tif->tif_flags&TIFF_SWAB)
2621 TIFFSwabShort((uint16*)ma);
2622 *mb++=(double)(*ma++);
2631 ma=(uint32*)origdata;
2633 for (n=0; n<count; n++)
2635 if (tif->tif_flags&TIFF_SWAB)
2637 *mb++=(double)(*ma++);
2646 ma=(int32*)origdata;
2648 for (n=0; n<count; n++)
2650 if (tif->tif_flags&TIFF_SWAB)
2651 TIFFSwabLong((uint32*)ma);
2652 *mb++=(double)(*ma++);
2661 ma=(uint64*)origdata;
2663 for (n=0; n<count; n++)
2665 if (tif->tif_flags&TIFF_SWAB)
2667 #if defined(__WIN32__) && (_MSC_VER < 1500)
2669 * XXX: MSVC 6.0 does not support
2670 * conversion of 64-bit integers into
2671 * floating point values.
2673 *mb++ = _TIFFUInt64ToDouble(*ma++);
2675 *mb++ = (double)(*ma++);
2685 ma=(int64*)origdata;
2687 for (n=0; n<count; n++)
2689 if (tif->tif_flags&TIFF_SWAB)
2690 TIFFSwabLong8((uint64*)ma);
2691 *mb++=(double)(*ma++);
2702 ma=(uint32*)origdata;
2704 for (n=0; n<count; n++)
2706 if (tif->tif_flags&TIFF_SWAB)
2709 if (tif->tif_flags&TIFF_SWAB)
2715 *mb++=(double)maa/(double)mab;
2719 case TIFF_SRATIONAL:
2726 ma=(uint32*)origdata;
2728 for (n=0; n<count; n++)
2730 if (tif->tif_flags&TIFF_SWAB)
2734 if (tif->tif_flags&TIFF_SWAB)
2740 *mb++=(double)maa/(double)mab;
2749 if (tif->tif_flags&TIFF_SWAB)
2750 TIFFSwabArrayOfLong((uint32*)origdata,count);
2751 TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
2752 ma=(float*)origdata;
2754 for (n=0; n<count; n++)
2755 *mb++=(double)(*ma++);
2759 _TIFFfree(origdata);
2761 return(TIFFReadDirEntryErrOk);
2764 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
2766 enum TIFFReadDirEntryErr err;
2770 switch (direntry->tdir_type)
2778 return(TIFFReadDirEntryErrType);
2780 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2781 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2786 switch (direntry->tdir_type)
2790 *value=(uint64*)origdata;
2791 if (tif->tif_flags&TIFF_SWAB)
2792 TIFFSwabArrayOfLong8(*value,count);
2793 return(TIFFReadDirEntryErrOk);
2795 data=(uint64*)_TIFFmalloc(count*8);
2798 _TIFFfree(origdata);
2799 return(TIFFReadDirEntryErrAlloc);
2801 switch (direntry->tdir_type)
2809 ma=(uint32*)origdata;
2811 for (n=0; n<count; n++)
2813 if (tif->tif_flags&TIFF_SWAB)
2815 *mb++=(uint64)(*ma++);
2820 _TIFFfree(origdata);
2822 return(TIFFReadDirEntryErrOk);
2825 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2827 enum TIFFReadDirEntryErr err;
2831 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2832 return(TIFFReadDirEntryErrCount);
2833 err=TIFFReadDirEntryShortArray(tif,direntry,&m);
2834 if (err!=TIFFReadDirEntryErrOk || m == NULL)
2837 nb=tif->tif_dir.td_samplesperpixel;
2844 err=TIFFReadDirEntryErrPsdif;
2854 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2856 enum TIFFReadDirEntryErr err;
2860 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2861 return(TIFFReadDirEntryErrCount);
2862 err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
2863 if (err!=TIFFReadDirEntryErrOk)
2866 nb=tif->tif_dir.td_samplesperpixel;
2873 err=TIFFReadDirEntryErrPsdif;
2883 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
2886 *value=*(uint8*)(&direntry->tdir_offset);
2889 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
2892 *value=*(int8*)(&direntry->tdir_offset);
2895 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2897 *value = direntry->tdir_offset.toff_short;
2898 /* *value=*(uint16*)(&direntry->tdir_offset); */
2899 if (tif->tif_flags&TIFF_SWAB)
2900 TIFFSwabShort(value);
2903 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
2905 *value=*(int16*)(&direntry->tdir_offset);
2906 if (tif->tif_flags&TIFF_SWAB)
2907 TIFFSwabShort((uint16*)value);
2910 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
2912 *value=*(uint32*)(&direntry->tdir_offset);
2913 if (tif->tif_flags&TIFF_SWAB)
2914 TIFFSwabLong(value);
2917 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
2919 *value=*(int32*)(&direntry->tdir_offset);
2920 if (tif->tif_flags&TIFF_SWAB)
2921 TIFFSwabLong((uint32*)value);
2924 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
2926 if (!(tif->tif_flags&TIFF_BIGTIFF))
2928 enum TIFFReadDirEntryErr err;
2929 uint32 offset = direntry->tdir_offset.toff_long;
2930 if (tif->tif_flags&TIFF_SWAB)
2931 TIFFSwabLong(&offset);
2932 err=TIFFReadDirEntryData(tif,offset,8,value);
2933 if (err!=TIFFReadDirEntryErrOk)
2937 *value = direntry->tdir_offset.toff_long8;
2938 if (tif->tif_flags&TIFF_SWAB)
2939 TIFFSwabLong8(value);
2940 return(TIFFReadDirEntryErrOk);
2943 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
2945 if (!(tif->tif_flags&TIFF_BIGTIFF))
2947 enum TIFFReadDirEntryErr err;
2948 uint32 offset = direntry->tdir_offset.toff_long;
2949 if (tif->tif_flags&TIFF_SWAB)
2950 TIFFSwabLong(&offset);
2951 err=TIFFReadDirEntryData(tif,offset,8,value);
2952 if (err!=TIFFReadDirEntryErrOk)
2956 *value=*(int64*)(&direntry->tdir_offset);
2957 if (tif->tif_flags&TIFF_SWAB)
2958 TIFFSwabLong8((uint64*)value);
2959 return(TIFFReadDirEntryErrOk);
2962 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2966 assert(sizeof(double)==8);
2967 assert(sizeof(uint64)==8);
2968 assert(sizeof(uint32)==4);
2969 if (!(tif->tif_flags&TIFF_BIGTIFF))
2971 enum TIFFReadDirEntryErr err;
2972 uint32 offset = direntry->tdir_offset.toff_long;
2973 if (tif->tif_flags&TIFF_SWAB)
2974 TIFFSwabLong(&offset);
2975 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2976 if (err!=TIFFReadDirEntryErrOk)
2980 m.l = direntry->tdir_offset.toff_long8;
2981 if (tif->tif_flags&TIFF_SWAB)
2982 TIFFSwabArrayOfLong(m.i,2);
2983 /* Not completely sure what we should do when m.i[1]==0, but some */
2984 /* sanitizers do not like division by 0.0: */
2985 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
2986 if (m.i[0]==0 || m.i[1]==0)
2989 *value=(double)m.i[0]/(double)m.i[1];
2990 return(TIFFReadDirEntryErrOk);
2993 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2996 assert(sizeof(double)==8);
2997 assert(sizeof(uint64)==8);
2998 assert(sizeof(int32)==4);
2999 assert(sizeof(uint32)==4);
3000 if (!(tif->tif_flags&TIFF_BIGTIFF))
3002 enum TIFFReadDirEntryErr err;
3003 uint32 offset = direntry->tdir_offset.toff_long;
3004 if (tif->tif_flags&TIFF_SWAB)
3005 TIFFSwabLong(&offset);
3006 err=TIFFReadDirEntryData(tif,offset,8,m.i);
3007 if (err!=TIFFReadDirEntryErrOk)
3011 m.l=direntry->tdir_offset.toff_long8;
3012 if (tif->tif_flags&TIFF_SWAB)
3013 TIFFSwabArrayOfLong(m.i,2);
3014 /* Not completely sure what we should do when m.i[1]==0, but some */
3015 /* sanitizers do not like division by 0.0: */
3016 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3017 if ((int32)m.i[0]==0 || m.i[1]==0)
3020 *value=(double)((int32)m.i[0])/(double)m.i[1];
3021 return(TIFFReadDirEntryErrOk);
3024 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
3031 assert(sizeof(float)==4);
3032 assert(sizeof(uint32)==4);
3033 assert(sizeof(float_union)==4);
3034 float_union.i=*(uint32*)(&direntry->tdir_offset);
3035 *value=float_union.f;
3036 if (tif->tif_flags&TIFF_SWAB)
3037 TIFFSwabLong((uint32*)value);
3040 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
3042 assert(sizeof(double)==8);
3043 assert(sizeof(uint64)==8);
3044 assert(sizeof(UInt64Aligned_t)==8);
3045 if (!(tif->tif_flags&TIFF_BIGTIFF))
3047 enum TIFFReadDirEntryErr err;
3048 uint32 offset = direntry->tdir_offset.toff_long;
3049 if (tif->tif_flags&TIFF_SWAB)
3050 TIFFSwabLong(&offset);
3051 err=TIFFReadDirEntryData(tif,offset,8,value);
3052 if (err!=TIFFReadDirEntryErrOk)
3057 UInt64Aligned_t uint64_union;
3058 uint64_union.l=direntry->tdir_offset.toff_long8;
3059 *value=uint64_union.d;
3061 if (tif->tif_flags&TIFF_SWAB)
3062 TIFFSwabLong8((uint64*)value);
3063 return(TIFFReadDirEntryErrOk);
3066 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
3069 return(TIFFReadDirEntryErrRange);
3071 return(TIFFReadDirEntryErrOk);
3074 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
3077 return(TIFFReadDirEntryErrRange);
3079 return(TIFFReadDirEntryErrOk);
3082 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
3084 if ((value<0)||(value>0xFF))
3085 return(TIFFReadDirEntryErrRange);
3087 return(TIFFReadDirEntryErrOk);
3090 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
3093 return(TIFFReadDirEntryErrRange);
3095 return(TIFFReadDirEntryErrOk);
3098 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
3100 if ((value<0)||(value>0xFF))
3101 return(TIFFReadDirEntryErrRange);
3103 return(TIFFReadDirEntryErrOk);
3106 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
3109 return(TIFFReadDirEntryErrRange);
3111 return(TIFFReadDirEntryErrOk);
3114 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
3116 if ((value<0)||(value>0xFF))
3117 return(TIFFReadDirEntryErrRange);
3119 return(TIFFReadDirEntryErrOk);
3122 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
3125 return(TIFFReadDirEntryErrRange);
3127 return(TIFFReadDirEntryErrOk);
3130 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
3133 return(TIFFReadDirEntryErrRange);
3135 return(TIFFReadDirEntryErrOk);
3138 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
3140 if ((value<-0x80)||(value>0x7F))
3141 return(TIFFReadDirEntryErrRange);
3143 return(TIFFReadDirEntryErrOk);
3146 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
3149 return(TIFFReadDirEntryErrRange);
3151 return(TIFFReadDirEntryErrOk);
3154 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
3156 if ((value<-0x80)||(value>0x7F))
3157 return(TIFFReadDirEntryErrRange);
3159 return(TIFFReadDirEntryErrOk);
3162 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
3165 return(TIFFReadDirEntryErrRange);
3167 return(TIFFReadDirEntryErrOk);
3170 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
3172 if ((value<-0x80)||(value>0x7F))
3173 return(TIFFReadDirEntryErrRange);
3175 return(TIFFReadDirEntryErrOk);
3178 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
3181 return(TIFFReadDirEntryErrRange);
3183 return(TIFFReadDirEntryErrOk);
3186 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
3189 return(TIFFReadDirEntryErrRange);
3191 return(TIFFReadDirEntryErrOk);
3194 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
3197 return(TIFFReadDirEntryErrRange);
3199 return(TIFFReadDirEntryErrOk);
3202 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
3204 if ((value<0)||(value>0xFFFF))
3205 return(TIFFReadDirEntryErrRange);
3207 return(TIFFReadDirEntryErrOk);
3210 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
3213 return(TIFFReadDirEntryErrRange);
3215 return(TIFFReadDirEntryErrOk);
3218 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
3220 if ((value<0)||(value>0xFFFF))
3221 return(TIFFReadDirEntryErrRange);
3223 return(TIFFReadDirEntryErrOk);
3226 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
3229 return(TIFFReadDirEntryErrRange);
3231 return(TIFFReadDirEntryErrOk);
3234 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
3237 return(TIFFReadDirEntryErrRange);
3239 return(TIFFReadDirEntryErrOk);
3242 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
3244 if ((value<-0x8000)||(value>0x7FFF))
3245 return(TIFFReadDirEntryErrRange);
3247 return(TIFFReadDirEntryErrOk);
3250 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
3253 return(TIFFReadDirEntryErrRange);
3255 return(TIFFReadDirEntryErrOk);
3258 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
3260 if ((value<-0x8000)||(value>0x7FFF))
3261 return(TIFFReadDirEntryErrRange);
3263 return(TIFFReadDirEntryErrOk);
3266 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
3269 return(TIFFReadDirEntryErrRange);
3271 return(TIFFReadDirEntryErrOk);
3274 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
3277 return(TIFFReadDirEntryErrRange);
3279 return(TIFFReadDirEntryErrOk);
3282 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
3285 return(TIFFReadDirEntryErrRange);
3287 return(TIFFReadDirEntryErrOk);
3291 * Largest 32-bit unsigned integer value.
3293 #define TIFF_UINT32_MAX 0xFFFFFFFFU
3295 static enum TIFFReadDirEntryErr
3296 TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
3298 if (value > TIFF_UINT32_MAX)
3299 return(TIFFReadDirEntryErrRange);
3301 return(TIFFReadDirEntryErrOk);
3304 static enum TIFFReadDirEntryErr
3305 TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
3307 if ((value < 0) || (value > (int64) TIFF_UINT32_MAX))
3308 return(TIFFReadDirEntryErrRange);
3310 return(TIFFReadDirEntryErrOk);
3313 #undef TIFF_UINT32_MAX
3315 static enum TIFFReadDirEntryErr
3316 TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
3318 if (value > 0x7FFFFFFFUL)
3319 return(TIFFReadDirEntryErrRange);
3321 return(TIFFReadDirEntryErrOk);
3324 /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3325 static enum TIFFReadDirEntryErr
3326 TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
3328 if (value > 0x7FFFFFFF)
3329 return(TIFFReadDirEntryErrRange);
3331 return(TIFFReadDirEntryErrOk);
3334 /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3335 static enum TIFFReadDirEntryErr
3336 TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
3338 if ((value < 0-((int64) 0x7FFFFFFF+1)) || (value > 0x7FFFFFFF))
3339 return(TIFFReadDirEntryErrRange);
3341 return(TIFFReadDirEntryErrOk);
3344 static enum TIFFReadDirEntryErr
3345 TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
3348 return(TIFFReadDirEntryErrRange);
3350 return(TIFFReadDirEntryErrOk);
3353 static enum TIFFReadDirEntryErr
3354 TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
3357 return(TIFFReadDirEntryErrRange);
3359 return(TIFFReadDirEntryErrOk);
3362 static enum TIFFReadDirEntryErr
3363 TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
3366 return(TIFFReadDirEntryErrRange);
3368 return(TIFFReadDirEntryErrOk);
3371 static enum TIFFReadDirEntryErr
3372 TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
3375 return(TIFFReadDirEntryErrRange);
3377 return(TIFFReadDirEntryErrOk);
3381 * Largest 64-bit signed integer value.
3383 #define TIFF_INT64_MAX ((int64)(((uint64) ~0) >> 1))
3385 static enum TIFFReadDirEntryErr
3386 TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
3388 if (value > TIFF_INT64_MAX)
3389 return(TIFFReadDirEntryErrRange);
3391 return(TIFFReadDirEntryErrOk);
3394 #undef TIFF_INT64_MAX
3396 static enum TIFFReadDirEntryErr
3397 TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
3400 if (!isMapped(tif)) {
3401 if (!SeekOK(tif,offset))
3402 return(TIFFReadDirEntryErrIo);
3403 if (!ReadOK(tif,dest,size))
3404 return(TIFFReadDirEntryErrIo);
3409 if (((uint64)ma!=offset)
3411 || (mb - ma != (size_t) size)
3412 || (mb < (size_t)size)
3413 || (mb > (size_t)tif->tif_size)
3415 return(TIFFReadDirEntryErrIo);
3416 _TIFFmemcpy(dest,tif->tif_base+ma,size);
3418 return(TIFFReadDirEntryErrOk);
3421 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
3425 case TIFFReadDirEntryErrCount:
3426 TIFFErrorExt(tif->tif_clientdata, module,
3427 "Incorrect count for \"%s\"",
3430 case TIFFReadDirEntryErrType:
3431 TIFFErrorExt(tif->tif_clientdata, module,
3432 "Incompatible type for \"%s\"",
3435 case TIFFReadDirEntryErrIo:
3436 TIFFErrorExt(tif->tif_clientdata, module,
3437 "IO error during reading of \"%s\"",
3440 case TIFFReadDirEntryErrRange:
3441 TIFFErrorExt(tif->tif_clientdata, module,
3442 "Incorrect value for \"%s\"",
3445 case TIFFReadDirEntryErrPsdif:
3446 TIFFErrorExt(tif->tif_clientdata, module,
3447 "Cannot handle different values per sample for \"%s\"",
3450 case TIFFReadDirEntryErrSizesan:
3451 TIFFErrorExt(tif->tif_clientdata, module,
3452 "Sanity check on size of \"%s\" value failed",
3455 case TIFFReadDirEntryErrAlloc:
3456 TIFFErrorExt(tif->tif_clientdata, module,
3457 "Out of memory reading of \"%s\"",
3461 assert(0); /* we should never get here */
3466 case TIFFReadDirEntryErrCount:
3467 TIFFWarningExt(tif->tif_clientdata, module,
3468 "Incorrect count for \"%s\"; tag ignored",
3471 case TIFFReadDirEntryErrType:
3472 TIFFWarningExt(tif->tif_clientdata, module,
3473 "Incompatible type for \"%s\"; tag ignored",
3476 case TIFFReadDirEntryErrIo:
3477 TIFFWarningExt(tif->tif_clientdata, module,
3478 "IO error during reading of \"%s\"; tag ignored",
3481 case TIFFReadDirEntryErrRange:
3482 TIFFWarningExt(tif->tif_clientdata, module,
3483 "Incorrect value for \"%s\"; tag ignored",
3486 case TIFFReadDirEntryErrPsdif:
3487 TIFFWarningExt(tif->tif_clientdata, module,
3488 "Cannot handle different values per sample for \"%s\"; tag ignored",
3491 case TIFFReadDirEntryErrSizesan:
3492 TIFFWarningExt(tif->tif_clientdata, module,
3493 "Sanity check on size of \"%s\" value failed; tag ignored",
3496 case TIFFReadDirEntryErrAlloc:
3497 TIFFWarningExt(tif->tif_clientdata, module,
3498 "Out of memory reading of \"%s\"; tag ignored",
3502 assert(0); /* we should never get here */
3509 * Return the maximum number of color channels specified for a given photometric
3510 * type. 0 is returned if photometric type isn't supported or no default value
3511 * is defined by the specification.
3513 static int _TIFFGetMaxColorChannels( uint16 photometric )
3515 switch (photometric) {
3516 case PHOTOMETRIC_PALETTE:
3517 case PHOTOMETRIC_MINISWHITE:
3518 case PHOTOMETRIC_MINISBLACK:
3520 case PHOTOMETRIC_YCBCR:
3521 case PHOTOMETRIC_RGB:
3522 case PHOTOMETRIC_CIELAB:
3523 case PHOTOMETRIC_LOGLUV:
3524 case PHOTOMETRIC_ITULAB:
3525 case PHOTOMETRIC_ICCLAB:
3527 case PHOTOMETRIC_SEPARATED:
3528 case PHOTOMETRIC_MASK:
3530 case PHOTOMETRIC_LOGL:
3531 case PHOTOMETRIC_CFA:
3538 * Read the next TIFF directory from a file and convert it to the internal
3539 * format. We read directories sequentially.
3542 TIFFReadDirectory(TIFF* tif)
3544 static const char module[] = "TIFFReadDirectory";
3549 const TIFFField* fip;
3550 uint32 fii=FAILED_FII;
3552 int bitspersample_read = FALSE;
3555 tif->tif_diroff=tif->tif_nextdiroff;
3556 if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
3557 return 0; /* last offset or bad offset (IFD looping) */
3558 (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
3560 nextdiroff = tif->tif_nextdiroff;
3561 dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
3564 TIFFErrorExt(tif->tif_clientdata,module,
3565 "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
3568 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
3571 * Mark duplicates of any tag to be ignored (bugzilla 1994)
3572 * to avoid certain pathological problems.
3577 for (ma=dir, mb=0; mb<dircount; ma++, mb++)
3581 for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
3583 if (ma->tdir_tag==na->tdir_tag)
3584 na->tdir_tag=IGNORE;
3589 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
3590 tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
3591 /* free any old stuff and reinit */
3592 TIFFFreeDirectory(tif);
3593 TIFFDefaultDirectory(tif);
3595 * Electronic Arts writes gray-scale TIFF files
3596 * without a PlanarConfiguration directory entry.
3597 * Thus we setup a default value here, even though
3598 * the TIFF spec says there is no default value.
3600 TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3602 * Setup default value and then make a pass over
3603 * the fields to check type and tag information,
3604 * and to extract info required to size data
3605 * structures. A second pass is made afterwards
3606 * to read in everything not taken in the first pass.
3607 * But we must process the Compression tag first
3608 * in order to merge in codec-private tag definitions (otherwise
3609 * we may get complaints about unknown tags). However, the
3610 * Compression tag may be dependent on the SamplesPerPixel
3611 * tag value because older TIFF specs permitted Compression
3612 * to be written as a SamplesPerPixel-count tag entry.
3613 * Thus if we don't first figure out the correct SamplesPerPixel
3614 * tag value then we may end up ignoring the Compression tag
3615 * value because it has an incorrect count value (if the
3616 * true value of SamplesPerPixel is not 1).
3618 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
3621 if (!TIFFFetchNormalTag(tif,dp,0))
3623 dp->tdir_tag=IGNORE;
3625 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
3629 * The 5.0 spec says the Compression tag has one value, while
3630 * earlier specs say it has one value per sample. Because of
3631 * this, we accept the tag if one value is supplied with either
3635 enum TIFFReadDirEntryErr err;
3636 err=TIFFReadDirEntryShort(tif,dp,&value);
3637 if (err==TIFFReadDirEntryErrCount)
3638 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3639 if (err!=TIFFReadDirEntryErrOk)
3641 TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
3644 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
3646 dp->tdir_tag=IGNORE;
3650 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
3654 * First real pass over the directory.
3656 for (di=0, dp=dir; di<dircount; di++, dp++)
3658 if (dp->tdir_tag!=IGNORE)
3660 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3661 if (fii == FAILED_FII)
3663 TIFFWarningExt(tif->tif_clientdata, module,
3664 "Unknown field with tag %d (0x%x) encountered",
3665 dp->tdir_tag,dp->tdir_tag);
3666 /* the following knowingly leaks the
3667 anonymous field structure */
3668 if (!_TIFFMergeFields(tif,
3669 _TIFFCreateAnonField(tif,
3671 (TIFFDataType) dp->tdir_type),
3673 TIFFWarningExt(tif->tif_clientdata,
3675 "Registering anonymous field with tag %d (0x%x) failed",
3678 dp->tdir_tag=IGNORE;
3680 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3681 assert(fii != FAILED_FII);
3685 if (dp->tdir_tag!=IGNORE)
3687 fip=tif->tif_fields[fii];
3688 if (fip->field_bit==FIELD_IGNORE)
3689 dp->tdir_tag=IGNORE;
3692 switch (dp->tdir_tag)
3694 case TIFFTAG_STRIPOFFSETS:
3695 case TIFFTAG_STRIPBYTECOUNTS:
3696 case TIFFTAG_TILEOFFSETS:
3697 case TIFFTAG_TILEBYTECOUNTS:
3698 TIFFSetFieldBit(tif,fip->field_bit);
3700 case TIFFTAG_IMAGEWIDTH:
3701 case TIFFTAG_IMAGELENGTH:
3702 case TIFFTAG_IMAGEDEPTH:
3703 case TIFFTAG_TILELENGTH:
3704 case TIFFTAG_TILEWIDTH:
3705 case TIFFTAG_TILEDEPTH:
3706 case TIFFTAG_PLANARCONFIG:
3707 case TIFFTAG_ROWSPERSTRIP:
3708 case TIFFTAG_EXTRASAMPLES:
3709 if (!TIFFFetchNormalTag(tif,dp,0))
3711 dp->tdir_tag=IGNORE;
3714 if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )
3715 dp->tdir_tag=IGNORE;
3723 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3724 * c) strip offsets/bytecounts tag are both present and
3725 * d) both contain exactly one value, then we consistently find
3726 * that the buggy implementation of the buggy compression scheme
3727 * matches contig planarconfig best. So we 'fix-up' the tag here
3729 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
3730 (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
3732 if (!_TIFFFillStriles(tif))
3734 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
3735 if ((dp!=0)&&(dp->tdir_count==1))
3737 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
3738 TIFFTAG_STRIPBYTECOUNTS);
3739 if ((dp!=0)&&(dp->tdir_count==1))
3741 tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
3742 TIFFWarningExt(tif->tif_clientdata,module,
3743 "Planarconfig tag value assumed incorrect, "
3744 "assuming data is contig instead of chunky");
3749 * Allocate directory structure and setup defaults.
3751 if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
3753 MissingRequired(tif,"ImageLength");
3757 * Setup appropriate structures (by strip or by tile)
3759 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3760 tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
3761 tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3762 tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
3763 tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
3764 tif->tif_flags &= ~TIFF_ISTILED;
3766 tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
3767 tif->tif_flags |= TIFF_ISTILED;
3769 if (!tif->tif_dir.td_nstrips) {
3770 TIFFErrorExt(tif->tif_clientdata, module,
3771 "Cannot handle zero number of %s",
3772 isTiled(tif) ? "tiles" : "strips");
3775 tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
3776 if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
3777 tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
3778 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
3779 #ifdef OJPEG_SUPPORT
3780 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
3781 (isTiled(tif)==0) &&
3782 (tif->tif_dir.td_nstrips==1)) {
3785 * If a) compression is OJPEG, b) it's not a tiled TIFF,
3786 * and c) the number of strips is 1,
3787 * then we tolerate the absence of stripoffsets tag,
3788 * because, presumably, all required data is in the
3789 * JpegInterchangeFormat stream.
3791 TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
3795 MissingRequired(tif,
3796 isTiled(tif) ? "TileOffsets" : "StripOffsets");
3801 * Second pass: extract other information.
3803 for (di=0, dp=dir; di<dircount; di++, dp++)
3805 switch (dp->tdir_tag)
3809 case TIFFTAG_MINSAMPLEVALUE:
3810 case TIFFTAG_MAXSAMPLEVALUE:
3811 case TIFFTAG_BITSPERSAMPLE:
3812 case TIFFTAG_DATATYPE:
3813 case TIFFTAG_SAMPLEFORMAT:
3815 * The MinSampleValue, MaxSampleValue, BitsPerSample
3816 * DataType and SampleFormat tags are supposed to be
3817 * written as one value/sample, but some vendors
3818 * incorrectly write one value only -- so we accept
3819 * that as well (yuck). Other vendors write correct
3820 * value for NumberOfSamples, but incorrect one for
3821 * BitsPerSample and friends, and we will read this
3826 enum TIFFReadDirEntryErr err;
3827 err=TIFFReadDirEntryShort(tif,dp,&value);
3828 if (err==TIFFReadDirEntryErrCount)
3829 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3830 if (err!=TIFFReadDirEntryErrOk)
3832 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3833 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3836 if (!TIFFSetField(tif,dp->tdir_tag,value))
3838 if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )
3839 bitspersample_read = TRUE;
3842 case TIFFTAG_SMINSAMPLEVALUE:
3843 case TIFFTAG_SMAXSAMPLEVALUE:
3846 double *data = NULL;
3847 enum TIFFReadDirEntryErr err;
3850 if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
3851 err = TIFFReadDirEntryErrCount;
3853 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
3854 if (err!=TIFFReadDirEntryErrOk)
3856 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3857 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3860 saved_flags = tif->tif_flags;
3861 tif->tif_flags |= TIFF_PERSAMPLE;
3862 m = TIFFSetField(tif,dp->tdir_tag,data);
3863 tif->tif_flags = saved_flags;
3869 case TIFFTAG_STRIPOFFSETS:
3870 case TIFFTAG_TILEOFFSETS:
3871 #if defined(DEFER_STRILE_LOAD)
3872 _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
3873 dp, sizeof(TIFFDirEntry) );
3875 if( tif->tif_dir.td_stripoffset != NULL )
3877 TIFFErrorExt(tif->tif_clientdata, module,
3878 "tif->tif_dir.td_stripoffset is "
3879 "already allocated. Likely duplicated "
3880 "StripOffsets/TileOffsets tag");
3883 if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
3887 case TIFFTAG_STRIPBYTECOUNTS:
3888 case TIFFTAG_TILEBYTECOUNTS:
3889 #if defined(DEFER_STRILE_LOAD)
3890 _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
3891 dp, sizeof(TIFFDirEntry) );
3893 if( tif->tif_dir.td_stripbytecount != NULL )
3895 TIFFErrorExt(tif->tif_clientdata, module,
3896 "tif->tif_dir.td_stripbytecount is "
3897 "already allocated. Likely duplicated "
3898 "StripByteCounts/TileByteCounts tag");
3901 if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
3905 case TIFFTAG_COLORMAP:
3906 case TIFFTAG_TRANSFERFUNCTION:
3908 enum TIFFReadDirEntryErr err;
3909 uint32 countpersample;
3910 uint32 countrequired;
3911 uint32 incrementpersample;
3913 /* It would be dangerous to instantiate those tag values */
3914 /* since if td_bitspersample has not yet been read (due to */
3915 /* unordered tags), it could be read afterwards with a */
3916 /* values greater than the default one (1), which may cause */
3917 /* crashes in user code */
3918 if( !bitspersample_read )
3920 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3921 TIFFWarningExt(tif->tif_clientdata,module,
3922 "Ignoring %s since BitsPerSample tag not found",
3923 fip ? fip->field_name : "unknown tagname");
3926 /* ColorMap or TransferFunction for high bit */
3927 /* depths do not make much sense and could be */
3928 /* used as a denial of service vector */
3929 if (tif->tif_dir.td_bitspersample > 24)
3931 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3932 TIFFWarningExt(tif->tif_clientdata,module,
3933 "Ignoring %s because BitsPerSample=%d>24",
3934 fip ? fip->field_name : "unknown tagname",
3935 tif->tif_dir.td_bitspersample);
3938 countpersample=(1U<<tif->tif_dir.td_bitspersample);
3939 if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
3941 countrequired=countpersample;
3942 incrementpersample=0;
3946 countrequired=3*countpersample;
3947 incrementpersample=countpersample;
3949 if (dp->tdir_count!=(uint64)countrequired)
3950 err=TIFFReadDirEntryErrCount;
3952 err=TIFFReadDirEntryShortArray(tif,dp,&value);
3953 if (err!=TIFFReadDirEntryErrOk)
3955 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3956 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
3960 TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
3965 /* BEGIN REV 4.0 COMPATIBILITY */
3966 case TIFFTAG_OSUBFILETYPE:
3970 if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
3974 case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
3975 case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
3976 default: value=0; break;
3979 TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
3983 /* END REV 4.0 COMPATIBILITY */
3985 (void) TIFFFetchNormalTag(tif, dp, TRUE);
3991 * - If a) compression is OJPEG, and b) photometric tag is missing,
3992 * then we consistently find that photometric should be YCbCr
3993 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
3994 * then we consistently find that the buggy implementation of the
3995 * buggy compression scheme matches photometric YCbCr instead.
3996 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
3997 * then we consistently find bitspersample should be 8.
3998 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3999 * and c) photometric is RGB or YCbCr, then we consistently find
4000 * samplesperpixel should be 3
4001 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4002 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
4003 * find samplesperpixel should be 3
4005 if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
4007 if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
4009 TIFFWarningExt(tif->tif_clientdata, module,
4010 "Photometric tag is missing, assuming data is YCbCr");
4011 if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
4014 else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
4016 tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
4017 TIFFWarningExt(tif->tif_clientdata, module,
4018 "Photometric tag value assumed incorrect, "
4019 "assuming data is YCbCr instead of RGB");
4021 if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
4023 TIFFWarningExt(tif->tif_clientdata,module,
4024 "BitsPerSample tag is missing, assuming 8 bits per sample");
4025 if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
4028 if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
4030 if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
4032 TIFFWarningExt(tif->tif_clientdata,module,
4033 "SamplesPerPixel tag is missing, "
4034 "assuming correct SamplesPerPixel value is 3");
4035 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
4038 if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
4040 TIFFWarningExt(tif->tif_clientdata,module,
4041 "SamplesPerPixel tag is missing, "
4042 "applying correct SamplesPerPixel value of 3");
4043 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
4046 else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
4047 || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
4050 * SamplesPerPixel tag is missing, but is not required
4051 * by spec. Assume correct SamplesPerPixel value of 1.
4053 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
4060 * Make sure all non-color channels are extrasamples.
4061 * If it's not the case, define them as such.
4063 color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
4064 if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {
4065 uint16 old_extrasamples;
4066 uint16 *new_sampleinfo;
4068 TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "
4069 "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4070 "Defining non-color channels as ExtraSamples.");
4072 old_extrasamples = tif->tif_dir.td_extrasamples;
4073 tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);
4075 // sampleinfo should contain information relative to these new extra samples
4076 new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));
4077 if (!new_sampleinfo) {
4078 TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "
4079 "temporary new sampleinfo array (%d 16 bit elements)",
4080 tif->tif_dir.td_extrasamples);
4084 memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));
4085 _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);
4086 _TIFFfree(new_sampleinfo);
4090 * Verify Palette image has a Colormap.
4092 if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
4093 !TIFFFieldSet(tif, FIELD_COLORMAP)) {
4094 if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
4095 tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
4096 else if (tif->tif_dir.td_bitspersample>=8)
4097 tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
4099 MissingRequired(tif, "Colormap");
4105 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
4108 if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
4111 * Attempt to deal with a missing StripByteCounts tag.
4113 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
4115 * Some manufacturers violate the spec by not giving
4116 * the size of the strips. In this case, assume there
4117 * is one uncompressed strip of data.
4119 if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
4120 tif->tif_dir.td_nstrips > 1) ||
4121 (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
4122 tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
4123 MissingRequired(tif, "StripByteCounts");
4126 TIFFWarningExt(tif->tif_clientdata, module,
4127 "TIFF directory is missing required "
4128 "\"StripByteCounts\" field, calculating from imagelength");
4129 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4132 * Assume we have wrong StripByteCount value (in case
4133 * of single strip) in following cases:
4134 * - it is equal to zero along with StripOffset;
4135 * - it is larger than file itself (in case of uncompressed
4137 * - it is smaller than the size of the bytes per row
4138 * multiplied on the number of rows. The last case should
4139 * not be checked in the case of writing new image,
4140 * because we may do not know the exact strip size
4141 * until the whole image will be written and directory
4144 #define BYTECOUNTLOOKSBAD \
4145 ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
4146 (tif->tif_dir.td_compression == COMPRESSION_NONE && \
4147 (tif->tif_dir.td_stripoffset[0] <= TIFFGetFileSize(tif) && \
4148 tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0])) || \
4149 (tif->tif_mode == O_RDONLY && \
4150 tif->tif_dir.td_compression == COMPRESSION_NONE && \
4151 tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
4153 } else if (tif->tif_dir.td_nstrips == 1
4154 && !(tif->tif_flags&TIFF_ISTILED)
4155 && _TIFFFillStriles(tif)
4156 && tif->tif_dir.td_stripoffset[0] != 0
4157 && BYTECOUNTLOOKSBAD) {
4159 * XXX: Plexus (and others) sometimes give a value of
4160 * zero for a tag when they don't know what the
4161 * correct value is! Try and handle the simple case
4162 * of estimating the size of a one strip image.
4164 TIFFWarningExt(tif->tif_clientdata, module,
4165 "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
4166 if(EstimateStripByteCounts(tif, dir, dircount) < 0)
4169 #if !defined(DEFER_STRILE_LOAD)
4170 } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
4171 && tif->tif_dir.td_nstrips > 2
4172 && tif->tif_dir.td_compression == COMPRESSION_NONE
4173 && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
4174 && tif->tif_dir.td_stripbytecount[0] != 0
4175 && tif->tif_dir.td_stripbytecount[1] != 0 ) {
4177 * XXX: Some vendors fill StripByteCount array with
4178 * absolutely wrong values (it can be equal to
4179 * StripOffset array, for example). Catch this case
4182 * We avoid this check if deferring strile loading
4183 * as it would always force us to load the strip/tile
4186 TIFFWarningExt(tif->tif_clientdata, module,
4187 "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
4188 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4190 #endif /* !defined(DEFER_STRILE_LOAD) */
4198 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
4200 if (tif->tif_dir.td_bitspersample>=16)
4201 tif->tif_dir.td_maxsamplevalue=0xFFFF;
4203 tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
4206 * XXX: We can optimize checking for the strip bounds using the sorted
4207 * bytecounts array. See also comments for TIFFAppendToStrip()
4208 * function in tif_write.c.
4210 #if !defined(DEFER_STRILE_LOAD)
4211 if (tif->tif_dir.td_nstrips > 1) {
4214 tif->tif_dir.td_stripbytecountsorted = 1;
4215 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
4216 if (tif->tif_dir.td_stripoffset[strip - 1] >
4217 tif->tif_dir.td_stripoffset[strip]) {
4218 tif->tif_dir.td_stripbytecountsorted = 0;
4223 #endif /* !defined(DEFER_STRILE_LOAD) */
4226 * An opportunity for compression mode dependent tag fixup
4228 (*tif->tif_fixuptags)(tif);
4231 * Some manufacturers make life difficult by writing
4232 * large amounts of uncompressed data as a single strip.
4233 * This is contrary to the recommendations of the spec.
4234 * The following makes an attempt at breaking such images
4235 * into strips closer to the recommended 8k bytes. A
4236 * side effect, however, is that the RowsPerStrip tag
4237 * value may be changed.
4239 if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
4240 (tif->tif_dir.td_nstrips==1)&&
4241 (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
4242 ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
4244 if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
4246 ChopUpSingleUncompressedStrip(tif);
4250 * Clear the dirty directory flag.
4252 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
4253 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
4256 * Reinitialize i/o since we are starting on a new directory.
4258 tif->tif_row = (uint32) -1;
4259 tif->tif_curstrip = (uint32) -1;
4260 tif->tif_col = (uint32) -1;
4261 tif->tif_curtile = (uint32) -1;
4262 tif->tif_tilesize = (tmsize_t) -1;
4264 tif->tif_scanlinesize = TIFFScanlineSize(tif);
4265 if (!tif->tif_scanlinesize) {
4266 TIFFErrorExt(tif->tif_clientdata, module,
4267 "Cannot handle zero scanline size");
4272 tif->tif_tilesize = TIFFTileSize(tif);
4273 if (!tif->tif_tilesize) {
4274 TIFFErrorExt(tif->tif_clientdata, module,
4275 "Cannot handle zero tile size");
4279 if (!TIFFStripSize(tif)) {
4280 TIFFErrorExt(tif->tif_clientdata, module,
4281 "Cannot handle zero strip size");
4293 TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4295 static const char module[] = "TIFFReadDirectoryCheckOrder";
4300 for (n=0, o=dir; n<dircount; n++, o++)
4304 TIFFWarningExt(tif->tif_clientdata,module,
4305 "Invalid TIFF directory; tags are not sorted in ascending order");
4312 static TIFFDirEntry*
4313 TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
4318 for (m=dir, n=0; n<dircount; m++, n++)
4320 if (m->tdir_tag==tagid)
4327 TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
4331 mc=(int32)tif->tif_nfields;
4340 if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
4342 if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
4351 if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
4359 * Read custom directory from the arbitrary offset.
4360 * The code is very similar to TIFFReadDirectory().
4363 TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
4364 const TIFFFieldArray* infoarray)
4366 static const char module[] = "TIFFReadCustomDirectory";
4371 const TIFFField* fip;
4373 _TIFFSetupFields(tif, infoarray);
4374 dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
4377 TIFFErrorExt(tif->tif_clientdata,module,
4378 "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
4381 TIFFFreeDirectory(tif);
4382 _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
4383 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
4384 for (di=0, dp=dir; di<dircount; di++, dp++)
4386 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4387 if (fii == FAILED_FII)
4389 TIFFWarningExt(tif->tif_clientdata, module,
4390 "Unknown field with tag %d (0x%x) encountered",
4391 dp->tdir_tag, dp->tdir_tag);
4392 if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
4394 (TIFFDataType) dp->tdir_type),
4396 TIFFWarningExt(tif->tif_clientdata, module,
4397 "Registering anonymous field with tag %d (0x%x) failed",
4398 dp->tdir_tag, dp->tdir_tag);
4399 dp->tdir_tag=IGNORE;
4401 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4402 assert( fii != FAILED_FII );
4405 if (dp->tdir_tag!=IGNORE)
4407 fip=tif->tif_fields[fii];
4408 if (fip->field_bit==FIELD_IGNORE)
4409 dp->tdir_tag=IGNORE;
4412 /* check data type */
4413 while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
4416 if ((fii==tif->tif_nfields)||
4417 (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
4422 fip=tif->tif_fields[fii];
4426 TIFFWarningExt(tif->tif_clientdata, module,
4427 "Wrong data type %d for \"%s\"; tag ignored",
4428 dp->tdir_type,fip->field_name);
4429 dp->tdir_tag=IGNORE;
4433 /* check count if known in advance */
4434 if ((fip->field_readcount!=TIFF_VARIABLE)&&
4435 (fip->field_readcount!=TIFF_VARIABLE2))
4438 if (fip->field_readcount==TIFF_SPP)
4439 expected=(uint32)tif->tif_dir.td_samplesperpixel;
4441 expected=(uint32)fip->field_readcount;
4442 if (!CheckDirCount(tif,dp,expected))
4443 dp->tdir_tag=IGNORE;
4447 switch (dp->tdir_tag)
4451 case EXIFTAG_SUBJECTDISTANCE:
4452 (void) TIFFFetchSubjectDistance(tif,dp);
4455 (void) TIFFFetchNormalTag(tif, dp, TRUE);
4466 * EXIF is important special case of custom IFD, so we have a special
4467 * function to read it.
4470 TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
4472 const TIFFFieldArray* exifFieldArray;
4473 exifFieldArray = _TIFFGetExifFields();
4474 return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
4478 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4480 static const char module[] = "EstimateStripByteCounts";
4483 TIFFDirectory *td = &tif->tif_dir;
4486 /* Do not try to load stripbytecount as we will compute it */
4487 if( !_TIFFFillStrilesInternal( tif, 0 ) )
4490 if (td->td_stripbytecount)
4491 _TIFFfree(td->td_stripbytecount);
4492 td->td_stripbytecount = (uint64*)
4493 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
4494 "for \"StripByteCounts\" array");
4495 if( td->td_stripbytecount == NULL )
4498 if (td->td_compression != COMPRESSION_NONE) {
4502 filesize = TIFFGetFileSize(tif);
4503 if (!(tif->tif_flags&TIFF_BIGTIFF))
4504 space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
4506 space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
4507 /* calculate amount of space used by indirect values */
4508 for (dp = dir, n = dircount; n > 0; n--, dp++)
4512 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4513 if (typewidth == 0) {
4514 TIFFErrorExt(tif->tif_clientdata, module,
4515 "Cannot determine size of unknown tag type %d",
4519 datasize=(uint64)typewidth*dp->tdir_count;
4520 if (!(tif->tif_flags&TIFF_BIGTIFF))
4532 if( filesize < space )
4533 /* we should perhaps return in error ? */
4536 space = filesize - space;
4537 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
4538 space /= td->td_samplesperpixel;
4539 for (strip = 0; strip < td->td_nstrips; strip++)
4540 td->td_stripbytecount[strip] = space;
4542 * This gross hack handles the case were the offset to
4543 * the last strip is past the place where we think the strip
4544 * should begin. Since a strip of data must be contiguous,
4545 * it's safe to assume that we've overestimated the amount
4546 * of data in the strip and trim this number back accordingly.
4549 if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
4550 td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
4551 } else if (isTiled(tif)) {
4552 uint64 bytespertile = TIFFTileSize64(tif);
4554 for (strip = 0; strip < td->td_nstrips; strip++)
4555 td->td_stripbytecount[strip] = bytespertile;
4557 uint64 rowbytes = TIFFScanlineSize64(tif);
4558 uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
4559 for (strip = 0; strip < td->td_nstrips; strip++)
4560 td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
4562 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
4563 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
4564 td->td_rowsperstrip = td->td_imagelength;
4569 MissingRequired(TIFF* tif, const char* tagname)
4571 static const char module[] = "MissingRequired";
4573 TIFFErrorExt(tif->tif_clientdata, module,
4574 "TIFF directory is missing required \"%s\" field",
4579 * Check the directory offset against the list of already seen directory
4580 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4581 * file with looped directory pointers. We will maintain a list of already
4582 * seen directories and check every IFD offset against that list.
4585 TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
4589 if (diroff == 0) /* no more directories */
4591 if (tif->tif_dirnumber == 65535) {
4592 TIFFErrorExt(tif->tif_clientdata, "TIFFCheckDirOffset",
4593 "Cannot handle more than 65535 TIFF directories");
4597 for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
4598 if (tif->tif_dirlist[n] == diroff)
4602 tif->tif_dirnumber++;
4604 if (tif->tif_dirlist == NULL || tif->tif_dirnumber > tif->tif_dirlistsize) {
4605 uint64* new_dirlist;
4608 * XXX: Reduce memory allocation granularity of the dirlist
4611 new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
4612 tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
4615 if( tif->tif_dirnumber >= 32768 )
4616 tif->tif_dirlistsize = 65535;
4618 tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
4619 tif->tif_dirlist = new_dirlist;
4622 tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
4628 * Check the count field of a directory entry against a known value. The
4629 * caller is expected to skip/ignore the tag if there is a mismatch.
4632 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
4634 if ((uint64)count > dir->tdir_count) {
4635 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4636 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4637 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
4638 fip ? fip->field_name : "unknown tagname",
4639 dir->tdir_count, count);
4641 } else if ((uint64)count < dir->tdir_count) {
4642 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4643 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4644 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
4645 fip ? fip->field_name : "unknown tagname",
4646 dir->tdir_count, count);
4647 dir->tdir_count = count;
4654 * Read IFD structure from the specified offset. If the pointer to
4655 * nextdiroff variable has been specified, read it too. Function returns a
4656 * number of fields in the directory or 0 if failed.
4659 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
4662 static const char module[] = "TIFFFetchDirectory";
4674 tif->tif_diroff = diroff;
4677 if (!isMapped(tif)) {
4678 if (!SeekOK(tif, tif->tif_diroff)) {
4679 TIFFErrorExt(tif->tif_clientdata, module,
4680 "%s: Seek error accessing TIFF directory",
4684 if (!(tif->tif_flags&TIFF_BIGTIFF))
4686 if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
4687 TIFFErrorExt(tif->tif_clientdata, module,
4688 "%s: Can not read TIFF directory count",
4692 if (tif->tif_flags & TIFF_SWAB)
4693 TIFFSwabShort(&dircount16);
4694 if (dircount16>4096)
4696 TIFFErrorExt(tif->tif_clientdata, module,
4697 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4703 if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
4704 TIFFErrorExt(tif->tif_clientdata, module,
4705 "%s: Can not read TIFF directory count",
4709 if (tif->tif_flags & TIFF_SWAB)
4710 TIFFSwabLong8(&dircount64);
4711 if (dircount64>4096)
4713 TIFFErrorExt(tif->tif_clientdata, module,
4714 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4717 dircount16 = (uint16)dircount64;
4720 origdir = _TIFFCheckMalloc(tif, dircount16,
4721 dirsize, "to read TIFF directory");
4722 if (origdir == NULL)
4724 if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
4725 TIFFErrorExt(tif->tif_clientdata, module,
4726 "%.100s: Can not read TIFF directory",
4732 * Read offset to next directory for sequential scans if
4737 if (!(tif->tif_flags&TIFF_BIGTIFF))
4739 uint32 nextdiroff32;
4740 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
4742 if (tif->tif_flags&TIFF_SWAB)
4743 TIFFSwabLong(&nextdiroff32);
4744 *nextdiroff=nextdiroff32;
4746 if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
4748 if (tif->tif_flags&TIFF_SWAB)
4749 TIFFSwabLong8(nextdiroff);
4754 tmsize_t off = (tmsize_t) tif->tif_diroff;
4755 if ((uint64)off!=tif->tif_diroff)
4757 TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
4762 * Check for integer overflow when validating the dir_off,
4763 * otherwise a very high offset may cause an OOB read and
4764 * crash the client. Make two comparisons instead of
4766 * off + sizeof(uint16) > tif->tif_size
4768 * to avoid overflow.
4770 if (!(tif->tif_flags&TIFF_BIGTIFF))
4772 m=off+sizeof(uint16);
4773 if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
4774 TIFFErrorExt(tif->tif_clientdata, module,
4775 "Can not read TIFF directory count");
4778 _TIFFmemcpy(&dircount16, tif->tif_base + off,
4781 off += sizeof (uint16);
4782 if (tif->tif_flags & TIFF_SWAB)
4783 TIFFSwabShort(&dircount16);
4784 if (dircount16>4096)
4786 TIFFErrorExt(tif->tif_clientdata, module,
4787 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4795 m=off+sizeof(uint64);
4796 if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
4797 TIFFErrorExt(tif->tif_clientdata, module,
4798 "Can not read TIFF directory count");
4801 _TIFFmemcpy(&dircount64, tif->tif_base + off,
4804 off += sizeof (uint64);
4805 if (tif->tif_flags & TIFF_SWAB)
4806 TIFFSwabLong8(&dircount64);
4807 if (dircount64>4096)
4809 TIFFErrorExt(tif->tif_clientdata, module,
4810 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4813 dircount16 = (uint16)dircount64;
4816 if (dircount16 == 0 )
4818 TIFFErrorExt(tif->tif_clientdata, module,
4819 "Sanity check on directory count failed, zero tag directories not supported");
4822 origdir = _TIFFCheckMalloc(tif, dircount16,
4824 "to read TIFF directory");
4825 if (origdir == NULL)
4827 m=off+dircount16*dirsize;
4828 if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
4829 TIFFErrorExt(tif->tif_clientdata, module,
4830 "Can not read TIFF directory");
4834 _TIFFmemcpy(origdir, tif->tif_base + off,
4835 dircount16 * dirsize);
4838 off += dircount16 * dirsize;
4839 if (!(tif->tif_flags&TIFF_BIGTIFF))
4841 uint32 nextdiroff32;
4842 m=off+sizeof(uint32);
4843 if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
4846 _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
4848 if (tif->tif_flags&TIFF_SWAB)
4849 TIFFSwabLong(&nextdiroff32);
4850 *nextdiroff = nextdiroff32;
4854 m=off+sizeof(uint64);
4855 if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
4858 _TIFFmemcpy(nextdiroff, tif->tif_base + off,
4860 if (tif->tif_flags&TIFF_SWAB)
4861 TIFFSwabLong8(nextdiroff);
4865 dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
4866 sizeof(TIFFDirEntry),
4867 "to read TIFF directory");
4875 for (n=0; n<dircount16; n++)
4877 if (tif->tif_flags&TIFF_SWAB)
4878 TIFFSwabShort((uint16*)ma);
4879 mb->tdir_tag=*(uint16*)ma;
4881 if (tif->tif_flags&TIFF_SWAB)
4882 TIFFSwabShort((uint16*)ma);
4883 mb->tdir_type=*(uint16*)ma;
4885 if (!(tif->tif_flags&TIFF_BIGTIFF))
4887 if (tif->tif_flags&TIFF_SWAB)
4888 TIFFSwabLong((uint32*)ma);
4889 mb->tdir_count=(uint64)(*(uint32*)ma);
4891 *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
4896 if (tif->tif_flags&TIFF_SWAB)
4897 TIFFSwabLong8((uint64*)ma);
4898 mb->tdir_count=TIFFReadUInt64(ma);
4900 mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
4911 * Fetch a tag that is not handled by special case code.
4914 TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
4916 static const char module[] = "TIFFFetchNormalTag";
4917 enum TIFFReadDirEntryErr err;
4919 const TIFFField* fip = NULL;
4920 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4921 if( fii == FAILED_FII )
4923 TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
4924 "No definition found for tag %d",
4928 fip=tif->tif_fields[fii];
4929 assert(fip != NULL); /* should not happen */
4930 assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
4931 assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
4932 err=TIFFReadDirEntryErrOk;
4933 switch (fip->set_field_type)
4935 case TIFF_SETGET_UNDEFINED:
4937 case TIFF_SETGET_ASCII:
4940 assert(fip->field_passcount==0);
4941 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4942 if (err==TIFFReadDirEntryErrOk)
4949 while (mb<(uint32)dp->tdir_count)
4957 if (mb+1<(uint32)dp->tdir_count)
4958 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);
4959 else if (mb+1>(uint32)dp->tdir_count)
4962 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
4963 if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
4966 o=_TIFFmalloc((uint32)dp->tdir_count+1);
4973 _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
4974 o[(uint32)dp->tdir_count]=0;
4979 n=TIFFSetField(tif,dp->tdir_tag,data);
4987 case TIFF_SETGET_UINT8:
4990 assert(fip->field_readcount==1);
4991 assert(fip->field_passcount==0);
4992 err=TIFFReadDirEntryByte(tif,dp,&data);
4993 if (err==TIFFReadDirEntryErrOk)
4995 if (!TIFFSetField(tif,dp->tdir_tag,data))
5000 case TIFF_SETGET_UINT16:
5003 assert(fip->field_readcount==1);
5004 assert(fip->field_passcount==0);
5005 err=TIFFReadDirEntryShort(tif,dp,&data);
5006 if (err==TIFFReadDirEntryErrOk)
5008 if (!TIFFSetField(tif,dp->tdir_tag,data))
5013 case TIFF_SETGET_UINT32:
5016 assert(fip->field_readcount==1);
5017 assert(fip->field_passcount==0);
5018 err=TIFFReadDirEntryLong(tif,dp,&data);
5019 if (err==TIFFReadDirEntryErrOk)
5021 if (!TIFFSetField(tif,dp->tdir_tag,data))
5026 case TIFF_SETGET_UINT64:
5029 assert(fip->field_readcount==1);
5030 assert(fip->field_passcount==0);
5031 err=TIFFReadDirEntryLong8(tif,dp,&data);
5032 if (err==TIFFReadDirEntryErrOk)
5034 if (!TIFFSetField(tif,dp->tdir_tag,data))
5039 case TIFF_SETGET_FLOAT:
5042 assert(fip->field_readcount==1);
5043 assert(fip->field_passcount==0);
5044 err=TIFFReadDirEntryFloat(tif,dp,&data);
5045 if (err==TIFFReadDirEntryErrOk)
5047 if (!TIFFSetField(tif,dp->tdir_tag,data))
5052 case TIFF_SETGET_DOUBLE:
5055 assert(fip->field_readcount==1);
5056 assert(fip->field_passcount==0);
5057 err=TIFFReadDirEntryDouble(tif,dp,&data);
5058 if (err==TIFFReadDirEntryErrOk)
5060 if (!TIFFSetField(tif,dp->tdir_tag,data))
5065 case TIFF_SETGET_IFD8:
5068 assert(fip->field_readcount==1);
5069 assert(fip->field_passcount==0);
5070 err=TIFFReadDirEntryIfd8(tif,dp,&data);
5071 if (err==TIFFReadDirEntryErrOk)
5073 if (!TIFFSetField(tif,dp->tdir_tag,data))
5078 case TIFF_SETGET_UINT16_PAIR:
5081 assert(fip->field_readcount==2);
5082 assert(fip->field_passcount==0);
5083 if (dp->tdir_count!=2) {
5084 TIFFWarningExt(tif->tif_clientdata,module,
5085 "incorrect count for field \"%s\", expected 2, got %d",
5086 fip->field_name,(int)dp->tdir_count);
5089 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5090 if (err==TIFFReadDirEntryErrOk)
5093 m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
5100 case TIFF_SETGET_C0_UINT8:
5103 assert(fip->field_readcount>=1);
5104 assert(fip->field_passcount==0);
5105 if (dp->tdir_count!=(uint64)fip->field_readcount) {
5106 TIFFWarningExt(tif->tif_clientdata,module,
5107 "incorrect count for field \"%s\", expected %d, got %d",
5108 fip->field_name,(int) fip->field_readcount, (int)dp->tdir_count);
5113 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5114 if (err==TIFFReadDirEntryErrOk)
5117 m=TIFFSetField(tif,dp->tdir_tag,data);
5126 case TIFF_SETGET_C0_UINT16:
5129 assert(fip->field_readcount>=1);
5130 assert(fip->field_passcount==0);
5131 if (dp->tdir_count!=(uint64)fip->field_readcount)
5135 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5136 if (err==TIFFReadDirEntryErrOk)
5139 m=TIFFSetField(tif,dp->tdir_tag,data);
5148 case TIFF_SETGET_C0_UINT32:
5151 assert(fip->field_readcount>=1);
5152 assert(fip->field_passcount==0);
5153 if (dp->tdir_count!=(uint64)fip->field_readcount)
5157 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5158 if (err==TIFFReadDirEntryErrOk)
5161 m=TIFFSetField(tif,dp->tdir_tag,data);
5170 case TIFF_SETGET_C0_FLOAT:
5173 assert(fip->field_readcount>=1);
5174 assert(fip->field_passcount==0);
5175 if (dp->tdir_count!=(uint64)fip->field_readcount)
5179 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5180 if (err==TIFFReadDirEntryErrOk)
5183 m=TIFFSetField(tif,dp->tdir_tag,data);
5192 case TIFF_SETGET_C16_ASCII:
5195 assert(fip->field_readcount==TIFF_VARIABLE);
5196 assert(fip->field_passcount==1);
5197 if (dp->tdir_count>0xFFFF)
5198 err=TIFFReadDirEntryErrCount;
5201 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5202 if (err==TIFFReadDirEntryErrOk)
5205 if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
5207 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
5208 data[dp->tdir_count-1] = '\0';
5210 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5219 case TIFF_SETGET_C16_UINT8:
5222 assert(fip->field_readcount==TIFF_VARIABLE);
5223 assert(fip->field_passcount==1);
5224 if (dp->tdir_count>0xFFFF)
5225 err=TIFFReadDirEntryErrCount;
5228 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5229 if (err==TIFFReadDirEntryErrOk)
5232 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5241 case TIFF_SETGET_C16_UINT16:
5244 assert(fip->field_readcount==TIFF_VARIABLE);
5245 assert(fip->field_passcount==1);
5246 if (dp->tdir_count>0xFFFF)
5247 err=TIFFReadDirEntryErrCount;
5250 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5251 if (err==TIFFReadDirEntryErrOk)
5254 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5263 case TIFF_SETGET_C16_UINT32:
5266 assert(fip->field_readcount==TIFF_VARIABLE);
5267 assert(fip->field_passcount==1);
5268 if (dp->tdir_count>0xFFFF)
5269 err=TIFFReadDirEntryErrCount;
5272 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5273 if (err==TIFFReadDirEntryErrOk)
5276 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5285 case TIFF_SETGET_C16_UINT64:
5288 assert(fip->field_readcount==TIFF_VARIABLE);
5289 assert(fip->field_passcount==1);
5290 if (dp->tdir_count>0xFFFF)
5291 err=TIFFReadDirEntryErrCount;
5294 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5295 if (err==TIFFReadDirEntryErrOk)
5298 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5307 case TIFF_SETGET_C16_FLOAT:
5310 assert(fip->field_readcount==TIFF_VARIABLE);
5311 assert(fip->field_passcount==1);
5312 if (dp->tdir_count>0xFFFF)
5313 err=TIFFReadDirEntryErrCount;
5316 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5317 if (err==TIFFReadDirEntryErrOk)
5320 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5329 case TIFF_SETGET_C16_DOUBLE:
5332 assert(fip->field_readcount==TIFF_VARIABLE);
5333 assert(fip->field_passcount==1);
5334 if (dp->tdir_count>0xFFFF)
5335 err=TIFFReadDirEntryErrCount;
5338 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5339 if (err==TIFFReadDirEntryErrOk)
5342 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5351 case TIFF_SETGET_C16_IFD8:
5354 assert(fip->field_readcount==TIFF_VARIABLE);
5355 assert(fip->field_passcount==1);
5356 if (dp->tdir_count>0xFFFF)
5357 err=TIFFReadDirEntryErrCount;
5360 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5361 if (err==TIFFReadDirEntryErrOk)
5364 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5373 case TIFF_SETGET_C32_ASCII:
5376 assert(fip->field_readcount==TIFF_VARIABLE2);
5377 assert(fip->field_passcount==1);
5378 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5379 if (err==TIFFReadDirEntryErrOk)
5382 if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
5384 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
5385 data[dp->tdir_count-1] = '\0';
5387 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5395 case TIFF_SETGET_C32_UINT8:
5398 assert(fip->field_readcount==TIFF_VARIABLE2);
5399 assert(fip->field_passcount==1);
5400 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5401 if (err==TIFFReadDirEntryErrOk)
5404 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5412 case TIFF_SETGET_C32_SINT8:
5415 assert(fip->field_readcount==TIFF_VARIABLE2);
5416 assert(fip->field_passcount==1);
5417 err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
5418 if (err==TIFFReadDirEntryErrOk)
5421 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5429 case TIFF_SETGET_C32_UINT16:
5432 assert(fip->field_readcount==TIFF_VARIABLE2);
5433 assert(fip->field_passcount==1);
5434 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5435 if (err==TIFFReadDirEntryErrOk)
5438 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5446 case TIFF_SETGET_C32_SINT16:
5449 assert(fip->field_readcount==TIFF_VARIABLE2);
5450 assert(fip->field_passcount==1);
5451 err=TIFFReadDirEntrySshortArray(tif,dp,&data);
5452 if (err==TIFFReadDirEntryErrOk)
5455 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5463 case TIFF_SETGET_C32_UINT32:
5466 assert(fip->field_readcount==TIFF_VARIABLE2);
5467 assert(fip->field_passcount==1);
5468 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5469 if (err==TIFFReadDirEntryErrOk)
5472 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5480 case TIFF_SETGET_C32_SINT32:
5483 assert(fip->field_readcount==TIFF_VARIABLE2);
5484 assert(fip->field_passcount==1);
5485 err=TIFFReadDirEntrySlongArray(tif,dp,&data);
5486 if (err==TIFFReadDirEntryErrOk)
5489 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5497 case TIFF_SETGET_C32_UINT64:
5500 assert(fip->field_readcount==TIFF_VARIABLE2);
5501 assert(fip->field_passcount==1);
5502 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5503 if (err==TIFFReadDirEntryErrOk)
5506 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5514 case TIFF_SETGET_C32_SINT64:
5517 assert(fip->field_readcount==TIFF_VARIABLE2);
5518 assert(fip->field_passcount==1);
5519 err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
5520 if (err==TIFFReadDirEntryErrOk)
5523 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5531 case TIFF_SETGET_C32_FLOAT:
5534 assert(fip->field_readcount==TIFF_VARIABLE2);
5535 assert(fip->field_passcount==1);
5536 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5537 if (err==TIFFReadDirEntryErrOk)
5540 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5548 case TIFF_SETGET_C32_DOUBLE:
5551 assert(fip->field_readcount==TIFF_VARIABLE2);
5552 assert(fip->field_passcount==1);
5553 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5554 if (err==TIFFReadDirEntryErrOk)
5557 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5565 case TIFF_SETGET_C32_IFD8:
5568 assert(fip->field_readcount==TIFF_VARIABLE2);
5569 assert(fip->field_passcount==1);
5570 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5571 if (err==TIFFReadDirEntryErrOk)
5574 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5583 assert(0); /* we should never get here */
5586 if (err!=TIFFReadDirEntryErrOk)
5588 TIFFReadDirEntryOutputErr(tif,err,module,fip->field_name,recover);
5595 * Fetch a set of offsets or lengths.
5596 * While this routine says "strips", in fact it's also used for tiles.
5599 TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
5601 static const char module[] = "TIFFFetchStripThing";
5602 enum TIFFReadDirEntryErr err;
5604 err=TIFFReadDirEntryLong8ArrayWithLimit(tif,dir,&data,nstrips);
5605 if (err!=TIFFReadDirEntryErrOk)
5607 const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5608 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
5611 if (dir->tdir_count<(uint64)nstrips)
5613 uint64* resizeddata;
5614 const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5615 const char* pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
5616 uint32 max_nstrips = 1000000;
5618 max_nstrips = (uint32) atoi(pszMax);
5619 TIFFReadDirEntryOutputErr(tif,TIFFReadDirEntryErrCount,
5621 fip ? fip->field_name : "unknown tagname",
5622 ( nstrips <= max_nstrips ) );
5624 if( nstrips > max_nstrips )
5630 resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
5631 if (resizeddata==0) {
5635 _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
5636 _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
5645 * Fetch and set the SubjectDistance EXIF tag.
5648 TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
5650 static const char module[] = "TIFFFetchSubjectDistance";
5651 enum TIFFReadDirEntryErr err;
5654 assert(sizeof(double)==8);
5655 assert(sizeof(uint64)==8);
5656 assert(sizeof(uint32)==4);
5657 if (dir->tdir_count!=1)
5658 err=TIFFReadDirEntryErrCount;
5659 else if (dir->tdir_type!=TIFF_RATIONAL)
5660 err=TIFFReadDirEntryErrType;
5663 if (!(tif->tif_flags&TIFF_BIGTIFF))
5666 offset=*(uint32*)(&dir->tdir_offset);
5667 if (tif->tif_flags&TIFF_SWAB)
5668 TIFFSwabLong(&offset);
5669 err=TIFFReadDirEntryData(tif,offset,8,m.i);
5673 m.l=dir->tdir_offset.toff_long8;
5674 err=TIFFReadDirEntryErrOk;
5677 if (err==TIFFReadDirEntryErrOk)
5680 if (tif->tif_flags&TIFF_SWAB)
5681 TIFFSwabArrayOfLong(m.i,2);
5684 else if (m.i[0]==0xFFFFFFFF)
5686 * XXX: Numerator 0xFFFFFFFF means that we have infinite
5687 * distance. Indicate that with a negative floating point
5688 * SubjectDistance value.
5692 n=(double)m.i[0]/(double)m.i[1];
5693 return(TIFFSetField(tif,dir->tdir_tag,n));
5697 TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
5703 * Replace a single strip (tile) of uncompressed data by multiple strips
5704 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5705 * dealing with large images or for dealing with machines with a limited
5709 ChopUpSingleUncompressedStrip(TIFF* tif)
5711 register TIFFDirectory *td = &tif->tif_dir;
5715 uint64 rowblockbytes;
5719 uint32 rowsperstrip;
5723 bytecount = td->td_stripbytecount[0];
5724 /* On a newly created file, just re-opened to be filled, we */
5725 /* don't want strip chop to trigger as it is going to cause issues */
5726 /* later ( StripOffsets and StripByteCounts improperly filled) . */
5727 if( bytecount == 0 && tif->tif_mode != O_RDONLY )
5729 offset = td->td_stripoffset[0];
5730 assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
5731 if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
5732 (!isUpSampled(tif)))
5733 rowblock = td->td_ycbcrsubsampling[1];
5736 rowblockbytes = TIFFVTileSize64(tif, rowblock);
5738 * Make the rows hold at least one scanline, but fill specified amount
5739 * of data if possible.
5741 if (rowblockbytes > STRIP_SIZE_DEFAULT) {
5742 stripbytes = rowblockbytes;
5743 rowsperstrip = rowblock;
5744 } else if (rowblockbytes > 0 ) {
5745 uint32 rowblocksperstrip;
5746 rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
5747 rowsperstrip = rowblocksperstrip * rowblock;
5748 stripbytes = rowblocksperstrip * rowblockbytes;
5754 * never increase the number of rows per strip
5756 if (rowsperstrip >= td->td_rowsperstrip)
5758 nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
5762 /* If we are going to allocate a lot of memory, make sure that the */
5763 /* file is as big as needed */
5764 if( tif->tif_mode == O_RDONLY &&
5765 nstrips > 1000000 &&
5766 (offset >= TIFFGetFileSize(tif) ||
5767 stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)) )
5772 newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
5773 "for chopped \"StripByteCounts\" array");
5774 newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
5775 "for chopped \"StripOffsets\" array");
5776 if (newcounts == NULL || newoffsets == NULL) {
5778 * Unable to allocate new strip information, give up and use
5779 * the original one strip information.
5781 if (newcounts != NULL)
5782 _TIFFfree(newcounts);
5783 if (newoffsets != NULL)
5784 _TIFFfree(newoffsets);
5788 * Fill the strip information arrays with new bytecounts and offsets
5789 * that reflect the broken-up format.
5791 for (strip = 0; strip < nstrips; strip++) {
5792 if (stripbytes > bytecount)
5793 stripbytes = bytecount;
5794 newcounts[strip] = stripbytes;
5795 newoffsets[strip] = stripbytes ? offset : 0;
5796 offset += stripbytes;
5797 bytecount -= stripbytes;
5800 * Replace old single strip info with multi-strip info.
5802 td->td_stripsperimage = td->td_nstrips = nstrips;
5803 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
5805 _TIFFfree(td->td_stripbytecount);
5806 _TIFFfree(td->td_stripoffset);
5807 td->td_stripbytecount = newcounts;
5808 td->td_stripoffset = newoffsets;
5809 td->td_stripbytecountsorted = 1;
5812 int _TIFFFillStriles( TIFF *tif )
5814 return _TIFFFillStrilesInternal( tif, 1 );
5817 static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount )
5819 #if defined(DEFER_STRILE_LOAD)
5820 register TIFFDirectory *td = &tif->tif_dir;
5821 int return_value = 1;
5823 if( td->td_stripoffset != NULL )
5826 if( td->td_stripoffset_entry.tdir_count == 0 )
5829 if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
5830 td->td_nstrips,&td->td_stripoffset))
5835 if (loadStripByteCount &&
5836 !TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
5837 td->td_nstrips,&td->td_stripbytecount))
5842 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
5843 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
5845 if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
5848 tif->tif_dir.td_stripbytecountsorted = 1;
5849 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
5850 if (tif->tif_dir.td_stripoffset[strip - 1] >
5851 tif->tif_dir.td_stripoffset[strip]) {
5852 tif->tif_dir.td_stripbytecountsorted = 0;
5858 return return_value;
5859 #else /* !defined(DEFER_STRILE_LOAD) */
5861 (void) loadStripByteCount;
5867 /* vim: set ts=8 sts=8 sw=8 noet: */