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.
44 #define FAILED_FII ((uint32_t)-1)
47 #define TIFFCvtIEEEFloatToNative(tif, n, fp)
48 #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
50 extern void TIFFCvtIEEEFloatToNative(TIFF
*, uint32_t, float *);
51 extern void TIFFCvtIEEEDoubleToNative(TIFF
*, uint32_t, double *);
54 enum TIFFReadDirEntryErr
56 TIFFReadDirEntryErrOk
= 0,
57 TIFFReadDirEntryErrCount
= 1,
58 TIFFReadDirEntryErrType
= 2,
59 TIFFReadDirEntryErrIo
= 3,
60 TIFFReadDirEntryErrRange
= 4,
61 TIFFReadDirEntryErrPsdif
= 5,
62 TIFFReadDirEntryErrSizesan
= 6,
63 TIFFReadDirEntryErrAlloc
= 7,
66 static enum TIFFReadDirEntryErr
67 TIFFReadDirEntryByte(TIFF
*tif
, TIFFDirEntry
*direntry
, uint8_t *value
);
68 static enum TIFFReadDirEntryErr
69 TIFFReadDirEntrySbyte(TIFF
*tif
, TIFFDirEntry
*direntry
, int8_t *value
);
70 static enum TIFFReadDirEntryErr
71 TIFFReadDirEntryShort(TIFF
*tif
, TIFFDirEntry
*direntry
, uint16_t *value
);
72 static enum TIFFReadDirEntryErr
73 TIFFReadDirEntrySshort(TIFF
*tif
, TIFFDirEntry
*direntry
, int16_t *value
);
74 static enum TIFFReadDirEntryErr
75 TIFFReadDirEntryLong(TIFF
*tif
, TIFFDirEntry
*direntry
, uint32_t *value
);
76 static enum TIFFReadDirEntryErr
77 TIFFReadDirEntrySlong(TIFF
*tif
, TIFFDirEntry
*direntry
, int32_t *value
);
78 static enum TIFFReadDirEntryErr
79 TIFFReadDirEntryLong8(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t *value
);
80 static enum TIFFReadDirEntryErr
81 TIFFReadDirEntrySlong8(TIFF
*tif
, TIFFDirEntry
*direntry
, int64_t *value
);
82 static enum TIFFReadDirEntryErr
83 TIFFReadDirEntryFloat(TIFF
*tif
, TIFFDirEntry
*direntry
, float *value
);
84 static enum TIFFReadDirEntryErr
85 TIFFReadDirEntryDouble(TIFF
*tif
, TIFFDirEntry
*direntry
, double *value
);
86 static enum TIFFReadDirEntryErr
87 TIFFReadDirEntryIfd8(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t *value
);
89 static enum TIFFReadDirEntryErr
90 TIFFReadDirEntryArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint32_t *count
,
91 uint32_t desttypesize
, void **value
);
92 static enum TIFFReadDirEntryErr
93 TIFFReadDirEntryByteArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint8_t **value
);
94 static enum TIFFReadDirEntryErr
95 TIFFReadDirEntrySbyteArray(TIFF
*tif
, TIFFDirEntry
*direntry
, int8_t **value
);
96 static enum TIFFReadDirEntryErr
97 TIFFReadDirEntryShortArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint16_t **value
);
98 static enum TIFFReadDirEntryErr
99 TIFFReadDirEntrySshortArray(TIFF
*tif
, TIFFDirEntry
*direntry
, int16_t **value
);
100 static enum TIFFReadDirEntryErr
101 TIFFReadDirEntryLongArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint32_t **value
);
102 static enum TIFFReadDirEntryErr
103 TIFFReadDirEntrySlongArray(TIFF
*tif
, TIFFDirEntry
*direntry
, int32_t **value
);
104 static enum TIFFReadDirEntryErr
105 TIFFReadDirEntryLong8Array(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t **value
);
106 static enum TIFFReadDirEntryErr
107 TIFFReadDirEntrySlong8Array(TIFF
*tif
, TIFFDirEntry
*direntry
, int64_t **value
);
108 static enum TIFFReadDirEntryErr
109 TIFFReadDirEntryFloatArray(TIFF
*tif
, TIFFDirEntry
*direntry
, float **value
);
110 static enum TIFFReadDirEntryErr
111 TIFFReadDirEntryDoubleArray(TIFF
*tif
, TIFFDirEntry
*direntry
, double **value
);
112 static enum TIFFReadDirEntryErr
113 TIFFReadDirEntryIfd8Array(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t **value
);
115 static enum TIFFReadDirEntryErr
116 TIFFReadDirEntryPersampleShort(TIFF
*tif
, TIFFDirEntry
*direntry
,
119 static void TIFFReadDirEntryCheckedByte(TIFF
*tif
, TIFFDirEntry
*direntry
,
121 static void TIFFReadDirEntryCheckedSbyte(TIFF
*tif
, TIFFDirEntry
*direntry
,
123 static void TIFFReadDirEntryCheckedShort(TIFF
*tif
, TIFFDirEntry
*direntry
,
125 static void TIFFReadDirEntryCheckedSshort(TIFF
*tif
, TIFFDirEntry
*direntry
,
127 static void TIFFReadDirEntryCheckedLong(TIFF
*tif
, TIFFDirEntry
*direntry
,
129 static void TIFFReadDirEntryCheckedSlong(TIFF
*tif
, TIFFDirEntry
*direntry
,
131 static enum TIFFReadDirEntryErr
132 TIFFReadDirEntryCheckedLong8(TIFF
*tif
, TIFFDirEntry
*direntry
,
134 static enum TIFFReadDirEntryErr
135 TIFFReadDirEntryCheckedSlong8(TIFF
*tif
, TIFFDirEntry
*direntry
,
137 static enum TIFFReadDirEntryErr
138 TIFFReadDirEntryCheckedRational(TIFF
*tif
, TIFFDirEntry
*direntry
,
140 static enum TIFFReadDirEntryErr
141 TIFFReadDirEntryCheckedSrational(TIFF
*tif
, TIFFDirEntry
*direntry
,
143 static void TIFFReadDirEntryCheckedFloat(TIFF
*tif
, TIFFDirEntry
*direntry
,
145 static enum TIFFReadDirEntryErr
146 TIFFReadDirEntryCheckedDouble(TIFF
*tif
, TIFFDirEntry
*direntry
, double *value
);
148 static enum TIFFReadDirEntryErr
149 TIFFReadDirEntryCheckedRationalDirect(TIFF
*tif
, TIFFDirEntry
*direntry
,
150 TIFFRational_t
*value
);
152 static enum TIFFReadDirEntryErr
153 TIFFReadDirEntryCheckRangeByteSbyte(int8_t value
);
154 static enum TIFFReadDirEntryErr
155 TIFFReadDirEntryCheckRangeByteShort(uint16_t value
);
156 static enum TIFFReadDirEntryErr
157 TIFFReadDirEntryCheckRangeByteSshort(int16_t value
);
158 static enum TIFFReadDirEntryErr
159 TIFFReadDirEntryCheckRangeByteLong(uint32_t value
);
160 static enum TIFFReadDirEntryErr
161 TIFFReadDirEntryCheckRangeByteSlong(int32_t value
);
162 static enum TIFFReadDirEntryErr
163 TIFFReadDirEntryCheckRangeByteLong8(uint64_t value
);
164 static enum TIFFReadDirEntryErr
165 TIFFReadDirEntryCheckRangeByteSlong8(int64_t value
);
167 static enum TIFFReadDirEntryErr
168 TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value
);
169 static enum TIFFReadDirEntryErr
170 TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value
);
171 static enum TIFFReadDirEntryErr
172 TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value
);
173 static enum TIFFReadDirEntryErr
174 TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value
);
175 static enum TIFFReadDirEntryErr
176 TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value
);
177 static enum TIFFReadDirEntryErr
178 TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value
);
179 static enum TIFFReadDirEntryErr
180 TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value
);
182 static enum TIFFReadDirEntryErr
183 TIFFReadDirEntryCheckRangeShortSbyte(int8_t value
);
184 static enum TIFFReadDirEntryErr
185 TIFFReadDirEntryCheckRangeShortSshort(int16_t value
);
186 static enum TIFFReadDirEntryErr
187 TIFFReadDirEntryCheckRangeShortLong(uint32_t value
);
188 static enum TIFFReadDirEntryErr
189 TIFFReadDirEntryCheckRangeShortSlong(int32_t value
);
190 static enum TIFFReadDirEntryErr
191 TIFFReadDirEntryCheckRangeShortLong8(uint64_t value
);
192 static enum TIFFReadDirEntryErr
193 TIFFReadDirEntryCheckRangeShortSlong8(int64_t value
);
195 static enum TIFFReadDirEntryErr
196 TIFFReadDirEntryCheckRangeSshortShort(uint16_t value
);
197 static enum TIFFReadDirEntryErr
198 TIFFReadDirEntryCheckRangeSshortLong(uint32_t value
);
199 static enum TIFFReadDirEntryErr
200 TIFFReadDirEntryCheckRangeSshortSlong(int32_t value
);
201 static enum TIFFReadDirEntryErr
202 TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value
);
203 static enum TIFFReadDirEntryErr
204 TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value
);
206 static enum TIFFReadDirEntryErr
207 TIFFReadDirEntryCheckRangeLongSbyte(int8_t value
);
208 static enum TIFFReadDirEntryErr
209 TIFFReadDirEntryCheckRangeLongSshort(int16_t value
);
210 static enum TIFFReadDirEntryErr
211 TIFFReadDirEntryCheckRangeLongSlong(int32_t value
);
212 static enum TIFFReadDirEntryErr
213 TIFFReadDirEntryCheckRangeLongLong8(uint64_t value
);
214 static enum TIFFReadDirEntryErr
215 TIFFReadDirEntryCheckRangeLongSlong8(int64_t value
);
217 static enum TIFFReadDirEntryErr
218 TIFFReadDirEntryCheckRangeSlongLong(uint32_t value
);
219 static enum TIFFReadDirEntryErr
220 TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value
);
221 static enum TIFFReadDirEntryErr
222 TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value
);
224 static enum TIFFReadDirEntryErr
225 TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value
);
226 static enum TIFFReadDirEntryErr
227 TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value
);
228 static enum TIFFReadDirEntryErr
229 TIFFReadDirEntryCheckRangeLong8Slong(int32_t value
);
230 static enum TIFFReadDirEntryErr
231 TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value
);
233 static enum TIFFReadDirEntryErr
234 TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value
);
236 static enum TIFFReadDirEntryErr
TIFFReadDirEntryData(TIFF
*tif
, uint64_t offset
,
237 tmsize_t size
, void *dest
);
238 static void TIFFReadDirEntryOutputErr(TIFF
*tif
, enum TIFFReadDirEntryErr err
,
239 const char *module
, const char *tagname
,
242 static void TIFFReadDirectoryCheckOrder(TIFF
*tif
, TIFFDirEntry
*dir
,
244 static TIFFDirEntry
*TIFFReadDirectoryFindEntry(TIFF
*tif
, TIFFDirEntry
*dir
,
247 static void TIFFReadDirectoryFindFieldInfo(TIFF
*tif
, uint16_t tagid
,
250 static int EstimateStripByteCounts(TIFF
*tif
, TIFFDirEntry
*dir
,
252 static void MissingRequired(TIFF
*, const char *);
253 static int CheckDirCount(TIFF
*, TIFFDirEntry
*, uint32_t);
254 static uint16_t TIFFFetchDirectory(TIFF
*tif
, uint64_t diroff
,
255 TIFFDirEntry
**pdir
, uint64_t *nextdiroff
);
256 static int TIFFFetchNormalTag(TIFF
*, TIFFDirEntry
*, int recover
);
257 static int TIFFFetchStripThing(TIFF
*tif
, TIFFDirEntry
*dir
, uint32_t nstrips
,
259 static int TIFFFetchSubjectDistance(TIFF
*, TIFFDirEntry
*);
260 static void ChopUpSingleUncompressedStrip(TIFF
*);
261 static void TryChopUpUncompressedBigTiff(TIFF
*);
262 static uint64_t TIFFReadUInt64(const uint8_t *value
);
263 static int _TIFFGetMaxColorChannels(uint16_t photometric
);
265 static int _TIFFFillStrilesInternal(TIFF
*tif
, int loadStripByteCount
);
267 typedef union _UInt64Aligned_t
277 Unaligned safe copy of a uint64_t value from an octet array.
279 static uint64_t TIFFReadUInt64(const uint8_t *value
)
281 UInt64Aligned_t result
;
283 result
.c
[0] = value
[0];
284 result
.c
[1] = value
[1];
285 result
.c
[2] = value
[2];
286 result
.c
[3] = value
[3];
287 result
.c
[4] = value
[4];
288 result
.c
[5] = value
[5];
289 result
.c
[6] = value
[6];
290 result
.c
[7] = value
[7];
295 static enum TIFFReadDirEntryErr
296 TIFFReadDirEntryByte(TIFF
*tif
, TIFFDirEntry
*direntry
, uint8_t *value
)
298 enum TIFFReadDirEntryErr err
;
299 if (direntry
->tdir_count
!= 1)
300 return (TIFFReadDirEntryErrCount
);
301 switch (direntry
->tdir_type
)
304 case TIFF_UNDEFINED
: /* Support to read TIFF_UNDEFINED with
305 field_readcount==1 */
306 TIFFReadDirEntryCheckedByte(tif
, direntry
, value
);
307 return (TIFFReadDirEntryErrOk
);
311 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
312 err
= TIFFReadDirEntryCheckRangeByteSbyte(m
);
313 if (err
!= TIFFReadDirEntryErrOk
)
316 return (TIFFReadDirEntryErrOk
);
321 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
322 err
= TIFFReadDirEntryCheckRangeByteShort(m
);
323 if (err
!= TIFFReadDirEntryErrOk
)
326 return (TIFFReadDirEntryErrOk
);
331 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
332 err
= TIFFReadDirEntryCheckRangeByteSshort(m
);
333 if (err
!= TIFFReadDirEntryErrOk
)
336 return (TIFFReadDirEntryErrOk
);
341 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
342 err
= TIFFReadDirEntryCheckRangeByteLong(m
);
343 if (err
!= TIFFReadDirEntryErrOk
)
346 return (TIFFReadDirEntryErrOk
);
351 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
352 err
= TIFFReadDirEntryCheckRangeByteSlong(m
);
353 if (err
!= TIFFReadDirEntryErrOk
)
356 return (TIFFReadDirEntryErrOk
);
361 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
362 if (err
!= TIFFReadDirEntryErrOk
)
364 err
= TIFFReadDirEntryCheckRangeByteLong8(m
);
365 if (err
!= TIFFReadDirEntryErrOk
)
368 return (TIFFReadDirEntryErrOk
);
373 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
374 if (err
!= TIFFReadDirEntryErrOk
)
376 err
= TIFFReadDirEntryCheckRangeByteSlong8(m
);
377 if (err
!= TIFFReadDirEntryErrOk
)
380 return (TIFFReadDirEntryErrOk
);
383 return (TIFFReadDirEntryErrType
);
387 static enum TIFFReadDirEntryErr
388 TIFFReadDirEntrySbyte(TIFF
*tif
, TIFFDirEntry
*direntry
, int8_t *value
)
390 enum TIFFReadDirEntryErr err
;
391 if (direntry
->tdir_count
!= 1)
392 return (TIFFReadDirEntryErrCount
);
393 switch (direntry
->tdir_type
)
396 case TIFF_UNDEFINED
: /* Support to read TIFF_UNDEFINED with
397 field_readcount==1 */
400 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
401 err
= TIFFReadDirEntryCheckRangeSbyteByte(m
);
402 if (err
!= TIFFReadDirEntryErrOk
)
405 return (TIFFReadDirEntryErrOk
);
409 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, value
);
410 return (TIFFReadDirEntryErrOk
);
415 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
416 err
= TIFFReadDirEntryCheckRangeSbyteShort(m
);
417 if (err
!= TIFFReadDirEntryErrOk
)
420 return (TIFFReadDirEntryErrOk
);
425 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
426 err
= TIFFReadDirEntryCheckRangeSbyteSshort(m
);
427 if (err
!= TIFFReadDirEntryErrOk
)
430 return (TIFFReadDirEntryErrOk
);
435 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
436 err
= TIFFReadDirEntryCheckRangeSbyteLong(m
);
437 if (err
!= TIFFReadDirEntryErrOk
)
440 return (TIFFReadDirEntryErrOk
);
445 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
446 err
= TIFFReadDirEntryCheckRangeSbyteSlong(m
);
447 if (err
!= TIFFReadDirEntryErrOk
)
450 return (TIFFReadDirEntryErrOk
);
455 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
456 if (err
!= TIFFReadDirEntryErrOk
)
458 err
= TIFFReadDirEntryCheckRangeSbyteLong8(m
);
459 if (err
!= TIFFReadDirEntryErrOk
)
462 return (TIFFReadDirEntryErrOk
);
467 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
468 if (err
!= TIFFReadDirEntryErrOk
)
470 err
= TIFFReadDirEntryCheckRangeSbyteSlong8(m
);
471 if (err
!= TIFFReadDirEntryErrOk
)
474 return (TIFFReadDirEntryErrOk
);
477 return (TIFFReadDirEntryErrType
);
479 } /*-- TIFFReadDirEntrySbyte() --*/
481 static enum TIFFReadDirEntryErr
482 TIFFReadDirEntryShort(TIFF
*tif
, TIFFDirEntry
*direntry
, uint16_t *value
)
484 enum TIFFReadDirEntryErr err
;
485 if (direntry
->tdir_count
!= 1)
486 return (TIFFReadDirEntryErrCount
);
487 switch (direntry
->tdir_type
)
492 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
493 *value
= (uint16_t)m
;
494 return (TIFFReadDirEntryErrOk
);
499 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
500 err
= TIFFReadDirEntryCheckRangeShortSbyte(m
);
501 if (err
!= TIFFReadDirEntryErrOk
)
503 *value
= (uint16_t)m
;
504 return (TIFFReadDirEntryErrOk
);
507 TIFFReadDirEntryCheckedShort(tif
, direntry
, value
);
508 return (TIFFReadDirEntryErrOk
);
512 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
513 err
= TIFFReadDirEntryCheckRangeShortSshort(m
);
514 if (err
!= TIFFReadDirEntryErrOk
)
516 *value
= (uint16_t)m
;
517 return (TIFFReadDirEntryErrOk
);
522 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
523 err
= TIFFReadDirEntryCheckRangeShortLong(m
);
524 if (err
!= TIFFReadDirEntryErrOk
)
526 *value
= (uint16_t)m
;
527 return (TIFFReadDirEntryErrOk
);
532 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
533 err
= TIFFReadDirEntryCheckRangeShortSlong(m
);
534 if (err
!= TIFFReadDirEntryErrOk
)
536 *value
= (uint16_t)m
;
537 return (TIFFReadDirEntryErrOk
);
542 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
543 if (err
!= TIFFReadDirEntryErrOk
)
545 err
= TIFFReadDirEntryCheckRangeShortLong8(m
);
546 if (err
!= TIFFReadDirEntryErrOk
)
548 *value
= (uint16_t)m
;
549 return (TIFFReadDirEntryErrOk
);
554 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
555 if (err
!= TIFFReadDirEntryErrOk
)
557 err
= TIFFReadDirEntryCheckRangeShortSlong8(m
);
558 if (err
!= TIFFReadDirEntryErrOk
)
560 *value
= (uint16_t)m
;
561 return (TIFFReadDirEntryErrOk
);
564 return (TIFFReadDirEntryErrType
);
566 } /*-- TIFFReadDirEntryShort() --*/
568 static enum TIFFReadDirEntryErr
569 TIFFReadDirEntrySshort(TIFF
*tif
, TIFFDirEntry
*direntry
, int16_t *value
)
571 enum TIFFReadDirEntryErr err
;
572 if (direntry
->tdir_count
!= 1)
573 return (TIFFReadDirEntryErrCount
);
574 switch (direntry
->tdir_type
)
579 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
581 return (TIFFReadDirEntryErrOk
);
586 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
588 return (TIFFReadDirEntryErrOk
);
593 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
594 err
= TIFFReadDirEntryCheckRangeSshortShort(m
);
595 if (err
!= TIFFReadDirEntryErrOk
)
597 *value
= (uint16_t)m
;
598 return (TIFFReadDirEntryErrOk
);
601 TIFFReadDirEntryCheckedSshort(tif
, direntry
, value
);
602 return (TIFFReadDirEntryErrOk
);
606 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
607 err
= TIFFReadDirEntryCheckRangeSshortLong(m
);
608 if (err
!= TIFFReadDirEntryErrOk
)
611 return (TIFFReadDirEntryErrOk
);
616 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
617 err
= TIFFReadDirEntryCheckRangeSshortSlong(m
);
618 if (err
!= TIFFReadDirEntryErrOk
)
621 return (TIFFReadDirEntryErrOk
);
626 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
627 if (err
!= TIFFReadDirEntryErrOk
)
629 err
= TIFFReadDirEntryCheckRangeSshortLong8(m
);
630 if (err
!= TIFFReadDirEntryErrOk
)
633 return (TIFFReadDirEntryErrOk
);
638 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
639 if (err
!= TIFFReadDirEntryErrOk
)
641 err
= TIFFReadDirEntryCheckRangeSshortSlong8(m
);
642 if (err
!= TIFFReadDirEntryErrOk
)
645 return (TIFFReadDirEntryErrOk
);
648 return (TIFFReadDirEntryErrType
);
650 } /*-- TIFFReadDirEntrySshort() --*/
652 static enum TIFFReadDirEntryErr
653 TIFFReadDirEntryLong(TIFF
*tif
, TIFFDirEntry
*direntry
, uint32_t *value
)
655 enum TIFFReadDirEntryErr err
;
656 if (direntry
->tdir_count
!= 1)
657 return (TIFFReadDirEntryErrCount
);
658 switch (direntry
->tdir_type
)
663 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
664 *value
= (uint32_t)m
;
665 return (TIFFReadDirEntryErrOk
);
670 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
671 err
= TIFFReadDirEntryCheckRangeLongSbyte(m
);
672 if (err
!= TIFFReadDirEntryErrOk
)
674 *value
= (uint32_t)m
;
675 return (TIFFReadDirEntryErrOk
);
680 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
681 *value
= (uint32_t)m
;
682 return (TIFFReadDirEntryErrOk
);
687 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
688 err
= TIFFReadDirEntryCheckRangeLongSshort(m
);
689 if (err
!= TIFFReadDirEntryErrOk
)
691 *value
= (uint32_t)m
;
692 return (TIFFReadDirEntryErrOk
);
695 TIFFReadDirEntryCheckedLong(tif
, direntry
, value
);
696 return (TIFFReadDirEntryErrOk
);
700 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
701 err
= TIFFReadDirEntryCheckRangeLongSlong(m
);
702 if (err
!= TIFFReadDirEntryErrOk
)
704 *value
= (uint32_t)m
;
705 return (TIFFReadDirEntryErrOk
);
710 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
711 if (err
!= TIFFReadDirEntryErrOk
)
713 err
= TIFFReadDirEntryCheckRangeLongLong8(m
);
714 if (err
!= TIFFReadDirEntryErrOk
)
716 *value
= (uint32_t)m
;
717 return (TIFFReadDirEntryErrOk
);
722 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
723 if (err
!= TIFFReadDirEntryErrOk
)
725 err
= TIFFReadDirEntryCheckRangeLongSlong8(m
);
726 if (err
!= TIFFReadDirEntryErrOk
)
728 *value
= (uint32_t)m
;
729 return (TIFFReadDirEntryErrOk
);
732 return (TIFFReadDirEntryErrType
);
734 } /*-- TIFFReadDirEntryLong() --*/
736 static enum TIFFReadDirEntryErr
737 TIFFReadDirEntrySlong(TIFF
*tif
, TIFFDirEntry
*direntry
, int32_t *value
)
739 enum TIFFReadDirEntryErr err
;
740 if (direntry
->tdir_count
!= 1)
741 return (TIFFReadDirEntryErrCount
);
742 switch (direntry
->tdir_type
)
747 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
749 return (TIFFReadDirEntryErrOk
);
754 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
756 return (TIFFReadDirEntryErrOk
);
761 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
763 return (TIFFReadDirEntryErrOk
);
768 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
770 return (TIFFReadDirEntryErrOk
);
775 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
776 err
= TIFFReadDirEntryCheckRangeSlongLong(m
);
777 if (err
!= TIFFReadDirEntryErrOk
)
780 return (TIFFReadDirEntryErrOk
);
783 TIFFReadDirEntryCheckedSlong(tif
, direntry
, value
);
784 return (TIFFReadDirEntryErrOk
);
788 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
789 if (err
!= TIFFReadDirEntryErrOk
)
791 err
= TIFFReadDirEntryCheckRangeSlongLong8(m
);
792 if (err
!= TIFFReadDirEntryErrOk
)
795 return (TIFFReadDirEntryErrOk
);
800 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
801 if (err
!= TIFFReadDirEntryErrOk
)
803 err
= TIFFReadDirEntryCheckRangeSlongSlong8(m
);
804 if (err
!= TIFFReadDirEntryErrOk
)
807 return (TIFFReadDirEntryErrOk
);
810 return (TIFFReadDirEntryErrType
);
812 } /*-- TIFFReadDirEntrySlong() --*/
814 static enum TIFFReadDirEntryErr
815 TIFFReadDirEntryLong8(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t *value
)
817 enum TIFFReadDirEntryErr err
;
818 if (direntry
->tdir_count
!= 1)
819 return (TIFFReadDirEntryErrCount
);
820 switch (direntry
->tdir_type
)
825 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
826 *value
= (uint64_t)m
;
827 return (TIFFReadDirEntryErrOk
);
832 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
833 err
= TIFFReadDirEntryCheckRangeLong8Sbyte(m
);
834 if (err
!= TIFFReadDirEntryErrOk
)
836 *value
= (uint64_t)m
;
837 return (TIFFReadDirEntryErrOk
);
842 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
843 *value
= (uint64_t)m
;
844 return (TIFFReadDirEntryErrOk
);
849 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
850 err
= TIFFReadDirEntryCheckRangeLong8Sshort(m
);
851 if (err
!= TIFFReadDirEntryErrOk
)
853 *value
= (uint64_t)m
;
854 return (TIFFReadDirEntryErrOk
);
859 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
860 *value
= (uint64_t)m
;
861 return (TIFFReadDirEntryErrOk
);
866 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
867 err
= TIFFReadDirEntryCheckRangeLong8Slong(m
);
868 if (err
!= TIFFReadDirEntryErrOk
)
870 *value
= (uint64_t)m
;
871 return (TIFFReadDirEntryErrOk
);
874 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, value
);
879 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
880 if (err
!= TIFFReadDirEntryErrOk
)
882 err
= TIFFReadDirEntryCheckRangeLong8Slong8(m
);
883 if (err
!= TIFFReadDirEntryErrOk
)
885 *value
= (uint64_t)m
;
886 return (TIFFReadDirEntryErrOk
);
889 return (TIFFReadDirEntryErrType
);
891 } /*-- TIFFReadDirEntryLong8() --*/
893 static enum TIFFReadDirEntryErr
894 TIFFReadDirEntrySlong8(TIFF
*tif
, TIFFDirEntry
*direntry
, int64_t *value
)
896 enum TIFFReadDirEntryErr err
;
897 if (direntry
->tdir_count
!= 1)
898 return (TIFFReadDirEntryErrCount
);
899 switch (direntry
->tdir_type
)
904 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
906 return (TIFFReadDirEntryErrOk
);
911 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
913 return (TIFFReadDirEntryErrOk
);
918 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
920 return (TIFFReadDirEntryErrOk
);
925 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
927 return (TIFFReadDirEntryErrOk
);
932 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
934 return (TIFFReadDirEntryErrOk
);
939 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
941 return (TIFFReadDirEntryErrOk
);
946 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
947 if (err
!= TIFFReadDirEntryErrOk
)
949 err
= TIFFReadDirEntryCheckRangeSlong8Long8(m
);
950 if (err
!= TIFFReadDirEntryErrOk
)
953 return (TIFFReadDirEntryErrOk
);
956 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, value
);
959 return (TIFFReadDirEntryErrType
);
961 } /*-- TIFFReadDirEntrySlong8() --*/
963 static enum TIFFReadDirEntryErr
964 TIFFReadDirEntryFloat(TIFF
*tif
, TIFFDirEntry
*direntry
, float *value
)
966 enum TIFFReadDirEntryErr err
;
967 if (direntry
->tdir_count
!= 1)
968 return (TIFFReadDirEntryErrCount
);
969 switch (direntry
->tdir_type
)
974 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
976 return (TIFFReadDirEntryErrOk
);
981 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
983 return (TIFFReadDirEntryErrOk
);
988 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
990 return (TIFFReadDirEntryErrOk
);
995 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
997 return (TIFFReadDirEntryErrOk
);
1002 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
1004 return (TIFFReadDirEntryErrOk
);
1009 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
1011 return (TIFFReadDirEntryErrOk
);
1016 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
1017 if (err
!= TIFFReadDirEntryErrOk
)
1019 #if defined(__WIN32__) && (_MSC_VER < 1500)
1021 * XXX: MSVC 6.0 does not support conversion
1022 * of 64-bit integers into floating point
1025 *value
= _TIFFUInt64ToFloat(m
);
1029 return (TIFFReadDirEntryErrOk
);
1034 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
1035 if (err
!= TIFFReadDirEntryErrOk
)
1038 return (TIFFReadDirEntryErrOk
);
1043 err
= TIFFReadDirEntryCheckedRational(tif
, direntry
, &m
);
1044 if (err
!= TIFFReadDirEntryErrOk
)
1047 return (TIFFReadDirEntryErrOk
);
1049 case TIFF_SRATIONAL
:
1052 err
= TIFFReadDirEntryCheckedSrational(tif
, direntry
, &m
);
1053 if (err
!= TIFFReadDirEntryErrOk
)
1056 return (TIFFReadDirEntryErrOk
);
1059 TIFFReadDirEntryCheckedFloat(tif
, direntry
, value
);
1060 return (TIFFReadDirEntryErrOk
);
1064 err
= TIFFReadDirEntryCheckedDouble(tif
, direntry
, &m
);
1065 if (err
!= TIFFReadDirEntryErrOk
)
1067 if ((m
> FLT_MAX
) || (m
< -FLT_MAX
))
1068 return (TIFFReadDirEntryErrRange
);
1070 return (TIFFReadDirEntryErrOk
);
1073 return (TIFFReadDirEntryErrType
);
1077 static enum TIFFReadDirEntryErr
1078 TIFFReadDirEntryDouble(TIFF
*tif
, TIFFDirEntry
*direntry
, double *value
)
1080 enum TIFFReadDirEntryErr err
;
1081 if (direntry
->tdir_count
!= 1)
1082 return (TIFFReadDirEntryErrCount
);
1083 switch (direntry
->tdir_type
)
1088 TIFFReadDirEntryCheckedByte(tif
, direntry
, &m
);
1090 return (TIFFReadDirEntryErrOk
);
1095 TIFFReadDirEntryCheckedSbyte(tif
, direntry
, &m
);
1097 return (TIFFReadDirEntryErrOk
);
1102 TIFFReadDirEntryCheckedShort(tif
, direntry
, &m
);
1104 return (TIFFReadDirEntryErrOk
);
1109 TIFFReadDirEntryCheckedSshort(tif
, direntry
, &m
);
1111 return (TIFFReadDirEntryErrOk
);
1116 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
1118 return (TIFFReadDirEntryErrOk
);
1123 TIFFReadDirEntryCheckedSlong(tif
, direntry
, &m
);
1125 return (TIFFReadDirEntryErrOk
);
1130 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, &m
);
1131 if (err
!= TIFFReadDirEntryErrOk
)
1133 #if defined(__WIN32__) && (_MSC_VER < 1500)
1135 * XXX: MSVC 6.0 does not support conversion
1136 * of 64-bit integers into floating point
1139 *value
= _TIFFUInt64ToDouble(m
);
1143 return (TIFFReadDirEntryErrOk
);
1148 err
= TIFFReadDirEntryCheckedSlong8(tif
, direntry
, &m
);
1149 if (err
!= TIFFReadDirEntryErrOk
)
1152 return (TIFFReadDirEntryErrOk
);
1155 err
= TIFFReadDirEntryCheckedRational(tif
, direntry
, value
);
1157 case TIFF_SRATIONAL
:
1158 err
= TIFFReadDirEntryCheckedSrational(tif
, direntry
, value
);
1163 TIFFReadDirEntryCheckedFloat(tif
, direntry
, &m
);
1165 return (TIFFReadDirEntryErrOk
);
1168 err
= TIFFReadDirEntryCheckedDouble(tif
, direntry
, value
);
1171 return (TIFFReadDirEntryErrType
);
1175 static enum TIFFReadDirEntryErr
1176 TIFFReadDirEntryIfd8(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t *value
)
1178 enum TIFFReadDirEntryErr err
;
1179 if (direntry
->tdir_count
!= 1)
1180 return (TIFFReadDirEntryErrCount
);
1181 switch (direntry
->tdir_type
)
1187 TIFFReadDirEntryCheckedLong(tif
, direntry
, &m
);
1188 *value
= (uint64_t)m
;
1189 return (TIFFReadDirEntryErrOk
);
1193 err
= TIFFReadDirEntryCheckedLong8(tif
, direntry
, value
);
1196 return (TIFFReadDirEntryErrType
);
1200 #define INITIAL_THRESHOLD (1024 * 1024)
1201 #define THRESHOLD_MULTIPLIER 10
1202 #define MAX_THRESHOLD \
1203 (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \
1206 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDataAndRealloc(TIFF
*tif
,
1211 #if SIZEOF_SIZE_T == 8
1212 tmsize_t threshold
= INITIAL_THRESHOLD
;
1214 tmsize_t already_read
= 0;
1216 assert(!isMapped(tif
));
1218 if (!SeekOK(tif
, offset
))
1219 return (TIFFReadDirEntryErrIo
);
1221 /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
1222 /* so as to avoid allocating too much memory in case the file is too */
1223 /* short. We could ask for the file size, but this might be */
1224 /* expensive with some I/O layers (think of reading a gzipped file) */
1225 /* Restrict to 64 bit processes, so as to avoid reallocs() */
1226 /* on 32 bit processes where virtual memory is scarce. */
1227 while (already_read
< size
)
1230 tmsize_t bytes_read
;
1231 tmsize_t to_read
= size
- already_read
;
1232 #if SIZEOF_SIZE_T == 8
1233 if (to_read
>= threshold
&& threshold
< MAX_THRESHOLD
)
1235 to_read
= threshold
;
1236 threshold
*= THRESHOLD_MULTIPLIER
;
1241 (uint8_t *)_TIFFreallocExt(tif
, *pdest
, already_read
+ to_read
);
1242 if (new_dest
== NULL
)
1244 TIFFErrorExtR(tif
, tif
->tif_name
,
1245 "Failed to allocate memory for %s "
1246 "(%" TIFF_SSIZE_FORMAT
1247 " elements of %" TIFF_SSIZE_FORMAT
" bytes each)",
1248 "TIFFReadDirEntryArray", (tmsize_t
)1,
1249 already_read
+ to_read
);
1250 return TIFFReadDirEntryErrAlloc
;
1254 bytes_read
= TIFFReadFile(tif
, (char *)*pdest
+ already_read
, to_read
);
1255 already_read
+= bytes_read
;
1256 if (bytes_read
!= to_read
)
1258 return TIFFReadDirEntryErrIo
;
1261 return TIFFReadDirEntryErrOk
;
1264 /* Caution: if raising that value, make sure int32 / uint32 overflows can't
1265 * occur elsewhere */
1266 #define MAX_SIZE_TAG_DATA 2147483647U
1268 static enum TIFFReadDirEntryErr
1269 TIFFReadDirEntryArrayWithLimit(TIFF
*tif
, TIFFDirEntry
*direntry
,
1270 uint32_t *count
, uint32_t desttypesize
,
1271 void **value
, uint64_t maxcount
)
1276 uint64_t target_count64
;
1277 int original_datasize_clamped
;
1278 typesize
= TIFFDataWidth(direntry
->tdir_type
);
1281 (direntry
->tdir_count
> maxcount
) ? maxcount
: direntry
->tdir_count
;
1283 if ((target_count64
== 0) || (typesize
== 0))
1286 return (TIFFReadDirEntryErrOk
);
1290 /* We just want to know if the original tag size is more than 4 bytes
1291 * (classic TIFF) or 8 bytes (BigTIFF)
1293 original_datasize_clamped
=
1294 ((direntry
->tdir_count
> 10) ? 10 : (int)direntry
->tdir_count
) *
1298 * As a sanity check, make sure we have no more than a 2GB tag array
1299 * in either the current data type or the dest data type. This also
1300 * avoids problems with overflow of tmsize_t on 32bit systems.
1302 if ((uint64_t)(MAX_SIZE_TAG_DATA
/ typesize
) < target_count64
)
1303 return (TIFFReadDirEntryErrSizesan
);
1304 if ((uint64_t)(MAX_SIZE_TAG_DATA
/ desttypesize
) < target_count64
)
1305 return (TIFFReadDirEntryErrSizesan
);
1307 *count
= (uint32_t)target_count64
;
1308 datasize
= (*count
) * typesize
;
1309 assert((tmsize_t
)datasize
> 0);
1311 if (isMapped(tif
) && datasize
> (uint64_t)tif
->tif_size
)
1312 return TIFFReadDirEntryErrIo
;
1314 if (!isMapped(tif
) && (((tif
->tif_flags
& TIFF_BIGTIFF
) && datasize
> 8) ||
1315 (!(tif
->tif_flags
& TIFF_BIGTIFF
) && datasize
> 4)))
1321 data
= _TIFFCheckMalloc(tif
, *count
, typesize
, "ReadDirEntryArray");
1323 return (TIFFReadDirEntryErrAlloc
);
1325 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
1327 /* Only the condition on original_datasize_clamped. The second
1328 * one is implied, but Coverity Scan cannot see it. */
1329 if (original_datasize_clamped
<= 4 && datasize
<= 4)
1330 _TIFFmemcpy(data
, &direntry
->tdir_offset
, datasize
);
1333 enum TIFFReadDirEntryErr err
;
1334 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
1335 if (tif
->tif_flags
& TIFF_SWAB
)
1336 TIFFSwabLong(&offset
);
1338 err
= TIFFReadDirEntryData(tif
, (uint64_t)offset
,
1339 (tmsize_t
)datasize
, data
);
1341 err
= TIFFReadDirEntryDataAndRealloc(tif
, (uint64_t)offset
,
1342 (tmsize_t
)datasize
, &data
);
1343 if (err
!= TIFFReadDirEntryErrOk
)
1345 _TIFFfreeExt(tif
, data
);
1352 /* See above comment for the Classic TIFF case */
1353 if (original_datasize_clamped
<= 8 && datasize
<= 8)
1354 _TIFFmemcpy(data
, &direntry
->tdir_offset
, datasize
);
1357 enum TIFFReadDirEntryErr err
;
1358 uint64_t offset
= direntry
->tdir_offset
.toff_long8
;
1359 if (tif
->tif_flags
& TIFF_SWAB
)
1360 TIFFSwabLong8(&offset
);
1362 err
= TIFFReadDirEntryData(tif
, (uint64_t)offset
,
1363 (tmsize_t
)datasize
, data
);
1365 err
= TIFFReadDirEntryDataAndRealloc(tif
, (uint64_t)offset
,
1366 (tmsize_t
)datasize
, &data
);
1367 if (err
!= TIFFReadDirEntryErrOk
)
1369 _TIFFfreeExt(tif
, data
);
1375 return (TIFFReadDirEntryErrOk
);
1378 static enum TIFFReadDirEntryErr
1379 TIFFReadDirEntryArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint32_t *count
,
1380 uint32_t desttypesize
, void **value
)
1382 return TIFFReadDirEntryArrayWithLimit(tif
, direntry
, count
, desttypesize
,
1383 value
, ~((uint64_t)0));
1386 static enum TIFFReadDirEntryErr
1387 TIFFReadDirEntryByteArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint8_t **value
)
1389 enum TIFFReadDirEntryErr err
;
1393 switch (direntry
->tdir_type
)
1396 case TIFF_UNDEFINED
:
1407 return (TIFFReadDirEntryErrType
);
1409 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 1, &origdata
);
1410 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
1415 switch (direntry
->tdir_type
)
1418 case TIFF_UNDEFINED
:
1420 *value
= (uint8_t *)origdata
;
1421 return (TIFFReadDirEntryErrOk
);
1426 m
= (int8_t *)origdata
;
1427 for (n
= 0; n
< count
; n
++)
1429 err
= TIFFReadDirEntryCheckRangeByteSbyte(*m
);
1430 if (err
!= TIFFReadDirEntryErrOk
)
1432 _TIFFfreeExt(tif
, origdata
);
1437 *value
= (uint8_t *)origdata
;
1438 return (TIFFReadDirEntryErrOk
);
1441 data
= (uint8_t *)_TIFFmallocExt(tif
, count
);
1444 _TIFFfreeExt(tif
, origdata
);
1445 return (TIFFReadDirEntryErrAlloc
);
1447 switch (direntry
->tdir_type
)
1454 ma
= (uint16_t *)origdata
;
1456 for (n
= 0; n
< count
; n
++)
1458 if (tif
->tif_flags
& TIFF_SWAB
)
1460 err
= TIFFReadDirEntryCheckRangeByteShort(*ma
);
1461 if (err
!= TIFFReadDirEntryErrOk
)
1463 *mb
++ = (uint8_t)(*ma
++);
1472 ma
= (int16_t *)origdata
;
1474 for (n
= 0; n
< count
; n
++)
1476 if (tif
->tif_flags
& TIFF_SWAB
)
1477 TIFFSwabShort((uint16_t *)ma
);
1478 err
= TIFFReadDirEntryCheckRangeByteSshort(*ma
);
1479 if (err
!= TIFFReadDirEntryErrOk
)
1481 *mb
++ = (uint8_t)(*ma
++);
1490 ma
= (uint32_t *)origdata
;
1492 for (n
= 0; n
< count
; n
++)
1494 if (tif
->tif_flags
& TIFF_SWAB
)
1496 err
= TIFFReadDirEntryCheckRangeByteLong(*ma
);
1497 if (err
!= TIFFReadDirEntryErrOk
)
1499 *mb
++ = (uint8_t)(*ma
++);
1508 ma
= (int32_t *)origdata
;
1510 for (n
= 0; n
< count
; n
++)
1512 if (tif
->tif_flags
& TIFF_SWAB
)
1513 TIFFSwabLong((uint32_t *)ma
);
1514 err
= TIFFReadDirEntryCheckRangeByteSlong(*ma
);
1515 if (err
!= TIFFReadDirEntryErrOk
)
1517 *mb
++ = (uint8_t)(*ma
++);
1526 ma
= (uint64_t *)origdata
;
1528 for (n
= 0; n
< count
; n
++)
1530 if (tif
->tif_flags
& TIFF_SWAB
)
1532 err
= TIFFReadDirEntryCheckRangeByteLong8(*ma
);
1533 if (err
!= TIFFReadDirEntryErrOk
)
1535 *mb
++ = (uint8_t)(*ma
++);
1544 ma
= (int64_t *)origdata
;
1546 for (n
= 0; n
< count
; n
++)
1548 if (tif
->tif_flags
& TIFF_SWAB
)
1549 TIFFSwabLong8((uint64_t *)ma
);
1550 err
= TIFFReadDirEntryCheckRangeByteSlong8(*ma
);
1551 if (err
!= TIFFReadDirEntryErrOk
)
1553 *mb
++ = (uint8_t)(*ma
++);
1558 _TIFFfreeExt(tif
, origdata
);
1559 if (err
!= TIFFReadDirEntryErrOk
)
1561 _TIFFfreeExt(tif
, data
);
1565 return (TIFFReadDirEntryErrOk
);
1568 static enum TIFFReadDirEntryErr
1569 TIFFReadDirEntrySbyteArray(TIFF
*tif
, TIFFDirEntry
*direntry
, int8_t **value
)
1571 enum TIFFReadDirEntryErr err
;
1575 switch (direntry
->tdir_type
)
1577 case TIFF_UNDEFINED
:
1588 return (TIFFReadDirEntryErrType
);
1590 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 1, &origdata
);
1591 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
1596 switch (direntry
->tdir_type
)
1598 case TIFF_UNDEFINED
:
1603 m
= (uint8_t *)origdata
;
1604 for (n
= 0; n
< count
; n
++)
1606 err
= TIFFReadDirEntryCheckRangeSbyteByte(*m
);
1607 if (err
!= TIFFReadDirEntryErrOk
)
1609 _TIFFfreeExt(tif
, origdata
);
1614 *value
= (int8_t *)origdata
;
1615 return (TIFFReadDirEntryErrOk
);
1618 *value
= (int8_t *)origdata
;
1619 return (TIFFReadDirEntryErrOk
);
1621 data
= (int8_t *)_TIFFmallocExt(tif
, count
);
1624 _TIFFfreeExt(tif
, origdata
);
1625 return (TIFFReadDirEntryErrAlloc
);
1627 switch (direntry
->tdir_type
)
1634 ma
= (uint16_t *)origdata
;
1636 for (n
= 0; n
< count
; n
++)
1638 if (tif
->tif_flags
& TIFF_SWAB
)
1640 err
= TIFFReadDirEntryCheckRangeSbyteShort(*ma
);
1641 if (err
!= TIFFReadDirEntryErrOk
)
1643 *mb
++ = (int8_t)(*ma
++);
1652 ma
= (int16_t *)origdata
;
1654 for (n
= 0; n
< count
; n
++)
1656 if (tif
->tif_flags
& TIFF_SWAB
)
1657 TIFFSwabShort((uint16_t *)ma
);
1658 err
= TIFFReadDirEntryCheckRangeSbyteSshort(*ma
);
1659 if (err
!= TIFFReadDirEntryErrOk
)
1661 *mb
++ = (int8_t)(*ma
++);
1670 ma
= (uint32_t *)origdata
;
1672 for (n
= 0; n
< count
; n
++)
1674 if (tif
->tif_flags
& TIFF_SWAB
)
1676 err
= TIFFReadDirEntryCheckRangeSbyteLong(*ma
);
1677 if (err
!= TIFFReadDirEntryErrOk
)
1679 *mb
++ = (int8_t)(*ma
++);
1688 ma
= (int32_t *)origdata
;
1690 for (n
= 0; n
< count
; n
++)
1692 if (tif
->tif_flags
& TIFF_SWAB
)
1693 TIFFSwabLong((uint32_t *)ma
);
1694 err
= TIFFReadDirEntryCheckRangeSbyteSlong(*ma
);
1695 if (err
!= TIFFReadDirEntryErrOk
)
1697 *mb
++ = (int8_t)(*ma
++);
1706 ma
= (uint64_t *)origdata
;
1708 for (n
= 0; n
< count
; n
++)
1710 if (tif
->tif_flags
& TIFF_SWAB
)
1712 err
= TIFFReadDirEntryCheckRangeSbyteLong8(*ma
);
1713 if (err
!= TIFFReadDirEntryErrOk
)
1715 *mb
++ = (int8_t)(*ma
++);
1724 ma
= (int64_t *)origdata
;
1726 for (n
= 0; n
< count
; n
++)
1728 if (tif
->tif_flags
& TIFF_SWAB
)
1729 TIFFSwabLong8((uint64_t *)ma
);
1730 err
= TIFFReadDirEntryCheckRangeSbyteSlong8(*ma
);
1731 if (err
!= TIFFReadDirEntryErrOk
)
1733 *mb
++ = (int8_t)(*ma
++);
1738 _TIFFfreeExt(tif
, origdata
);
1739 if (err
!= TIFFReadDirEntryErrOk
)
1741 _TIFFfreeExt(tif
, data
);
1745 return (TIFFReadDirEntryErrOk
);
1748 static enum TIFFReadDirEntryErr
1749 TIFFReadDirEntryShortArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint16_t **value
)
1751 enum TIFFReadDirEntryErr err
;
1755 switch (direntry
->tdir_type
)
1767 return (TIFFReadDirEntryErrType
);
1769 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 2, &origdata
);
1770 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
1775 switch (direntry
->tdir_type
)
1778 *value
= (uint16_t *)origdata
;
1779 if (tif
->tif_flags
& TIFF_SWAB
)
1780 TIFFSwabArrayOfShort(*value
, count
);
1781 return (TIFFReadDirEntryErrOk
);
1786 m
= (int16_t *)origdata
;
1787 for (n
= 0; n
< count
; n
++)
1789 if (tif
->tif_flags
& TIFF_SWAB
)
1790 TIFFSwabShort((uint16_t *)m
);
1791 err
= TIFFReadDirEntryCheckRangeShortSshort(*m
);
1792 if (err
!= TIFFReadDirEntryErrOk
)
1794 _TIFFfreeExt(tif
, origdata
);
1799 *value
= (uint16_t *)origdata
;
1800 return (TIFFReadDirEntryErrOk
);
1803 data
= (uint16_t *)_TIFFmallocExt(tif
, count
* 2);
1806 _TIFFfreeExt(tif
, origdata
);
1807 return (TIFFReadDirEntryErrAlloc
);
1809 switch (direntry
->tdir_type
)
1816 ma
= (uint8_t *)origdata
;
1818 for (n
= 0; n
< count
; n
++)
1819 *mb
++ = (uint16_t)(*ma
++);
1827 ma
= (int8_t *)origdata
;
1829 for (n
= 0; n
< count
; n
++)
1831 err
= TIFFReadDirEntryCheckRangeShortSbyte(*ma
);
1832 if (err
!= TIFFReadDirEntryErrOk
)
1834 *mb
++ = (uint16_t)(*ma
++);
1843 ma
= (uint32_t *)origdata
;
1845 for (n
= 0; n
< count
; n
++)
1847 if (tif
->tif_flags
& TIFF_SWAB
)
1849 err
= TIFFReadDirEntryCheckRangeShortLong(*ma
);
1850 if (err
!= TIFFReadDirEntryErrOk
)
1852 *mb
++ = (uint16_t)(*ma
++);
1861 ma
= (int32_t *)origdata
;
1863 for (n
= 0; n
< count
; n
++)
1865 if (tif
->tif_flags
& TIFF_SWAB
)
1866 TIFFSwabLong((uint32_t *)ma
);
1867 err
= TIFFReadDirEntryCheckRangeShortSlong(*ma
);
1868 if (err
!= TIFFReadDirEntryErrOk
)
1870 *mb
++ = (uint16_t)(*ma
++);
1879 ma
= (uint64_t *)origdata
;
1881 for (n
= 0; n
< count
; n
++)
1883 if (tif
->tif_flags
& TIFF_SWAB
)
1885 err
= TIFFReadDirEntryCheckRangeShortLong8(*ma
);
1886 if (err
!= TIFFReadDirEntryErrOk
)
1888 *mb
++ = (uint16_t)(*ma
++);
1897 ma
= (int64_t *)origdata
;
1899 for (n
= 0; n
< count
; n
++)
1901 if (tif
->tif_flags
& TIFF_SWAB
)
1902 TIFFSwabLong8((uint64_t *)ma
);
1903 err
= TIFFReadDirEntryCheckRangeShortSlong8(*ma
);
1904 if (err
!= TIFFReadDirEntryErrOk
)
1906 *mb
++ = (uint16_t)(*ma
++);
1911 _TIFFfreeExt(tif
, origdata
);
1912 if (err
!= TIFFReadDirEntryErrOk
)
1914 _TIFFfreeExt(tif
, data
);
1918 return (TIFFReadDirEntryErrOk
);
1921 static enum TIFFReadDirEntryErr
1922 TIFFReadDirEntrySshortArray(TIFF
*tif
, TIFFDirEntry
*direntry
, int16_t **value
)
1924 enum TIFFReadDirEntryErr err
;
1928 switch (direntry
->tdir_type
)
1940 return (TIFFReadDirEntryErrType
);
1942 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 2, &origdata
);
1943 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
1948 switch (direntry
->tdir_type
)
1954 m
= (uint16_t *)origdata
;
1955 for (n
= 0; n
< count
; n
++)
1957 if (tif
->tif_flags
& TIFF_SWAB
)
1959 err
= TIFFReadDirEntryCheckRangeSshortShort(*m
);
1960 if (err
!= TIFFReadDirEntryErrOk
)
1962 _TIFFfreeExt(tif
, origdata
);
1967 *value
= (int16_t *)origdata
;
1968 return (TIFFReadDirEntryErrOk
);
1971 *value
= (int16_t *)origdata
;
1972 if (tif
->tif_flags
& TIFF_SWAB
)
1973 TIFFSwabArrayOfShort((uint16_t *)(*value
), count
);
1974 return (TIFFReadDirEntryErrOk
);
1976 data
= (int16_t *)_TIFFmallocExt(tif
, count
* 2);
1979 _TIFFfreeExt(tif
, origdata
);
1980 return (TIFFReadDirEntryErrAlloc
);
1982 switch (direntry
->tdir_type
)
1989 ma
= (uint8_t *)origdata
;
1991 for (n
= 0; n
< count
; n
++)
1992 *mb
++ = (int16_t)(*ma
++);
2000 ma
= (int8_t *)origdata
;
2002 for (n
= 0; n
< count
; n
++)
2003 *mb
++ = (int16_t)(*ma
++);
2011 ma
= (uint32_t *)origdata
;
2013 for (n
= 0; n
< count
; n
++)
2015 if (tif
->tif_flags
& TIFF_SWAB
)
2017 err
= TIFFReadDirEntryCheckRangeSshortLong(*ma
);
2018 if (err
!= TIFFReadDirEntryErrOk
)
2020 *mb
++ = (int16_t)(*ma
++);
2029 ma
= (int32_t *)origdata
;
2031 for (n
= 0; n
< count
; n
++)
2033 if (tif
->tif_flags
& TIFF_SWAB
)
2034 TIFFSwabLong((uint32_t *)ma
);
2035 err
= TIFFReadDirEntryCheckRangeSshortSlong(*ma
);
2036 if (err
!= TIFFReadDirEntryErrOk
)
2038 *mb
++ = (int16_t)(*ma
++);
2047 ma
= (uint64_t *)origdata
;
2049 for (n
= 0; n
< count
; n
++)
2051 if (tif
->tif_flags
& TIFF_SWAB
)
2053 err
= TIFFReadDirEntryCheckRangeSshortLong8(*ma
);
2054 if (err
!= TIFFReadDirEntryErrOk
)
2056 *mb
++ = (int16_t)(*ma
++);
2065 ma
= (int64_t *)origdata
;
2067 for (n
= 0; n
< count
; n
++)
2069 if (tif
->tif_flags
& TIFF_SWAB
)
2070 TIFFSwabLong8((uint64_t *)ma
);
2071 err
= TIFFReadDirEntryCheckRangeSshortSlong8(*ma
);
2072 if (err
!= TIFFReadDirEntryErrOk
)
2074 *mb
++ = (int16_t)(*ma
++);
2079 _TIFFfreeExt(tif
, origdata
);
2080 if (err
!= TIFFReadDirEntryErrOk
)
2082 _TIFFfreeExt(tif
, data
);
2086 return (TIFFReadDirEntryErrOk
);
2089 static enum TIFFReadDirEntryErr
2090 TIFFReadDirEntryLongArray(TIFF
*tif
, TIFFDirEntry
*direntry
, uint32_t **value
)
2092 enum TIFFReadDirEntryErr err
;
2096 switch (direntry
->tdir_type
)
2108 return (TIFFReadDirEntryErrType
);
2110 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 4, &origdata
);
2111 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
2116 switch (direntry
->tdir_type
)
2119 *value
= (uint32_t *)origdata
;
2120 if (tif
->tif_flags
& TIFF_SWAB
)
2121 TIFFSwabArrayOfLong(*value
, count
);
2122 return (TIFFReadDirEntryErrOk
);
2127 m
= (int32_t *)origdata
;
2128 for (n
= 0; n
< count
; n
++)
2130 if (tif
->tif_flags
& TIFF_SWAB
)
2131 TIFFSwabLong((uint32_t *)m
);
2132 err
= TIFFReadDirEntryCheckRangeLongSlong(*m
);
2133 if (err
!= TIFFReadDirEntryErrOk
)
2135 _TIFFfreeExt(tif
, origdata
);
2140 *value
= (uint32_t *)origdata
;
2141 return (TIFFReadDirEntryErrOk
);
2144 data
= (uint32_t *)_TIFFmallocExt(tif
, count
* 4);
2147 _TIFFfreeExt(tif
, origdata
);
2148 return (TIFFReadDirEntryErrAlloc
);
2150 switch (direntry
->tdir_type
)
2157 ma
= (uint8_t *)origdata
;
2159 for (n
= 0; n
< count
; n
++)
2160 *mb
++ = (uint32_t)(*ma
++);
2168 ma
= (int8_t *)origdata
;
2170 for (n
= 0; n
< count
; n
++)
2172 err
= TIFFReadDirEntryCheckRangeLongSbyte(*ma
);
2173 if (err
!= TIFFReadDirEntryErrOk
)
2175 *mb
++ = (uint32_t)(*ma
++);
2184 ma
= (uint16_t *)origdata
;
2186 for (n
= 0; n
< count
; n
++)
2188 if (tif
->tif_flags
& TIFF_SWAB
)
2190 *mb
++ = (uint32_t)(*ma
++);
2199 ma
= (int16_t *)origdata
;
2201 for (n
= 0; n
< count
; n
++)
2203 if (tif
->tif_flags
& TIFF_SWAB
)
2204 TIFFSwabShort((uint16_t *)ma
);
2205 err
= TIFFReadDirEntryCheckRangeLongSshort(*ma
);
2206 if (err
!= TIFFReadDirEntryErrOk
)
2208 *mb
++ = (uint32_t)(*ma
++);
2217 ma
= (uint64_t *)origdata
;
2219 for (n
= 0; n
< count
; n
++)
2221 if (tif
->tif_flags
& TIFF_SWAB
)
2223 err
= TIFFReadDirEntryCheckRangeLongLong8(*ma
);
2224 if (err
!= TIFFReadDirEntryErrOk
)
2226 *mb
++ = (uint32_t)(*ma
++);
2235 ma
= (int64_t *)origdata
;
2237 for (n
= 0; n
< count
; n
++)
2239 if (tif
->tif_flags
& TIFF_SWAB
)
2240 TIFFSwabLong8((uint64_t *)ma
);
2241 err
= TIFFReadDirEntryCheckRangeLongSlong8(*ma
);
2242 if (err
!= TIFFReadDirEntryErrOk
)
2244 *mb
++ = (uint32_t)(*ma
++);
2249 _TIFFfreeExt(tif
, origdata
);
2250 if (err
!= TIFFReadDirEntryErrOk
)
2252 _TIFFfreeExt(tif
, data
);
2256 return (TIFFReadDirEntryErrOk
);
2259 static enum TIFFReadDirEntryErr
2260 TIFFReadDirEntrySlongArray(TIFF
*tif
, TIFFDirEntry
*direntry
, int32_t **value
)
2262 enum TIFFReadDirEntryErr err
;
2266 switch (direntry
->tdir_type
)
2278 return (TIFFReadDirEntryErrType
);
2280 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 4, &origdata
);
2281 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
2286 switch (direntry
->tdir_type
)
2292 m
= (uint32_t *)origdata
;
2293 for (n
= 0; n
< count
; n
++)
2295 if (tif
->tif_flags
& TIFF_SWAB
)
2296 TIFFSwabLong((uint32_t *)m
);
2297 err
= TIFFReadDirEntryCheckRangeSlongLong(*m
);
2298 if (err
!= TIFFReadDirEntryErrOk
)
2300 _TIFFfreeExt(tif
, origdata
);
2305 *value
= (int32_t *)origdata
;
2306 return (TIFFReadDirEntryErrOk
);
2309 *value
= (int32_t *)origdata
;
2310 if (tif
->tif_flags
& TIFF_SWAB
)
2311 TIFFSwabArrayOfLong((uint32_t *)(*value
), count
);
2312 return (TIFFReadDirEntryErrOk
);
2314 data
= (int32_t *)_TIFFmallocExt(tif
, count
* 4);
2317 _TIFFfreeExt(tif
, origdata
);
2318 return (TIFFReadDirEntryErrAlloc
);
2320 switch (direntry
->tdir_type
)
2327 ma
= (uint8_t *)origdata
;
2329 for (n
= 0; n
< count
; n
++)
2330 *mb
++ = (int32_t)(*ma
++);
2338 ma
= (int8_t *)origdata
;
2340 for (n
= 0; n
< count
; n
++)
2341 *mb
++ = (int32_t)(*ma
++);
2349 ma
= (uint16_t *)origdata
;
2351 for (n
= 0; n
< count
; n
++)
2353 if (tif
->tif_flags
& TIFF_SWAB
)
2355 *mb
++ = (int32_t)(*ma
++);
2364 ma
= (int16_t *)origdata
;
2366 for (n
= 0; n
< count
; n
++)
2368 if (tif
->tif_flags
& TIFF_SWAB
)
2369 TIFFSwabShort((uint16_t *)ma
);
2370 *mb
++ = (int32_t)(*ma
++);
2379 ma
= (uint64_t *)origdata
;
2381 for (n
= 0; n
< count
; n
++)
2383 if (tif
->tif_flags
& TIFF_SWAB
)
2385 err
= TIFFReadDirEntryCheckRangeSlongLong8(*ma
);
2386 if (err
!= TIFFReadDirEntryErrOk
)
2388 *mb
++ = (int32_t)(*ma
++);
2397 ma
= (int64_t *)origdata
;
2399 for (n
= 0; n
< count
; n
++)
2401 if (tif
->tif_flags
& TIFF_SWAB
)
2402 TIFFSwabLong8((uint64_t *)ma
);
2403 err
= TIFFReadDirEntryCheckRangeSlongSlong8(*ma
);
2404 if (err
!= TIFFReadDirEntryErrOk
)
2406 *mb
++ = (int32_t)(*ma
++);
2411 _TIFFfreeExt(tif
, origdata
);
2412 if (err
!= TIFFReadDirEntryErrOk
)
2414 _TIFFfreeExt(tif
, data
);
2418 return (TIFFReadDirEntryErrOk
);
2421 static enum TIFFReadDirEntryErr
2422 TIFFReadDirEntryLong8ArrayWithLimit(TIFF
*tif
, TIFFDirEntry
*direntry
,
2423 uint64_t **value
, uint64_t maxcount
)
2425 enum TIFFReadDirEntryErr err
;
2429 switch (direntry
->tdir_type
)
2441 return (TIFFReadDirEntryErrType
);
2443 err
= TIFFReadDirEntryArrayWithLimit(tif
, direntry
, &count
, 8, &origdata
,
2445 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
2450 switch (direntry
->tdir_type
)
2453 *value
= (uint64_t *)origdata
;
2454 if (tif
->tif_flags
& TIFF_SWAB
)
2455 TIFFSwabArrayOfLong8(*value
, count
);
2456 return (TIFFReadDirEntryErrOk
);
2461 m
= (int64_t *)origdata
;
2462 for (n
= 0; n
< count
; n
++)
2464 if (tif
->tif_flags
& TIFF_SWAB
)
2465 TIFFSwabLong8((uint64_t *)m
);
2466 err
= TIFFReadDirEntryCheckRangeLong8Slong8(*m
);
2467 if (err
!= TIFFReadDirEntryErrOk
)
2469 _TIFFfreeExt(tif
, origdata
);
2474 *value
= (uint64_t *)origdata
;
2475 return (TIFFReadDirEntryErrOk
);
2478 data
= (uint64_t *)_TIFFmallocExt(tif
, count
* 8);
2481 _TIFFfreeExt(tif
, origdata
);
2482 return (TIFFReadDirEntryErrAlloc
);
2484 switch (direntry
->tdir_type
)
2491 ma
= (uint8_t *)origdata
;
2493 for (n
= 0; n
< count
; n
++)
2494 *mb
++ = (uint64_t)(*ma
++);
2502 ma
= (int8_t *)origdata
;
2504 for (n
= 0; n
< count
; n
++)
2506 err
= TIFFReadDirEntryCheckRangeLong8Sbyte(*ma
);
2507 if (err
!= TIFFReadDirEntryErrOk
)
2509 *mb
++ = (uint64_t)(*ma
++);
2518 ma
= (uint16_t *)origdata
;
2520 for (n
= 0; n
< count
; n
++)
2522 if (tif
->tif_flags
& TIFF_SWAB
)
2524 *mb
++ = (uint64_t)(*ma
++);
2533 ma
= (int16_t *)origdata
;
2535 for (n
= 0; n
< count
; n
++)
2537 if (tif
->tif_flags
& TIFF_SWAB
)
2538 TIFFSwabShort((uint16_t *)ma
);
2539 err
= TIFFReadDirEntryCheckRangeLong8Sshort(*ma
);
2540 if (err
!= TIFFReadDirEntryErrOk
)
2542 *mb
++ = (uint64_t)(*ma
++);
2551 ma
= (uint32_t *)origdata
;
2553 for (n
= 0; n
< count
; n
++)
2555 if (tif
->tif_flags
& TIFF_SWAB
)
2557 *mb
++ = (uint64_t)(*ma
++);
2566 ma
= (int32_t *)origdata
;
2568 for (n
= 0; n
< count
; n
++)
2570 if (tif
->tif_flags
& TIFF_SWAB
)
2571 TIFFSwabLong((uint32_t *)ma
);
2572 err
= TIFFReadDirEntryCheckRangeLong8Slong(*ma
);
2573 if (err
!= TIFFReadDirEntryErrOk
)
2575 *mb
++ = (uint64_t)(*ma
++);
2580 _TIFFfreeExt(tif
, origdata
);
2581 if (err
!= TIFFReadDirEntryErrOk
)
2583 _TIFFfreeExt(tif
, data
);
2587 return (TIFFReadDirEntryErrOk
);
2590 static enum TIFFReadDirEntryErr
2591 TIFFReadDirEntryLong8Array(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t **value
)
2593 return TIFFReadDirEntryLong8ArrayWithLimit(tif
, direntry
, value
,
2597 static enum TIFFReadDirEntryErr
2598 TIFFReadDirEntrySlong8Array(TIFF
*tif
, TIFFDirEntry
*direntry
, int64_t **value
)
2600 enum TIFFReadDirEntryErr err
;
2604 switch (direntry
->tdir_type
)
2616 return (TIFFReadDirEntryErrType
);
2618 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 8, &origdata
);
2619 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
2624 switch (direntry
->tdir_type
)
2630 m
= (uint64_t *)origdata
;
2631 for (n
= 0; n
< count
; n
++)
2633 if (tif
->tif_flags
& TIFF_SWAB
)
2635 err
= TIFFReadDirEntryCheckRangeSlong8Long8(*m
);
2636 if (err
!= TIFFReadDirEntryErrOk
)
2638 _TIFFfreeExt(tif
, origdata
);
2643 *value
= (int64_t *)origdata
;
2644 return (TIFFReadDirEntryErrOk
);
2647 *value
= (int64_t *)origdata
;
2648 if (tif
->tif_flags
& TIFF_SWAB
)
2649 TIFFSwabArrayOfLong8((uint64_t *)(*value
), count
);
2650 return (TIFFReadDirEntryErrOk
);
2652 data
= (int64_t *)_TIFFmallocExt(tif
, count
* 8);
2655 _TIFFfreeExt(tif
, origdata
);
2656 return (TIFFReadDirEntryErrAlloc
);
2658 switch (direntry
->tdir_type
)
2665 ma
= (uint8_t *)origdata
;
2667 for (n
= 0; n
< count
; n
++)
2668 *mb
++ = (int64_t)(*ma
++);
2676 ma
= (int8_t *)origdata
;
2678 for (n
= 0; n
< count
; n
++)
2679 *mb
++ = (int64_t)(*ma
++);
2687 ma
= (uint16_t *)origdata
;
2689 for (n
= 0; n
< count
; n
++)
2691 if (tif
->tif_flags
& TIFF_SWAB
)
2693 *mb
++ = (int64_t)(*ma
++);
2702 ma
= (int16_t *)origdata
;
2704 for (n
= 0; n
< count
; n
++)
2706 if (tif
->tif_flags
& TIFF_SWAB
)
2707 TIFFSwabShort((uint16_t *)ma
);
2708 *mb
++ = (int64_t)(*ma
++);
2717 ma
= (uint32_t *)origdata
;
2719 for (n
= 0; n
< count
; n
++)
2721 if (tif
->tif_flags
& TIFF_SWAB
)
2723 *mb
++ = (int64_t)(*ma
++);
2732 ma
= (int32_t *)origdata
;
2734 for (n
= 0; n
< count
; n
++)
2736 if (tif
->tif_flags
& TIFF_SWAB
)
2737 TIFFSwabLong((uint32_t *)ma
);
2738 *mb
++ = (int64_t)(*ma
++);
2743 _TIFFfreeExt(tif
, origdata
);
2745 return (TIFFReadDirEntryErrOk
);
2748 static enum TIFFReadDirEntryErr
2749 TIFFReadDirEntryFloatArray(TIFF
*tif
, TIFFDirEntry
*direntry
, float **value
)
2751 enum TIFFReadDirEntryErr err
;
2755 switch (direntry
->tdir_type
)
2766 case TIFF_SRATIONAL
:
2771 return (TIFFReadDirEntryErrType
);
2773 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 4, &origdata
);
2774 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
2779 switch (direntry
->tdir_type
)
2782 if (tif
->tif_flags
& TIFF_SWAB
)
2783 TIFFSwabArrayOfLong((uint32_t *)origdata
, count
);
2784 TIFFCvtIEEEDoubleToNative(tif
, count
, (float *)origdata
);
2785 *value
= (float *)origdata
;
2786 return (TIFFReadDirEntryErrOk
);
2788 data
= (float *)_TIFFmallocExt(tif
, count
* sizeof(float));
2791 _TIFFfreeExt(tif
, origdata
);
2792 return (TIFFReadDirEntryErrAlloc
);
2794 switch (direntry
->tdir_type
)
2801 ma
= (uint8_t *)origdata
;
2803 for (n
= 0; n
< count
; n
++)
2804 *mb
++ = (float)(*ma
++);
2812 ma
= (int8_t *)origdata
;
2814 for (n
= 0; n
< count
; n
++)
2815 *mb
++ = (float)(*ma
++);
2823 ma
= (uint16_t *)origdata
;
2825 for (n
= 0; n
< count
; n
++)
2827 if (tif
->tif_flags
& TIFF_SWAB
)
2829 *mb
++ = (float)(*ma
++);
2838 ma
= (int16_t *)origdata
;
2840 for (n
= 0; n
< count
; n
++)
2842 if (tif
->tif_flags
& TIFF_SWAB
)
2843 TIFFSwabShort((uint16_t *)ma
);
2844 *mb
++ = (float)(*ma
++);
2853 ma
= (uint32_t *)origdata
;
2855 for (n
= 0; n
< count
; n
++)
2857 if (tif
->tif_flags
& TIFF_SWAB
)
2859 *mb
++ = (float)(*ma
++);
2868 ma
= (int32_t *)origdata
;
2870 for (n
= 0; n
< count
; n
++)
2872 if (tif
->tif_flags
& TIFF_SWAB
)
2873 TIFFSwabLong((uint32_t *)ma
);
2874 *mb
++ = (float)(*ma
++);
2883 ma
= (uint64_t *)origdata
;
2885 for (n
= 0; n
< count
; n
++)
2887 if (tif
->tif_flags
& TIFF_SWAB
)
2889 #if defined(__WIN32__) && (_MSC_VER < 1500)
2891 * XXX: MSVC 6.0 does not support
2892 * conversion of 64-bit integers into
2893 * floating point values.
2895 *mb
++ = _TIFFUInt64ToFloat(*ma
++);
2897 *mb
++ = (float)(*ma
++);
2907 ma
= (int64_t *)origdata
;
2909 for (n
= 0; n
< count
; n
++)
2911 if (tif
->tif_flags
& TIFF_SWAB
)
2912 TIFFSwabLong8((uint64_t *)ma
);
2913 *mb
++ = (float)(*ma
++);
2924 ma
= (uint32_t *)origdata
;
2926 for (n
= 0; n
< count
; n
++)
2928 if (tif
->tif_flags
& TIFF_SWAB
)
2931 if (tif
->tif_flags
& TIFF_SWAB
)
2937 *mb
++ = (float)maa
/ (float)mab
;
2941 case TIFF_SRATIONAL
:
2948 ma
= (uint32_t *)origdata
;
2950 for (n
= 0; n
< count
; n
++)
2952 if (tif
->tif_flags
& TIFF_SWAB
)
2954 maa
= *(int32_t *)ma
;
2956 if (tif
->tif_flags
& TIFF_SWAB
)
2962 *mb
++ = (float)maa
/ (float)mab
;
2971 if (tif
->tif_flags
& TIFF_SWAB
)
2972 TIFFSwabArrayOfLong8((uint64_t *)origdata
, count
);
2973 TIFFCvtIEEEDoubleToNative(tif
, count
, (double *)origdata
);
2974 ma
= (double *)origdata
;
2976 for (n
= 0; n
< count
; n
++)
2981 else if (val
< -FLT_MAX
)
2988 _TIFFfreeExt(tif
, origdata
);
2990 return (TIFFReadDirEntryErrOk
);
2993 static enum TIFFReadDirEntryErr
2994 TIFFReadDirEntryDoubleArray(TIFF
*tif
, TIFFDirEntry
*direntry
, double **value
)
2996 enum TIFFReadDirEntryErr err
;
3000 switch (direntry
->tdir_type
)
3011 case TIFF_SRATIONAL
:
3016 return (TIFFReadDirEntryErrType
);
3018 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 8, &origdata
);
3019 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
3024 switch (direntry
->tdir_type
)
3027 if (tif
->tif_flags
& TIFF_SWAB
)
3028 TIFFSwabArrayOfLong8((uint64_t *)origdata
, count
);
3029 TIFFCvtIEEEDoubleToNative(tif
, count
, (double *)origdata
);
3030 *value
= (double *)origdata
;
3031 return (TIFFReadDirEntryErrOk
);
3033 data
= (double *)_TIFFmallocExt(tif
, count
* sizeof(double));
3036 _TIFFfreeExt(tif
, origdata
);
3037 return (TIFFReadDirEntryErrAlloc
);
3039 switch (direntry
->tdir_type
)
3046 ma
= (uint8_t *)origdata
;
3048 for (n
= 0; n
< count
; n
++)
3049 *mb
++ = (double)(*ma
++);
3057 ma
= (int8_t *)origdata
;
3059 for (n
= 0; n
< count
; n
++)
3060 *mb
++ = (double)(*ma
++);
3068 ma
= (uint16_t *)origdata
;
3070 for (n
= 0; n
< count
; n
++)
3072 if (tif
->tif_flags
& TIFF_SWAB
)
3074 *mb
++ = (double)(*ma
++);
3083 ma
= (int16_t *)origdata
;
3085 for (n
= 0; n
< count
; n
++)
3087 if (tif
->tif_flags
& TIFF_SWAB
)
3088 TIFFSwabShort((uint16_t *)ma
);
3089 *mb
++ = (double)(*ma
++);
3098 ma
= (uint32_t *)origdata
;
3100 for (n
= 0; n
< count
; n
++)
3102 if (tif
->tif_flags
& TIFF_SWAB
)
3104 *mb
++ = (double)(*ma
++);
3113 ma
= (int32_t *)origdata
;
3115 for (n
= 0; n
< count
; n
++)
3117 if (tif
->tif_flags
& TIFF_SWAB
)
3118 TIFFSwabLong((uint32_t *)ma
);
3119 *mb
++ = (double)(*ma
++);
3128 ma
= (uint64_t *)origdata
;
3130 for (n
= 0; n
< count
; n
++)
3132 if (tif
->tif_flags
& TIFF_SWAB
)
3134 #if defined(__WIN32__) && (_MSC_VER < 1500)
3136 * XXX: MSVC 6.0 does not support
3137 * conversion of 64-bit integers into
3138 * floating point values.
3140 *mb
++ = _TIFFUInt64ToDouble(*ma
++);
3142 *mb
++ = (double)(*ma
++);
3152 ma
= (int64_t *)origdata
;
3154 for (n
= 0; n
< count
; n
++)
3156 if (tif
->tif_flags
& TIFF_SWAB
)
3157 TIFFSwabLong8((uint64_t *)ma
);
3158 *mb
++ = (double)(*ma
++);
3169 ma
= (uint32_t *)origdata
;
3171 for (n
= 0; n
< count
; n
++)
3173 if (tif
->tif_flags
& TIFF_SWAB
)
3176 if (tif
->tif_flags
& TIFF_SWAB
)
3182 *mb
++ = (double)maa
/ (double)mab
;
3186 case TIFF_SRATIONAL
:
3193 ma
= (uint32_t *)origdata
;
3195 for (n
= 0; n
< count
; n
++)
3197 if (tif
->tif_flags
& TIFF_SWAB
)
3199 maa
= *(int32_t *)ma
;
3201 if (tif
->tif_flags
& TIFF_SWAB
)
3207 *mb
++ = (double)maa
/ (double)mab
;
3216 if (tif
->tif_flags
& TIFF_SWAB
)
3217 TIFFSwabArrayOfLong((uint32_t *)origdata
, count
);
3218 TIFFCvtIEEEFloatToNative(tif
, count
, (float *)origdata
);
3219 ma
= (float *)origdata
;
3221 for (n
= 0; n
< count
; n
++)
3222 *mb
++ = (double)(*ma
++);
3226 _TIFFfreeExt(tif
, origdata
);
3228 return (TIFFReadDirEntryErrOk
);
3231 static enum TIFFReadDirEntryErr
3232 TIFFReadDirEntryIfd8Array(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t **value
)
3234 enum TIFFReadDirEntryErr err
;
3238 switch (direntry
->tdir_type
)
3246 return (TIFFReadDirEntryErrType
);
3248 err
= TIFFReadDirEntryArray(tif
, direntry
, &count
, 8, &origdata
);
3249 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
3254 switch (direntry
->tdir_type
)
3258 *value
= (uint64_t *)origdata
;
3259 if (tif
->tif_flags
& TIFF_SWAB
)
3260 TIFFSwabArrayOfLong8(*value
, count
);
3261 return (TIFFReadDirEntryErrOk
);
3263 data
= (uint64_t *)_TIFFmallocExt(tif
, count
* 8);
3266 _TIFFfreeExt(tif
, origdata
);
3267 return (TIFFReadDirEntryErrAlloc
);
3269 switch (direntry
->tdir_type
)
3277 ma
= (uint32_t *)origdata
;
3279 for (n
= 0; n
< count
; n
++)
3281 if (tif
->tif_flags
& TIFF_SWAB
)
3283 *mb
++ = (uint64_t)(*ma
++);
3288 _TIFFfreeExt(tif
, origdata
);
3290 return (TIFFReadDirEntryErrOk
);
3293 static enum TIFFReadDirEntryErr
3294 TIFFReadDirEntryPersampleShort(TIFF
*tif
, TIFFDirEntry
*direntry
,
3297 enum TIFFReadDirEntryErr err
;
3301 if (direntry
->tdir_count
< (uint64_t)tif
->tif_dir
.td_samplesperpixel
)
3302 return (TIFFReadDirEntryErrCount
);
3303 err
= TIFFReadDirEntryShortArray(tif
, direntry
, &m
);
3304 if (err
!= TIFFReadDirEntryErrOk
|| m
== NULL
)
3307 nb
= tif
->tif_dir
.td_samplesperpixel
;
3312 if (*na
++ != *value
)
3314 err
= TIFFReadDirEntryErrPsdif
;
3319 _TIFFfreeExt(tif
, m
);
3323 static void TIFFReadDirEntryCheckedByte(TIFF
*tif
, TIFFDirEntry
*direntry
,
3327 *value
= *(uint8_t *)(&direntry
->tdir_offset
);
3330 static void TIFFReadDirEntryCheckedSbyte(TIFF
*tif
, TIFFDirEntry
*direntry
,
3334 *value
= *(int8_t *)(&direntry
->tdir_offset
);
3337 static void TIFFReadDirEntryCheckedShort(TIFF
*tif
, TIFFDirEntry
*direntry
,
3340 *value
= direntry
->tdir_offset
.toff_short
;
3341 /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3342 if (tif
->tif_flags
& TIFF_SWAB
)
3343 TIFFSwabShort(value
);
3346 static void TIFFReadDirEntryCheckedSshort(TIFF
*tif
, TIFFDirEntry
*direntry
,
3349 *value
= *(int16_t *)(&direntry
->tdir_offset
);
3350 if (tif
->tif_flags
& TIFF_SWAB
)
3351 TIFFSwabShort((uint16_t *)value
);
3354 static void TIFFReadDirEntryCheckedLong(TIFF
*tif
, TIFFDirEntry
*direntry
,
3357 *value
= *(uint32_t *)(&direntry
->tdir_offset
);
3358 if (tif
->tif_flags
& TIFF_SWAB
)
3359 TIFFSwabLong(value
);
3362 static void TIFFReadDirEntryCheckedSlong(TIFF
*tif
, TIFFDirEntry
*direntry
,
3365 *value
= *(int32_t *)(&direntry
->tdir_offset
);
3366 if (tif
->tif_flags
& TIFF_SWAB
)
3367 TIFFSwabLong((uint32_t *)value
);
3370 static enum TIFFReadDirEntryErr
3371 TIFFReadDirEntryCheckedLong8(TIFF
*tif
, TIFFDirEntry
*direntry
, uint64_t *value
)
3373 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
3375 enum TIFFReadDirEntryErr err
;
3376 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3377 if (tif
->tif_flags
& TIFF_SWAB
)
3378 TIFFSwabLong(&offset
);
3379 err
= TIFFReadDirEntryData(tif
, offset
, 8, value
);
3380 if (err
!= TIFFReadDirEntryErrOk
)
3384 *value
= direntry
->tdir_offset
.toff_long8
;
3385 if (tif
->tif_flags
& TIFF_SWAB
)
3386 TIFFSwabLong8(value
);
3387 return (TIFFReadDirEntryErrOk
);
3390 static enum TIFFReadDirEntryErr
3391 TIFFReadDirEntryCheckedSlong8(TIFF
*tif
, TIFFDirEntry
*direntry
, int64_t *value
)
3393 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
3395 enum TIFFReadDirEntryErr err
;
3396 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3397 if (tif
->tif_flags
& TIFF_SWAB
)
3398 TIFFSwabLong(&offset
);
3399 err
= TIFFReadDirEntryData(tif
, offset
, 8, value
);
3400 if (err
!= TIFFReadDirEntryErrOk
)
3404 *value
= *(int64_t *)(&direntry
->tdir_offset
);
3405 if (tif
->tif_flags
& TIFF_SWAB
)
3406 TIFFSwabLong8((uint64_t *)value
);
3407 return (TIFFReadDirEntryErrOk
);
3410 static enum TIFFReadDirEntryErr
3411 TIFFReadDirEntryCheckedRational(TIFF
*tif
, TIFFDirEntry
*direntry
,
3416 assert(sizeof(double) == 8);
3417 assert(sizeof(uint64_t) == 8);
3418 assert(sizeof(uint32_t) == 4);
3419 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
3421 enum TIFFReadDirEntryErr err
;
3422 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3423 if (tif
->tif_flags
& TIFF_SWAB
)
3424 TIFFSwabLong(&offset
);
3425 err
= TIFFReadDirEntryData(tif
, offset
, 8, m
.i
);
3426 if (err
!= TIFFReadDirEntryErrOk
)
3430 m
.l
= direntry
->tdir_offset
.toff_long8
;
3431 if (tif
->tif_flags
& TIFF_SWAB
)
3432 TIFFSwabArrayOfLong(m
.i
, 2);
3433 /* Not completely sure what we should do when m.i[1]==0, but some */
3434 /* sanitizers do not like division by 0.0: */
3435 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3436 if (m
.i
[0] == 0 || m
.i
[1] == 0)
3439 *value
= (double)m
.i
[0] / (double)m
.i
[1];
3440 return (TIFFReadDirEntryErrOk
);
3443 static enum TIFFReadDirEntryErr
3444 TIFFReadDirEntryCheckedSrational(TIFF
*tif
, TIFFDirEntry
*direntry
,
3448 assert(sizeof(double) == 8);
3449 assert(sizeof(uint64_t) == 8);
3450 assert(sizeof(int32_t) == 4);
3451 assert(sizeof(uint32_t) == 4);
3452 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
3454 enum TIFFReadDirEntryErr err
;
3455 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3456 if (tif
->tif_flags
& TIFF_SWAB
)
3457 TIFFSwabLong(&offset
);
3458 err
= TIFFReadDirEntryData(tif
, offset
, 8, m
.i
);
3459 if (err
!= TIFFReadDirEntryErrOk
)
3463 m
.l
= direntry
->tdir_offset
.toff_long8
;
3464 if (tif
->tif_flags
& TIFF_SWAB
)
3465 TIFFSwabArrayOfLong(m
.i
, 2);
3466 /* Not completely sure what we should do when m.i[1]==0, but some */
3467 /* sanitizers do not like division by 0.0: */
3468 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3469 if ((int32_t)m
.i
[0] == 0 || m
.i
[1] == 0)
3472 *value
= (double)((int32_t)m
.i
[0]) / (double)m
.i
[1];
3473 return (TIFFReadDirEntryErrOk
);
3477 static enum TIFFReadDirEntryErr
3478 TIFFReadDirEntryCheckedRationalDirect(TIFF
*tif
, TIFFDirEntry
*direntry
,
3479 TIFFRational_t
*value
)
3480 { /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
3484 assert(sizeof(double) == 8);
3485 assert(sizeof(uint64_t) == 8);
3486 assert(sizeof(uint32_t) == 4);
3488 if (direntry
->tdir_count
!= 1)
3489 return (TIFFReadDirEntryErrCount
);
3491 if (direntry
->tdir_type
!= TIFF_RATIONAL
&&
3492 direntry
->tdir_type
!= TIFF_SRATIONAL
)
3493 return (TIFFReadDirEntryErrType
);
3495 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
3497 enum TIFFReadDirEntryErr err
;
3498 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3499 if (tif
->tif_flags
& TIFF_SWAB
)
3500 TIFFSwabLong(&offset
);
3501 err
= TIFFReadDirEntryData(tif
, offset
, 8, m
.i
);
3502 if (err
!= TIFFReadDirEntryErrOk
)
3507 m
.l
= direntry
->tdir_offset
.toff_long8
;
3510 if (tif
->tif_flags
& TIFF_SWAB
)
3511 TIFFSwabArrayOfLong(m
.i
, 2);
3513 value
->uNum
= m
.i
[0];
3514 value
->uDenom
= m
.i
[1];
3515 return (TIFFReadDirEntryErrOk
);
3516 } /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
3519 static void TIFFReadDirEntryCheckedFloat(TIFF
*tif
, TIFFDirEntry
*direntry
,
3527 assert(sizeof(float) == 4);
3528 assert(sizeof(uint32_t) == 4);
3529 assert(sizeof(float_union
) == 4);
3530 float_union
.i
= *(uint32_t *)(&direntry
->tdir_offset
);
3531 *value
= float_union
.f
;
3532 if (tif
->tif_flags
& TIFF_SWAB
)
3533 TIFFSwabLong((uint32_t *)value
);
3536 static enum TIFFReadDirEntryErr
3537 TIFFReadDirEntryCheckedDouble(TIFF
*tif
, TIFFDirEntry
*direntry
, double *value
)
3539 assert(sizeof(double) == 8);
3540 assert(sizeof(uint64_t) == 8);
3541 assert(sizeof(UInt64Aligned_t
) == 8);
3542 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
3544 enum TIFFReadDirEntryErr err
;
3545 uint32_t offset
= direntry
->tdir_offset
.toff_long
;
3546 if (tif
->tif_flags
& TIFF_SWAB
)
3547 TIFFSwabLong(&offset
);
3548 err
= TIFFReadDirEntryData(tif
, offset
, 8, value
);
3549 if (err
!= TIFFReadDirEntryErrOk
)
3554 UInt64Aligned_t uint64_union
;
3555 uint64_union
.l
= direntry
->tdir_offset
.toff_long8
;
3556 *value
= uint64_union
.d
;
3558 if (tif
->tif_flags
& TIFF_SWAB
)
3559 TIFFSwabLong8((uint64_t *)value
);
3560 return (TIFFReadDirEntryErrOk
);
3563 static enum TIFFReadDirEntryErr
3564 TIFFReadDirEntryCheckRangeByteSbyte(int8_t value
)
3567 return (TIFFReadDirEntryErrRange
);
3569 return (TIFFReadDirEntryErrOk
);
3572 static enum TIFFReadDirEntryErr
3573 TIFFReadDirEntryCheckRangeByteShort(uint16_t value
)
3576 return (TIFFReadDirEntryErrRange
);
3578 return (TIFFReadDirEntryErrOk
);
3581 static enum TIFFReadDirEntryErr
3582 TIFFReadDirEntryCheckRangeByteSshort(int16_t value
)
3584 if ((value
< 0) || (value
> 0xFF))
3585 return (TIFFReadDirEntryErrRange
);
3587 return (TIFFReadDirEntryErrOk
);
3590 static enum TIFFReadDirEntryErr
3591 TIFFReadDirEntryCheckRangeByteLong(uint32_t value
)
3594 return (TIFFReadDirEntryErrRange
);
3596 return (TIFFReadDirEntryErrOk
);
3599 static enum TIFFReadDirEntryErr
3600 TIFFReadDirEntryCheckRangeByteSlong(int32_t value
)
3602 if ((value
< 0) || (value
> 0xFF))
3603 return (TIFFReadDirEntryErrRange
);
3605 return (TIFFReadDirEntryErrOk
);
3608 static enum TIFFReadDirEntryErr
3609 TIFFReadDirEntryCheckRangeByteLong8(uint64_t value
)
3612 return (TIFFReadDirEntryErrRange
);
3614 return (TIFFReadDirEntryErrOk
);
3617 static enum TIFFReadDirEntryErr
3618 TIFFReadDirEntryCheckRangeByteSlong8(int64_t value
)
3620 if ((value
< 0) || (value
> 0xFF))
3621 return (TIFFReadDirEntryErrRange
);
3623 return (TIFFReadDirEntryErrOk
);
3626 static enum TIFFReadDirEntryErr
3627 TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value
)
3630 return (TIFFReadDirEntryErrRange
);
3632 return (TIFFReadDirEntryErrOk
);
3635 static enum TIFFReadDirEntryErr
3636 TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value
)
3639 return (TIFFReadDirEntryErrRange
);
3641 return (TIFFReadDirEntryErrOk
);
3644 static enum TIFFReadDirEntryErr
3645 TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value
)
3647 if ((value
< -0x80) || (value
> 0x7F))
3648 return (TIFFReadDirEntryErrRange
);
3650 return (TIFFReadDirEntryErrOk
);
3653 static enum TIFFReadDirEntryErr
3654 TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value
)
3657 return (TIFFReadDirEntryErrRange
);
3659 return (TIFFReadDirEntryErrOk
);
3662 static enum TIFFReadDirEntryErr
3663 TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value
)
3665 if ((value
< -0x80) || (value
> 0x7F))
3666 return (TIFFReadDirEntryErrRange
);
3668 return (TIFFReadDirEntryErrOk
);
3671 static enum TIFFReadDirEntryErr
3672 TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value
)
3675 return (TIFFReadDirEntryErrRange
);
3677 return (TIFFReadDirEntryErrOk
);
3680 static enum TIFFReadDirEntryErr
3681 TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value
)
3683 if ((value
< -0x80) || (value
> 0x7F))
3684 return (TIFFReadDirEntryErrRange
);
3686 return (TIFFReadDirEntryErrOk
);
3689 static enum TIFFReadDirEntryErr
3690 TIFFReadDirEntryCheckRangeShortSbyte(int8_t value
)
3693 return (TIFFReadDirEntryErrRange
);
3695 return (TIFFReadDirEntryErrOk
);
3698 static enum TIFFReadDirEntryErr
3699 TIFFReadDirEntryCheckRangeShortSshort(int16_t value
)
3702 return (TIFFReadDirEntryErrRange
);
3704 return (TIFFReadDirEntryErrOk
);
3707 static enum TIFFReadDirEntryErr
3708 TIFFReadDirEntryCheckRangeShortLong(uint32_t value
)
3711 return (TIFFReadDirEntryErrRange
);
3713 return (TIFFReadDirEntryErrOk
);
3716 static enum TIFFReadDirEntryErr
3717 TIFFReadDirEntryCheckRangeShortSlong(int32_t value
)
3719 if ((value
< 0) || (value
> 0xFFFF))
3720 return (TIFFReadDirEntryErrRange
);
3722 return (TIFFReadDirEntryErrOk
);
3725 static enum TIFFReadDirEntryErr
3726 TIFFReadDirEntryCheckRangeShortLong8(uint64_t value
)
3729 return (TIFFReadDirEntryErrRange
);
3731 return (TIFFReadDirEntryErrOk
);
3734 static enum TIFFReadDirEntryErr
3735 TIFFReadDirEntryCheckRangeShortSlong8(int64_t value
)
3737 if ((value
< 0) || (value
> 0xFFFF))
3738 return (TIFFReadDirEntryErrRange
);
3740 return (TIFFReadDirEntryErrOk
);
3743 static enum TIFFReadDirEntryErr
3744 TIFFReadDirEntryCheckRangeSshortShort(uint16_t value
)
3747 return (TIFFReadDirEntryErrRange
);
3749 return (TIFFReadDirEntryErrOk
);
3752 static enum TIFFReadDirEntryErr
3753 TIFFReadDirEntryCheckRangeSshortLong(uint32_t value
)
3756 return (TIFFReadDirEntryErrRange
);
3758 return (TIFFReadDirEntryErrOk
);
3761 static enum TIFFReadDirEntryErr
3762 TIFFReadDirEntryCheckRangeSshortSlong(int32_t value
)
3764 if ((value
< -0x8000) || (value
> 0x7FFF))
3765 return (TIFFReadDirEntryErrRange
);
3767 return (TIFFReadDirEntryErrOk
);
3770 static enum TIFFReadDirEntryErr
3771 TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value
)
3774 return (TIFFReadDirEntryErrRange
);
3776 return (TIFFReadDirEntryErrOk
);
3779 static enum TIFFReadDirEntryErr
3780 TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value
)
3782 if ((value
< -0x8000) || (value
> 0x7FFF))
3783 return (TIFFReadDirEntryErrRange
);
3785 return (TIFFReadDirEntryErrOk
);
3788 static enum TIFFReadDirEntryErr
3789 TIFFReadDirEntryCheckRangeLongSbyte(int8_t value
)
3792 return (TIFFReadDirEntryErrRange
);
3794 return (TIFFReadDirEntryErrOk
);
3797 static enum TIFFReadDirEntryErr
3798 TIFFReadDirEntryCheckRangeLongSshort(int16_t value
)
3801 return (TIFFReadDirEntryErrRange
);
3803 return (TIFFReadDirEntryErrOk
);
3806 static enum TIFFReadDirEntryErr
3807 TIFFReadDirEntryCheckRangeLongSlong(int32_t value
)
3810 return (TIFFReadDirEntryErrRange
);
3812 return (TIFFReadDirEntryErrOk
);
3815 static enum TIFFReadDirEntryErr
3816 TIFFReadDirEntryCheckRangeLongLong8(uint64_t value
)
3818 if (value
> UINT32_MAX
)
3819 return (TIFFReadDirEntryErrRange
);
3821 return (TIFFReadDirEntryErrOk
);
3824 static enum TIFFReadDirEntryErr
3825 TIFFReadDirEntryCheckRangeLongSlong8(int64_t value
)
3827 if ((value
< 0) || (value
> (int64_t)UINT32_MAX
))
3828 return (TIFFReadDirEntryErrRange
);
3830 return (TIFFReadDirEntryErrOk
);
3833 static enum TIFFReadDirEntryErr
3834 TIFFReadDirEntryCheckRangeSlongLong(uint32_t value
)
3836 if (value
> 0x7FFFFFFFUL
)
3837 return (TIFFReadDirEntryErrRange
);
3839 return (TIFFReadDirEntryErrOk
);
3842 /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3843 static enum TIFFReadDirEntryErr
3844 TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value
)
3846 if (value
> 0x7FFFFFFF)
3847 return (TIFFReadDirEntryErrRange
);
3849 return (TIFFReadDirEntryErrOk
);
3852 /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3853 static enum TIFFReadDirEntryErr
3854 TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value
)
3856 if ((value
< 0 - ((int64_t)0x7FFFFFFF + 1)) || (value
> 0x7FFFFFFF))
3857 return (TIFFReadDirEntryErrRange
);
3859 return (TIFFReadDirEntryErrOk
);
3862 static enum TIFFReadDirEntryErr
3863 TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value
)
3866 return (TIFFReadDirEntryErrRange
);
3868 return (TIFFReadDirEntryErrOk
);
3871 static enum TIFFReadDirEntryErr
3872 TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value
)
3875 return (TIFFReadDirEntryErrRange
);
3877 return (TIFFReadDirEntryErrOk
);
3880 static enum TIFFReadDirEntryErr
3881 TIFFReadDirEntryCheckRangeLong8Slong(int32_t value
)
3884 return (TIFFReadDirEntryErrRange
);
3886 return (TIFFReadDirEntryErrOk
);
3889 static enum TIFFReadDirEntryErr
3890 TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value
)
3893 return (TIFFReadDirEntryErrRange
);
3895 return (TIFFReadDirEntryErrOk
);
3898 static enum TIFFReadDirEntryErr
3899 TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value
)
3901 if (value
> INT64_MAX
)
3902 return (TIFFReadDirEntryErrRange
);
3904 return (TIFFReadDirEntryErrOk
);
3907 static enum TIFFReadDirEntryErr
TIFFReadDirEntryData(TIFF
*tif
, uint64_t offset
,
3908 tmsize_t size
, void *dest
)
3913 if (!SeekOK(tif
, offset
))
3914 return (TIFFReadDirEntryErrIo
);
3915 if (!ReadOK(tif
, dest
, size
))
3916 return (TIFFReadDirEntryErrIo
);
3921 ma
= (size_t)offset
;
3922 if ((uint64_t)ma
!= offset
|| ma
> (~(size_t)0) - (size_t)size
)
3924 return TIFFReadDirEntryErrIo
;
3927 if (mb
> (uint64_t)tif
->tif_size
)
3928 return (TIFFReadDirEntryErrIo
);
3929 _TIFFmemcpy(dest
, tif
->tif_base
+ ma
, size
);
3931 return (TIFFReadDirEntryErrOk
);
3934 static void TIFFReadDirEntryOutputErr(TIFF
*tif
, enum TIFFReadDirEntryErr err
,
3935 const char *module
, const char *tagname
,
3942 case TIFFReadDirEntryErrCount
:
3943 TIFFErrorExtR(tif
, module
, "Incorrect count for \"%s\"",
3946 case TIFFReadDirEntryErrType
:
3947 TIFFErrorExtR(tif
, module
, "Incompatible type for \"%s\"",
3950 case TIFFReadDirEntryErrIo
:
3951 TIFFErrorExtR(tif
, module
, "IO error during reading of \"%s\"",
3954 case TIFFReadDirEntryErrRange
:
3955 TIFFErrorExtR(tif
, module
, "Incorrect value for \"%s\"",
3958 case TIFFReadDirEntryErrPsdif
:
3961 "Cannot handle different values per sample for \"%s\"",
3964 case TIFFReadDirEntryErrSizesan
:
3965 TIFFErrorExtR(tif
, module
,
3966 "Sanity check on size of \"%s\" value failed",
3969 case TIFFReadDirEntryErrAlloc
:
3970 TIFFErrorExtR(tif
, module
, "Out of memory reading of \"%s\"",
3974 assert(0); /* we should never get here */
3982 case TIFFReadDirEntryErrCount
:
3983 TIFFWarningExtR(tif
, module
,
3984 "Incorrect count for \"%s\"; tag ignored",
3987 case TIFFReadDirEntryErrType
:
3988 TIFFWarningExtR(tif
, module
,
3989 "Incompatible type for \"%s\"; tag ignored",
3992 case TIFFReadDirEntryErrIo
:
3995 "IO error during reading of \"%s\"; tag ignored", tagname
);
3997 case TIFFReadDirEntryErrRange
:
3998 TIFFWarningExtR(tif
, module
,
3999 "Incorrect value for \"%s\"; tag ignored",
4002 case TIFFReadDirEntryErrPsdif
:
4003 TIFFWarningExtR(tif
, module
,
4004 "Cannot handle different values per sample for "
4005 "\"%s\"; tag ignored",
4008 case TIFFReadDirEntryErrSizesan
:
4011 "Sanity check on size of \"%s\" value failed; tag ignored",
4014 case TIFFReadDirEntryErrAlloc
:
4015 TIFFWarningExtR(tif
, module
,
4016 "Out of memory reading of \"%s\"; tag ignored",
4020 assert(0); /* we should never get here */
4027 * Return the maximum number of color channels specified for a given photometric
4028 * type. 0 is returned if photometric type isn't supported or no default value
4029 * is defined by the specification.
4031 static int _TIFFGetMaxColorChannels(uint16_t photometric
)
4033 switch (photometric
)
4035 case PHOTOMETRIC_PALETTE
:
4036 case PHOTOMETRIC_MINISWHITE
:
4037 case PHOTOMETRIC_MINISBLACK
:
4039 case PHOTOMETRIC_YCBCR
:
4040 case PHOTOMETRIC_RGB
:
4041 case PHOTOMETRIC_CIELAB
:
4042 case PHOTOMETRIC_LOGLUV
:
4043 case PHOTOMETRIC_ITULAB
:
4044 case PHOTOMETRIC_ICCLAB
:
4046 case PHOTOMETRIC_SEPARATED
:
4047 case PHOTOMETRIC_MASK
:
4049 case PHOTOMETRIC_LOGL
:
4050 case PHOTOMETRIC_CFA
:
4056 static int ByteCountLooksBad(TIFF
*tif
)
4059 * Assume we have wrong StripByteCount value (in case
4060 * of single strip) in following cases:
4061 * - it is equal to zero along with StripOffset;
4062 * - it is larger than file itself (in case of uncompressed
4064 * - it is smaller than the size of the bytes per row
4065 * multiplied on the number of rows. The last case should
4066 * not be checked in the case of writing new image,
4067 * because we may do not know the exact strip size
4068 * until the whole image will be written and directory
4071 uint64_t bytecount
= TIFFGetStrileByteCount(tif
, 0);
4072 uint64_t offset
= TIFFGetStrileOffset(tif
, 0);
4079 if (tif
->tif_dir
.td_compression
!= COMPRESSION_NONE
)
4081 filesize
= TIFFGetFileSize(tif
);
4082 if (offset
<= filesize
&& bytecount
> filesize
- offset
)
4084 if (tif
->tif_mode
== O_RDONLY
)
4086 uint64_t scanlinesize
= TIFFScanlineSize64(tif
);
4087 if (tif
->tif_dir
.td_imagelength
> 0 &&
4088 scanlinesize
> UINT64_MAX
/ tif
->tif_dir
.td_imagelength
)
4092 if (bytecount
< scanlinesize
* tif
->tif_dir
.td_imagelength
)
4099 * Read the next TIFF directory from a file and convert it to the internal
4100 * format. We read directories sequentially.
4102 int TIFFReadDirectory(TIFF
*tif
)
4104 static const char module
[] = "TIFFReadDirectory";
4109 const TIFFField
*fip
;
4110 uint32_t fii
= FAILED_FII
;
4112 int bitspersample_read
= FALSE
;
4115 if (tif
->tif_nextdiroff
== 0)
4117 /* In this special case, tif_diroff needs also to be set to 0.
4118 * This is behind the last IFD, thus no checking or reading necessary.
4120 tif
->tif_diroff
= tif
->tif_nextdiroff
;
4124 nextdiroff
= tif
->tif_nextdiroff
;
4125 /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
4126 * reading of the directory. Otherwise, invalid IFD offsets could corrupt
4128 if (!_TIFFCheckDirNumberAndOffset(tif
,
4130 TIFF_NON_EXISTENT_DIR_NUMBER
4132 : tif
->tif_curdir
+ 1,
4135 return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
4138 dircount
= TIFFFetchDirectory(tif
, nextdiroff
, &dir
, &tif
->tif_nextdiroff
);
4141 TIFFErrorExtR(tif
, module
,
4142 "Failed to read directory at offset %" PRIu64
,
4146 /* Set global values after a valid directory has been fetched.
4147 * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
4149 if (tif
->tif_curdir
== TIFF_NON_EXISTENT_DIR_NUMBER
)
4150 tif
->tif_curdir
= 0;
4153 (*tif
->tif_cleanup
)(tif
); /* cleanup any previous compression state */
4155 TIFFReadDirectoryCheckOrder(tif
, dir
, dircount
);
4158 * Mark duplicates of any tag to be ignored (bugzilla 1994)
4159 * to avoid certain pathological problems.
4164 for (ma
= dir
, mb
= 0; mb
< dircount
; ma
++, mb
++)
4168 for (na
= ma
+ 1, nb
= mb
+ 1; nb
< dircount
; na
++, nb
++)
4170 if (ma
->tdir_tag
== na
->tdir_tag
)
4172 na
->tdir_ignore
= TRUE
;
4178 tif
->tif_flags
&= ~TIFF_BEENWRITING
; /* reset before new dir */
4179 tif
->tif_flags
&= ~TIFF_BUF4WRITE
; /* reset before new dir */
4180 tif
->tif_flags
&= ~TIFF_CHOPPEDUPARRAYS
;
4182 /* free any old stuff and reinit */
4183 TIFFFreeDirectory(tif
);
4184 TIFFDefaultDirectory(tif
);
4186 * Electronic Arts writes gray-scale TIFF files
4187 * without a PlanarConfiguration directory entry.
4188 * Thus we setup a default value here, even though
4189 * the TIFF spec says there is no default value.
4190 * After PlanarConfiguration is preset in TIFFDefaultDirectory()
4191 * the following setting is not needed, but does not harm either.
4193 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
4195 * Setup default value and then make a pass over
4196 * the fields to check type and tag information,
4197 * and to extract info required to size data
4198 * structures. A second pass is made afterwards
4199 * to read in everything not taken in the first pass.
4200 * But we must process the Compression tag first
4201 * in order to merge in codec-private tag definitions (otherwise
4202 * we may get complaints about unknown tags). However, the
4203 * Compression tag may be dependent on the SamplesPerPixel
4204 * tag value because older TIFF specs permitted Compression
4205 * to be written as a SamplesPerPixel-count tag entry.
4206 * Thus if we don't first figure out the correct SamplesPerPixel
4207 * tag value then we may end up ignoring the Compression tag
4208 * value because it has an incorrect count value (if the
4209 * true value of SamplesPerPixel is not 1).
4212 TIFFReadDirectoryFindEntry(tif
, dir
, dircount
, TIFFTAG_SAMPLESPERPIXEL
);
4215 if (!TIFFFetchNormalTag(tif
, dp
, 0))
4217 dp
->tdir_ignore
= TRUE
;
4219 dp
= TIFFReadDirectoryFindEntry(tif
, dir
, dircount
, TIFFTAG_COMPRESSION
);
4223 * The 5.0 spec says the Compression tag has one value, while
4224 * earlier specs say it has one value per sample. Because of
4225 * this, we accept the tag if one value is supplied with either
4229 enum TIFFReadDirEntryErr err
;
4230 err
= TIFFReadDirEntryShort(tif
, dp
, &value
);
4231 if (err
== TIFFReadDirEntryErrCount
)
4232 err
= TIFFReadDirEntryPersampleShort(tif
, dp
, &value
);
4233 if (err
!= TIFFReadDirEntryErrOk
)
4235 TIFFReadDirEntryOutputErr(tif
, err
, module
, "Compression", 0);
4238 if (!TIFFSetField(tif
, TIFFTAG_COMPRESSION
, value
))
4240 dp
->tdir_ignore
= TRUE
;
4244 if (!TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
))
4248 * First real pass over the directory.
4250 for (di
= 0, dp
= dir
; di
< dircount
; di
++, dp
++)
4252 if (!dp
->tdir_ignore
)
4254 TIFFReadDirectoryFindFieldInfo(tif
, dp
->tdir_tag
, &fii
);
4255 if (fii
== FAILED_FII
)
4257 TIFFWarningExtR(tif
, module
,
4258 "Unknown field with tag %" PRIu16
" (0x%" PRIx16
4260 dp
->tdir_tag
, dp
->tdir_tag
);
4261 /* the following knowingly leaks the
4262 anonymous field structure */
4263 if (!_TIFFMergeFields(
4265 _TIFFCreateAnonField(tif
, dp
->tdir_tag
,
4266 (TIFFDataType
)dp
->tdir_type
),
4271 "Registering anonymous field with tag %" PRIu16
4272 " (0x%" PRIx16
") failed",
4273 dp
->tdir_tag
, dp
->tdir_tag
);
4274 dp
->tdir_ignore
= TRUE
;
4278 TIFFReadDirectoryFindFieldInfo(tif
, dp
->tdir_tag
, &fii
);
4279 assert(fii
!= FAILED_FII
);
4283 if (!dp
->tdir_ignore
)
4285 fip
= tif
->tif_fields
[fii
];
4286 if (fip
->field_bit
== FIELD_IGNORE
)
4287 dp
->tdir_ignore
= TRUE
;
4290 switch (dp
->tdir_tag
)
4292 case TIFFTAG_STRIPOFFSETS
:
4293 case TIFFTAG_STRIPBYTECOUNTS
:
4294 case TIFFTAG_TILEOFFSETS
:
4295 case TIFFTAG_TILEBYTECOUNTS
:
4296 TIFFSetFieldBit(tif
, fip
->field_bit
);
4298 case TIFFTAG_IMAGEWIDTH
:
4299 case TIFFTAG_IMAGELENGTH
:
4300 case TIFFTAG_IMAGEDEPTH
:
4301 case TIFFTAG_TILELENGTH
:
4302 case TIFFTAG_TILEWIDTH
:
4303 case TIFFTAG_TILEDEPTH
:
4304 case TIFFTAG_PLANARCONFIG
:
4305 case TIFFTAG_ROWSPERSTRIP
:
4306 case TIFFTAG_EXTRASAMPLES
:
4307 if (!TIFFFetchNormalTag(tif
, dp
, 0))
4309 dp
->tdir_ignore
= TRUE
;
4312 if (!_TIFFCheckFieldIsValidForCodec(tif
, dp
->tdir_tag
))
4313 dp
->tdir_ignore
= TRUE
;
4321 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
4322 * c) strip offsets/bytecounts tag are both present and
4323 * d) both contain exactly one value, then we consistently find
4324 * that the buggy implementation of the buggy compression scheme
4325 * matches contig planarconfig best. So we 'fix-up' the tag here
4327 if ((tif
->tif_dir
.td_compression
== COMPRESSION_OJPEG
) &&
4328 (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
))
4330 if (!_TIFFFillStriles(tif
))
4332 dp
= TIFFReadDirectoryFindEntry(tif
, dir
, dircount
,
4333 TIFFTAG_STRIPOFFSETS
);
4334 if ((dp
!= 0) && (dp
->tdir_count
== 1))
4336 dp
= TIFFReadDirectoryFindEntry(tif
, dir
, dircount
,
4337 TIFFTAG_STRIPBYTECOUNTS
);
4338 if ((dp
!= 0) && (dp
->tdir_count
== 1))
4340 tif
->tif_dir
.td_planarconfig
= PLANARCONFIG_CONTIG
;
4341 TIFFWarningExtR(tif
, module
,
4342 "Planarconfig tag value assumed incorrect, "
4343 "assuming data is contig instead of chunky");
4348 * Allocate directory structure and setup defaults.
4350 if (!TIFFFieldSet(tif
, FIELD_IMAGEDIMENSIONS
))
4352 MissingRequired(tif
, "ImageLength");
4357 * Second pass: extract other information.
4359 for (di
= 0, dp
= dir
; di
< dircount
; di
++, dp
++)
4361 if (!dp
->tdir_ignore
)
4363 switch (dp
->tdir_tag
)
4365 case TIFFTAG_MINSAMPLEVALUE
:
4366 case TIFFTAG_MAXSAMPLEVALUE
:
4367 case TIFFTAG_BITSPERSAMPLE
:
4368 case TIFFTAG_DATATYPE
:
4369 case TIFFTAG_SAMPLEFORMAT
:
4371 * The MinSampleValue, MaxSampleValue, BitsPerSample
4372 * DataType and SampleFormat tags are supposed to be
4373 * written as one value/sample, but some vendors
4374 * incorrectly write one value only -- so we accept
4375 * that as well (yuck). Other vendors write correct
4376 * value for NumberOfSamples, but incorrect one for
4377 * BitsPerSample and friends, and we will read this
4382 enum TIFFReadDirEntryErr err
;
4383 err
= TIFFReadDirEntryShort(tif
, dp
, &value
);
4384 if (err
== TIFFReadDirEntryErrCount
)
4386 TIFFReadDirEntryPersampleShort(tif
, dp
, &value
);
4387 if (err
!= TIFFReadDirEntryErrOk
)
4389 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4390 TIFFReadDirEntryOutputErr(
4392 fip
? fip
->field_name
: "unknown tagname", 0);
4395 if (!TIFFSetField(tif
, dp
->tdir_tag
, value
))
4397 if (dp
->tdir_tag
== TIFFTAG_BITSPERSAMPLE
)
4398 bitspersample_read
= TRUE
;
4401 case TIFFTAG_SMINSAMPLEVALUE
:
4402 case TIFFTAG_SMAXSAMPLEVALUE
:
4405 double *data
= NULL
;
4406 enum TIFFReadDirEntryErr err
;
4407 uint32_t saved_flags
;
4409 if (dp
->tdir_count
!=
4410 (uint64_t)tif
->tif_dir
.td_samplesperpixel
)
4411 err
= TIFFReadDirEntryErrCount
;
4413 err
= TIFFReadDirEntryDoubleArray(tif
, dp
, &data
);
4414 if (err
!= TIFFReadDirEntryErrOk
)
4416 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4417 TIFFReadDirEntryOutputErr(
4419 fip
? fip
->field_name
: "unknown tagname", 0);
4422 saved_flags
= tif
->tif_flags
;
4423 tif
->tif_flags
|= TIFF_PERSAMPLE
;
4424 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
4425 tif
->tif_flags
= saved_flags
;
4426 _TIFFfreeExt(tif
, data
);
4431 case TIFFTAG_STRIPOFFSETS
:
4432 case TIFFTAG_TILEOFFSETS
:
4433 switch (dp
->tdir_type
)
4440 /* Warn except if directory typically created with
4441 * TIFFDeferStrileArrayWriting() */
4442 if (!(tif
->tif_mode
== O_RDWR
&&
4443 dp
->tdir_count
== 0 && dp
->tdir_type
== 0 &&
4444 dp
->tdir_offset
.toff_long8
== 0))
4446 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4448 tif
, module
, "Invalid data type for tag %s",
4449 fip
? fip
->field_name
: "unknown tagname");
4453 _TIFFmemcpy(&(tif
->tif_dir
.td_stripoffset_entry
), dp
,
4454 sizeof(TIFFDirEntry
));
4456 case TIFFTAG_STRIPBYTECOUNTS
:
4457 case TIFFTAG_TILEBYTECOUNTS
:
4458 switch (dp
->tdir_type
)
4465 /* Warn except if directory typically created with
4466 * TIFFDeferStrileArrayWriting() */
4467 if (!(tif
->tif_mode
== O_RDWR
&&
4468 dp
->tdir_count
== 0 && dp
->tdir_type
== 0 &&
4469 dp
->tdir_offset
.toff_long8
== 0))
4471 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4473 tif
, module
, "Invalid data type for tag %s",
4474 fip
? fip
->field_name
: "unknown tagname");
4478 _TIFFmemcpy(&(tif
->tif_dir
.td_stripbytecount_entry
), dp
,
4479 sizeof(TIFFDirEntry
));
4481 case TIFFTAG_COLORMAP
:
4482 case TIFFTAG_TRANSFERFUNCTION
:
4484 enum TIFFReadDirEntryErr err
;
4485 uint32_t countpersample
;
4486 uint32_t countrequired
;
4487 uint32_t incrementpersample
;
4488 uint16_t *value
= NULL
;
4489 /* It would be dangerous to instantiate those tag values */
4490 /* since if td_bitspersample has not yet been read (due to
4492 /* unordered tags), it could be read afterwards with a */
4493 /* values greater than the default one (1), which may cause
4495 /* crashes in user code */
4496 if (!bitspersample_read
)
4498 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4501 "Ignoring %s since BitsPerSample tag not found",
4502 fip
? fip
->field_name
: "unknown tagname");
4505 /* ColorMap or TransferFunction for high bit */
4506 /* depths do not make much sense and could be */
4507 /* used as a denial of service vector */
4508 if (tif
->tif_dir
.td_bitspersample
> 24)
4510 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4513 "Ignoring %s because BitsPerSample=%" PRIu16
">24",
4514 fip
? fip
->field_name
: "unknown tagname",
4515 tif
->tif_dir
.td_bitspersample
);
4518 countpersample
= (1U << tif
->tif_dir
.td_bitspersample
);
4519 if ((dp
->tdir_tag
== TIFFTAG_TRANSFERFUNCTION
) &&
4520 (dp
->tdir_count
== (uint64_t)countpersample
))
4522 countrequired
= countpersample
;
4523 incrementpersample
= 0;
4527 countrequired
= 3 * countpersample
;
4528 incrementpersample
= countpersample
;
4530 if (dp
->tdir_count
!= (uint64_t)countrequired
)
4531 err
= TIFFReadDirEntryErrCount
;
4533 err
= TIFFReadDirEntryShortArray(tif
, dp
, &value
);
4534 if (err
!= TIFFReadDirEntryErrOk
)
4536 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4537 TIFFReadDirEntryOutputErr(
4539 fip
? fip
->field_name
: "unknown tagname", 1);
4543 TIFFSetField(tif
, dp
->tdir_tag
, value
,
4544 value
+ incrementpersample
,
4545 value
+ 2 * incrementpersample
);
4546 _TIFFfreeExt(tif
, value
);
4550 /* BEGIN REV 4.0 COMPATIBILITY */
4551 case TIFFTAG_OSUBFILETYPE
:
4555 if (TIFFReadDirEntryShort(tif
, dp
, &valueo
) ==
4556 TIFFReadDirEntryErrOk
)
4560 case OFILETYPE_REDUCEDIMAGE
:
4561 value
= FILETYPE_REDUCEDIMAGE
;
4563 case OFILETYPE_PAGE
:
4564 value
= FILETYPE_PAGE
;
4571 TIFFSetField(tif
, TIFFTAG_SUBFILETYPE
, value
);
4575 /* END REV 4.0 COMPATIBILITY */
4577 case TIFFTAG_EP_BATTERYLEVEL
:
4578 /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
4579 * LibTiff defines it as ASCII and converts RATIONAL to an
4581 switch (dp
->tdir_type
)
4585 /* Read rational and convert to ASCII*/
4586 enum TIFFReadDirEntryErr err
;
4587 TIFFRational_t rValue
;
4588 err
= TIFFReadDirEntryCheckedRationalDirect(
4590 if (err
!= TIFFReadDirEntryErrOk
)
4592 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4593 TIFFReadDirEntryOutputErr(
4595 fip
? fip
->field_name
: "unknown tagname",
4601 snprintf(szAux
, sizeof(szAux
) - 1, "%d/%d",
4602 rValue
.uNum
, rValue
.uDenom
);
4603 TIFFSetField(tif
, dp
->tdir_tag
, szAux
);
4608 (void)TIFFFetchNormalTag(tif
, dp
, TRUE
);
4611 fip
= TIFFFieldWithTag(tif
, dp
->tdir_tag
);
4612 TIFFWarningExtR(tif
, module
,
4613 "Invalid data type for tag %s. "
4614 "ASCII or RATIONAL expected",
4615 fip
? fip
->field_name
4616 : "unknown tagname");
4622 (void)TIFFFetchNormalTag(tif
, dp
, TRUE
);
4625 } /* -- if (!dp->tdir_ignore) */
4626 } /* -- for-loop -- */
4630 * - If a) compression is OJPEG, and b) photometric tag is missing,
4631 * then we consistently find that photometric should be YCbCr
4632 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
4633 * then we consistently find that the buggy implementation of the
4634 * buggy compression scheme matches photometric YCbCr instead.
4635 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
4636 * then we consistently find bitspersample should be 8.
4637 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4638 * and c) photometric is RGB or YCbCr, then we consistently find
4639 * samplesperpixel should be 3
4640 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4641 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
4642 * find samplesperpixel should be 3
4644 if (tif
->tif_dir
.td_compression
== COMPRESSION_OJPEG
)
4646 if (!TIFFFieldSet(tif
, FIELD_PHOTOMETRIC
))
4650 "Photometric tag is missing, assuming data is YCbCr");
4651 if (!TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_YCBCR
))
4654 else if (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_RGB
)
4656 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_YCBCR
;
4657 TIFFWarningExtR(tif
, module
,
4658 "Photometric tag value assumed incorrect, "
4659 "assuming data is YCbCr instead of RGB");
4661 if (!TIFFFieldSet(tif
, FIELD_BITSPERSAMPLE
))
4665 "BitsPerSample tag is missing, assuming 8 bits per sample");
4666 if (!TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 8))
4669 if (!TIFFFieldSet(tif
, FIELD_SAMPLESPERPIXEL
))
4671 if (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_RGB
)
4673 TIFFWarningExtR(tif
, module
,
4674 "SamplesPerPixel tag is missing, "
4675 "assuming correct SamplesPerPixel value is 3");
4676 if (!TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 3))
4679 if (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_YCBCR
)
4681 TIFFWarningExtR(tif
, module
,
4682 "SamplesPerPixel tag is missing, "
4683 "applying correct SamplesPerPixel value of 3");
4684 if (!TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 3))
4687 else if ((tif
->tif_dir
.td_photometric
== PHOTOMETRIC_MINISWHITE
) ||
4688 (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_MINISBLACK
))
4691 * SamplesPerPixel tag is missing, but is not required
4692 * by spec. Assume correct SamplesPerPixel value of 1.
4694 if (!TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 1))
4701 * Setup appropriate structures (by strip or by tile)
4702 * We do that only after the above OJPEG hack which alters SamplesPerPixel
4703 * and thus influences the number of strips in the separate planarconfig.
4705 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
))
4707 tif
->tif_dir
.td_nstrips
= TIFFNumberOfStrips(tif
);
4708 tif
->tif_dir
.td_tilewidth
= tif
->tif_dir
.td_imagewidth
;
4709 tif
->tif_dir
.td_tilelength
= tif
->tif_dir
.td_rowsperstrip
;
4710 tif
->tif_dir
.td_tiledepth
= tif
->tif_dir
.td_imagedepth
;
4711 tif
->tif_flags
&= ~TIFF_ISTILED
;
4715 tif
->tif_dir
.td_nstrips
= TIFFNumberOfTiles(tif
);
4716 tif
->tif_flags
|= TIFF_ISTILED
;
4718 if (!tif
->tif_dir
.td_nstrips
)
4720 TIFFErrorExtR(tif
, module
, "Cannot handle zero number of %s",
4721 isTiled(tif
) ? "tiles" : "strips");
4724 tif
->tif_dir
.td_stripsperimage
= tif
->tif_dir
.td_nstrips
;
4725 if (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
)
4726 tif
->tif_dir
.td_stripsperimage
/= tif
->tif_dir
.td_samplesperpixel
;
4727 if (!TIFFFieldSet(tif
, FIELD_STRIPOFFSETS
))
4729 #ifdef OJPEG_SUPPORT
4730 if ((tif
->tif_dir
.td_compression
== COMPRESSION_OJPEG
) &&
4731 (isTiled(tif
) == 0) && (tif
->tif_dir
.td_nstrips
== 1))
4735 * If a) compression is OJPEG, b) it's not a tiled TIFF,
4736 * and c) the number of strips is 1,
4737 * then we tolerate the absence of stripoffsets tag,
4738 * because, presumably, all required data is in the
4739 * JpegInterchangeFormat stream.
4741 TIFFSetFieldBit(tif
, FIELD_STRIPOFFSETS
);
4746 MissingRequired(tif
, isTiled(tif
) ? "TileOffsets" : "StripOffsets");
4751 if (tif
->tif_mode
== O_RDWR
&&
4752 tif
->tif_dir
.td_stripoffset_entry
.tdir_tag
!= 0 &&
4753 tif
->tif_dir
.td_stripoffset_entry
.tdir_count
== 0 &&
4754 tif
->tif_dir
.td_stripoffset_entry
.tdir_type
== 0 &&
4755 tif
->tif_dir
.td_stripoffset_entry
.tdir_offset
.toff_long8
== 0 &&
4756 tif
->tif_dir
.td_stripbytecount_entry
.tdir_tag
!= 0 &&
4757 tif
->tif_dir
.td_stripbytecount_entry
.tdir_count
== 0 &&
4758 tif
->tif_dir
.td_stripbytecount_entry
.tdir_type
== 0 &&
4759 tif
->tif_dir
.td_stripbytecount_entry
.tdir_offset
.toff_long8
== 0)
4761 /* Directory typically created with TIFFDeferStrileArrayWriting() */
4762 TIFFSetupStrips(tif
);
4764 else if (!(tif
->tif_flags
& TIFF_DEFERSTRILELOAD
))
4766 if (tif
->tif_dir
.td_stripoffset_entry
.tdir_tag
!= 0)
4768 if (!TIFFFetchStripThing(tif
, &(tif
->tif_dir
.td_stripoffset_entry
),
4769 tif
->tif_dir
.td_nstrips
,
4770 &tif
->tif_dir
.td_stripoffset_p
))
4775 if (tif
->tif_dir
.td_stripbytecount_entry
.tdir_tag
!= 0)
4777 if (!TIFFFetchStripThing(
4778 tif
, &(tif
->tif_dir
.td_stripbytecount_entry
),
4779 tif
->tif_dir
.td_nstrips
, &tif
->tif_dir
.td_stripbytecount_p
))
4787 * Make sure all non-color channels are extrasamples.
4788 * If it's not the case, define them as such.
4790 color_channels
= _TIFFGetMaxColorChannels(tif
->tif_dir
.td_photometric
);
4791 if (color_channels
&&
4792 tif
->tif_dir
.td_samplesperpixel
- tif
->tif_dir
.td_extrasamples
>
4795 uint16_t old_extrasamples
;
4796 uint16_t *new_sampleinfo
;
4800 "Sum of Photometric type-related "
4801 "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4802 "Defining non-color channels as ExtraSamples.");
4804 old_extrasamples
= tif
->tif_dir
.td_extrasamples
;
4805 tif
->tif_dir
.td_extrasamples
=
4806 (uint16_t)(tif
->tif_dir
.td_samplesperpixel
- color_channels
);
4808 // sampleinfo should contain information relative to these new extra
4810 new_sampleinfo
= (uint16_t *)_TIFFcallocExt(
4811 tif
, tif
->tif_dir
.td_extrasamples
, sizeof(uint16_t));
4812 if (!new_sampleinfo
)
4814 TIFFErrorExtR(tif
, module
,
4815 "Failed to allocate memory for "
4816 "temporary new sampleinfo array "
4817 "(%" PRIu16
" 16 bit elements)",
4818 tif
->tif_dir
.td_extrasamples
);
4822 if (old_extrasamples
> 0)
4823 memcpy(new_sampleinfo
, tif
->tif_dir
.td_sampleinfo
,
4824 old_extrasamples
* sizeof(uint16_t));
4825 _TIFFsetShortArrayExt(tif
, &tif
->tif_dir
.td_sampleinfo
, new_sampleinfo
,
4826 tif
->tif_dir
.td_extrasamples
);
4827 _TIFFfreeExt(tif
, new_sampleinfo
);
4831 * Verify Palette image has a Colormap.
4833 if (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_PALETTE
&&
4834 !TIFFFieldSet(tif
, FIELD_COLORMAP
))
4836 if (tif
->tif_dir
.td_bitspersample
>= 8 &&
4837 tif
->tif_dir
.td_samplesperpixel
== 3)
4838 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_RGB
;
4839 else if (tif
->tif_dir
.td_bitspersample
>= 8)
4840 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_MINISBLACK
;
4843 MissingRequired(tif
, "Colormap");
4849 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
4852 if (tif
->tif_dir
.td_compression
!= COMPRESSION_OJPEG
)
4855 * Attempt to deal with a missing StripByteCounts tag.
4857 if (!TIFFFieldSet(tif
, FIELD_STRIPBYTECOUNTS
))
4860 * Some manufacturers violate the spec by not giving
4861 * the size of the strips. In this case, assume there
4862 * is one uncompressed strip of data.
4864 if ((tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
&&
4865 tif
->tif_dir
.td_nstrips
> 1) ||
4866 (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
&&
4867 tif
->tif_dir
.td_nstrips
!=
4868 (uint32_t)tif
->tif_dir
.td_samplesperpixel
))
4870 MissingRequired(tif
, "StripByteCounts");
4875 "TIFF directory is missing required "
4876 "\"StripByteCounts\" field, calculating from imagelength");
4877 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
4880 else if (tif
->tif_dir
.td_nstrips
== 1 &&
4881 !(tif
->tif_flags
& TIFF_ISTILED
) && ByteCountLooksBad(tif
))
4884 * XXX: Plexus (and others) sometimes give a value of
4885 * zero for a tag when they don't know what the
4886 * correct value is! Try and handle the simple case
4887 * of estimating the size of a one strip image.
4889 TIFFWarningExtR(tif
, module
,
4890 "Bogus \"StripByteCounts\" field, ignoring and "
4891 "calculating from imagelength");
4892 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
4895 else if (!(tif
->tif_flags
& TIFF_DEFERSTRILELOAD
) &&
4896 tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
&&
4897 tif
->tif_dir
.td_nstrips
> 2 &&
4898 tif
->tif_dir
.td_compression
== COMPRESSION_NONE
&&
4899 TIFFGetStrileByteCount(tif
, 0) !=
4900 TIFFGetStrileByteCount(tif
, 1) &&
4901 TIFFGetStrileByteCount(tif
, 0) != 0 &&
4902 TIFFGetStrileByteCount(tif
, 1) != 0)
4905 * XXX: Some vendors fill StripByteCount array with
4906 * absolutely wrong values (it can be equal to
4907 * StripOffset array, for example). Catch this case
4910 * We avoid this check if deferring strile loading
4911 * as it would always force us to load the strip/tile
4914 TIFFWarningExtR(tif
, module
,
4915 "Wrong \"StripByteCounts\" field, ignoring and "
4916 "calculating from imagelength");
4917 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
4923 _TIFFfreeExt(tif
, dir
);
4926 if (!TIFFFieldSet(tif
, FIELD_MAXSAMPLEVALUE
))
4928 if (tif
->tif_dir
.td_bitspersample
>= 16)
4929 tif
->tif_dir
.td_maxsamplevalue
= 0xFFFF;
4931 tif
->tif_dir
.td_maxsamplevalue
=
4932 (uint16_t)((1L << tif
->tif_dir
.td_bitspersample
) - 1);
4935 #ifdef STRIPBYTECOUNTSORTED_UNUSED
4937 * XXX: We can optimize checking for the strip bounds using the sorted
4938 * bytecounts array. See also comments for TIFFAppendToStrip()
4939 * function in tif_write.c.
4941 if (!(tif
->tif_flags
& TIFF_DEFERSTRILELOAD
) && tif
->tif_dir
.td_nstrips
> 1)
4945 tif
->tif_dir
.td_stripbytecountsorted
= 1;
4946 for (strip
= 1; strip
< tif
->tif_dir
.td_nstrips
; strip
++)
4948 if (TIFFGetStrileOffset(tif
, strip
- 1) >
4949 TIFFGetStrileOffset(tif
, strip
))
4951 tif
->tif_dir
.td_stripbytecountsorted
= 0;
4959 * An opportunity for compression mode dependent tag fixup
4961 (*tif
->tif_fixuptags
)(tif
);
4964 * Some manufacturers make life difficult by writing
4965 * large amounts of uncompressed data as a single strip.
4966 * This is contrary to the recommendations of the spec.
4967 * The following makes an attempt at breaking such images
4968 * into strips closer to the recommended 8k bytes. A
4969 * side effect, however, is that the RowsPerStrip tag
4970 * value may be changed.
4972 if ((tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
) &&
4973 (tif
->tif_dir
.td_nstrips
== 1) &&
4974 (tif
->tif_dir
.td_compression
== COMPRESSION_NONE
) &&
4975 ((tif
->tif_flags
& (TIFF_STRIPCHOP
| TIFF_ISTILED
)) == TIFF_STRIPCHOP
))
4977 ChopUpSingleUncompressedStrip(tif
);
4980 /* There are also uncompressed striped files with strips larger than */
4981 /* 2 GB, which make them unfriendly with a lot of code. If possible, */
4982 /* try to expose smaller "virtual" strips. */
4983 if (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
&&
4984 tif
->tif_dir
.td_compression
== COMPRESSION_NONE
&&
4985 (tif
->tif_flags
& (TIFF_STRIPCHOP
| TIFF_ISTILED
)) == TIFF_STRIPCHOP
&&
4986 TIFFStripSize64(tif
) > 0x7FFFFFFFUL
)
4988 TryChopUpUncompressedBigTiff(tif
);
4992 * Clear the dirty directory flag.
4994 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
4995 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
4998 * Reinitialize i/o since we are starting on a new directory.
5000 tif
->tif_row
= (uint32_t)-1;
5001 tif
->tif_curstrip
= (uint32_t)-1;
5002 tif
->tif_col
= (uint32_t)-1;
5003 tif
->tif_curtile
= (uint32_t)-1;
5004 tif
->tif_tilesize
= (tmsize_t
)-1;
5006 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
5007 if (!tif
->tif_scanlinesize
)
5009 TIFFErrorExtR(tif
, module
, "Cannot handle zero scanline size");
5015 tif
->tif_tilesize
= TIFFTileSize(tif
);
5016 if (!tif
->tif_tilesize
)
5018 TIFFErrorExtR(tif
, module
, "Cannot handle zero tile size");
5024 if (!TIFFStripSize(tif
))
5026 TIFFErrorExtR(tif
, module
, "Cannot handle zero strip size");
5033 _TIFFfreeExt(tif
, dir
);
5037 static void TIFFReadDirectoryCheckOrder(TIFF
*tif
, TIFFDirEntry
*dir
,
5040 static const char module
[] = "TIFFReadDirectoryCheckOrder";
5045 for (n
= 0, o
= dir
; n
< dircount
; n
++, o
++)
5047 if (o
->tdir_tag
< m
)
5049 TIFFWarningExtR(tif
, module
,
5050 "Invalid TIFF directory; tags are not sorted in "
5054 m
= o
->tdir_tag
+ 1;
5058 static TIFFDirEntry
*TIFFReadDirectoryFindEntry(TIFF
*tif
, TIFFDirEntry
*dir
,
5065 for (m
= dir
, n
= 0; n
< dircount
; m
++, n
++)
5067 if (m
->tdir_tag
== tagid
)
5073 static void TIFFReadDirectoryFindFieldInfo(TIFF
*tif
, uint16_t tagid
,
5078 mc
= (int32_t)tif
->tif_nfields
;
5087 if (tif
->tif_fields
[mb
]->field_tag
== (uint32_t)tagid
)
5089 if (tif
->tif_fields
[mb
]->field_tag
< (uint32_t)tagid
)
5098 if (tif
->tif_fields
[mb
- 1]->field_tag
!= (uint32_t)tagid
)
5106 * Read custom directory from the arbitrary offset.
5107 * The code is very similar to TIFFReadDirectory().
5109 int TIFFReadCustomDirectory(TIFF
*tif
, toff_t diroff
,
5110 const TIFFFieldArray
*infoarray
)
5112 static const char module
[] = "TIFFReadCustomDirectory";
5117 const TIFFField
*fip
;
5119 (*tif
->tif_cleanup
)(tif
); /* cleanup any previous compression state */
5120 _TIFFSetupFields(tif
, infoarray
);
5121 dircount
= TIFFFetchDirectory(tif
, diroff
, &dir
, NULL
);
5124 TIFFErrorExtR(tif
, module
,
5125 "Failed to read custom directory at offset %" PRIu64
,
5129 TIFFFreeDirectory(tif
);
5130 _TIFFmemset(&tif
->tif_dir
, 0, sizeof(TIFFDirectory
));
5131 TIFFReadDirectoryCheckOrder(tif
, dir
, dircount
);
5132 for (di
= 0, dp
= dir
; di
< dircount
; di
++, dp
++)
5134 TIFFReadDirectoryFindFieldInfo(tif
, dp
->tdir_tag
, &fii
);
5135 if (fii
== FAILED_FII
)
5137 TIFFWarningExtR(tif
, module
,
5138 "Unknown field with tag %" PRIu16
" (0x%" PRIx16
5140 dp
->tdir_tag
, dp
->tdir_tag
);
5141 if (!_TIFFMergeFields(
5143 _TIFFCreateAnonField(tif
, dp
->tdir_tag
,
5144 (TIFFDataType
)dp
->tdir_type
),
5147 TIFFWarningExtR(tif
, module
,
5148 "Registering anonymous field with tag %" PRIu16
5149 " (0x%" PRIx16
") failed",
5150 dp
->tdir_tag
, dp
->tdir_tag
);
5151 dp
->tdir_ignore
= TRUE
;
5155 TIFFReadDirectoryFindFieldInfo(tif
, dp
->tdir_tag
, &fii
);
5156 assert(fii
!= FAILED_FII
);
5159 if (!dp
->tdir_ignore
)
5161 fip
= tif
->tif_fields
[fii
];
5162 if (fip
->field_bit
== FIELD_IGNORE
)
5163 dp
->tdir_ignore
= TRUE
;
5166 /* check data type */
5167 while ((fip
->field_type
!= TIFF_ANY
) &&
5168 (fip
->field_type
!= dp
->tdir_type
))
5171 if ((fii
== tif
->tif_nfields
) ||
5172 (tif
->tif_fields
[fii
]->field_tag
!=
5173 (uint32_t)dp
->tdir_tag
))
5178 fip
= tif
->tif_fields
[fii
];
5182 TIFFWarningExtR(tif
, module
,
5183 "Wrong data type %" PRIu16
5184 " for \"%s\"; tag ignored",
5185 dp
->tdir_type
, fip
->field_name
);
5186 dp
->tdir_ignore
= TRUE
;
5190 /* check count if known in advance */
5191 if ((fip
->field_readcount
!= TIFF_VARIABLE
) &&
5192 (fip
->field_readcount
!= TIFF_VARIABLE2
))
5195 if (fip
->field_readcount
== TIFF_SPP
)
5197 (uint32_t)tif
->tif_dir
.td_samplesperpixel
;
5199 expected
= (uint32_t)fip
->field_readcount
;
5200 if (!CheckDirCount(tif
, dp
, expected
))
5201 dp
->tdir_ignore
= TRUE
;
5205 if (!dp
->tdir_ignore
)
5207 switch (dp
->tdir_tag
)
5209 case EXIFTAG_SUBJECTDISTANCE
:
5210 if (!TIFFFieldIsAnonymous(fip
))
5212 /* should only be called on a Exif directory */
5213 /* when exifFields[] is active */
5214 (void)TIFFFetchSubjectDistance(tif
, dp
);
5218 (void)TIFFFetchNormalTag(tif
, dp
, TRUE
);
5222 (void)TIFFFetchNormalTag(tif
, dp
, TRUE
);
5225 } /*-- if (!dp->tdir_ignore) */
5228 /* To be able to return from SubIFD or custom-IFD to main-IFD */
5229 tif
->tif_setdirectory_force_absolute
= TRUE
;
5231 _TIFFfreeExt(tif
, dir
);
5236 * EXIF is important special case of custom IFD, so we have a special
5237 * function to read it.
5239 int TIFFReadEXIFDirectory(TIFF
*tif
, toff_t diroff
)
5241 const TIFFFieldArray
*exifFieldArray
;
5242 exifFieldArray
= _TIFFGetExifFields();
5243 return TIFFReadCustomDirectory(tif
, diroff
, exifFieldArray
);
5247 *--: EXIF-GPS custom directory reading as another special case of custom IFD.
5249 int TIFFReadGPSDirectory(TIFF
*tif
, toff_t diroff
)
5251 const TIFFFieldArray
*gpsFieldArray
;
5252 gpsFieldArray
= _TIFFGetGpsFields();
5253 return TIFFReadCustomDirectory(tif
, diroff
, gpsFieldArray
);
5256 static int EstimateStripByteCounts(TIFF
*tif
, TIFFDirEntry
*dir
,
5259 static const char module
[] = "EstimateStripByteCounts";
5262 TIFFDirectory
*td
= &tif
->tif_dir
;
5265 /* Do not try to load stripbytecount as we will compute it */
5266 if (!_TIFFFillStrilesInternal(tif
, 0))
5269 if (td
->td_stripbytecount_p
)
5270 _TIFFfreeExt(tif
, td
->td_stripbytecount_p
);
5271 td
->td_stripbytecount_p
= (uint64_t *)_TIFFCheckMalloc(
5272 tif
, td
->td_nstrips
, sizeof(uint64_t), "for \"StripByteCounts\" array");
5273 if (td
->td_stripbytecount_p
== NULL
)
5276 if (td
->td_compression
!= COMPRESSION_NONE
)
5281 filesize
= TIFFGetFileSize(tif
);
5282 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5283 space
= sizeof(TIFFHeaderClassic
) + 2 + dircount
* 12 + 4;
5285 space
= sizeof(TIFFHeaderBig
) + 8 + dircount
* 20 + 8;
5286 /* calculate amount of space used by indirect values */
5287 for (dp
= dir
, n
= dircount
; n
> 0; n
--, dp
++)
5291 typewidth
= TIFFDataWidth((TIFFDataType
)dp
->tdir_type
);
5296 "Cannot determine size of unknown tag type %" PRIu16
,
5300 if (dp
->tdir_count
> UINT64_MAX
/ typewidth
)
5302 datasize
= (uint64_t)typewidth
* dp
->tdir_count
;
5303 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5313 if (space
> UINT64_MAX
- datasize
)
5317 if (filesize
< space
)
5318 /* we should perhaps return in error ? */
5321 space
= filesize
- space
;
5322 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
)
5323 space
/= td
->td_samplesperpixel
;
5324 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
5325 td
->td_stripbytecount_p
[strip
] = space
;
5327 * This gross hack handles the case were the offset to
5328 * the last strip is past the place where we think the strip
5329 * should begin. Since a strip of data must be contiguous,
5330 * it's safe to assume that we've overestimated the amount
5331 * of data in the strip and trim this number back accordingly.
5334 if (td
->td_stripoffset_p
[strip
] >
5335 UINT64_MAX
- td
->td_stripbytecount_p
[strip
])
5337 if (td
->td_stripoffset_p
[strip
] + td
->td_stripbytecount_p
[strip
] >
5340 if (td
->td_stripoffset_p
[strip
] >= filesize
)
5342 /* Not sure what we should in that case... */
5343 td
->td_stripbytecount_p
[strip
] = 0;
5347 td
->td_stripbytecount_p
[strip
] =
5348 filesize
- td
->td_stripoffset_p
[strip
];
5352 else if (isTiled(tif
))
5354 uint64_t bytespertile
= TIFFTileSize64(tif
);
5356 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
5357 td
->td_stripbytecount_p
[strip
] = bytespertile
;
5361 uint64_t rowbytes
= TIFFScanlineSize64(tif
);
5362 uint32_t rowsperstrip
= td
->td_imagelength
/ td
->td_stripsperimage
;
5363 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
5365 if (rowbytes
> 0 && rowsperstrip
> UINT64_MAX
/ rowbytes
)
5367 td
->td_stripbytecount_p
[strip
] = rowbytes
* rowsperstrip
;
5370 TIFFSetFieldBit(tif
, FIELD_STRIPBYTECOUNTS
);
5371 if (!TIFFFieldSet(tif
, FIELD_ROWSPERSTRIP
))
5372 td
->td_rowsperstrip
= td
->td_imagelength
;
5376 static void MissingRequired(TIFF
*tif
, const char *tagname
)
5378 static const char module
[] = "MissingRequired";
5380 TIFFErrorExtR(tif
, module
,
5381 "TIFF directory is missing required \"%s\" field", tagname
);
5384 static unsigned long hashFuncOffsetToNumber(const void *elt
)
5386 const TIFFOffsetAndDirNumber
*offsetAndDirNumber
=
5387 (const TIFFOffsetAndDirNumber
*)elt
;
5388 const uint32_t hash
= (uint32_t)(offsetAndDirNumber
->offset
>> 32) ^
5389 ((uint32_t)offsetAndDirNumber
->offset
& 0xFFFFFFFFU
);
5393 static bool equalFuncOffsetToNumber(const void *elt1
, const void *elt2
)
5395 const TIFFOffsetAndDirNumber
*offsetAndDirNumber1
=
5396 (const TIFFOffsetAndDirNumber
*)elt1
;
5397 const TIFFOffsetAndDirNumber
*offsetAndDirNumber2
=
5398 (const TIFFOffsetAndDirNumber
*)elt2
;
5399 return offsetAndDirNumber1
->offset
== offsetAndDirNumber2
->offset
;
5402 static unsigned long hashFuncNumberToOffset(const void *elt
)
5404 const TIFFOffsetAndDirNumber
*offsetAndDirNumber
=
5405 (const TIFFOffsetAndDirNumber
*)elt
;
5406 return offsetAndDirNumber
->dirNumber
;
5409 static bool equalFuncNumberToOffset(const void *elt1
, const void *elt2
)
5411 const TIFFOffsetAndDirNumber
*offsetAndDirNumber1
=
5412 (const TIFFOffsetAndDirNumber
*)elt1
;
5413 const TIFFOffsetAndDirNumber
*offsetAndDirNumber2
=
5414 (const TIFFOffsetAndDirNumber
*)elt2
;
5415 return offsetAndDirNumber1
->dirNumber
== offsetAndDirNumber2
->dirNumber
;
5419 * Check the directory number and offset against the list of already seen
5420 * directory numbers and offsets. This is a trick to prevent IFD looping.
5421 * The one can create TIFF file with looped directory pointers. We will
5422 * maintain a list of already seen directories and check every IFD offset
5423 * and its IFD number against that list. However, the offset of an IFD number
5424 * can change - e.g. when writing updates to file.
5425 * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
5426 * or an error has occurred.
5428 int _TIFFCheckDirNumberAndOffset(TIFF
*tif
, tdir_t dirn
, uint64_t diroff
)
5430 if (diroff
== 0) /* no more directories */
5433 if (tif
->tif_map_dir_offset_to_number
== NULL
)
5435 tif
->tif_map_dir_offset_to_number
= TIFFHashSetNew(
5436 hashFuncOffsetToNumber
, equalFuncOffsetToNumber
, free
);
5437 if (tif
->tif_map_dir_offset_to_number
== NULL
)
5439 TIFFErrorExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5440 "Not enough memory");
5445 if (tif
->tif_map_dir_number_to_offset
== NULL
)
5447 /* No free callback for this map, as it shares the same items as
5448 * tif->tif_map_dir_offset_to_number. */
5449 tif
->tif_map_dir_number_to_offset
= TIFFHashSetNew(
5450 hashFuncNumberToOffset
, equalFuncNumberToOffset
, NULL
);
5451 if (tif
->tif_map_dir_number_to_offset
== NULL
)
5453 TIFFErrorExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5454 "Not enough memory");
5459 /* Check if offset is already in the list:
5460 * - yes: check, if offset is at the same IFD number - if not, it is an IFD
5462 * - no: add to list or update offset at that IFD number
5464 TIFFOffsetAndDirNumber entry
;
5465 entry
.offset
= diroff
;
5466 entry
.dirNumber
= dirn
;
5468 TIFFOffsetAndDirNumber
*foundEntry
=
5469 (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5470 tif
->tif_map_dir_offset_to_number
, &entry
);
5473 if (foundEntry
->dirNumber
== dirn
)
5479 TIFFWarningExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5480 "TIFF directory %d has IFD looping to directory %u "
5481 "at offset 0x%" PRIx64
" (%" PRIu64
")",
5482 (int)dirn
- 1, foundEntry
->dirNumber
, diroff
,
5488 /* Check if offset of an IFD has been changed and update offset of that IFD
5490 foundEntry
= (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5491 tif
->tif_map_dir_number_to_offset
, &entry
);
5494 if (foundEntry
->offset
!= diroff
)
5496 TIFFOffsetAndDirNumber entryOld
;
5497 entryOld
.offset
= foundEntry
->offset
;
5498 entryOld
.dirNumber
= dirn
;
5499 /* We must remove first from tif_map_dir_number_to_offset as the */
5500 /* entry is owned (and thus freed) by */
5501 /* tif_map_dir_offset_to_number */
5502 TIFFOffsetAndDirNumber
*foundEntryOld
=
5503 (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5504 tif
->tif_map_dir_number_to_offset
, &entryOld
);
5507 TIFFHashSetRemove(tif
->tif_map_dir_number_to_offset
,
5510 foundEntryOld
= (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5511 tif
->tif_map_dir_offset_to_number
, &entryOld
);
5514 TIFFHashSetRemove(tif
->tif_map_dir_offset_to_number
,
5518 TIFFOffsetAndDirNumber
*entryPtr
= (TIFFOffsetAndDirNumber
*)malloc(
5519 sizeof(TIFFOffsetAndDirNumber
));
5520 if (entryPtr
== NULL
)
5525 /* Add IFD offset and dirn to IFD directory list */
5528 if (!TIFFHashSetInsert(tif
->tif_map_dir_offset_to_number
, entryPtr
))
5531 tif
, "_TIFFCheckDirNumberAndOffset",
5532 "Insertion in tif_map_dir_offset_to_number failed");
5535 if (!TIFFHashSetInsert(tif
->tif_map_dir_number_to_offset
, entryPtr
))
5538 tif
, "_TIFFCheckDirNumberAndOffset",
5539 "Insertion in tif_map_dir_number_to_offset failed");
5546 /* Arbitrary (hopefully big enough) limit */
5547 if (TIFFHashSetSize(tif
->tif_map_dir_offset_to_number
) >=
5550 TIFFErrorExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5551 "Cannot handle more than %u TIFF directories",
5552 TIFF_MAX_DIR_COUNT
);
5556 TIFFOffsetAndDirNumber
*entryPtr
=
5557 (TIFFOffsetAndDirNumber
*)malloc(sizeof(TIFFOffsetAndDirNumber
));
5558 if (entryPtr
== NULL
)
5560 TIFFErrorExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5561 "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
5565 /* Add IFD offset and dirn to IFD directory list */
5568 if (!TIFFHashSetInsert(tif
->tif_map_dir_offset_to_number
, entryPtr
))
5570 TIFFErrorExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5571 "Insertion in tif_map_dir_offset_to_number failed");
5574 if (!TIFFHashSetInsert(tif
->tif_map_dir_number_to_offset
, entryPtr
))
5576 TIFFErrorExtR(tif
, "_TIFFCheckDirNumberAndOffset",
5577 "Insertion in tif_map_dir_number_to_offset failed");
5582 } /* --- _TIFFCheckDirNumberAndOffset() ---*/
5585 * Retrieve the matching IFD directory number of a given IFD offset
5586 * from the list of directories already seen.
5587 * Returns 1 if the offset was in the list and the directory number
5589 * Otherwise returns 0 or if an error occurred.
5591 int _TIFFGetDirNumberFromOffset(TIFF
*tif
, uint64_t diroff
, tdir_t
*dirn
)
5593 if (diroff
== 0) /* no more directories */
5596 /* Check if offset is already in the list and return matching directory
5597 * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
5598 * search again in IFD list.
5600 if (tif
->tif_map_dir_offset_to_number
== NULL
)
5602 TIFFOffsetAndDirNumber entry
;
5603 entry
.offset
= diroff
;
5604 entry
.dirNumber
= 0; /* not used */
5606 TIFFOffsetAndDirNumber
*foundEntry
=
5607 (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5608 tif
->tif_map_dir_offset_to_number
, &entry
);
5611 *dirn
= foundEntry
->dirNumber
;
5615 /* This updates the directory list for all main-IFDs in the file. */
5616 TIFFNumberOfDirectories(tif
);
5618 foundEntry
= (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5619 tif
->tif_map_dir_offset_to_number
, &entry
);
5622 *dirn
= foundEntry
->dirNumber
;
5627 } /*--- _TIFFGetDirNumberFromOffset() ---*/
5630 * Retrieve the matching IFD directory offset of a given IFD number
5631 * from the list of directories already seen.
5632 * Returns 1 if the offset was in the list of already seen IFDs and the
5633 * directory offset can be returned. The directory list is not updated.
5634 * Otherwise returns 0 or if an error occurred.
5636 int _TIFFGetOffsetFromDirNumber(TIFF
*tif
, tdir_t dirn
, uint64_t *diroff
)
5639 if (tif
->tif_map_dir_number_to_offset
== NULL
)
5641 TIFFOffsetAndDirNumber entry
;
5642 entry
.offset
= 0; /* not used */
5643 entry
.dirNumber
= dirn
;
5645 TIFFOffsetAndDirNumber
*foundEntry
=
5646 (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5647 tif
->tif_map_dir_number_to_offset
, &entry
);
5650 *diroff
= foundEntry
->offset
;
5655 } /*--- _TIFFGetOffsetFromDirNumber() ---*/
5658 * Remove an entry from the directory list of already seen directories
5659 * by directory offset.
5660 * If an entry is to be removed from the list, it is also okay if the entry
5661 * is not in the list or the list does not exist.
5663 int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF
*tif
, uint64_t diroff
)
5665 if (tif
->tif_map_dir_offset_to_number
== NULL
)
5668 TIFFOffsetAndDirNumber entryOld
;
5669 entryOld
.offset
= diroff
;
5670 entryOld
.dirNumber
= 0;
5671 /* We must remove first from tif_map_dir_number_to_offset as the
5672 * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
5673 * However, we need firstly to find the directory number from offset. */
5675 TIFFOffsetAndDirNumber
*foundEntryOldOff
=
5676 (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5677 tif
->tif_map_dir_offset_to_number
, &entryOld
);
5678 if (foundEntryOldOff
)
5680 entryOld
.dirNumber
= foundEntryOldOff
->dirNumber
;
5681 if (tif
->tif_map_dir_number_to_offset
!= NULL
)
5683 TIFFOffsetAndDirNumber
*foundEntryOldDir
=
5684 (TIFFOffsetAndDirNumber
*)TIFFHashSetLookup(
5685 tif
->tif_map_dir_number_to_offset
, &entryOld
);
5686 if (foundEntryOldDir
)
5688 TIFFHashSetRemove(tif
->tif_map_dir_number_to_offset
,
5690 TIFFHashSetRemove(tif
->tif_map_dir_offset_to_number
,
5697 TIFFErrorExtR(tif
, "_TIFFRemoveEntryFromDirectoryListByOffset",
5698 "Unexpectedly tif_map_dir_number_to_offset is "
5699 "missing but tif_map_dir_offset_to_number exists.");
5704 } /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
5707 * Check the count field of a directory entry against a known value. The
5708 * caller is expected to skip/ignore the tag if there is a mismatch.
5710 static int CheckDirCount(TIFF
*tif
, TIFFDirEntry
*dir
, uint32_t count
)
5712 if ((uint64_t)count
> dir
->tdir_count
)
5714 const TIFFField
*fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
5715 TIFFWarningExtR(tif
, tif
->tif_name
,
5716 "incorrect count for field \"%s\" (%" PRIu64
5717 ", expecting %" PRIu32
"); tag ignored",
5718 fip
? fip
->field_name
: "unknown tagname",
5719 dir
->tdir_count
, count
);
5722 else if ((uint64_t)count
< dir
->tdir_count
)
5724 const TIFFField
*fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
5725 TIFFWarningExtR(tif
, tif
->tif_name
,
5726 "incorrect count for field \"%s\" (%" PRIu64
5727 ", expecting %" PRIu32
"); tag trimmed",
5728 fip
? fip
->field_name
: "unknown tagname",
5729 dir
->tdir_count
, count
);
5730 dir
->tdir_count
= count
;
5737 * Read IFD structure from the specified offset. If the pointer to
5738 * nextdiroff variable has been specified, read it too. Function returns a
5739 * number of fields in the directory or 0 if failed.
5741 static uint16_t TIFFFetchDirectory(TIFF
*tif
, uint64_t diroff
,
5742 TIFFDirEntry
**pdir
, uint64_t *nextdiroff
)
5744 static const char module
[] = "TIFFFetchDirectory";
5747 uint16_t dircount16
;
5756 tif
->tif_diroff
= diroff
;
5761 if (!SeekOK(tif
, tif
->tif_diroff
))
5763 TIFFErrorExtR(tif
, module
,
5764 "%s: Seek error accessing TIFF directory",
5768 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5770 if (!ReadOK(tif
, &dircount16
, sizeof(uint16_t)))
5772 TIFFErrorExtR(tif
, module
,
5773 "%s: Can not read TIFF directory count",
5777 if (tif
->tif_flags
& TIFF_SWAB
)
5778 TIFFSwabShort(&dircount16
);
5779 if (dircount16
> 4096)
5781 TIFFErrorExtR(tif
, module
,
5782 "Sanity check on directory count failed, this is "
5783 "probably not a valid IFD offset");
5790 uint64_t dircount64
;
5791 if (!ReadOK(tif
, &dircount64
, sizeof(uint64_t)))
5793 TIFFErrorExtR(tif
, module
,
5794 "%s: Can not read TIFF directory count",
5798 if (tif
->tif_flags
& TIFF_SWAB
)
5799 TIFFSwabLong8(&dircount64
);
5800 if (dircount64
> 4096)
5802 TIFFErrorExtR(tif
, module
,
5803 "Sanity check on directory count failed, this is "
5804 "probably not a valid IFD offset");
5807 dircount16
= (uint16_t)dircount64
;
5810 origdir
= _TIFFCheckMalloc(tif
, dircount16
, dirsize
,
5811 "to read TIFF directory");
5812 if (origdir
== NULL
)
5814 if (!ReadOK(tif
, origdir
, (tmsize_t
)(dircount16
* dirsize
)))
5816 TIFFErrorExtR(tif
, module
, "%.100s: Can not read TIFF directory",
5818 _TIFFfreeExt(tif
, origdir
);
5822 * Read offset to next directory for sequential scans if
5827 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5829 uint32_t nextdiroff32
;
5830 if (!ReadOK(tif
, &nextdiroff32
, sizeof(uint32_t)))
5832 if (tif
->tif_flags
& TIFF_SWAB
)
5833 TIFFSwabLong(&nextdiroff32
);
5834 *nextdiroff
= nextdiroff32
;
5838 if (!ReadOK(tif
, nextdiroff
, sizeof(uint64_t)))
5840 if (tif
->tif_flags
& TIFF_SWAB
)
5841 TIFFSwabLong8(nextdiroff
);
5849 if (tif
->tif_diroff
> (uint64_t)INT64_MAX
)
5851 TIFFErrorExtR(tif
, module
, "Can not read TIFF directory count");
5854 off
= (tmsize_t
)tif
->tif_diroff
;
5857 * Check for integer overflow when validating the dir_off,
5858 * otherwise a very high offset may cause an OOB read and
5859 * crash the client. Make two comparisons instead of
5861 * off + sizeof(uint16_t) > tif->tif_size
5863 * to avoid overflow.
5865 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5867 m
= off
+ sizeof(uint16_t);
5868 if ((m
< off
) || (m
< (tmsize_t
)sizeof(uint16_t)) ||
5869 (m
> tif
->tif_size
))
5871 TIFFErrorExtR(tif
, module
, "Can not read TIFF directory count");
5876 _TIFFmemcpy(&dircount16
, tif
->tif_base
+ off
, sizeof(uint16_t));
5878 off
+= sizeof(uint16_t);
5879 if (tif
->tif_flags
& TIFF_SWAB
)
5880 TIFFSwabShort(&dircount16
);
5881 if (dircount16
> 4096)
5883 TIFFErrorExtR(tif
, module
,
5884 "Sanity check on directory count failed, this is "
5885 "probably not a valid IFD offset");
5892 uint64_t dircount64
;
5893 m
= off
+ sizeof(uint64_t);
5894 if ((m
< off
) || (m
< (tmsize_t
)sizeof(uint64_t)) ||
5895 (m
> tif
->tif_size
))
5897 TIFFErrorExtR(tif
, module
, "Can not read TIFF directory count");
5902 _TIFFmemcpy(&dircount64
, tif
->tif_base
+ off
, sizeof(uint64_t));
5904 off
+= sizeof(uint64_t);
5905 if (tif
->tif_flags
& TIFF_SWAB
)
5906 TIFFSwabLong8(&dircount64
);
5907 if (dircount64
> 4096)
5909 TIFFErrorExtR(tif
, module
,
5910 "Sanity check on directory count failed, this is "
5911 "probably not a valid IFD offset");
5914 dircount16
= (uint16_t)dircount64
;
5917 if (dircount16
== 0)
5919 TIFFErrorExtR(tif
, module
,
5920 "Sanity check on directory count failed, zero tag "
5921 "directories not supported");
5924 origdir
= _TIFFCheckMalloc(tif
, dircount16
, dirsize
,
5925 "to read TIFF directory");
5926 if (origdir
== NULL
)
5928 m
= off
+ dircount16
* dirsize
;
5929 if ((m
< off
) || (m
< (tmsize_t
)(dircount16
* dirsize
)) ||
5930 (m
> tif
->tif_size
))
5932 TIFFErrorExtR(tif
, module
, "Can not read TIFF directory");
5933 _TIFFfreeExt(tif
, origdir
);
5938 _TIFFmemcpy(origdir
, tif
->tif_base
+ off
, dircount16
* dirsize
);
5942 off
+= dircount16
* dirsize
;
5943 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5945 uint32_t nextdiroff32
;
5946 m
= off
+ sizeof(uint32_t);
5947 if ((m
< off
) || (m
< (tmsize_t
)sizeof(uint32_t)) ||
5948 (m
> tif
->tif_size
))
5951 _TIFFmemcpy(&nextdiroff32
, tif
->tif_base
+ off
,
5953 if (tif
->tif_flags
& TIFF_SWAB
)
5954 TIFFSwabLong(&nextdiroff32
);
5955 *nextdiroff
= nextdiroff32
;
5959 m
= off
+ sizeof(uint64_t);
5960 if ((m
< off
) || (m
< (tmsize_t
)sizeof(uint64_t)) ||
5961 (m
> tif
->tif_size
))
5964 _TIFFmemcpy(nextdiroff
, tif
->tif_base
+ off
,
5966 if (tif
->tif_flags
& TIFF_SWAB
)
5967 TIFFSwabLong8(nextdiroff
);
5971 dir
= (TIFFDirEntry
*)_TIFFCheckMalloc(
5972 tif
, dircount16
, sizeof(TIFFDirEntry
), "to read TIFF directory");
5975 _TIFFfreeExt(tif
, origdir
);
5978 ma
= (uint8_t *)origdir
;
5980 for (n
= 0; n
< dircount16
; n
++)
5982 mb
->tdir_ignore
= FALSE
;
5983 if (tif
->tif_flags
& TIFF_SWAB
)
5984 TIFFSwabShort((uint16_t *)ma
);
5985 mb
->tdir_tag
= *(uint16_t *)ma
;
5986 ma
+= sizeof(uint16_t);
5987 if (tif
->tif_flags
& TIFF_SWAB
)
5988 TIFFSwabShort((uint16_t *)ma
);
5989 mb
->tdir_type
= *(uint16_t *)ma
;
5990 ma
+= sizeof(uint16_t);
5991 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
5993 if (tif
->tif_flags
& TIFF_SWAB
)
5994 TIFFSwabLong((uint32_t *)ma
);
5995 mb
->tdir_count
= (uint64_t)(*(uint32_t *)ma
);
5996 ma
+= sizeof(uint32_t);
5997 mb
->tdir_offset
.toff_long8
= 0;
5998 *(uint32_t *)(&mb
->tdir_offset
) = *(uint32_t *)ma
;
5999 ma
+= sizeof(uint32_t);
6003 if (tif
->tif_flags
& TIFF_SWAB
)
6004 TIFFSwabLong8((uint64_t *)ma
);
6005 mb
->tdir_count
= TIFFReadUInt64(ma
);
6006 ma
+= sizeof(uint64_t);
6007 mb
->tdir_offset
.toff_long8
= TIFFReadUInt64(ma
);
6008 ma
+= sizeof(uint64_t);
6012 _TIFFfreeExt(tif
, origdir
);
6018 * Fetch a tag that is not handled by special case code.
6020 static int TIFFFetchNormalTag(TIFF
*tif
, TIFFDirEntry
*dp
, int recover
)
6022 static const char module
[] = "TIFFFetchNormalTag";
6023 enum TIFFReadDirEntryErr err
;
6025 const TIFFField
*fip
= NULL
;
6026 TIFFReadDirectoryFindFieldInfo(tif
, dp
->tdir_tag
, &fii
);
6027 if (fii
== FAILED_FII
)
6029 TIFFErrorExtR(tif
, "TIFFFetchNormalTag",
6030 "No definition found for tag %" PRIu16
, dp
->tdir_tag
);
6033 fip
= tif
->tif_fields
[fii
];
6034 assert(fip
!= NULL
); /* should not happen */
6035 assert(fip
->set_field_type
!=
6036 TIFF_SETGET_OTHER
); /* if so, we shouldn't arrive here but deal with
6037 this in specialized code */
6038 assert(fip
->set_field_type
!=
6039 TIFF_SETGET_INT
); /* if so, we shouldn't arrive here as this is only
6040 the case for pseudo-tags */
6041 err
= TIFFReadDirEntryErrOk
;
6042 switch (fip
->set_field_type
)
6044 case TIFF_SETGET_UNDEFINED
:
6046 tif
, "TIFFFetchNormalTag",
6047 "Defined set_field_type of custom tag %u (%s) is "
6048 "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
6049 fip
->field_tag
, fip
->field_name
);
6051 case TIFF_SETGET_ASCII
:
6054 assert(fip
->field_passcount
== 0);
6055 err
= TIFFReadDirEntryByteArray(tif
, dp
, &data
);
6056 if (err
== TIFFReadDirEntryErrOk
)
6062 if (dp
->tdir_count
> 0 && data
[dp
->tdir_count
- 1] == 0)
6064 /* optimization: if data is known to be 0 terminated, we
6065 * can use strlen() */
6066 mb
= strlen((const char *)data
);
6070 /* general case. equivalent to non-portable */
6071 /* mb = strnlen((const char*)data,
6072 * (uint32_t)dp->tdir_count); */
6074 while (mb
< (uint32_t)dp
->tdir_count
)
6083 if (mb
+ 1 < (uint32_t)dp
->tdir_count
)
6086 "ASCII value for tag \"%s\" contains null byte in "
6087 "value; value incorrectly truncated during reading due "
6088 "to implementation limitations",
6090 else if (mb
+ 1 > (uint32_t)dp
->tdir_count
)
6095 "ASCII value for tag \"%s\" does not end in null byte",
6097 /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
6098 * larger than MAX_SIZE_TAG_DATA */
6099 assert((uint32_t)dp
->tdir_count
+ 1 == dp
->tdir_count
+ 1);
6100 o
= _TIFFmallocExt(tif
, (uint32_t)dp
->tdir_count
+ 1);
6104 _TIFFfreeExt(tif
, data
);
6107 if (dp
->tdir_count
> 0)
6109 _TIFFmemcpy(o
, data
, (uint32_t)dp
->tdir_count
);
6111 o
[(uint32_t)dp
->tdir_count
] = 0;
6113 _TIFFfreeExt(tif
, data
);
6116 n
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6118 _TIFFfreeExt(tif
, data
);
6124 case TIFF_SETGET_UINT8
:
6127 assert(fip
->field_readcount
== 1);
6128 assert(fip
->field_passcount
== 0);
6129 err
= TIFFReadDirEntryByte(tif
, dp
, &data
);
6130 if (err
== TIFFReadDirEntryErrOk
)
6132 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6137 case TIFF_SETGET_SINT8
:
6140 assert(fip
->field_readcount
== 1);
6141 assert(fip
->field_passcount
== 0);
6142 err
= TIFFReadDirEntrySbyte(tif
, dp
, &data
);
6143 if (err
== TIFFReadDirEntryErrOk
)
6145 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6150 case TIFF_SETGET_UINT16
:
6153 assert(fip
->field_readcount
== 1);
6154 assert(fip
->field_passcount
== 0);
6155 err
= TIFFReadDirEntryShort(tif
, dp
, &data
);
6156 if (err
== TIFFReadDirEntryErrOk
)
6158 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6163 case TIFF_SETGET_SINT16
:
6166 assert(fip
->field_readcount
== 1);
6167 assert(fip
->field_passcount
== 0);
6168 err
= TIFFReadDirEntrySshort(tif
, dp
, &data
);
6169 if (err
== TIFFReadDirEntryErrOk
)
6171 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6176 case TIFF_SETGET_UINT32
:
6179 assert(fip
->field_readcount
== 1);
6180 assert(fip
->field_passcount
== 0);
6181 err
= TIFFReadDirEntryLong(tif
, dp
, &data
);
6182 if (err
== TIFFReadDirEntryErrOk
)
6184 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6189 case TIFF_SETGET_SINT32
:
6192 assert(fip
->field_readcount
== 1);
6193 assert(fip
->field_passcount
== 0);
6194 err
= TIFFReadDirEntrySlong(tif
, dp
, &data
);
6195 if (err
== TIFFReadDirEntryErrOk
)
6197 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6202 case TIFF_SETGET_UINT64
:
6205 assert(fip
->field_readcount
== 1);
6206 assert(fip
->field_passcount
== 0);
6207 err
= TIFFReadDirEntryLong8(tif
, dp
, &data
);
6208 if (err
== TIFFReadDirEntryErrOk
)
6210 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6215 case TIFF_SETGET_SINT64
:
6218 assert(fip
->field_readcount
== 1);
6219 assert(fip
->field_passcount
== 0);
6220 err
= TIFFReadDirEntrySlong8(tif
, dp
, &data
);
6221 if (err
== TIFFReadDirEntryErrOk
)
6223 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6228 case TIFF_SETGET_FLOAT
:
6231 assert(fip
->field_readcount
== 1);
6232 assert(fip
->field_passcount
== 0);
6233 err
= TIFFReadDirEntryFloat(tif
, dp
, &data
);
6234 if (err
== TIFFReadDirEntryErrOk
)
6236 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6241 case TIFF_SETGET_DOUBLE
:
6244 assert(fip
->field_readcount
== 1);
6245 assert(fip
->field_passcount
== 0);
6246 err
= TIFFReadDirEntryDouble(tif
, dp
, &data
);
6247 if (err
== TIFFReadDirEntryErrOk
)
6249 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6254 case TIFF_SETGET_IFD8
:
6257 assert(fip
->field_readcount
== 1);
6258 assert(fip
->field_passcount
== 0);
6259 err
= TIFFReadDirEntryIfd8(tif
, dp
, &data
);
6260 if (err
== TIFFReadDirEntryErrOk
)
6262 if (!TIFFSetField(tif
, dp
->tdir_tag
, data
))
6267 case TIFF_SETGET_UINT16_PAIR
:
6270 assert(fip
->field_readcount
== 2);
6271 assert(fip
->field_passcount
== 0);
6272 if (dp
->tdir_count
!= 2)
6274 TIFFWarningExtR(tif
, module
,
6275 "incorrect count for field \"%s\", expected 2, "
6277 fip
->field_name
, dp
->tdir_count
);
6280 err
= TIFFReadDirEntryShortArray(tif
, dp
, &data
);
6281 if (err
== TIFFReadDirEntryErrOk
)
6284 assert(data
); /* avoid CLang static Analyzer false positive */
6285 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
[0], data
[1]);
6286 _TIFFfreeExt(tif
, data
);
6292 case TIFF_SETGET_C0_UINT8
:
6295 assert(fip
->field_readcount
>= 1);
6296 assert(fip
->field_passcount
== 0);
6297 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6299 TIFFWarningExtR(tif
, module
,
6300 "incorrect count for field \"%s\", expected "
6302 fip
->field_name
, (int)fip
->field_readcount
,
6308 err
= TIFFReadDirEntryByteArray(tif
, dp
, &data
);
6309 if (err
== TIFFReadDirEntryErrOk
)
6312 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6314 _TIFFfreeExt(tif
, data
);
6321 case TIFF_SETGET_C0_SINT8
:
6324 assert(fip
->field_readcount
>= 1);
6325 assert(fip
->field_passcount
== 0);
6326 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6328 TIFFWarningExtR(tif
, module
,
6329 "incorrect count for field \"%s\", expected "
6331 fip
->field_name
, (int)fip
->field_readcount
,
6337 err
= TIFFReadDirEntrySbyteArray(tif
, dp
, &data
);
6338 if (err
== TIFFReadDirEntryErrOk
)
6341 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6343 _TIFFfreeExt(tif
, data
);
6350 case TIFF_SETGET_C0_UINT16
:
6353 assert(fip
->field_readcount
>= 1);
6354 assert(fip
->field_passcount
== 0);
6355 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6357 TIFFWarningExtR(tif
, module
,
6358 "incorrect count for field \"%s\", expected "
6360 fip
->field_name
, (int)fip
->field_readcount
,
6366 err
= TIFFReadDirEntryShortArray(tif
, dp
, &data
);
6367 if (err
== TIFFReadDirEntryErrOk
)
6370 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6372 _TIFFfreeExt(tif
, data
);
6379 case TIFF_SETGET_C0_SINT16
:
6382 assert(fip
->field_readcount
>= 1);
6383 assert(fip
->field_passcount
== 0);
6384 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6386 TIFFWarningExtR(tif
, module
,
6387 "incorrect count for field \"%s\", expected "
6389 fip
->field_name
, (int)fip
->field_readcount
,
6395 err
= TIFFReadDirEntrySshortArray(tif
, dp
, &data
);
6396 if (err
== TIFFReadDirEntryErrOk
)
6399 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6401 _TIFFfreeExt(tif
, data
);
6408 case TIFF_SETGET_C0_UINT32
:
6411 assert(fip
->field_readcount
>= 1);
6412 assert(fip
->field_passcount
== 0);
6413 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6415 TIFFWarningExtR(tif
, module
,
6416 "incorrect count for field \"%s\", expected "
6418 fip
->field_name
, (int)fip
->field_readcount
,
6424 err
= TIFFReadDirEntryLongArray(tif
, dp
, &data
);
6425 if (err
== TIFFReadDirEntryErrOk
)
6428 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6430 _TIFFfreeExt(tif
, data
);
6437 case TIFF_SETGET_C0_SINT32
:
6440 assert(fip
->field_readcount
>= 1);
6441 assert(fip
->field_passcount
== 0);
6442 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6444 TIFFWarningExtR(tif
, module
,
6445 "incorrect count for field \"%s\", expected "
6447 fip
->field_name
, (int)fip
->field_readcount
,
6453 err
= TIFFReadDirEntrySlongArray(tif
, dp
, &data
);
6454 if (err
== TIFFReadDirEntryErrOk
)
6457 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6459 _TIFFfreeExt(tif
, data
);
6466 case TIFF_SETGET_C0_UINT64
:
6469 assert(fip
->field_readcount
>= 1);
6470 assert(fip
->field_passcount
== 0);
6471 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6473 TIFFWarningExtR(tif
, module
,
6474 "incorrect count for field \"%s\", expected "
6476 fip
->field_name
, (int)fip
->field_readcount
,
6482 err
= TIFFReadDirEntryLong8Array(tif
, dp
, &data
);
6483 if (err
== TIFFReadDirEntryErrOk
)
6486 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6488 _TIFFfreeExt(tif
, data
);
6495 case TIFF_SETGET_C0_SINT64
:
6498 assert(fip
->field_readcount
>= 1);
6499 assert(fip
->field_passcount
== 0);
6500 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6502 TIFFWarningExtR(tif
, module
,
6503 "incorrect count for field \"%s\", expected "
6505 fip
->field_name
, (int)fip
->field_readcount
,
6511 err
= TIFFReadDirEntrySlong8Array(tif
, dp
, &data
);
6512 if (err
== TIFFReadDirEntryErrOk
)
6515 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6517 _TIFFfreeExt(tif
, data
);
6524 case TIFF_SETGET_C0_FLOAT
:
6527 assert(fip
->field_readcount
>= 1);
6528 assert(fip
->field_passcount
== 0);
6529 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6531 TIFFWarningExtR(tif
, module
,
6532 "incorrect count for field \"%s\", expected "
6534 fip
->field_name
, (int)fip
->field_readcount
,
6540 err
= TIFFReadDirEntryFloatArray(tif
, dp
, &data
);
6541 if (err
== TIFFReadDirEntryErrOk
)
6544 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6546 _TIFFfreeExt(tif
, data
);
6553 /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6554 * into Double-Arrays. */
6555 case TIFF_SETGET_C0_DOUBLE
:
6558 assert(fip
->field_readcount
>= 1);
6559 assert(fip
->field_passcount
== 0);
6560 if (dp
->tdir_count
!= (uint64_t)fip
->field_readcount
)
6562 TIFFWarningExtR(tif
, module
,
6563 "incorrect count for field \"%s\", expected "
6565 fip
->field_name
, (int)fip
->field_readcount
,
6571 err
= TIFFReadDirEntryDoubleArray(tif
, dp
, &data
);
6572 if (err
== TIFFReadDirEntryErrOk
)
6575 m
= TIFFSetField(tif
, dp
->tdir_tag
, data
);
6577 _TIFFfreeExt(tif
, data
);
6584 case TIFF_SETGET_C16_ASCII
:
6587 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6588 assert(fip
->field_passcount
== 1);
6589 if (dp
->tdir_count
> 0xFFFF)
6590 err
= TIFFReadDirEntryErrCount
;
6593 err
= TIFFReadDirEntryByteArray(tif
, dp
, &data
);
6594 if (err
== TIFFReadDirEntryErrOk
)
6597 if (data
!= 0 && dp
->tdir_count
> 0 &&
6598 data
[dp
->tdir_count
- 1] != '\0')
6602 "ASCII value for tag \"%s\" does not end in null "
6603 "byte. Forcing it to be null",
6605 data
[dp
->tdir_count
- 1] = '\0';
6607 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6608 (uint16_t)(dp
->tdir_count
), data
);
6610 _TIFFfreeExt(tif
, data
);
6617 case TIFF_SETGET_C16_UINT8
:
6620 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6621 assert(fip
->field_passcount
== 1);
6622 if (dp
->tdir_count
> 0xFFFF)
6623 err
= TIFFReadDirEntryErrCount
;
6626 err
= TIFFReadDirEntryByteArray(tif
, dp
, &data
);
6627 if (err
== TIFFReadDirEntryErrOk
)
6630 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6631 (uint16_t)(dp
->tdir_count
), data
);
6633 _TIFFfreeExt(tif
, data
);
6640 case TIFF_SETGET_C16_SINT8
:
6643 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6644 assert(fip
->field_passcount
== 1);
6645 if (dp
->tdir_count
> 0xFFFF)
6646 err
= TIFFReadDirEntryErrCount
;
6649 err
= TIFFReadDirEntrySbyteArray(tif
, dp
, &data
);
6650 if (err
== TIFFReadDirEntryErrOk
)
6653 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6654 (uint16_t)(dp
->tdir_count
), data
);
6656 _TIFFfreeExt(tif
, data
);
6663 case TIFF_SETGET_C16_UINT16
:
6666 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6667 assert(fip
->field_passcount
== 1);
6668 if (dp
->tdir_count
> 0xFFFF)
6669 err
= TIFFReadDirEntryErrCount
;
6672 err
= TIFFReadDirEntryShortArray(tif
, dp
, &data
);
6673 if (err
== TIFFReadDirEntryErrOk
)
6676 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6677 (uint16_t)(dp
->tdir_count
), data
);
6679 _TIFFfreeExt(tif
, data
);
6686 case TIFF_SETGET_C16_SINT16
:
6689 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6690 assert(fip
->field_passcount
== 1);
6691 if (dp
->tdir_count
> 0xFFFF)
6692 err
= TIFFReadDirEntryErrCount
;
6695 err
= TIFFReadDirEntrySshortArray(tif
, dp
, &data
);
6696 if (err
== TIFFReadDirEntryErrOk
)
6699 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6700 (uint16_t)(dp
->tdir_count
), data
);
6702 _TIFFfreeExt(tif
, data
);
6709 case TIFF_SETGET_C16_UINT32
:
6712 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6713 assert(fip
->field_passcount
== 1);
6714 if (dp
->tdir_count
> 0xFFFF)
6715 err
= TIFFReadDirEntryErrCount
;
6718 err
= TIFFReadDirEntryLongArray(tif
, dp
, &data
);
6719 if (err
== TIFFReadDirEntryErrOk
)
6722 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6723 (uint16_t)(dp
->tdir_count
), data
);
6725 _TIFFfreeExt(tif
, data
);
6732 case TIFF_SETGET_C16_SINT32
:
6735 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6736 assert(fip
->field_passcount
== 1);
6737 if (dp
->tdir_count
> 0xFFFF)
6738 err
= TIFFReadDirEntryErrCount
;
6741 err
= TIFFReadDirEntrySlongArray(tif
, dp
, &data
);
6742 if (err
== TIFFReadDirEntryErrOk
)
6745 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6746 (uint16_t)(dp
->tdir_count
), data
);
6748 _TIFFfreeExt(tif
, data
);
6755 case TIFF_SETGET_C16_UINT64
:
6758 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6759 assert(fip
->field_passcount
== 1);
6760 if (dp
->tdir_count
> 0xFFFF)
6761 err
= TIFFReadDirEntryErrCount
;
6764 err
= TIFFReadDirEntryLong8Array(tif
, dp
, &data
);
6765 if (err
== TIFFReadDirEntryErrOk
)
6768 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6769 (uint16_t)(dp
->tdir_count
), data
);
6771 _TIFFfreeExt(tif
, data
);
6778 case TIFF_SETGET_C16_SINT64
:
6781 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6782 assert(fip
->field_passcount
== 1);
6783 if (dp
->tdir_count
> 0xFFFF)
6784 err
= TIFFReadDirEntryErrCount
;
6787 err
= TIFFReadDirEntrySlong8Array(tif
, dp
, &data
);
6788 if (err
== TIFFReadDirEntryErrOk
)
6791 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6792 (uint16_t)(dp
->tdir_count
), data
);
6794 _TIFFfreeExt(tif
, data
);
6801 case TIFF_SETGET_C16_FLOAT
:
6804 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6805 assert(fip
->field_passcount
== 1);
6806 if (dp
->tdir_count
> 0xFFFF)
6807 err
= TIFFReadDirEntryErrCount
;
6810 err
= TIFFReadDirEntryFloatArray(tif
, dp
, &data
);
6811 if (err
== TIFFReadDirEntryErrOk
)
6814 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6815 (uint16_t)(dp
->tdir_count
), data
);
6817 _TIFFfreeExt(tif
, data
);
6824 case TIFF_SETGET_C16_DOUBLE
:
6827 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6828 assert(fip
->field_passcount
== 1);
6829 if (dp
->tdir_count
> 0xFFFF)
6830 err
= TIFFReadDirEntryErrCount
;
6833 err
= TIFFReadDirEntryDoubleArray(tif
, dp
, &data
);
6834 if (err
== TIFFReadDirEntryErrOk
)
6837 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6838 (uint16_t)(dp
->tdir_count
), data
);
6840 _TIFFfreeExt(tif
, data
);
6847 case TIFF_SETGET_C16_IFD8
:
6850 assert(fip
->field_readcount
== TIFF_VARIABLE
);
6851 assert(fip
->field_passcount
== 1);
6852 if (dp
->tdir_count
> 0xFFFF)
6853 err
= TIFFReadDirEntryErrCount
;
6856 err
= TIFFReadDirEntryIfd8Array(tif
, dp
, &data
);
6857 if (err
== TIFFReadDirEntryErrOk
)
6860 m
= TIFFSetField(tif
, dp
->tdir_tag
,
6861 (uint16_t)(dp
->tdir_count
), data
);
6863 _TIFFfreeExt(tif
, data
);
6870 case TIFF_SETGET_C32_ASCII
:
6873 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
6874 assert(fip
->field_passcount
== 1);
6875 err
= TIFFReadDirEntryByteArray(tif
, dp
, &data
);
6876 if (err
== TIFFReadDirEntryErrOk
)
6879 if (data
!= 0 && dp
->tdir_count
> 0 &&
6880 data
[dp
->tdir_count
- 1] != '\0')
6882 TIFFWarningExtR(tif
, module
,
6883 "ASCII value for tag \"%s\" does not end "
6884 "in null byte. Forcing it to be null",
6886 data
[dp
->tdir_count
- 1] = '\0';
6888 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
6891 _TIFFfreeExt(tif
, data
);
6897 case TIFF_SETGET_C32_UINT8
:
6901 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
6902 assert(fip
->field_passcount
== 1);
6903 if (fip
->field_tag
== TIFFTAG_RICHTIFFIPTC
&&
6904 dp
->tdir_type
== TIFF_LONG
)
6906 /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
6907 * data type LONG instead of UNDEFINED. Work around this
6908 * frequently found issue */
6910 err
= TIFFReadDirEntryArray(tif
, dp
, &count
, 4, &origdata
);
6911 if ((err
!= TIFFReadDirEntryErrOk
) || (origdata
== 0))
6917 if (tif
->tif_flags
& TIFF_SWAB
)
6918 TIFFSwabArrayOfLong((uint32_t *)origdata
, count
);
6919 data
= (uint8_t *)origdata
;
6920 count
= (uint32_t)(count
* 4);
6925 err
= TIFFReadDirEntryByteArray(tif
, dp
, &data
);
6926 count
= (uint32_t)(dp
->tdir_count
);
6928 if (err
== TIFFReadDirEntryErrOk
)
6931 m
= TIFFSetField(tif
, dp
->tdir_tag
, count
, data
);
6933 _TIFFfreeExt(tif
, data
);
6939 case TIFF_SETGET_C32_SINT8
:
6941 int8_t *data
= NULL
;
6942 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
6943 assert(fip
->field_passcount
== 1);
6944 err
= TIFFReadDirEntrySbyteArray(tif
, dp
, &data
);
6945 if (err
== TIFFReadDirEntryErrOk
)
6948 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
6951 _TIFFfreeExt(tif
, data
);
6957 case TIFF_SETGET_C32_UINT16
:
6960 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
6961 assert(fip
->field_passcount
== 1);
6962 err
= TIFFReadDirEntryShortArray(tif
, dp
, &data
);
6963 if (err
== TIFFReadDirEntryErrOk
)
6966 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
6969 _TIFFfreeExt(tif
, data
);
6975 case TIFF_SETGET_C32_SINT16
:
6977 int16_t *data
= NULL
;
6978 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
6979 assert(fip
->field_passcount
== 1);
6980 err
= TIFFReadDirEntrySshortArray(tif
, dp
, &data
);
6981 if (err
== TIFFReadDirEntryErrOk
)
6984 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
6987 _TIFFfreeExt(tif
, data
);
6993 case TIFF_SETGET_C32_UINT32
:
6996 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
6997 assert(fip
->field_passcount
== 1);
6998 err
= TIFFReadDirEntryLongArray(tif
, dp
, &data
);
6999 if (err
== TIFFReadDirEntryErrOk
)
7002 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7005 _TIFFfreeExt(tif
, data
);
7011 case TIFF_SETGET_C32_SINT32
:
7013 int32_t *data
= NULL
;
7014 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
7015 assert(fip
->field_passcount
== 1);
7016 err
= TIFFReadDirEntrySlongArray(tif
, dp
, &data
);
7017 if (err
== TIFFReadDirEntryErrOk
)
7020 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7023 _TIFFfreeExt(tif
, data
);
7029 case TIFF_SETGET_C32_UINT64
:
7032 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
7033 assert(fip
->field_passcount
== 1);
7034 err
= TIFFReadDirEntryLong8Array(tif
, dp
, &data
);
7035 if (err
== TIFFReadDirEntryErrOk
)
7038 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7041 _TIFFfreeExt(tif
, data
);
7047 case TIFF_SETGET_C32_SINT64
:
7049 int64_t *data
= NULL
;
7050 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
7051 assert(fip
->field_passcount
== 1);
7052 err
= TIFFReadDirEntrySlong8Array(tif
, dp
, &data
);
7053 if (err
== TIFFReadDirEntryErrOk
)
7056 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7059 _TIFFfreeExt(tif
, data
);
7065 case TIFF_SETGET_C32_FLOAT
:
7068 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
7069 assert(fip
->field_passcount
== 1);
7070 err
= TIFFReadDirEntryFloatArray(tif
, dp
, &data
);
7071 if (err
== TIFFReadDirEntryErrOk
)
7074 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7077 _TIFFfreeExt(tif
, data
);
7083 case TIFF_SETGET_C32_DOUBLE
:
7086 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
7087 assert(fip
->field_passcount
== 1);
7088 err
= TIFFReadDirEntryDoubleArray(tif
, dp
, &data
);
7089 if (err
== TIFFReadDirEntryErrOk
)
7092 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7095 _TIFFfreeExt(tif
, data
);
7101 case TIFF_SETGET_C32_IFD8
:
7104 assert(fip
->field_readcount
== TIFF_VARIABLE2
);
7105 assert(fip
->field_passcount
== 1);
7106 err
= TIFFReadDirEntryIfd8Array(tif
, dp
, &data
);
7107 if (err
== TIFFReadDirEntryErrOk
)
7110 m
= TIFFSetField(tif
, dp
->tdir_tag
, (uint32_t)(dp
->tdir_count
),
7113 _TIFFfreeExt(tif
, data
);
7120 assert(0); /* we should never get here */
7123 if (err
!= TIFFReadDirEntryErrOk
)
7125 TIFFReadDirEntryOutputErr(tif
, err
, module
, fip
->field_name
, recover
);
7132 * Fetch a set of offsets or lengths.
7133 * While this routine says "strips", in fact it's also used for tiles.
7135 static int TIFFFetchStripThing(TIFF
*tif
, TIFFDirEntry
*dir
, uint32_t nstrips
,
7138 static const char module
[] = "TIFFFetchStripThing";
7139 enum TIFFReadDirEntryErr err
;
7141 err
= TIFFReadDirEntryLong8ArrayWithLimit(tif
, dir
, &data
, nstrips
);
7142 if (err
!= TIFFReadDirEntryErrOk
)
7144 const TIFFField
*fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
7145 TIFFReadDirEntryOutputErr(tif
, err
, module
,
7146 fip
? fip
->field_name
: "unknown tagname", 0);
7149 if (dir
->tdir_count
< (uint64_t)nstrips
)
7151 uint64_t *resizeddata
;
7152 const TIFFField
*fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
7153 const char *pszMax
= getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
7154 uint32_t max_nstrips
= 1000000;
7156 max_nstrips
= (uint32_t)atoi(pszMax
);
7157 TIFFReadDirEntryOutputErr(tif
, TIFFReadDirEntryErrCount
, module
,
7158 fip
? fip
->field_name
: "unknown tagname",
7159 (nstrips
<= max_nstrips
));
7161 if (nstrips
> max_nstrips
)
7163 _TIFFfreeExt(tif
, data
);
7167 resizeddata
= (uint64_t *)_TIFFCheckMalloc(
7168 tif
, nstrips
, sizeof(uint64_t), "for strip array");
7169 if (resizeddata
== 0)
7171 _TIFFfreeExt(tif
, data
);
7174 if (dir
->tdir_count
)
7175 _TIFFmemcpy(resizeddata
, data
,
7176 (uint32_t)dir
->tdir_count
* sizeof(uint64_t));
7177 _TIFFmemset(resizeddata
+ (uint32_t)dir
->tdir_count
, 0,
7178 (nstrips
- (uint32_t)dir
->tdir_count
) * sizeof(uint64_t));
7179 _TIFFfreeExt(tif
, data
);
7187 * Fetch and set the SubjectDistance EXIF tag.
7189 static int TIFFFetchSubjectDistance(TIFF
*tif
, TIFFDirEntry
*dir
)
7191 static const char module
[] = "TIFFFetchSubjectDistance";
7192 enum TIFFReadDirEntryErr err
;
7195 assert(sizeof(double) == 8);
7196 assert(sizeof(uint64_t) == 8);
7197 assert(sizeof(uint32_t) == 4);
7198 if (dir
->tdir_count
!= 1)
7199 err
= TIFFReadDirEntryErrCount
;
7200 else if (dir
->tdir_type
!= TIFF_RATIONAL
)
7201 err
= TIFFReadDirEntryErrType
;
7204 if (!(tif
->tif_flags
& TIFF_BIGTIFF
))
7207 offset
= *(uint32_t *)(&dir
->tdir_offset
);
7208 if (tif
->tif_flags
& TIFF_SWAB
)
7209 TIFFSwabLong(&offset
);
7210 err
= TIFFReadDirEntryData(tif
, offset
, 8, m
.i
);
7214 m
.l
= dir
->tdir_offset
.toff_long8
;
7215 err
= TIFFReadDirEntryErrOk
;
7218 if (err
== TIFFReadDirEntryErrOk
)
7221 if (tif
->tif_flags
& TIFF_SWAB
)
7222 TIFFSwabArrayOfLong(m
.i
, 2);
7225 else if (m
.i
[0] == 0xFFFFFFFF || m
.i
[1] == 0)
7227 * XXX: Numerator 0xFFFFFFFF means that we have infinite
7228 * distance. Indicate that with a negative floating point
7229 * SubjectDistance value.
7233 n
= (double)m
.i
[0] / (double)m
.i
[1];
7234 return (TIFFSetField(tif
, dir
->tdir_tag
, n
));
7238 TIFFReadDirEntryOutputErr(tif
, err
, module
, "SubjectDistance", TRUE
);
7243 static void allocChoppedUpStripArrays(TIFF
*tif
, uint32_t nstrips
,
7244 uint64_t stripbytes
,
7245 uint32_t rowsperstrip
)
7247 TIFFDirectory
*td
= &tif
->tif_dir
;
7250 uint64_t last_offset
;
7251 uint64_t last_bytecount
;
7253 uint64_t *newcounts
;
7254 uint64_t *newoffsets
;
7256 offset
= TIFFGetStrileOffset(tif
, 0);
7257 last_offset
= TIFFGetStrileOffset(tif
, td
->td_nstrips
- 1);
7258 last_bytecount
= TIFFGetStrileByteCount(tif
, td
->td_nstrips
- 1);
7259 if (last_offset
> UINT64_MAX
- last_bytecount
||
7260 last_offset
+ last_bytecount
< offset
)
7264 bytecount
= last_offset
+ last_bytecount
- offset
;
7267 (uint64_t *)_TIFFCheckMalloc(tif
, nstrips
, sizeof(uint64_t),
7268 "for chopped \"StripByteCounts\" array");
7269 newoffsets
= (uint64_t *)_TIFFCheckMalloc(
7270 tif
, nstrips
, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7271 if (newcounts
== NULL
|| newoffsets
== NULL
)
7274 * Unable to allocate new strip information, give up and use
7275 * the original one strip information.
7277 if (newcounts
!= NULL
)
7278 _TIFFfreeExt(tif
, newcounts
);
7279 if (newoffsets
!= NULL
)
7280 _TIFFfreeExt(tif
, newoffsets
);
7285 * Fill the strip information arrays with new bytecounts and offsets
7286 * that reflect the broken-up format.
7288 for (i
= 0; i
< nstrips
; i
++)
7290 if (stripbytes
> bytecount
)
7291 stripbytes
= bytecount
;
7292 newcounts
[i
] = stripbytes
;
7293 newoffsets
[i
] = stripbytes
? offset
: 0;
7294 offset
+= stripbytes
;
7295 bytecount
-= stripbytes
;
7299 * Replace old single strip info with multi-strip info.
7301 td
->td_stripsperimage
= td
->td_nstrips
= nstrips
;
7302 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rowsperstrip
);
7304 _TIFFfreeExt(tif
, td
->td_stripbytecount_p
);
7305 _TIFFfreeExt(tif
, td
->td_stripoffset_p
);
7306 td
->td_stripbytecount_p
= newcounts
;
7307 td
->td_stripoffset_p
= newoffsets
;
7308 #ifdef STRIPBYTECOUNTSORTED_UNUSED
7309 td
->td_stripbytecountsorted
= 1;
7311 tif
->tif_flags
|= TIFF_CHOPPEDUPARRAYS
;
7315 * Replace a single strip (tile) of uncompressed data by multiple strips
7316 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
7317 * dealing with large images or for dealing with machines with a limited
7320 static void ChopUpSingleUncompressedStrip(TIFF
*tif
)
7322 register TIFFDirectory
*td
= &tif
->tif_dir
;
7326 uint64_t rowblockbytes
;
7327 uint64_t stripbytes
;
7329 uint32_t rowsperstrip
;
7331 bytecount
= TIFFGetStrileByteCount(tif
, 0);
7332 /* On a newly created file, just re-opened to be filled, we */
7333 /* don't want strip chop to trigger as it is going to cause issues */
7334 /* later ( StripOffsets and StripByteCounts improperly filled) . */
7335 if (bytecount
== 0 && tif
->tif_mode
!= O_RDONLY
)
7337 offset
= TIFFGetStrileByteCount(tif
, 0);
7338 assert(td
->td_planarconfig
== PLANARCONFIG_CONTIG
);
7339 if ((td
->td_photometric
== PHOTOMETRIC_YCBCR
) && (!isUpSampled(tif
)))
7340 rowblock
= td
->td_ycbcrsubsampling
[1];
7343 rowblockbytes
= TIFFVTileSize64(tif
, rowblock
);
7345 * Make the rows hold at least one scanline, but fill specified amount
7346 * of data if possible.
7348 if (rowblockbytes
> STRIP_SIZE_DEFAULT
)
7350 stripbytes
= rowblockbytes
;
7351 rowsperstrip
= rowblock
;
7353 else if (rowblockbytes
> 0)
7355 uint32_t rowblocksperstrip
;
7356 rowblocksperstrip
= (uint32_t)(STRIP_SIZE_DEFAULT
/ rowblockbytes
);
7357 rowsperstrip
= rowblocksperstrip
* rowblock
;
7358 stripbytes
= rowblocksperstrip
* rowblockbytes
;
7364 * never increase the number of rows per strip
7366 if (rowsperstrip
>= td
->td_rowsperstrip
)
7368 nstrips
= TIFFhowmany_32(td
->td_imagelength
, rowsperstrip
);
7372 /* If we are going to allocate a lot of memory, make sure that the */
7373 /* file is as big as needed */
7374 if (tif
->tif_mode
== O_RDONLY
&& nstrips
> 1000000 &&
7375 (offset
>= TIFFGetFileSize(tif
) ||
7376 stripbytes
> (TIFFGetFileSize(tif
) - offset
) / (nstrips
- 1)))
7381 allocChoppedUpStripArrays(tif
, nstrips
, stripbytes
, rowsperstrip
);
7385 * Replace a file with contiguous strips > 2 GB of uncompressed data by
7386 * multiple smaller strips. This is useful for
7387 * dealing with large images or for dealing with machines with a limited
7390 static void TryChopUpUncompressedBigTiff(TIFF
*tif
)
7392 TIFFDirectory
*td
= &tif
->tif_dir
;
7394 uint64_t rowblockbytes
;
7397 uint32_t rowblocksperstrip
;
7398 uint32_t rowsperstrip
;
7399 uint64_t stripbytes
;
7402 stripsize
= TIFFStripSize64(tif
);
7404 assert(tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
);
7405 assert(tif
->tif_dir
.td_compression
== COMPRESSION_NONE
);
7406 assert((tif
->tif_flags
& (TIFF_STRIPCHOP
| TIFF_ISTILED
)) ==
7408 assert(stripsize
> 0x7FFFFFFFUL
);
7410 /* On a newly created file, just re-opened to be filled, we */
7411 /* don't want strip chop to trigger as it is going to cause issues */
7412 /* later ( StripOffsets and StripByteCounts improperly filled) . */
7413 if (TIFFGetStrileByteCount(tif
, 0) == 0 && tif
->tif_mode
!= O_RDONLY
)
7416 if ((td
->td_photometric
== PHOTOMETRIC_YCBCR
) && (!isUpSampled(tif
)))
7417 rowblock
= td
->td_ycbcrsubsampling
[1];
7420 rowblockbytes
= TIFFVStripSize64(tif
, rowblock
);
7421 if (rowblockbytes
== 0 || rowblockbytes
> 0x7FFFFFFFUL
)
7423 /* In case of file with gigantic width */
7427 /* Check that the strips are contiguous and of the expected size */
7428 for (i
= 0; i
< td
->td_nstrips
; i
++)
7430 if (i
== td
->td_nstrips
- 1)
7432 if (TIFFGetStrileByteCount(tif
, i
) <
7433 TIFFVStripSize64(tif
,
7434 td
->td_imagelength
- i
* td
->td_rowsperstrip
))
7441 if (TIFFGetStrileByteCount(tif
, i
) != stripsize
)
7445 if (i
> 0 && TIFFGetStrileOffset(tif
, i
) !=
7446 TIFFGetStrileOffset(tif
, i
- 1) +
7447 TIFFGetStrileByteCount(tif
, i
- 1))
7454 /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
7455 rowblocksperstrip
= (uint32_t)(512 * 1024 * 1024 / rowblockbytes
);
7456 if (rowblocksperstrip
== 0)
7457 rowblocksperstrip
= 1;
7458 rowsperstrip
= rowblocksperstrip
* rowblock
;
7459 stripbytes
= rowblocksperstrip
* rowblockbytes
;
7460 assert(stripbytes
<= 0x7FFFFFFFUL
);
7462 nstrips
= TIFFhowmany_32(td
->td_imagelength
, rowsperstrip
);
7466 /* If we are going to allocate a lot of memory, make sure that the */
7467 /* file is as big as needed */
7468 if (tif
->tif_mode
== O_RDONLY
&& nstrips
> 1000000)
7470 uint64_t last_offset
= TIFFGetStrileOffset(tif
, td
->td_nstrips
- 1);
7471 uint64_t filesize
= TIFFGetFileSize(tif
);
7472 uint64_t last_bytecount
=
7473 TIFFGetStrileByteCount(tif
, td
->td_nstrips
- 1);
7474 if (last_offset
> filesize
|| last_bytecount
> filesize
- last_offset
)
7480 allocChoppedUpStripArrays(tif
, nstrips
, stripbytes
, rowsperstrip
);
7483 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
7484 static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a
, int b
)
7489 /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
7490 * strip/tile of number strile. Also fetch the neighbouring values using a
7491 * 4096 byte page size.
7493 static int _TIFFPartialReadStripArray(TIFF
*tif
, TIFFDirEntry
*dirent
,
7494 int strile
, uint64_t *panVals
)
7496 static const char module
[] = "_TIFFPartialReadStripArray";
7497 #define IO_CACHE_PAGE_SIZE 4096
7500 const int bSwab
= (tif
->tif_flags
& TIFF_SWAB
) != 0;
7502 uint64_t nBaseOffset
;
7504 uint64_t nOffsetStartPage
;
7505 uint64_t nOffsetEndPage
;
7508 uint64_t nLastStripOffset
;
7511 const uint32_t arraySize
= tif
->tif_dir
.td_stripoffsetbyteallocsize
;
7512 unsigned char buffer
[2 * IO_CACHE_PAGE_SIZE
];
7514 assert(dirent
->tdir_count
> 4);
7516 if (dirent
->tdir_type
== TIFF_SHORT
)
7518 sizeofval
= sizeof(uint16_t);
7520 else if (dirent
->tdir_type
== TIFF_LONG
)
7522 sizeofval
= sizeof(uint32_t);
7524 else if (dirent
->tdir_type
== TIFF_LONG8
)
7526 sizeofval
= sizeof(uint64_t);
7528 else if (dirent
->tdir_type
== TIFF_SLONG8
)
7530 /* Non conformant but used by some images as in */
7531 /* https://github.com/OSGeo/gdal/issues/2165 */
7532 sizeofval
= sizeof(int64_t);
7536 TIFFErrorExtR(tif
, module
,
7537 "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
7538 panVals
[strile
] = 0;
7541 sizeofvalint
= (int)(sizeofval
);
7543 if (tif
->tif_flags
& TIFF_BIGTIFF
)
7545 uint64_t offset
= dirent
->tdir_offset
.toff_long8
;
7547 TIFFSwabLong8(&offset
);
7548 nBaseOffset
= offset
;
7552 uint32_t offset
= dirent
->tdir_offset
.toff_long
;
7554 TIFFSwabLong(&offset
);
7555 nBaseOffset
= offset
;
7557 /* To avoid later unsigned integer overflows */
7558 if (nBaseOffset
> (uint64_t)INT64_MAX
)
7560 TIFFErrorExtR(tif
, module
, "Cannot read offset/size for strile %d",
7562 panVals
[strile
] = 0;
7565 nOffset
= nBaseOffset
+ sizeofval
* strile
;
7566 nOffsetStartPage
= (nOffset
/ IO_CACHE_PAGE_SIZE
) * IO_CACHE_PAGE_SIZE
;
7567 nOffsetEndPage
= nOffsetStartPage
+ IO_CACHE_PAGE_SIZE
;
7569 if (nOffset
+ sizeofval
> nOffsetEndPage
)
7570 nOffsetEndPage
+= IO_CACHE_PAGE_SIZE
;
7571 #undef IO_CACHE_PAGE_SIZE
7573 nLastStripOffset
= nBaseOffset
+ arraySize
* sizeofval
;
7574 if (nLastStripOffset
< nOffsetEndPage
)
7575 nOffsetEndPage
= nLastStripOffset
;
7576 if (nOffsetStartPage
>= nOffsetEndPage
)
7578 TIFFErrorExtR(tif
, module
, "Cannot read offset/size for strile %d",
7580 panVals
[strile
] = 0;
7583 if (!SeekOK(tif
, nOffsetStartPage
))
7585 panVals
[strile
] = 0;
7589 nToRead
= (tmsize_t
)(nOffsetEndPage
- nOffsetStartPage
);
7590 nRead
= TIFFReadFile(tif
, buffer
, nToRead
);
7591 if (nRead
< nToRead
)
7593 TIFFErrorExtR(tif
, module
,
7594 "Cannot read offset/size for strile around ~%d", strile
);
7597 iStartBefore
= -(int)((nOffset
- nOffsetStartPage
) / sizeofval
);
7598 if (strile
+ iStartBefore
< 0)
7599 iStartBefore
= -strile
;
7600 for (i
= iStartBefore
;
7601 (uint32_t)(strile
+ i
) < arraySize
&&
7602 _TIFFUnsanitizedAddUInt64AndInt(nOffset
, (i
+ 1) * sizeofvalint
) <=
7606 if (dirent
->tdir_type
== TIFF_SHORT
)
7610 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
7613 TIFFSwabShort(&val
);
7614 panVals
[strile
+ i
] = val
;
7616 else if (dirent
->tdir_type
== TIFF_LONG
)
7620 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
7624 panVals
[strile
+ i
] = val
;
7626 else if (dirent
->tdir_type
== TIFF_LONG8
)
7630 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
7633 TIFFSwabLong8(&val
);
7634 panVals
[strile
+ i
] = val
;
7636 else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
7638 /* Non conformant data type */
7641 buffer
+ (nOffset
- nOffsetStartPage
) + i
* sizeofvalint
,
7644 TIFFSwabLong8((uint64_t *)&val
);
7645 panVals
[strile
+ i
] = (uint64_t)val
;
7651 static int _TIFFFetchStrileValue(TIFF
*tif
, uint32_t strile
,
7652 TIFFDirEntry
*dirent
, uint64_t **parray
)
7654 static const char module
[] = "_TIFFFetchStrileValue";
7655 TIFFDirectory
*td
= &tif
->tif_dir
;
7656 if (strile
>= dirent
->tdir_count
)
7660 if (strile
>= td
->td_stripoffsetbyteallocsize
)
7662 uint32_t nStripArrayAllocBefore
= td
->td_stripoffsetbyteallocsize
;
7663 uint32_t nStripArrayAllocNew
;
7664 uint64_t nArraySize64
;
7666 uint64_t *offsetArray
;
7667 uint64_t *bytecountArray
;
7669 if (strile
> 1000000)
7671 uint64_t filesize
= TIFFGetFileSize(tif
);
7672 /* Avoid excessive memory allocation attempt */
7673 /* For such a big blockid we need at least a TIFF_LONG per strile */
7674 /* for the offset array. */
7675 if (strile
> filesize
/ sizeof(uint32_t))
7677 TIFFErrorExtR(tif
, module
, "File too short");
7682 if (td
->td_stripoffsetbyteallocsize
== 0 &&
7683 td
->td_nstrips
< 1024 * 1024)
7685 nStripArrayAllocNew
= td
->td_nstrips
;
7689 #define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
7690 #define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
7691 nStripArrayAllocNew
= TIFF_MAX(strile
+ 1, 1024U * 512U);
7692 if (nStripArrayAllocNew
< 0xFFFFFFFFU
/ 2)
7693 nStripArrayAllocNew
*= 2;
7694 nStripArrayAllocNew
= TIFF_MIN(nStripArrayAllocNew
, td
->td_nstrips
);
7696 assert(strile
< nStripArrayAllocNew
);
7697 nArraySize64
= (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew
;
7698 nArraySize
= (size_t)(nArraySize64
);
7699 #if SIZEOF_SIZE_T == 4
7700 if (nArraySize
!= nArraySize64
)
7702 TIFFErrorExtR(tif
, module
,
7703 "Cannot allocate strip offset and bytecount arrays");
7707 offsetArray
= (uint64_t *)(_TIFFreallocExt(tif
, td
->td_stripoffset_p
,
7709 bytecountArray
= (uint64_t *)(_TIFFreallocExt(
7710 tif
, td
->td_stripbytecount_p
, nArraySize
));
7712 td
->td_stripoffset_p
= offsetArray
;
7714 td
->td_stripbytecount_p
= bytecountArray
;
7715 if (offsetArray
&& bytecountArray
)
7717 td
->td_stripoffsetbyteallocsize
= nStripArrayAllocNew
;
7718 /* Initialize new entries to ~0 / -1 */
7719 /* coverity[overrun-buffer-arg] */
7720 memset(td
->td_stripoffset_p
+ nStripArrayAllocBefore
, 0xFF,
7721 (td
->td_stripoffsetbyteallocsize
- nStripArrayAllocBefore
) *
7723 /* coverity[overrun-buffer-arg] */
7724 memset(td
->td_stripbytecount_p
+ nStripArrayAllocBefore
, 0xFF,
7725 (td
->td_stripoffsetbyteallocsize
- nStripArrayAllocBefore
) *
7730 TIFFErrorExtR(tif
, module
,
7731 "Cannot allocate strip offset and bytecount arrays");
7732 _TIFFfreeExt(tif
, td
->td_stripoffset_p
);
7733 td
->td_stripoffset_p
= NULL
;
7734 _TIFFfreeExt(tif
, td
->td_stripbytecount_p
);
7735 td
->td_stripbytecount_p
= NULL
;
7736 td
->td_stripoffsetbyteallocsize
= 0;
7739 if (*parray
== NULL
|| strile
>= td
->td_stripoffsetbyteallocsize
)
7742 if (~((*parray
)[strile
]) == 0)
7744 if (!_TIFFPartialReadStripArray(tif
, dirent
, strile
, *parray
))
7746 (*parray
)[strile
] = 0;
7754 static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF
*tif
, uint32_t strile
,
7755 TIFFDirEntry
*dirent
,
7759 TIFFDirectory
*td
= &tif
->tif_dir
;
7762 if ((tif
->tif_flags
& TIFF_DEFERSTRILELOAD
) &&
7763 !(tif
->tif_flags
& TIFF_CHOPPEDUPARRAYS
))
7765 if (!(tif
->tif_flags
& TIFF_LAZYSTRILELOAD
) ||
7766 /* If the values may fit in the toff_long/toff_long8 member */
7767 /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
7768 dirent
->tdir_count
<= 4)
7770 if (!_TIFFFillStriles(tif
))
7774 /* Do not return, as we want this function to always */
7775 /* return the same value if called several times with */
7776 /* the same arguments */
7781 if (!_TIFFFetchStrileValue(tif
, strile
, dirent
, parray
))
7789 if (*parray
== NULL
|| strile
>= td
->td_nstrips
)
7795 return (*parray
)[strile
];
7798 /* Return the value of the TileOffsets/StripOffsets array for the specified
7800 uint64_t TIFFGetStrileOffset(TIFF
*tif
, uint32_t strile
)
7802 return TIFFGetStrileOffsetWithErr(tif
, strile
, NULL
);
7805 /* Return the value of the TileOffsets/StripOffsets array for the specified
7807 uint64_t TIFFGetStrileOffsetWithErr(TIFF
*tif
, uint32_t strile
, int *pbErr
)
7809 TIFFDirectory
*td
= &tif
->tif_dir
;
7810 return _TIFFGetStrileOffsetOrByteCountValue(tif
, strile
,
7811 &(td
->td_stripoffset_entry
),
7812 &(td
->td_stripoffset_p
), pbErr
);
7815 /* Return the value of the TileByteCounts/StripByteCounts array for the
7816 * specified tile/strile */
7817 uint64_t TIFFGetStrileByteCount(TIFF
*tif
, uint32_t strile
)
7819 return TIFFGetStrileByteCountWithErr(tif
, strile
, NULL
);
7822 /* Return the value of the TileByteCounts/StripByteCounts array for the
7823 * specified tile/strile */
7824 uint64_t TIFFGetStrileByteCountWithErr(TIFF
*tif
, uint32_t strile
, int *pbErr
)
7826 TIFFDirectory
*td
= &tif
->tif_dir
;
7827 return _TIFFGetStrileOffsetOrByteCountValue(
7828 tif
, strile
, &(td
->td_stripbytecount_entry
), &(td
->td_stripbytecount_p
),
7832 int _TIFFFillStriles(TIFF
*tif
) { return _TIFFFillStrilesInternal(tif
, 1); }
7834 static int _TIFFFillStrilesInternal(TIFF
*tif
, int loadStripByteCount
)
7836 register TIFFDirectory
*td
= &tif
->tif_dir
;
7837 int return_value
= 1;
7839 /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
7840 if (!(tif
->tif_flags
& TIFF_DEFERSTRILELOAD
) ||
7841 (tif
->tif_flags
& TIFF_CHOPPEDUPARRAYS
) != 0)
7844 if (tif
->tif_flags
& TIFF_LAZYSTRILELOAD
)
7846 /* In case of lazy loading, reload completely the arrays */
7847 _TIFFfreeExt(tif
, td
->td_stripoffset_p
);
7848 _TIFFfreeExt(tif
, td
->td_stripbytecount_p
);
7849 td
->td_stripoffset_p
= NULL
;
7850 td
->td_stripbytecount_p
= NULL
;
7851 td
->td_stripoffsetbyteallocsize
= 0;
7852 tif
->tif_flags
&= ~TIFF_LAZYSTRILELOAD
;
7855 /* If stripoffset array is already loaded, exit with success */
7856 if (td
->td_stripoffset_p
!= NULL
)
7859 /* If tdir_count was canceled, then we already got there, but in error */
7860 if (td
->td_stripoffset_entry
.tdir_count
== 0)
7863 if (!TIFFFetchStripThing(tif
, &(td
->td_stripoffset_entry
), td
->td_nstrips
,
7864 &td
->td_stripoffset_p
))
7869 if (loadStripByteCount
&&
7870 !TIFFFetchStripThing(tif
, &(td
->td_stripbytecount_entry
),
7871 td
->td_nstrips
, &td
->td_stripbytecount_p
))
7876 _TIFFmemset(&(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
7877 _TIFFmemset(&(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
7879 #ifdef STRIPBYTECOUNTSORTED_UNUSED
7880 if (tif
->tif_dir
.td_nstrips
> 1 && return_value
== 1)
7884 tif
->tif_dir
.td_stripbytecountsorted
= 1;
7885 for (strip
= 1; strip
< tif
->tif_dir
.td_nstrips
; strip
++)
7887 if (tif
->tif_dir
.td_stripoffset_p
[strip
- 1] >
7888 tif
->tif_dir
.td_stripoffset_p
[strip
])
7890 tif
->tif_dir
.td_stripbytecountsorted
= 0;
7897 return return_value
;