1 /* $Id: tif_dirread.c,v 1.191 2015-09-05 20:31:41 bfriesen Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Directory Read Support Routines.
33 /* Suggested pending improvements:
34 * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
35 * eliminating current use of the IGNORE value, and therefore eliminating
36 * current irrational behaviour on tags with tag id code 0
37 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
38 * the pointer to the appropriate TIFFField structure early on in
39 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
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);
169 typedef union _UInt64Aligned_t
179 Unaligned safe copy of a uint64 value from an octet array.
181 static uint64 TIFFReadUInt64(const uint8 *value)
183 UInt64Aligned_t result;
185 result.c[0]=value[0];
186 result.c[1]=value[1];
187 result.c[2]=value[2];
188 result.c[3]=value[3];
189 result.c[4]=value[4];
190 result.c[5]=value[5];
191 result.c[6]=value[6];
192 result.c[7]=value[7];
197 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
199 enum TIFFReadDirEntryErr err;
200 if (direntry->tdir_count!=1)
201 return(TIFFReadDirEntryErrCount);
202 switch (direntry->tdir_type)
205 TIFFReadDirEntryCheckedByte(tif,direntry,value);
206 return(TIFFReadDirEntryErrOk);
210 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
211 err=TIFFReadDirEntryCheckRangeByteSbyte(m);
212 if (err!=TIFFReadDirEntryErrOk)
215 return(TIFFReadDirEntryErrOk);
220 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
221 err=TIFFReadDirEntryCheckRangeByteShort(m);
222 if (err!=TIFFReadDirEntryErrOk)
225 return(TIFFReadDirEntryErrOk);
230 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
231 err=TIFFReadDirEntryCheckRangeByteSshort(m);
232 if (err!=TIFFReadDirEntryErrOk)
235 return(TIFFReadDirEntryErrOk);
240 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
241 err=TIFFReadDirEntryCheckRangeByteLong(m);
242 if (err!=TIFFReadDirEntryErrOk)
245 return(TIFFReadDirEntryErrOk);
250 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
251 err=TIFFReadDirEntryCheckRangeByteSlong(m);
252 if (err!=TIFFReadDirEntryErrOk)
255 return(TIFFReadDirEntryErrOk);
260 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
261 if (err!=TIFFReadDirEntryErrOk)
263 err=TIFFReadDirEntryCheckRangeByteLong8(m);
264 if (err!=TIFFReadDirEntryErrOk)
267 return(TIFFReadDirEntryErrOk);
272 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
273 if (err!=TIFFReadDirEntryErrOk)
275 err=TIFFReadDirEntryCheckRangeByteSlong8(m);
276 if (err!=TIFFReadDirEntryErrOk)
279 return(TIFFReadDirEntryErrOk);
282 return(TIFFReadDirEntryErrType);
286 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
288 enum TIFFReadDirEntryErr err;
289 if (direntry->tdir_count!=1)
290 return(TIFFReadDirEntryErrCount);
291 switch (direntry->tdir_type)
296 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
298 return(TIFFReadDirEntryErrOk);
303 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
304 err=TIFFReadDirEntryCheckRangeShortSbyte(m);
305 if (err!=TIFFReadDirEntryErrOk)
308 return(TIFFReadDirEntryErrOk);
311 TIFFReadDirEntryCheckedShort(tif,direntry,value);
312 return(TIFFReadDirEntryErrOk);
316 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
317 err=TIFFReadDirEntryCheckRangeShortSshort(m);
318 if (err!=TIFFReadDirEntryErrOk)
321 return(TIFFReadDirEntryErrOk);
326 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
327 err=TIFFReadDirEntryCheckRangeShortLong(m);
328 if (err!=TIFFReadDirEntryErrOk)
331 return(TIFFReadDirEntryErrOk);
336 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
337 err=TIFFReadDirEntryCheckRangeShortSlong(m);
338 if (err!=TIFFReadDirEntryErrOk)
341 return(TIFFReadDirEntryErrOk);
346 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
347 if (err!=TIFFReadDirEntryErrOk)
349 err=TIFFReadDirEntryCheckRangeShortLong8(m);
350 if (err!=TIFFReadDirEntryErrOk)
353 return(TIFFReadDirEntryErrOk);
358 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
359 if (err!=TIFFReadDirEntryErrOk)
361 err=TIFFReadDirEntryCheckRangeShortSlong8(m);
362 if (err!=TIFFReadDirEntryErrOk)
365 return(TIFFReadDirEntryErrOk);
368 return(TIFFReadDirEntryErrType);
372 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
374 enum TIFFReadDirEntryErr err;
375 if (direntry->tdir_count!=1)
376 return(TIFFReadDirEntryErrCount);
377 switch (direntry->tdir_type)
382 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
384 return(TIFFReadDirEntryErrOk);
389 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
390 err=TIFFReadDirEntryCheckRangeLongSbyte(m);
391 if (err!=TIFFReadDirEntryErrOk)
394 return(TIFFReadDirEntryErrOk);
399 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
401 return(TIFFReadDirEntryErrOk);
406 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
407 err=TIFFReadDirEntryCheckRangeLongSshort(m);
408 if (err!=TIFFReadDirEntryErrOk)
411 return(TIFFReadDirEntryErrOk);
414 TIFFReadDirEntryCheckedLong(tif,direntry,value);
415 return(TIFFReadDirEntryErrOk);
419 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
420 err=TIFFReadDirEntryCheckRangeLongSlong(m);
421 if (err!=TIFFReadDirEntryErrOk)
424 return(TIFFReadDirEntryErrOk);
429 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
430 if (err!=TIFFReadDirEntryErrOk)
432 err=TIFFReadDirEntryCheckRangeLongLong8(m);
433 if (err!=TIFFReadDirEntryErrOk)
436 return(TIFFReadDirEntryErrOk);
441 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
442 if (err!=TIFFReadDirEntryErrOk)
444 err=TIFFReadDirEntryCheckRangeLongSlong8(m);
445 if (err!=TIFFReadDirEntryErrOk)
448 return(TIFFReadDirEntryErrOk);
451 return(TIFFReadDirEntryErrType);
455 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
457 enum TIFFReadDirEntryErr err;
458 if (direntry->tdir_count!=1)
459 return(TIFFReadDirEntryErrCount);
460 switch (direntry->tdir_type)
465 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
467 return(TIFFReadDirEntryErrOk);
472 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
473 err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
474 if (err!=TIFFReadDirEntryErrOk)
477 return(TIFFReadDirEntryErrOk);
482 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
484 return(TIFFReadDirEntryErrOk);
489 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
490 err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
491 if (err!=TIFFReadDirEntryErrOk)
494 return(TIFFReadDirEntryErrOk);
499 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
501 return(TIFFReadDirEntryErrOk);
506 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
507 err=TIFFReadDirEntryCheckRangeLong8Slong(m);
508 if (err!=TIFFReadDirEntryErrOk)
511 return(TIFFReadDirEntryErrOk);
514 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
519 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
520 if (err!=TIFFReadDirEntryErrOk)
522 err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
523 if (err!=TIFFReadDirEntryErrOk)
526 return(TIFFReadDirEntryErrOk);
529 return(TIFFReadDirEntryErrType);
533 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
535 enum TIFFReadDirEntryErr err;
536 if (direntry->tdir_count!=1)
537 return(TIFFReadDirEntryErrCount);
538 switch (direntry->tdir_type)
543 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
545 return(TIFFReadDirEntryErrOk);
550 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
552 return(TIFFReadDirEntryErrOk);
557 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
559 return(TIFFReadDirEntryErrOk);
564 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
566 return(TIFFReadDirEntryErrOk);
571 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
573 return(TIFFReadDirEntryErrOk);
578 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
580 return(TIFFReadDirEntryErrOk);
585 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
586 if (err!=TIFFReadDirEntryErrOk)
588 #if defined(__WIN32__) && (_MSC_VER < 1500)
590 * XXX: MSVC 6.0 does not support conversion
591 * of 64-bit integers into floating point
594 *value = _TIFFUInt64ToFloat(m);
598 return(TIFFReadDirEntryErrOk);
603 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
604 if (err!=TIFFReadDirEntryErrOk)
607 return(TIFFReadDirEntryErrOk);
612 err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
613 if (err!=TIFFReadDirEntryErrOk)
616 return(TIFFReadDirEntryErrOk);
621 err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
622 if (err!=TIFFReadDirEntryErrOk)
625 return(TIFFReadDirEntryErrOk);
628 TIFFReadDirEntryCheckedFloat(tif,direntry,value);
629 return(TIFFReadDirEntryErrOk);
633 err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
634 if (err!=TIFFReadDirEntryErrOk)
637 return(TIFFReadDirEntryErrOk);
640 return(TIFFReadDirEntryErrType);
644 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
646 enum TIFFReadDirEntryErr err;
647 if (direntry->tdir_count!=1)
648 return(TIFFReadDirEntryErrCount);
649 switch (direntry->tdir_type)
654 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
656 return(TIFFReadDirEntryErrOk);
661 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
663 return(TIFFReadDirEntryErrOk);
668 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
670 return(TIFFReadDirEntryErrOk);
675 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
677 return(TIFFReadDirEntryErrOk);
682 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
684 return(TIFFReadDirEntryErrOk);
689 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
691 return(TIFFReadDirEntryErrOk);
696 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
697 if (err!=TIFFReadDirEntryErrOk)
699 #if defined(__WIN32__) && (_MSC_VER < 1500)
701 * XXX: MSVC 6.0 does not support conversion
702 * of 64-bit integers into floating point
705 *value = _TIFFUInt64ToDouble(m);
709 return(TIFFReadDirEntryErrOk);
714 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
715 if (err!=TIFFReadDirEntryErrOk)
718 return(TIFFReadDirEntryErrOk);
721 err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
724 err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
729 TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
731 return(TIFFReadDirEntryErrOk);
734 err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
737 return(TIFFReadDirEntryErrType);
741 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
743 enum TIFFReadDirEntryErr err;
744 if (direntry->tdir_count!=1)
745 return(TIFFReadDirEntryErrCount);
746 switch (direntry->tdir_type)
752 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
754 return(TIFFReadDirEntryErrOk);
758 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
761 return(TIFFReadDirEntryErrType);
765 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
770 typesize=TIFFDataWidth(direntry->tdir_type);
771 if ((direntry->tdir_count==0)||(typesize==0))
774 return(TIFFReadDirEntryErrOk);
779 * As a sanity check, make sure we have no more than a 2GB tag array
780 * in either the current data type or the dest data type. This also
781 * avoids problems with overflow of tmsize_t on 32bit systems.
783 if ((uint64)(2147483647/typesize)<direntry->tdir_count)
784 return(TIFFReadDirEntryErrSizesan);
785 if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
786 return(TIFFReadDirEntryErrSizesan);
788 *count=(uint32)direntry->tdir_count;
789 datasize=(*count)*typesize;
790 assert((tmsize_t)datasize>0);
791 data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
793 return(TIFFReadDirEntryErrAlloc);
794 if (!(tif->tif_flags&TIFF_BIGTIFF))
797 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
800 enum TIFFReadDirEntryErr err;
801 uint32 offset = direntry->tdir_offset.toff_long;
802 if (tif->tif_flags&TIFF_SWAB)
803 TIFFSwabLong(&offset);
804 err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
805 if (err!=TIFFReadDirEntryErrOk)
815 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
818 enum TIFFReadDirEntryErr err;
819 uint64 offset = direntry->tdir_offset.toff_long8;
820 if (tif->tif_flags&TIFF_SWAB)
821 TIFFSwabLong8(&offset);
822 err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
823 if (err!=TIFFReadDirEntryErrOk)
831 return(TIFFReadDirEntryErrOk);
834 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
836 enum TIFFReadDirEntryErr err;
840 switch (direntry->tdir_type)
854 return(TIFFReadDirEntryErrType);
856 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
857 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
862 switch (direntry->tdir_type)
867 *value=(uint8*)origdata;
868 return(TIFFReadDirEntryErrOk);
874 for (n=0; n<count; n++)
876 err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
877 if (err!=TIFFReadDirEntryErrOk)
884 *value=(uint8*)origdata;
885 return(TIFFReadDirEntryErrOk);
888 data=(uint8*)_TIFFmalloc(count);
892 return(TIFFReadDirEntryErrAlloc);
894 switch (direntry->tdir_type)
901 ma=(uint16*)origdata;
903 for (n=0; n<count; n++)
905 if (tif->tif_flags&TIFF_SWAB)
907 err=TIFFReadDirEntryCheckRangeByteShort(*ma);
908 if (err!=TIFFReadDirEntryErrOk)
910 *mb++=(uint8)(*ma++);
921 for (n=0; n<count; n++)
923 if (tif->tif_flags&TIFF_SWAB)
924 TIFFSwabShort((uint16*)ma);
925 err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
926 if (err!=TIFFReadDirEntryErrOk)
928 *mb++=(uint8)(*ma++);
937 ma=(uint32*)origdata;
939 for (n=0; n<count; n++)
941 if (tif->tif_flags&TIFF_SWAB)
943 err=TIFFReadDirEntryCheckRangeByteLong(*ma);
944 if (err!=TIFFReadDirEntryErrOk)
946 *mb++=(uint8)(*ma++);
957 for (n=0; n<count; n++)
959 if (tif->tif_flags&TIFF_SWAB)
960 TIFFSwabLong((uint32*)ma);
961 err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
962 if (err!=TIFFReadDirEntryErrOk)
964 *mb++=(uint8)(*ma++);
973 ma=(uint64*)origdata;
975 for (n=0; n<count; n++)
977 if (tif->tif_flags&TIFF_SWAB)
979 err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
980 if (err!=TIFFReadDirEntryErrOk)
982 *mb++=(uint8)(*ma++);
993 for (n=0; n<count; n++)
995 if (tif->tif_flags&TIFF_SWAB)
996 TIFFSwabLong8((uint64*)ma);
997 err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
998 if (err!=TIFFReadDirEntryErrOk)
1000 *mb++=(uint8)(*ma++);
1005 _TIFFfree(origdata);
1006 if (err!=TIFFReadDirEntryErrOk)
1012 return(TIFFReadDirEntryErrOk);
1015 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
1017 enum TIFFReadDirEntryErr err;
1021 switch (direntry->tdir_type)
1023 case TIFF_UNDEFINED:
1034 return(TIFFReadDirEntryErrType);
1036 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1037 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1042 switch (direntry->tdir_type)
1044 case TIFF_UNDEFINED:
1050 for (n=0; n<count; n++)
1052 err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1053 if (err!=TIFFReadDirEntryErrOk)
1055 _TIFFfree(origdata);
1060 *value=(int8*)origdata;
1061 return(TIFFReadDirEntryErrOk);
1064 *value=(int8*)origdata;
1065 return(TIFFReadDirEntryErrOk);
1067 data=(int8*)_TIFFmalloc(count);
1070 _TIFFfree(origdata);
1071 return(TIFFReadDirEntryErrAlloc);
1073 switch (direntry->tdir_type)
1080 ma=(uint16*)origdata;
1082 for (n=0; n<count; n++)
1084 if (tif->tif_flags&TIFF_SWAB)
1086 err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1087 if (err!=TIFFReadDirEntryErrOk)
1089 *mb++=(int8)(*ma++);
1098 ma=(int16*)origdata;
1100 for (n=0; n<count; n++)
1102 if (tif->tif_flags&TIFF_SWAB)
1103 TIFFSwabShort((uint16*)ma);
1104 err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1105 if (err!=TIFFReadDirEntryErrOk)
1107 *mb++=(int8)(*ma++);
1116 ma=(uint32*)origdata;
1118 for (n=0; n<count; n++)
1120 if (tif->tif_flags&TIFF_SWAB)
1122 err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1123 if (err!=TIFFReadDirEntryErrOk)
1125 *mb++=(int8)(*ma++);
1134 ma=(int32*)origdata;
1136 for (n=0; n<count; n++)
1138 if (tif->tif_flags&TIFF_SWAB)
1139 TIFFSwabLong((uint32*)ma);
1140 err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1141 if (err!=TIFFReadDirEntryErrOk)
1143 *mb++=(int8)(*ma++);
1152 ma=(uint64*)origdata;
1154 for (n=0; n<count; n++)
1156 if (tif->tif_flags&TIFF_SWAB)
1158 err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1159 if (err!=TIFFReadDirEntryErrOk)
1161 *mb++=(int8)(*ma++);
1170 ma=(int64*)origdata;
1172 for (n=0; n<count; n++)
1174 if (tif->tif_flags&TIFF_SWAB)
1175 TIFFSwabLong8((uint64*)ma);
1176 err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1177 if (err!=TIFFReadDirEntryErrOk)
1179 *mb++=(int8)(*ma++);
1184 _TIFFfree(origdata);
1185 if (err!=TIFFReadDirEntryErrOk)
1191 return(TIFFReadDirEntryErrOk);
1194 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
1196 enum TIFFReadDirEntryErr err;
1200 switch (direntry->tdir_type)
1212 return(TIFFReadDirEntryErrType);
1214 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1215 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1220 switch (direntry->tdir_type)
1223 *value=(uint16*)origdata;
1224 if (tif->tif_flags&TIFF_SWAB)
1225 TIFFSwabArrayOfShort(*value,count);
1226 return(TIFFReadDirEntryErrOk);
1232 for (n=0; n<count; n++)
1234 if (tif->tif_flags&TIFF_SWAB)
1235 TIFFSwabShort((uint16*)m);
1236 err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1237 if (err!=TIFFReadDirEntryErrOk)
1239 _TIFFfree(origdata);
1244 *value=(uint16*)origdata;
1245 return(TIFFReadDirEntryErrOk);
1248 data=(uint16*)_TIFFmalloc(count*2);
1251 _TIFFfree(origdata);
1252 return(TIFFReadDirEntryErrAlloc);
1254 switch (direntry->tdir_type)
1261 ma=(uint8*)origdata;
1263 for (n=0; n<count; n++)
1264 *mb++=(uint16)(*ma++);
1274 for (n=0; n<count; n++)
1276 err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1277 if (err!=TIFFReadDirEntryErrOk)
1279 *mb++=(uint16)(*ma++);
1288 ma=(uint32*)origdata;
1290 for (n=0; n<count; n++)
1292 if (tif->tif_flags&TIFF_SWAB)
1294 err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1295 if (err!=TIFFReadDirEntryErrOk)
1297 *mb++=(uint16)(*ma++);
1306 ma=(int32*)origdata;
1308 for (n=0; n<count; n++)
1310 if (tif->tif_flags&TIFF_SWAB)
1311 TIFFSwabLong((uint32*)ma);
1312 err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1313 if (err!=TIFFReadDirEntryErrOk)
1315 *mb++=(uint16)(*ma++);
1324 ma=(uint64*)origdata;
1326 for (n=0; n<count; n++)
1328 if (tif->tif_flags&TIFF_SWAB)
1330 err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1331 if (err!=TIFFReadDirEntryErrOk)
1333 *mb++=(uint16)(*ma++);
1342 ma=(int64*)origdata;
1344 for (n=0; n<count; n++)
1346 if (tif->tif_flags&TIFF_SWAB)
1347 TIFFSwabLong8((uint64*)ma);
1348 err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1349 if (err!=TIFFReadDirEntryErrOk)
1351 *mb++=(uint16)(*ma++);
1356 _TIFFfree(origdata);
1357 if (err!=TIFFReadDirEntryErrOk)
1363 return(TIFFReadDirEntryErrOk);
1366 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
1368 enum TIFFReadDirEntryErr err;
1372 switch (direntry->tdir_type)
1384 return(TIFFReadDirEntryErrType);
1386 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1387 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1392 switch (direntry->tdir_type)
1398 m=(uint16*)origdata;
1399 for (n=0; n<count; n++)
1401 if (tif->tif_flags&TIFF_SWAB)
1403 err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1404 if (err!=TIFFReadDirEntryErrOk)
1406 _TIFFfree(origdata);
1411 *value=(int16*)origdata;
1412 return(TIFFReadDirEntryErrOk);
1415 *value=(int16*)origdata;
1416 if (tif->tif_flags&TIFF_SWAB)
1417 TIFFSwabArrayOfShort((uint16*)(*value),count);
1418 return(TIFFReadDirEntryErrOk);
1420 data=(int16*)_TIFFmalloc(count*2);
1423 _TIFFfree(origdata);
1424 return(TIFFReadDirEntryErrAlloc);
1426 switch (direntry->tdir_type)
1433 ma=(uint8*)origdata;
1435 for (n=0; n<count; n++)
1436 *mb++=(int16)(*ma++);
1446 for (n=0; n<count; n++)
1447 *mb++=(int16)(*ma++);
1455 ma=(uint32*)origdata;
1457 for (n=0; n<count; n++)
1459 if (tif->tif_flags&TIFF_SWAB)
1461 err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1462 if (err!=TIFFReadDirEntryErrOk)
1464 *mb++=(int16)(*ma++);
1473 ma=(int32*)origdata;
1475 for (n=0; n<count; n++)
1477 if (tif->tif_flags&TIFF_SWAB)
1478 TIFFSwabLong((uint32*)ma);
1479 err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1480 if (err!=TIFFReadDirEntryErrOk)
1482 *mb++=(int16)(*ma++);
1491 ma=(uint64*)origdata;
1493 for (n=0; n<count; n++)
1495 if (tif->tif_flags&TIFF_SWAB)
1497 err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1498 if (err!=TIFFReadDirEntryErrOk)
1500 *mb++=(int16)(*ma++);
1509 ma=(int64*)origdata;
1511 for (n=0; n<count; n++)
1513 if (tif->tif_flags&TIFF_SWAB)
1514 TIFFSwabLong8((uint64*)ma);
1515 err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1516 if (err!=TIFFReadDirEntryErrOk)
1518 *mb++=(int16)(*ma++);
1523 _TIFFfree(origdata);
1524 if (err!=TIFFReadDirEntryErrOk)
1530 return(TIFFReadDirEntryErrOk);
1533 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
1535 enum TIFFReadDirEntryErr err;
1539 switch (direntry->tdir_type)
1551 return(TIFFReadDirEntryErrType);
1553 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1554 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1559 switch (direntry->tdir_type)
1562 *value=(uint32*)origdata;
1563 if (tif->tif_flags&TIFF_SWAB)
1564 TIFFSwabArrayOfLong(*value,count);
1565 return(TIFFReadDirEntryErrOk);
1571 for (n=0; n<count; n++)
1573 if (tif->tif_flags&TIFF_SWAB)
1574 TIFFSwabLong((uint32*)m);
1575 err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1576 if (err!=TIFFReadDirEntryErrOk)
1578 _TIFFfree(origdata);
1583 *value=(uint32*)origdata;
1584 return(TIFFReadDirEntryErrOk);
1587 data=(uint32*)_TIFFmalloc(count*4);
1590 _TIFFfree(origdata);
1591 return(TIFFReadDirEntryErrAlloc);
1593 switch (direntry->tdir_type)
1600 ma=(uint8*)origdata;
1602 for (n=0; n<count; n++)
1603 *mb++=(uint32)(*ma++);
1613 for (n=0; n<count; n++)
1615 err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1616 if (err!=TIFFReadDirEntryErrOk)
1618 *mb++=(uint32)(*ma++);
1627 ma=(uint16*)origdata;
1629 for (n=0; n<count; n++)
1631 if (tif->tif_flags&TIFF_SWAB)
1633 *mb++=(uint32)(*ma++);
1642 ma=(int16*)origdata;
1644 for (n=0; n<count; n++)
1646 if (tif->tif_flags&TIFF_SWAB)
1647 TIFFSwabShort((uint16*)ma);
1648 err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1649 if (err!=TIFFReadDirEntryErrOk)
1651 *mb++=(uint32)(*ma++);
1660 ma=(uint64*)origdata;
1662 for (n=0; n<count; n++)
1664 if (tif->tif_flags&TIFF_SWAB)
1666 err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1667 if (err!=TIFFReadDirEntryErrOk)
1669 *mb++=(uint32)(*ma++);
1678 ma=(int64*)origdata;
1680 for (n=0; n<count; n++)
1682 if (tif->tif_flags&TIFF_SWAB)
1683 TIFFSwabLong8((uint64*)ma);
1684 err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1685 if (err!=TIFFReadDirEntryErrOk)
1687 *mb++=(uint32)(*ma++);
1692 _TIFFfree(origdata);
1693 if (err!=TIFFReadDirEntryErrOk)
1699 return(TIFFReadDirEntryErrOk);
1702 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
1704 enum TIFFReadDirEntryErr err;
1708 switch (direntry->tdir_type)
1720 return(TIFFReadDirEntryErrType);
1722 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1723 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1728 switch (direntry->tdir_type)
1734 m=(uint32*)origdata;
1735 for (n=0; n<count; n++)
1737 if (tif->tif_flags&TIFF_SWAB)
1738 TIFFSwabLong((uint32*)m);
1739 err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1740 if (err!=TIFFReadDirEntryErrOk)
1742 _TIFFfree(origdata);
1747 *value=(int32*)origdata;
1748 return(TIFFReadDirEntryErrOk);
1751 *value=(int32*)origdata;
1752 if (tif->tif_flags&TIFF_SWAB)
1753 TIFFSwabArrayOfLong((uint32*)(*value),count);
1754 return(TIFFReadDirEntryErrOk);
1756 data=(int32*)_TIFFmalloc(count*4);
1759 _TIFFfree(origdata);
1760 return(TIFFReadDirEntryErrAlloc);
1762 switch (direntry->tdir_type)
1769 ma=(uint8*)origdata;
1771 for (n=0; n<count; n++)
1772 *mb++=(int32)(*ma++);
1782 for (n=0; n<count; n++)
1783 *mb++=(int32)(*ma++);
1791 ma=(uint16*)origdata;
1793 for (n=0; n<count; n++)
1795 if (tif->tif_flags&TIFF_SWAB)
1797 *mb++=(int32)(*ma++);
1806 ma=(int16*)origdata;
1808 for (n=0; n<count; n++)
1810 if (tif->tif_flags&TIFF_SWAB)
1811 TIFFSwabShort((uint16*)ma);
1812 *mb++=(int32)(*ma++);
1821 ma=(uint64*)origdata;
1823 for (n=0; n<count; n++)
1825 if (tif->tif_flags&TIFF_SWAB)
1827 err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1828 if (err!=TIFFReadDirEntryErrOk)
1830 *mb++=(int32)(*ma++);
1839 ma=(int64*)origdata;
1841 for (n=0; n<count; n++)
1843 if (tif->tif_flags&TIFF_SWAB)
1844 TIFFSwabLong8((uint64*)ma);
1845 err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1846 if (err!=TIFFReadDirEntryErrOk)
1848 *mb++=(int32)(*ma++);
1853 _TIFFfree(origdata);
1854 if (err!=TIFFReadDirEntryErrOk)
1860 return(TIFFReadDirEntryErrOk);
1863 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
1865 enum TIFFReadDirEntryErr err;
1869 switch (direntry->tdir_type)
1881 return(TIFFReadDirEntryErrType);
1883 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
1884 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1889 switch (direntry->tdir_type)
1892 *value=(uint64*)origdata;
1893 if (tif->tif_flags&TIFF_SWAB)
1894 TIFFSwabArrayOfLong8(*value,count);
1895 return(TIFFReadDirEntryErrOk);
1901 for (n=0; n<count; n++)
1903 if (tif->tif_flags&TIFF_SWAB)
1904 TIFFSwabLong8((uint64*)m);
1905 err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
1906 if (err!=TIFFReadDirEntryErrOk)
1908 _TIFFfree(origdata);
1913 *value=(uint64*)origdata;
1914 return(TIFFReadDirEntryErrOk);
1917 data=(uint64*)_TIFFmalloc(count*8);
1920 _TIFFfree(origdata);
1921 return(TIFFReadDirEntryErrAlloc);
1923 switch (direntry->tdir_type)
1930 ma=(uint8*)origdata;
1932 for (n=0; n<count; n++)
1933 *mb++=(uint64)(*ma++);
1943 for (n=0; n<count; n++)
1945 err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
1946 if (err!=TIFFReadDirEntryErrOk)
1948 *mb++=(uint64)(*ma++);
1957 ma=(uint16*)origdata;
1959 for (n=0; n<count; n++)
1961 if (tif->tif_flags&TIFF_SWAB)
1963 *mb++=(uint64)(*ma++);
1972 ma=(int16*)origdata;
1974 for (n=0; n<count; n++)
1976 if (tif->tif_flags&TIFF_SWAB)
1977 TIFFSwabShort((uint16*)ma);
1978 err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
1979 if (err!=TIFFReadDirEntryErrOk)
1981 *mb++=(uint64)(*ma++);
1990 ma=(uint32*)origdata;
1992 for (n=0; n<count; n++)
1994 if (tif->tif_flags&TIFF_SWAB)
1996 *mb++=(uint64)(*ma++);
2005 ma=(int32*)origdata;
2007 for (n=0; n<count; n++)
2009 if (tif->tif_flags&TIFF_SWAB)
2010 TIFFSwabLong((uint32*)ma);
2011 err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2012 if (err!=TIFFReadDirEntryErrOk)
2014 *mb++=(uint64)(*ma++);
2019 _TIFFfree(origdata);
2020 if (err!=TIFFReadDirEntryErrOk)
2026 return(TIFFReadDirEntryErrOk);
2029 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
2031 enum TIFFReadDirEntryErr err;
2035 switch (direntry->tdir_type)
2047 return(TIFFReadDirEntryErrType);
2049 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2050 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2055 switch (direntry->tdir_type)
2061 m=(uint64*)origdata;
2062 for (n=0; n<count; n++)
2064 if (tif->tif_flags&TIFF_SWAB)
2066 err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2067 if (err!=TIFFReadDirEntryErrOk)
2069 _TIFFfree(origdata);
2074 *value=(int64*)origdata;
2075 return(TIFFReadDirEntryErrOk);
2078 *value=(int64*)origdata;
2079 if (tif->tif_flags&TIFF_SWAB)
2080 TIFFSwabArrayOfLong8((uint64*)(*value),count);
2081 return(TIFFReadDirEntryErrOk);
2083 data=(int64*)_TIFFmalloc(count*8);
2086 _TIFFfree(origdata);
2087 return(TIFFReadDirEntryErrAlloc);
2089 switch (direntry->tdir_type)
2096 ma=(uint8*)origdata;
2098 for (n=0; n<count; n++)
2099 *mb++=(int64)(*ma++);
2109 for (n=0; n<count; n++)
2110 *mb++=(int64)(*ma++);
2118 ma=(uint16*)origdata;
2120 for (n=0; n<count; n++)
2122 if (tif->tif_flags&TIFF_SWAB)
2124 *mb++=(int64)(*ma++);
2133 ma=(int16*)origdata;
2135 for (n=0; n<count; n++)
2137 if (tif->tif_flags&TIFF_SWAB)
2138 TIFFSwabShort((uint16*)ma);
2139 *mb++=(int64)(*ma++);
2148 ma=(uint32*)origdata;
2150 for (n=0; n<count; n++)
2152 if (tif->tif_flags&TIFF_SWAB)
2154 *mb++=(int64)(*ma++);
2163 ma=(int32*)origdata;
2165 for (n=0; n<count; n++)
2167 if (tif->tif_flags&TIFF_SWAB)
2168 TIFFSwabLong((uint32*)ma);
2169 *mb++=(int64)(*ma++);
2174 _TIFFfree(origdata);
2176 return(TIFFReadDirEntryErrOk);
2179 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2181 enum TIFFReadDirEntryErr err;
2185 switch (direntry->tdir_type)
2196 case TIFF_SRATIONAL:
2201 return(TIFFReadDirEntryErrType);
2203 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2204 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2209 switch (direntry->tdir_type)
2212 if (tif->tif_flags&TIFF_SWAB)
2213 TIFFSwabArrayOfLong((uint32*)origdata,count);
2214 TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2215 *value=(float*)origdata;
2216 return(TIFFReadDirEntryErrOk);
2218 data=(float*)_TIFFmalloc(count*sizeof(float));
2221 _TIFFfree(origdata);
2222 return(TIFFReadDirEntryErrAlloc);
2224 switch (direntry->tdir_type)
2231 ma=(uint8*)origdata;
2233 for (n=0; n<count; n++)
2234 *mb++=(float)(*ma++);
2244 for (n=0; n<count; n++)
2245 *mb++=(float)(*ma++);
2253 ma=(uint16*)origdata;
2255 for (n=0; n<count; n++)
2257 if (tif->tif_flags&TIFF_SWAB)
2259 *mb++=(float)(*ma++);
2268 ma=(int16*)origdata;
2270 for (n=0; n<count; n++)
2272 if (tif->tif_flags&TIFF_SWAB)
2273 TIFFSwabShort((uint16*)ma);
2274 *mb++=(float)(*ma++);
2283 ma=(uint32*)origdata;
2285 for (n=0; n<count; n++)
2287 if (tif->tif_flags&TIFF_SWAB)
2289 *mb++=(float)(*ma++);
2298 ma=(int32*)origdata;
2300 for (n=0; n<count; n++)
2302 if (tif->tif_flags&TIFF_SWAB)
2303 TIFFSwabLong((uint32*)ma);
2304 *mb++=(float)(*ma++);
2313 ma=(uint64*)origdata;
2315 for (n=0; n<count; n++)
2317 if (tif->tif_flags&TIFF_SWAB)
2319 #if defined(__WIN32__) && (_MSC_VER < 1500)
2321 * XXX: MSVC 6.0 does not support
2322 * conversion of 64-bit integers into
2323 * floating point values.
2325 *mb++ = _TIFFUInt64ToFloat(*ma++);
2327 *mb++ = (float)(*ma++);
2337 ma=(int64*)origdata;
2339 for (n=0; n<count; n++)
2341 if (tif->tif_flags&TIFF_SWAB)
2342 TIFFSwabLong8((uint64*)ma);
2343 *mb++=(float)(*ma++);
2354 ma=(uint32*)origdata;
2356 for (n=0; n<count; n++)
2358 if (tif->tif_flags&TIFF_SWAB)
2361 if (tif->tif_flags&TIFF_SWAB)
2367 *mb++=(float)maa/(float)mab;
2371 case TIFF_SRATIONAL:
2378 ma=(uint32*)origdata;
2380 for (n=0; n<count; n++)
2382 if (tif->tif_flags&TIFF_SWAB)
2386 if (tif->tif_flags&TIFF_SWAB)
2392 *mb++=(float)maa/(float)mab;
2401 if (tif->tif_flags&TIFF_SWAB)
2402 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2403 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2404 ma=(double*)origdata;
2406 for (n=0; n<count; n++)
2407 *mb++=(float)(*ma++);
2411 _TIFFfree(origdata);
2413 return(TIFFReadDirEntryErrOk);
2416 static enum TIFFReadDirEntryErr
2417 TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2419 enum TIFFReadDirEntryErr err;
2423 switch (direntry->tdir_type)
2434 case TIFF_SRATIONAL:
2439 return(TIFFReadDirEntryErrType);
2441 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2442 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2447 switch (direntry->tdir_type)
2450 if (tif->tif_flags&TIFF_SWAB)
2451 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2452 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2453 *value=(double*)origdata;
2454 return(TIFFReadDirEntryErrOk);
2456 data=(double*)_TIFFmalloc(count*sizeof(double));
2459 _TIFFfree(origdata);
2460 return(TIFFReadDirEntryErrAlloc);
2462 switch (direntry->tdir_type)
2469 ma=(uint8*)origdata;
2471 for (n=0; n<count; n++)
2472 *mb++=(double)(*ma++);
2482 for (n=0; n<count; n++)
2483 *mb++=(double)(*ma++);
2491 ma=(uint16*)origdata;
2493 for (n=0; n<count; n++)
2495 if (tif->tif_flags&TIFF_SWAB)
2497 *mb++=(double)(*ma++);
2506 ma=(int16*)origdata;
2508 for (n=0; n<count; n++)
2510 if (tif->tif_flags&TIFF_SWAB)
2511 TIFFSwabShort((uint16*)ma);
2512 *mb++=(double)(*ma++);
2521 ma=(uint32*)origdata;
2523 for (n=0; n<count; n++)
2525 if (tif->tif_flags&TIFF_SWAB)
2527 *mb++=(double)(*ma++);
2536 ma=(int32*)origdata;
2538 for (n=0; n<count; n++)
2540 if (tif->tif_flags&TIFF_SWAB)
2541 TIFFSwabLong((uint32*)ma);
2542 *mb++=(double)(*ma++);
2551 ma=(uint64*)origdata;
2553 for (n=0; n<count; n++)
2555 if (tif->tif_flags&TIFF_SWAB)
2557 #if defined(__WIN32__) && (_MSC_VER < 1500)
2559 * XXX: MSVC 6.0 does not support
2560 * conversion of 64-bit integers into
2561 * floating point values.
2563 *mb++ = _TIFFUInt64ToDouble(*ma++);
2565 *mb++ = (double)(*ma++);
2575 ma=(int64*)origdata;
2577 for (n=0; n<count; n++)
2579 if (tif->tif_flags&TIFF_SWAB)
2580 TIFFSwabLong8((uint64*)ma);
2581 *mb++=(double)(*ma++);
2592 ma=(uint32*)origdata;
2594 for (n=0; n<count; n++)
2596 if (tif->tif_flags&TIFF_SWAB)
2599 if (tif->tif_flags&TIFF_SWAB)
2605 *mb++=(double)maa/(double)mab;
2609 case TIFF_SRATIONAL:
2616 ma=(uint32*)origdata;
2618 for (n=0; n<count; n++)
2620 if (tif->tif_flags&TIFF_SWAB)
2624 if (tif->tif_flags&TIFF_SWAB)
2630 *mb++=(double)maa/(double)mab;
2639 if (tif->tif_flags&TIFF_SWAB)
2640 TIFFSwabArrayOfLong((uint32*)origdata,count);
2641 TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
2642 ma=(float*)origdata;
2644 for (n=0; n<count; n++)
2645 *mb++=(double)(*ma++);
2649 _TIFFfree(origdata);
2651 return(TIFFReadDirEntryErrOk);
2654 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
2656 enum TIFFReadDirEntryErr err;
2660 switch (direntry->tdir_type)
2668 return(TIFFReadDirEntryErrType);
2670 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2671 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2676 switch (direntry->tdir_type)
2680 *value=(uint64*)origdata;
2681 if (tif->tif_flags&TIFF_SWAB)
2682 TIFFSwabArrayOfLong8(*value,count);
2683 return(TIFFReadDirEntryErrOk);
2685 data=(uint64*)_TIFFmalloc(count*8);
2688 _TIFFfree(origdata);
2689 return(TIFFReadDirEntryErrAlloc);
2691 switch (direntry->tdir_type)
2699 ma=(uint32*)origdata;
2701 for (n=0; n<count; n++)
2703 if (tif->tif_flags&TIFF_SWAB)
2705 *mb++=(uint64)(*ma++);
2710 _TIFFfree(origdata);
2712 return(TIFFReadDirEntryErrOk);
2715 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2717 enum TIFFReadDirEntryErr err;
2721 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2722 return(TIFFReadDirEntryErrCount);
2723 err=TIFFReadDirEntryShortArray(tif,direntry,&m);
2724 if (err!=TIFFReadDirEntryErrOk)
2727 nb=tif->tif_dir.td_samplesperpixel;
2734 err=TIFFReadDirEntryErrPsdif;
2744 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2746 enum TIFFReadDirEntryErr err;
2750 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2751 return(TIFFReadDirEntryErrCount);
2752 err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
2753 if (err!=TIFFReadDirEntryErrOk)
2756 nb=tif->tif_dir.td_samplesperpixel;
2763 err=TIFFReadDirEntryErrPsdif;
2773 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
2776 *value=*(uint8*)(&direntry->tdir_offset);
2779 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
2782 *value=*(int8*)(&direntry->tdir_offset);
2785 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2787 *value = direntry->tdir_offset.toff_short;
2788 /* *value=*(uint16*)(&direntry->tdir_offset); */
2789 if (tif->tif_flags&TIFF_SWAB)
2790 TIFFSwabShort(value);
2793 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
2795 *value=*(int16*)(&direntry->tdir_offset);
2796 if (tif->tif_flags&TIFF_SWAB)
2797 TIFFSwabShort((uint16*)value);
2800 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
2802 *value=*(uint32*)(&direntry->tdir_offset);
2803 if (tif->tif_flags&TIFF_SWAB)
2804 TIFFSwabLong(value);
2807 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
2809 *value=*(int32*)(&direntry->tdir_offset);
2810 if (tif->tif_flags&TIFF_SWAB)
2811 TIFFSwabLong((uint32*)value);
2814 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
2816 if (!(tif->tif_flags&TIFF_BIGTIFF))
2818 enum TIFFReadDirEntryErr err;
2819 uint32 offset = direntry->tdir_offset.toff_long;
2820 if (tif->tif_flags&TIFF_SWAB)
2821 TIFFSwabLong(&offset);
2822 err=TIFFReadDirEntryData(tif,offset,8,value);
2823 if (err!=TIFFReadDirEntryErrOk)
2827 *value = direntry->tdir_offset.toff_long8;
2828 if (tif->tif_flags&TIFF_SWAB)
2829 TIFFSwabLong8(value);
2830 return(TIFFReadDirEntryErrOk);
2833 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
2835 if (!(tif->tif_flags&TIFF_BIGTIFF))
2837 enum TIFFReadDirEntryErr err;
2838 uint32 offset = direntry->tdir_offset.toff_long;
2839 if (tif->tif_flags&TIFF_SWAB)
2840 TIFFSwabLong(&offset);
2841 err=TIFFReadDirEntryData(tif,offset,8,value);
2842 if (err!=TIFFReadDirEntryErrOk)
2846 *value=*(int64*)(&direntry->tdir_offset);
2847 if (tif->tif_flags&TIFF_SWAB)
2848 TIFFSwabLong8((uint64*)value);
2849 return(TIFFReadDirEntryErrOk);
2852 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2856 assert(sizeof(double)==8);
2857 assert(sizeof(uint64)==8);
2858 assert(sizeof(uint32)==4);
2859 if (!(tif->tif_flags&TIFF_BIGTIFF))
2861 enum TIFFReadDirEntryErr err;
2862 uint32 offset = direntry->tdir_offset.toff_long;
2863 if (tif->tif_flags&TIFF_SWAB)
2864 TIFFSwabLong(&offset);
2865 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2866 if (err!=TIFFReadDirEntryErrOk)
2870 m.l = direntry->tdir_offset.toff_long8;
2871 if (tif->tif_flags&TIFF_SWAB)
2872 TIFFSwabArrayOfLong(m.i,2);
2876 *value=(double)m.i[0]/(double)m.i[1];
2877 return(TIFFReadDirEntryErrOk);
2880 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2883 assert(sizeof(double)==8);
2884 assert(sizeof(uint64)==8);
2885 assert(sizeof(int32)==4);
2886 assert(sizeof(uint32)==4);
2887 if (!(tif->tif_flags&TIFF_BIGTIFF))
2889 enum TIFFReadDirEntryErr err;
2890 uint32 offset = direntry->tdir_offset.toff_long;
2891 if (tif->tif_flags&TIFF_SWAB)
2892 TIFFSwabLong(&offset);
2893 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2894 if (err!=TIFFReadDirEntryErrOk)
2898 m.l=direntry->tdir_offset.toff_long8;
2899 if (tif->tif_flags&TIFF_SWAB)
2900 TIFFSwabArrayOfLong(m.i,2);
2901 if ((int32)m.i[0]==0)
2904 *value=(double)((int32)m.i[0])/(double)m.i[1];
2905 return(TIFFReadDirEntryErrOk);
2908 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
2915 assert(sizeof(float)==4);
2916 assert(sizeof(uint32)==4);
2917 assert(sizeof(float_union)==4);
2918 float_union.i=*(uint32*)(&direntry->tdir_offset);
2919 *value=float_union.f;
2920 if (tif->tif_flags&TIFF_SWAB)
2921 TIFFSwabLong((uint32*)value);
2924 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2926 assert(sizeof(double)==8);
2927 assert(sizeof(uint64)==8);
2928 assert(sizeof(UInt64Aligned_t)==8);
2929 if (!(tif->tif_flags&TIFF_BIGTIFF))
2931 enum TIFFReadDirEntryErr err;
2932 uint32 offset = direntry->tdir_offset.toff_long;
2933 if (tif->tif_flags&TIFF_SWAB)
2934 TIFFSwabLong(&offset);
2935 err=TIFFReadDirEntryData(tif,offset,8,value);
2936 if (err!=TIFFReadDirEntryErrOk)
2941 UInt64Aligned_t uint64_union;
2942 uint64_union.l=direntry->tdir_offset.toff_long8;
2943 *value=uint64_union.d;
2945 if (tif->tif_flags&TIFF_SWAB)
2946 TIFFSwabLong8((uint64*)value);
2947 return(TIFFReadDirEntryErrOk);
2950 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
2953 return(TIFFReadDirEntryErrRange);
2955 return(TIFFReadDirEntryErrOk);
2958 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
2961 return(TIFFReadDirEntryErrRange);
2963 return(TIFFReadDirEntryErrOk);
2966 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
2968 if ((value<0)||(value>0xFF))
2969 return(TIFFReadDirEntryErrRange);
2971 return(TIFFReadDirEntryErrOk);
2974 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
2977 return(TIFFReadDirEntryErrRange);
2979 return(TIFFReadDirEntryErrOk);
2982 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
2984 if ((value<0)||(value>0xFF))
2985 return(TIFFReadDirEntryErrRange);
2987 return(TIFFReadDirEntryErrOk);
2990 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
2993 return(TIFFReadDirEntryErrRange);
2995 return(TIFFReadDirEntryErrOk);
2998 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
3000 if ((value<0)||(value>0xFF))
3001 return(TIFFReadDirEntryErrRange);
3003 return(TIFFReadDirEntryErrOk);
3006 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
3009 return(TIFFReadDirEntryErrRange);
3011 return(TIFFReadDirEntryErrOk);
3014 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
3017 return(TIFFReadDirEntryErrRange);
3019 return(TIFFReadDirEntryErrOk);
3022 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
3024 if ((value<-0x80)||(value>0x7F))
3025 return(TIFFReadDirEntryErrRange);
3027 return(TIFFReadDirEntryErrOk);
3030 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
3033 return(TIFFReadDirEntryErrRange);
3035 return(TIFFReadDirEntryErrOk);
3038 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
3040 if ((value<-0x80)||(value>0x7F))
3041 return(TIFFReadDirEntryErrRange);
3043 return(TIFFReadDirEntryErrOk);
3046 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
3049 return(TIFFReadDirEntryErrRange);
3051 return(TIFFReadDirEntryErrOk);
3054 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
3056 if ((value<-0x80)||(value>0x7F))
3057 return(TIFFReadDirEntryErrRange);
3059 return(TIFFReadDirEntryErrOk);
3062 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
3065 return(TIFFReadDirEntryErrRange);
3067 return(TIFFReadDirEntryErrOk);
3070 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
3073 return(TIFFReadDirEntryErrRange);
3075 return(TIFFReadDirEntryErrOk);
3078 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
3081 return(TIFFReadDirEntryErrRange);
3083 return(TIFFReadDirEntryErrOk);
3086 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
3088 if ((value<0)||(value>0xFFFF))
3089 return(TIFFReadDirEntryErrRange);
3091 return(TIFFReadDirEntryErrOk);
3094 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
3097 return(TIFFReadDirEntryErrRange);
3099 return(TIFFReadDirEntryErrOk);
3102 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
3104 if ((value<0)||(value>0xFFFF))
3105 return(TIFFReadDirEntryErrRange);
3107 return(TIFFReadDirEntryErrOk);
3110 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
3113 return(TIFFReadDirEntryErrRange);
3115 return(TIFFReadDirEntryErrOk);
3118 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
3121 return(TIFFReadDirEntryErrRange);
3123 return(TIFFReadDirEntryErrOk);
3126 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
3128 if ((value<-0x8000)||(value>0x7FFF))
3129 return(TIFFReadDirEntryErrRange);
3131 return(TIFFReadDirEntryErrOk);
3134 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
3137 return(TIFFReadDirEntryErrRange);
3139 return(TIFFReadDirEntryErrOk);
3142 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
3144 if ((value<-0x8000)||(value>0x7FFF))
3145 return(TIFFReadDirEntryErrRange);
3147 return(TIFFReadDirEntryErrOk);
3150 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
3153 return(TIFFReadDirEntryErrRange);
3155 return(TIFFReadDirEntryErrOk);
3158 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
3161 return(TIFFReadDirEntryErrRange);
3163 return(TIFFReadDirEntryErrOk);
3166 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
3169 return(TIFFReadDirEntryErrRange);
3171 return(TIFFReadDirEntryErrOk);
3175 * Largest 32-bit unsigned integer value.
3177 #define TIFF_UINT32_MAX 0xFFFFFFFFU
3179 static enum TIFFReadDirEntryErr
3180 TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
3182 if (value > TIFF_UINT32_MAX)
3183 return(TIFFReadDirEntryErrRange);
3185 return(TIFFReadDirEntryErrOk);
3188 static enum TIFFReadDirEntryErr
3189 TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
3191 if ((value < 0) || (value > (int64) TIFF_UINT32_MAX))
3192 return(TIFFReadDirEntryErrRange);
3194 return(TIFFReadDirEntryErrOk);
3197 #undef TIFF_UINT32_MAX
3199 static enum TIFFReadDirEntryErr
3200 TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
3202 if (value > 0x7FFFFFFFUL)
3203 return(TIFFReadDirEntryErrRange);
3205 return(TIFFReadDirEntryErrOk);
3208 /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3209 static enum TIFFReadDirEntryErr
3210 TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
3212 if (value > 0x7FFFFFFF)
3213 return(TIFFReadDirEntryErrRange);
3215 return(TIFFReadDirEntryErrOk);
3218 /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3219 static enum TIFFReadDirEntryErr
3220 TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
3222 if ((value < 0-((int64) 0x7FFFFFFF+1)) || (value > 0x7FFFFFFF))
3223 return(TIFFReadDirEntryErrRange);
3225 return(TIFFReadDirEntryErrOk);
3228 static enum TIFFReadDirEntryErr
3229 TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
3232 return(TIFFReadDirEntryErrRange);
3234 return(TIFFReadDirEntryErrOk);
3237 static enum TIFFReadDirEntryErr
3238 TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
3241 return(TIFFReadDirEntryErrRange);
3243 return(TIFFReadDirEntryErrOk);
3246 static enum TIFFReadDirEntryErr
3247 TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
3250 return(TIFFReadDirEntryErrRange);
3252 return(TIFFReadDirEntryErrOk);
3255 static enum TIFFReadDirEntryErr
3256 TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
3259 return(TIFFReadDirEntryErrRange);
3261 return(TIFFReadDirEntryErrOk);
3265 * Largest 64-bit signed integer value.
3267 #define TIFF_INT64_MAX ((int64)(((uint64) ~0) >> 1))
3269 static enum TIFFReadDirEntryErr
3270 TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
3272 if (value > TIFF_INT64_MAX)
3273 return(TIFFReadDirEntryErrRange);
3275 return(TIFFReadDirEntryErrOk);
3278 #undef TIFF_INT64_MAX
3280 static enum TIFFReadDirEntryErr
3281 TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
3284 if (!isMapped(tif)) {
3285 if (!SeekOK(tif,offset))
3286 return(TIFFReadDirEntryErrIo);
3287 if (!ReadOK(tif,dest,size))
3288 return(TIFFReadDirEntryErrIo);
3293 if (((uint64)ma!=offset)
3295 || (mb - ma != (size_t) size)
3296 || (mb < (size_t)size)
3297 || (mb > (size_t)tif->tif_size)
3299 return(TIFFReadDirEntryErrIo);
3300 _TIFFmemcpy(dest,tif->tif_base+ma,size);
3302 return(TIFFReadDirEntryErrOk);
3305 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
3309 case TIFFReadDirEntryErrCount:
3310 TIFFErrorExt(tif->tif_clientdata, module,
3311 "Incorrect count for \"%s\"",
3314 case TIFFReadDirEntryErrType:
3315 TIFFErrorExt(tif->tif_clientdata, module,
3316 "Incompatible type for \"%s\"",
3319 case TIFFReadDirEntryErrIo:
3320 TIFFErrorExt(tif->tif_clientdata, module,
3321 "IO error during reading of \"%s\"",
3324 case TIFFReadDirEntryErrRange:
3325 TIFFErrorExt(tif->tif_clientdata, module,
3326 "Incorrect value for \"%s\"",
3329 case TIFFReadDirEntryErrPsdif:
3330 TIFFErrorExt(tif->tif_clientdata, module,
3331 "Cannot handle different values per sample for \"%s\"",
3334 case TIFFReadDirEntryErrSizesan:
3335 TIFFErrorExt(tif->tif_clientdata, module,
3336 "Sanity check on size of \"%s\" value failed",
3339 case TIFFReadDirEntryErrAlloc:
3340 TIFFErrorExt(tif->tif_clientdata, module,
3341 "Out of memory reading of \"%s\"",
3345 assert(0); /* we should never get here */
3350 case TIFFReadDirEntryErrCount:
3351 TIFFWarningExt(tif->tif_clientdata, module,
3352 "Incorrect count for \"%s\"; tag ignored",
3355 case TIFFReadDirEntryErrType:
3356 TIFFWarningExt(tif->tif_clientdata, module,
3357 "Incompatible type for \"%s\"; tag ignored",
3360 case TIFFReadDirEntryErrIo:
3361 TIFFWarningExt(tif->tif_clientdata, module,
3362 "IO error during reading of \"%s\"; tag ignored",
3365 case TIFFReadDirEntryErrRange:
3366 TIFFWarningExt(tif->tif_clientdata, module,
3367 "Incorrect value for \"%s\"; tag ignored",
3370 case TIFFReadDirEntryErrPsdif:
3371 TIFFWarningExt(tif->tif_clientdata, module,
3372 "Cannot handle different values per sample for \"%s\"; tag ignored",
3375 case TIFFReadDirEntryErrSizesan:
3376 TIFFWarningExt(tif->tif_clientdata, module,
3377 "Sanity check on size of \"%s\" value failed; tag ignored",
3380 case TIFFReadDirEntryErrAlloc:
3381 TIFFWarningExt(tif->tif_clientdata, module,
3382 "Out of memory reading of \"%s\"; tag ignored",
3386 assert(0); /* we should never get here */
3393 * Read the next TIFF directory from a file and convert it to the internal
3394 * format. We read directories sequentially.
3397 TIFFReadDirectory(TIFF* tif)
3399 static const char module[] = "TIFFReadDirectory";
3404 const TIFFField* fip;
3405 uint32 fii=FAILED_FII;
3407 int bitspersample_read = FALSE;
3409 tif->tif_diroff=tif->tif_nextdiroff;
3410 if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
3411 return 0; /* last offset or bad offset (IFD looping) */
3412 (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
3414 nextdiroff = tif->tif_nextdiroff;
3415 dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
3418 TIFFErrorExt(tif->tif_clientdata,module,
3419 "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
3422 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
3425 * Mark duplicates of any tag to be ignored (bugzilla 1994)
3426 * to avoid certain pathological problems.
3431 for (ma=dir, mb=0; mb<dircount; ma++, mb++)
3435 for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
3437 if (ma->tdir_tag==na->tdir_tag)
3438 na->tdir_tag=IGNORE;
3443 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
3444 tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
3445 /* free any old stuff and reinit */
3446 TIFFFreeDirectory(tif);
3447 TIFFDefaultDirectory(tif);
3449 * Electronic Arts writes gray-scale TIFF files
3450 * without a PlanarConfiguration directory entry.
3451 * Thus we setup a default value here, even though
3452 * the TIFF spec says there is no default value.
3454 TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3456 * Setup default value and then make a pass over
3457 * the fields to check type and tag information,
3458 * and to extract info required to size data
3459 * structures. A second pass is made afterwards
3460 * to read in everthing not taken in the first pass.
3461 * But we must process the Compression tag first
3462 * in order to merge in codec-private tag definitions (otherwise
3463 * we may get complaints about unknown tags). However, the
3464 * Compression tag may be dependent on the SamplesPerPixel
3465 * tag value because older TIFF specs permited Compression
3466 * to be written as a SamplesPerPixel-count tag entry.
3467 * Thus if we don't first figure out the correct SamplesPerPixel
3468 * tag value then we may end up ignoring the Compression tag
3469 * value because it has an incorrect count value (if the
3470 * true value of SamplesPerPixel is not 1).
3472 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
3475 if (!TIFFFetchNormalTag(tif,dp,0))
3477 dp->tdir_tag=IGNORE;
3479 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
3483 * The 5.0 spec says the Compression tag has one value, while
3484 * earlier specs say it has one value per sample. Because of
3485 * this, we accept the tag if one value is supplied with either
3489 enum TIFFReadDirEntryErr err;
3490 err=TIFFReadDirEntryShort(tif,dp,&value);
3491 if (err==TIFFReadDirEntryErrCount)
3492 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3493 if (err!=TIFFReadDirEntryErrOk)
3495 TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
3498 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
3500 dp->tdir_tag=IGNORE;
3504 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
3508 * First real pass over the directory.
3510 for (di=0, dp=dir; di<dircount; di++, dp++)
3512 if (dp->tdir_tag!=IGNORE)
3514 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3515 if (fii == FAILED_FII)
3517 TIFFWarningExt(tif->tif_clientdata, module,
3518 "Unknown field with tag %d (0x%x) encountered",
3519 dp->tdir_tag,dp->tdir_tag);
3520 /* the following knowingly leaks the
3521 anonymous field structure */
3522 if (!_TIFFMergeFields(tif,
3523 _TIFFCreateAnonField(tif,
3525 (TIFFDataType) dp->tdir_type),
3527 TIFFWarningExt(tif->tif_clientdata,
3529 "Registering anonymous field with tag %d (0x%x) failed",
3532 dp->tdir_tag=IGNORE;
3534 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3535 assert(fii != FAILED_FII);
3539 if (dp->tdir_tag!=IGNORE)
3541 fip=tif->tif_fields[fii];
3542 if (fip->field_bit==FIELD_IGNORE)
3543 dp->tdir_tag=IGNORE;
3546 switch (dp->tdir_tag)
3548 case TIFFTAG_STRIPOFFSETS:
3549 case TIFFTAG_STRIPBYTECOUNTS:
3550 case TIFFTAG_TILEOFFSETS:
3551 case TIFFTAG_TILEBYTECOUNTS:
3552 TIFFSetFieldBit(tif,fip->field_bit);
3554 case TIFFTAG_IMAGEWIDTH:
3555 case TIFFTAG_IMAGELENGTH:
3556 case TIFFTAG_IMAGEDEPTH:
3557 case TIFFTAG_TILELENGTH:
3558 case TIFFTAG_TILEWIDTH:
3559 case TIFFTAG_TILEDEPTH:
3560 case TIFFTAG_PLANARCONFIG:
3561 case TIFFTAG_ROWSPERSTRIP:
3562 case TIFFTAG_EXTRASAMPLES:
3563 if (!TIFFFetchNormalTag(tif,dp,0))
3565 dp->tdir_tag=IGNORE;
3573 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3574 * c) strip offsets/bytecounts tag are both present and
3575 * d) both contain exactly one value, then we consistently find
3576 * that the buggy implementation of the buggy compression scheme
3577 * matches contig planarconfig best. So we 'fix-up' the tag here
3579 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
3580 (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
3582 if (!_TIFFFillStriles(tif))
3584 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
3585 if ((dp!=0)&&(dp->tdir_count==1))
3587 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
3588 TIFFTAG_STRIPBYTECOUNTS);
3589 if ((dp!=0)&&(dp->tdir_count==1))
3591 tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
3592 TIFFWarningExt(tif->tif_clientdata,module,
3593 "Planarconfig tag value assumed incorrect, "
3594 "assuming data is contig instead of chunky");
3599 * Allocate directory structure and setup defaults.
3601 if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
3603 MissingRequired(tif,"ImageLength");
3607 * Setup appropriate structures (by strip or by tile)
3609 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3610 tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
3611 tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3612 tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
3613 tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
3614 tif->tif_flags &= ~TIFF_ISTILED;
3616 tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
3617 tif->tif_flags |= TIFF_ISTILED;
3619 if (!tif->tif_dir.td_nstrips) {
3620 TIFFErrorExt(tif->tif_clientdata, module,
3621 "Cannot handle zero number of %s",
3622 isTiled(tif) ? "tiles" : "strips");
3625 tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
3626 if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
3627 tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
3628 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
3629 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
3630 (isTiled(tif)==0) &&
3631 (tif->tif_dir.td_nstrips==1)) {
3634 * If a) compression is OJPEG, b) it's not a tiled TIFF,
3635 * and c) the number of strips is 1,
3636 * then we tolerate the absence of stripoffsets tag,
3637 * because, presumably, all required data is in the
3638 * JpegInterchangeFormat stream.
3640 TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
3642 MissingRequired(tif,
3643 isTiled(tif) ? "TileOffsets" : "StripOffsets");
3648 * Second pass: extract other information.
3650 for (di=0, dp=dir; di<dircount; di++, dp++)
3652 switch (dp->tdir_tag)
3656 case TIFFTAG_MINSAMPLEVALUE:
3657 case TIFFTAG_MAXSAMPLEVALUE:
3658 case TIFFTAG_BITSPERSAMPLE:
3659 case TIFFTAG_DATATYPE:
3660 case TIFFTAG_SAMPLEFORMAT:
3662 * The MinSampleValue, MaxSampleValue, BitsPerSample
3663 * DataType and SampleFormat tags are supposed to be
3664 * written as one value/sample, but some vendors
3665 * incorrectly write one value only -- so we accept
3666 * that as well (yech). Other vendors write correct
3667 * value for NumberOfSamples, but incorrect one for
3668 * BitsPerSample and friends, and we will read this
3673 enum TIFFReadDirEntryErr err;
3674 err=TIFFReadDirEntryShort(tif,dp,&value);
3675 if (err==TIFFReadDirEntryErrCount)
3676 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3677 if (err!=TIFFReadDirEntryErrOk)
3679 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3680 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3683 if (!TIFFSetField(tif,dp->tdir_tag,value))
3685 if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )
3686 bitspersample_read = TRUE;
3689 case TIFFTAG_SMINSAMPLEVALUE:
3690 case TIFFTAG_SMAXSAMPLEVALUE:
3694 enum TIFFReadDirEntryErr err;
3697 if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
3698 err = TIFFReadDirEntryErrCount;
3700 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
3701 if (err!=TIFFReadDirEntryErrOk)
3703 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3704 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3707 saved_flags = tif->tif_flags;
3708 tif->tif_flags |= TIFF_PERSAMPLE;
3709 m = TIFFSetField(tif,dp->tdir_tag,data);
3710 tif->tif_flags = saved_flags;
3716 case TIFFTAG_STRIPOFFSETS:
3717 case TIFFTAG_TILEOFFSETS:
3718 #if defined(DEFER_STRILE_LOAD)
3719 _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
3720 dp, sizeof(TIFFDirEntry) );
3722 if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
3726 case TIFFTAG_STRIPBYTECOUNTS:
3727 case TIFFTAG_TILEBYTECOUNTS:
3728 #if defined(DEFER_STRILE_LOAD)
3729 _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
3730 dp, sizeof(TIFFDirEntry) );
3732 if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
3736 case TIFFTAG_COLORMAP:
3737 case TIFFTAG_TRANSFERFUNCTION:
3739 enum TIFFReadDirEntryErr err;
3740 uint32 countpersample;
3741 uint32 countrequired;
3742 uint32 incrementpersample;
3744 /* It would be dangerous to instanciate those tag values */
3745 /* since if td_bitspersample has not yet been read (due to */
3746 /* unordered tags), it could be read afterwards with a */
3747 /* values greater than the default one (1), which may cause */
3748 /* crashes in user code */
3749 if( !bitspersample_read )
3751 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3752 TIFFWarningExt(tif->tif_clientdata,module,
3753 "Ignoring %s since BitsPerSample tag not found",
3754 fip ? fip->field_name : "unknown tagname");
3757 countpersample=(1L<<tif->tif_dir.td_bitspersample);
3758 if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
3760 countrequired=countpersample;
3761 incrementpersample=0;
3765 countrequired=3*countpersample;
3766 incrementpersample=countpersample;
3768 if (dp->tdir_count!=(uint64)countrequired)
3769 err=TIFFReadDirEntryErrCount;
3771 err=TIFFReadDirEntryShortArray(tif,dp,&value);
3772 if (err!=TIFFReadDirEntryErrOk)
3774 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3775 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
3779 TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
3784 /* BEGIN REV 4.0 COMPATIBILITY */
3785 case TIFFTAG_OSUBFILETYPE:
3789 if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
3793 case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
3794 case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
3795 default: value=0; break;
3798 TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
3802 /* END REV 4.0 COMPATIBILITY */
3804 (void) TIFFFetchNormalTag(tif, dp, TRUE);
3810 * - If a) compression is OJPEG, and b) photometric tag is missing,
3811 * then we consistently find that photometric should be YCbCr
3812 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
3813 * then we consistently find that the buggy implementation of the
3814 * buggy compression scheme matches photometric YCbCr instead.
3815 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
3816 * then we consistently find bitspersample should be 8.
3817 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3818 * and c) photometric is RGB or YCbCr, then we consistently find
3819 * samplesperpixel should be 3
3820 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3821 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
3822 * find samplesperpixel should be 3
3824 if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
3826 if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
3828 TIFFWarningExt(tif->tif_clientdata, module,
3829 "Photometric tag is missing, assuming data is YCbCr");
3830 if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
3833 else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
3835 tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
3836 TIFFWarningExt(tif->tif_clientdata, module,
3837 "Photometric tag value assumed incorrect, "
3838 "assuming data is YCbCr instead of RGB");
3840 if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
3842 TIFFWarningExt(tif->tif_clientdata,module,
3843 "BitsPerSample tag is missing, assuming 8 bits per sample");
3844 if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
3847 if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
3849 if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
3851 TIFFWarningExt(tif->tif_clientdata,module,
3852 "SamplesPerPixel tag is missing, "
3853 "assuming correct SamplesPerPixel value is 3");
3854 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
3857 if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
3859 TIFFWarningExt(tif->tif_clientdata,module,
3860 "SamplesPerPixel tag is missing, "
3861 "applying correct SamplesPerPixel value of 3");
3862 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
3865 else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
3866 || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
3869 * SamplesPerPixel tag is missing, but is not required
3870 * by spec. Assume correct SamplesPerPixel value of 1.
3872 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
3878 * Verify Palette image has a Colormap.
3880 if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
3881 !TIFFFieldSet(tif, FIELD_COLORMAP)) {
3882 if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
3883 tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
3884 else if (tif->tif_dir.td_bitspersample>=8)
3885 tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
3887 MissingRequired(tif, "Colormap");
3893 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
3896 if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
3899 * Attempt to deal with a missing StripByteCounts tag.
3901 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
3903 * Some manufacturers violate the spec by not giving
3904 * the size of the strips. In this case, assume there
3905 * is one uncompressed strip of data.
3907 if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
3908 tif->tif_dir.td_nstrips > 1) ||
3909 (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
3910 tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
3911 MissingRequired(tif, "StripByteCounts");
3914 TIFFWarningExt(tif->tif_clientdata, module,
3915 "TIFF directory is missing required "
3916 "\"StripByteCounts\" field, calculating from imagelength");
3917 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
3920 * Assume we have wrong StripByteCount value (in case
3921 * of single strip) in following cases:
3922 * - it is equal to zero along with StripOffset;
3923 * - it is larger than file itself (in case of uncompressed
3925 * - it is smaller than the size of the bytes per row
3926 * multiplied on the number of rows. The last case should
3927 * not be checked in the case of writing new image,
3928 * because we may do not know the exact strip size
3929 * until the whole image will be written and directory
3932 #define BYTECOUNTLOOKSBAD \
3933 ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
3934 (tif->tif_dir.td_compression == COMPRESSION_NONE && \
3935 tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
3936 (tif->tif_mode == O_RDONLY && \
3937 tif->tif_dir.td_compression == COMPRESSION_NONE && \
3938 tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
3940 } else if (tif->tif_dir.td_nstrips == 1
3941 && _TIFFFillStriles(tif)
3942 && tif->tif_dir.td_stripoffset[0] != 0
3943 && BYTECOUNTLOOKSBAD) {
3945 * XXX: Plexus (and others) sometimes give a value of
3946 * zero for a tag when they don't know what the
3947 * correct value is! Try and handle the simple case
3948 * of estimating the size of a one strip image.
3950 TIFFWarningExt(tif->tif_clientdata, module,
3951 "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
3952 if(EstimateStripByteCounts(tif, dir, dircount) < 0)
3955 #if !defined(DEFER_STRILE_LOAD)
3956 } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
3957 && tif->tif_dir.td_nstrips > 2
3958 && tif->tif_dir.td_compression == COMPRESSION_NONE
3959 && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
3960 && tif->tif_dir.td_stripbytecount[0] != 0
3961 && tif->tif_dir.td_stripbytecount[1] != 0 ) {
3963 * XXX: Some vendors fill StripByteCount array with
3964 * absolutely wrong values (it can be equal to
3965 * StripOffset array, for example). Catch this case
3968 * We avoid this check if deferring strile loading
3969 * as it would always force us to load the strip/tile
3972 TIFFWarningExt(tif->tif_clientdata, module,
3973 "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
3974 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
3976 #endif /* !defined(DEFER_STRILE_LOAD) */
3984 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
3986 if (tif->tif_dir.td_bitspersample>=16)
3987 tif->tif_dir.td_maxsamplevalue=0xFFFF;
3989 tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
3992 * XXX: We can optimize checking for the strip bounds using the sorted
3993 * bytecounts array. See also comments for TIFFAppendToStrip()
3994 * function in tif_write.c.
3996 #if !defined(DEFER_STRILE_LOAD)
3997 if (tif->tif_dir.td_nstrips > 1) {
4000 tif->tif_dir.td_stripbytecountsorted = 1;
4001 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
4002 if (tif->tif_dir.td_stripoffset[strip - 1] >
4003 tif->tif_dir.td_stripoffset[strip]) {
4004 tif->tif_dir.td_stripbytecountsorted = 0;
4009 #endif /* !defined(DEFER_STRILE_LOAD) */
4012 * An opportunity for compression mode dependent tag fixup
4014 (*tif->tif_fixuptags)(tif);
4017 * Some manufacturers make life difficult by writing
4018 * large amounts of uncompressed data as a single strip.
4019 * This is contrary to the recommendations of the spec.
4020 * The following makes an attempt at breaking such images
4021 * into strips closer to the recommended 8k bytes. A
4022 * side effect, however, is that the RowsPerStrip tag
4023 * value may be changed.
4025 if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
4026 (tif->tif_dir.td_nstrips==1)&&
4027 (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
4028 ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
4030 if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
4032 ChopUpSingleUncompressedStrip(tif);
4036 * Clear the dirty directory flag.
4038 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
4039 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
4042 * Reinitialize i/o since we are starting on a new directory.
4044 tif->tif_row = (uint32) -1;
4045 tif->tif_curstrip = (uint32) -1;
4046 tif->tif_col = (uint32) -1;
4047 tif->tif_curtile = (uint32) -1;
4048 tif->tif_tilesize = (tmsize_t) -1;
4050 tif->tif_scanlinesize = TIFFScanlineSize(tif);
4051 if (!tif->tif_scanlinesize) {
4052 TIFFErrorExt(tif->tif_clientdata, module,
4053 "Cannot handle zero scanline size");
4058 tif->tif_tilesize = TIFFTileSize(tif);
4059 if (!tif->tif_tilesize) {
4060 TIFFErrorExt(tif->tif_clientdata, module,
4061 "Cannot handle zero tile size");
4065 if (!TIFFStripSize(tif)) {
4066 TIFFErrorExt(tif->tif_clientdata, module,
4067 "Cannot handle zero strip size");
4079 TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4081 static const char module[] = "TIFFReadDirectoryCheckOrder";
4086 for (n=0, o=dir; n<dircount; n++, o++)
4090 TIFFWarningExt(tif->tif_clientdata,module,
4091 "Invalid TIFF directory; tags are not sorted in ascending order");
4098 static TIFFDirEntry*
4099 TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
4104 for (m=dir, n=0; n<dircount; m++, n++)
4106 if (m->tdir_tag==tagid)
4113 TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
4117 mc=(int32)tif->tif_nfields;
4126 if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
4128 if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
4137 if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
4145 * Read custom directory from the arbitarry offset.
4146 * The code is very similar to TIFFReadDirectory().
4149 TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
4150 const TIFFFieldArray* infoarray)
4152 static const char module[] = "TIFFReadCustomDirectory";
4157 const TIFFField* fip;
4159 _TIFFSetupFields(tif, infoarray);
4160 dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
4163 TIFFErrorExt(tif->tif_clientdata,module,
4164 "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
4167 TIFFFreeDirectory(tif);
4168 _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
4169 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
4170 for (di=0, dp=dir; di<dircount; di++, dp++)
4172 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4173 if (fii == FAILED_FII)
4175 TIFFWarningExt(tif->tif_clientdata, module,
4176 "Unknown field with tag %d (0x%x) encountered",
4177 dp->tdir_tag, dp->tdir_tag);
4178 if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
4180 (TIFFDataType) dp->tdir_type),
4182 TIFFWarningExt(tif->tif_clientdata, module,
4183 "Registering anonymous field with tag %d (0x%x) failed",
4184 dp->tdir_tag, dp->tdir_tag);
4185 dp->tdir_tag=IGNORE;
4187 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4188 assert( fii != FAILED_FII );
4191 if (dp->tdir_tag!=IGNORE)
4193 fip=tif->tif_fields[fii];
4194 if (fip->field_bit==FIELD_IGNORE)
4195 dp->tdir_tag=IGNORE;
4198 /* check data type */
4199 while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
4202 if ((fii==tif->tif_nfields)||
4203 (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
4208 fip=tif->tif_fields[fii];
4212 TIFFWarningExt(tif->tif_clientdata, module,
4213 "Wrong data type %d for \"%s\"; tag ignored",
4214 dp->tdir_type,fip->field_name);
4215 dp->tdir_tag=IGNORE;
4219 /* check count if known in advance */
4220 if ((fip->field_readcount!=TIFF_VARIABLE)&&
4221 (fip->field_readcount!=TIFF_VARIABLE2))
4224 if (fip->field_readcount==TIFF_SPP)
4225 expected=(uint32)tif->tif_dir.td_samplesperpixel;
4227 expected=(uint32)fip->field_readcount;
4228 if (!CheckDirCount(tif,dp,expected))
4229 dp->tdir_tag=IGNORE;
4233 switch (dp->tdir_tag)
4237 case EXIFTAG_SUBJECTDISTANCE:
4238 (void) TIFFFetchSubjectDistance(tif,dp);
4241 (void) TIFFFetchNormalTag(tif, dp, TRUE);
4252 * EXIF is important special case of custom IFD, so we have a special
4253 * function to read it.
4256 TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
4258 const TIFFFieldArray* exifFieldArray;
4259 exifFieldArray = _TIFFGetExifFields();
4260 return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
4264 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4266 static const char module[] = "EstimateStripByteCounts";
4269 TIFFDirectory *td = &tif->tif_dir;
4272 if( !_TIFFFillStriles( tif ) )
4275 if (td->td_stripbytecount)
4276 _TIFFfree(td->td_stripbytecount);
4277 td->td_stripbytecount = (uint64*)
4278 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
4279 "for \"StripByteCounts\" array");
4280 if( td->td_stripbytecount == NULL )
4283 if (td->td_compression != COMPRESSION_NONE) {
4287 filesize = TIFFGetFileSize(tif);
4288 if (!(tif->tif_flags&TIFF_BIGTIFF))
4289 space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
4291 space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
4292 /* calculate amount of space used by indirect values */
4293 for (dp = dir, n = dircount; n > 0; n--, dp++)
4295 uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4297 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4298 if (typewidth == 0) {
4299 TIFFErrorExt(tif->tif_clientdata, module,
4300 "Cannot determine size of unknown tag type %d",
4304 datasize=(uint64)typewidth*dp->tdir_count;
4305 if (!(tif->tif_flags&TIFF_BIGTIFF))
4317 space = filesize - space;
4318 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
4319 space /= td->td_samplesperpixel;
4320 for (strip = 0; strip < td->td_nstrips; strip++)
4321 td->td_stripbytecount[strip] = space;
4323 * This gross hack handles the case were the offset to
4324 * the last strip is past the place where we think the strip
4325 * should begin. Since a strip of data must be contiguous,
4326 * it's safe to assume that we've overestimated the amount
4327 * of data in the strip and trim this number back accordingly.
4330 if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
4331 td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
4332 } else if (isTiled(tif)) {
4333 uint64 bytespertile = TIFFTileSize64(tif);
4335 for (strip = 0; strip < td->td_nstrips; strip++)
4336 td->td_stripbytecount[strip] = bytespertile;
4338 uint64 rowbytes = TIFFScanlineSize64(tif);
4339 uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
4340 for (strip = 0; strip < td->td_nstrips; strip++)
4341 td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
4343 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
4344 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
4345 td->td_rowsperstrip = td->td_imagelength;
4350 MissingRequired(TIFF* tif, const char* tagname)
4352 static const char module[] = "MissingRequired";
4354 TIFFErrorExt(tif->tif_clientdata, module,
4355 "TIFF directory is missing required \"%s\" field",
4360 * Check the directory offset against the list of already seen directory
4361 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4362 * file with looped directory pointers. We will maintain a list of already
4363 * seen directories and check every IFD offset against that list.
4366 TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
4370 if (diroff == 0) /* no more directories */
4372 if (tif->tif_dirnumber == 65535) {
4373 TIFFErrorExt(tif->tif_clientdata, "TIFFCheckDirOffset",
4374 "Cannot handle more than 65535 TIFF directories");
4378 for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
4379 if (tif->tif_dirlist[n] == diroff)
4383 tif->tif_dirnumber++;
4385 if (tif->tif_dirnumber > tif->tif_dirlistsize) {
4386 uint64* new_dirlist;
4389 * XXX: Reduce memory allocation granularity of the dirlist
4392 new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
4393 tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
4396 if( tif->tif_dirnumber >= 32768 )
4397 tif->tif_dirlistsize = 65535;
4399 tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
4400 tif->tif_dirlist = new_dirlist;
4403 tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
4409 * Check the count field of a directory entry against a known value. The
4410 * caller is expected to skip/ignore the tag if there is a mismatch.
4413 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
4415 if ((uint64)count > dir->tdir_count) {
4416 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4417 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4418 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
4419 fip ? fip->field_name : "unknown tagname",
4420 dir->tdir_count, count);
4422 } else if ((uint64)count < dir->tdir_count) {
4423 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4424 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4425 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
4426 fip ? fip->field_name : "unknown tagname",
4427 dir->tdir_count, count);
4428 dir->tdir_count = count;
4435 * Read IFD structure from the specified offset. If the pointer to
4436 * nextdiroff variable has been specified, read it too. Function returns a
4437 * number of fields in the directory or 0 if failed.
4440 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
4443 static const char module[] = "TIFFFetchDirectory";
4455 tif->tif_diroff = diroff;
4458 if (!isMapped(tif)) {
4459 if (!SeekOK(tif, tif->tif_diroff)) {
4460 TIFFErrorExt(tif->tif_clientdata, module,
4461 "%s: Seek error accessing TIFF directory",
4465 if (!(tif->tif_flags&TIFF_BIGTIFF))
4467 if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
4468 TIFFErrorExt(tif->tif_clientdata, module,
4469 "%s: Can not read TIFF directory count",
4473 if (tif->tif_flags & TIFF_SWAB)
4474 TIFFSwabShort(&dircount16);
4475 if (dircount16>4096)
4477 TIFFErrorExt(tif->tif_clientdata, module,
4478 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4484 if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
4485 TIFFErrorExt(tif->tif_clientdata, module,
4486 "%s: Can not read TIFF directory count",
4490 if (tif->tif_flags & TIFF_SWAB)
4491 TIFFSwabLong8(&dircount64);
4492 if (dircount64>4096)
4494 TIFFErrorExt(tif->tif_clientdata, module,
4495 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4498 dircount16 = (uint16)dircount64;
4501 origdir = _TIFFCheckMalloc(tif, dircount16,
4502 dirsize, "to read TIFF directory");
4503 if (origdir == NULL)
4505 if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
4506 TIFFErrorExt(tif->tif_clientdata, module,
4507 "%.100s: Can not read TIFF directory",
4513 * Read offset to next directory for sequential scans if
4518 if (!(tif->tif_flags&TIFF_BIGTIFF))
4520 uint32 nextdiroff32;
4521 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
4523 if (tif->tif_flags&TIFF_SWAB)
4524 TIFFSwabLong(&nextdiroff32);
4525 *nextdiroff=nextdiroff32;
4527 if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
4529 if (tif->tif_flags&TIFF_SWAB)
4530 TIFFSwabLong8(nextdiroff);
4535 tmsize_t off = (tmsize_t) tif->tif_diroff;
4536 if ((uint64)off!=tif->tif_diroff)
4538 TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
4543 * Check for integer overflow when validating the dir_off,
4544 * otherwise a very high offset may cause an OOB read and
4545 * crash the client. Make two comparisons instead of
4547 * off + sizeof(uint16) > tif->tif_size
4549 * to avoid overflow.
4551 if (!(tif->tif_flags&TIFF_BIGTIFF))
4553 m=off+sizeof(uint16);
4554 if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
4555 TIFFErrorExt(tif->tif_clientdata, module,
4556 "Can not read TIFF directory count");
4559 _TIFFmemcpy(&dircount16, tif->tif_base + off,
4562 off += sizeof (uint16);
4563 if (tif->tif_flags & TIFF_SWAB)
4564 TIFFSwabShort(&dircount16);
4565 if (dircount16>4096)
4567 TIFFErrorExt(tif->tif_clientdata, module,
4568 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4577 m=off+sizeof(uint64);
4578 if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
4579 TIFFErrorExt(tif->tif_clientdata, module,
4580 "Can not read TIFF directory count");
4583 _TIFFmemcpy(&dircount64, tif->tif_base + off,
4586 off += sizeof (uint64);
4587 if (tif->tif_flags & TIFF_SWAB)
4588 TIFFSwabLong8(&dircount64);
4589 if (dircount64>4096)
4591 TIFFErrorExt(tif->tif_clientdata, module,
4592 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4595 dircount16 = (uint16)dircount64;
4598 if (dircount16 == 0 )
4600 TIFFErrorExt(tif->tif_clientdata, module,
4601 "Sanity check on directory count failed, zero tag directories not supported");
4604 origdir = _TIFFCheckMalloc(tif, dircount16,
4606 "to read TIFF directory");
4607 if (origdir == NULL)
4609 m=off+dircount16*dirsize;
4610 if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
4611 TIFFErrorExt(tif->tif_clientdata, module,
4612 "Can not read TIFF directory");
4616 _TIFFmemcpy(origdir, tif->tif_base + off,
4617 dircount16 * dirsize);
4620 off += dircount16 * dirsize;
4621 if (!(tif->tif_flags&TIFF_BIGTIFF))
4623 uint32 nextdiroff32;
4624 m=off+sizeof(uint32);
4625 if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
4628 _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
4630 if (tif->tif_flags&TIFF_SWAB)
4631 TIFFSwabLong(&nextdiroff32);
4632 *nextdiroff = nextdiroff32;
4636 m=off+sizeof(uint64);
4637 if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
4640 _TIFFmemcpy(nextdiroff, tif->tif_base + off,
4642 if (tif->tif_flags&TIFF_SWAB)
4643 TIFFSwabLong8(nextdiroff);
4647 dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
4648 sizeof(TIFFDirEntry),
4649 "to read TIFF directory");
4657 for (n=0; n<dircount16; n++)
4659 if (tif->tif_flags&TIFF_SWAB)
4660 TIFFSwabShort((uint16*)ma);
4661 mb->tdir_tag=*(uint16*)ma;
4663 if (tif->tif_flags&TIFF_SWAB)
4664 TIFFSwabShort((uint16*)ma);
4665 mb->tdir_type=*(uint16*)ma;
4667 if (!(tif->tif_flags&TIFF_BIGTIFF))
4669 if (tif->tif_flags&TIFF_SWAB)
4670 TIFFSwabLong((uint32*)ma);
4671 mb->tdir_count=(uint64)(*(uint32*)ma);
4673 *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
4678 if (tif->tif_flags&TIFF_SWAB)
4679 TIFFSwabLong8((uint64*)ma);
4680 mb->tdir_count=TIFFReadUInt64(ma);
4682 mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
4693 * Fetch a tag that is not handled by special case code.
4696 TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
4698 static const char module[] = "TIFFFetchNormalTag";
4699 enum TIFFReadDirEntryErr err;
4701 const TIFFField* fip = NULL;
4702 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4703 if( fii == FAILED_FII )
4705 TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
4706 "No definition found for tag %d",
4710 fip=tif->tif_fields[fii];
4711 assert(fip != NULL); /* should not happen */
4712 assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
4713 assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
4714 err=TIFFReadDirEntryErrOk;
4715 switch (fip->set_field_type)
4717 case TIFF_SETGET_UNDEFINED:
4719 case TIFF_SETGET_ASCII:
4722 assert(fip->field_passcount==0);
4723 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4724 if (err==TIFFReadDirEntryErrOk)
4731 while (mb<(uint32)dp->tdir_count)
4738 if (mb+1<(uint32)dp->tdir_count)
4739 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);
4740 else if (mb+1>(uint32)dp->tdir_count)
4743 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
4744 if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
4747 o=_TIFFmalloc((uint32)dp->tdir_count+1);
4754 _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
4755 o[(uint32)dp->tdir_count]=0;
4760 n=TIFFSetField(tif,dp->tdir_tag,data);
4768 case TIFF_SETGET_UINT8:
4771 assert(fip->field_readcount==1);
4772 assert(fip->field_passcount==0);
4773 err=TIFFReadDirEntryByte(tif,dp,&data);
4774 if (err==TIFFReadDirEntryErrOk)
4776 if (!TIFFSetField(tif,dp->tdir_tag,data))
4781 case TIFF_SETGET_UINT16:
4784 assert(fip->field_readcount==1);
4785 assert(fip->field_passcount==0);
4786 err=TIFFReadDirEntryShort(tif,dp,&data);
4787 if (err==TIFFReadDirEntryErrOk)
4789 if (!TIFFSetField(tif,dp->tdir_tag,data))
4794 case TIFF_SETGET_UINT32:
4797 assert(fip->field_readcount==1);
4798 assert(fip->field_passcount==0);
4799 err=TIFFReadDirEntryLong(tif,dp,&data);
4800 if (err==TIFFReadDirEntryErrOk)
4802 if (!TIFFSetField(tif,dp->tdir_tag,data))
4807 case TIFF_SETGET_UINT64:
4810 assert(fip->field_readcount==1);
4811 assert(fip->field_passcount==0);
4812 err=TIFFReadDirEntryLong8(tif,dp,&data);
4813 if (err==TIFFReadDirEntryErrOk)
4815 if (!TIFFSetField(tif,dp->tdir_tag,data))
4820 case TIFF_SETGET_FLOAT:
4823 assert(fip->field_readcount==1);
4824 assert(fip->field_passcount==0);
4825 err=TIFFReadDirEntryFloat(tif,dp,&data);
4826 if (err==TIFFReadDirEntryErrOk)
4828 if (!TIFFSetField(tif,dp->tdir_tag,data))
4833 case TIFF_SETGET_DOUBLE:
4836 assert(fip->field_readcount==1);
4837 assert(fip->field_passcount==0);
4838 err=TIFFReadDirEntryDouble(tif,dp,&data);
4839 if (err==TIFFReadDirEntryErrOk)
4841 if (!TIFFSetField(tif,dp->tdir_tag,data))
4846 case TIFF_SETGET_IFD8:
4849 assert(fip->field_readcount==1);
4850 assert(fip->field_passcount==0);
4851 err=TIFFReadDirEntryIfd8(tif,dp,&data);
4852 if (err==TIFFReadDirEntryErrOk)
4854 if (!TIFFSetField(tif,dp->tdir_tag,data))
4859 case TIFF_SETGET_UINT16_PAIR:
4862 assert(fip->field_readcount==2);
4863 assert(fip->field_passcount==0);
4864 if (dp->tdir_count!=2) {
4865 TIFFWarningExt(tif->tif_clientdata,module,
4866 "incorrect count for field \"%s\", expected 2, got %d",
4867 fip->field_name,(int)dp->tdir_count);
4870 err=TIFFReadDirEntryShortArray(tif,dp,&data);
4871 if (err==TIFFReadDirEntryErrOk)
4874 m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
4881 case TIFF_SETGET_C0_UINT8:
4884 assert(fip->field_readcount>=1);
4885 assert(fip->field_passcount==0);
4886 if (dp->tdir_count!=(uint64)fip->field_readcount) {
4887 TIFFWarningExt(tif->tif_clientdata,module,
4888 "incorrect count for field \"%s\", expected %d, got %d",
4889 fip->field_name,(int) fip->field_readcount, (int)dp->tdir_count);
4894 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4895 if (err==TIFFReadDirEntryErrOk)
4898 m=TIFFSetField(tif,dp->tdir_tag,data);
4907 case TIFF_SETGET_C0_UINT16:
4910 assert(fip->field_readcount>=1);
4911 assert(fip->field_passcount==0);
4912 if (dp->tdir_count!=(uint64)fip->field_readcount)
4916 err=TIFFReadDirEntryShortArray(tif,dp,&data);
4917 if (err==TIFFReadDirEntryErrOk)
4920 m=TIFFSetField(tif,dp->tdir_tag,data);
4929 case TIFF_SETGET_C0_UINT32:
4932 assert(fip->field_readcount>=1);
4933 assert(fip->field_passcount==0);
4934 if (dp->tdir_count!=(uint64)fip->field_readcount)
4938 err=TIFFReadDirEntryLongArray(tif,dp,&data);
4939 if (err==TIFFReadDirEntryErrOk)
4942 m=TIFFSetField(tif,dp->tdir_tag,data);
4951 case TIFF_SETGET_C0_FLOAT:
4954 assert(fip->field_readcount>=1);
4955 assert(fip->field_passcount==0);
4956 if (dp->tdir_count!=(uint64)fip->field_readcount)
4960 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
4961 if (err==TIFFReadDirEntryErrOk)
4964 m=TIFFSetField(tif,dp->tdir_tag,data);
4973 case TIFF_SETGET_C16_ASCII:
4976 assert(fip->field_readcount==TIFF_VARIABLE);
4977 assert(fip->field_passcount==1);
4978 if (dp->tdir_count>0xFFFF)
4979 err=TIFFReadDirEntryErrCount;
4982 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4983 if (err==TIFFReadDirEntryErrOk)
4986 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
4995 case TIFF_SETGET_C16_UINT8:
4998 assert(fip->field_readcount==TIFF_VARIABLE);
4999 assert(fip->field_passcount==1);
5000 if (dp->tdir_count>0xFFFF)
5001 err=TIFFReadDirEntryErrCount;
5004 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5005 if (err==TIFFReadDirEntryErrOk)
5008 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5017 case TIFF_SETGET_C16_UINT16:
5020 assert(fip->field_readcount==TIFF_VARIABLE);
5021 assert(fip->field_passcount==1);
5022 if (dp->tdir_count>0xFFFF)
5023 err=TIFFReadDirEntryErrCount;
5026 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5027 if (err==TIFFReadDirEntryErrOk)
5030 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5039 case TIFF_SETGET_C16_UINT32:
5042 assert(fip->field_readcount==TIFF_VARIABLE);
5043 assert(fip->field_passcount==1);
5044 if (dp->tdir_count>0xFFFF)
5045 err=TIFFReadDirEntryErrCount;
5048 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5049 if (err==TIFFReadDirEntryErrOk)
5052 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5061 case TIFF_SETGET_C16_UINT64:
5064 assert(fip->field_readcount==TIFF_VARIABLE);
5065 assert(fip->field_passcount==1);
5066 if (dp->tdir_count>0xFFFF)
5067 err=TIFFReadDirEntryErrCount;
5070 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5071 if (err==TIFFReadDirEntryErrOk)
5074 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5083 case TIFF_SETGET_C16_FLOAT:
5086 assert(fip->field_readcount==TIFF_VARIABLE);
5087 assert(fip->field_passcount==1);
5088 if (dp->tdir_count>0xFFFF)
5089 err=TIFFReadDirEntryErrCount;
5092 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5093 if (err==TIFFReadDirEntryErrOk)
5096 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5105 case TIFF_SETGET_C16_DOUBLE:
5108 assert(fip->field_readcount==TIFF_VARIABLE);
5109 assert(fip->field_passcount==1);
5110 if (dp->tdir_count>0xFFFF)
5111 err=TIFFReadDirEntryErrCount;
5114 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5115 if (err==TIFFReadDirEntryErrOk)
5118 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5127 case TIFF_SETGET_C16_IFD8:
5130 assert(fip->field_readcount==TIFF_VARIABLE);
5131 assert(fip->field_passcount==1);
5132 if (dp->tdir_count>0xFFFF)
5133 err=TIFFReadDirEntryErrCount;
5136 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5137 if (err==TIFFReadDirEntryErrOk)
5140 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5149 case TIFF_SETGET_C32_ASCII:
5152 assert(fip->field_readcount==TIFF_VARIABLE2);
5153 assert(fip->field_passcount==1);
5154 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5155 if (err==TIFFReadDirEntryErrOk)
5158 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5166 case TIFF_SETGET_C32_UINT8:
5169 assert(fip->field_readcount==TIFF_VARIABLE2);
5170 assert(fip->field_passcount==1);
5171 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5172 if (err==TIFFReadDirEntryErrOk)
5175 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5183 case TIFF_SETGET_C32_SINT8:
5186 assert(fip->field_readcount==TIFF_VARIABLE2);
5187 assert(fip->field_passcount==1);
5188 err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
5189 if (err==TIFFReadDirEntryErrOk)
5192 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5200 case TIFF_SETGET_C32_UINT16:
5203 assert(fip->field_readcount==TIFF_VARIABLE2);
5204 assert(fip->field_passcount==1);
5205 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5206 if (err==TIFFReadDirEntryErrOk)
5209 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5217 case TIFF_SETGET_C32_SINT16:
5220 assert(fip->field_readcount==TIFF_VARIABLE2);
5221 assert(fip->field_passcount==1);
5222 err=TIFFReadDirEntrySshortArray(tif,dp,&data);
5223 if (err==TIFFReadDirEntryErrOk)
5226 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5234 case TIFF_SETGET_C32_UINT32:
5237 assert(fip->field_readcount==TIFF_VARIABLE2);
5238 assert(fip->field_passcount==1);
5239 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5240 if (err==TIFFReadDirEntryErrOk)
5243 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5251 case TIFF_SETGET_C32_SINT32:
5254 assert(fip->field_readcount==TIFF_VARIABLE2);
5255 assert(fip->field_passcount==1);
5256 err=TIFFReadDirEntrySlongArray(tif,dp,&data);
5257 if (err==TIFFReadDirEntryErrOk)
5260 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5268 case TIFF_SETGET_C32_UINT64:
5271 assert(fip->field_readcount==TIFF_VARIABLE2);
5272 assert(fip->field_passcount==1);
5273 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5274 if (err==TIFFReadDirEntryErrOk)
5277 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5285 case TIFF_SETGET_C32_SINT64:
5288 assert(fip->field_readcount==TIFF_VARIABLE2);
5289 assert(fip->field_passcount==1);
5290 err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
5291 if (err==TIFFReadDirEntryErrOk)
5294 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5302 case TIFF_SETGET_C32_FLOAT:
5305 assert(fip->field_readcount==TIFF_VARIABLE2);
5306 assert(fip->field_passcount==1);
5307 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5308 if (err==TIFFReadDirEntryErrOk)
5311 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5319 case TIFF_SETGET_C32_DOUBLE:
5322 assert(fip->field_readcount==TIFF_VARIABLE2);
5323 assert(fip->field_passcount==1);
5324 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5325 if (err==TIFFReadDirEntryErrOk)
5328 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5336 case TIFF_SETGET_C32_IFD8:
5339 assert(fip->field_readcount==TIFF_VARIABLE2);
5340 assert(fip->field_passcount==1);
5341 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5342 if (err==TIFFReadDirEntryErrOk)
5345 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5354 assert(0); /* we should never get here */
5357 if (err!=TIFFReadDirEntryErrOk)
5359 TIFFReadDirEntryOutputErr(tif,err,module,fip->field_name,recover);
5366 * Fetch a set of offsets or lengths.
5367 * While this routine says "strips", in fact it's also used for tiles.
5370 TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
5372 static const char module[] = "TIFFFetchStripThing";
5373 enum TIFFReadDirEntryErr err;
5375 err=TIFFReadDirEntryLong8Array(tif,dir,&data);
5376 if (err!=TIFFReadDirEntryErrOk)
5378 const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5379 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
5382 if (dir->tdir_count!=(uint64)nstrips)
5384 uint64* resizeddata;
5385 resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
5386 if (resizeddata==0) {
5390 if (dir->tdir_count<(uint64)nstrips)
5392 _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
5393 _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
5396 _TIFFmemcpy(resizeddata,data,nstrips*sizeof(uint64));
5405 * Fetch and set the SubjectDistance EXIF tag.
5408 TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
5410 static const char module[] = "TIFFFetchSubjectDistance";
5411 enum TIFFReadDirEntryErr err;
5414 assert(sizeof(double)==8);
5415 assert(sizeof(uint64)==8);
5416 assert(sizeof(uint32)==4);
5417 if (dir->tdir_count!=1)
5418 err=TIFFReadDirEntryErrCount;
5419 else if (dir->tdir_type!=TIFF_RATIONAL)
5420 err=TIFFReadDirEntryErrType;
5423 if (!(tif->tif_flags&TIFF_BIGTIFF))
5426 offset=*(uint32*)(&dir->tdir_offset);
5427 if (tif->tif_flags&TIFF_SWAB)
5428 TIFFSwabLong(&offset);
5429 err=TIFFReadDirEntryData(tif,offset,8,m.i);
5433 m.l=dir->tdir_offset.toff_long8;
5434 err=TIFFReadDirEntryErrOk;
5437 if (err==TIFFReadDirEntryErrOk)
5440 if (tif->tif_flags&TIFF_SWAB)
5441 TIFFSwabArrayOfLong(m.i,2);
5444 else if (m.i[0]==0xFFFFFFFF)
5446 * XXX: Numerator 0xFFFFFFFF means that we have infinite
5447 * distance. Indicate that with a negative floating point
5448 * SubjectDistance value.
5452 n=(double)m.i[0]/(double)m.i[1];
5453 return(TIFFSetField(tif,dir->tdir_tag,n));
5457 TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
5463 * Replace a single strip (tile) of uncompressed data by multiple strips
5464 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5465 * dealing with large images or for dealing with machines with a limited
5469 ChopUpSingleUncompressedStrip(TIFF* tif)
5471 register TIFFDirectory *td = &tif->tif_dir;
5475 uint64 rowblockbytes;
5480 uint32 rowsperstrip;
5484 bytecount = td->td_stripbytecount[0];
5485 offset = td->td_stripoffset[0];
5486 assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
5487 if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
5488 (!isUpSampled(tif)))
5489 rowblock = td->td_ycbcrsubsampling[1];
5492 rowblockbytes = TIFFVTileSize64(tif, rowblock);
5494 * Make the rows hold at least one scanline, but fill specified amount
5495 * of data if possible.
5497 if (rowblockbytes > STRIP_SIZE_DEFAULT) {
5498 stripbytes = rowblockbytes;
5499 rowsperstrip = rowblock;
5500 } else if (rowblockbytes > 0 ) {
5501 uint32 rowblocksperstrip;
5502 rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
5503 rowsperstrip = rowblocksperstrip * rowblock;
5504 stripbytes = rowblocksperstrip * rowblockbytes;
5510 * never increase the number of strips in an image
5512 if (rowsperstrip >= td->td_rowsperstrip)
5514 nstrips64 = TIFFhowmany_64(bytecount, stripbytes);
5515 if ((nstrips64==0)||(nstrips64>0xFFFFFFFF)) /* something is wonky, do nothing. */
5517 nstrips32 = (uint32)nstrips64;
5519 newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
5520 "for chopped \"StripByteCounts\" array");
5521 newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
5522 "for chopped \"StripOffsets\" array");
5523 if (newcounts == NULL || newoffsets == NULL) {
5525 * Unable to allocate new strip information, give up and use
5526 * the original one strip information.
5528 if (newcounts != NULL)
5529 _TIFFfree(newcounts);
5530 if (newoffsets != NULL)
5531 _TIFFfree(newoffsets);
5535 * Fill the strip information arrays with new bytecounts and offsets
5536 * that reflect the broken-up format.
5538 for (strip = 0; strip < nstrips32; strip++) {
5539 if (stripbytes > bytecount)
5540 stripbytes = bytecount;
5541 newcounts[strip] = stripbytes;
5542 newoffsets[strip] = offset;
5543 offset += stripbytes;
5544 bytecount -= stripbytes;
5547 * Replace old single strip info with multi-strip info.
5549 td->td_stripsperimage = td->td_nstrips = nstrips32;
5550 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
5552 _TIFFfree(td->td_stripbytecount);
5553 _TIFFfree(td->td_stripoffset);
5554 td->td_stripbytecount = newcounts;
5555 td->td_stripoffset = newoffsets;
5556 td->td_stripbytecountsorted = 1;
5559 int _TIFFFillStriles( TIFF *tif )
5561 #if defined(DEFER_STRILE_LOAD)
5562 register TIFFDirectory *td = &tif->tif_dir;
5563 int return_value = 1;
5565 if( td->td_stripoffset != NULL )
5568 if( td->td_stripoffset_entry.tdir_count == 0 )
5571 if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
5572 td->td_nstrips,&td->td_stripoffset))
5577 if (!TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
5578 td->td_nstrips,&td->td_stripbytecount))
5583 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
5584 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
5586 if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
5589 tif->tif_dir.td_stripbytecountsorted = 1;
5590 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
5591 if (tif->tif_dir.td_stripoffset[strip - 1] >
5592 tif->tif_dir.td_stripoffset[strip]) {
5593 tif->tif_dir.td_stripbytecountsorted = 0;
5599 return return_value;
5600 #else /* !defined(DEFER_STRILE_LOAD) */
5607 /* vim: set ts=8 sts=8 sw=8 noet: */