2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Directory Read Support Routines.
31 /* Suggested pending improvements:
32 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33 * the pointer to the appropriate TIFFField structure early on in
34 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
42 #define FAILED_FII ((uint32_t) -1)
45 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
46 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
48 extern void TIFFCvtIEEEFloatToNative(TIFF
*, uint32_t, float*);
49 extern void TIFFCvtIEEEDoubleToNative(TIFF
*, uint32_t, double*);
52 enum TIFFReadDirEntryErr
{
53 TIFFReadDirEntryErrOk
= 0,
54 TIFFReadDirEntryErrCount
= 1,
55 TIFFReadDirEntryErrType
= 2,
56 TIFFReadDirEntryErrIo
= 3,
57 TIFFReadDirEntryErrRange
= 4,
58 TIFFReadDirEntryErrPsdif
= 5,
59 TIFFReadDirEntryErrSizesan
= 6,
60 TIFFReadDirEntryErrAlloc
= 7,
63 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8_t* value
);
64 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t* value
);
65 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* value
);
66 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t* value
);
67 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
);
68 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
69 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t* value
);
71 static enum TIFFReadDirEntryErr
TIFFReadDirEntryArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* count
, uint32_t desttypesize
, void** value
);
72 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8_t** value
);
73 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySbyteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int8_t** value
);
74 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t** value
);
75 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySshortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int16_t** value
);
76 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t** value
);
77 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int32_t** value
);
78 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t** value
);
79 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, int64_t** value
);
80 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloatArray(TIFF
* tif
, TIFFDirEntry
* direntry
, float** value
);
81 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDoubleArray(TIFF
* tif
, TIFFDirEntry
* direntry
, double** value
);
82 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t** value
);
84 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t* value
);
86 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
89 static void TIFFReadDirEntryCheckedByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8_t* value
);
90 static void TIFFReadDirEntryCheckedSbyte(TIFF
* tif
, TIFFDirEntry
* direntry
, int8_t* value
);
91 static void TIFFReadDirEntryCheckedShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t* value
);
92 static void TIFFReadDirEntryCheckedSshort(TIFF
* tif
, TIFFDirEntry
* direntry
, int16_t* value
);
93 static void TIFFReadDirEntryCheckedLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* value
);
94 static void TIFFReadDirEntryCheckedSlong(TIFF
* tif
, TIFFDirEntry
* direntry
, int32_t* value
);
95 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t* value
);
96 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSlong8(TIFF
* tif
, TIFFDirEntry
* direntry
, int64_t* value
);
97 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedRational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
98 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSrational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
99 static void TIFFReadDirEntryCheckedFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
);
100 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
102 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value
);
103 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteShort(uint16_t value
);
104 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSshort(int16_t value
);
105 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong(uint32_t value
);
106 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong(int32_t value
);
107 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong8(uint64_t value
);
108 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong8(int64_t value
);
110 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value
);
111 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value
);
112 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value
);
113 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value
);
114 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value
);
115 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value
);
116 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value
);
118 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSbyte(int8_t value
);
119 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSshort(int16_t value
);
120 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong(uint32_t value
);
121 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong(int32_t value
);
122 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong8(uint64_t value
);
123 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong8(int64_t value
);
125 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortShort(uint16_t value
);
126 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong(uint32_t value
);
127 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong(int32_t value
);
128 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value
);
129 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value
);
131 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSbyte(int8_t value
);
132 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSshort(int16_t value
);
133 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSlong(int32_t value
);
134 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongLong8(uint64_t value
);
135 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSlong8(int64_t value
);
137 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlongLong(uint32_t value
);
138 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value
);
139 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value
);
141 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value
);
142 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value
);
143 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Slong(int32_t value
);
144 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value
);
146 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value
);
148 static enum TIFFReadDirEntryErr
TIFFReadDirEntryData(TIFF
* tif
, uint64_t offset
, tmsize_t size
, void* dest
);
149 static void TIFFReadDirEntryOutputErr(TIFF
* tif
, enum TIFFReadDirEntryErr err
, const char* module
, const char* tagname
, int recover
);
151 static void TIFFReadDirectoryCheckOrder(TIFF
* tif
, TIFFDirEntry
* dir
, uint16_t dircount
);
152 static TIFFDirEntry
* TIFFReadDirectoryFindEntry(TIFF
* tif
, TIFFDirEntry
* dir
, uint16_t dircount
, uint16_t tagid
);
153 static void TIFFReadDirectoryFindFieldInfo(TIFF
* tif
, uint16_t tagid
, uint32_t* fii
);
155 static int EstimateStripByteCounts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16_t dircount
);
156 static void MissingRequired(TIFF
*, const char*);
157 static int TIFFCheckDirOffset(TIFF
* tif
, uint64_t diroff
);
158 static int CheckDirCount(TIFF
*, TIFFDirEntry
*, uint32_t);
159 static uint16_t TIFFFetchDirectory(TIFF
* tif
, uint64_t diroff
, TIFFDirEntry
** pdir
, uint64_t* nextdiroff
);
160 static int TIFFFetchNormalTag(TIFF
*, TIFFDirEntry
*, int recover
);
161 static int TIFFFetchStripThing(TIFF
* tif
, TIFFDirEntry
* dir
, uint32_t nstrips
, uint64_t** lpp
);
162 static int TIFFFetchSubjectDistance(TIFF
*, TIFFDirEntry
*);
163 static void ChopUpSingleUncompressedStrip(TIFF
*);
164 static void TryChopUpUncompressedBigTiff(TIFF
*);
165 static uint64_t TIFFReadUInt64(const uint8_t *value
);
166 static int _TIFFGetMaxColorChannels(uint16_t photometric
);
168 static int _TIFFFillStrilesInternal( TIFF
*tif
, int loadStripByteCount
);
170 typedef union _UInt64Aligned_t
180 Unaligned safe copy of a uint64_t value from an octet array.
182 static uint64_t TIFFReadUInt64(const uint8_t *value
)
184 UInt64Aligned_t result
;
186 result
.c
[0]=value
[0];
187 result
.c
[1]=value
[1];
188 result
.c
[2]=value
[2];
189 result
.c
[3]=value
[3];
190 result
.c
[4]=value
[4];
191 result
.c
[5]=value
[5];
192 result
.c
[6]=value
[6];
193 result
.c
[7]=value
[7];
198 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8_t* value
)
200 enum TIFFReadDirEntryErr err
;
201 if (direntry
->tdir_count
!=1)
202 return(TIFFReadDirEntryErrCount
);
203 switch (direntry
->tdir_type
)
206 case TIFF_UNDEFINED
: /* Support to read TIFF_UNDEFINED with field_readcount==1 */
207 TIFFReadDirEntryCheckedByte(tif
,direntry
,value
);
208 return(TIFFReadDirEntryErrOk
);
212 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
213 err
=TIFFReadDirEntryCheckRangeByteSbyte(m
);
214 if (err
!=TIFFReadDirEntryErrOk
)
217 return(TIFFReadDirEntryErrOk
);
222 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
223 err
=TIFFReadDirEntryCheckRangeByteShort(m
);
224 if (err
!=TIFFReadDirEntryErrOk
)
227 return(TIFFReadDirEntryErrOk
);
232 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
233 err
=TIFFReadDirEntryCheckRangeByteSshort(m
);
234 if (err
!=TIFFReadDirEntryErrOk
)
237 return(TIFFReadDirEntryErrOk
);
242 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
243 err
=TIFFReadDirEntryCheckRangeByteLong(m
);
244 if (err
!=TIFFReadDirEntryErrOk
)
247 return(TIFFReadDirEntryErrOk
);
252 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
253 err
=TIFFReadDirEntryCheckRangeByteSlong(m
);
254 if (err
!=TIFFReadDirEntryErrOk
)
257 return(TIFFReadDirEntryErrOk
);
262 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
263 if (err
!=TIFFReadDirEntryErrOk
)
265 err
=TIFFReadDirEntryCheckRangeByteLong8(m
);
266 if (err
!=TIFFReadDirEntryErrOk
)
269 return(TIFFReadDirEntryErrOk
);
274 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
275 if (err
!=TIFFReadDirEntryErrOk
)
277 err
=TIFFReadDirEntryCheckRangeByteSlong8(m
);
278 if (err
!=TIFFReadDirEntryErrOk
)
281 return(TIFFReadDirEntryErrOk
);
284 return(TIFFReadDirEntryErrType
);
288 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t* value
)
290 enum TIFFReadDirEntryErr err
;
291 if (direntry
->tdir_count
!=1)
292 return(TIFFReadDirEntryErrCount
);
293 switch (direntry
->tdir_type
)
298 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
300 return(TIFFReadDirEntryErrOk
);
305 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
306 err
=TIFFReadDirEntryCheckRangeShortSbyte(m
);
307 if (err
!=TIFFReadDirEntryErrOk
)
310 return(TIFFReadDirEntryErrOk
);
313 TIFFReadDirEntryCheckedShort(tif
,direntry
,value
);
314 return(TIFFReadDirEntryErrOk
);
318 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
319 err
=TIFFReadDirEntryCheckRangeShortSshort(m
);
320 if (err
!=TIFFReadDirEntryErrOk
)
323 return(TIFFReadDirEntryErrOk
);
328 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
329 err
=TIFFReadDirEntryCheckRangeShortLong(m
);
330 if (err
!=TIFFReadDirEntryErrOk
)
333 return(TIFFReadDirEntryErrOk
);
338 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
339 err
=TIFFReadDirEntryCheckRangeShortSlong(m
);
340 if (err
!=TIFFReadDirEntryErrOk
)
343 return(TIFFReadDirEntryErrOk
);
348 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
349 if (err
!=TIFFReadDirEntryErrOk
)
351 err
=TIFFReadDirEntryCheckRangeShortLong8(m
);
352 if (err
!=TIFFReadDirEntryErrOk
)
355 return(TIFFReadDirEntryErrOk
);
360 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
361 if (err
!=TIFFReadDirEntryErrOk
)
363 err
=TIFFReadDirEntryCheckRangeShortSlong8(m
);
364 if (err
!=TIFFReadDirEntryErrOk
)
367 return(TIFFReadDirEntryErrOk
);
370 return(TIFFReadDirEntryErrType
);
374 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* value
)
376 enum TIFFReadDirEntryErr err
;
377 if (direntry
->tdir_count
!=1)
378 return(TIFFReadDirEntryErrCount
);
379 switch (direntry
->tdir_type
)
384 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
386 return(TIFFReadDirEntryErrOk
);
391 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
392 err
=TIFFReadDirEntryCheckRangeLongSbyte(m
);
393 if (err
!=TIFFReadDirEntryErrOk
)
396 return(TIFFReadDirEntryErrOk
);
401 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
403 return(TIFFReadDirEntryErrOk
);
408 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
409 err
=TIFFReadDirEntryCheckRangeLongSshort(m
);
410 if (err
!=TIFFReadDirEntryErrOk
)
413 return(TIFFReadDirEntryErrOk
);
416 TIFFReadDirEntryCheckedLong(tif
,direntry
,value
);
417 return(TIFFReadDirEntryErrOk
);
421 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
422 err
=TIFFReadDirEntryCheckRangeLongSlong(m
);
423 if (err
!=TIFFReadDirEntryErrOk
)
426 return(TIFFReadDirEntryErrOk
);
431 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
432 if (err
!=TIFFReadDirEntryErrOk
)
434 err
=TIFFReadDirEntryCheckRangeLongLong8(m
);
435 if (err
!=TIFFReadDirEntryErrOk
)
438 return(TIFFReadDirEntryErrOk
);
443 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
444 if (err
!=TIFFReadDirEntryErrOk
)
446 err
=TIFFReadDirEntryCheckRangeLongSlong8(m
);
447 if (err
!=TIFFReadDirEntryErrOk
)
450 return(TIFFReadDirEntryErrOk
);
453 return(TIFFReadDirEntryErrType
);
457 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t* value
)
459 enum TIFFReadDirEntryErr err
;
460 if (direntry
->tdir_count
!=1)
461 return(TIFFReadDirEntryErrCount
);
462 switch (direntry
->tdir_type
)
467 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
469 return(TIFFReadDirEntryErrOk
);
474 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
475 err
=TIFFReadDirEntryCheckRangeLong8Sbyte(m
);
476 if (err
!=TIFFReadDirEntryErrOk
)
479 return(TIFFReadDirEntryErrOk
);
484 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
486 return(TIFFReadDirEntryErrOk
);
491 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
492 err
=TIFFReadDirEntryCheckRangeLong8Sshort(m
);
493 if (err
!=TIFFReadDirEntryErrOk
)
496 return(TIFFReadDirEntryErrOk
);
501 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
503 return(TIFFReadDirEntryErrOk
);
508 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
509 err
=TIFFReadDirEntryCheckRangeLong8Slong(m
);
510 if (err
!=TIFFReadDirEntryErrOk
)
513 return(TIFFReadDirEntryErrOk
);
516 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,value
);
521 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
522 if (err
!=TIFFReadDirEntryErrOk
)
524 err
=TIFFReadDirEntryCheckRangeLong8Slong8(m
);
525 if (err
!=TIFFReadDirEntryErrOk
)
528 return(TIFFReadDirEntryErrOk
);
531 return(TIFFReadDirEntryErrType
);
535 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
)
537 enum TIFFReadDirEntryErr err
;
538 if (direntry
->tdir_count
!=1)
539 return(TIFFReadDirEntryErrCount
);
540 switch (direntry
->tdir_type
)
545 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
547 return(TIFFReadDirEntryErrOk
);
552 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
554 return(TIFFReadDirEntryErrOk
);
559 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
561 return(TIFFReadDirEntryErrOk
);
566 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
568 return(TIFFReadDirEntryErrOk
);
573 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
575 return(TIFFReadDirEntryErrOk
);
580 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
582 return(TIFFReadDirEntryErrOk
);
587 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
588 if (err
!=TIFFReadDirEntryErrOk
)
590 #if defined(__WIN32__) && (_MSC_VER < 1500)
592 * XXX: MSVC 6.0 does not support conversion
593 * of 64-bit integers into floating point
596 *value
= _TIFFUInt64ToFloat(m
);
600 return(TIFFReadDirEntryErrOk
);
605 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
606 if (err
!=TIFFReadDirEntryErrOk
)
609 return(TIFFReadDirEntryErrOk
);
614 err
=TIFFReadDirEntryCheckedRational(tif
,direntry
,&m
);
615 if (err
!=TIFFReadDirEntryErrOk
)
618 return(TIFFReadDirEntryErrOk
);
623 err
=TIFFReadDirEntryCheckedSrational(tif
,direntry
,&m
);
624 if (err
!=TIFFReadDirEntryErrOk
)
627 return(TIFFReadDirEntryErrOk
);
630 TIFFReadDirEntryCheckedFloat(tif
,direntry
,value
);
631 return(TIFFReadDirEntryErrOk
);
635 err
=TIFFReadDirEntryCheckedDouble(tif
,direntry
,&m
);
636 if (err
!=TIFFReadDirEntryErrOk
)
638 if ((m
> FLT_MAX
) || (m
< -FLT_MAX
))
639 return(TIFFReadDirEntryErrRange
);
641 return(TIFFReadDirEntryErrOk
);
644 return(TIFFReadDirEntryErrType
);
648 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
650 enum TIFFReadDirEntryErr err
;
651 if (direntry
->tdir_count
!=1)
652 return(TIFFReadDirEntryErrCount
);
653 switch (direntry
->tdir_type
)
658 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
660 return(TIFFReadDirEntryErrOk
);
665 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
667 return(TIFFReadDirEntryErrOk
);
672 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
674 return(TIFFReadDirEntryErrOk
);
679 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
681 return(TIFFReadDirEntryErrOk
);
686 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
688 return(TIFFReadDirEntryErrOk
);
693 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
695 return(TIFFReadDirEntryErrOk
);
700 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
701 if (err
!=TIFFReadDirEntryErrOk
)
703 #if defined(__WIN32__) && (_MSC_VER < 1500)
705 * XXX: MSVC 6.0 does not support conversion
706 * of 64-bit integers into floating point
709 *value
= _TIFFUInt64ToDouble(m
);
713 return(TIFFReadDirEntryErrOk
);
718 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
719 if (err
!=TIFFReadDirEntryErrOk
)
722 return(TIFFReadDirEntryErrOk
);
725 err
=TIFFReadDirEntryCheckedRational(tif
,direntry
,value
);
728 err
=TIFFReadDirEntryCheckedSrational(tif
,direntry
,value
);
733 TIFFReadDirEntryCheckedFloat(tif
,direntry
,&m
);
735 return(TIFFReadDirEntryErrOk
);
738 err
=TIFFReadDirEntryCheckedDouble(tif
,direntry
,value
);
741 return(TIFFReadDirEntryErrType
);
745 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t* value
)
747 enum TIFFReadDirEntryErr err
;
748 if (direntry
->tdir_count
!=1)
749 return(TIFFReadDirEntryErrCount
);
750 switch (direntry
->tdir_type
)
756 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
758 return(TIFFReadDirEntryErrOk
);
762 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,value
);
765 return(TIFFReadDirEntryErrType
);
770 #define INITIAL_THRESHOLD (1024 * 1024)
771 #define THRESHOLD_MULTIPLIER 10
772 #define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
774 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDataAndRealloc(
775 TIFF
* tif
, uint64_t offset
, tmsize_t size
, void** pdest
)
777 #if SIZEOF_SIZE_T == 8
778 tmsize_t threshold
= INITIAL_THRESHOLD
;
780 tmsize_t already_read
= 0;
782 assert( !isMapped(tif
) );
784 if (!SeekOK(tif
,offset
))
785 return(TIFFReadDirEntryErrIo
);
787 /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
788 /* so as to avoid allocating too much memory in case the file is too */
789 /* short. We could ask for the file size, but this might be */
790 /* expensive with some I/O layers (think of reading a gzipped file) */
791 /* Restrict to 64 bit processes, so as to avoid reallocs() */
792 /* on 32 bit processes where virtual memory is scarce. */
793 while( already_read
< size
)
797 tmsize_t to_read
= size
- already_read
;
798 #if SIZEOF_SIZE_T == 8
799 if( to_read
>= threshold
&& threshold
< MAX_THRESHOLD
)
802 threshold
*= THRESHOLD_MULTIPLIER
;
806 new_dest
= (uint8_t*) _TIFFrealloc(
807 *pdest
, already_read
+ to_read
);
808 if( new_dest
== NULL
)
810 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
811 "Failed to allocate memory for %s "
812 "(%"TIFF_SSIZE_FORMAT
" elements of %"TIFF_SSIZE_FORMAT
" bytes each)",
813 "TIFFReadDirEntryArray",
814 (tmsize_t
) 1, already_read
+ to_read
);
815 return TIFFReadDirEntryErrAlloc
;
819 bytes_read
= TIFFReadFile(tif
,
820 (char*)*pdest
+ already_read
, to_read
);
821 already_read
+= bytes_read
;
822 if (bytes_read
!= to_read
) {
823 return TIFFReadDirEntryErrIo
;
826 return TIFFReadDirEntryErrOk
;
829 static enum TIFFReadDirEntryErr
TIFFReadDirEntryArrayWithLimit(
830 TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* count
, uint32_t desttypesize
,
831 void** value
, uint64_t maxcount
)
836 uint64_t target_count64
;
837 int original_datasize_clamped
;
838 typesize
=TIFFDataWidth(direntry
->tdir_type
);
840 target_count64
= (direntry
->tdir_count
> maxcount
) ?
841 maxcount
: direntry
->tdir_count
;
843 if ((target_count64
==0)||(typesize
==0))
846 return(TIFFReadDirEntryErrOk
);
850 /* We just want to know if the original tag size is more than 4 bytes
851 * (classic TIFF) or 8 bytes (BigTIFF)
853 original_datasize_clamped
=
854 ((direntry
->tdir_count
> 10) ? 10 : (int)direntry
->tdir_count
) * typesize
;
857 * As a sanity check, make sure we have no more than a 2GB tag array
858 * in either the current data type or the dest data type. This also
859 * avoids problems with overflow of tmsize_t on 32bit systems.
861 if ((uint64_t)(2147483647 / typesize
) < target_count64
)
862 return(TIFFReadDirEntryErrSizesan
);
863 if ((uint64_t)(2147483647 / desttypesize
) < target_count64
)
864 return(TIFFReadDirEntryErrSizesan
);
866 *count
=(uint32_t)target_count64
;
867 datasize
=(*count
)*typesize
;
868 assert((tmsize_t
)datasize
>0);
870 if( isMapped(tif
) && datasize
> (uint64_t)tif
->tif_size
)
871 return TIFFReadDirEntryErrIo
;
873 if( !isMapped(tif
) &&
874 (((tif
->tif_flags
&TIFF_BIGTIFF
) && datasize
> 8) ||
875 (!(tif
->tif_flags
&TIFF_BIGTIFF
) && datasize
> 4)) )
881 data
=_TIFFCheckMalloc(tif
, *count
, typesize
, "ReadDirEntryArray");
883 return(TIFFReadDirEntryErrAlloc
);
885 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
887 /* Only the condition on original_datasize_clamped. The second
888 * one is implied, but Coverity Scan cannot see it. */
889 if (original_datasize_clamped
<=4 && datasize
<= 4 )
890 _TIFFmemcpy(data
,&direntry
->tdir_offset
,datasize
);
893 enum TIFFReadDirEntryErr err
;
894 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
895 if (tif
->tif_flags
&TIFF_SWAB
)
896 TIFFSwabLong(&offset
);
898 err
=TIFFReadDirEntryData(tif
, (uint64_t)offset
, (tmsize_t
)datasize
, data
);
900 err
=TIFFReadDirEntryDataAndRealloc(tif
, (uint64_t)offset
, (tmsize_t
)datasize
, &data
);
901 if (err
!=TIFFReadDirEntryErrOk
)
910 /* See above comment for the Classic TIFF case */
911 if (original_datasize_clamped
<=8 && datasize
<= 8 )
912 _TIFFmemcpy(data
,&direntry
->tdir_offset
,datasize
);
915 enum TIFFReadDirEntryErr err
;
916 uint64_t offset
= direntry
->tdir_offset
.toff_long8
;
917 if (tif
->tif_flags
&TIFF_SWAB
)
918 TIFFSwabLong8(&offset
);
920 err
=TIFFReadDirEntryData(tif
, (uint64_t)offset
, (tmsize_t
)datasize
, data
);
922 err
=TIFFReadDirEntryDataAndRealloc(tif
, (uint64_t)offset
, (tmsize_t
)datasize
, &data
);
923 if (err
!=TIFFReadDirEntryErrOk
)
931 return(TIFFReadDirEntryErrOk
);
934 static enum TIFFReadDirEntryErr
TIFFReadDirEntryArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* count
, uint32_t desttypesize
, void** value
)
936 return TIFFReadDirEntryArrayWithLimit(tif
, direntry
, count
,
937 desttypesize
, value
, ~((uint64_t)0));
940 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8_t** value
)
942 enum TIFFReadDirEntryErr err
;
946 switch (direntry
->tdir_type
)
960 return(TIFFReadDirEntryErrType
);
962 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,1,&origdata
);
963 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
968 switch (direntry
->tdir_type
)
973 *value
=(uint8_t*)origdata
;
974 return(TIFFReadDirEntryErrOk
);
980 for (n
=0; n
<count
; n
++)
982 err
=TIFFReadDirEntryCheckRangeByteSbyte(*m
);
983 if (err
!=TIFFReadDirEntryErrOk
)
990 *value
=(uint8_t*)origdata
;
991 return(TIFFReadDirEntryErrOk
);
994 data
=(uint8_t*)_TIFFmalloc(count
);
998 return(TIFFReadDirEntryErrAlloc
);
1000 switch (direntry
->tdir_type
)
1007 ma
=(uint16_t*)origdata
;
1009 for (n
=0; n
<count
; n
++)
1011 if (tif
->tif_flags
&TIFF_SWAB
)
1013 err
=TIFFReadDirEntryCheckRangeByteShort(*ma
);
1014 if (err
!=TIFFReadDirEntryErrOk
)
1016 *mb
++=(uint8_t)(*ma
++);
1025 ma
=(int16_t*)origdata
;
1027 for (n
=0; n
<count
; n
++)
1029 if (tif
->tif_flags
&TIFF_SWAB
)
1030 TIFFSwabShort((uint16_t*)ma
);
1031 err
=TIFFReadDirEntryCheckRangeByteSshort(*ma
);
1032 if (err
!=TIFFReadDirEntryErrOk
)
1034 *mb
++=(uint8_t)(*ma
++);
1043 ma
=(uint32_t*)origdata
;
1045 for (n
=0; n
<count
; n
++)
1047 if (tif
->tif_flags
&TIFF_SWAB
)
1049 err
=TIFFReadDirEntryCheckRangeByteLong(*ma
);
1050 if (err
!=TIFFReadDirEntryErrOk
)
1052 *mb
++=(uint8_t)(*ma
++);
1061 ma
=(int32_t*)origdata
;
1063 for (n
=0; n
<count
; n
++)
1065 if (tif
->tif_flags
&TIFF_SWAB
)
1066 TIFFSwabLong((uint32_t*)ma
);
1067 err
=TIFFReadDirEntryCheckRangeByteSlong(*ma
);
1068 if (err
!=TIFFReadDirEntryErrOk
)
1070 *mb
++=(uint8_t)(*ma
++);
1079 ma
=(uint64_t*)origdata
;
1081 for (n
=0; n
<count
; n
++)
1083 if (tif
->tif_flags
&TIFF_SWAB
)
1085 err
=TIFFReadDirEntryCheckRangeByteLong8(*ma
);
1086 if (err
!=TIFFReadDirEntryErrOk
)
1088 *mb
++=(uint8_t)(*ma
++);
1097 ma
=(int64_t*)origdata
;
1099 for (n
=0; n
<count
; n
++)
1101 if (tif
->tif_flags
&TIFF_SWAB
)
1102 TIFFSwabLong8((uint64_t*)ma
);
1103 err
=TIFFReadDirEntryCheckRangeByteSlong8(*ma
);
1104 if (err
!=TIFFReadDirEntryErrOk
)
1106 *mb
++=(uint8_t)(*ma
++);
1111 _TIFFfree(origdata
);
1112 if (err
!=TIFFReadDirEntryErrOk
)
1118 return(TIFFReadDirEntryErrOk
);
1121 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySbyteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int8_t** value
)
1123 enum TIFFReadDirEntryErr err
;
1127 switch (direntry
->tdir_type
)
1129 case TIFF_UNDEFINED
:
1140 return(TIFFReadDirEntryErrType
);
1142 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,1,&origdata
);
1143 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1148 switch (direntry
->tdir_type
)
1150 case TIFF_UNDEFINED
:
1155 m
=(uint8_t*)origdata
;
1156 for (n
=0; n
<count
; n
++)
1158 err
=TIFFReadDirEntryCheckRangeSbyteByte(*m
);
1159 if (err
!=TIFFReadDirEntryErrOk
)
1161 _TIFFfree(origdata
);
1166 *value
=(int8_t*)origdata
;
1167 return(TIFFReadDirEntryErrOk
);
1170 *value
=(int8_t*)origdata
;
1171 return(TIFFReadDirEntryErrOk
);
1173 data
=(int8_t*)_TIFFmalloc(count
);
1176 _TIFFfree(origdata
);
1177 return(TIFFReadDirEntryErrAlloc
);
1179 switch (direntry
->tdir_type
)
1186 ma
=(uint16_t*)origdata
;
1188 for (n
=0; n
<count
; n
++)
1190 if (tif
->tif_flags
&TIFF_SWAB
)
1192 err
=TIFFReadDirEntryCheckRangeSbyteShort(*ma
);
1193 if (err
!=TIFFReadDirEntryErrOk
)
1195 *mb
++=(int8_t)(*ma
++);
1204 ma
=(int16_t*)origdata
;
1206 for (n
=0; n
<count
; n
++)
1208 if (tif
->tif_flags
&TIFF_SWAB
)
1209 TIFFSwabShort((uint16_t*)ma
);
1210 err
=TIFFReadDirEntryCheckRangeSbyteSshort(*ma
);
1211 if (err
!=TIFFReadDirEntryErrOk
)
1213 *mb
++=(int8_t)(*ma
++);
1222 ma
=(uint32_t*)origdata
;
1224 for (n
=0; n
<count
; n
++)
1226 if (tif
->tif_flags
&TIFF_SWAB
)
1228 err
=TIFFReadDirEntryCheckRangeSbyteLong(*ma
);
1229 if (err
!=TIFFReadDirEntryErrOk
)
1231 *mb
++=(int8_t)(*ma
++);
1240 ma
=(int32_t*)origdata
;
1242 for (n
=0; n
<count
; n
++)
1244 if (tif
->tif_flags
&TIFF_SWAB
)
1245 TIFFSwabLong((uint32_t*)ma
);
1246 err
=TIFFReadDirEntryCheckRangeSbyteSlong(*ma
);
1247 if (err
!=TIFFReadDirEntryErrOk
)
1249 *mb
++=(int8_t)(*ma
++);
1258 ma
=(uint64_t*)origdata
;
1260 for (n
=0; n
<count
; n
++)
1262 if (tif
->tif_flags
&TIFF_SWAB
)
1264 err
=TIFFReadDirEntryCheckRangeSbyteLong8(*ma
);
1265 if (err
!=TIFFReadDirEntryErrOk
)
1267 *mb
++=(int8_t)(*ma
++);
1276 ma
=(int64_t*)origdata
;
1278 for (n
=0; n
<count
; n
++)
1280 if (tif
->tif_flags
&TIFF_SWAB
)
1281 TIFFSwabLong8((uint64_t*)ma
);
1282 err
=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma
);
1283 if (err
!=TIFFReadDirEntryErrOk
)
1285 *mb
++=(int8_t)(*ma
++);
1290 _TIFFfree(origdata
);
1291 if (err
!=TIFFReadDirEntryErrOk
)
1297 return(TIFFReadDirEntryErrOk
);
1300 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t** value
)
1302 enum TIFFReadDirEntryErr err
;
1306 switch (direntry
->tdir_type
)
1318 return(TIFFReadDirEntryErrType
);
1320 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,2,&origdata
);
1321 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1326 switch (direntry
->tdir_type
)
1329 *value
=(uint16_t*)origdata
;
1330 if (tif
->tif_flags
&TIFF_SWAB
)
1331 TIFFSwabArrayOfShort(*value
,count
);
1332 return(TIFFReadDirEntryErrOk
);
1337 m
=(int16_t*)origdata
;
1338 for (n
=0; n
<count
; n
++)
1340 if (tif
->tif_flags
&TIFF_SWAB
)
1341 TIFFSwabShort((uint16_t*)m
);
1342 err
=TIFFReadDirEntryCheckRangeShortSshort(*m
);
1343 if (err
!=TIFFReadDirEntryErrOk
)
1345 _TIFFfree(origdata
);
1350 *value
=(uint16_t*)origdata
;
1351 return(TIFFReadDirEntryErrOk
);
1354 data
=(uint16_t*)_TIFFmalloc(count
* 2);
1357 _TIFFfree(origdata
);
1358 return(TIFFReadDirEntryErrAlloc
);
1360 switch (direntry
->tdir_type
)
1367 ma
=(uint8_t*)origdata
;
1369 for (n
=0; n
<count
; n
++)
1370 *mb
++=(uint16_t)(*ma
++);
1378 ma
=(int8_t*)origdata
;
1380 for (n
=0; n
<count
; n
++)
1382 err
=TIFFReadDirEntryCheckRangeShortSbyte(*ma
);
1383 if (err
!=TIFFReadDirEntryErrOk
)
1385 *mb
++=(uint16_t)(*ma
++);
1394 ma
=(uint32_t*)origdata
;
1396 for (n
=0; n
<count
; n
++)
1398 if (tif
->tif_flags
&TIFF_SWAB
)
1400 err
=TIFFReadDirEntryCheckRangeShortLong(*ma
);
1401 if (err
!=TIFFReadDirEntryErrOk
)
1403 *mb
++=(uint16_t)(*ma
++);
1412 ma
=(int32_t*)origdata
;
1414 for (n
=0; n
<count
; n
++)
1416 if (tif
->tif_flags
&TIFF_SWAB
)
1417 TIFFSwabLong((uint32_t*)ma
);
1418 err
=TIFFReadDirEntryCheckRangeShortSlong(*ma
);
1419 if (err
!=TIFFReadDirEntryErrOk
)
1421 *mb
++=(uint16_t)(*ma
++);
1430 ma
=(uint64_t*)origdata
;
1432 for (n
=0; n
<count
; n
++)
1434 if (tif
->tif_flags
&TIFF_SWAB
)
1436 err
=TIFFReadDirEntryCheckRangeShortLong8(*ma
);
1437 if (err
!=TIFFReadDirEntryErrOk
)
1439 *mb
++=(uint16_t)(*ma
++);
1448 ma
=(int64_t*)origdata
;
1450 for (n
=0; n
<count
; n
++)
1452 if (tif
->tif_flags
&TIFF_SWAB
)
1453 TIFFSwabLong8((uint64_t*)ma
);
1454 err
=TIFFReadDirEntryCheckRangeShortSlong8(*ma
);
1455 if (err
!=TIFFReadDirEntryErrOk
)
1457 *mb
++=(uint16_t)(*ma
++);
1462 _TIFFfree(origdata
);
1463 if (err
!=TIFFReadDirEntryErrOk
)
1469 return(TIFFReadDirEntryErrOk
);
1472 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySshortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int16_t** value
)
1474 enum TIFFReadDirEntryErr err
;
1478 switch (direntry
->tdir_type
)
1490 return(TIFFReadDirEntryErrType
);
1492 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,2,&origdata
);
1493 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1498 switch (direntry
->tdir_type
)
1504 m
=(uint16_t*)origdata
;
1505 for (n
=0; n
<count
; n
++)
1507 if (tif
->tif_flags
&TIFF_SWAB
)
1509 err
=TIFFReadDirEntryCheckRangeSshortShort(*m
);
1510 if (err
!=TIFFReadDirEntryErrOk
)
1512 _TIFFfree(origdata
);
1517 *value
=(int16_t*)origdata
;
1518 return(TIFFReadDirEntryErrOk
);
1521 *value
=(int16_t*)origdata
;
1522 if (tif
->tif_flags
&TIFF_SWAB
)
1523 TIFFSwabArrayOfShort((uint16_t*)(*value
), count
);
1524 return(TIFFReadDirEntryErrOk
);
1526 data
=(int16_t*)_TIFFmalloc(count
* 2);
1529 _TIFFfree(origdata
);
1530 return(TIFFReadDirEntryErrAlloc
);
1532 switch (direntry
->tdir_type
)
1539 ma
=(uint8_t*)origdata
;
1541 for (n
=0; n
<count
; n
++)
1542 *mb
++=(int16_t)(*ma
++);
1550 ma
=(int8_t*)origdata
;
1552 for (n
=0; n
<count
; n
++)
1553 *mb
++=(int16_t)(*ma
++);
1561 ma
=(uint32_t*)origdata
;
1563 for (n
=0; n
<count
; n
++)
1565 if (tif
->tif_flags
&TIFF_SWAB
)
1567 err
=TIFFReadDirEntryCheckRangeSshortLong(*ma
);
1568 if (err
!=TIFFReadDirEntryErrOk
)
1570 *mb
++=(int16_t)(*ma
++);
1579 ma
=(int32_t*)origdata
;
1581 for (n
=0; n
<count
; n
++)
1583 if (tif
->tif_flags
&TIFF_SWAB
)
1584 TIFFSwabLong((uint32_t*)ma
);
1585 err
=TIFFReadDirEntryCheckRangeSshortSlong(*ma
);
1586 if (err
!=TIFFReadDirEntryErrOk
)
1588 *mb
++=(int16_t)(*ma
++);
1597 ma
=(uint64_t*)origdata
;
1599 for (n
=0; n
<count
; n
++)
1601 if (tif
->tif_flags
&TIFF_SWAB
)
1603 err
=TIFFReadDirEntryCheckRangeSshortLong8(*ma
);
1604 if (err
!=TIFFReadDirEntryErrOk
)
1606 *mb
++=(int16_t)(*ma
++);
1615 ma
=(int64_t*)origdata
;
1617 for (n
=0; n
<count
; n
++)
1619 if (tif
->tif_flags
&TIFF_SWAB
)
1620 TIFFSwabLong8((uint64_t*)ma
);
1621 err
=TIFFReadDirEntryCheckRangeSshortSlong8(*ma
);
1622 if (err
!=TIFFReadDirEntryErrOk
)
1624 *mb
++=(int16_t)(*ma
++);
1629 _TIFFfree(origdata
);
1630 if (err
!=TIFFReadDirEntryErrOk
)
1636 return(TIFFReadDirEntryErrOk
);
1639 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t** value
)
1641 enum TIFFReadDirEntryErr err
;
1645 switch (direntry
->tdir_type
)
1657 return(TIFFReadDirEntryErrType
);
1659 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,4,&origdata
);
1660 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1665 switch (direntry
->tdir_type
)
1668 *value
=(uint32_t*)origdata
;
1669 if (tif
->tif_flags
&TIFF_SWAB
)
1670 TIFFSwabArrayOfLong(*value
,count
);
1671 return(TIFFReadDirEntryErrOk
);
1676 m
=(int32_t*)origdata
;
1677 for (n
=0; n
<count
; n
++)
1679 if (tif
->tif_flags
&TIFF_SWAB
)
1680 TIFFSwabLong((uint32_t*)m
);
1681 err
=TIFFReadDirEntryCheckRangeLongSlong(*m
);
1682 if (err
!=TIFFReadDirEntryErrOk
)
1684 _TIFFfree(origdata
);
1689 *value
=(uint32_t*)origdata
;
1690 return(TIFFReadDirEntryErrOk
);
1693 data
=(uint32_t*)_TIFFmalloc(count
* 4);
1696 _TIFFfree(origdata
);
1697 return(TIFFReadDirEntryErrAlloc
);
1699 switch (direntry
->tdir_type
)
1706 ma
=(uint8_t*)origdata
;
1708 for (n
=0; n
<count
; n
++)
1709 *mb
++=(uint32_t)(*ma
++);
1717 ma
=(int8_t*)origdata
;
1719 for (n
=0; n
<count
; n
++)
1721 err
=TIFFReadDirEntryCheckRangeLongSbyte(*ma
);
1722 if (err
!=TIFFReadDirEntryErrOk
)
1724 *mb
++=(uint32_t)(*ma
++);
1733 ma
=(uint16_t*)origdata
;
1735 for (n
=0; n
<count
; n
++)
1737 if (tif
->tif_flags
&TIFF_SWAB
)
1739 *mb
++=(uint32_t)(*ma
++);
1748 ma
=(int16_t*)origdata
;
1750 for (n
=0; n
<count
; n
++)
1752 if (tif
->tif_flags
&TIFF_SWAB
)
1753 TIFFSwabShort((uint16_t*)ma
);
1754 err
=TIFFReadDirEntryCheckRangeLongSshort(*ma
);
1755 if (err
!=TIFFReadDirEntryErrOk
)
1757 *mb
++=(uint32_t)(*ma
++);
1766 ma
=(uint64_t*)origdata
;
1768 for (n
=0; n
<count
; n
++)
1770 if (tif
->tif_flags
&TIFF_SWAB
)
1772 err
=TIFFReadDirEntryCheckRangeLongLong8(*ma
);
1773 if (err
!=TIFFReadDirEntryErrOk
)
1775 *mb
++=(uint32_t)(*ma
++);
1784 ma
=(int64_t*)origdata
;
1786 for (n
=0; n
<count
; n
++)
1788 if (tif
->tif_flags
&TIFF_SWAB
)
1789 TIFFSwabLong8((uint64_t*)ma
);
1790 err
=TIFFReadDirEntryCheckRangeLongSlong8(*ma
);
1791 if (err
!=TIFFReadDirEntryErrOk
)
1793 *mb
++=(uint32_t)(*ma
++);
1798 _TIFFfree(origdata
);
1799 if (err
!=TIFFReadDirEntryErrOk
)
1805 return(TIFFReadDirEntryErrOk
);
1808 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int32_t** value
)
1810 enum TIFFReadDirEntryErr err
;
1814 switch (direntry
->tdir_type
)
1826 return(TIFFReadDirEntryErrType
);
1828 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,4,&origdata
);
1829 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1834 switch (direntry
->tdir_type
)
1840 m
=(uint32_t*)origdata
;
1841 for (n
=0; n
<count
; n
++)
1843 if (tif
->tif_flags
&TIFF_SWAB
)
1844 TIFFSwabLong((uint32_t*)m
);
1845 err
=TIFFReadDirEntryCheckRangeSlongLong(*m
);
1846 if (err
!=TIFFReadDirEntryErrOk
)
1848 _TIFFfree(origdata
);
1853 *value
=(int32_t*)origdata
;
1854 return(TIFFReadDirEntryErrOk
);
1857 *value
=(int32_t*)origdata
;
1858 if (tif
->tif_flags
&TIFF_SWAB
)
1859 TIFFSwabArrayOfLong((uint32_t*)(*value
), count
);
1860 return(TIFFReadDirEntryErrOk
);
1862 data
=(int32_t*)_TIFFmalloc(count
* 4);
1865 _TIFFfree(origdata
);
1866 return(TIFFReadDirEntryErrAlloc
);
1868 switch (direntry
->tdir_type
)
1875 ma
=(uint8_t*)origdata
;
1877 for (n
=0; n
<count
; n
++)
1878 *mb
++=(int32_t)(*ma
++);
1886 ma
=(int8_t*)origdata
;
1888 for (n
=0; n
<count
; n
++)
1889 *mb
++=(int32_t)(*ma
++);
1897 ma
=(uint16_t*)origdata
;
1899 for (n
=0; n
<count
; n
++)
1901 if (tif
->tif_flags
&TIFF_SWAB
)
1903 *mb
++=(int32_t)(*ma
++);
1912 ma
=(int16_t*)origdata
;
1914 for (n
=0; n
<count
; n
++)
1916 if (tif
->tif_flags
&TIFF_SWAB
)
1917 TIFFSwabShort((uint16_t*)ma
);
1918 *mb
++=(int32_t)(*ma
++);
1927 ma
=(uint64_t*)origdata
;
1929 for (n
=0; n
<count
; n
++)
1931 if (tif
->tif_flags
&TIFF_SWAB
)
1933 err
=TIFFReadDirEntryCheckRangeSlongLong8(*ma
);
1934 if (err
!=TIFFReadDirEntryErrOk
)
1936 *mb
++=(int32_t)(*ma
++);
1945 ma
=(int64_t*)origdata
;
1947 for (n
=0; n
<count
; n
++)
1949 if (tif
->tif_flags
&TIFF_SWAB
)
1950 TIFFSwabLong8((uint64_t*)ma
);
1951 err
=TIFFReadDirEntryCheckRangeSlongSlong8(*ma
);
1952 if (err
!=TIFFReadDirEntryErrOk
)
1954 *mb
++=(int32_t)(*ma
++);
1959 _TIFFfree(origdata
);
1960 if (err
!=TIFFReadDirEntryErrOk
)
1966 return(TIFFReadDirEntryErrOk
);
1969 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8ArrayWithLimit(
1970 TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t** value
, uint64_t maxcount
)
1972 enum TIFFReadDirEntryErr err
;
1976 switch (direntry
->tdir_type
)
1988 return(TIFFReadDirEntryErrType
);
1990 err
=TIFFReadDirEntryArrayWithLimit(tif
,direntry
,&count
,8,&origdata
,maxcount
);
1991 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1996 switch (direntry
->tdir_type
)
1999 *value
=(uint64_t*)origdata
;
2000 if (tif
->tif_flags
&TIFF_SWAB
)
2001 TIFFSwabArrayOfLong8(*value
,count
);
2002 return(TIFFReadDirEntryErrOk
);
2007 m
=(int64_t*)origdata
;
2008 for (n
=0; n
<count
; n
++)
2010 if (tif
->tif_flags
&TIFF_SWAB
)
2011 TIFFSwabLong8((uint64_t*)m
);
2012 err
=TIFFReadDirEntryCheckRangeLong8Slong8(*m
);
2013 if (err
!=TIFFReadDirEntryErrOk
)
2015 _TIFFfree(origdata
);
2020 *value
=(uint64_t*)origdata
;
2021 return(TIFFReadDirEntryErrOk
);
2024 data
=(uint64_t*)_TIFFmalloc(count
* 8);
2027 _TIFFfree(origdata
);
2028 return(TIFFReadDirEntryErrAlloc
);
2030 switch (direntry
->tdir_type
)
2037 ma
=(uint8_t*)origdata
;
2039 for (n
=0; n
<count
; n
++)
2040 *mb
++=(uint64_t)(*ma
++);
2048 ma
=(int8_t*)origdata
;
2050 for (n
=0; n
<count
; n
++)
2052 err
=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma
);
2053 if (err
!=TIFFReadDirEntryErrOk
)
2055 *mb
++=(uint64_t)(*ma
++);
2064 ma
=(uint16_t*)origdata
;
2066 for (n
=0; n
<count
; n
++)
2068 if (tif
->tif_flags
&TIFF_SWAB
)
2070 *mb
++=(uint64_t)(*ma
++);
2079 ma
=(int16_t*)origdata
;
2081 for (n
=0; n
<count
; n
++)
2083 if (tif
->tif_flags
&TIFF_SWAB
)
2084 TIFFSwabShort((uint16_t*)ma
);
2085 err
=TIFFReadDirEntryCheckRangeLong8Sshort(*ma
);
2086 if (err
!=TIFFReadDirEntryErrOk
)
2088 *mb
++=(uint64_t)(*ma
++);
2097 ma
=(uint32_t*)origdata
;
2099 for (n
=0; n
<count
; n
++)
2101 if (tif
->tif_flags
&TIFF_SWAB
)
2103 *mb
++=(uint64_t)(*ma
++);
2112 ma
=(int32_t*)origdata
;
2114 for (n
=0; n
<count
; n
++)
2116 if (tif
->tif_flags
&TIFF_SWAB
)
2117 TIFFSwabLong((uint32_t*)ma
);
2118 err
=TIFFReadDirEntryCheckRangeLong8Slong(*ma
);
2119 if (err
!=TIFFReadDirEntryErrOk
)
2121 *mb
++=(uint64_t)(*ma
++);
2126 _TIFFfree(origdata
);
2127 if (err
!=TIFFReadDirEntryErrOk
)
2133 return(TIFFReadDirEntryErrOk
);
2136 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t** value
)
2138 return TIFFReadDirEntryLong8ArrayWithLimit(tif
, direntry
, value
, ~((uint64_t)0));
2141 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, int64_t** value
)
2143 enum TIFFReadDirEntryErr err
;
2147 switch (direntry
->tdir_type
)
2159 return(TIFFReadDirEntryErrType
);
2161 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
2162 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2167 switch (direntry
->tdir_type
)
2173 m
=(uint64_t*)origdata
;
2174 for (n
=0; n
<count
; n
++)
2176 if (tif
->tif_flags
&TIFF_SWAB
)
2178 err
=TIFFReadDirEntryCheckRangeSlong8Long8(*m
);
2179 if (err
!=TIFFReadDirEntryErrOk
)
2181 _TIFFfree(origdata
);
2186 *value
=(int64_t*)origdata
;
2187 return(TIFFReadDirEntryErrOk
);
2190 *value
=(int64_t*)origdata
;
2191 if (tif
->tif_flags
&TIFF_SWAB
)
2192 TIFFSwabArrayOfLong8((uint64_t*)(*value
), count
);
2193 return(TIFFReadDirEntryErrOk
);
2195 data
=(int64_t*)_TIFFmalloc(count
* 8);
2198 _TIFFfree(origdata
);
2199 return(TIFFReadDirEntryErrAlloc
);
2201 switch (direntry
->tdir_type
)
2208 ma
=(uint8_t*)origdata
;
2210 for (n
=0; n
<count
; n
++)
2211 *mb
++=(int64_t)(*ma
++);
2219 ma
=(int8_t*)origdata
;
2221 for (n
=0; n
<count
; n
++)
2222 *mb
++=(int64_t)(*ma
++);
2230 ma
=(uint16_t*)origdata
;
2232 for (n
=0; n
<count
; n
++)
2234 if (tif
->tif_flags
&TIFF_SWAB
)
2236 *mb
++=(int64_t)(*ma
++);
2245 ma
=(int16_t*)origdata
;
2247 for (n
=0; n
<count
; n
++)
2249 if (tif
->tif_flags
&TIFF_SWAB
)
2250 TIFFSwabShort((uint16_t*)ma
);
2251 *mb
++=(int64_t)(*ma
++);
2260 ma
=(uint32_t*)origdata
;
2262 for (n
=0; n
<count
; n
++)
2264 if (tif
->tif_flags
&TIFF_SWAB
)
2266 *mb
++=(int64_t)(*ma
++);
2275 ma
=(int32_t*)origdata
;
2277 for (n
=0; n
<count
; n
++)
2279 if (tif
->tif_flags
&TIFF_SWAB
)
2280 TIFFSwabLong((uint32_t*)ma
);
2281 *mb
++=(int64_t)(*ma
++);
2286 _TIFFfree(origdata
);
2288 return(TIFFReadDirEntryErrOk
);
2291 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloatArray(TIFF
* tif
, TIFFDirEntry
* direntry
, float** value
)
2293 enum TIFFReadDirEntryErr err
;
2297 switch (direntry
->tdir_type
)
2308 case TIFF_SRATIONAL
:
2313 return(TIFFReadDirEntryErrType
);
2315 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,4,&origdata
);
2316 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2321 switch (direntry
->tdir_type
)
2324 if (tif
->tif_flags
&TIFF_SWAB
)
2325 TIFFSwabArrayOfLong((uint32_t*)origdata
, count
);
2326 TIFFCvtIEEEDoubleToNative(tif
,count
,(float*)origdata
);
2327 *value
=(float*)origdata
;
2328 return(TIFFReadDirEntryErrOk
);
2330 data
=(float*)_TIFFmalloc(count
*sizeof(float));
2333 _TIFFfree(origdata
);
2334 return(TIFFReadDirEntryErrAlloc
);
2336 switch (direntry
->tdir_type
)
2343 ma
=(uint8_t*)origdata
;
2345 for (n
=0; n
<count
; n
++)
2346 *mb
++=(float)(*ma
++);
2354 ma
=(int8_t*)origdata
;
2356 for (n
=0; n
<count
; n
++)
2357 *mb
++=(float)(*ma
++);
2365 ma
=(uint16_t*)origdata
;
2367 for (n
=0; n
<count
; n
++)
2369 if (tif
->tif_flags
&TIFF_SWAB
)
2371 *mb
++=(float)(*ma
++);
2380 ma
=(int16_t*)origdata
;
2382 for (n
=0; n
<count
; n
++)
2384 if (tif
->tif_flags
&TIFF_SWAB
)
2385 TIFFSwabShort((uint16_t*)ma
);
2386 *mb
++=(float)(*ma
++);
2395 ma
=(uint32_t*)origdata
;
2397 for (n
=0; n
<count
; n
++)
2399 if (tif
->tif_flags
&TIFF_SWAB
)
2401 *mb
++=(float)(*ma
++);
2410 ma
=(int32_t*)origdata
;
2412 for (n
=0; n
<count
; n
++)
2414 if (tif
->tif_flags
&TIFF_SWAB
)
2415 TIFFSwabLong((uint32_t*)ma
);
2416 *mb
++=(float)(*ma
++);
2425 ma
=(uint64_t*)origdata
;
2427 for (n
=0; n
<count
; n
++)
2429 if (tif
->tif_flags
&TIFF_SWAB
)
2431 #if defined(__WIN32__) && (_MSC_VER < 1500)
2433 * XXX: MSVC 6.0 does not support
2434 * conversion of 64-bit integers into
2435 * floating point values.
2437 *mb
++ = _TIFFUInt64ToFloat(*ma
++);
2439 *mb
++ = (float)(*ma
++);
2449 ma
=(int64_t*)origdata
;
2451 for (n
=0; n
<count
; n
++)
2453 if (tif
->tif_flags
&TIFF_SWAB
)
2454 TIFFSwabLong8((uint64_t*)ma
);
2455 *mb
++=(float)(*ma
++);
2466 ma
=(uint32_t*)origdata
;
2468 for (n
=0; n
<count
; n
++)
2470 if (tif
->tif_flags
&TIFF_SWAB
)
2473 if (tif
->tif_flags
&TIFF_SWAB
)
2479 *mb
++=(float)maa
/(float)mab
;
2483 case TIFF_SRATIONAL
:
2490 ma
=(uint32_t*)origdata
;
2492 for (n
=0; n
<count
; n
++)
2494 if (tif
->tif_flags
&TIFF_SWAB
)
2498 if (tif
->tif_flags
&TIFF_SWAB
)
2504 *mb
++=(float)maa
/(float)mab
;
2513 if (tif
->tif_flags
&TIFF_SWAB
)
2514 TIFFSwabArrayOfLong8((uint64_t*)origdata
, count
);
2515 TIFFCvtIEEEDoubleToNative(tif
,count
,(double*)origdata
);
2516 ma
=(double*)origdata
;
2518 for (n
=0; n
<count
; n
++)
2523 else if( val
< -FLT_MAX
)
2530 _TIFFfree(origdata
);
2532 return(TIFFReadDirEntryErrOk
);
2535 static enum TIFFReadDirEntryErr
2536 TIFFReadDirEntryDoubleArray(TIFF
* tif
, TIFFDirEntry
* direntry
, double** value
)
2538 enum TIFFReadDirEntryErr err
;
2542 switch (direntry
->tdir_type
)
2553 case TIFF_SRATIONAL
:
2558 return(TIFFReadDirEntryErrType
);
2560 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
2561 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2566 switch (direntry
->tdir_type
)
2569 if (tif
->tif_flags
&TIFF_SWAB
)
2570 TIFFSwabArrayOfLong8((uint64_t*)origdata
, count
);
2571 TIFFCvtIEEEDoubleToNative(tif
,count
,(double*)origdata
);
2572 *value
=(double*)origdata
;
2573 return(TIFFReadDirEntryErrOk
);
2575 data
=(double*)_TIFFmalloc(count
*sizeof(double));
2578 _TIFFfree(origdata
);
2579 return(TIFFReadDirEntryErrAlloc
);
2581 switch (direntry
->tdir_type
)
2588 ma
=(uint8_t*)origdata
;
2590 for (n
=0; n
<count
; n
++)
2591 *mb
++=(double)(*ma
++);
2599 ma
=(int8_t*)origdata
;
2601 for (n
=0; n
<count
; n
++)
2602 *mb
++=(double)(*ma
++);
2610 ma
=(uint16_t*)origdata
;
2612 for (n
=0; n
<count
; n
++)
2614 if (tif
->tif_flags
&TIFF_SWAB
)
2616 *mb
++=(double)(*ma
++);
2625 ma
=(int16_t*)origdata
;
2627 for (n
=0; n
<count
; n
++)
2629 if (tif
->tif_flags
&TIFF_SWAB
)
2630 TIFFSwabShort((uint16_t*)ma
);
2631 *mb
++=(double)(*ma
++);
2640 ma
=(uint32_t*)origdata
;
2642 for (n
=0; n
<count
; n
++)
2644 if (tif
->tif_flags
&TIFF_SWAB
)
2646 *mb
++=(double)(*ma
++);
2655 ma
=(int32_t*)origdata
;
2657 for (n
=0; n
<count
; n
++)
2659 if (tif
->tif_flags
&TIFF_SWAB
)
2660 TIFFSwabLong((uint32_t*)ma
);
2661 *mb
++=(double)(*ma
++);
2670 ma
=(uint64_t*)origdata
;
2672 for (n
=0; n
<count
; n
++)
2674 if (tif
->tif_flags
&TIFF_SWAB
)
2676 #if defined(__WIN32__) && (_MSC_VER < 1500)
2678 * XXX: MSVC 6.0 does not support
2679 * conversion of 64-bit integers into
2680 * floating point values.
2682 *mb
++ = _TIFFUInt64ToDouble(*ma
++);
2684 *mb
++ = (double)(*ma
++);
2694 ma
=(int64_t*)origdata
;
2696 for (n
=0; n
<count
; n
++)
2698 if (tif
->tif_flags
&TIFF_SWAB
)
2699 TIFFSwabLong8((uint64_t*)ma
);
2700 *mb
++=(double)(*ma
++);
2711 ma
=(uint32_t*)origdata
;
2713 for (n
=0; n
<count
; n
++)
2715 if (tif
->tif_flags
&TIFF_SWAB
)
2718 if (tif
->tif_flags
&TIFF_SWAB
)
2724 *mb
++=(double)maa
/(double)mab
;
2728 case TIFF_SRATIONAL
:
2735 ma
=(uint32_t*)origdata
;
2737 for (n
=0; n
<count
; n
++)
2739 if (tif
->tif_flags
&TIFF_SWAB
)
2743 if (tif
->tif_flags
&TIFF_SWAB
)
2749 *mb
++=(double)maa
/(double)mab
;
2758 if (tif
->tif_flags
&TIFF_SWAB
)
2759 TIFFSwabArrayOfLong((uint32_t*)origdata
, count
);
2760 TIFFCvtIEEEFloatToNative(tif
,count
,(float*)origdata
);
2761 ma
=(float*)origdata
;
2763 for (n
=0; n
<count
; n
++)
2764 *mb
++=(double)(*ma
++);
2768 _TIFFfree(origdata
);
2770 return(TIFFReadDirEntryErrOk
);
2773 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t** value
)
2775 enum TIFFReadDirEntryErr err
;
2779 switch (direntry
->tdir_type
)
2787 return(TIFFReadDirEntryErrType
);
2789 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
2790 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2795 switch (direntry
->tdir_type
)
2799 *value
=(uint64_t*)origdata
;
2800 if (tif
->tif_flags
&TIFF_SWAB
)
2801 TIFFSwabArrayOfLong8(*value
,count
);
2802 return(TIFFReadDirEntryErrOk
);
2804 data
=(uint64_t*)_TIFFmalloc(count
* 8);
2807 _TIFFfree(origdata
);
2808 return(TIFFReadDirEntryErrAlloc
);
2810 switch (direntry
->tdir_type
)
2818 ma
=(uint32_t*)origdata
;
2820 for (n
=0; n
<count
; n
++)
2822 if (tif
->tif_flags
&TIFF_SWAB
)
2824 *mb
++=(uint64_t)(*ma
++);
2829 _TIFFfree(origdata
);
2831 return(TIFFReadDirEntryErrOk
);
2834 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t* value
)
2836 enum TIFFReadDirEntryErr err
;
2840 if (direntry
->tdir_count
<(uint64_t)tif
->tif_dir
.td_samplesperpixel
)
2841 return(TIFFReadDirEntryErrCount
);
2842 err
=TIFFReadDirEntryShortArray(tif
,direntry
,&m
);
2843 if (err
!=TIFFReadDirEntryErrOk
|| m
== NULL
)
2846 nb
=tif
->tif_dir
.td_samplesperpixel
;
2853 err
=TIFFReadDirEntryErrPsdif
;
2863 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
2865 enum TIFFReadDirEntryErr err
;
2869 if (direntry
->tdir_count
<(uint64_t)tif
->tif_dir
.td_samplesperpixel
)
2870 return(TIFFReadDirEntryErrCount
);
2871 err
=TIFFReadDirEntryDoubleArray(tif
,direntry
,&m
);
2872 if (err
!=TIFFReadDirEntryErrOk
)
2875 nb
=tif
->tif_dir
.td_samplesperpixel
;
2882 err
=TIFFReadDirEntryErrPsdif
;
2892 static void TIFFReadDirEntryCheckedByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8_t* value
)
2895 *value
=*(uint8_t*)(&direntry
->tdir_offset
);
2898 static void TIFFReadDirEntryCheckedSbyte(TIFF
* tif
, TIFFDirEntry
* direntry
, int8_t* value
)
2901 *value
=*(int8_t*)(&direntry
->tdir_offset
);
2904 static void TIFFReadDirEntryCheckedShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16_t* value
)
2906 *value
= direntry
->tdir_offset
.toff_short
;
2907 /* *value=*(uint16_t*)(&direntry->tdir_offset); */
2908 if (tif
->tif_flags
&TIFF_SWAB
)
2909 TIFFSwabShort(value
);
2912 static void TIFFReadDirEntryCheckedSshort(TIFF
* tif
, TIFFDirEntry
* direntry
, int16_t* value
)
2914 *value
=*(int16_t*)(&direntry
->tdir_offset
);
2915 if (tif
->tif_flags
&TIFF_SWAB
)
2916 TIFFSwabShort((uint16_t*)value
);
2919 static void TIFFReadDirEntryCheckedLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32_t* value
)
2921 *value
=*(uint32_t*)(&direntry
->tdir_offset
);
2922 if (tif
->tif_flags
&TIFF_SWAB
)
2923 TIFFSwabLong(value
);
2926 static void TIFFReadDirEntryCheckedSlong(TIFF
* tif
, TIFFDirEntry
* direntry
, int32_t* value
)
2928 *value
=*(int32_t*)(&direntry
->tdir_offset
);
2929 if (tif
->tif_flags
&TIFF_SWAB
)
2930 TIFFSwabLong((uint32_t*)value
);
2933 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64_t* value
)
2935 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2937 enum TIFFReadDirEntryErr err
;
2938 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
2939 if (tif
->tif_flags
&TIFF_SWAB
)
2940 TIFFSwabLong(&offset
);
2941 err
=TIFFReadDirEntryData(tif
,offset
,8,value
);
2942 if (err
!=TIFFReadDirEntryErrOk
)
2946 *value
= direntry
->tdir_offset
.toff_long8
;
2947 if (tif
->tif_flags
&TIFF_SWAB
)
2948 TIFFSwabLong8(value
);
2949 return(TIFFReadDirEntryErrOk
);
2952 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSlong8(TIFF
* tif
, TIFFDirEntry
* direntry
, int64_t* value
)
2954 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2956 enum TIFFReadDirEntryErr err
;
2957 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
2958 if (tif
->tif_flags
&TIFF_SWAB
)
2959 TIFFSwabLong(&offset
);
2960 err
=TIFFReadDirEntryData(tif
,offset
,8,value
);
2961 if (err
!=TIFFReadDirEntryErrOk
)
2965 *value
=*(int64_t*)(&direntry
->tdir_offset
);
2966 if (tif
->tif_flags
&TIFF_SWAB
)
2967 TIFFSwabLong8((uint64_t*)value
);
2968 return(TIFFReadDirEntryErrOk
);
2971 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedRational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
2975 assert(sizeof(double)==8);
2976 assert(sizeof(uint64_t) == 8);
2977 assert(sizeof(uint32_t) == 4);
2978 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2980 enum TIFFReadDirEntryErr err
;
2981 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
2982 if (tif
->tif_flags
&TIFF_SWAB
)
2983 TIFFSwabLong(&offset
);
2984 err
=TIFFReadDirEntryData(tif
,offset
,8,m
.i
);
2985 if (err
!=TIFFReadDirEntryErrOk
)
2989 m
.l
= direntry
->tdir_offset
.toff_long8
;
2990 if (tif
->tif_flags
&TIFF_SWAB
)
2991 TIFFSwabArrayOfLong(m
.i
,2);
2992 /* Not completely sure what we should do when m.i[1]==0, but some */
2993 /* sanitizers do not like division by 0.0: */
2994 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
2995 if (m
.i
[0]==0 || m
.i
[1]==0)
2998 *value
=(double)m
.i
[0]/(double)m
.i
[1];
2999 return(TIFFReadDirEntryErrOk
);
3002 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSrational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
3005 assert(sizeof(double)==8);
3006 assert(sizeof(uint64_t) == 8);
3007 assert(sizeof(int32_t) == 4);
3008 assert(sizeof(uint32_t) == 4);
3009 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
3011 enum TIFFReadDirEntryErr err
;
3012 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3013 if (tif
->tif_flags
&TIFF_SWAB
)
3014 TIFFSwabLong(&offset
);
3015 err
=TIFFReadDirEntryData(tif
,offset
,8,m
.i
);
3016 if (err
!=TIFFReadDirEntryErrOk
)
3020 m
.l
=direntry
->tdir_offset
.toff_long8
;
3021 if (tif
->tif_flags
&TIFF_SWAB
)
3022 TIFFSwabArrayOfLong(m
.i
,2);
3023 /* Not completely sure what we should do when m.i[1]==0, but some */
3024 /* sanitizers do not like division by 0.0: */
3025 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3026 if ((int32_t)m
.i
[0] == 0 || m
.i
[1] == 0)
3029 *value
= (double)((int32_t)m
.i
[0]) / (double)m
.i
[1];
3030 return(TIFFReadDirEntryErrOk
);
3033 static void TIFFReadDirEntryCheckedFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
)
3040 assert(sizeof(float)==4);
3041 assert(sizeof(uint32_t) == 4);
3042 assert(sizeof(float_union
)==4);
3043 float_union
.i
=*(uint32_t*)(&direntry
->tdir_offset
);
3044 *value
=float_union
.f
;
3045 if (tif
->tif_flags
&TIFF_SWAB
)
3046 TIFFSwabLong((uint32_t*)value
);
3049 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
3051 assert(sizeof(double)==8);
3052 assert(sizeof(uint64_t) == 8);
3053 assert(sizeof(UInt64Aligned_t
)==8);
3054 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
3056 enum TIFFReadDirEntryErr err
;
3057 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3058 if (tif
->tif_flags
&TIFF_SWAB
)
3059 TIFFSwabLong(&offset
);
3060 err
=TIFFReadDirEntryData(tif
,offset
,8,value
);
3061 if (err
!=TIFFReadDirEntryErrOk
)
3066 UInt64Aligned_t uint64_union
;
3067 uint64_union
.l
=direntry
->tdir_offset
.toff_long8
;
3068 *value
=uint64_union
.d
;
3070 if (tif
->tif_flags
&TIFF_SWAB
)
3071 TIFFSwabLong8((uint64_t*)value
);
3072 return(TIFFReadDirEntryErrOk
);
3075 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value
)
3078 return(TIFFReadDirEntryErrRange
);
3080 return(TIFFReadDirEntryErrOk
);
3083 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteShort(uint16_t value
)
3086 return(TIFFReadDirEntryErrRange
);
3088 return(TIFFReadDirEntryErrOk
);
3091 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSshort(int16_t value
)
3093 if ((value
<0)||(value
>0xFF))
3094 return(TIFFReadDirEntryErrRange
);
3096 return(TIFFReadDirEntryErrOk
);
3099 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong(uint32_t value
)
3102 return(TIFFReadDirEntryErrRange
);
3104 return(TIFFReadDirEntryErrOk
);
3107 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong(int32_t value
)
3109 if ((value
<0)||(value
>0xFF))
3110 return(TIFFReadDirEntryErrRange
);
3112 return(TIFFReadDirEntryErrOk
);
3115 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong8(uint64_t value
)
3118 return(TIFFReadDirEntryErrRange
);
3120 return(TIFFReadDirEntryErrOk
);
3123 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong8(int64_t value
)
3125 if ((value
<0)||(value
>0xFF))
3126 return(TIFFReadDirEntryErrRange
);
3128 return(TIFFReadDirEntryErrOk
);
3131 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value
)
3134 return(TIFFReadDirEntryErrRange
);
3136 return(TIFFReadDirEntryErrOk
);
3139 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value
)
3142 return(TIFFReadDirEntryErrRange
);
3144 return(TIFFReadDirEntryErrOk
);
3147 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value
)
3149 if ((value
<-0x80)||(value
>0x7F))
3150 return(TIFFReadDirEntryErrRange
);
3152 return(TIFFReadDirEntryErrOk
);
3155 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value
)
3158 return(TIFFReadDirEntryErrRange
);
3160 return(TIFFReadDirEntryErrOk
);
3163 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value
)
3165 if ((value
<-0x80)||(value
>0x7F))
3166 return(TIFFReadDirEntryErrRange
);
3168 return(TIFFReadDirEntryErrOk
);
3171 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value
)
3174 return(TIFFReadDirEntryErrRange
);
3176 return(TIFFReadDirEntryErrOk
);
3179 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value
)
3181 if ((value
<-0x80)||(value
>0x7F))
3182 return(TIFFReadDirEntryErrRange
);
3184 return(TIFFReadDirEntryErrOk
);
3187 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSbyte(int8_t value
)
3190 return(TIFFReadDirEntryErrRange
);
3192 return(TIFFReadDirEntryErrOk
);
3195 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSshort(int16_t value
)
3198 return(TIFFReadDirEntryErrRange
);
3200 return(TIFFReadDirEntryErrOk
);
3203 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong(uint32_t value
)
3206 return(TIFFReadDirEntryErrRange
);
3208 return(TIFFReadDirEntryErrOk
);
3211 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong(int32_t value
)
3213 if ((value
<0)||(value
>0xFFFF))
3214 return(TIFFReadDirEntryErrRange
);
3216 return(TIFFReadDirEntryErrOk
);
3219 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong8(uint64_t value
)
3222 return(TIFFReadDirEntryErrRange
);
3224 return(TIFFReadDirEntryErrOk
);
3227 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong8(int64_t value
)
3229 if ((value
<0)||(value
>0xFFFF))
3230 return(TIFFReadDirEntryErrRange
);
3232 return(TIFFReadDirEntryErrOk
);
3235 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortShort(uint16_t value
)
3238 return(TIFFReadDirEntryErrRange
);
3240 return(TIFFReadDirEntryErrOk
);
3243 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong(uint32_t value
)
3246 return(TIFFReadDirEntryErrRange
);
3248 return(TIFFReadDirEntryErrOk
);
3251 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong(int32_t value
)
3253 if ((value
<-0x8000)||(value
>0x7FFF))
3254 return(TIFFReadDirEntryErrRange
);
3256 return(TIFFReadDirEntryErrOk
);
3259 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value
)
3262 return(TIFFReadDirEntryErrRange
);
3264 return(TIFFReadDirEntryErrOk
);
3267 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value
)
3269 if ((value
<-0x8000)||(value
>0x7FFF))
3270 return(TIFFReadDirEntryErrRange
);
3272 return(TIFFReadDirEntryErrOk
);
3275 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSbyte(int8_t value
)
3278 return(TIFFReadDirEntryErrRange
);
3280 return(TIFFReadDirEntryErrOk
);
3283 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSshort(int16_t value
)
3286 return(TIFFReadDirEntryErrRange
);
3288 return(TIFFReadDirEntryErrOk
);
3291 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSlong(int32_t value
)
3294 return(TIFFReadDirEntryErrRange
);
3296 return(TIFFReadDirEntryErrOk
);
3299 static enum TIFFReadDirEntryErr
3300 TIFFReadDirEntryCheckRangeLongLong8(uint64_t value
)
3302 if (value
> UINT32_MAX
)
3303 return(TIFFReadDirEntryErrRange
);
3305 return(TIFFReadDirEntryErrOk
);
3308 static enum TIFFReadDirEntryErr
3309 TIFFReadDirEntryCheckRangeLongSlong8(int64_t value
)
3311 if ((value
< 0) || (value
> (int64_t) UINT32_MAX
))
3312 return(TIFFReadDirEntryErrRange
);
3314 return(TIFFReadDirEntryErrOk
);
3317 static enum TIFFReadDirEntryErr
3318 TIFFReadDirEntryCheckRangeSlongLong(uint32_t value
)
3320 if (value
> 0x7FFFFFFFUL
)
3321 return(TIFFReadDirEntryErrRange
);
3323 return(TIFFReadDirEntryErrOk
);
3326 /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3327 static enum TIFFReadDirEntryErr
3328 TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value
)
3330 if (value
> 0x7FFFFFFF)
3331 return(TIFFReadDirEntryErrRange
);
3333 return(TIFFReadDirEntryErrOk
);
3336 /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3337 static enum TIFFReadDirEntryErr
3338 TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value
)
3340 if ((value
< 0-((int64_t) 0x7FFFFFFF + 1)) || (value
> 0x7FFFFFFF))
3341 return(TIFFReadDirEntryErrRange
);
3343 return(TIFFReadDirEntryErrOk
);
3346 static enum TIFFReadDirEntryErr
3347 TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value
)
3350 return(TIFFReadDirEntryErrRange
);
3352 return(TIFFReadDirEntryErrOk
);
3355 static enum TIFFReadDirEntryErr
3356 TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value
)
3359 return(TIFFReadDirEntryErrRange
);
3361 return(TIFFReadDirEntryErrOk
);
3364 static enum TIFFReadDirEntryErr
3365 TIFFReadDirEntryCheckRangeLong8Slong(int32_t value
)
3368 return(TIFFReadDirEntryErrRange
);
3370 return(TIFFReadDirEntryErrOk
);
3373 static enum TIFFReadDirEntryErr
3374 TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value
)
3377 return(TIFFReadDirEntryErrRange
);
3379 return(TIFFReadDirEntryErrOk
);
3382 static enum TIFFReadDirEntryErr
3383 TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value
)
3385 if (value
> INT64_MAX
)
3386 return(TIFFReadDirEntryErrRange
);
3388 return(TIFFReadDirEntryErrOk
);
3391 static enum TIFFReadDirEntryErr
3392 TIFFReadDirEntryData(TIFF
* tif
, uint64_t offset
, tmsize_t size
, void* dest
)
3395 if (!isMapped(tif
)) {
3396 if (!SeekOK(tif
,offset
))
3397 return(TIFFReadDirEntryErrIo
);
3398 if (!ReadOK(tif
,dest
,size
))
3399 return(TIFFReadDirEntryErrIo
);
3403 if((uint64_t)ma
!= offset
||
3404 ma
> (~(size_t)0) - (size_t)size
)
3406 return TIFFReadDirEntryErrIo
;
3409 if (mb
> (uint64_t)tif
->tif_size
)
3410 return(TIFFReadDirEntryErrIo
);
3411 _TIFFmemcpy(dest
,tif
->tif_base
+ma
,size
);
3413 return(TIFFReadDirEntryErrOk
);
3416 static void TIFFReadDirEntryOutputErr(TIFF
* tif
, enum TIFFReadDirEntryErr err
, const char* module
, const char* tagname
, int recover
)
3420 case TIFFReadDirEntryErrCount
:
3421 TIFFErrorExt(tif
->tif_clientdata
, module
,
3422 "Incorrect count for \"%s\"",
3425 case TIFFReadDirEntryErrType
:
3426 TIFFErrorExt(tif
->tif_clientdata
, module
,
3427 "Incompatible type for \"%s\"",
3430 case TIFFReadDirEntryErrIo
:
3431 TIFFErrorExt(tif
->tif_clientdata
, module
,
3432 "IO error during reading of \"%s\"",
3435 case TIFFReadDirEntryErrRange
:
3436 TIFFErrorExt(tif
->tif_clientdata
, module
,
3437 "Incorrect value for \"%s\"",
3440 case TIFFReadDirEntryErrPsdif
:
3441 TIFFErrorExt(tif
->tif_clientdata
, module
,
3442 "Cannot handle different values per sample for \"%s\"",
3445 case TIFFReadDirEntryErrSizesan
:
3446 TIFFErrorExt(tif
->tif_clientdata
, module
,
3447 "Sanity check on size of \"%s\" value failed",
3450 case TIFFReadDirEntryErrAlloc
:
3451 TIFFErrorExt(tif
->tif_clientdata
, module
,
3452 "Out of memory reading of \"%s\"",
3456 assert(0); /* we should never get here */
3461 case TIFFReadDirEntryErrCount
:
3462 TIFFWarningExt(tif
->tif_clientdata
, module
,
3463 "Incorrect count for \"%s\"; tag ignored",
3466 case TIFFReadDirEntryErrType
:
3467 TIFFWarningExt(tif
->tif_clientdata
, module
,
3468 "Incompatible type for \"%s\"; tag ignored",
3471 case TIFFReadDirEntryErrIo
:
3472 TIFFWarningExt(tif
->tif_clientdata
, module
,
3473 "IO error during reading of \"%s\"; tag ignored",
3476 case TIFFReadDirEntryErrRange
:
3477 TIFFWarningExt(tif
->tif_clientdata
, module
,
3478 "Incorrect value for \"%s\"; tag ignored",
3481 case TIFFReadDirEntryErrPsdif
:
3482 TIFFWarningExt(tif
->tif_clientdata
, module
,
3483 "Cannot handle different values per sample for \"%s\"; tag ignored",
3486 case TIFFReadDirEntryErrSizesan
:
3487 TIFFWarningExt(tif
->tif_clientdata
, module
,
3488 "Sanity check on size of \"%s\" value failed; tag ignored",
3491 case TIFFReadDirEntryErrAlloc
:
3492 TIFFWarningExt(tif
->tif_clientdata
, module
,
3493 "Out of memory reading of \"%s\"; tag ignored",
3497 assert(0); /* we should never get here */
3504 * Return the maximum number of color channels specified for a given photometric
3505 * type. 0 is returned if photometric type isn't supported or no default value
3506 * is defined by the specification.
3508 static int _TIFFGetMaxColorChannels(uint16_t photometric
)
3510 switch (photometric
) {
3511 case PHOTOMETRIC_PALETTE
:
3512 case PHOTOMETRIC_MINISWHITE
:
3513 case PHOTOMETRIC_MINISBLACK
:
3515 case PHOTOMETRIC_YCBCR
:
3516 case PHOTOMETRIC_RGB
:
3517 case PHOTOMETRIC_CIELAB
:
3518 case PHOTOMETRIC_LOGLUV
:
3519 case PHOTOMETRIC_ITULAB
:
3520 case PHOTOMETRIC_ICCLAB
:
3522 case PHOTOMETRIC_SEPARATED
:
3523 case PHOTOMETRIC_MASK
:
3525 case PHOTOMETRIC_LOGL
:
3526 case PHOTOMETRIC_CFA
:
3532 static int ByteCountLooksBad(TIFF
* tif
)
3535 * Assume we have wrong StripByteCount value (in case
3536 * of single strip) in following cases:
3537 * - it is equal to zero along with StripOffset;
3538 * - it is larger than file itself (in case of uncompressed
3540 * - it is smaller than the size of the bytes per row
3541 * multiplied on the number of rows. The last case should
3542 * not be checked in the case of writing new image,
3543 * because we may do not know the exact strip size
3544 * until the whole image will be written and directory
3547 uint64_t bytecount
= TIFFGetStrileByteCount(tif
, 0);
3548 uint64_t offset
= TIFFGetStrileOffset(tif
, 0);
3555 if ( tif
->tif_dir
.td_compression
!= COMPRESSION_NONE
)
3557 filesize
= TIFFGetFileSize(tif
);
3558 if( offset
<= filesize
&& bytecount
> filesize
- offset
)
3560 if( tif
->tif_mode
== O_RDONLY
)
3562 uint64_t scanlinesize
= TIFFScanlineSize64(tif
);
3563 if( tif
->tif_dir
.td_imagelength
> 0 &&
3564 scanlinesize
> UINT64_MAX
/ tif
->tif_dir
.td_imagelength
)
3568 if( bytecount
< scanlinesize
* tif
->tif_dir
.td_imagelength
)
3576 * Read the next TIFF directory from a file and convert it to the internal
3577 * format. We read directories sequentially.
3580 TIFFReadDirectory(TIFF
* tif
)
3582 static const char module
[] = "TIFFReadDirectory";
3587 const TIFFField
* fip
;
3588 uint32_t fii
=FAILED_FII
;
3590 int bitspersample_read
= FALSE
;
3593 tif
->tif_diroff
=tif
->tif_nextdiroff
;
3594 if (!TIFFCheckDirOffset(tif
,tif
->tif_nextdiroff
))
3595 return 0; /* last offset or bad offset (IFD looping) */
3596 (*tif
->tif_cleanup
)(tif
); /* cleanup any previous compression state */
3598 nextdiroff
= tif
->tif_nextdiroff
;
3599 dircount
=TIFFFetchDirectory(tif
,nextdiroff
,&dir
,&tif
->tif_nextdiroff
);
3602 TIFFErrorExt(tif
->tif_clientdata
,module
,
3603 "Failed to read directory at offset %" PRIu64
, nextdiroff
);
3606 TIFFReadDirectoryCheckOrder(tif
,dir
,dircount
);
3609 * Mark duplicates of any tag to be ignored (bugzilla 1994)
3610 * to avoid certain pathological problems.
3615 for (ma
=dir
, mb
=0; mb
<dircount
; ma
++, mb
++)
3619 for (na
=ma
+1, nb
=mb
+1; nb
<dircount
; na
++, nb
++)
3621 if (ma
->tdir_tag
== na
->tdir_tag
) {
3622 na
->tdir_ignore
= TRUE
;
3628 tif
->tif_flags
&= ~TIFF_BEENWRITING
; /* reset before new dir */
3629 tif
->tif_flags
&= ~TIFF_BUF4WRITE
; /* reset before new dir */
3630 tif
->tif_flags
&= ~TIFF_CHOPPEDUPARRAYS
;
3632 /* free any old stuff and reinit */
3633 TIFFFreeDirectory(tif
);
3634 TIFFDefaultDirectory(tif
);
3636 * Electronic Arts writes gray-scale TIFF files
3637 * without a PlanarConfiguration directory entry.
3638 * Thus we setup a default value here, even though
3639 * the TIFF spec says there is no default value.
3641 TIFFSetField(tif
,TIFFTAG_PLANARCONFIG
,PLANARCONFIG_CONTIG
);
3643 * Setup default value and then make a pass over
3644 * the fields to check type and tag information,
3645 * and to extract info required to size data
3646 * structures. A second pass is made afterwards
3647 * to read in everything not taken in the first pass.
3648 * But we must process the Compression tag first
3649 * in order to merge in codec-private tag definitions (otherwise
3650 * we may get complaints about unknown tags). However, the
3651 * Compression tag may be dependent on the SamplesPerPixel
3652 * tag value because older TIFF specs permitted Compression
3653 * to be written as a SamplesPerPixel-count tag entry.
3654 * Thus if we don't first figure out the correct SamplesPerPixel
3655 * tag value then we may end up ignoring the Compression tag
3656 * value because it has an incorrect count value (if the
3657 * true value of SamplesPerPixel is not 1).
3659 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,TIFFTAG_SAMPLESPERPIXEL
);
3662 if (!TIFFFetchNormalTag(tif
,dp
,0))
3664 dp
->tdir_ignore
= TRUE
;
3666 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,TIFFTAG_COMPRESSION
);
3670 * The 5.0 spec says the Compression tag has one value, while
3671 * earlier specs say it has one value per sample. Because of
3672 * this, we accept the tag if one value is supplied with either
3676 enum TIFFReadDirEntryErr err
;
3677 err
=TIFFReadDirEntryShort(tif
,dp
,&value
);
3678 if (err
==TIFFReadDirEntryErrCount
)
3679 err
=TIFFReadDirEntryPersampleShort(tif
,dp
,&value
);
3680 if (err
!=TIFFReadDirEntryErrOk
)
3682 TIFFReadDirEntryOutputErr(tif
,err
,module
,"Compression",0);
3685 if (!TIFFSetField(tif
,TIFFTAG_COMPRESSION
,value
))
3687 dp
->tdir_ignore
= TRUE
;
3691 if (!TIFFSetField(tif
,TIFFTAG_COMPRESSION
,COMPRESSION_NONE
))
3695 * First real pass over the directory.
3697 for (di
=0, dp
=dir
; di
<dircount
; di
++, dp
++)
3699 if (!dp
->tdir_ignore
)
3701 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
3702 if (fii
== FAILED_FII
)
3704 TIFFWarningExt(tif
->tif_clientdata
, module
,
3705 "Unknown field with tag %"PRIu16
" (0x%"PRIx16
") encountered",
3706 dp
->tdir_tag
,dp
->tdir_tag
);
3707 /* the following knowingly leaks the
3708 anonymous field structure */
3709 if (!_TIFFMergeFields(tif
,
3710 _TIFFCreateAnonField(tif
,
3712 (TIFFDataType
) dp
->tdir_type
),
3714 TIFFWarningExt(tif
->tif_clientdata
,
3716 "Registering anonymous field with tag %"PRIu16
" (0x%"PRIx16
") failed",
3719 dp
->tdir_ignore
= TRUE
;
3721 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
3722 assert(fii
!= FAILED_FII
);
3726 if (!dp
->tdir_ignore
)
3728 fip
=tif
->tif_fields
[fii
];
3729 if (fip
->field_bit
==FIELD_IGNORE
)
3730 dp
->tdir_ignore
= TRUE
;
3733 switch (dp
->tdir_tag
)
3735 case TIFFTAG_STRIPOFFSETS
:
3736 case TIFFTAG_STRIPBYTECOUNTS
:
3737 case TIFFTAG_TILEOFFSETS
:
3738 case TIFFTAG_TILEBYTECOUNTS
:
3739 TIFFSetFieldBit(tif
,fip
->field_bit
);
3741 case TIFFTAG_IMAGEWIDTH
:
3742 case TIFFTAG_IMAGELENGTH
:
3743 case TIFFTAG_IMAGEDEPTH
:
3744 case TIFFTAG_TILELENGTH
:
3745 case TIFFTAG_TILEWIDTH
:
3746 case TIFFTAG_TILEDEPTH
:
3747 case TIFFTAG_PLANARCONFIG
:
3748 case TIFFTAG_ROWSPERSTRIP
:
3749 case TIFFTAG_EXTRASAMPLES
:
3750 if (!TIFFFetchNormalTag(tif
,dp
,0))
3752 dp
->tdir_ignore
= TRUE
;
3755 if( !_TIFFCheckFieldIsValidForCodec(tif
, dp
->tdir_tag
) )
3756 dp
->tdir_ignore
= TRUE
;
3764 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3765 * c) strip offsets/bytecounts tag are both present and
3766 * d) both contain exactly one value, then we consistently find
3767 * that the buggy implementation of the buggy compression scheme
3768 * matches contig planarconfig best. So we 'fix-up' the tag here
3770 if ((tif
->tif_dir
.td_compression
==COMPRESSION_OJPEG
)&&
3771 (tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_SEPARATE
))
3773 if (!_TIFFFillStriles(tif
))
3775 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,TIFFTAG_STRIPOFFSETS
);
3776 if ((dp
!=0)&&(dp
->tdir_count
==1))
3778 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,
3779 TIFFTAG_STRIPBYTECOUNTS
);
3780 if ((dp
!=0)&&(dp
->tdir_count
==1))
3782 tif
->tif_dir
.td_planarconfig
=PLANARCONFIG_CONTIG
;
3783 TIFFWarningExt(tif
->tif_clientdata
,module
,
3784 "Planarconfig tag value assumed incorrect, "
3785 "assuming data is contig instead of chunky");
3790 * Allocate directory structure and setup defaults.
3792 if (!TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
3794 MissingRequired(tif
,"ImageLength");
3798 * Setup appropriate structures (by strip or by tile)
3800 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) {
3801 tif
->tif_dir
.td_nstrips
= TIFFNumberOfStrips(tif
);
3802 tif
->tif_dir
.td_tilewidth
= tif
->tif_dir
.td_imagewidth
;
3803 tif
->tif_dir
.td_tilelength
= tif
->tif_dir
.td_rowsperstrip
;
3804 tif
->tif_dir
.td_tiledepth
= tif
->tif_dir
.td_imagedepth
;
3805 tif
->tif_flags
&= ~TIFF_ISTILED
;
3807 tif
->tif_dir
.td_nstrips
= TIFFNumberOfTiles(tif
);
3808 tif
->tif_flags
|= TIFF_ISTILED
;
3810 if (!tif
->tif_dir
.td_nstrips
) {
3811 TIFFErrorExt(tif
->tif_clientdata
, module
,
3812 "Cannot handle zero number of %s",
3813 isTiled(tif
) ? "tiles" : "strips");
3816 tif
->tif_dir
.td_stripsperimage
= tif
->tif_dir
.td_nstrips
;
3817 if (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
)
3818 tif
->tif_dir
.td_stripsperimage
/= tif
->tif_dir
.td_samplesperpixel
;
3819 if (!TIFFFieldSet(tif
, FIELD_STRIPOFFSETS
)) {
3820 #ifdef OJPEG_SUPPORT
3821 if ((tif
->tif_dir
.td_compression
==COMPRESSION_OJPEG
) &&
3822 (isTiled(tif
)==0) &&
3823 (tif
->tif_dir
.td_nstrips
==1)) {
3826 * If a) compression is OJPEG, b) it's not a tiled TIFF,
3827 * and c) the number of strips is 1,
3828 * then we tolerate the absence of stripoffsets tag,
3829 * because, presumably, all required data is in the
3830 * JpegInterchangeFormat stream.
3832 TIFFSetFieldBit(tif
, FIELD_STRIPOFFSETS
);
3836 MissingRequired(tif
,
3837 isTiled(tif
) ? "TileOffsets" : "StripOffsets");
3842 * Second pass: extract other information.
3844 for (di
=0, dp
=dir
; di
<dircount
; di
++, dp
++)
3846 if (!dp
->tdir_ignore
) {
3847 switch (dp
->tdir_tag
)
3849 case TIFFTAG_MINSAMPLEVALUE
:
3850 case TIFFTAG_MAXSAMPLEVALUE
:
3851 case TIFFTAG_BITSPERSAMPLE
:
3852 case TIFFTAG_DATATYPE
:
3853 case TIFFTAG_SAMPLEFORMAT
:
3855 * The MinSampleValue, MaxSampleValue, BitsPerSample
3856 * DataType and SampleFormat tags are supposed to be
3857 * written as one value/sample, but some vendors
3858 * incorrectly write one value only -- so we accept
3859 * that as well (yuck). Other vendors write correct
3860 * value for NumberOfSamples, but incorrect one for
3861 * BitsPerSample and friends, and we will read this
3866 enum TIFFReadDirEntryErr err
;
3867 err
=TIFFReadDirEntryShort(tif
,dp
,&value
);
3868 if (err
==TIFFReadDirEntryErrCount
)
3869 err
=TIFFReadDirEntryPersampleShort(tif
,dp
,&value
);
3870 if (err
!=TIFFReadDirEntryErrOk
)
3872 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3873 TIFFReadDirEntryOutputErr(tif
,err
,module
,fip
? fip
->field_name
: "unknown tagname",0);
3876 if (!TIFFSetField(tif
,dp
->tdir_tag
,value
))
3878 if( dp
->tdir_tag
== TIFFTAG_BITSPERSAMPLE
)
3879 bitspersample_read
= TRUE
;
3882 case TIFFTAG_SMINSAMPLEVALUE
:
3883 case TIFFTAG_SMAXSAMPLEVALUE
:
3886 double *data
= NULL
;
3887 enum TIFFReadDirEntryErr err
;
3888 uint32_t saved_flags
;
3890 if (dp
->tdir_count
!= (uint64_t)tif
->tif_dir
.td_samplesperpixel
)
3891 err
= TIFFReadDirEntryErrCount
;
3893 err
= TIFFReadDirEntryDoubleArray(tif
, dp
, &data
);
3894 if (err
!=TIFFReadDirEntryErrOk
)
3896 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3897 TIFFReadDirEntryOutputErr(tif
,err
,module
,fip
? fip
->field_name
: "unknown tagname",0);
3900 saved_flags
= tif
->tif_flags
;
3901 tif
->tif_flags
|= TIFF_PERSAMPLE
;
3902 m
= TIFFSetField(tif
,dp
->tdir_tag
,data
);
3903 tif
->tif_flags
= saved_flags
;
3909 case TIFFTAG_STRIPOFFSETS
:
3910 case TIFFTAG_TILEOFFSETS
:
3911 switch( dp
->tdir_type
)
3918 /* Warn except if directory typically created with TIFFDeferStrileArrayWriting() */
3919 if( !(tif
->tif_mode
== O_RDWR
&&
3920 dp
->tdir_count
== 0 &&
3921 dp
->tdir_type
== 0 &&
3922 dp
->tdir_offset
.toff_long8
== 0) )
3924 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3925 TIFFWarningExt(tif
->tif_clientdata
,module
,
3926 "Invalid data type for tag %s",
3927 fip
? fip
->field_name
: "unknown tagname");
3931 _TIFFmemcpy( &(tif
->tif_dir
.td_stripoffset_entry
),
3932 dp
, sizeof(TIFFDirEntry
) );
3934 case TIFFTAG_STRIPBYTECOUNTS
:
3935 case TIFFTAG_TILEBYTECOUNTS
:
3936 switch( dp
->tdir_type
)
3943 /* Warn except if directory typically created with TIFFDeferStrileArrayWriting() */
3944 if( !(tif
->tif_mode
== O_RDWR
&&
3945 dp
->tdir_count
== 0 &&
3946 dp
->tdir_type
== 0 &&
3947 dp
->tdir_offset
.toff_long8
== 0) )
3949 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3950 TIFFWarningExt(tif
->tif_clientdata
,module
,
3951 "Invalid data type for tag %s",
3952 fip
? fip
->field_name
: "unknown tagname");
3956 _TIFFmemcpy( &(tif
->tif_dir
.td_stripbytecount_entry
),
3957 dp
, sizeof(TIFFDirEntry
) );
3959 case TIFFTAG_COLORMAP
:
3960 case TIFFTAG_TRANSFERFUNCTION
:
3962 enum TIFFReadDirEntryErr err
;
3963 uint32_t countpersample
;
3964 uint32_t countrequired
;
3965 uint32_t incrementpersample
;
3966 uint16_t* value
=NULL
;
3967 /* It would be dangerous to instantiate those tag values */
3968 /* since if td_bitspersample has not yet been read (due to */
3969 /* unordered tags), it could be read afterwards with a */
3970 /* values greater than the default one (1), which may cause */
3971 /* crashes in user code */
3972 if( !bitspersample_read
)
3974 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3975 TIFFWarningExt(tif
->tif_clientdata
,module
,
3976 "Ignoring %s since BitsPerSample tag not found",
3977 fip
? fip
->field_name
: "unknown tagname");
3980 /* ColorMap or TransferFunction for high bit */
3981 /* depths do not make much sense and could be */
3982 /* used as a denial of service vector */
3983 if (tif
->tif_dir
.td_bitspersample
> 24)
3985 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3986 TIFFWarningExt(tif
->tif_clientdata
,module
,
3987 "Ignoring %s because BitsPerSample=%"PRIu16
">24",
3988 fip
? fip
->field_name
: "unknown tagname",
3989 tif
->tif_dir
.td_bitspersample
);
3992 countpersample
=(1U<<tif
->tif_dir
.td_bitspersample
);
3993 if ((dp
->tdir_tag
==TIFFTAG_TRANSFERFUNCTION
)&&(dp
->tdir_count
==(uint64_t)countpersample
))
3995 countrequired
=countpersample
;
3996 incrementpersample
=0;
4000 countrequired
=3*countpersample
;
4001 incrementpersample
=countpersample
;
4003 if (dp
->tdir_count
!=(uint64_t)countrequired
)
4004 err
=TIFFReadDirEntryErrCount
;
4006 err
=TIFFReadDirEntryShortArray(tif
,dp
,&value
);
4007 if (err
!=TIFFReadDirEntryErrOk
)
4009 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
4010 TIFFReadDirEntryOutputErr(tif
,err
,module
,fip
? fip
->field_name
: "unknown tagname",1);
4014 TIFFSetField(tif
,dp
->tdir_tag
,value
,value
+incrementpersample
,value
+2*incrementpersample
);
4019 /* BEGIN REV 4.0 COMPATIBILITY */
4020 case TIFFTAG_OSUBFILETYPE
:
4024 if (TIFFReadDirEntryShort(tif
,dp
,&valueo
)==TIFFReadDirEntryErrOk
)
4028 case OFILETYPE_REDUCEDIMAGE
: value
=FILETYPE_REDUCEDIMAGE
; break;
4029 case OFILETYPE_PAGE
: value
=FILETYPE_PAGE
; break;
4030 default: value
=0; break;
4033 TIFFSetField(tif
,TIFFTAG_SUBFILETYPE
,value
);
4037 /* END REV 4.0 COMPATIBILITY */
4039 (void) TIFFFetchNormalTag(tif
, dp
, TRUE
);
4042 } /* -- if (!dp->tdir_ignore) */
4043 } /* -- for-loop -- */
4045 if( tif
->tif_mode
== O_RDWR
&&
4046 tif
->tif_dir
.td_stripoffset_entry
.tdir_tag
!= 0 &&
4047 tif
->tif_dir
.td_stripoffset_entry
.tdir_count
== 0 &&
4048 tif
->tif_dir
.td_stripoffset_entry
.tdir_type
== 0 &&
4049 tif
->tif_dir
.td_stripoffset_entry
.tdir_offset
.toff_long8
== 0 &&
4050 tif
->tif_dir
.td_stripbytecount_entry
.tdir_tag
!= 0 &&
4051 tif
->tif_dir
.td_stripbytecount_entry
.tdir_count
== 0 &&
4052 tif
->tif_dir
.td_stripbytecount_entry
.tdir_type
== 0 &&
4053 tif
->tif_dir
.td_stripbytecount_entry
.tdir_offset
.toff_long8
== 0 )
4055 /* Directory typically created with TIFFDeferStrileArrayWriting() */
4056 TIFFSetupStrips(tif
);
4058 else if( !(tif
->tif_flags
&TIFF_DEFERSTRILELOAD
) )
4060 if( tif
->tif_dir
.td_stripoffset_entry
.tdir_tag
!= 0 )
4062 if (!TIFFFetchStripThing(tif
,&(tif
->tif_dir
.td_stripoffset_entry
),
4063 tif
->tif_dir
.td_nstrips
,
4064 &tif
->tif_dir
.td_stripoffset_p
))
4069 if( tif
->tif_dir
.td_stripbytecount_entry
.tdir_tag
!= 0 )
4071 if (!TIFFFetchStripThing(tif
,&(tif
->tif_dir
.td_stripbytecount_entry
),
4072 tif
->tif_dir
.td_nstrips
,
4073 &tif
->tif_dir
.td_stripbytecount_p
))
4082 * - If a) compression is OJPEG, and b) photometric tag is missing,
4083 * then we consistently find that photometric should be YCbCr
4084 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
4085 * then we consistently find that the buggy implementation of the
4086 * buggy compression scheme matches photometric YCbCr instead.
4087 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
4088 * then we consistently find bitspersample should be 8.
4089 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4090 * and c) photometric is RGB or YCbCr, then we consistently find
4091 * samplesperpixel should be 3
4092 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4093 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
4094 * find samplesperpixel should be 3
4096 if (tif
->tif_dir
.td_compression
==COMPRESSION_OJPEG
)
4098 if (!TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
4100 TIFFWarningExt(tif
->tif_clientdata
, module
,
4101 "Photometric tag is missing, assuming data is YCbCr");
4102 if (!TIFFSetField(tif
,TIFFTAG_PHOTOMETRIC
,PHOTOMETRIC_YCBCR
))
4105 else if (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_RGB
)
4107 tif
->tif_dir
.td_photometric
=PHOTOMETRIC_YCBCR
;
4108 TIFFWarningExt(tif
->tif_clientdata
, module
,
4109 "Photometric tag value assumed incorrect, "
4110 "assuming data is YCbCr instead of RGB");
4112 if (!TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
4114 TIFFWarningExt(tif
->tif_clientdata
,module
,
4115 "BitsPerSample tag is missing, assuming 8 bits per sample");
4116 if (!TIFFSetField(tif
,TIFFTAG_BITSPERSAMPLE
,8))
4119 if (!TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
4121 if (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_RGB
)
4123 TIFFWarningExt(tif
->tif_clientdata
,module
,
4124 "SamplesPerPixel tag is missing, "
4125 "assuming correct SamplesPerPixel value is 3");
4126 if (!TIFFSetField(tif
,TIFFTAG_SAMPLESPERPIXEL
,3))
4129 if (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_YCBCR
)
4131 TIFFWarningExt(tif
->tif_clientdata
,module
,
4132 "SamplesPerPixel tag is missing, "
4133 "applying correct SamplesPerPixel value of 3");
4134 if (!TIFFSetField(tif
,TIFFTAG_SAMPLESPERPIXEL
,3))
4137 else if ((tif
->tif_dir
.td_photometric
==PHOTOMETRIC_MINISWHITE
)
4138 || (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_MINISBLACK
))
4141 * SamplesPerPixel tag is missing, but is not required
4142 * by spec. Assume correct SamplesPerPixel value of 1.
4144 if (!TIFFSetField(tif
,TIFFTAG_SAMPLESPERPIXEL
,1))
4151 * Make sure all non-color channels are extrasamples.
4152 * If it's not the case, define them as such.
4154 color_channels
= _TIFFGetMaxColorChannels(tif
->tif_dir
.td_photometric
);
4155 if (color_channels
&& tif
->tif_dir
.td_samplesperpixel
- tif
->tif_dir
.td_extrasamples
> color_channels
) {
4156 uint16_t old_extrasamples
;
4157 uint16_t *new_sampleinfo
;
4159 TIFFWarningExt(tif
->tif_clientdata
,module
, "Sum of Photometric type-related "
4160 "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4161 "Defining non-color channels as ExtraSamples.");
4163 old_extrasamples
= tif
->tif_dir
.td_extrasamples
;
4164 tif
->tif_dir
.td_extrasamples
= (uint16_t) (tif
->tif_dir
.td_samplesperpixel
- color_channels
);
4166 // sampleinfo should contain information relative to these new extra samples
4167 new_sampleinfo
= (uint16_t*) _TIFFcalloc(tif
->tif_dir
.td_extrasamples
, sizeof(uint16_t));
4168 if (!new_sampleinfo
) {
4169 TIFFErrorExt(tif
->tif_clientdata
, module
, "Failed to allocate memory for "
4170 "temporary new sampleinfo array "
4171 "(%"PRIu16
" 16 bit elements)",
4172 tif
->tif_dir
.td_extrasamples
);
4176 memcpy(new_sampleinfo
, tif
->tif_dir
.td_sampleinfo
, old_extrasamples
* sizeof(uint16_t));
4177 _TIFFsetShortArray(&tif
->tif_dir
.td_sampleinfo
, new_sampleinfo
, tif
->tif_dir
.td_extrasamples
);
4178 _TIFFfree(new_sampleinfo
);
4182 * Verify Palette image has a Colormap.
4184 if (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_PALETTE
&&
4185 !TIFFFieldSet(tif
, FIELD_COLORMAP
)) {
4186 if ( tif
->tif_dir
.td_bitspersample
>=8 && tif
->tif_dir
.td_samplesperpixel
==3)
4187 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_RGB
;
4188 else if (tif
->tif_dir
.td_bitspersample
>=8)
4189 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_MINISBLACK
;
4191 MissingRequired(tif
, "Colormap");
4197 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
4200 if (tif
->tif_dir
.td_compression
!=COMPRESSION_OJPEG
)
4203 * Attempt to deal with a missing StripByteCounts tag.
4205 if (!TIFFFieldSet(tif
, FIELD_STRIPBYTECOUNTS
)) {
4207 * Some manufacturers violate the spec by not giving
4208 * the size of the strips. In this case, assume there
4209 * is one uncompressed strip of data.
4211 if ((tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
&&
4212 tif
->tif_dir
.td_nstrips
> 1) ||
4213 (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
&&
4214 tif
->tif_dir
.td_nstrips
!= (uint32_t)tif
->tif_dir
.td_samplesperpixel
)) {
4215 MissingRequired(tif
, "StripByteCounts");
4218 TIFFWarningExt(tif
->tif_clientdata
, module
,
4219 "TIFF directory is missing required "
4220 "\"StripByteCounts\" field, calculating from imagelength");
4221 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
4224 } else if (tif
->tif_dir
.td_nstrips
== 1
4225 && !(tif
->tif_flags
&TIFF_ISTILED
)
4226 && ByteCountLooksBad(tif
)) {
4228 * XXX: Plexus (and others) sometimes give a value of
4229 * zero for a tag when they don't know what the
4230 * correct value is! Try and handle the simple case
4231 * of estimating the size of a one strip image.
4233 TIFFWarningExt(tif
->tif_clientdata
, module
,
4234 "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
4235 if(EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
4238 } else if (!(tif
->tif_flags
&TIFF_DEFERSTRILELOAD
)
4239 && tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
4240 && tif
->tif_dir
.td_nstrips
> 2
4241 && tif
->tif_dir
.td_compression
== COMPRESSION_NONE
4242 && TIFFGetStrileByteCount(tif
, 0) != TIFFGetStrileByteCount(tif
, 1)
4243 && TIFFGetStrileByteCount(tif
, 0) != 0
4244 && TIFFGetStrileByteCount(tif
, 1) != 0 ) {
4246 * XXX: Some vendors fill StripByteCount array with
4247 * absolutely wrong values (it can be equal to
4248 * StripOffset array, for example). Catch this case
4251 * We avoid this check if deferring strile loading
4252 * as it would always force us to load the strip/tile
4255 TIFFWarningExt(tif
->tif_clientdata
, module
,
4256 "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
4257 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
4266 if (!TIFFFieldSet(tif
, FIELD_MAXSAMPLEVALUE
))
4268 if (tif
->tif_dir
.td_bitspersample
>=16)
4269 tif
->tif_dir
.td_maxsamplevalue
=0xFFFF;
4271 tif
->tif_dir
.td_maxsamplevalue
= (uint16_t)((1L << tif
->tif_dir
.td_bitspersample
) - 1);
4274 #ifdef STRIPBYTECOUNTSORTED_UNUSED
4276 * XXX: We can optimize checking for the strip bounds using the sorted
4277 * bytecounts array. See also comments for TIFFAppendToStrip()
4278 * function in tif_write.c.
4280 if (!(tif
->tif_flags
&TIFF_DEFERSTRILELOAD
) && tif
->tif_dir
.td_nstrips
> 1) {
4283 tif
->tif_dir
.td_stripbytecountsorted
= 1;
4284 for (strip
= 1; strip
< tif
->tif_dir
.td_nstrips
; strip
++) {
4285 if (TIFFGetStrileOffset(tif
, strip
- 1) >
4286 TIFFGetStrileOffset(tif
, strip
)) {
4287 tif
->tif_dir
.td_stripbytecountsorted
= 0;
4295 * An opportunity for compression mode dependent tag fixup
4297 (*tif
->tif_fixuptags
)(tif
);
4300 * Some manufacturers make life difficult by writing
4301 * large amounts of uncompressed data as a single strip.
4302 * This is contrary to the recommendations of the spec.
4303 * The following makes an attempt at breaking such images
4304 * into strips closer to the recommended 8k bytes. A
4305 * side effect, however, is that the RowsPerStrip tag
4306 * value may be changed.
4308 if ((tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_CONTIG
)&&
4309 (tif
->tif_dir
.td_nstrips
==1)&&
4310 (tif
->tif_dir
.td_compression
==COMPRESSION_NONE
)&&
4311 ((tif
->tif_flags
&(TIFF_STRIPCHOP
|TIFF_ISTILED
))==TIFF_STRIPCHOP
))
4313 ChopUpSingleUncompressedStrip(tif
);
4316 /* There are also uncompressed striped files with strips larger than */
4317 /* 2 GB, which make them unfriendly with a lot of code. If possible, */
4318 /* try to expose smaller "virtual" strips. */
4319 if( tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
&&
4320 tif
->tif_dir
.td_compression
== COMPRESSION_NONE
&&
4321 (tif
->tif_flags
&(TIFF_STRIPCHOP
|TIFF_ISTILED
)) == TIFF_STRIPCHOP
&&
4322 TIFFStripSize64(tif
) > 0x7FFFFFFFUL
)
4324 TryChopUpUncompressedBigTiff(tif
);
4328 * Clear the dirty directory flag.
4330 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
4331 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
4334 * Reinitialize i/o since we are starting on a new directory.
4336 tif
->tif_row
= (uint32_t) -1;
4337 tif
->tif_curstrip
= (uint32_t) -1;
4338 tif
->tif_col
= (uint32_t) -1;
4339 tif
->tif_curtile
= (uint32_t) -1;
4340 tif
->tif_tilesize
= (tmsize_t
) -1;
4342 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
4343 if (!tif
->tif_scanlinesize
) {
4344 TIFFErrorExt(tif
->tif_clientdata
, module
,
4345 "Cannot handle zero scanline size");
4350 tif
->tif_tilesize
= TIFFTileSize(tif
);
4351 if (!tif
->tif_tilesize
) {
4352 TIFFErrorExt(tif
->tif_clientdata
, module
,
4353 "Cannot handle zero tile size");
4357 if (!TIFFStripSize(tif
)) {
4358 TIFFErrorExt(tif
->tif_clientdata
, module
,
4359 "Cannot handle zero strip size");
4371 TIFFReadDirectoryCheckOrder(TIFF
* tif
, TIFFDirEntry
* dir
, uint16_t dircount
)
4373 static const char module
[] = "TIFFReadDirectoryCheckOrder";
4378 for (n
=0, o
=dir
; n
<dircount
; n
++, o
++)
4382 TIFFWarningExt(tif
->tif_clientdata
,module
,
4383 "Invalid TIFF directory; tags are not sorted in ascending order");
4390 static TIFFDirEntry
*
4391 TIFFReadDirectoryFindEntry(TIFF
* tif
, TIFFDirEntry
* dir
, uint16_t dircount
, uint16_t tagid
)
4396 for (m
=dir
, n
=0; n
<dircount
; m
++, n
++)
4398 if (m
->tdir_tag
==tagid
)
4405 TIFFReadDirectoryFindFieldInfo(TIFF
* tif
, uint16_t tagid
, uint32_t* fii
)
4409 mc
=(int32_t)tif
->tif_nfields
;
4418 if (tif
->tif_fields
[mb
]->field_tag
==(uint32_t)tagid
)
4420 if (tif
->tif_fields
[mb
]->field_tag
<(uint32_t)tagid
)
4429 if (tif
->tif_fields
[mb
-1]->field_tag
!=(uint32_t)tagid
)
4437 * Read custom directory from the arbitrary offset.
4438 * The code is very similar to TIFFReadDirectory().
4441 TIFFReadCustomDirectory(TIFF
* tif
, toff_t diroff
,
4442 const TIFFFieldArray
* infoarray
)
4444 static const char module
[] = "TIFFReadCustomDirectory";
4449 const TIFFField
* fip
;
4451 (*tif
->tif_cleanup
)(tif
); /* cleanup any previous compression state */
4452 _TIFFSetupFields(tif
, infoarray
);
4453 dircount
=TIFFFetchDirectory(tif
,diroff
,&dir
,NULL
);
4456 TIFFErrorExt(tif
->tif_clientdata
,module
,
4457 "Failed to read custom directory at offset %" PRIu64
,diroff
);
4460 TIFFFreeDirectory(tif
);
4461 _TIFFmemset(&tif
->tif_dir
, 0, sizeof(TIFFDirectory
));
4462 TIFFReadDirectoryCheckOrder(tif
,dir
,dircount
);
4463 for (di
=0, dp
=dir
; di
<dircount
; di
++, dp
++)
4465 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
4466 if (fii
== FAILED_FII
)
4468 TIFFWarningExt(tif
->tif_clientdata
, module
,
4469 "Unknown field with tag %"PRIu16
" (0x%"PRIx16
") encountered",
4470 dp
->tdir_tag
, dp
->tdir_tag
);
4471 if (!_TIFFMergeFields(tif
, _TIFFCreateAnonField(tif
,
4473 (TIFFDataType
) dp
->tdir_type
),
4475 TIFFWarningExt(tif
->tif_clientdata
, module
,
4476 "Registering anonymous field with tag %"PRIu16
" (0x%"PRIx16
") failed",
4477 dp
->tdir_tag
, dp
->tdir_tag
);
4478 dp
->tdir_ignore
= TRUE
;
4480 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
4481 assert( fii
!= FAILED_FII
);
4484 if (!dp
->tdir_ignore
)
4486 fip
=tif
->tif_fields
[fii
];
4487 if (fip
->field_bit
==FIELD_IGNORE
)
4488 dp
->tdir_ignore
= TRUE
;
4491 /* check data type */
4492 while ((fip
->field_type
!=TIFF_ANY
)&&(fip
->field_type
!=dp
->tdir_type
))
4495 if ((fii
==tif
->tif_nfields
)||
4496 (tif
->tif_fields
[fii
]->field_tag
!=(uint32_t)dp
->tdir_tag
))
4501 fip
=tif
->tif_fields
[fii
];
4505 TIFFWarningExt(tif
->tif_clientdata
, module
,
4506 "Wrong data type %"PRIu16
" for \"%s\"; tag ignored",
4507 dp
->tdir_type
,fip
->field_name
);
4508 dp
->tdir_ignore
= TRUE
;
4512 /* check count if known in advance */
4513 if ((fip
->field_readcount
!=TIFF_VARIABLE
)&&
4514 (fip
->field_readcount
!=TIFF_VARIABLE2
))
4517 if (fip
->field_readcount
==TIFF_SPP
)
4518 expected
=(uint32_t)tif
->tif_dir
.td_samplesperpixel
;
4520 expected
=(uint32_t)fip
->field_readcount
;
4521 if (!CheckDirCount(tif
,dp
,expected
))
4522 dp
->tdir_ignore
= TRUE
;
4526 if (!dp
->tdir_ignore
) {
4527 switch (dp
->tdir_tag
)
4529 case EXIFTAG_SUBJECTDISTANCE
:
4530 (void)TIFFFetchSubjectDistance(tif
, dp
);
4533 (void)TIFFFetchNormalTag(tif
, dp
, TRUE
);
4536 } /*-- if (!dp->tdir_ignore) */
4545 * EXIF is important special case of custom IFD, so we have a special
4546 * function to read it.
4549 TIFFReadEXIFDirectory(TIFF
* tif
, toff_t diroff
)
4551 const TIFFFieldArray
* exifFieldArray
;
4552 exifFieldArray
= _TIFFGetExifFields();
4553 return TIFFReadCustomDirectory(tif
, diroff
, exifFieldArray
);
4557 *--: EXIF-GPS custom directory reading as another special case of custom IFD.
4560 TIFFReadGPSDirectory(TIFF
* tif
, toff_t diroff
)
4562 const TIFFFieldArray
* gpsFieldArray
;
4563 gpsFieldArray
= _TIFFGetGpsFields();
4564 return TIFFReadCustomDirectory(tif
, diroff
, gpsFieldArray
);
4568 EstimateStripByteCounts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16_t dircount
)
4570 static const char module
[] = "EstimateStripByteCounts";
4573 TIFFDirectory
*td
= &tif
->tif_dir
;
4576 /* Do not try to load stripbytecount as we will compute it */
4577 if( !_TIFFFillStrilesInternal( tif
, 0 ) )
4580 if (td
->td_stripbytecount_p
)
4581 _TIFFfree(td
->td_stripbytecount_p
);
4582 td
->td_stripbytecount_p
= (uint64_t*)
4583 _TIFFCheckMalloc(tif
, td
->td_nstrips
, sizeof (uint64_t),
4584 "for \"StripByteCounts\" array");
4585 if( td
->td_stripbytecount_p
== NULL
)
4588 if (td
->td_compression
!= COMPRESSION_NONE
) {
4592 filesize
= TIFFGetFileSize(tif
);
4593 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4594 space
=sizeof(TIFFHeaderClassic
)+2+dircount
*12+4;
4596 space
=sizeof(TIFFHeaderBig
)+8+dircount
*20+8;
4597 /* calculate amount of space used by indirect values */
4598 for (dp
= dir
, n
= dircount
; n
> 0; n
--, dp
++)
4602 typewidth
= TIFFDataWidth((TIFFDataType
) dp
->tdir_type
);
4603 if (typewidth
== 0) {
4604 TIFFErrorExt(tif
->tif_clientdata
, module
,
4605 "Cannot determine size of unknown tag type %"PRIu16
,
4609 if( dp
->tdir_count
> UINT64_MAX
/ typewidth
)
4611 datasize
= (uint64_t)typewidth
* dp
->tdir_count
;
4612 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4622 if( space
> UINT64_MAX
- datasize
)
4626 if( filesize
< space
)
4627 /* we should perhaps return in error ? */
4630 space
= filesize
- space
;
4631 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
)
4632 space
/= td
->td_samplesperpixel
;
4633 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
4634 td
->td_stripbytecount_p
[strip
] = space
;
4636 * This gross hack handles the case were the offset to
4637 * the last strip is past the place where we think the strip
4638 * should begin. Since a strip of data must be contiguous,
4639 * it's safe to assume that we've overestimated the amount
4640 * of data in the strip and trim this number back accordingly.
4643 if (td
->td_stripoffset_p
[strip
] > UINT64_MAX
- td
->td_stripbytecount_p
[strip
])
4645 if (td
->td_stripoffset_p
[strip
]+td
->td_stripbytecount_p
[strip
] > filesize
) {
4646 if( td
->td_stripoffset_p
[strip
] >= filesize
) {
4647 /* Not sure what we should in that case... */
4648 td
->td_stripbytecount_p
[strip
] = 0;
4650 td
->td_stripbytecount_p
[strip
] = filesize
- td
->td_stripoffset_p
[strip
];
4653 } else if (isTiled(tif
)) {
4654 uint64_t bytespertile
= TIFFTileSize64(tif
);
4656 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
4657 td
->td_stripbytecount_p
[strip
] = bytespertile
;
4659 uint64_t rowbytes
= TIFFScanlineSize64(tif
);
4660 uint32_t rowsperstrip
= td
->td_imagelength
/ td
->td_stripsperimage
;
4661 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
4663 if( rowbytes
> 0 && rowsperstrip
> UINT64_MAX
/ rowbytes
)
4665 td
->td_stripbytecount_p
[strip
] = rowbytes
* rowsperstrip
;
4668 TIFFSetFieldBit(tif
, FIELD_STRIPBYTECOUNTS
);
4669 if (!TIFFFieldSet(tif
, FIELD_ROWSPERSTRIP
))
4670 td
->td_rowsperstrip
= td
->td_imagelength
;
4675 MissingRequired(TIFF
* tif
, const char* tagname
)
4677 static const char module
[] = "MissingRequired";
4679 TIFFErrorExt(tif
->tif_clientdata
, module
,
4680 "TIFF directory is missing required \"%s\" field",
4685 * Check the directory offset against the list of already seen directory
4686 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4687 * file with looped directory pointers. We will maintain a list of already
4688 * seen directories and check every IFD offset against that list.
4691 TIFFCheckDirOffset(TIFF
* tif
, uint64_t diroff
)
4695 if (diroff
== 0) /* no more directories */
4697 if (tif
->tif_dirnumber
== 65535) {
4698 TIFFErrorExt(tif
->tif_clientdata
, "TIFFCheckDirOffset",
4699 "Cannot handle more than 65535 TIFF directories");
4703 for (n
= 0; n
< tif
->tif_dirnumber
&& tif
->tif_dirlist
; n
++) {
4704 if (tif
->tif_dirlist
[n
] == diroff
)
4708 tif
->tif_dirnumber
++;
4710 if (tif
->tif_dirlist
== NULL
|| tif
->tif_dirnumber
> tif
->tif_dirlistsize
) {
4711 uint64_t* new_dirlist
;
4714 * XXX: Reduce memory allocation granularity of the dirlist
4717 new_dirlist
= (uint64_t*)_TIFFCheckRealloc(tif
, tif
->tif_dirlist
,
4718 tif
->tif_dirnumber
, 2 * sizeof(uint64_t), "for IFD list");
4721 if( tif
->tif_dirnumber
>= 32768 )
4722 tif
->tif_dirlistsize
= 65535;
4724 tif
->tif_dirlistsize
= 2 * tif
->tif_dirnumber
;
4725 tif
->tif_dirlist
= new_dirlist
;
4728 tif
->tif_dirlist
[tif
->tif_dirnumber
- 1] = diroff
;
4734 * Check the count field of a directory entry against a known value. The
4735 * caller is expected to skip/ignore the tag if there is a mismatch.
4738 CheckDirCount(TIFF
* tif
, TIFFDirEntry
* dir
, uint32_t count
)
4740 if ((uint64_t)count
> dir
->tdir_count
) {
4741 const TIFFField
* fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
4742 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
4743 "incorrect count for field \"%s\" (%" PRIu64
", expecting %"PRIu32
"); tag ignored",
4744 fip
? fip
->field_name
: "unknown tagname",
4745 dir
->tdir_count
, count
);
4747 } else if ((uint64_t)count
< dir
->tdir_count
) {
4748 const TIFFField
* fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
4749 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
4750 "incorrect count for field \"%s\" (%" PRIu64
", expecting %"PRIu32
"); tag trimmed",
4751 fip
? fip
->field_name
: "unknown tagname",
4752 dir
->tdir_count
, count
);
4753 dir
->tdir_count
= count
;
4760 * Read IFD structure from the specified offset. If the pointer to
4761 * nextdiroff variable has been specified, read it too. Function returns a
4762 * number of fields in the directory or 0 if failed.
4765 TIFFFetchDirectory(TIFF
* tif
, uint64_t diroff
, TIFFDirEntry
** pdir
,
4766 uint64_t *nextdiroff
)
4768 static const char module
[] = "TIFFFetchDirectory";
4771 uint16_t dircount16
;
4780 tif
->tif_diroff
= diroff
;
4783 if (!isMapped(tif
)) {
4784 if (!SeekOK(tif
, tif
->tif_diroff
)) {
4785 TIFFErrorExt(tif
->tif_clientdata
, module
,
4786 "%s: Seek error accessing TIFF directory",
4790 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4792 if (!ReadOK(tif
, &dircount16
, sizeof (uint16_t))) {
4793 TIFFErrorExt(tif
->tif_clientdata
, module
,
4794 "%s: Can not read TIFF directory count",
4798 if (tif
->tif_flags
& TIFF_SWAB
)
4799 TIFFSwabShort(&dircount16
);
4800 if (dircount16
>4096)
4802 TIFFErrorExt(tif
->tif_clientdata
, module
,
4803 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4808 uint64_t dircount64
;
4809 if (!ReadOK(tif
, &dircount64
, sizeof (uint64_t))) {
4810 TIFFErrorExt(tif
->tif_clientdata
, module
,
4811 "%s: Can not read TIFF directory count",
4815 if (tif
->tif_flags
& TIFF_SWAB
)
4816 TIFFSwabLong8(&dircount64
);
4817 if (dircount64
>4096)
4819 TIFFErrorExt(tif
->tif_clientdata
, module
,
4820 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4823 dircount16
= (uint16_t)dircount64
;
4826 origdir
= _TIFFCheckMalloc(tif
, dircount16
,
4827 dirsize
, "to read TIFF directory");
4828 if (origdir
== NULL
)
4830 if (!ReadOK(tif
, origdir
, (tmsize_t
)(dircount16
*dirsize
))) {
4831 TIFFErrorExt(tif
->tif_clientdata
, module
,
4832 "%.100s: Can not read TIFF directory",
4838 * Read offset to next directory for sequential scans if
4843 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4845 uint32_t nextdiroff32
;
4846 if (!ReadOK(tif
, &nextdiroff32
, sizeof(uint32_t)))
4848 if (tif
->tif_flags
&TIFF_SWAB
)
4849 TIFFSwabLong(&nextdiroff32
);
4850 *nextdiroff
=nextdiroff32
;
4852 if (!ReadOK(tif
, nextdiroff
, sizeof(uint64_t)))
4854 if (tif
->tif_flags
&TIFF_SWAB
)
4855 TIFFSwabLong8(nextdiroff
);
4861 if (tif
->tif_diroff
> (uint64_t)INT64_MAX
)
4863 TIFFErrorExt(tif
->tif_clientdata
,module
,"Can not read TIFF directory count");
4866 off
= (tmsize_t
) tif
->tif_diroff
;
4869 * Check for integer overflow when validating the dir_off,
4870 * otherwise a very high offset may cause an OOB read and
4871 * crash the client. Make two comparisons instead of
4873 * off + sizeof(uint16_t) > tif->tif_size
4875 * to avoid overflow.
4877 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4879 m
=off
+sizeof(uint16_t);
4880 if ((m
<off
) || (m
<(tmsize_t
)sizeof(uint16_t)) || (m
> tif
->tif_size
)) {
4881 TIFFErrorExt(tif
->tif_clientdata
, module
,
4882 "Can not read TIFF directory count");
4885 _TIFFmemcpy(&dircount16
, tif
->tif_base
+ off
,
4888 off
+= sizeof (uint16_t);
4889 if (tif
->tif_flags
& TIFF_SWAB
)
4890 TIFFSwabShort(&dircount16
);
4891 if (dircount16
>4096)
4893 TIFFErrorExt(tif
->tif_clientdata
, module
,
4894 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4901 uint64_t dircount64
;
4902 m
=off
+sizeof(uint64_t);
4903 if ((m
<off
) || (m
<(tmsize_t
)sizeof(uint64_t)) || (m
> tif
->tif_size
)) {
4904 TIFFErrorExt(tif
->tif_clientdata
, module
,
4905 "Can not read TIFF directory count");
4908 _TIFFmemcpy(&dircount64
, tif
->tif_base
+ off
,
4911 off
+= sizeof (uint64_t);
4912 if (tif
->tif_flags
& TIFF_SWAB
)
4913 TIFFSwabLong8(&dircount64
);
4914 if (dircount64
>4096)
4916 TIFFErrorExt(tif
->tif_clientdata
, module
,
4917 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4920 dircount16
= (uint16_t)dircount64
;
4923 if (dircount16
== 0 )
4925 TIFFErrorExt(tif
->tif_clientdata
, module
,
4926 "Sanity check on directory count failed, zero tag directories not supported");
4929 origdir
= _TIFFCheckMalloc(tif
, dircount16
,
4931 "to read TIFF directory");
4932 if (origdir
== NULL
)
4934 m
=off
+dircount16
*dirsize
;
4935 if ((m
<off
)||(m
<(tmsize_t
)(dircount16
*dirsize
))||(m
>tif
->tif_size
)) {
4936 TIFFErrorExt(tif
->tif_clientdata
, module
,
4937 "Can not read TIFF directory");
4941 _TIFFmemcpy(origdir
, tif
->tif_base
+ off
,
4942 dircount16
* dirsize
);
4945 off
+= dircount16
* dirsize
;
4946 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4948 uint32_t nextdiroff32
;
4949 m
=off
+sizeof(uint32_t);
4950 if ((m
<off
) || (m
<(tmsize_t
)sizeof(uint32_t)) || (m
> tif
->tif_size
))
4953 _TIFFmemcpy(&nextdiroff32
, tif
->tif_base
+ off
,
4955 if (tif
->tif_flags
&TIFF_SWAB
)
4956 TIFFSwabLong(&nextdiroff32
);
4957 *nextdiroff
= nextdiroff32
;
4961 m
=off
+sizeof(uint64_t);
4962 if ((m
<off
) || (m
<(tmsize_t
)sizeof(uint64_t)) || (m
> tif
->tif_size
))
4965 _TIFFmemcpy(nextdiroff
, tif
->tif_base
+ off
,
4967 if (tif
->tif_flags
&TIFF_SWAB
)
4968 TIFFSwabLong8(nextdiroff
);
4972 dir
= (TIFFDirEntry
*)_TIFFCheckMalloc(tif
, dircount16
,
4973 sizeof(TIFFDirEntry
),
4974 "to read TIFF directory");
4980 ma
=(uint8_t*)origdir
;
4982 for (n
=0; n
<dircount16
; n
++)
4984 mb
->tdir_ignore
= FALSE
;
4985 if (tif
->tif_flags
&TIFF_SWAB
)
4986 TIFFSwabShort((uint16_t*)ma
);
4987 mb
->tdir_tag
=*(uint16_t*)ma
;
4988 ma
+=sizeof(uint16_t);
4989 if (tif
->tif_flags
&TIFF_SWAB
)
4990 TIFFSwabShort((uint16_t*)ma
);
4991 mb
->tdir_type
=*(uint16_t*)ma
;
4992 ma
+=sizeof(uint16_t);
4993 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4995 if (tif
->tif_flags
&TIFF_SWAB
)
4996 TIFFSwabLong((uint32_t*)ma
);
4997 mb
->tdir_count
=(uint64_t)(*(uint32_t*)ma
);
4998 ma
+=sizeof(uint32_t);
4999 mb
->tdir_offset
.toff_long8
=0;
5000 *(uint32_t*)(&mb
->tdir_offset
)=*(uint32_t*)ma
;
5001 ma
+=sizeof(uint32_t);
5005 if (tif
->tif_flags
&TIFF_SWAB
)
5006 TIFFSwabLong8((uint64_t*)ma
);
5007 mb
->tdir_count
=TIFFReadUInt64(ma
);
5008 ma
+=sizeof(uint64_t);
5009 mb
->tdir_offset
.toff_long8
=TIFFReadUInt64(ma
);
5010 ma
+=sizeof(uint64_t);
5020 * Fetch a tag that is not handled by special case code.
5023 TIFFFetchNormalTag(TIFF
* tif
, TIFFDirEntry
* dp
, int recover
)
5025 static const char module
[] = "TIFFFetchNormalTag";
5026 enum TIFFReadDirEntryErr err
;
5028 const TIFFField
* fip
= NULL
;
5029 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
5030 if( fii
== FAILED_FII
)
5032 TIFFErrorExt(tif
->tif_clientdata
, "TIFFFetchNormalTag",
5033 "No definition found for tag %"PRIu16
,
5037 fip
=tif
->tif_fields
[fii
];
5038 assert(fip
!= NULL
); /* should not happen */
5039 assert(fip
->set_field_type
!=TIFF_SETGET_OTHER
); /* if so, we shouldn't arrive here but deal with this in specialized code */
5040 assert(fip
->set_field_type
!=TIFF_SETGET_INT
); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
5041 err
=TIFFReadDirEntryErrOk
;
5042 switch (fip
->set_field_type
)
5044 case TIFF_SETGET_UNDEFINED
:
5046 case TIFF_SETGET_ASCII
:
5049 assert(fip
->field_passcount
==0);
5050 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5051 if (err
==TIFFReadDirEntryErrOk
)
5058 while (mb
<(uint32_t)dp
->tdir_count
)
5066 if (mb
+1<(uint32_t)dp
->tdir_count
)
5067 TIFFWarningExt(tif
->tif_clientdata
,module
,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip
->field_name
);
5068 else if (mb
+1>(uint32_t)dp
->tdir_count
)
5071 TIFFWarningExt(tif
->tif_clientdata
,module
,"ASCII value for tag \"%s\" does not end in null byte",fip
->field_name
);
5072 if ((uint32_t)dp
->tdir_count
+ 1 != dp
->tdir_count
+ 1)
5075 o
=_TIFFmalloc((uint32_t)dp
->tdir_count
+ 1);
5082 _TIFFmemcpy(o
,data
,(uint32_t)dp
->tdir_count
);
5083 o
[(uint32_t)dp
->tdir_count
]=0;
5088 n
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
5096 case TIFF_SETGET_UINT8
:
5099 assert(fip
->field_readcount
==1);
5100 assert(fip
->field_passcount
==0);
5101 err
=TIFFReadDirEntryByte(tif
,dp
,&data
);
5102 if (err
==TIFFReadDirEntryErrOk
)
5104 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5109 case TIFF_SETGET_UINT16
:
5112 assert(fip
->field_readcount
==1);
5113 assert(fip
->field_passcount
==0);
5114 err
=TIFFReadDirEntryShort(tif
,dp
,&data
);
5115 if (err
==TIFFReadDirEntryErrOk
)
5117 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5122 case TIFF_SETGET_UINT32
:
5125 assert(fip
->field_readcount
==1);
5126 assert(fip
->field_passcount
==0);
5127 err
=TIFFReadDirEntryLong(tif
,dp
,&data
);
5128 if (err
==TIFFReadDirEntryErrOk
)
5130 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5135 case TIFF_SETGET_UINT64
:
5138 assert(fip
->field_readcount
==1);
5139 assert(fip
->field_passcount
==0);
5140 err
=TIFFReadDirEntryLong8(tif
,dp
,&data
);
5141 if (err
==TIFFReadDirEntryErrOk
)
5143 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5148 case TIFF_SETGET_FLOAT
:
5151 assert(fip
->field_readcount
==1);
5152 assert(fip
->field_passcount
==0);
5153 err
=TIFFReadDirEntryFloat(tif
,dp
,&data
);
5154 if (err
==TIFFReadDirEntryErrOk
)
5156 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5161 case TIFF_SETGET_DOUBLE
:
5164 assert(fip
->field_readcount
==1);
5165 assert(fip
->field_passcount
==0);
5166 err
=TIFFReadDirEntryDouble(tif
,dp
,&data
);
5167 if (err
==TIFFReadDirEntryErrOk
)
5169 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5174 case TIFF_SETGET_IFD8
:
5177 assert(fip
->field_readcount
==1);
5178 assert(fip
->field_passcount
==0);
5179 err
=TIFFReadDirEntryIfd8(tif
,dp
,&data
);
5180 if (err
==TIFFReadDirEntryErrOk
)
5182 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
5187 case TIFF_SETGET_UINT16_PAIR
:
5190 assert(fip
->field_readcount
==2);
5191 assert(fip
->field_passcount
==0);
5192 if (dp
->tdir_count
!=2) {
5193 TIFFWarningExt(tif
->tif_clientdata
,module
,
5194 "incorrect count for field \"%s\", expected 2, got %"PRIu64
,
5195 fip
->field_name
, dp
->tdir_count
);
5198 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
5199 if (err
==TIFFReadDirEntryErrOk
)
5202 assert(data
); /* avoid CLang static Analyzer false positive */
5203 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
[0],data
[1]);
5210 case TIFF_SETGET_C0_UINT8
:
5213 assert(fip
->field_readcount
>=1);
5214 assert(fip
->field_passcount
==0);
5215 if (dp
->tdir_count
!=(uint64_t)fip
->field_readcount
) {
5216 TIFFWarningExt(tif
->tif_clientdata
,module
,
5217 "incorrect count for field \"%s\", expected %d, got %"PRIu64
,
5218 fip
->field_name
,(int) fip
->field_readcount
, dp
->tdir_count
);
5223 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5224 if (err
==TIFFReadDirEntryErrOk
)
5227 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
5236 case TIFF_SETGET_C0_UINT16
:
5239 assert(fip
->field_readcount
>=1);
5240 assert(fip
->field_passcount
==0);
5241 if (dp
->tdir_count
!=(uint64_t)fip
->field_readcount
)
5245 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
5246 if (err
==TIFFReadDirEntryErrOk
)
5249 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
5258 case TIFF_SETGET_C0_UINT32
:
5261 assert(fip
->field_readcount
>=1);
5262 assert(fip
->field_passcount
==0);
5263 if (dp
->tdir_count
!=(uint64_t)fip
->field_readcount
)
5267 err
=TIFFReadDirEntryLongArray(tif
,dp
,&data
);
5268 if (err
==TIFFReadDirEntryErrOk
)
5271 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
5280 case TIFF_SETGET_C0_FLOAT
:
5283 assert(fip
->field_readcount
>=1);
5284 assert(fip
->field_passcount
==0);
5285 if (dp
->tdir_count
!=(uint64_t)fip
->field_readcount
)
5289 err
=TIFFReadDirEntryFloatArray(tif
,dp
,&data
);
5290 if (err
==TIFFReadDirEntryErrOk
)
5293 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
5302 /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read into Double-Arrays. */
5303 case TIFF_SETGET_C0_DOUBLE
:
5306 assert(fip
->field_readcount
>=1);
5307 assert(fip
->field_passcount
==0);
5308 if (dp
->tdir_count
!=(uint64_t)fip
->field_readcount
)
5312 err
=TIFFReadDirEntryDoubleArray(tif
,dp
,&data
);
5313 if (err
==TIFFReadDirEntryErrOk
)
5316 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
5325 case TIFF_SETGET_C16_ASCII
:
5328 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5329 assert(fip
->field_passcount
==1);
5330 if (dp
->tdir_count
>0xFFFF)
5331 err
=TIFFReadDirEntryErrCount
;
5334 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5335 if (err
==TIFFReadDirEntryErrOk
)
5338 if( data
!= 0 && dp
->tdir_count
> 0 && data
[dp
->tdir_count
-1] != '\0' )
5340 TIFFWarningExt(tif
->tif_clientdata
,module
,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip
->field_name
);
5341 data
[dp
->tdir_count
-1] = '\0';
5343 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5352 case TIFF_SETGET_C16_UINT8
:
5355 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5356 assert(fip
->field_passcount
==1);
5357 if (dp
->tdir_count
>0xFFFF)
5358 err
=TIFFReadDirEntryErrCount
;
5361 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5362 if (err
==TIFFReadDirEntryErrOk
)
5365 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5374 case TIFF_SETGET_C16_UINT16
:
5377 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5378 assert(fip
->field_passcount
==1);
5379 if (dp
->tdir_count
>0xFFFF)
5380 err
=TIFFReadDirEntryErrCount
;
5383 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
5384 if (err
==TIFFReadDirEntryErrOk
)
5387 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5396 case TIFF_SETGET_C16_UINT32
:
5399 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5400 assert(fip
->field_passcount
==1);
5401 if (dp
->tdir_count
>0xFFFF)
5402 err
=TIFFReadDirEntryErrCount
;
5405 err
=TIFFReadDirEntryLongArray(tif
,dp
,&data
);
5406 if (err
==TIFFReadDirEntryErrOk
)
5409 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5418 case TIFF_SETGET_C16_UINT64
:
5421 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5422 assert(fip
->field_passcount
==1);
5423 if (dp
->tdir_count
>0xFFFF)
5424 err
=TIFFReadDirEntryErrCount
;
5427 err
=TIFFReadDirEntryLong8Array(tif
,dp
,&data
);
5428 if (err
==TIFFReadDirEntryErrOk
)
5431 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5440 case TIFF_SETGET_C16_FLOAT
:
5443 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5444 assert(fip
->field_passcount
==1);
5445 if (dp
->tdir_count
>0xFFFF)
5446 err
=TIFFReadDirEntryErrCount
;
5449 err
=TIFFReadDirEntryFloatArray(tif
,dp
,&data
);
5450 if (err
==TIFFReadDirEntryErrOk
)
5453 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5462 case TIFF_SETGET_C16_DOUBLE
:
5465 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5466 assert(fip
->field_passcount
==1);
5467 if (dp
->tdir_count
>0xFFFF)
5468 err
=TIFFReadDirEntryErrCount
;
5471 err
=TIFFReadDirEntryDoubleArray(tif
,dp
,&data
);
5472 if (err
==TIFFReadDirEntryErrOk
)
5475 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5484 case TIFF_SETGET_C16_IFD8
:
5487 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5488 assert(fip
->field_passcount
==1);
5489 if (dp
->tdir_count
>0xFFFF)
5490 err
=TIFFReadDirEntryErrCount
;
5493 err
=TIFFReadDirEntryIfd8Array(tif
,dp
,&data
);
5494 if (err
==TIFFReadDirEntryErrOk
)
5497 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint16_t)(dp
->tdir_count
), data
);
5506 case TIFF_SETGET_C32_ASCII
:
5509 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5510 assert(fip
->field_passcount
==1);
5511 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5512 if (err
==TIFFReadDirEntryErrOk
)
5515 if( data
!= 0 && dp
->tdir_count
> 0 && data
[dp
->tdir_count
-1] != '\0' )
5517 TIFFWarningExt(tif
->tif_clientdata
,module
,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip
->field_name
);
5518 data
[dp
->tdir_count
-1] = '\0';
5520 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5528 case TIFF_SETGET_C32_UINT8
:
5531 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5532 assert(fip
->field_passcount
==1);
5533 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5534 if (err
==TIFFReadDirEntryErrOk
)
5537 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5545 case TIFF_SETGET_C32_SINT8
:
5547 int8_t* data
= NULL
;
5548 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5549 assert(fip
->field_passcount
==1);
5550 err
=TIFFReadDirEntrySbyteArray(tif
,dp
,&data
);
5551 if (err
==TIFFReadDirEntryErrOk
)
5554 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5562 case TIFF_SETGET_C32_UINT16
:
5565 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5566 assert(fip
->field_passcount
==1);
5567 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
5568 if (err
==TIFFReadDirEntryErrOk
)
5571 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5579 case TIFF_SETGET_C32_SINT16
:
5581 int16_t* data
= NULL
;
5582 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5583 assert(fip
->field_passcount
==1);
5584 err
=TIFFReadDirEntrySshortArray(tif
,dp
,&data
);
5585 if (err
==TIFFReadDirEntryErrOk
)
5588 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5596 case TIFF_SETGET_C32_UINT32
:
5599 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5600 assert(fip
->field_passcount
==1);
5601 err
=TIFFReadDirEntryLongArray(tif
,dp
,&data
);
5602 if (err
==TIFFReadDirEntryErrOk
)
5605 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5613 case TIFF_SETGET_C32_SINT32
:
5615 int32_t* data
= NULL
;
5616 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5617 assert(fip
->field_passcount
==1);
5618 err
=TIFFReadDirEntrySlongArray(tif
,dp
,&data
);
5619 if (err
==TIFFReadDirEntryErrOk
)
5622 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5630 case TIFF_SETGET_C32_UINT64
:
5633 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5634 assert(fip
->field_passcount
==1);
5635 err
=TIFFReadDirEntryLong8Array(tif
,dp
,&data
);
5636 if (err
==TIFFReadDirEntryErrOk
)
5639 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5647 case TIFF_SETGET_C32_SINT64
:
5649 int64_t* data
= NULL
;
5650 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5651 assert(fip
->field_passcount
==1);
5652 err
=TIFFReadDirEntrySlong8Array(tif
,dp
,&data
);
5653 if (err
==TIFFReadDirEntryErrOk
)
5656 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5664 case TIFF_SETGET_C32_FLOAT
:
5667 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5668 assert(fip
->field_passcount
==1);
5669 err
=TIFFReadDirEntryFloatArray(tif
,dp
,&data
);
5670 if (err
==TIFFReadDirEntryErrOk
)
5673 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5681 case TIFF_SETGET_C32_DOUBLE
:
5684 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5685 assert(fip
->field_passcount
==1);
5686 err
=TIFFReadDirEntryDoubleArray(tif
,dp
,&data
);
5687 if (err
==TIFFReadDirEntryErrOk
)
5690 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5698 case TIFF_SETGET_C32_IFD8
:
5701 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5702 assert(fip
->field_passcount
==1);
5703 err
=TIFFReadDirEntryIfd8Array(tif
,dp
,&data
);
5704 if (err
==TIFFReadDirEntryErrOk
)
5707 m
=TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
), data
);
5716 assert(0); /* we should never get here */
5719 if (err
!=TIFFReadDirEntryErrOk
)
5721 TIFFReadDirEntryOutputErr(tif
,err
,module
,fip
->field_name
,recover
);
5728 * Fetch a set of offsets or lengths.
5729 * While this routine says "strips", in fact it's also used for tiles.
5732 TIFFFetchStripThing(TIFF
* tif
, TIFFDirEntry
* dir
, uint32_t nstrips
, uint64_t** lpp
)
5734 static const char module
[] = "TIFFFetchStripThing";
5735 enum TIFFReadDirEntryErr err
;
5737 err
=TIFFReadDirEntryLong8ArrayWithLimit(tif
,dir
,&data
,nstrips
);
5738 if (err
!=TIFFReadDirEntryErrOk
)
5740 const TIFFField
* fip
= TIFFFieldWithTag(tif
,dir
->tdir_tag
);
5741 TIFFReadDirEntryOutputErr(tif
,err
,module
,fip
? fip
->field_name
: "unknown tagname",0);
5744 if (dir
->tdir_count
<(uint64_t)nstrips
)
5746 uint64_t* resizeddata
;
5747 const TIFFField
* fip
= TIFFFieldWithTag(tif
,dir
->tdir_tag
);
5748 const char* pszMax
= getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
5749 uint32_t max_nstrips
= 1000000;
5751 max_nstrips
= (uint32_t) atoi(pszMax
);
5752 TIFFReadDirEntryOutputErr(tif
,TIFFReadDirEntryErrCount
,
5754 fip
? fip
->field_name
: "unknown tagname",
5755 ( nstrips
<= max_nstrips
) );
5757 if( nstrips
> max_nstrips
)
5763 resizeddata
=(uint64_t*)_TIFFCheckMalloc(tif
, nstrips
, sizeof(uint64_t), "for strip array");
5764 if (resizeddata
==0) {
5768 _TIFFmemcpy(resizeddata
,data
, (uint32_t)dir
->tdir_count
* sizeof(uint64_t));
5769 _TIFFmemset(resizeddata
+(uint32_t)dir
->tdir_count
, 0, (nstrips
- (uint32_t)dir
->tdir_count
) * sizeof(uint64_t));
5778 * Fetch and set the SubjectDistance EXIF tag.
5781 TIFFFetchSubjectDistance(TIFF
* tif
, TIFFDirEntry
* dir
)
5783 static const char module
[] = "TIFFFetchSubjectDistance";
5784 enum TIFFReadDirEntryErr err
;
5787 assert(sizeof(double)==8);
5788 assert(sizeof(uint64_t) == 8);
5789 assert(sizeof(uint32_t) == 4);
5790 if (dir
->tdir_count
!=1)
5791 err
=TIFFReadDirEntryErrCount
;
5792 else if (dir
->tdir_type
!=TIFF_RATIONAL
)
5793 err
=TIFFReadDirEntryErrType
;
5796 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
5799 offset
=*(uint32_t*)(&dir
->tdir_offset
);
5800 if (tif
->tif_flags
&TIFF_SWAB
)
5801 TIFFSwabLong(&offset
);
5802 err
=TIFFReadDirEntryData(tif
,offset
,8,m
.i
);
5806 m
.l
=dir
->tdir_offset
.toff_long8
;
5807 err
=TIFFReadDirEntryErrOk
;
5810 if (err
==TIFFReadDirEntryErrOk
)
5813 if (tif
->tif_flags
&TIFF_SWAB
)
5814 TIFFSwabArrayOfLong(m
.i
,2);
5817 else if (m
.i
[0]==0xFFFFFFFF || m
.i
[1]==0)
5819 * XXX: Numerator 0xFFFFFFFF means that we have infinite
5820 * distance. Indicate that with a negative floating point
5821 * SubjectDistance value.
5825 n
=(double)m
.i
[0]/(double)m
.i
[1];
5826 return(TIFFSetField(tif
,dir
->tdir_tag
,n
));
5830 TIFFReadDirEntryOutputErr(tif
,err
,module
,"SubjectDistance",TRUE
);
5835 static void allocChoppedUpStripArrays(TIFF
* tif
, uint32_t nstrips
,
5836 uint64_t stripbytes
, uint32_t rowsperstrip
)
5838 TIFFDirectory
*td
= &tif
->tif_dir
;
5841 uint64_t last_offset
;
5842 uint64_t last_bytecount
;
5844 uint64_t *newcounts
;
5845 uint64_t *newoffsets
;
5847 offset
= TIFFGetStrileOffset(tif
, 0);
5848 last_offset
= TIFFGetStrileOffset(tif
, td
->td_nstrips
-1);
5849 last_bytecount
= TIFFGetStrileByteCount(tif
, td
->td_nstrips
-1);
5850 if( last_offset
> UINT64_MAX
- last_bytecount
||
5851 last_offset
+ last_bytecount
< offset
)
5855 bytecount
= last_offset
+ last_bytecount
- offset
;
5857 newcounts
= (uint64_t*) _TIFFCheckMalloc(tif
, nstrips
, sizeof (uint64_t),
5858 "for chopped \"StripByteCounts\" array");
5859 newoffsets
= (uint64_t*) _TIFFCheckMalloc(tif
, nstrips
, sizeof (uint64_t),
5860 "for chopped \"StripOffsets\" array");
5861 if (newcounts
== NULL
|| newoffsets
== NULL
) {
5863 * Unable to allocate new strip information, give up and use
5864 * the original one strip information.
5866 if (newcounts
!= NULL
)
5867 _TIFFfree(newcounts
);
5868 if (newoffsets
!= NULL
)
5869 _TIFFfree(newoffsets
);
5874 * Fill the strip information arrays with new bytecounts and offsets
5875 * that reflect the broken-up format.
5877 for (i
= 0; i
< nstrips
; i
++)
5879 if (stripbytes
> bytecount
)
5880 stripbytes
= bytecount
;
5881 newcounts
[i
] = stripbytes
;
5882 newoffsets
[i
] = stripbytes
? offset
: 0;
5883 offset
+= stripbytes
;
5884 bytecount
-= stripbytes
;
5888 * Replace old single strip info with multi-strip info.
5890 td
->td_stripsperimage
= td
->td_nstrips
= nstrips
;
5891 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rowsperstrip
);
5893 _TIFFfree(td
->td_stripbytecount_p
);
5894 _TIFFfree(td
->td_stripoffset_p
);
5895 td
->td_stripbytecount_p
= newcounts
;
5896 td
->td_stripoffset_p
= newoffsets
;
5897 #ifdef STRIPBYTECOUNTSORTED_UNUSED
5898 td
->td_stripbytecountsorted
= 1;
5900 tif
->tif_flags
|= TIFF_CHOPPEDUPARRAYS
;
5905 * Replace a single strip (tile) of uncompressed data by multiple strips
5906 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5907 * dealing with large images or for dealing with machines with a limited
5911 ChopUpSingleUncompressedStrip(TIFF
* tif
)
5913 register TIFFDirectory
*td
= &tif
->tif_dir
;
5917 uint64_t rowblockbytes
;
5918 uint64_t stripbytes
;
5920 uint32_t rowsperstrip
;
5922 bytecount
= TIFFGetStrileByteCount(tif
, 0);
5923 /* On a newly created file, just re-opened to be filled, we */
5924 /* don't want strip chop to trigger as it is going to cause issues */
5925 /* later ( StripOffsets and StripByteCounts improperly filled) . */
5926 if( bytecount
== 0 && tif
->tif_mode
!= O_RDONLY
)
5928 offset
= TIFFGetStrileByteCount(tif
, 0);
5929 assert(td
->td_planarconfig
== PLANARCONFIG_CONTIG
);
5930 if ((td
->td_photometric
== PHOTOMETRIC_YCBCR
)&&
5931 (!isUpSampled(tif
)))
5932 rowblock
= td
->td_ycbcrsubsampling
[1];
5935 rowblockbytes
= TIFFVTileSize64(tif
, rowblock
);
5937 * Make the rows hold at least one scanline, but fill specified amount
5938 * of data if possible.
5940 if (rowblockbytes
> STRIP_SIZE_DEFAULT
) {
5941 stripbytes
= rowblockbytes
;
5942 rowsperstrip
= rowblock
;
5943 } else if (rowblockbytes
> 0 ) {
5944 uint32_t rowblocksperstrip
;
5945 rowblocksperstrip
= (uint32_t) (STRIP_SIZE_DEFAULT
/ rowblockbytes
);
5946 rowsperstrip
= rowblocksperstrip
* rowblock
;
5947 stripbytes
= rowblocksperstrip
* rowblockbytes
;
5953 * never increase the number of rows per strip
5955 if (rowsperstrip
>= td
->td_rowsperstrip
)
5957 nstrips
= TIFFhowmany_32(td
->td_imagelength
, rowsperstrip
);
5961 /* If we are going to allocate a lot of memory, make sure that the */
5962 /* file is as big as needed */
5963 if( tif
->tif_mode
== O_RDONLY
&&
5964 nstrips
> 1000000 &&
5965 (offset
>= TIFFGetFileSize(tif
) ||
5966 stripbytes
> (TIFFGetFileSize(tif
) - offset
) / (nstrips
- 1)) )
5971 allocChoppedUpStripArrays(tif
, nstrips
, stripbytes
, rowsperstrip
);
5976 * Replace a file with contiguous strips > 2 GB of uncompressed data by
5977 * multiple smaller strips. This is useful for
5978 * dealing with large images or for dealing with machines with a limited
5981 static void TryChopUpUncompressedBigTiff( TIFF
* tif
)
5983 TIFFDirectory
*td
= &tif
->tif_dir
;
5985 uint64_t rowblockbytes
;
5988 uint32_t rowblocksperstrip
;
5989 uint32_t rowsperstrip
;
5990 uint64_t stripbytes
;
5993 stripsize
= TIFFStripSize64(tif
);
5995 assert( tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
);
5996 assert( tif
->tif_dir
.td_compression
== COMPRESSION_NONE
);
5997 assert( (tif
->tif_flags
&(TIFF_STRIPCHOP
|TIFF_ISTILED
)) == TIFF_STRIPCHOP
);
5998 assert( stripsize
> 0x7FFFFFFFUL
);
6000 /* On a newly created file, just re-opened to be filled, we */
6001 /* don't want strip chop to trigger as it is going to cause issues */
6002 /* later ( StripOffsets and StripByteCounts improperly filled) . */
6003 if( TIFFGetStrileByteCount(tif
, 0) == 0 && tif
->tif_mode
!= O_RDONLY
)
6006 if ((td
->td_photometric
== PHOTOMETRIC_YCBCR
)&&
6007 (!isUpSampled(tif
)))
6008 rowblock
= td
->td_ycbcrsubsampling
[1];
6011 rowblockbytes
= TIFFVStripSize64(tif
, rowblock
);
6012 if( rowblockbytes
== 0 || rowblockbytes
> 0x7FFFFFFFUL
)
6014 /* In case of file with gigantic width */
6018 /* Check that the strips are contiguous and of the expected size */
6019 for( i
= 0; i
< td
->td_nstrips
; i
++ )
6021 if( i
== td
->td_nstrips
- 1 )
6023 if( TIFFGetStrileByteCount(tif
, i
) < TIFFVStripSize64(
6024 tif
, td
->td_imagelength
- i
* td
->td_rowsperstrip
) )
6031 if( TIFFGetStrileByteCount(tif
, i
) != stripsize
)
6035 if( i
> 0 && TIFFGetStrileOffset(tif
, i
) !=
6036 TIFFGetStrileOffset(tif
, i
-1) + TIFFGetStrileByteCount(tif
, i
-1) )
6043 /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
6044 rowblocksperstrip
= (uint32_t) (512 * 1024 * 1024 / rowblockbytes
);
6045 if( rowblocksperstrip
== 0 )
6046 rowblocksperstrip
= 1;
6047 rowsperstrip
= rowblocksperstrip
* rowblock
;
6048 stripbytes
= rowblocksperstrip
* rowblockbytes
;
6049 assert( stripbytes
<= 0x7FFFFFFFUL
);
6051 nstrips
= TIFFhowmany_32(td
->td_imagelength
, rowsperstrip
);
6055 /* If we are going to allocate a lot of memory, make sure that the */
6056 /* file is as big as needed */
6057 if( tif
->tif_mode
== O_RDONLY
&&
6060 uint64_t last_offset
= TIFFGetStrileOffset(tif
, td
->td_nstrips
- 1);
6061 uint64_t filesize
= TIFFGetFileSize(tif
);
6062 uint64_t last_bytecount
= TIFFGetStrileByteCount(tif
, td
->td_nstrips
- 1);
6063 if( last_offset
> filesize
||
6064 last_bytecount
> filesize
- last_offset
)
6070 allocChoppedUpStripArrays(tif
, nstrips
, stripbytes
, rowsperstrip
);
6074 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
6075 static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a
, int b
)
6080 /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
6081 * strip/tile of number strile. Also fetch the neighbouring values using a
6082 * 4096 byte page size.
6085 int _TIFFPartialReadStripArray(TIFF
* tif
, TIFFDirEntry
* dirent
,
6086 int strile
, uint64_t* panVals
)
6088 static const char module
[] = "_TIFFPartialReadStripArray";
6089 #define IO_CACHE_PAGE_SIZE 4096
6092 const int bSwab
= (tif
->tif_flags
& TIFF_SWAB
) != 0;
6094 uint64_t nBaseOffset
;
6096 uint64_t nOffsetStartPage
;
6097 uint64_t nOffsetEndPage
;
6100 uint64_t nLastStripOffset
;
6103 const uint32_t arraySize
= tif
->tif_dir
.td_stripoffsetbyteallocsize
;
6104 unsigned char buffer
[2 * IO_CACHE_PAGE_SIZE
];
6106 assert( dirent
->tdir_count
> 4 );
6108 if( dirent
->tdir_type
== TIFF_SHORT
)
6110 sizeofval
= sizeof(uint16_t);
6112 else if( dirent
->tdir_type
== TIFF_LONG
)
6114 sizeofval
= sizeof(uint32_t);
6116 else if( dirent
->tdir_type
== TIFF_LONG8
)
6118 sizeofval
= sizeof(uint64_t);
6120 else if( dirent
->tdir_type
== TIFF_SLONG8
)
6122 /* Non conformant but used by some images as in */
6123 /* https://github.com/OSGeo/gdal/issues/2165 */
6124 sizeofval
= sizeof(int64_t);
6128 TIFFErrorExt(tif
->tif_clientdata
, module
,
6129 "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
6130 panVals
[strile
] = 0;
6133 sizeofvalint
= (int)(sizeofval
);
6135 if( tif
->tif_flags
&TIFF_BIGTIFF
)
6137 uint64_t offset
= dirent
->tdir_offset
.toff_long8
;
6139 TIFFSwabLong8(&offset
);
6140 nBaseOffset
= offset
;
6144 uint32_t offset
= dirent
->tdir_offset
.toff_long
;
6146 TIFFSwabLong(&offset
);
6147 nBaseOffset
= offset
;
6149 /* To avoid later unsigned integer overflows */
6150 if( nBaseOffset
> (uint64_t)INT64_MAX
)
6152 TIFFErrorExt(tif
->tif_clientdata
, module
,
6153 "Cannot read offset/size for strile %d", strile
);
6154 panVals
[strile
] = 0;
6157 nOffset
= nBaseOffset
+ sizeofval
* strile
;
6159 (nOffset
/ IO_CACHE_PAGE_SIZE
) * IO_CACHE_PAGE_SIZE
;
6160 nOffsetEndPage
= nOffsetStartPage
+ IO_CACHE_PAGE_SIZE
;
6162 if( nOffset
+ sizeofval
> nOffsetEndPage
)
6163 nOffsetEndPage
+= IO_CACHE_PAGE_SIZE
;
6164 #undef IO_CACHE_PAGE_SIZE
6166 nLastStripOffset
= nBaseOffset
+ arraySize
* sizeofval
;
6167 if( nLastStripOffset
< nOffsetEndPage
)
6168 nOffsetEndPage
= nLastStripOffset
;
6169 if( nOffsetStartPage
>= nOffsetEndPage
)
6171 TIFFErrorExt(tif
->tif_clientdata
, module
,
6172 "Cannot read offset/size for strile %d", strile
);
6173 panVals
[strile
] = 0;
6176 if (!SeekOK(tif
,nOffsetStartPage
))
6178 panVals
[strile
] = 0;
6182 nToRead
= (tmsize_t
)(nOffsetEndPage
- nOffsetStartPage
);
6183 nRead
= TIFFReadFile(tif
, buffer
, nToRead
);
6184 if( nRead
< nToRead
)
6186 TIFFErrorExt(tif
->tif_clientdata
, module
,
6187 "Cannot read offset/size for strile around ~%d", strile
);
6190 iStartBefore
= -(int)((nOffset
- nOffsetStartPage
) / sizeofval
);
6191 if( strile
+ iStartBefore
< 0 )
6192 iStartBefore
= -strile
;
6193 for( i
= iStartBefore
;
6194 (uint32_t)(strile
+ i
) < arraySize
&&
6195 _TIFFUnsanitizedAddUInt64AndInt(nOffset
, (i
+ 1) * sizeofvalint
) <= nOffsetEndPage
;
6198 if( dirent
->tdir_type
== TIFF_SHORT
)
6202 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
6205 TIFFSwabShort(&val
);
6206 panVals
[strile
+ i
] = val
;
6208 else if( dirent
->tdir_type
== TIFF_LONG
)
6212 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
6216 panVals
[strile
+ i
] = val
;
6218 else if( dirent
->tdir_type
== TIFF_LONG8
)
6222 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
6225 TIFFSwabLong8(&val
);
6226 panVals
[strile
+ i
] = val
;
6228 else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
6230 /* Non conformant data type */
6233 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
6236 TIFFSwabLong8((uint64_t*) &val
);
6237 panVals
[strile
+ i
] = (uint64_t) val
;
6243 static int _TIFFFetchStrileValue(TIFF
* tif
,
6245 TIFFDirEntry
* dirent
,
6248 static const char module
[] = "_TIFFFetchStrileValue";
6249 TIFFDirectory
*td
= &tif
->tif_dir
;
6250 if( strile
>= dirent
->tdir_count
)
6254 if( strile
>= td
->td_stripoffsetbyteallocsize
)
6256 uint32_t nStripArrayAllocBefore
= td
->td_stripoffsetbyteallocsize
;
6257 uint32_t nStripArrayAllocNew
;
6258 uint64_t nArraySize64
;
6260 uint64_t* offsetArray
;
6261 uint64_t* bytecountArray
;
6263 if( strile
> 1000000 )
6265 uint64_t filesize
= TIFFGetFileSize(tif
);
6266 /* Avoid excessive memory allocation attempt */
6267 /* For such a big blockid we need at least a TIFF_LONG per strile */
6268 /* for the offset array. */
6269 if( strile
> filesize
/ sizeof(uint32_t) )
6271 TIFFErrorExt(tif
->tif_clientdata
, module
, "File too short");
6276 if( td
->td_stripoffsetbyteallocsize
== 0 &&
6277 td
->td_nstrips
< 1024 * 1024 )
6279 nStripArrayAllocNew
= td
->td_nstrips
;
6283 #define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))
6284 #define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))
6285 nStripArrayAllocNew
= TIFF_MAX(strile
+ 1, 1024U * 512U );
6286 if( nStripArrayAllocNew
< 0xFFFFFFFFU
/ 2 )
6287 nStripArrayAllocNew
*= 2;
6288 nStripArrayAllocNew
= TIFF_MIN(nStripArrayAllocNew
, td
->td_nstrips
);
6290 assert( strile
< nStripArrayAllocNew
);
6291 nArraySize64
= (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew
;
6292 nArraySize
= (size_t)(nArraySize64
);
6293 #if SIZEOF_SIZE_T == 4
6294 if( nArraySize
!= nArraySize64
)
6296 TIFFErrorExt(tif
->tif_clientdata
, module
,
6297 "Cannot allocate strip offset and bytecount arrays");
6301 offsetArray
= (uint64_t*)(
6302 _TIFFrealloc( td
->td_stripoffset_p
, nArraySize
) );
6303 bytecountArray
= (uint64_t*)(
6304 _TIFFrealloc( td
->td_stripbytecount_p
, nArraySize
) );
6306 td
->td_stripoffset_p
= offsetArray
;
6307 if( bytecountArray
)
6308 td
->td_stripbytecount_p
= bytecountArray
;
6309 if( offsetArray
&& bytecountArray
)
6311 td
->td_stripoffsetbyteallocsize
= nStripArrayAllocNew
;
6312 /* Initialize new entries to ~0 / -1 */
6313 memset(td
->td_stripoffset_p
+ nStripArrayAllocBefore
,
6315 (td
->td_stripoffsetbyteallocsize
- nStripArrayAllocBefore
) * sizeof(uint64_t) );
6316 memset(td
->td_stripbytecount_p
+ nStripArrayAllocBefore
,
6318 (td
->td_stripoffsetbyteallocsize
- nStripArrayAllocBefore
) * sizeof(uint64_t) );
6322 TIFFErrorExt(tif
->tif_clientdata
, module
,
6323 "Cannot allocate strip offset and bytecount arrays");
6324 _TIFFfree(td
->td_stripoffset_p
);
6325 td
->td_stripoffset_p
= NULL
;
6326 _TIFFfree(td
->td_stripbytecount_p
);
6327 td
->td_stripbytecount_p
= NULL
;
6328 td
->td_stripoffsetbyteallocsize
= 0;
6331 if( *parray
== NULL
|| strile
>= td
->td_stripoffsetbyteallocsize
)
6334 if( ~((*parray
)[strile
]) == 0 )
6336 if( !_TIFFPartialReadStripArray( tif
, dirent
, strile
, *parray
) )
6338 (*parray
)[strile
] = 0;
6346 static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF
*tif
, uint32_t strile
,
6347 TIFFDirEntry
* dirent
,
6351 TIFFDirectory
*td
= &tif
->tif_dir
;
6354 if( (tif
->tif_flags
&TIFF_DEFERSTRILELOAD
) && !(tif
->tif_flags
&TIFF_CHOPPEDUPARRAYS
) )
6356 if( !(tif
->tif_flags
&TIFF_LAZYSTRILELOAD
) ||
6357 /* If the values may fit in the toff_long/toff_long8 member */
6358 /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
6359 dirent
->tdir_count
<= 4 )
6361 if( !_TIFFFillStriles(tif
) )
6365 /* Do not return, as we want this function to always */
6366 /* return the same value if called several times with */
6367 /* the same arguments */
6372 if( !_TIFFFetchStrileValue(tif
, strile
, dirent
, parray
) )
6380 if( *parray
== NULL
|| strile
>= td
->td_nstrips
)
6386 return (*parray
)[strile
];
6389 /* Return the value of the TileOffsets/StripOffsets array for the specified tile/strile */
6390 uint64_t TIFFGetStrileOffset(TIFF
*tif
, uint32_t strile
)
6392 return TIFFGetStrileOffsetWithErr(tif
, strile
, NULL
);
6395 /* Return the value of the TileOffsets/StripOffsets array for the specified tile/strile */
6396 uint64_t TIFFGetStrileOffsetWithErr(TIFF
*tif
, uint32_t strile
, int *pbErr
)
6398 TIFFDirectory
*td
= &tif
->tif_dir
;
6399 return _TIFFGetStrileOffsetOrByteCountValue(tif
, strile
,
6400 &(td
->td_stripoffset_entry
),
6401 &(td
->td_stripoffset_p
), pbErr
);
6404 /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
6405 uint64_t TIFFGetStrileByteCount(TIFF
*tif
, uint32_t strile
)
6407 return TIFFGetStrileByteCountWithErr(tif
, strile
, NULL
);
6410 /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
6411 uint64_t TIFFGetStrileByteCountWithErr(TIFF
*tif
, uint32_t strile
, int *pbErr
)
6413 TIFFDirectory
*td
= &tif
->tif_dir
;
6414 return _TIFFGetStrileOffsetOrByteCountValue(tif
, strile
,
6415 &(td
->td_stripbytecount_entry
),
6416 &(td
->td_stripbytecount_p
), pbErr
);
6420 int _TIFFFillStriles( TIFF
*tif
)
6422 return _TIFFFillStrilesInternal( tif
, 1 );
6425 static int _TIFFFillStrilesInternal( TIFF
*tif
, int loadStripByteCount
)
6427 register TIFFDirectory
*td
= &tif
->tif_dir
;
6428 int return_value
= 1;
6430 /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
6431 if( !(tif
->tif_flags
&TIFF_DEFERSTRILELOAD
) || (tif
->tif_flags
&TIFF_CHOPPEDUPARRAYS
) != 0 )
6434 if( tif
->tif_flags
&TIFF_LAZYSTRILELOAD
)
6436 /* In case of lazy loading, reload completely the arrays */
6437 _TIFFfree(td
->td_stripoffset_p
);
6438 _TIFFfree(td
->td_stripbytecount_p
);
6439 td
->td_stripoffset_p
= NULL
;
6440 td
->td_stripbytecount_p
= NULL
;
6441 td
->td_stripoffsetbyteallocsize
= 0;
6442 tif
->tif_flags
&= ~TIFF_LAZYSTRILELOAD
;
6445 /* If stripoffset array is already loaded, exit with success */
6446 if( td
->td_stripoffset_p
!= NULL
)
6449 /* If tdir_count was canceled, then we already got there, but in error */
6450 if( td
->td_stripoffset_entry
.tdir_count
== 0 )
6453 if (!TIFFFetchStripThing(tif
,&(td
->td_stripoffset_entry
),
6454 td
->td_nstrips
,&td
->td_stripoffset_p
))
6459 if (loadStripByteCount
&&
6460 !TIFFFetchStripThing(tif
,&(td
->td_stripbytecount_entry
),
6461 td
->td_nstrips
,&td
->td_stripbytecount_p
))
6466 _TIFFmemset( &(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
6467 _TIFFmemset( &(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
6469 #ifdef STRIPBYTECOUNTSORTED_UNUSED
6470 if (tif
->tif_dir
.td_nstrips
> 1 && return_value
== 1 ) {
6473 tif
->tif_dir
.td_stripbytecountsorted
= 1;
6474 for (strip
= 1; strip
< tif
->tif_dir
.td_nstrips
; strip
++) {
6475 if (tif
->tif_dir
.td_stripoffset_p
[strip
- 1] >
6476 tif
->tif_dir
.td_stripoffset_p
[strip
]) {
6477 tif
->tif_dir
.td_stripbytecountsorted
= 0;
6484 return return_value
;
6488 /* vim: set ts=8 sts=8 sw=8 noet: */