3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 * Directory Read Support Routines.
32 /* Suggested pending improvements:
33 * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
34 * eliminating current use of the IGNORE value, and therefore eliminating
35 * current irrational behaviour on tags with tag id code 0
36 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
37 * the pointer to the appropriate TIFFField structure early on in
38 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
43 #define IGNORE 0 /* tag placeholder used below */
44 #define FAILED_FII ((uint32) -1)
47 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
48 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
50 extern void TIFFCvtIEEEFloatToNative(TIFF
*, uint32
, float*);
51 extern void TIFFCvtIEEEDoubleToNative(TIFF
*, uint32
, double*);
54 enum TIFFReadDirEntryErr
{
55 TIFFReadDirEntryErrOk
= 0,
56 TIFFReadDirEntryErrCount
= 1,
57 TIFFReadDirEntryErrType
= 2,
58 TIFFReadDirEntryErrIo
= 3,
59 TIFFReadDirEntryErrRange
= 4,
60 TIFFReadDirEntryErrPsdif
= 5,
61 TIFFReadDirEntryErrSizesan
= 6,
62 TIFFReadDirEntryErrAlloc
= 7,
65 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8
* value
);
66 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
* value
);
67 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
* value
);
68 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
* value
);
69 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
);
70 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
71 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
* value
);
73 static enum TIFFReadDirEntryErr
TIFFReadDirEntryArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
* count
, uint32 desttypesize
, void** value
);
74 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8
** value
);
75 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySbyteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int8
** value
);
76 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
** value
);
77 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySshortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int16
** value
);
78 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
** value
);
79 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int32
** value
);
80 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
** value
);
81 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, int64
** value
);
82 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloatArray(TIFF
* tif
, TIFFDirEntry
* direntry
, float** value
);
83 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDoubleArray(TIFF
* tif
, TIFFDirEntry
* direntry
, double** value
);
84 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
** value
);
86 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
* value
);
88 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
91 static void TIFFReadDirEntryCheckedByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8
* value
);
92 static void TIFFReadDirEntryCheckedSbyte(TIFF
* tif
, TIFFDirEntry
* direntry
, int8
* value
);
93 static void TIFFReadDirEntryCheckedShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
* value
);
94 static void TIFFReadDirEntryCheckedSshort(TIFF
* tif
, TIFFDirEntry
* direntry
, int16
* value
);
95 static void TIFFReadDirEntryCheckedLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
* value
);
96 static void TIFFReadDirEntryCheckedSlong(TIFF
* tif
, TIFFDirEntry
* direntry
, int32
* value
);
97 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
* value
);
98 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSlong8(TIFF
* tif
, TIFFDirEntry
* direntry
, int64
* value
);
99 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedRational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
100 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSrational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
101 static void TIFFReadDirEntryCheckedFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
);
102 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
);
104 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSbyte(int8 value
);
105 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteShort(uint16 value
);
106 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSshort(int16 value
);
107 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong(uint32 value
);
108 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong(int32 value
);
109 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong8(uint64 value
);
110 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong8(int64 value
);
112 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteByte(uint8 value
);
113 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteShort(uint16 value
);
114 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSshort(int16 value
);
115 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong(uint32 value
);
116 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong(int32 value
);
117 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value
);
118 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value
);
120 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSbyte(int8 value
);
121 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSshort(int16 value
);
122 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong(uint32 value
);
123 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong(int32 value
);
124 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong8(uint64 value
);
125 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong8(int64 value
);
127 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortShort(uint16 value
);
128 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong(uint32 value
);
129 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong(int32 value
);
130 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong8(uint64 value
);
131 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong8(int64 value
);
133 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSbyte(int8 value
);
134 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSshort(int16 value
);
135 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSlong(int32 value
);
136 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongLong8(uint64 value
);
137 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSlong8(int64 value
);
139 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlongLong(uint32 value
);
140 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlongLong8(uint64 value
);
141 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlongSlong8(int64 value
);
143 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value
);
144 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Sshort(int16 value
);
145 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Slong(int32 value
);
146 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLong8Slong8(int64 value
);
148 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value
);
150 static enum TIFFReadDirEntryErr
TIFFReadDirEntryData(TIFF
* tif
, uint64 offset
, tmsize_t size
, void* dest
);
151 static void TIFFReadDirEntryOutputErr(TIFF
* tif
, enum TIFFReadDirEntryErr err
, const char* module, const char* tagname
, int recover
);
153 static void TIFFReadDirectoryCheckOrder(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
);
154 static TIFFDirEntry
* TIFFReadDirectoryFindEntry(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
, uint16 tagid
);
155 static void TIFFReadDirectoryFindFieldInfo(TIFF
* tif
, uint16 tagid
, uint32
* fii
);
157 static int EstimateStripByteCounts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
);
158 static void MissingRequired(TIFF
*, const char*);
159 static int TIFFCheckDirOffset(TIFF
* tif
, uint64 diroff
);
160 static int CheckDirCount(TIFF
*, TIFFDirEntry
*, uint32
);
161 static uint16
TIFFFetchDirectory(TIFF
* tif
, uint64 diroff
, TIFFDirEntry
** pdir
, uint64
* nextdiroff
);
162 static int TIFFFetchNormalTag(TIFF
*, TIFFDirEntry
*, int recover
);
163 static int TIFFFetchStripThing(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 nstrips
, uint64
** lpp
);
164 static int TIFFFetchSubjectDistance(TIFF
*, TIFFDirEntry
*);
165 static void ChopUpSingleUncompressedStrip(TIFF
*);
166 static uint64
TIFFReadUInt64(const uint8
*value
);
168 typedef union _UInt64Aligned_t
178 Unaligned safe copy of a uint64 value from an octet array.
180 static uint64
TIFFReadUInt64(const uint8
*value
)
182 UInt64Aligned_t result
;
184 result
.c
[0]=value
[0];
185 result
.c
[1]=value
[1];
186 result
.c
[2]=value
[2];
187 result
.c
[3]=value
[3];
188 result
.c
[4]=value
[4];
189 result
.c
[5]=value
[5];
190 result
.c
[6]=value
[6];
191 result
.c
[7]=value
[7];
196 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8
* value
)
198 enum TIFFReadDirEntryErr err
;
199 if (direntry
->tdir_count
!=1)
200 return(TIFFReadDirEntryErrCount
);
201 switch (direntry
->tdir_type
)
204 TIFFReadDirEntryCheckedByte(tif
,direntry
,value
);
205 return(TIFFReadDirEntryErrOk
);
209 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
210 err
=TIFFReadDirEntryCheckRangeByteSbyte(m
);
211 if (err
!=TIFFReadDirEntryErrOk
)
214 return(TIFFReadDirEntryErrOk
);
219 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
220 err
=TIFFReadDirEntryCheckRangeByteShort(m
);
221 if (err
!=TIFFReadDirEntryErrOk
)
224 return(TIFFReadDirEntryErrOk
);
229 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
230 err
=TIFFReadDirEntryCheckRangeByteSshort(m
);
231 if (err
!=TIFFReadDirEntryErrOk
)
234 return(TIFFReadDirEntryErrOk
);
239 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
240 err
=TIFFReadDirEntryCheckRangeByteLong(m
);
241 if (err
!=TIFFReadDirEntryErrOk
)
244 return(TIFFReadDirEntryErrOk
);
249 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
250 err
=TIFFReadDirEntryCheckRangeByteSlong(m
);
251 if (err
!=TIFFReadDirEntryErrOk
)
254 return(TIFFReadDirEntryErrOk
);
259 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
260 if (err
!=TIFFReadDirEntryErrOk
)
262 err
=TIFFReadDirEntryCheckRangeByteLong8(m
);
263 if (err
!=TIFFReadDirEntryErrOk
)
266 return(TIFFReadDirEntryErrOk
);
271 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
272 if (err
!=TIFFReadDirEntryErrOk
)
274 err
=TIFFReadDirEntryCheckRangeByteSlong8(m
);
275 if (err
!=TIFFReadDirEntryErrOk
)
278 return(TIFFReadDirEntryErrOk
);
281 return(TIFFReadDirEntryErrType
);
285 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
* value
)
287 enum TIFFReadDirEntryErr err
;
288 if (direntry
->tdir_count
!=1)
289 return(TIFFReadDirEntryErrCount
);
290 switch (direntry
->tdir_type
)
295 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
297 return(TIFFReadDirEntryErrOk
);
302 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
303 err
=TIFFReadDirEntryCheckRangeShortSbyte(m
);
304 if (err
!=TIFFReadDirEntryErrOk
)
307 return(TIFFReadDirEntryErrOk
);
310 TIFFReadDirEntryCheckedShort(tif
,direntry
,value
);
311 return(TIFFReadDirEntryErrOk
);
315 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
316 err
=TIFFReadDirEntryCheckRangeShortSshort(m
);
317 if (err
!=TIFFReadDirEntryErrOk
)
320 return(TIFFReadDirEntryErrOk
);
325 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
326 err
=TIFFReadDirEntryCheckRangeShortLong(m
);
327 if (err
!=TIFFReadDirEntryErrOk
)
330 return(TIFFReadDirEntryErrOk
);
335 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
336 err
=TIFFReadDirEntryCheckRangeShortSlong(m
);
337 if (err
!=TIFFReadDirEntryErrOk
)
340 return(TIFFReadDirEntryErrOk
);
345 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
346 if (err
!=TIFFReadDirEntryErrOk
)
348 err
=TIFFReadDirEntryCheckRangeShortLong8(m
);
349 if (err
!=TIFFReadDirEntryErrOk
)
352 return(TIFFReadDirEntryErrOk
);
357 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
358 if (err
!=TIFFReadDirEntryErrOk
)
360 err
=TIFFReadDirEntryCheckRangeShortSlong8(m
);
361 if (err
!=TIFFReadDirEntryErrOk
)
364 return(TIFFReadDirEntryErrOk
);
367 return(TIFFReadDirEntryErrType
);
371 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
* value
)
373 enum TIFFReadDirEntryErr err
;
374 if (direntry
->tdir_count
!=1)
375 return(TIFFReadDirEntryErrCount
);
376 switch (direntry
->tdir_type
)
381 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
383 return(TIFFReadDirEntryErrOk
);
388 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
389 err
=TIFFReadDirEntryCheckRangeLongSbyte(m
);
390 if (err
!=TIFFReadDirEntryErrOk
)
393 return(TIFFReadDirEntryErrOk
);
398 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
400 return(TIFFReadDirEntryErrOk
);
405 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
406 err
=TIFFReadDirEntryCheckRangeLongSshort(m
);
407 if (err
!=TIFFReadDirEntryErrOk
)
410 return(TIFFReadDirEntryErrOk
);
413 TIFFReadDirEntryCheckedLong(tif
,direntry
,value
);
414 return(TIFFReadDirEntryErrOk
);
418 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
419 err
=TIFFReadDirEntryCheckRangeLongSlong(m
);
420 if (err
!=TIFFReadDirEntryErrOk
)
423 return(TIFFReadDirEntryErrOk
);
428 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
429 if (err
!=TIFFReadDirEntryErrOk
)
431 err
=TIFFReadDirEntryCheckRangeLongLong8(m
);
432 if (err
!=TIFFReadDirEntryErrOk
)
435 return(TIFFReadDirEntryErrOk
);
440 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
441 if (err
!=TIFFReadDirEntryErrOk
)
443 err
=TIFFReadDirEntryCheckRangeLongSlong8(m
);
444 if (err
!=TIFFReadDirEntryErrOk
)
447 return(TIFFReadDirEntryErrOk
);
450 return(TIFFReadDirEntryErrType
);
454 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
* value
)
456 enum TIFFReadDirEntryErr err
;
457 if (direntry
->tdir_count
!=1)
458 return(TIFFReadDirEntryErrCount
);
459 switch (direntry
->tdir_type
)
464 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
466 return(TIFFReadDirEntryErrOk
);
471 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
472 err
=TIFFReadDirEntryCheckRangeLong8Sbyte(m
);
473 if (err
!=TIFFReadDirEntryErrOk
)
476 return(TIFFReadDirEntryErrOk
);
481 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
483 return(TIFFReadDirEntryErrOk
);
488 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
489 err
=TIFFReadDirEntryCheckRangeLong8Sshort(m
);
490 if (err
!=TIFFReadDirEntryErrOk
)
493 return(TIFFReadDirEntryErrOk
);
498 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
500 return(TIFFReadDirEntryErrOk
);
505 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
506 err
=TIFFReadDirEntryCheckRangeLong8Slong(m
);
507 if (err
!=TIFFReadDirEntryErrOk
)
510 return(TIFFReadDirEntryErrOk
);
513 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,value
);
518 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
519 if (err
!=TIFFReadDirEntryErrOk
)
521 err
=TIFFReadDirEntryCheckRangeLong8Slong8(m
);
522 if (err
!=TIFFReadDirEntryErrOk
)
525 return(TIFFReadDirEntryErrOk
);
528 return(TIFFReadDirEntryErrType
);
532 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
)
534 enum TIFFReadDirEntryErr err
;
535 if (direntry
->tdir_count
!=1)
536 return(TIFFReadDirEntryErrCount
);
537 switch (direntry
->tdir_type
)
542 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
544 return(TIFFReadDirEntryErrOk
);
549 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
551 return(TIFFReadDirEntryErrOk
);
556 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
558 return(TIFFReadDirEntryErrOk
);
563 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
565 return(TIFFReadDirEntryErrOk
);
570 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
572 return(TIFFReadDirEntryErrOk
);
577 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
579 return(TIFFReadDirEntryErrOk
);
584 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
585 if (err
!=TIFFReadDirEntryErrOk
)
587 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
589 * XXX: MSVC 6.0 does not support conversion
590 * of 64-bit integers into floating point
593 *value
= _TIFFUInt64ToFloat(m
);
597 return(TIFFReadDirEntryErrOk
);
602 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
603 if (err
!=TIFFReadDirEntryErrOk
)
606 return(TIFFReadDirEntryErrOk
);
611 err
=TIFFReadDirEntryCheckedRational(tif
,direntry
,&m
);
612 if (err
!=TIFFReadDirEntryErrOk
)
615 return(TIFFReadDirEntryErrOk
);
620 err
=TIFFReadDirEntryCheckedSrational(tif
,direntry
,&m
);
621 if (err
!=TIFFReadDirEntryErrOk
)
624 return(TIFFReadDirEntryErrOk
);
627 TIFFReadDirEntryCheckedFloat(tif
,direntry
,value
);
628 return(TIFFReadDirEntryErrOk
);
632 err
=TIFFReadDirEntryCheckedDouble(tif
,direntry
,&m
);
633 if (err
!=TIFFReadDirEntryErrOk
)
636 return(TIFFReadDirEntryErrOk
);
639 return(TIFFReadDirEntryErrType
);
643 static enum TIFFReadDirEntryErr
TIFFReadDirEntryDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
645 enum TIFFReadDirEntryErr err
;
646 if (direntry
->tdir_count
!=1)
647 return(TIFFReadDirEntryErrCount
);
648 switch (direntry
->tdir_type
)
653 TIFFReadDirEntryCheckedByte(tif
,direntry
,&m
);
655 return(TIFFReadDirEntryErrOk
);
660 TIFFReadDirEntryCheckedSbyte(tif
,direntry
,&m
);
662 return(TIFFReadDirEntryErrOk
);
667 TIFFReadDirEntryCheckedShort(tif
,direntry
,&m
);
669 return(TIFFReadDirEntryErrOk
);
674 TIFFReadDirEntryCheckedSshort(tif
,direntry
,&m
);
676 return(TIFFReadDirEntryErrOk
);
681 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
683 return(TIFFReadDirEntryErrOk
);
688 TIFFReadDirEntryCheckedSlong(tif
,direntry
,&m
);
690 return(TIFFReadDirEntryErrOk
);
695 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,&m
);
696 if (err
!=TIFFReadDirEntryErrOk
)
698 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
700 * XXX: MSVC 6.0 does not support conversion
701 * of 64-bit integers into floating point
704 *value
= _TIFFUInt64ToDouble(m
);
708 return(TIFFReadDirEntryErrOk
);
713 err
=TIFFReadDirEntryCheckedSlong8(tif
,direntry
,&m
);
714 if (err
!=TIFFReadDirEntryErrOk
)
717 return(TIFFReadDirEntryErrOk
);
720 err
=TIFFReadDirEntryCheckedRational(tif
,direntry
,value
);
723 err
=TIFFReadDirEntryCheckedSrational(tif
,direntry
,value
);
728 TIFFReadDirEntryCheckedFloat(tif
,direntry
,&m
);
730 return(TIFFReadDirEntryErrOk
);
733 err
=TIFFReadDirEntryCheckedDouble(tif
,direntry
,value
);
736 return(TIFFReadDirEntryErrType
);
740 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
* value
)
742 enum TIFFReadDirEntryErr err
;
743 if (direntry
->tdir_count
!=1)
744 return(TIFFReadDirEntryErrCount
);
745 switch (direntry
->tdir_type
)
751 TIFFReadDirEntryCheckedLong(tif
,direntry
,&m
);
753 return(TIFFReadDirEntryErrOk
);
757 err
=TIFFReadDirEntryCheckedLong8(tif
,direntry
,value
);
760 return(TIFFReadDirEntryErrType
);
764 static enum TIFFReadDirEntryErr
TIFFReadDirEntryArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
* count
, uint32 desttypesize
, void** value
)
769 typesize
=TIFFDataWidth(direntry
->tdir_type
);
770 if ((direntry
->tdir_count
==0)||(typesize
==0))
773 return(TIFFReadDirEntryErrOk
);
778 * As a sanity check, make sure we have no more than a 2GB tag array
779 * in either the current data type or the dest data type. This also
780 * avoids problems with overflow of tmsize_t on 32bit systems.
782 if ((uint64
)(2147483647/typesize
)<direntry
->tdir_count
)
783 return(TIFFReadDirEntryErrSizesan
);
784 if ((uint64
)(2147483647/desttypesize
)<direntry
->tdir_count
)
785 return(TIFFReadDirEntryErrSizesan
);
787 *count
=(uint32
)direntry
->tdir_count
;
788 datasize
=(*count
)*typesize
;
789 assert((tmsize_t
)datasize
>0);
790 data
=_TIFFCheckMalloc(tif
, *count
, typesize
, "ReadDirEntryArray");
792 return(TIFFReadDirEntryErrAlloc
);
793 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
796 _TIFFmemcpy(data
,&direntry
->tdir_offset
,datasize
);
799 enum TIFFReadDirEntryErr err
;
800 uint32 offset
= direntry
->tdir_offset
.toff_long
;
801 if (tif
->tif_flags
&TIFF_SWAB
)
802 TIFFSwabLong(&offset
);
803 err
=TIFFReadDirEntryData(tif
,(uint64
)offset
,(tmsize_t
)datasize
,data
);
804 if (err
!=TIFFReadDirEntryErrOk
)
814 _TIFFmemcpy(data
,&direntry
->tdir_offset
,datasize
);
817 enum TIFFReadDirEntryErr err
;
818 uint64 offset
= direntry
->tdir_offset
.toff_long8
;
819 if (tif
->tif_flags
&TIFF_SWAB
)
820 TIFFSwabLong8(&offset
);
821 err
=TIFFReadDirEntryData(tif
,offset
,(tmsize_t
)datasize
,data
);
822 if (err
!=TIFFReadDirEntryErrOk
)
830 return(TIFFReadDirEntryErrOk
);
833 static enum TIFFReadDirEntryErr
TIFFReadDirEntryByteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8
** value
)
835 enum TIFFReadDirEntryErr err
;
839 switch (direntry
->tdir_type
)
853 return(TIFFReadDirEntryErrType
);
855 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,1,&origdata
);
856 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
861 switch (direntry
->tdir_type
)
866 *value
=(uint8
*)origdata
;
867 return(TIFFReadDirEntryErrOk
);
873 for (n
=0; n
<count
; n
++)
875 err
=TIFFReadDirEntryCheckRangeByteSbyte(*m
);
876 if (err
!=TIFFReadDirEntryErrOk
)
883 *value
=(uint8
*)origdata
;
884 return(TIFFReadDirEntryErrOk
);
887 data
=(uint8
*)_TIFFmalloc(count
);
891 return(TIFFReadDirEntryErrAlloc
);
893 switch (direntry
->tdir_type
)
900 ma
=(uint16
*)origdata
;
902 for (n
=0; n
<count
; n
++)
904 if (tif
->tif_flags
&TIFF_SWAB
)
906 err
=TIFFReadDirEntryCheckRangeByteShort(*ma
);
907 if (err
!=TIFFReadDirEntryErrOk
)
909 *mb
++=(uint8
)(*ma
++);
920 for (n
=0; n
<count
; n
++)
922 if (tif
->tif_flags
&TIFF_SWAB
)
923 TIFFSwabShort((uint16
*)ma
);
924 err
=TIFFReadDirEntryCheckRangeByteSshort(*ma
);
925 if (err
!=TIFFReadDirEntryErrOk
)
927 *mb
++=(uint8
)(*ma
++);
936 ma
=(uint32
*)origdata
;
938 for (n
=0; n
<count
; n
++)
940 if (tif
->tif_flags
&TIFF_SWAB
)
942 err
=TIFFReadDirEntryCheckRangeByteLong(*ma
);
943 if (err
!=TIFFReadDirEntryErrOk
)
945 *mb
++=(uint8
)(*ma
++);
956 for (n
=0; n
<count
; n
++)
958 if (tif
->tif_flags
&TIFF_SWAB
)
959 TIFFSwabLong((uint32
*)ma
);
960 err
=TIFFReadDirEntryCheckRangeByteSlong(*ma
);
961 if (err
!=TIFFReadDirEntryErrOk
)
963 *mb
++=(uint8
)(*ma
++);
972 ma
=(uint64
*)origdata
;
974 for (n
=0; n
<count
; n
++)
976 if (tif
->tif_flags
&TIFF_SWAB
)
978 err
=TIFFReadDirEntryCheckRangeByteLong8(*ma
);
979 if (err
!=TIFFReadDirEntryErrOk
)
981 *mb
++=(uint8
)(*ma
++);
992 for (n
=0; n
<count
; n
++)
994 if (tif
->tif_flags
&TIFF_SWAB
)
995 TIFFSwabLong8((uint64
*)ma
);
996 err
=TIFFReadDirEntryCheckRangeByteSlong8(*ma
);
997 if (err
!=TIFFReadDirEntryErrOk
)
999 *mb
++=(uint8
)(*ma
++);
1004 _TIFFfree(origdata
);
1005 if (err
!=TIFFReadDirEntryErrOk
)
1011 return(TIFFReadDirEntryErrOk
);
1014 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySbyteArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int8
** value
)
1016 enum TIFFReadDirEntryErr err
;
1020 switch (direntry
->tdir_type
)
1022 case TIFF_UNDEFINED
:
1033 return(TIFFReadDirEntryErrType
);
1035 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,1,&origdata
);
1036 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1041 switch (direntry
->tdir_type
)
1043 case TIFF_UNDEFINED
:
1049 for (n
=0; n
<count
; n
++)
1051 err
=TIFFReadDirEntryCheckRangeSbyteByte(*m
);
1052 if (err
!=TIFFReadDirEntryErrOk
)
1054 _TIFFfree(origdata
);
1059 *value
=(int8
*)origdata
;
1060 return(TIFFReadDirEntryErrOk
);
1063 *value
=(int8
*)origdata
;
1064 return(TIFFReadDirEntryErrOk
);
1066 data
=(int8
*)_TIFFmalloc(count
);
1069 _TIFFfree(origdata
);
1070 return(TIFFReadDirEntryErrAlloc
);
1072 switch (direntry
->tdir_type
)
1079 ma
=(uint16
*)origdata
;
1081 for (n
=0; n
<count
; n
++)
1083 if (tif
->tif_flags
&TIFF_SWAB
)
1085 err
=TIFFReadDirEntryCheckRangeSbyteShort(*ma
);
1086 if (err
!=TIFFReadDirEntryErrOk
)
1088 *mb
++=(int8
)(*ma
++);
1097 ma
=(int16
*)origdata
;
1099 for (n
=0; n
<count
; n
++)
1101 if (tif
->tif_flags
&TIFF_SWAB
)
1102 TIFFSwabShort((uint16
*)ma
);
1103 err
=TIFFReadDirEntryCheckRangeSbyteSshort(*ma
);
1104 if (err
!=TIFFReadDirEntryErrOk
)
1106 *mb
++=(int8
)(*ma
++);
1115 ma
=(uint32
*)origdata
;
1117 for (n
=0; n
<count
; n
++)
1119 if (tif
->tif_flags
&TIFF_SWAB
)
1121 err
=TIFFReadDirEntryCheckRangeSbyteLong(*ma
);
1122 if (err
!=TIFFReadDirEntryErrOk
)
1124 *mb
++=(int8
)(*ma
++);
1133 ma
=(int32
*)origdata
;
1135 for (n
=0; n
<count
; n
++)
1137 if (tif
->tif_flags
&TIFF_SWAB
)
1138 TIFFSwabLong((uint32
*)ma
);
1139 err
=TIFFReadDirEntryCheckRangeSbyteSlong(*ma
);
1140 if (err
!=TIFFReadDirEntryErrOk
)
1142 *mb
++=(int8
)(*ma
++);
1151 ma
=(uint64
*)origdata
;
1153 for (n
=0; n
<count
; n
++)
1155 if (tif
->tif_flags
&TIFF_SWAB
)
1157 err
=TIFFReadDirEntryCheckRangeSbyteLong8(*ma
);
1158 if (err
!=TIFFReadDirEntryErrOk
)
1160 *mb
++=(int8
)(*ma
++);
1169 ma
=(int64
*)origdata
;
1171 for (n
=0; n
<count
; n
++)
1173 if (tif
->tif_flags
&TIFF_SWAB
)
1174 TIFFSwabLong8((uint64
*)ma
);
1175 err
=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma
);
1176 if (err
!=TIFFReadDirEntryErrOk
)
1178 *mb
++=(int8
)(*ma
++);
1183 _TIFFfree(origdata
);
1184 if (err
!=TIFFReadDirEntryErrOk
)
1190 return(TIFFReadDirEntryErrOk
);
1193 static enum TIFFReadDirEntryErr
TIFFReadDirEntryShortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
** value
)
1195 enum TIFFReadDirEntryErr err
;
1199 switch (direntry
->tdir_type
)
1211 return(TIFFReadDirEntryErrType
);
1213 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,2,&origdata
);
1214 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1219 switch (direntry
->tdir_type
)
1222 *value
=(uint16
*)origdata
;
1223 if (tif
->tif_flags
&TIFF_SWAB
)
1224 TIFFSwabArrayOfShort(*value
,count
);
1225 return(TIFFReadDirEntryErrOk
);
1231 for (n
=0; n
<count
; n
++)
1233 if (tif
->tif_flags
&TIFF_SWAB
)
1234 TIFFSwabShort((uint16
*)m
);
1235 err
=TIFFReadDirEntryCheckRangeShortSshort(*m
);
1236 if (err
!=TIFFReadDirEntryErrOk
)
1238 _TIFFfree(origdata
);
1243 *value
=(uint16
*)origdata
;
1244 return(TIFFReadDirEntryErrOk
);
1247 data
=(uint16
*)_TIFFmalloc(count
*2);
1250 _TIFFfree(origdata
);
1251 return(TIFFReadDirEntryErrAlloc
);
1253 switch (direntry
->tdir_type
)
1260 ma
=(uint8
*)origdata
;
1262 for (n
=0; n
<count
; n
++)
1263 *mb
++=(uint16
)(*ma
++);
1273 for (n
=0; n
<count
; n
++)
1275 err
=TIFFReadDirEntryCheckRangeShortSbyte(*ma
);
1276 if (err
!=TIFFReadDirEntryErrOk
)
1278 *mb
++=(uint16
)(*ma
++);
1287 ma
=(uint32
*)origdata
;
1289 for (n
=0; n
<count
; n
++)
1291 if (tif
->tif_flags
&TIFF_SWAB
)
1293 err
=TIFFReadDirEntryCheckRangeShortLong(*ma
);
1294 if (err
!=TIFFReadDirEntryErrOk
)
1296 *mb
++=(uint16
)(*ma
++);
1305 ma
=(int32
*)origdata
;
1307 for (n
=0; n
<count
; n
++)
1309 if (tif
->tif_flags
&TIFF_SWAB
)
1310 TIFFSwabLong((uint32
*)ma
);
1311 err
=TIFFReadDirEntryCheckRangeShortSlong(*ma
);
1312 if (err
!=TIFFReadDirEntryErrOk
)
1314 *mb
++=(uint16
)(*ma
++);
1323 ma
=(uint64
*)origdata
;
1325 for (n
=0; n
<count
; n
++)
1327 if (tif
->tif_flags
&TIFF_SWAB
)
1329 err
=TIFFReadDirEntryCheckRangeShortLong8(*ma
);
1330 if (err
!=TIFFReadDirEntryErrOk
)
1332 *mb
++=(uint16
)(*ma
++);
1341 ma
=(int64
*)origdata
;
1343 for (n
=0; n
<count
; n
++)
1345 if (tif
->tif_flags
&TIFF_SWAB
)
1346 TIFFSwabLong8((uint64
*)ma
);
1347 err
=TIFFReadDirEntryCheckRangeShortSlong8(*ma
);
1348 if (err
!=TIFFReadDirEntryErrOk
)
1350 *mb
++=(uint16
)(*ma
++);
1355 _TIFFfree(origdata
);
1356 if (err
!=TIFFReadDirEntryErrOk
)
1362 return(TIFFReadDirEntryErrOk
);
1365 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySshortArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int16
** value
)
1367 enum TIFFReadDirEntryErr err
;
1371 switch (direntry
->tdir_type
)
1383 return(TIFFReadDirEntryErrType
);
1385 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,2,&origdata
);
1386 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1391 switch (direntry
->tdir_type
)
1397 m
=(uint16
*)origdata
;
1398 for (n
=0; n
<count
; n
++)
1400 if (tif
->tif_flags
&TIFF_SWAB
)
1402 err
=TIFFReadDirEntryCheckRangeSshortShort(*m
);
1403 if (err
!=TIFFReadDirEntryErrOk
)
1405 _TIFFfree(origdata
);
1410 *value
=(int16
*)origdata
;
1411 return(TIFFReadDirEntryErrOk
);
1414 *value
=(int16
*)origdata
;
1415 if (tif
->tif_flags
&TIFF_SWAB
)
1416 TIFFSwabArrayOfShort((uint16
*)(*value
),count
);
1417 return(TIFFReadDirEntryErrOk
);
1419 data
=(int16
*)_TIFFmalloc(count
*2);
1422 _TIFFfree(origdata
);
1423 return(TIFFReadDirEntryErrAlloc
);
1425 switch (direntry
->tdir_type
)
1432 ma
=(uint8
*)origdata
;
1434 for (n
=0; n
<count
; n
++)
1435 *mb
++=(int16
)(*ma
++);
1445 for (n
=0; n
<count
; n
++)
1446 *mb
++=(int16
)(*ma
++);
1454 ma
=(uint32
*)origdata
;
1456 for (n
=0; n
<count
; n
++)
1458 if (tif
->tif_flags
&TIFF_SWAB
)
1460 err
=TIFFReadDirEntryCheckRangeSshortLong(*ma
);
1461 if (err
!=TIFFReadDirEntryErrOk
)
1463 *mb
++=(int16
)(*ma
++);
1472 ma
=(int32
*)origdata
;
1474 for (n
=0; n
<count
; n
++)
1476 if (tif
->tif_flags
&TIFF_SWAB
)
1477 TIFFSwabLong((uint32
*)ma
);
1478 err
=TIFFReadDirEntryCheckRangeSshortSlong(*ma
);
1479 if (err
!=TIFFReadDirEntryErrOk
)
1481 *mb
++=(int16
)(*ma
++);
1490 ma
=(uint64
*)origdata
;
1492 for (n
=0; n
<count
; n
++)
1494 if (tif
->tif_flags
&TIFF_SWAB
)
1496 err
=TIFFReadDirEntryCheckRangeSshortLong8(*ma
);
1497 if (err
!=TIFFReadDirEntryErrOk
)
1499 *mb
++=(int16
)(*ma
++);
1508 ma
=(int64
*)origdata
;
1510 for (n
=0; n
<count
; n
++)
1512 if (tif
->tif_flags
&TIFF_SWAB
)
1513 TIFFSwabLong8((uint64
*)ma
);
1514 err
=TIFFReadDirEntryCheckRangeSshortSlong8(*ma
);
1515 if (err
!=TIFFReadDirEntryErrOk
)
1517 *mb
++=(int16
)(*ma
++);
1522 _TIFFfree(origdata
);
1523 if (err
!=TIFFReadDirEntryErrOk
)
1529 return(TIFFReadDirEntryErrOk
);
1532 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
** value
)
1534 enum TIFFReadDirEntryErr err
;
1538 switch (direntry
->tdir_type
)
1550 return(TIFFReadDirEntryErrType
);
1552 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,4,&origdata
);
1553 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1558 switch (direntry
->tdir_type
)
1561 *value
=(uint32
*)origdata
;
1562 if (tif
->tif_flags
&TIFF_SWAB
)
1563 TIFFSwabArrayOfLong(*value
,count
);
1564 return(TIFFReadDirEntryErrOk
);
1570 for (n
=0; n
<count
; n
++)
1572 if (tif
->tif_flags
&TIFF_SWAB
)
1573 TIFFSwabLong((uint32
*)m
);
1574 err
=TIFFReadDirEntryCheckRangeLongSlong(*m
);
1575 if (err
!=TIFFReadDirEntryErrOk
)
1577 _TIFFfree(origdata
);
1582 *value
=(uint32
*)origdata
;
1583 return(TIFFReadDirEntryErrOk
);
1586 data
=(uint32
*)_TIFFmalloc(count
*4);
1589 _TIFFfree(origdata
);
1590 return(TIFFReadDirEntryErrAlloc
);
1592 switch (direntry
->tdir_type
)
1599 ma
=(uint8
*)origdata
;
1601 for (n
=0; n
<count
; n
++)
1602 *mb
++=(uint32
)(*ma
++);
1612 for (n
=0; n
<count
; n
++)
1614 err
=TIFFReadDirEntryCheckRangeLongSbyte(*ma
);
1615 if (err
!=TIFFReadDirEntryErrOk
)
1617 *mb
++=(uint32
)(*ma
++);
1626 ma
=(uint16
*)origdata
;
1628 for (n
=0; n
<count
; n
++)
1630 if (tif
->tif_flags
&TIFF_SWAB
)
1632 *mb
++=(uint32
)(*ma
++);
1641 ma
=(int16
*)origdata
;
1643 for (n
=0; n
<count
; n
++)
1645 if (tif
->tif_flags
&TIFF_SWAB
)
1646 TIFFSwabShort((uint16
*)ma
);
1647 err
=TIFFReadDirEntryCheckRangeLongSshort(*ma
);
1648 if (err
!=TIFFReadDirEntryErrOk
)
1650 *mb
++=(uint32
)(*ma
++);
1659 ma
=(uint64
*)origdata
;
1661 for (n
=0; n
<count
; n
++)
1663 if (tif
->tif_flags
&TIFF_SWAB
)
1665 err
=TIFFReadDirEntryCheckRangeLongLong8(*ma
);
1666 if (err
!=TIFFReadDirEntryErrOk
)
1668 *mb
++=(uint32
)(*ma
++);
1677 ma
=(int64
*)origdata
;
1679 for (n
=0; n
<count
; n
++)
1681 if (tif
->tif_flags
&TIFF_SWAB
)
1682 TIFFSwabLong8((uint64
*)ma
);
1683 err
=TIFFReadDirEntryCheckRangeLongSlong8(*ma
);
1684 if (err
!=TIFFReadDirEntryErrOk
)
1686 *mb
++=(uint32
)(*ma
++);
1691 _TIFFfree(origdata
);
1692 if (err
!=TIFFReadDirEntryErrOk
)
1698 return(TIFFReadDirEntryErrOk
);
1701 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlongArray(TIFF
* tif
, TIFFDirEntry
* direntry
, int32
** value
)
1703 enum TIFFReadDirEntryErr err
;
1707 switch (direntry
->tdir_type
)
1719 return(TIFFReadDirEntryErrType
);
1721 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,4,&origdata
);
1722 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1727 switch (direntry
->tdir_type
)
1733 m
=(uint32
*)origdata
;
1734 for (n
=0; n
<count
; n
++)
1736 if (tif
->tif_flags
&TIFF_SWAB
)
1737 TIFFSwabLong((uint32
*)m
);
1738 err
=TIFFReadDirEntryCheckRangeSlongLong(*m
);
1739 if (err
!=TIFFReadDirEntryErrOk
)
1741 _TIFFfree(origdata
);
1746 *value
=(int32
*)origdata
;
1747 return(TIFFReadDirEntryErrOk
);
1750 *value
=(int32
*)origdata
;
1751 if (tif
->tif_flags
&TIFF_SWAB
)
1752 TIFFSwabArrayOfLong((uint32
*)(*value
),count
);
1753 return(TIFFReadDirEntryErrOk
);
1755 data
=(int32
*)_TIFFmalloc(count
*4);
1758 _TIFFfree(origdata
);
1759 return(TIFFReadDirEntryErrAlloc
);
1761 switch (direntry
->tdir_type
)
1768 ma
=(uint8
*)origdata
;
1770 for (n
=0; n
<count
; n
++)
1771 *mb
++=(int32
)(*ma
++);
1781 for (n
=0; n
<count
; n
++)
1782 *mb
++=(int32
)(*ma
++);
1790 ma
=(uint16
*)origdata
;
1792 for (n
=0; n
<count
; n
++)
1794 if (tif
->tif_flags
&TIFF_SWAB
)
1796 *mb
++=(int32
)(*ma
++);
1805 ma
=(int16
*)origdata
;
1807 for (n
=0; n
<count
; n
++)
1809 if (tif
->tif_flags
&TIFF_SWAB
)
1810 TIFFSwabShort((uint16
*)ma
);
1811 *mb
++=(int32
)(*ma
++);
1820 ma
=(uint64
*)origdata
;
1822 for (n
=0; n
<count
; n
++)
1824 if (tif
->tif_flags
&TIFF_SWAB
)
1826 err
=TIFFReadDirEntryCheckRangeSlongLong8(*ma
);
1827 if (err
!=TIFFReadDirEntryErrOk
)
1829 *mb
++=(int32
)(*ma
++);
1838 ma
=(int64
*)origdata
;
1840 for (n
=0; n
<count
; n
++)
1842 if (tif
->tif_flags
&TIFF_SWAB
)
1843 TIFFSwabLong8((uint64
*)ma
);
1844 err
=TIFFReadDirEntryCheckRangeSlongSlong8(*ma
);
1845 if (err
!=TIFFReadDirEntryErrOk
)
1847 *mb
++=(int32
)(*ma
++);
1852 _TIFFfree(origdata
);
1853 if (err
!=TIFFReadDirEntryErrOk
)
1859 return(TIFFReadDirEntryErrOk
);
1862 static enum TIFFReadDirEntryErr
TIFFReadDirEntryLong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
** value
)
1864 enum TIFFReadDirEntryErr err
;
1868 switch (direntry
->tdir_type
)
1880 return(TIFFReadDirEntryErrType
);
1882 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
1883 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
1888 switch (direntry
->tdir_type
)
1891 *value
=(uint64
*)origdata
;
1892 if (tif
->tif_flags
&TIFF_SWAB
)
1893 TIFFSwabArrayOfLong8(*value
,count
);
1894 return(TIFFReadDirEntryErrOk
);
1900 for (n
=0; n
<count
; n
++)
1902 if (tif
->tif_flags
&TIFF_SWAB
)
1903 TIFFSwabLong8((uint64
*)m
);
1904 err
=TIFFReadDirEntryCheckRangeLong8Slong8(*m
);
1905 if (err
!=TIFFReadDirEntryErrOk
)
1907 _TIFFfree(origdata
);
1912 *value
=(uint64
*)origdata
;
1913 return(TIFFReadDirEntryErrOk
);
1916 data
=(uint64
*)_TIFFmalloc(count
*8);
1919 _TIFFfree(origdata
);
1920 return(TIFFReadDirEntryErrAlloc
);
1922 switch (direntry
->tdir_type
)
1929 ma
=(uint8
*)origdata
;
1931 for (n
=0; n
<count
; n
++)
1932 *mb
++=(uint64
)(*ma
++);
1942 for (n
=0; n
<count
; n
++)
1944 err
=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma
);
1945 if (err
!=TIFFReadDirEntryErrOk
)
1947 *mb
++=(uint64
)(*ma
++);
1956 ma
=(uint16
*)origdata
;
1958 for (n
=0; n
<count
; n
++)
1960 if (tif
->tif_flags
&TIFF_SWAB
)
1962 *mb
++=(uint64
)(*ma
++);
1971 ma
=(int16
*)origdata
;
1973 for (n
=0; n
<count
; n
++)
1975 if (tif
->tif_flags
&TIFF_SWAB
)
1976 TIFFSwabShort((uint16
*)ma
);
1977 err
=TIFFReadDirEntryCheckRangeLong8Sshort(*ma
);
1978 if (err
!=TIFFReadDirEntryErrOk
)
1980 *mb
++=(uint64
)(*ma
++);
1989 ma
=(uint32
*)origdata
;
1991 for (n
=0; n
<count
; n
++)
1993 if (tif
->tif_flags
&TIFF_SWAB
)
1995 *mb
++=(uint64
)(*ma
++);
2004 ma
=(int32
*)origdata
;
2006 for (n
=0; n
<count
; n
++)
2008 if (tif
->tif_flags
&TIFF_SWAB
)
2009 TIFFSwabLong((uint32
*)ma
);
2010 err
=TIFFReadDirEntryCheckRangeLong8Slong(*ma
);
2011 if (err
!=TIFFReadDirEntryErrOk
)
2013 *mb
++=(uint64
)(*ma
++);
2018 _TIFFfree(origdata
);
2019 if (err
!=TIFFReadDirEntryErrOk
)
2025 return(TIFFReadDirEntryErrOk
);
2028 static enum TIFFReadDirEntryErr
TIFFReadDirEntrySlong8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, int64
** value
)
2030 enum TIFFReadDirEntryErr err
;
2034 switch (direntry
->tdir_type
)
2046 return(TIFFReadDirEntryErrType
);
2048 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
2049 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2054 switch (direntry
->tdir_type
)
2060 m
=(uint64
*)origdata
;
2061 for (n
=0; n
<count
; n
++)
2063 if (tif
->tif_flags
&TIFF_SWAB
)
2065 err
=TIFFReadDirEntryCheckRangeSlong8Long8(*m
);
2066 if (err
!=TIFFReadDirEntryErrOk
)
2068 _TIFFfree(origdata
);
2073 *value
=(int64
*)origdata
;
2074 return(TIFFReadDirEntryErrOk
);
2077 *value
=(int64
*)origdata
;
2078 if (tif
->tif_flags
&TIFF_SWAB
)
2079 TIFFSwabArrayOfLong8((uint64
*)(*value
),count
);
2080 return(TIFFReadDirEntryErrOk
);
2082 data
=(int64
*)_TIFFmalloc(count
*8);
2085 _TIFFfree(origdata
);
2086 return(TIFFReadDirEntryErrAlloc
);
2088 switch (direntry
->tdir_type
)
2095 ma
=(uint8
*)origdata
;
2097 for (n
=0; n
<count
; n
++)
2098 *mb
++=(int64
)(*ma
++);
2108 for (n
=0; n
<count
; n
++)
2109 *mb
++=(int64
)(*ma
++);
2117 ma
=(uint16
*)origdata
;
2119 for (n
=0; n
<count
; n
++)
2121 if (tif
->tif_flags
&TIFF_SWAB
)
2123 *mb
++=(int64
)(*ma
++);
2132 ma
=(int16
*)origdata
;
2134 for (n
=0; n
<count
; n
++)
2136 if (tif
->tif_flags
&TIFF_SWAB
)
2137 TIFFSwabShort((uint16
*)ma
);
2138 *mb
++=(int64
)(*ma
++);
2147 ma
=(uint32
*)origdata
;
2149 for (n
=0; n
<count
; n
++)
2151 if (tif
->tif_flags
&TIFF_SWAB
)
2153 *mb
++=(int64
)(*ma
++);
2162 ma
=(int32
*)origdata
;
2164 for (n
=0; n
<count
; n
++)
2166 if (tif
->tif_flags
&TIFF_SWAB
)
2167 TIFFSwabLong((uint32
*)ma
);
2168 *mb
++=(int64
)(*ma
++);
2173 _TIFFfree(origdata
);
2174 if (err
!=TIFFReadDirEntryErrOk
)
2180 return(TIFFReadDirEntryErrOk
);
2183 static enum TIFFReadDirEntryErr
TIFFReadDirEntryFloatArray(TIFF
* tif
, TIFFDirEntry
* direntry
, float** value
)
2185 enum TIFFReadDirEntryErr err
;
2189 switch (direntry
->tdir_type
)
2200 case TIFF_SRATIONAL
:
2205 return(TIFFReadDirEntryErrType
);
2207 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,4,&origdata
);
2208 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2213 switch (direntry
->tdir_type
)
2216 if (tif
->tif_flags
&TIFF_SWAB
)
2217 TIFFSwabArrayOfLong((uint32
*)origdata
,count
);
2218 TIFFCvtIEEEDoubleToNative(tif
,count
,(float*)origdata
);
2219 *value
=(float*)origdata
;
2220 return(TIFFReadDirEntryErrOk
);
2222 data
=(float*)_TIFFmalloc(count
*sizeof(float));
2225 _TIFFfree(origdata
);
2226 return(TIFFReadDirEntryErrAlloc
);
2228 switch (direntry
->tdir_type
)
2235 ma
=(uint8
*)origdata
;
2237 for (n
=0; n
<count
; n
++)
2238 *mb
++=(float)(*ma
++);
2248 for (n
=0; n
<count
; n
++)
2249 *mb
++=(float)(*ma
++);
2257 ma
=(uint16
*)origdata
;
2259 for (n
=0; n
<count
; n
++)
2261 if (tif
->tif_flags
&TIFF_SWAB
)
2263 *mb
++=(float)(*ma
++);
2272 ma
=(int16
*)origdata
;
2274 for (n
=0; n
<count
; n
++)
2276 if (tif
->tif_flags
&TIFF_SWAB
)
2277 TIFFSwabShort((uint16
*)ma
);
2278 *mb
++=(float)(*ma
++);
2287 ma
=(uint32
*)origdata
;
2289 for (n
=0; n
<count
; n
++)
2291 if (tif
->tif_flags
&TIFF_SWAB
)
2293 *mb
++=(float)(*ma
++);
2302 ma
=(int32
*)origdata
;
2304 for (n
=0; n
<count
; n
++)
2306 if (tif
->tif_flags
&TIFF_SWAB
)
2307 TIFFSwabLong((uint32
*)ma
);
2308 *mb
++=(float)(*ma
++);
2317 ma
=(uint64
*)origdata
;
2319 for (n
=0; n
<count
; n
++)
2321 if (tif
->tif_flags
&TIFF_SWAB
)
2323 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
2325 * XXX: MSVC 6.0 does not support
2326 * conversion of 64-bit integers into
2327 * floating point values.
2329 *mb
++ = _TIFFUInt64ToFloat(*ma
++);
2331 *mb
++ = (float)(*ma
++);
2341 ma
=(int64
*)origdata
;
2343 for (n
=0; n
<count
; n
++)
2345 if (tif
->tif_flags
&TIFF_SWAB
)
2346 TIFFSwabLong8((uint64
*)ma
);
2347 *mb
++=(float)(*ma
++);
2358 ma
=(uint32
*)origdata
;
2360 for (n
=0; n
<count
; n
++)
2362 if (tif
->tif_flags
&TIFF_SWAB
)
2365 if (tif
->tif_flags
&TIFF_SWAB
)
2371 *mb
++=(float)maa
/(float)mab
;
2375 case TIFF_SRATIONAL
:
2382 ma
=(uint32
*)origdata
;
2384 for (n
=0; n
<count
; n
++)
2386 if (tif
->tif_flags
&TIFF_SWAB
)
2390 if (tif
->tif_flags
&TIFF_SWAB
)
2396 *mb
++=(float)maa
/(float)mab
;
2405 if (tif
->tif_flags
&TIFF_SWAB
)
2406 TIFFSwabArrayOfLong8((uint64
*)origdata
,count
);
2407 TIFFCvtIEEEDoubleToNative(tif
,count
,(double*)origdata
);
2408 ma
=(double*)origdata
;
2410 for (n
=0; n
<count
; n
++)
2411 *mb
++=(float)(*ma
++);
2415 _TIFFfree(origdata
);
2416 if (err
!=TIFFReadDirEntryErrOk
)
2422 return(TIFFReadDirEntryErrOk
);
2425 static enum TIFFReadDirEntryErr
2426 TIFFReadDirEntryDoubleArray(TIFF
* tif
, TIFFDirEntry
* direntry
, double** value
)
2428 enum TIFFReadDirEntryErr err
;
2432 switch (direntry
->tdir_type
)
2443 case TIFF_SRATIONAL
:
2448 return(TIFFReadDirEntryErrType
);
2450 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
2451 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2456 switch (direntry
->tdir_type
)
2459 if (tif
->tif_flags
&TIFF_SWAB
)
2460 TIFFSwabArrayOfLong8((uint64
*)origdata
,count
);
2461 TIFFCvtIEEEDoubleToNative(tif
,count
,(double*)origdata
);
2462 *value
=(double*)origdata
;
2463 return(TIFFReadDirEntryErrOk
);
2465 data
=(double*)_TIFFmalloc(count
*sizeof(double));
2468 _TIFFfree(origdata
);
2469 return(TIFFReadDirEntryErrAlloc
);
2471 switch (direntry
->tdir_type
)
2478 ma
=(uint8
*)origdata
;
2480 for (n
=0; n
<count
; n
++)
2481 *mb
++=(double)(*ma
++);
2491 for (n
=0; n
<count
; n
++)
2492 *mb
++=(double)(*ma
++);
2500 ma
=(uint16
*)origdata
;
2502 for (n
=0; n
<count
; n
++)
2504 if (tif
->tif_flags
&TIFF_SWAB
)
2506 *mb
++=(double)(*ma
++);
2515 ma
=(int16
*)origdata
;
2517 for (n
=0; n
<count
; n
++)
2519 if (tif
->tif_flags
&TIFF_SWAB
)
2520 TIFFSwabShort((uint16
*)ma
);
2521 *mb
++=(double)(*ma
++);
2530 ma
=(uint32
*)origdata
;
2532 for (n
=0; n
<count
; n
++)
2534 if (tif
->tif_flags
&TIFF_SWAB
)
2536 *mb
++=(double)(*ma
++);
2545 ma
=(int32
*)origdata
;
2547 for (n
=0; n
<count
; n
++)
2549 if (tif
->tif_flags
&TIFF_SWAB
)
2550 TIFFSwabLong((uint32
*)ma
);
2551 *mb
++=(double)(*ma
++);
2560 ma
=(uint64
*)origdata
;
2562 for (n
=0; n
<count
; n
++)
2564 if (tif
->tif_flags
&TIFF_SWAB
)
2566 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
2568 * XXX: MSVC 6.0 does not support
2569 * conversion of 64-bit integers into
2570 * floating point values.
2572 *mb
++ = _TIFFUInt64ToDouble(*ma
++);
2574 *mb
++ = (double)(*ma
++);
2584 ma
=(int64
*)origdata
;
2586 for (n
=0; n
<count
; n
++)
2588 if (tif
->tif_flags
&TIFF_SWAB
)
2589 TIFFSwabLong8((uint64
*)ma
);
2590 *mb
++=(double)(*ma
++);
2601 ma
=(uint32
*)origdata
;
2603 for (n
=0; n
<count
; n
++)
2605 if (tif
->tif_flags
&TIFF_SWAB
)
2608 if (tif
->tif_flags
&TIFF_SWAB
)
2614 *mb
++=(double)maa
/(double)mab
;
2618 case TIFF_SRATIONAL
:
2625 ma
=(uint32
*)origdata
;
2627 for (n
=0; n
<count
; n
++)
2629 if (tif
->tif_flags
&TIFF_SWAB
)
2633 if (tif
->tif_flags
&TIFF_SWAB
)
2639 *mb
++=(double)maa
/(double)mab
;
2648 if (tif
->tif_flags
&TIFF_SWAB
)
2649 TIFFSwabArrayOfLong((uint32
*)origdata
,count
);
2650 TIFFCvtIEEEFloatToNative(tif
,count
,(float*)origdata
);
2651 ma
=(float*)origdata
;
2653 for (n
=0; n
<count
; n
++)
2654 *mb
++=(double)(*ma
++);
2658 _TIFFfree(origdata
);
2659 if (err
!=TIFFReadDirEntryErrOk
)
2665 return(TIFFReadDirEntryErrOk
);
2668 static enum TIFFReadDirEntryErr
TIFFReadDirEntryIfd8Array(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
** value
)
2670 enum TIFFReadDirEntryErr err
;
2674 switch (direntry
->tdir_type
)
2682 return(TIFFReadDirEntryErrType
);
2684 err
=TIFFReadDirEntryArray(tif
,direntry
,&count
,8,&origdata
);
2685 if ((err
!=TIFFReadDirEntryErrOk
)||(origdata
==0))
2690 switch (direntry
->tdir_type
)
2694 *value
=(uint64
*)origdata
;
2695 if (tif
->tif_flags
&TIFF_SWAB
)
2696 TIFFSwabArrayOfLong8(*value
,count
);
2697 return(TIFFReadDirEntryErrOk
);
2699 data
=(uint64
*)_TIFFmalloc(count
*8);
2702 _TIFFfree(origdata
);
2703 return(TIFFReadDirEntryErrAlloc
);
2705 switch (direntry
->tdir_type
)
2713 ma
=(uint32
*)origdata
;
2715 for (n
=0; n
<count
; n
++)
2717 if (tif
->tif_flags
&TIFF_SWAB
)
2719 *mb
++=(uint64
)(*ma
++);
2724 _TIFFfree(origdata
);
2725 if (err
!=TIFFReadDirEntryErrOk
)
2731 return(TIFFReadDirEntryErrOk
);
2734 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
* value
)
2736 enum TIFFReadDirEntryErr err
;
2740 if (direntry
->tdir_count
<(uint64
)tif
->tif_dir
.td_samplesperpixel
)
2741 return(TIFFReadDirEntryErrCount
);
2742 err
=TIFFReadDirEntryShortArray(tif
,direntry
,&m
);
2743 if (err
!=TIFFReadDirEntryErrOk
)
2746 nb
=tif
->tif_dir
.td_samplesperpixel
;
2753 err
=TIFFReadDirEntryErrPsdif
;
2763 static enum TIFFReadDirEntryErr
TIFFReadDirEntryPersampleDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
2765 enum TIFFReadDirEntryErr err
;
2769 if (direntry
->tdir_count
<(uint64
)tif
->tif_dir
.td_samplesperpixel
)
2770 return(TIFFReadDirEntryErrCount
);
2771 err
=TIFFReadDirEntryDoubleArray(tif
,direntry
,&m
);
2772 if (err
!=TIFFReadDirEntryErrOk
)
2775 nb
=tif
->tif_dir
.td_samplesperpixel
;
2782 err
=TIFFReadDirEntryErrPsdif
;
2792 static void TIFFReadDirEntryCheckedByte(TIFF
* tif
, TIFFDirEntry
* direntry
, uint8
* value
)
2795 *value
=*(uint8
*)(&direntry
->tdir_offset
);
2798 static void TIFFReadDirEntryCheckedSbyte(TIFF
* tif
, TIFFDirEntry
* direntry
, int8
* value
)
2801 *value
=*(int8
*)(&direntry
->tdir_offset
);
2804 static void TIFFReadDirEntryCheckedShort(TIFF
* tif
, TIFFDirEntry
* direntry
, uint16
* value
)
2806 *value
= direntry
->tdir_offset
.toff_short
;
2807 /* *value=*(uint16*)(&direntry->tdir_offset); */
2808 if (tif
->tif_flags
&TIFF_SWAB
)
2809 TIFFSwabShort(value
);
2812 static void TIFFReadDirEntryCheckedSshort(TIFF
* tif
, TIFFDirEntry
* direntry
, int16
* value
)
2814 *value
=*(int16
*)(&direntry
->tdir_offset
);
2815 if (tif
->tif_flags
&TIFF_SWAB
)
2816 TIFFSwabShort((uint16
*)value
);
2819 static void TIFFReadDirEntryCheckedLong(TIFF
* tif
, TIFFDirEntry
* direntry
, uint32
* value
)
2821 *value
=*(uint32
*)(&direntry
->tdir_offset
);
2822 if (tif
->tif_flags
&TIFF_SWAB
)
2823 TIFFSwabLong(value
);
2826 static void TIFFReadDirEntryCheckedSlong(TIFF
* tif
, TIFFDirEntry
* direntry
, int32
* value
)
2828 *value
=*(int32
*)(&direntry
->tdir_offset
);
2829 if (tif
->tif_flags
&TIFF_SWAB
)
2830 TIFFSwabLong((uint32
*)value
);
2833 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedLong8(TIFF
* tif
, TIFFDirEntry
* direntry
, uint64
* value
)
2835 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2837 enum TIFFReadDirEntryErr err
;
2838 uint32 offset
= direntry
->tdir_offset
.toff_long
;
2839 if (tif
->tif_flags
&TIFF_SWAB
)
2840 TIFFSwabLong(&offset
);
2841 err
=TIFFReadDirEntryData(tif
,offset
,8,value
);
2842 if (err
!=TIFFReadDirEntryErrOk
)
2846 *value
= direntry
->tdir_offset
.toff_long8
;
2847 if (tif
->tif_flags
&TIFF_SWAB
)
2848 TIFFSwabLong8(value
);
2849 return(TIFFReadDirEntryErrOk
);
2852 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSlong8(TIFF
* tif
, TIFFDirEntry
* direntry
, int64
* value
)
2854 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2856 enum TIFFReadDirEntryErr err
;
2857 uint32 offset
= direntry
->tdir_offset
.toff_long
;
2858 if (tif
->tif_flags
&TIFF_SWAB
)
2859 TIFFSwabLong(&offset
);
2860 err
=TIFFReadDirEntryData(tif
,offset
,8,value
);
2861 if (err
!=TIFFReadDirEntryErrOk
)
2865 *value
=*(int64
*)(&direntry
->tdir_offset
);
2866 if (tif
->tif_flags
&TIFF_SWAB
)
2867 TIFFSwabLong8((uint64
*)value
);
2868 return(TIFFReadDirEntryErrOk
);
2871 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedRational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
2875 assert(sizeof(double)==8);
2876 assert(sizeof(uint64
)==8);
2877 assert(sizeof(uint32
)==4);
2878 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2880 enum TIFFReadDirEntryErr err
;
2881 uint32 offset
= direntry
->tdir_offset
.toff_long
;
2882 if (tif
->tif_flags
&TIFF_SWAB
)
2883 TIFFSwabLong(&offset
);
2884 err
=TIFFReadDirEntryData(tif
,offset
,8,m
.i
);
2885 if (err
!=TIFFReadDirEntryErrOk
)
2889 m
.l
= direntry
->tdir_offset
.toff_long8
;
2890 if (tif
->tif_flags
&TIFF_SWAB
)
2891 TIFFSwabArrayOfLong(m
.i
,2);
2895 *value
=(double)m
.i
[0]/(double)m
.i
[1];
2896 return(TIFFReadDirEntryErrOk
);
2899 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedSrational(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
2902 assert(sizeof(double)==8);
2903 assert(sizeof(uint64
)==8);
2904 assert(sizeof(int32
)==4);
2905 assert(sizeof(uint32
)==4);
2906 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2908 enum TIFFReadDirEntryErr err
;
2909 uint32 offset
= direntry
->tdir_offset
.toff_long
;
2910 if (tif
->tif_flags
&TIFF_SWAB
)
2911 TIFFSwabLong(&offset
);
2912 err
=TIFFReadDirEntryData(tif
,offset
,8,m
.i
);
2913 if (err
!=TIFFReadDirEntryErrOk
)
2917 m
.l
=direntry
->tdir_offset
.toff_long8
;
2918 if (tif
->tif_flags
&TIFF_SWAB
)
2919 TIFFSwabArrayOfLong(m
.i
,2);
2920 if ((int32
)m
.i
[0]==0)
2923 *value
=(double)((int32
)m
.i
[0])/(double)m
.i
[1];
2924 return(TIFFReadDirEntryErrOk
);
2927 static void TIFFReadDirEntryCheckedFloat(TIFF
* tif
, TIFFDirEntry
* direntry
, float* value
)
2934 assert(sizeof(float)==4);
2935 assert(sizeof(uint32
)==4);
2936 assert(sizeof(float_union
)==4);
2937 float_union
.i
=*(uint32
*)(&direntry
->tdir_offset
);
2938 *value
=float_union
.f
;
2939 if (tif
->tif_flags
&TIFF_SWAB
)
2940 TIFFSwabLong((uint32
*)value
);
2943 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedDouble(TIFF
* tif
, TIFFDirEntry
* direntry
, double* value
)
2945 assert(sizeof(double)==8);
2946 assert(sizeof(uint64
)==8);
2947 assert(sizeof(UInt64Aligned_t
)==8);
2948 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2950 enum TIFFReadDirEntryErr err
;
2951 uint32 offset
= direntry
->tdir_offset
.toff_long
;
2952 if (tif
->tif_flags
&TIFF_SWAB
)
2953 TIFFSwabLong(&offset
);
2954 err
=TIFFReadDirEntryData(tif
,offset
,8,value
);
2955 if (err
!=TIFFReadDirEntryErrOk
)
2960 UInt64Aligned_t uint64_union
;
2961 uint64_union
.l
=direntry
->tdir_offset
.toff_long8
;
2962 *value
=uint64_union
.d
;
2964 if (tif
->tif_flags
&TIFF_SWAB
)
2965 TIFFSwabLong8((uint64
*)value
);
2966 return(TIFFReadDirEntryErrOk
);
2969 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSbyte(int8 value
)
2972 return(TIFFReadDirEntryErrRange
);
2974 return(TIFFReadDirEntryErrOk
);
2977 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteShort(uint16 value
)
2980 return(TIFFReadDirEntryErrRange
);
2982 return(TIFFReadDirEntryErrOk
);
2985 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSshort(int16 value
)
2987 if ((value
<0)||(value
>0xFF))
2988 return(TIFFReadDirEntryErrRange
);
2990 return(TIFFReadDirEntryErrOk
);
2993 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong(uint32 value
)
2996 return(TIFFReadDirEntryErrRange
);
2998 return(TIFFReadDirEntryErrOk
);
3001 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong(int32 value
)
3003 if ((value
<0)||(value
>0xFF))
3004 return(TIFFReadDirEntryErrRange
);
3006 return(TIFFReadDirEntryErrOk
);
3009 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteLong8(uint64 value
)
3012 return(TIFFReadDirEntryErrRange
);
3014 return(TIFFReadDirEntryErrOk
);
3017 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSlong8(int64 value
)
3019 if ((value
<0)||(value
>0xFF))
3020 return(TIFFReadDirEntryErrRange
);
3022 return(TIFFReadDirEntryErrOk
);
3025 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteByte(uint8 value
)
3028 return(TIFFReadDirEntryErrRange
);
3030 return(TIFFReadDirEntryErrOk
);
3033 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteShort(uint16 value
)
3036 return(TIFFReadDirEntryErrRange
);
3038 return(TIFFReadDirEntryErrOk
);
3041 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSshort(int16 value
)
3043 if ((value
<-0x80)||(value
>0x7F))
3044 return(TIFFReadDirEntryErrRange
);
3046 return(TIFFReadDirEntryErrOk
);
3049 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong(uint32 value
)
3052 return(TIFFReadDirEntryErrRange
);
3054 return(TIFFReadDirEntryErrOk
);
3057 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong(int32 value
)
3059 if ((value
<-0x80)||(value
>0x7F))
3060 return(TIFFReadDirEntryErrRange
);
3062 return(TIFFReadDirEntryErrOk
);
3065 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value
)
3068 return(TIFFReadDirEntryErrRange
);
3070 return(TIFFReadDirEntryErrOk
);
3073 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value
)
3075 if ((value
<-0x80)||(value
>0x7F))
3076 return(TIFFReadDirEntryErrRange
);
3078 return(TIFFReadDirEntryErrOk
);
3081 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSbyte(int8 value
)
3084 return(TIFFReadDirEntryErrRange
);
3086 return(TIFFReadDirEntryErrOk
);
3089 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSshort(int16 value
)
3092 return(TIFFReadDirEntryErrRange
);
3094 return(TIFFReadDirEntryErrOk
);
3097 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong(uint32 value
)
3100 return(TIFFReadDirEntryErrRange
);
3102 return(TIFFReadDirEntryErrOk
);
3105 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong(int32 value
)
3107 if ((value
<0)||(value
>0xFFFF))
3108 return(TIFFReadDirEntryErrRange
);
3110 return(TIFFReadDirEntryErrOk
);
3113 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortLong8(uint64 value
)
3116 return(TIFFReadDirEntryErrRange
);
3118 return(TIFFReadDirEntryErrOk
);
3121 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeShortSlong8(int64 value
)
3123 if ((value
<0)||(value
>0xFFFF))
3124 return(TIFFReadDirEntryErrRange
);
3126 return(TIFFReadDirEntryErrOk
);
3129 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortShort(uint16 value
)
3132 return(TIFFReadDirEntryErrRange
);
3134 return(TIFFReadDirEntryErrOk
);
3137 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong(uint32 value
)
3140 return(TIFFReadDirEntryErrRange
);
3142 return(TIFFReadDirEntryErrOk
);
3145 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong(int32 value
)
3147 if ((value
<-0x8000)||(value
>0x7FFF))
3148 return(TIFFReadDirEntryErrRange
);
3150 return(TIFFReadDirEntryErrOk
);
3153 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortLong8(uint64 value
)
3156 return(TIFFReadDirEntryErrRange
);
3158 return(TIFFReadDirEntryErrOk
);
3161 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeSshortSlong8(int64 value
)
3163 if ((value
<-0x8000)||(value
>0x7FFF))
3164 return(TIFFReadDirEntryErrRange
);
3166 return(TIFFReadDirEntryErrOk
);
3169 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSbyte(int8 value
)
3172 return(TIFFReadDirEntryErrRange
);
3174 return(TIFFReadDirEntryErrOk
);
3177 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSshort(int16 value
)
3180 return(TIFFReadDirEntryErrRange
);
3182 return(TIFFReadDirEntryErrOk
);
3185 static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeLongSlong(int32 value
)
3188 return(TIFFReadDirEntryErrRange
);
3190 return(TIFFReadDirEntryErrOk
);
3194 * Largest 32-bit unsigned integer value.
3196 #if defined(__WIN32__) && defined(_MSC_VER)
3197 # define TIFF_UINT32_MAX 0xFFFFFFFFI64
3199 # define TIFF_UINT32_MAX 0xFFFFFFFFLL
3202 static enum TIFFReadDirEntryErr
3203 TIFFReadDirEntryCheckRangeLongLong8(uint64 value
)
3205 if (value
> TIFF_UINT32_MAX
)
3206 return(TIFFReadDirEntryErrRange
);
3208 return(TIFFReadDirEntryErrOk
);
3211 static enum TIFFReadDirEntryErr
3212 TIFFReadDirEntryCheckRangeLongSlong8(int64 value
)
3214 if ((value
<0) || (value
> TIFF_UINT32_MAX
))
3215 return(TIFFReadDirEntryErrRange
);
3217 return(TIFFReadDirEntryErrOk
);
3220 #undef TIFF_UINT32_MAX
3222 static enum TIFFReadDirEntryErr
3223 TIFFReadDirEntryCheckRangeSlongLong(uint32 value
)
3225 if (value
> 0x7FFFFFFFUL
)
3226 return(TIFFReadDirEntryErrRange
);
3228 return(TIFFReadDirEntryErrOk
);
3231 static enum TIFFReadDirEntryErr
3232 TIFFReadDirEntryCheckRangeSlongLong8(uint64 value
)
3234 if (value
> 0x7FFFFFFFUL
)
3235 return(TIFFReadDirEntryErrRange
);
3237 return(TIFFReadDirEntryErrOk
);
3240 static enum TIFFReadDirEntryErr
3241 TIFFReadDirEntryCheckRangeSlongSlong8(int64 value
)
3243 if ((value
< 0L-0x80000000L
) || (value
> 0x7FFFFFFFL
))
3244 return(TIFFReadDirEntryErrRange
);
3246 return(TIFFReadDirEntryErrOk
);
3249 static enum TIFFReadDirEntryErr
3250 TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value
)
3253 return(TIFFReadDirEntryErrRange
);
3255 return(TIFFReadDirEntryErrOk
);
3258 static enum TIFFReadDirEntryErr
3259 TIFFReadDirEntryCheckRangeLong8Sshort(int16 value
)
3262 return(TIFFReadDirEntryErrRange
);
3264 return(TIFFReadDirEntryErrOk
);
3267 static enum TIFFReadDirEntryErr
3268 TIFFReadDirEntryCheckRangeLong8Slong(int32 value
)
3271 return(TIFFReadDirEntryErrRange
);
3273 return(TIFFReadDirEntryErrOk
);
3276 static enum TIFFReadDirEntryErr
3277 TIFFReadDirEntryCheckRangeLong8Slong8(int64 value
)
3280 return(TIFFReadDirEntryErrRange
);
3282 return(TIFFReadDirEntryErrOk
);
3286 * Largest 64-bit signed integer value.
3288 #if defined(__WIN32__) && defined(_MSC_VER)
3289 # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFI64
3291 # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFLL
3294 static enum TIFFReadDirEntryErr
3295 TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value
)
3297 if (value
> TIFF_INT64_MAX
)
3298 return(TIFFReadDirEntryErrRange
);
3300 return(TIFFReadDirEntryErrOk
);
3303 #undef TIFF_INT64_MAX
3305 static enum TIFFReadDirEntryErr
3306 TIFFReadDirEntryData(TIFF
* tif
, uint64 offset
, tmsize_t size
, void* dest
)
3309 if (!isMapped(tif
)) {
3310 if (!SeekOK(tif
,offset
))
3311 return(TIFFReadDirEntryErrIo
);
3312 if (!ReadOK(tif
,dest
,size
))
3313 return(TIFFReadDirEntryErrIo
);
3318 if (((uint64
)ma
!=offset
)
3320 || (mb
- ma
!= (size_t) size
)
3321 || (mb
< (size_t)size
)
3322 || (mb
> (size_t)tif
->tif_size
)
3324 return(TIFFReadDirEntryErrIo
);
3325 _TIFFmemcpy(dest
,tif
->tif_base
+ma
,size
);
3327 return(TIFFReadDirEntryErrOk
);
3330 static void TIFFReadDirEntryOutputErr(TIFF
* tif
, enum TIFFReadDirEntryErr err
, const char* module, const char* tagname
, int recover
)
3334 case TIFFReadDirEntryErrCount
:
3335 TIFFErrorExt(tif
->tif_clientdata
, module,
3336 "Incorrect count for \"%s\"",
3339 case TIFFReadDirEntryErrType
:
3340 TIFFErrorExt(tif
->tif_clientdata
, module,
3341 "Incompatible type for \"%s\"",
3344 case TIFFReadDirEntryErrIo
:
3345 TIFFErrorExt(tif
->tif_clientdata
, module,
3346 "IO error during reading of \"%s\"",
3349 case TIFFReadDirEntryErrRange
:
3350 TIFFErrorExt(tif
->tif_clientdata
, module,
3351 "Incorrect value for \"%s\"",
3354 case TIFFReadDirEntryErrPsdif
:
3355 TIFFErrorExt(tif
->tif_clientdata
, module,
3356 "Cannot handle different values per sample for \"%s\"",
3359 case TIFFReadDirEntryErrSizesan
:
3360 TIFFErrorExt(tif
->tif_clientdata
, module,
3361 "Sanity check on size of \"%s\" value failed",
3364 case TIFFReadDirEntryErrAlloc
:
3365 TIFFErrorExt(tif
->tif_clientdata
, module,
3366 "Out of memory reading of \"%s\"",
3370 assert(0); /* we should never get here */
3375 case TIFFReadDirEntryErrCount
:
3376 TIFFErrorExt(tif
->tif_clientdata
, module,
3377 "Incorrect count for \"%s\"; tag ignored",
3380 case TIFFReadDirEntryErrType
:
3381 TIFFWarningExt(tif
->tif_clientdata
, module,
3382 "Incompatible type for \"%s\"; tag ignored",
3385 case TIFFReadDirEntryErrIo
:
3386 TIFFWarningExt(tif
->tif_clientdata
, module,
3387 "IO error during reading of \"%s\"; tag ignored",
3390 case TIFFReadDirEntryErrRange
:
3391 TIFFWarningExt(tif
->tif_clientdata
, module,
3392 "Incorrect value for \"%s\"; tag ignored",
3395 case TIFFReadDirEntryErrPsdif
:
3396 TIFFWarningExt(tif
->tif_clientdata
, module,
3397 "Cannot handle different values per sample for \"%s\"; tag ignored",
3400 case TIFFReadDirEntryErrSizesan
:
3401 TIFFWarningExt(tif
->tif_clientdata
, module,
3402 "Sanity check on size of \"%s\" value failed; tag ignored",
3405 case TIFFReadDirEntryErrAlloc
:
3406 TIFFWarningExt(tif
->tif_clientdata
, module,
3407 "Out of memory reading of \"%s\"; tag ignored",
3411 assert(0); /* we should never get here */
3418 * Read the next TIFF directory from a file and convert it to the internal
3419 * format. We read directories sequentially.
3422 TIFFReadDirectory(TIFF
* tif
)
3424 static const char module[] = "TIFFReadDirectory";
3429 const TIFFField
* fip
;
3430 uint32 fii
=FAILED_FII
;
3432 tif
->tif_diroff
=tif
->tif_nextdiroff
;
3433 if (!TIFFCheckDirOffset(tif
,tif
->tif_nextdiroff
))
3434 return 0; /* last offset or bad offset (IFD looping) */
3435 (*tif
->tif_cleanup
)(tif
); /* cleanup any previous compression state */
3437 nextdiroff
= tif
->tif_nextdiroff
;
3438 dircount
=TIFFFetchDirectory(tif
,nextdiroff
,&dir
,&tif
->tif_nextdiroff
);
3441 TIFFErrorExt(tif
->tif_clientdata
,module,
3442 "Failed to read directory at offset " TIFF_UINT64_FORMAT
,nextdiroff
);
3445 TIFFReadDirectoryCheckOrder(tif
,dir
,dircount
);
3448 * Mark duplicates of any tag to be ignored (bugzilla 1994)
3449 * to avoid certain pathological problems.
3454 for (ma
=dir
, mb
=0; mb
<dircount
; ma
++, mb
++)
3458 for (na
=ma
+1, nb
=mb
+1; nb
<dircount
; na
++, nb
++)
3460 if (ma
->tdir_tag
==na
->tdir_tag
)
3461 na
->tdir_tag
=IGNORE
;
3466 tif
->tif_flags
&= ~TIFF_BEENWRITING
; /* reset before new dir */
3467 tif
->tif_flags
&= ~TIFF_BUF4WRITE
; /* reset before new dir */
3468 /* free any old stuff and reinit */
3469 TIFFFreeDirectory(tif
);
3470 TIFFDefaultDirectory(tif
);
3472 * Electronic Arts writes gray-scale TIFF files
3473 * without a PlanarConfiguration directory entry.
3474 * Thus we setup a default value here, even though
3475 * the TIFF spec says there is no default value.
3477 TIFFSetField(tif
,TIFFTAG_PLANARCONFIG
,PLANARCONFIG_CONTIG
);
3479 * Setup default value and then make a pass over
3480 * the fields to check type and tag information,
3481 * and to extract info required to size data
3482 * structures. A second pass is made afterwards
3483 * to read in everthing not taken in the first pass.
3484 * But we must process the Compression tag first
3485 * in order to merge in codec-private tag definitions (otherwise
3486 * we may get complaints about unknown tags). However, the
3487 * Compression tag may be dependent on the SamplesPerPixel
3488 * tag value because older TIFF specs permited Compression
3489 * to be written as a SamplesPerPixel-count tag entry.
3490 * Thus if we don't first figure out the correct SamplesPerPixel
3491 * tag value then we may end up ignoring the Compression tag
3492 * value because it has an incorrect count value (if the
3493 * true value of SamplesPerPixel is not 1).
3495 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,TIFFTAG_SAMPLESPERPIXEL
);
3498 if (!TIFFFetchNormalTag(tif
,dp
,0))
3500 dp
->tdir_tag
=IGNORE
;
3502 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,TIFFTAG_COMPRESSION
);
3506 * The 5.0 spec says the Compression tag has one value, while
3507 * earlier specs say it has one value per sample. Because of
3508 * this, we accept the tag if one value is supplied with either
3512 enum TIFFReadDirEntryErr err
;
3513 err
=TIFFReadDirEntryShort(tif
,dp
,&value
);
3514 if (err
==TIFFReadDirEntryErrCount
)
3515 err
=TIFFReadDirEntryPersampleShort(tif
,dp
,&value
);
3516 if (err
!=TIFFReadDirEntryErrOk
)
3518 TIFFReadDirEntryOutputErr(tif
,err
,module,"Compression",0);
3521 if (!TIFFSetField(tif
,TIFFTAG_COMPRESSION
,value
))
3523 dp
->tdir_tag
=IGNORE
;
3527 if (!TIFFSetField(tif
,TIFFTAG_COMPRESSION
,COMPRESSION_NONE
))
3531 * First real pass over the directory.
3533 for (di
=0, dp
=dir
; di
<dircount
; di
++, dp
++)
3535 if (dp
->tdir_tag
!=IGNORE
)
3537 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
3538 if (fii
== FAILED_FII
)
3540 TIFFWarningExt(tif
->tif_clientdata
, module,
3541 "Unknown field with tag %d (0x%x) encountered",
3542 dp
->tdir_tag
,dp
->tdir_tag
);
3543 /* the following knowingly leaks the
3544 anonymous field structure */
3545 if (!_TIFFMergeFields(tif
,
3546 _TIFFCreateAnonField(tif
,
3548 (TIFFDataType
) dp
->tdir_type
),
3550 TIFFWarningExt(tif
->tif_clientdata
,
3552 "Registering anonymous field with tag %d (0x%x) failed",
3555 dp
->tdir_tag
=IGNORE
;
3557 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
3558 assert(fii
!= FAILED_FII
);
3562 if (dp
->tdir_tag
!=IGNORE
)
3564 fip
=tif
->tif_fields
[fii
];
3565 if (fip
->field_bit
==FIELD_IGNORE
)
3566 dp
->tdir_tag
=IGNORE
;
3569 switch (dp
->tdir_tag
)
3571 case TIFFTAG_STRIPOFFSETS
:
3572 case TIFFTAG_STRIPBYTECOUNTS
:
3573 case TIFFTAG_TILEOFFSETS
:
3574 case TIFFTAG_TILEBYTECOUNTS
:
3575 TIFFSetFieldBit(tif
,fip
->field_bit
);
3577 case TIFFTAG_IMAGEWIDTH
:
3578 case TIFFTAG_IMAGELENGTH
:
3579 case TIFFTAG_IMAGEDEPTH
:
3580 case TIFFTAG_TILELENGTH
:
3581 case TIFFTAG_TILEWIDTH
:
3582 case TIFFTAG_TILEDEPTH
:
3583 case TIFFTAG_PLANARCONFIG
:
3584 case TIFFTAG_ROWSPERSTRIP
:
3585 case TIFFTAG_EXTRASAMPLES
:
3586 if (!TIFFFetchNormalTag(tif
,dp
,0))
3588 dp
->tdir_tag
=IGNORE
;
3596 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3597 * c) strip offsets/bytecounts tag are both present and
3598 * d) both contain exactly one value, then we consistently find
3599 * that the buggy implementation of the buggy compression scheme
3600 * matches contig planarconfig best. So we 'fix-up' the tag here
3602 if ((tif
->tif_dir
.td_compression
==COMPRESSION_OJPEG
)&&
3603 (tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_SEPARATE
))
3605 if (!_TIFFFillStriles(tif
))
3607 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,TIFFTAG_STRIPOFFSETS
);
3608 if ((dp
!=0)&&(dp
->tdir_count
==1))
3610 dp
=TIFFReadDirectoryFindEntry(tif
,dir
,dircount
,
3611 TIFFTAG_STRIPBYTECOUNTS
);
3612 if ((dp
!=0)&&(dp
->tdir_count
==1))
3614 tif
->tif_dir
.td_planarconfig
=PLANARCONFIG_CONTIG
;
3615 TIFFWarningExt(tif
->tif_clientdata
,module,
3616 "Planarconfig tag value assumed incorrect, "
3617 "assuming data is contig instead of chunky");
3622 * Allocate directory structure and setup defaults.
3624 if (!TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
3626 MissingRequired(tif
,"ImageLength");
3630 * Setup appropriate structures (by strip or by tile)
3632 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) {
3633 tif
->tif_dir
.td_nstrips
= TIFFNumberOfStrips(tif
);
3634 tif
->tif_dir
.td_tilewidth
= tif
->tif_dir
.td_imagewidth
;
3635 tif
->tif_dir
.td_tilelength
= tif
->tif_dir
.td_rowsperstrip
;
3636 tif
->tif_dir
.td_tiledepth
= tif
->tif_dir
.td_imagedepth
;
3637 tif
->tif_flags
&= ~TIFF_ISTILED
;
3639 tif
->tif_dir
.td_nstrips
= TIFFNumberOfTiles(tif
);
3640 tif
->tif_flags
|= TIFF_ISTILED
;
3642 if (!tif
->tif_dir
.td_nstrips
) {
3643 TIFFErrorExt(tif
->tif_clientdata
, module,
3644 "Cannot handle zero number of %s",
3645 isTiled(tif
) ? "tiles" : "strips");
3648 tif
->tif_dir
.td_stripsperimage
= tif
->tif_dir
.td_nstrips
;
3649 if (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
)
3650 tif
->tif_dir
.td_stripsperimage
/= tif
->tif_dir
.td_samplesperpixel
;
3651 if (!TIFFFieldSet(tif
, FIELD_STRIPOFFSETS
)) {
3652 if ((tif
->tif_dir
.td_compression
==COMPRESSION_OJPEG
) &&
3653 (isTiled(tif
)==0) &&
3654 (tif
->tif_dir
.td_nstrips
==1)) {
3657 * If a) compression is OJPEG, b) it's not a tiled TIFF,
3658 * and c) the number of strips is 1,
3659 * then we tolerate the absence of stripoffsets tag,
3660 * because, presumably, all required data is in the
3661 * JpegInterchangeFormat stream.
3663 TIFFSetFieldBit(tif
, FIELD_STRIPOFFSETS
);
3665 MissingRequired(tif
,
3666 isTiled(tif
) ? "TileOffsets" : "StripOffsets");
3671 * Second pass: extract other information.
3673 for (di
=0, dp
=dir
; di
<dircount
; di
++, dp
++)
3675 switch (dp
->tdir_tag
)
3679 case TIFFTAG_MINSAMPLEVALUE
:
3680 case TIFFTAG_MAXSAMPLEVALUE
:
3681 case TIFFTAG_BITSPERSAMPLE
:
3682 case TIFFTAG_DATATYPE
:
3683 case TIFFTAG_SAMPLEFORMAT
:
3685 * The MinSampleValue, MaxSampleValue, BitsPerSample
3686 * DataType and SampleFormat tags are supposed to be
3687 * written as one value/sample, but some vendors
3688 * incorrectly write one value only -- so we accept
3689 * that as well (yech). Other vendors write correct
3690 * value for NumberOfSamples, but incorrect one for
3691 * BitsPerSample and friends, and we will read this
3696 enum TIFFReadDirEntryErr err
;
3697 err
=TIFFReadDirEntryShort(tif
,dp
,&value
);
3698 if (err
==TIFFReadDirEntryErrCount
)
3699 err
=TIFFReadDirEntryPersampleShort(tif
,dp
,&value
);
3700 if (err
!=TIFFReadDirEntryErrOk
)
3702 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3703 TIFFReadDirEntryOutputErr(tif
,err
,module,fip
? fip
->field_name
: "unknown tagname",0);
3706 if (!TIFFSetField(tif
,dp
->tdir_tag
,value
))
3710 case TIFFTAG_SMINSAMPLEVALUE
:
3711 case TIFFTAG_SMAXSAMPLEVALUE
:
3715 enum TIFFReadDirEntryErr err
;
3718 if (dp
->tdir_count
!= (uint64
)tif
->tif_dir
.td_samplesperpixel
)
3719 err
= TIFFReadDirEntryErrCount
;
3721 err
= TIFFReadDirEntryDoubleArray(tif
, dp
, &data
);
3722 if (err
!=TIFFReadDirEntryErrOk
)
3724 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3725 TIFFReadDirEntryOutputErr(tif
,err
,module,fip
? fip
->field_name
: "unknown tagname",0);
3728 saved_flags
= tif
->tif_flags
;
3729 tif
->tif_flags
|= TIFF_PERSAMPLE
;
3730 m
= TIFFSetField(tif
,dp
->tdir_tag
,data
);
3731 tif
->tif_flags
= saved_flags
;
3737 case TIFFTAG_STRIPOFFSETS
:
3738 case TIFFTAG_TILEOFFSETS
:
3739 #if defined(DEFER_STRILE_LOAD)
3740 _TIFFmemcpy( &(tif
->tif_dir
.td_stripoffset_entry
),
3741 dp
, sizeof(TIFFDirEntry
) );
3743 if (!TIFFFetchStripThing(tif
,dp
,tif
->tif_dir
.td_nstrips
,&tif
->tif_dir
.td_stripoffset
))
3747 case TIFFTAG_STRIPBYTECOUNTS
:
3748 case TIFFTAG_TILEBYTECOUNTS
:
3749 #if defined(DEFER_STRILE_LOAD)
3750 _TIFFmemcpy( &(tif
->tif_dir
.td_stripbytecount_entry
),
3751 dp
, sizeof(TIFFDirEntry
) );
3753 if (!TIFFFetchStripThing(tif
,dp
,tif
->tif_dir
.td_nstrips
,&tif
->tif_dir
.td_stripbytecount
))
3757 case TIFFTAG_COLORMAP
:
3758 case TIFFTAG_TRANSFERFUNCTION
:
3760 enum TIFFReadDirEntryErr err
;
3761 uint32 countpersample
;
3762 uint32 countrequired
;
3763 uint32 incrementpersample
;
3765 countpersample
=(1L<<tif
->tif_dir
.td_bitspersample
);
3766 if ((dp
->tdir_tag
==TIFFTAG_TRANSFERFUNCTION
)&&(dp
->tdir_count
==(uint64
)countpersample
))
3768 countrequired
=countpersample
;
3769 incrementpersample
=0;
3773 countrequired
=3*countpersample
;
3774 incrementpersample
=countpersample
;
3776 if (dp
->tdir_count
!=(uint64
)countrequired
)
3777 err
=TIFFReadDirEntryErrCount
;
3779 err
=TIFFReadDirEntryShortArray(tif
,dp
,&value
);
3780 if (err
!=TIFFReadDirEntryErrOk
)
3782 fip
= TIFFFieldWithTag(tif
,dp
->tdir_tag
);
3783 TIFFReadDirEntryOutputErr(tif
,err
,module,fip
? fip
->field_name
: "unknown tagname",1);
3787 TIFFSetField(tif
,dp
->tdir_tag
,value
,value
+incrementpersample
,value
+2*incrementpersample
);
3792 /* BEGIN REV 4.0 COMPATIBILITY */
3793 case TIFFTAG_OSUBFILETYPE
:
3797 if (TIFFReadDirEntryShort(tif
,dp
,&valueo
)==TIFFReadDirEntryErrOk
)
3801 case OFILETYPE_REDUCEDIMAGE
: value
=FILETYPE_REDUCEDIMAGE
; break;
3802 case OFILETYPE_PAGE
: value
=FILETYPE_PAGE
; break;
3803 default: value
=0; break;
3806 TIFFSetField(tif
,TIFFTAG_SUBFILETYPE
,value
);
3810 /* END REV 4.0 COMPATIBILITY */
3812 (void) TIFFFetchNormalTag(tif
, dp
, TRUE
);
3818 * - If a) compression is OJPEG, and b) photometric tag is missing,
3819 * then we consistently find that photometric should be YCbCr
3820 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
3821 * then we consistently find that the buggy implementation of the
3822 * buggy compression scheme matches photometric YCbCr instead.
3823 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
3824 * then we consistently find bitspersample should be 8.
3825 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3826 * and c) photometric is RGB or YCbCr, then we consistently find
3827 * samplesperpixel should be 3
3828 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3829 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
3830 * find samplesperpixel should be 3
3832 if (tif
->tif_dir
.td_compression
==COMPRESSION_OJPEG
)
3834 if (!TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
3836 TIFFWarningExt(tif
->tif_clientdata
, module,
3837 "Photometric tag is missing, assuming data is YCbCr");
3838 if (!TIFFSetField(tif
,TIFFTAG_PHOTOMETRIC
,PHOTOMETRIC_YCBCR
))
3841 else if (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_RGB
)
3843 tif
->tif_dir
.td_photometric
=PHOTOMETRIC_YCBCR
;
3844 TIFFWarningExt(tif
->tif_clientdata
, module,
3845 "Photometric tag value assumed incorrect, "
3846 "assuming data is YCbCr instead of RGB");
3848 if (!TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
3850 TIFFWarningExt(tif
->tif_clientdata
,module,
3851 "BitsPerSample tag is missing, assuming 8 bits per sample");
3852 if (!TIFFSetField(tif
,TIFFTAG_BITSPERSAMPLE
,8))
3855 if (!TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
3857 if (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_RGB
)
3859 TIFFWarningExt(tif
->tif_clientdata
,module,
3860 "SamplesPerPixel tag is missing, "
3861 "assuming correct SamplesPerPixel value is 3");
3862 if (!TIFFSetField(tif
,TIFFTAG_SAMPLESPERPIXEL
,3))
3865 if (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_YCBCR
)
3867 TIFFWarningExt(tif
->tif_clientdata
,module,
3868 "SamplesPerPixel tag is missing, "
3869 "applying correct SamplesPerPixel value of 3");
3870 if (!TIFFSetField(tif
,TIFFTAG_SAMPLESPERPIXEL
,3))
3873 else if ((tif
->tif_dir
.td_photometric
==PHOTOMETRIC_MINISWHITE
)
3874 || (tif
->tif_dir
.td_photometric
==PHOTOMETRIC_MINISBLACK
))
3877 * SamplesPerPixel tag is missing, but is not required
3878 * by spec. Assume correct SamplesPerPixel value of 1.
3880 if (!TIFFSetField(tif
,TIFFTAG_SAMPLESPERPIXEL
,1))
3886 * Verify Palette image has a Colormap.
3888 if (tif
->tif_dir
.td_photometric
== PHOTOMETRIC_PALETTE
&&
3889 !TIFFFieldSet(tif
, FIELD_COLORMAP
)) {
3890 if ( tif
->tif_dir
.td_bitspersample
>=8 && tif
->tif_dir
.td_samplesperpixel
==3)
3891 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_RGB
;
3892 else if (tif
->tif_dir
.td_bitspersample
>=8)
3893 tif
->tif_dir
.td_photometric
= PHOTOMETRIC_MINISBLACK
;
3895 MissingRequired(tif
, "Colormap");
3901 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
3904 if (tif
->tif_dir
.td_compression
!=COMPRESSION_OJPEG
)
3907 * Attempt to deal with a missing StripByteCounts tag.
3909 if (!TIFFFieldSet(tif
, FIELD_STRIPBYTECOUNTS
)) {
3911 * Some manufacturers violate the spec by not giving
3912 * the size of the strips. In this case, assume there
3913 * is one uncompressed strip of data.
3915 if ((tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
&&
3916 tif
->tif_dir
.td_nstrips
> 1) ||
3917 (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_SEPARATE
&&
3918 tif
->tif_dir
.td_nstrips
!= (uint32
)tif
->tif_dir
.td_samplesperpixel
)) {
3919 MissingRequired(tif
, "StripByteCounts");
3922 TIFFWarningExt(tif
->tif_clientdata
, module,
3923 "TIFF directory is missing required "
3924 "\"StripByteCounts\" field, calculating from imagelength");
3925 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
3928 * Assume we have wrong StripByteCount value (in case
3929 * of single strip) in following cases:
3930 * - it is equal to zero along with StripOffset;
3931 * - it is larger than file itself (in case of uncompressed
3933 * - it is smaller than the size of the bytes per row
3934 * multiplied on the number of rows. The last case should
3935 * not be checked in the case of writing new image,
3936 * because we may do not know the exact strip size
3937 * until the whole image will be written and directory
3940 #define BYTECOUNTLOOKSBAD \
3941 ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
3942 (tif->tif_dir.td_compression == COMPRESSION_NONE && \
3943 tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
3944 (tif->tif_mode == O_RDONLY && \
3945 tif->tif_dir.td_compression == COMPRESSION_NONE && \
3946 tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
3948 } else if (tif
->tif_dir
.td_nstrips
== 1
3949 && _TIFFFillStriles(tif
)
3950 && tif
->tif_dir
.td_stripoffset
[0] != 0
3951 && BYTECOUNTLOOKSBAD
) {
3953 * XXX: Plexus (and others) sometimes give a value of
3954 * zero for a tag when they don't know what the
3955 * correct value is! Try and handle the simple case
3956 * of estimating the size of a one strip image.
3958 TIFFWarningExt(tif
->tif_clientdata
, module,
3959 "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
3960 if(EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
3963 #if !defined(DEFER_STRILE_LOAD)
3964 } else if (tif
->tif_dir
.td_planarconfig
== PLANARCONFIG_CONTIG
3965 && tif
->tif_dir
.td_nstrips
> 2
3966 && tif
->tif_dir
.td_compression
== COMPRESSION_NONE
3967 && tif
->tif_dir
.td_stripbytecount
[0] != tif
->tif_dir
.td_stripbytecount
[1]
3968 && tif
->tif_dir
.td_stripbytecount
[0] != 0
3969 && tif
->tif_dir
.td_stripbytecount
[1] != 0 ) {
3971 * XXX: Some vendors fill StripByteCount array with
3972 * absolutely wrong values (it can be equal to
3973 * StripOffset array, for example). Catch this case
3976 * We avoid this check if deferring strile loading
3977 * as it would always force us to load the strip/tile
3980 TIFFWarningExt(tif
->tif_clientdata
, module,
3981 "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
3982 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0)
3984 #endif /* !defined(DEFER_STRILE_LOAD) */
3992 if (!TIFFFieldSet(tif
, FIELD_MAXSAMPLEVALUE
))
3994 if (tif
->tif_dir
.td_bitspersample
>=16)
3995 tif
->tif_dir
.td_maxsamplevalue
=0xFFFF;
3997 tif
->tif_dir
.td_maxsamplevalue
= (uint16
)((1L<<tif
->tif_dir
.td_bitspersample
)-1);
4000 * XXX: We can optimize checking for the strip bounds using the sorted
4001 * bytecounts array. See also comments for TIFFAppendToStrip()
4002 * function in tif_write.c.
4004 #if !defined(DEFER_STRILE_LOAD)
4005 if (tif
->tif_dir
.td_nstrips
> 1) {
4008 tif
->tif_dir
.td_stripbytecountsorted
= 1;
4009 for (strip
= 1; strip
< tif
->tif_dir
.td_nstrips
; strip
++) {
4010 if (tif
->tif_dir
.td_stripoffset
[strip
- 1] >
4011 tif
->tif_dir
.td_stripoffset
[strip
]) {
4012 tif
->tif_dir
.td_stripbytecountsorted
= 0;
4017 #endif /* !defined(DEFER_STRILE_LOAD) */
4020 * An opportunity for compression mode dependent tag fixup
4022 (*tif
->tif_fixuptags
)(tif
);
4025 * Some manufacturers make life difficult by writing
4026 * large amounts of uncompressed data as a single strip.
4027 * This is contrary to the recommendations of the spec.
4028 * The following makes an attempt at breaking such images
4029 * into strips closer to the recommended 8k bytes. A
4030 * side effect, however, is that the RowsPerStrip tag
4031 * value may be changed.
4033 if ((tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_CONTIG
)&&
4034 (tif
->tif_dir
.td_nstrips
==1)&&
4035 (tif
->tif_dir
.td_compression
==COMPRESSION_NONE
)&&
4036 ((tif
->tif_flags
&(TIFF_STRIPCHOP
|TIFF_ISTILED
))==TIFF_STRIPCHOP
))
4038 if ( !_TIFFFillStriles(tif
) || !tif
->tif_dir
.td_stripbytecount
)
4040 ChopUpSingleUncompressedStrip(tif
);
4044 * Clear the dirty directory flag.
4046 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
4047 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
4050 * Reinitialize i/o since we are starting on a new directory.
4052 tif
->tif_row
= (uint32
) -1;
4053 tif
->tif_curstrip
= (uint32
) -1;
4054 tif
->tif_col
= (uint32
) -1;
4055 tif
->tif_curtile
= (uint32
) -1;
4056 tif
->tif_tilesize
= (tmsize_t
) -1;
4058 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
4059 if (!tif
->tif_scanlinesize
) {
4060 TIFFErrorExt(tif
->tif_clientdata
, module,
4061 "Cannot handle zero scanline size");
4066 tif
->tif_tilesize
= TIFFTileSize(tif
);
4067 if (!tif
->tif_tilesize
) {
4068 TIFFErrorExt(tif
->tif_clientdata
, module,
4069 "Cannot handle zero tile size");
4073 if (!TIFFStripSize(tif
)) {
4074 TIFFErrorExt(tif
->tif_clientdata
, module,
4075 "Cannot handle zero strip size");
4087 TIFFReadDirectoryCheckOrder(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
)
4089 static const char module[] = "TIFFReadDirectoryCheckOrder";
4094 for (n
=0, o
=dir
; n
<dircount
; n
++, o
++)
4098 TIFFWarningExt(tif
->tif_clientdata
,module,
4099 "Invalid TIFF directory; tags are not sorted in ascending order");
4106 static TIFFDirEntry
*
4107 TIFFReadDirectoryFindEntry(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
, uint16 tagid
)
4112 for (m
=dir
, n
=0; n
<dircount
; m
++, n
++)
4114 if (m
->tdir_tag
==tagid
)
4121 TIFFReadDirectoryFindFieldInfo(TIFF
* tif
, uint16 tagid
, uint32
* fii
)
4125 mc
=(int32
)tif
->tif_nfields
;
4134 if (tif
->tif_fields
[mb
]->field_tag
==(uint32
)tagid
)
4136 if (tif
->tif_fields
[mb
]->field_tag
<(uint32
)tagid
)
4145 if (tif
->tif_fields
[mb
-1]->field_tag
!=(uint32
)tagid
)
4153 * Read custom directory from the arbitarry offset.
4154 * The code is very similar to TIFFReadDirectory().
4157 TIFFReadCustomDirectory(TIFF
* tif
, toff_t diroff
,
4158 const TIFFFieldArray
* infoarray
)
4160 static const char module[] = "TIFFReadCustomDirectory";
4165 const TIFFField
* fip
;
4167 _TIFFSetupFields(tif
, infoarray
);
4168 dircount
=TIFFFetchDirectory(tif
,diroff
,&dir
,NULL
);
4171 TIFFErrorExt(tif
->tif_clientdata
,module,
4172 "Failed to read custom directory at offset " TIFF_UINT64_FORMAT
,diroff
);
4175 TIFFFreeDirectory(tif
);
4176 _TIFFmemset(&tif
->tif_dir
, 0, sizeof(TIFFDirectory
));
4177 TIFFReadDirectoryCheckOrder(tif
,dir
,dircount
);
4178 for (di
=0, dp
=dir
; di
<dircount
; di
++, dp
++)
4180 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
4181 if (fii
== FAILED_FII
)
4183 TIFFWarningExt(tif
->tif_clientdata
, module,
4184 "Unknown field with tag %d (0x%x) encountered",
4185 dp
->tdir_tag
, dp
->tdir_tag
);
4186 if (!_TIFFMergeFields(tif
, _TIFFCreateAnonField(tif
,
4188 (TIFFDataType
) dp
->tdir_type
),
4190 TIFFWarningExt(tif
->tif_clientdata
, module,
4191 "Registering anonymous field with tag %d (0x%x) failed",
4192 dp
->tdir_tag
, dp
->tdir_tag
);
4193 dp
->tdir_tag
=IGNORE
;
4195 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
4196 assert( fii
!= FAILED_FII
);
4199 if (dp
->tdir_tag
!=IGNORE
)
4201 fip
=tif
->tif_fields
[fii
];
4202 if (fip
->field_bit
==FIELD_IGNORE
)
4203 dp
->tdir_tag
=IGNORE
;
4206 /* check data type */
4207 while ((fip
->field_type
!=TIFF_ANY
)&&(fip
->field_type
!=dp
->tdir_type
))
4210 if ((fii
==tif
->tif_nfields
)||
4211 (tif
->tif_fields
[fii
]->field_tag
!=(uint32
)dp
->tdir_tag
))
4216 fip
=tif
->tif_fields
[fii
];
4220 TIFFWarningExt(tif
->tif_clientdata
, module,
4221 "Wrong data type %d for \"%s\"; tag ignored",
4222 dp
->tdir_type
,fip
->field_name
);
4223 dp
->tdir_tag
=IGNORE
;
4227 /* check count if known in advance */
4228 if ((fip
->field_readcount
!=TIFF_VARIABLE
)&&
4229 (fip
->field_readcount
!=TIFF_VARIABLE2
))
4232 if (fip
->field_readcount
==TIFF_SPP
)
4233 expected
=(uint32
)tif
->tif_dir
.td_samplesperpixel
;
4235 expected
=(uint32
)fip
->field_readcount
;
4236 if (!CheckDirCount(tif
,dp
,expected
))
4237 dp
->tdir_tag
=IGNORE
;
4241 switch (dp
->tdir_tag
)
4245 case EXIFTAG_SUBJECTDISTANCE
:
4246 (void) TIFFFetchSubjectDistance(tif
,dp
);
4249 (void) TIFFFetchNormalTag(tif
, dp
, TRUE
);
4260 * EXIF is important special case of custom IFD, so we have a special
4261 * function to read it.
4264 TIFFReadEXIFDirectory(TIFF
* tif
, toff_t diroff
)
4266 const TIFFFieldArray
* exifFieldArray
;
4267 exifFieldArray
= _TIFFGetExifFields();
4268 return TIFFReadCustomDirectory(tif
, diroff
, exifFieldArray
);
4272 EstimateStripByteCounts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
)
4274 static const char module[] = "EstimateStripByteCounts";
4277 TIFFDirectory
*td
= &tif
->tif_dir
;
4280 _TIFFFillStriles( tif
);
4282 if (td
->td_stripbytecount
)
4283 _TIFFfree(td
->td_stripbytecount
);
4284 td
->td_stripbytecount
= (uint64
*)
4285 _TIFFCheckMalloc(tif
, td
->td_nstrips
, sizeof (uint64
),
4286 "for \"StripByteCounts\" array");
4287 if( td
->td_stripbytecount
== NULL
)
4290 if (td
->td_compression
!= COMPRESSION_NONE
) {
4294 filesize
= TIFFGetFileSize(tif
);
4295 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4296 space
=sizeof(TIFFHeaderClassic
)+2+dircount
*12+4;
4298 space
=sizeof(TIFFHeaderBig
)+8+dircount
*20+8;
4299 /* calculate amount of space used by indirect values */
4300 for (dp
= dir
, n
= dircount
; n
> 0; n
--, dp
++)
4302 uint32 typewidth
= TIFFDataWidth((TIFFDataType
) dp
->tdir_type
);
4304 typewidth
= TIFFDataWidth((TIFFDataType
) dp
->tdir_type
);
4305 if (typewidth
== 0) {
4306 TIFFErrorExt(tif
->tif_clientdata
, module,
4307 "Cannot determine size of unknown tag type %d",
4311 datasize
=(uint64
)typewidth
*dp
->tdir_count
;
4312 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4324 space
= filesize
- space
;
4325 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
)
4326 space
/= td
->td_samplesperpixel
;
4327 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
4328 td
->td_stripbytecount
[strip
] = space
;
4330 * This gross hack handles the case were the offset to
4331 * the last strip is past the place where we think the strip
4332 * should begin. Since a strip of data must be contiguous,
4333 * it's safe to assume that we've overestimated the amount
4334 * of data in the strip and trim this number back accordingly.
4337 if (td
->td_stripoffset
[strip
]+td
->td_stripbytecount
[strip
] > filesize
)
4338 td
->td_stripbytecount
[strip
] = filesize
- td
->td_stripoffset
[strip
];
4339 } else if (isTiled(tif
)) {
4340 uint64 bytespertile
= TIFFTileSize64(tif
);
4342 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
4343 td
->td_stripbytecount
[strip
] = bytespertile
;
4345 uint64 rowbytes
= TIFFScanlineSize64(tif
);
4346 uint32 rowsperstrip
= td
->td_imagelength
/td
->td_stripsperimage
;
4347 for (strip
= 0; strip
< td
->td_nstrips
; strip
++)
4348 td
->td_stripbytecount
[strip
] = rowbytes
* rowsperstrip
;
4350 TIFFSetFieldBit(tif
, FIELD_STRIPBYTECOUNTS
);
4351 if (!TIFFFieldSet(tif
, FIELD_ROWSPERSTRIP
))
4352 td
->td_rowsperstrip
= td
->td_imagelength
;
4357 MissingRequired(TIFF
* tif
, const char* tagname
)
4359 static const char module[] = "MissingRequired";
4361 TIFFErrorExt(tif
->tif_clientdata
, module,
4362 "TIFF directory is missing required \"%s\" field",
4367 * Check the directory offset against the list of already seen directory
4368 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4369 * file with looped directory pointers. We will maintain a list of already
4370 * seen directories and check every IFD offset against that list.
4373 TIFFCheckDirOffset(TIFF
* tif
, uint64 diroff
)
4377 if (diroff
== 0) /* no more directories */
4380 for (n
= 0; n
< tif
->tif_dirnumber
&& tif
->tif_dirlist
; n
++) {
4381 if (tif
->tif_dirlist
[n
] == diroff
)
4385 tif
->tif_dirnumber
++;
4387 if (tif
->tif_dirnumber
> tif
->tif_dirlistsize
) {
4388 uint64
* new_dirlist
;
4391 * XXX: Reduce memory allocation granularity of the dirlist
4394 new_dirlist
= (uint64
*)_TIFFCheckRealloc(tif
, tif
->tif_dirlist
,
4395 tif
->tif_dirnumber
, 2 * sizeof(uint64
), "for IFD list");
4398 tif
->tif_dirlistsize
= 2 * tif
->tif_dirnumber
;
4399 tif
->tif_dirlist
= new_dirlist
;
4402 tif
->tif_dirlist
[tif
->tif_dirnumber
- 1] = diroff
;
4408 * Check the count field of a directory entry against a known value. The
4409 * caller is expected to skip/ignore the tag if there is a mismatch.
4412 CheckDirCount(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 count
)
4414 if ((uint64
)count
> dir
->tdir_count
) {
4415 const TIFFField
* fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
4416 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
4417 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT
", expecting %u); tag ignored",
4418 fip
? fip
->field_name
: "unknown tagname",
4419 dir
->tdir_count
, count
);
4421 } else if ((uint64
)count
< dir
->tdir_count
) {
4422 const TIFFField
* fip
= TIFFFieldWithTag(tif
, dir
->tdir_tag
);
4423 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
4424 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT
", expecting %u); tag trimmed",
4425 fip
? fip
->field_name
: "unknown tagname",
4426 dir
->tdir_count
, count
);
4427 dir
->tdir_count
= count
;
4434 * Read IFD structure from the specified offset. If the pointer to
4435 * nextdiroff variable has been specified, read it too. Function returns a
4436 * number of fields in the directory or 0 if failed.
4439 TIFFFetchDirectory(TIFF
* tif
, uint64 diroff
, TIFFDirEntry
** pdir
,
4442 static const char module[] = "TIFFFetchDirectory";
4454 tif
->tif_diroff
= diroff
;
4457 if (!isMapped(tif
)) {
4458 if (!SeekOK(tif
, tif
->tif_diroff
)) {
4459 TIFFErrorExt(tif
->tif_clientdata
, module,
4460 "%s: Seek error accessing TIFF directory",
4464 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4466 if (!ReadOK(tif
, &dircount16
, sizeof (uint16
))) {
4467 TIFFErrorExt(tif
->tif_clientdata
, module,
4468 "%s: Can not read TIFF directory count",
4472 if (tif
->tif_flags
& TIFF_SWAB
)
4473 TIFFSwabShort(&dircount16
);
4474 if (dircount16
>4096)
4476 TIFFErrorExt(tif
->tif_clientdata
, module,
4477 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4483 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
4484 TIFFErrorExt(tif
->tif_clientdata
, module,
4485 "%s: Can not read TIFF directory count",
4489 if (tif
->tif_flags
& TIFF_SWAB
)
4490 TIFFSwabLong8(&dircount64
);
4491 if (dircount64
>4096)
4493 TIFFErrorExt(tif
->tif_clientdata
, module,
4494 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4497 dircount16
= (uint16
)dircount64
;
4500 origdir
= _TIFFCheckMalloc(tif
, dircount16
,
4501 dirsize
, "to read TIFF directory");
4502 if (origdir
== NULL
)
4504 if (!ReadOK(tif
, origdir
, (tmsize_t
)(dircount16
*dirsize
))) {
4505 TIFFErrorExt(tif
->tif_clientdata
, module,
4506 "%.100s: Can not read TIFF directory",
4512 * Read offset to next directory for sequential scans if
4517 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4519 uint32 nextdiroff32
;
4520 if (!ReadOK(tif
, &nextdiroff32
, sizeof(uint32
)))
4522 if (tif
->tif_flags
&TIFF_SWAB
)
4523 TIFFSwabLong(&nextdiroff32
);
4524 *nextdiroff
=nextdiroff32
;
4526 if (!ReadOK(tif
, nextdiroff
, sizeof(uint64
)))
4528 if (tif
->tif_flags
&TIFF_SWAB
)
4529 TIFFSwabLong8(nextdiroff
);
4534 tmsize_t off
= (tmsize_t
) tif
->tif_diroff
;
4535 if ((uint64
)off
!=tif
->tif_diroff
)
4537 TIFFErrorExt(tif
->tif_clientdata
,module,"Can not read TIFF directory count");
4542 * Check for integer overflow when validating the dir_off,
4543 * otherwise a very high offset may cause an OOB read and
4544 * crash the client. Make two comparisons instead of
4546 * off + sizeof(uint16) > tif->tif_size
4548 * to avoid overflow.
4550 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4552 m
=off
+sizeof(uint16
);
4553 if ((m
<off
)||(m
<(tmsize_t
)sizeof(uint16
))||(m
>tif
->tif_size
)) {
4554 TIFFErrorExt(tif
->tif_clientdata
, module,
4555 "Can not read TIFF directory count");
4558 _TIFFmemcpy(&dircount16
, tif
->tif_base
+ off
,
4561 off
+= sizeof (uint16
);
4562 if (tif
->tif_flags
& TIFF_SWAB
)
4563 TIFFSwabShort(&dircount16
);
4564 if (dircount16
>4096)
4566 TIFFErrorExt(tif
->tif_clientdata
, module,
4567 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4576 m
=off
+sizeof(uint64
);
4577 if ((m
<off
)||(m
<(tmsize_t
)sizeof(uint64
))||(m
>tif
->tif_size
)) {
4578 TIFFErrorExt(tif
->tif_clientdata
, module,
4579 "Can not read TIFF directory count");
4582 _TIFFmemcpy(&dircount64
, tif
->tif_base
+ off
,
4585 off
+= sizeof (uint64
);
4586 if (tif
->tif_flags
& TIFF_SWAB
)
4587 TIFFSwabLong8(&dircount64
);
4588 if (dircount64
>4096)
4590 TIFFErrorExt(tif
->tif_clientdata
, module,
4591 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4594 dircount16
= (uint16
)dircount64
;
4597 if (dircount16
== 0 )
4599 TIFFErrorExt(tif
->tif_clientdata
, module,
4600 "Sanity check on directory count failed, zero tag directories not supported");
4603 origdir
= _TIFFCheckMalloc(tif
, dircount16
,
4605 "to read TIFF directory");
4606 if (origdir
== NULL
)
4608 m
=off
+dircount16
*dirsize
;
4609 if ((m
<off
)||(m
<(tmsize_t
)(dircount16
*dirsize
))||(m
>tif
->tif_size
)) {
4610 TIFFErrorExt(tif
->tif_clientdata
, module,
4611 "Can not read TIFF directory");
4615 _TIFFmemcpy(origdir
, tif
->tif_base
+ off
,
4616 dircount16
* dirsize
);
4619 off
+= dircount16
* dirsize
;
4620 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4622 uint32 nextdiroff32
;
4623 m
=off
+sizeof(uint32
);
4624 if ((m
<off
)||(m
<(tmsize_t
)sizeof(uint32
))||(m
>tif
->tif_size
))
4627 _TIFFmemcpy(&nextdiroff32
, tif
->tif_base
+ off
,
4629 if (tif
->tif_flags
&TIFF_SWAB
)
4630 TIFFSwabLong(&nextdiroff32
);
4631 *nextdiroff
= nextdiroff32
;
4635 m
=off
+sizeof(uint64
);
4636 if ((m
<off
)||(m
<(tmsize_t
)sizeof(uint64
))||(m
>tif
->tif_size
))
4639 _TIFFmemcpy(nextdiroff
, tif
->tif_base
+ off
,
4641 if (tif
->tif_flags
&TIFF_SWAB
)
4642 TIFFSwabLong8(nextdiroff
);
4646 dir
= (TIFFDirEntry
*)_TIFFCheckMalloc(tif
, dircount16
,
4647 sizeof(TIFFDirEntry
),
4648 "to read TIFF directory");
4656 for (n
=0; n
<dircount16
; n
++)
4658 if (tif
->tif_flags
&TIFF_SWAB
)
4659 TIFFSwabShort((uint16
*)ma
);
4660 mb
->tdir_tag
=*(uint16
*)ma
;
4662 if (tif
->tif_flags
&TIFF_SWAB
)
4663 TIFFSwabShort((uint16
*)ma
);
4664 mb
->tdir_type
=*(uint16
*)ma
;
4666 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
4668 if (tif
->tif_flags
&TIFF_SWAB
)
4669 TIFFSwabLong((uint32
*)ma
);
4670 mb
->tdir_count
=(uint64
)(*(uint32
*)ma
);
4672 *(uint32
*)(&mb
->tdir_offset
)=*(uint32
*)ma
;
4677 if (tif
->tif_flags
&TIFF_SWAB
)
4678 TIFFSwabLong8((uint64
*)ma
);
4679 mb
->tdir_count
=TIFFReadUInt64(ma
);
4681 mb
->tdir_offset
.toff_long8
=TIFFReadUInt64(ma
);
4692 * Fetch a tag that is not handled by special case code.
4695 TIFFFetchNormalTag(TIFF
* tif
, TIFFDirEntry
* dp
, int recover
)
4697 static const char module[] = "TIFFFetchNormalTag";
4698 enum TIFFReadDirEntryErr err
;
4700 const TIFFField
* fip
= NULL
;
4701 TIFFReadDirectoryFindFieldInfo(tif
,dp
->tdir_tag
,&fii
);
4702 if( fii
== FAILED_FII
)
4704 TIFFErrorExt(tif
->tif_clientdata
, "TIFFFetchNormalTag",
4705 "No definition found for tag %d",
4709 fip
=tif
->tif_fields
[fii
];
4710 assert(fip
->set_field_type
!=TIFF_SETGET_OTHER
); /* if so, we shouldn't arrive here but deal with this in specialized code */
4711 assert(fip
->set_field_type
!=TIFF_SETGET_INT
); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
4712 err
=TIFFReadDirEntryErrOk
;
4713 switch (fip
->set_field_type
)
4715 case TIFF_SETGET_UNDEFINED
:
4717 case TIFF_SETGET_ASCII
:
4720 assert(fip
->field_passcount
==0);
4721 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
4722 if (err
==TIFFReadDirEntryErrOk
)
4729 while (mb
<(uint32
)dp
->tdir_count
)
4736 if (mb
+1<(uint32
)dp
->tdir_count
)
4737 TIFFWarningExt(tif
->tif_clientdata
,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip
->field_name
);
4738 else if (mb
+1>(uint32
)dp
->tdir_count
)
4741 TIFFWarningExt(tif
->tif_clientdata
,module,"ASCII value for tag \"%s\" does not end in null byte",fip
->field_name
);
4742 if ((uint32
)dp
->tdir_count
+1!=dp
->tdir_count
+1)
4745 o
=_TIFFmalloc((uint32
)dp
->tdir_count
+1);
4752 _TIFFmemcpy(o
,data
,(uint32
)dp
->tdir_count
);
4753 o
[(uint32
)dp
->tdir_count
]=0;
4758 n
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
4766 case TIFF_SETGET_UINT8
:
4769 assert(fip
->field_readcount
==1);
4770 assert(fip
->field_passcount
==0);
4771 err
=TIFFReadDirEntryByte(tif
,dp
,&data
);
4772 if (err
==TIFFReadDirEntryErrOk
)
4774 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4779 case TIFF_SETGET_UINT16
:
4782 assert(fip
->field_readcount
==1);
4783 assert(fip
->field_passcount
==0);
4784 err
=TIFFReadDirEntryShort(tif
,dp
,&data
);
4785 if (err
==TIFFReadDirEntryErrOk
)
4787 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4792 case TIFF_SETGET_UINT32
:
4795 assert(fip
->field_readcount
==1);
4796 assert(fip
->field_passcount
==0);
4797 err
=TIFFReadDirEntryLong(tif
,dp
,&data
);
4798 if (err
==TIFFReadDirEntryErrOk
)
4800 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4805 case TIFF_SETGET_UINT64
:
4808 assert(fip
->field_readcount
==1);
4809 assert(fip
->field_passcount
==0);
4810 err
=TIFFReadDirEntryLong8(tif
,dp
,&data
);
4811 if (err
==TIFFReadDirEntryErrOk
)
4813 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4818 case TIFF_SETGET_FLOAT
:
4821 assert(fip
->field_readcount
==1);
4822 assert(fip
->field_passcount
==0);
4823 err
=TIFFReadDirEntryFloat(tif
,dp
,&data
);
4824 if (err
==TIFFReadDirEntryErrOk
)
4826 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4831 case TIFF_SETGET_DOUBLE
:
4834 assert(fip
->field_readcount
==1);
4835 assert(fip
->field_passcount
==0);
4836 err
=TIFFReadDirEntryDouble(tif
,dp
,&data
);
4837 if (err
==TIFFReadDirEntryErrOk
)
4839 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4844 case TIFF_SETGET_IFD8
:
4847 assert(fip
->field_readcount
==1);
4848 assert(fip
->field_passcount
==0);
4849 err
=TIFFReadDirEntryIfd8(tif
,dp
,&data
);
4850 if (err
==TIFFReadDirEntryErrOk
)
4852 if (!TIFFSetField(tif
,dp
->tdir_tag
,data
))
4857 case TIFF_SETGET_UINT16_PAIR
:
4860 assert(fip
->field_readcount
==2);
4861 assert(fip
->field_passcount
==0);
4862 if (dp
->tdir_count
!=2) {
4863 TIFFWarningExt(tif
->tif_clientdata
,module,
4864 "incorrect count for field \"%s\", expected 2, got %d",
4865 fip
->field_name
,(int)dp
->tdir_count
);
4868 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
4869 if (err
==TIFFReadDirEntryErrOk
)
4872 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
[0],data
[1]);
4879 case TIFF_SETGET_C0_UINT8
:
4882 assert(fip
->field_readcount
>=1);
4883 assert(fip
->field_passcount
==0);
4884 if (dp
->tdir_count
!=(uint64
)fip
->field_readcount
) {
4885 TIFFWarningExt(tif
->tif_clientdata
,module,
4886 "incorrect count for field \"%s\", expected %d, got %d",
4887 fip
->field_name
,(int) fip
->field_readcount
, (int)dp
->tdir_count
);
4892 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
4893 if (err
==TIFFReadDirEntryErrOk
)
4896 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
4905 case TIFF_SETGET_C0_UINT16
:
4908 assert(fip
->field_readcount
>=1);
4909 assert(fip
->field_passcount
==0);
4910 if (dp
->tdir_count
!=(uint64
)fip
->field_readcount
)
4914 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
4915 if (err
==TIFFReadDirEntryErrOk
)
4918 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
4927 case TIFF_SETGET_C0_UINT32
:
4930 assert(fip
->field_readcount
>=1);
4931 assert(fip
->field_passcount
==0);
4932 if (dp
->tdir_count
!=(uint64
)fip
->field_readcount
)
4936 err
=TIFFReadDirEntryLongArray(tif
,dp
,&data
);
4937 if (err
==TIFFReadDirEntryErrOk
)
4940 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
4949 case TIFF_SETGET_C0_FLOAT
:
4952 assert(fip
->field_readcount
>=1);
4953 assert(fip
->field_passcount
==0);
4954 if (dp
->tdir_count
!=(uint64
)fip
->field_readcount
)
4958 err
=TIFFReadDirEntryFloatArray(tif
,dp
,&data
);
4959 if (err
==TIFFReadDirEntryErrOk
)
4962 m
=TIFFSetField(tif
,dp
->tdir_tag
,data
);
4971 case TIFF_SETGET_C16_ASCII
:
4974 assert(fip
->field_readcount
==TIFF_VARIABLE
);
4975 assert(fip
->field_passcount
==1);
4976 if (dp
->tdir_count
>0xFFFF)
4977 err
=TIFFReadDirEntryErrCount
;
4980 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
4981 if (err
==TIFFReadDirEntryErrOk
)
4984 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
4993 case TIFF_SETGET_C16_UINT8
:
4996 assert(fip
->field_readcount
==TIFF_VARIABLE
);
4997 assert(fip
->field_passcount
==1);
4998 if (dp
->tdir_count
>0xFFFF)
4999 err
=TIFFReadDirEntryErrCount
;
5002 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5003 if (err
==TIFFReadDirEntryErrOk
)
5006 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5015 case TIFF_SETGET_C16_UINT16
:
5018 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5019 assert(fip
->field_passcount
==1);
5020 if (dp
->tdir_count
>0xFFFF)
5021 err
=TIFFReadDirEntryErrCount
;
5024 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
5025 if (err
==TIFFReadDirEntryErrOk
)
5028 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5037 case TIFF_SETGET_C16_UINT32
:
5040 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5041 assert(fip
->field_passcount
==1);
5042 if (dp
->tdir_count
>0xFFFF)
5043 err
=TIFFReadDirEntryErrCount
;
5046 err
=TIFFReadDirEntryLongArray(tif
,dp
,&data
);
5047 if (err
==TIFFReadDirEntryErrOk
)
5050 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5059 case TIFF_SETGET_C16_UINT64
:
5062 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5063 assert(fip
->field_passcount
==1);
5064 if (dp
->tdir_count
>0xFFFF)
5065 err
=TIFFReadDirEntryErrCount
;
5068 err
=TIFFReadDirEntryLong8Array(tif
,dp
,&data
);
5069 if (err
==TIFFReadDirEntryErrOk
)
5072 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5081 case TIFF_SETGET_C16_FLOAT
:
5084 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5085 assert(fip
->field_passcount
==1);
5086 if (dp
->tdir_count
>0xFFFF)
5087 err
=TIFFReadDirEntryErrCount
;
5090 err
=TIFFReadDirEntryFloatArray(tif
,dp
,&data
);
5091 if (err
==TIFFReadDirEntryErrOk
)
5094 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5103 case TIFF_SETGET_C16_DOUBLE
:
5106 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5107 assert(fip
->field_passcount
==1);
5108 if (dp
->tdir_count
>0xFFFF)
5109 err
=TIFFReadDirEntryErrCount
;
5112 err
=TIFFReadDirEntryDoubleArray(tif
,dp
,&data
);
5113 if (err
==TIFFReadDirEntryErrOk
)
5116 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5125 case TIFF_SETGET_C16_IFD8
:
5128 assert(fip
->field_readcount
==TIFF_VARIABLE
);
5129 assert(fip
->field_passcount
==1);
5130 if (dp
->tdir_count
>0xFFFF)
5131 err
=TIFFReadDirEntryErrCount
;
5134 err
=TIFFReadDirEntryIfd8Array(tif
,dp
,&data
);
5135 if (err
==TIFFReadDirEntryErrOk
)
5138 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint16
)(dp
->tdir_count
),data
);
5147 case TIFF_SETGET_C32_ASCII
:
5150 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5151 assert(fip
->field_passcount
==1);
5152 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5153 if (err
==TIFFReadDirEntryErrOk
)
5156 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5164 case TIFF_SETGET_C32_UINT8
:
5167 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5168 assert(fip
->field_passcount
==1);
5169 err
=TIFFReadDirEntryByteArray(tif
,dp
,&data
);
5170 if (err
==TIFFReadDirEntryErrOk
)
5173 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5181 case TIFF_SETGET_C32_SINT8
:
5184 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5185 assert(fip
->field_passcount
==1);
5186 err
=TIFFReadDirEntrySbyteArray(tif
,dp
,&data
);
5187 if (err
==TIFFReadDirEntryErrOk
)
5190 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5198 case TIFF_SETGET_C32_UINT16
:
5201 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5202 assert(fip
->field_passcount
==1);
5203 err
=TIFFReadDirEntryShortArray(tif
,dp
,&data
);
5204 if (err
==TIFFReadDirEntryErrOk
)
5207 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5215 case TIFF_SETGET_C32_SINT16
:
5218 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5219 assert(fip
->field_passcount
==1);
5220 err
=TIFFReadDirEntrySshortArray(tif
,dp
,&data
);
5221 if (err
==TIFFReadDirEntryErrOk
)
5224 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5232 case TIFF_SETGET_C32_UINT32
:
5235 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5236 assert(fip
->field_passcount
==1);
5237 err
=TIFFReadDirEntryLongArray(tif
,dp
,&data
);
5238 if (err
==TIFFReadDirEntryErrOk
)
5241 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5249 case TIFF_SETGET_C32_SINT32
:
5252 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5253 assert(fip
->field_passcount
==1);
5254 err
=TIFFReadDirEntrySlongArray(tif
,dp
,&data
);
5255 if (err
==TIFFReadDirEntryErrOk
)
5258 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5266 case TIFF_SETGET_C32_UINT64
:
5269 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5270 assert(fip
->field_passcount
==1);
5271 err
=TIFFReadDirEntryLong8Array(tif
,dp
,&data
);
5272 if (err
==TIFFReadDirEntryErrOk
)
5275 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5283 case TIFF_SETGET_C32_SINT64
:
5286 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5287 assert(fip
->field_passcount
==1);
5288 err
=TIFFReadDirEntrySlong8Array(tif
,dp
,&data
);
5289 if (err
==TIFFReadDirEntryErrOk
)
5292 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5300 case TIFF_SETGET_C32_FLOAT
:
5303 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5304 assert(fip
->field_passcount
==1);
5305 err
=TIFFReadDirEntryFloatArray(tif
,dp
,&data
);
5306 if (err
==TIFFReadDirEntryErrOk
)
5309 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5317 case TIFF_SETGET_C32_DOUBLE
:
5320 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5321 assert(fip
->field_passcount
==1);
5322 err
=TIFFReadDirEntryDoubleArray(tif
,dp
,&data
);
5323 if (err
==TIFFReadDirEntryErrOk
)
5326 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5334 case TIFF_SETGET_C32_IFD8
:
5337 assert(fip
->field_readcount
==TIFF_VARIABLE2
);
5338 assert(fip
->field_passcount
==1);
5339 err
=TIFFReadDirEntryIfd8Array(tif
,dp
,&data
);
5340 if (err
==TIFFReadDirEntryErrOk
)
5343 m
=TIFFSetField(tif
,dp
->tdir_tag
,(uint32
)(dp
->tdir_count
),data
);
5352 assert(0); /* we should never get here */
5355 if (err
!=TIFFReadDirEntryErrOk
)
5357 TIFFReadDirEntryOutputErr(tif
,err
,module,fip
? fip
->field_name
: "unknown tagname",recover
);
5364 * Fetch a set of offsets or lengths.
5365 * While this routine says "strips", in fact it's also used for tiles.
5368 TIFFFetchStripThing(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 nstrips
, uint64
** lpp
)
5370 static const char module[] = "TIFFFetchStripThing";
5371 enum TIFFReadDirEntryErr err
;
5373 err
=TIFFReadDirEntryLong8Array(tif
,dir
,&data
);
5374 if (err
!=TIFFReadDirEntryErrOk
)
5376 const TIFFField
* fip
= TIFFFieldWithTag(tif
,dir
->tdir_tag
);
5377 TIFFReadDirEntryOutputErr(tif
,err
,module,fip
? fip
->field_name
: "unknown tagname",0);
5380 if (dir
->tdir_count
!=(uint64
)nstrips
)
5382 uint64
* resizeddata
;
5383 resizeddata
=(uint64
*)_TIFFCheckMalloc(tif
,nstrips
,sizeof(uint64
),"for strip array");
5384 if (resizeddata
==0) {
5388 if (dir
->tdir_count
<(uint64
)nstrips
)
5390 _TIFFmemcpy(resizeddata
,data
,(uint32
)dir
->tdir_count
*sizeof(uint64
));
5391 _TIFFmemset(resizeddata
+(uint32
)dir
->tdir_count
,0,(nstrips
-(uint32
)dir
->tdir_count
)*sizeof(uint64
));
5394 _TIFFmemcpy(resizeddata
,data
,nstrips
*sizeof(uint64
));
5403 * Fetch and set the SubjectDistance EXIF tag.
5406 TIFFFetchSubjectDistance(TIFF
* tif
, TIFFDirEntry
* dir
)
5408 static const char module[] = "TIFFFetchSubjectDistance";
5409 enum TIFFReadDirEntryErr err
;
5412 assert(sizeof(double)==8);
5413 assert(sizeof(uint64
)==8);
5414 assert(sizeof(uint32
)==4);
5415 if (dir
->tdir_count
!=1)
5416 err
=TIFFReadDirEntryErrCount
;
5417 else if (dir
->tdir_type
!=TIFF_RATIONAL
)
5418 err
=TIFFReadDirEntryErrType
;
5421 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
5424 offset
=*(uint32
*)(&dir
->tdir_offset
);
5425 if (tif
->tif_flags
&TIFF_SWAB
)
5426 TIFFSwabLong(&offset
);
5427 err
=TIFFReadDirEntryData(tif
,offset
,8,m
.i
);
5431 m
.l
=dir
->tdir_offset
.toff_long8
;
5432 err
=TIFFReadDirEntryErrOk
;
5435 if (err
==TIFFReadDirEntryErrOk
)
5438 if (tif
->tif_flags
&TIFF_SWAB
)
5439 TIFFSwabArrayOfLong(m
.i
,2);
5442 else if (m
.i
[0]==0xFFFFFFFF)
5444 * XXX: Numerator 0xFFFFFFFF means that we have infinite
5445 * distance. Indicate that with a negative floating point
5446 * SubjectDistance value.
5450 n
=(double)m
.i
[0]/(double)m
.i
[1];
5451 return(TIFFSetField(tif
,dir
->tdir_tag
,n
));
5455 TIFFReadDirEntryOutputErr(tif
,err
,module,"SubjectDistance",TRUE
);
5461 * Replace a single strip (tile) of uncompressed data by multiple strips
5462 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5463 * dealing with large images or for dealing with machines with a limited
5467 ChopUpSingleUncompressedStrip(TIFF
* tif
)
5469 register TIFFDirectory
*td
= &tif
->tif_dir
;
5473 uint64 rowblockbytes
;
5478 uint32 rowsperstrip
;
5482 bytecount
= td
->td_stripbytecount
[0];
5483 offset
= td
->td_stripoffset
[0];
5484 assert(td
->td_planarconfig
== PLANARCONFIG_CONTIG
);
5485 if ((td
->td_photometric
== PHOTOMETRIC_YCBCR
)&&
5486 (!isUpSampled(tif
)))
5487 rowblock
= td
->td_ycbcrsubsampling
[1];
5490 rowblockbytes
= TIFFVTileSize64(tif
, rowblock
);
5492 * Make the rows hold at least one scanline, but fill specified amount
5493 * of data if possible.
5495 if (rowblockbytes
> STRIP_SIZE_DEFAULT
) {
5496 stripbytes
= rowblockbytes
;
5497 rowsperstrip
= rowblock
;
5498 } else if (rowblockbytes
> 0 ) {
5499 uint32 rowblocksperstrip
;
5500 rowblocksperstrip
= (uint32
) (STRIP_SIZE_DEFAULT
/ rowblockbytes
);
5501 rowsperstrip
= rowblocksperstrip
* rowblock
;
5502 stripbytes
= rowblocksperstrip
* rowblockbytes
;
5508 * never increase the number of strips in an image
5510 if (rowsperstrip
>= td
->td_rowsperstrip
)
5512 nstrips64
= TIFFhowmany_64(bytecount
, stripbytes
);
5513 if ((nstrips64
==0)||(nstrips64
>0xFFFFFFFF)) /* something is wonky, do nothing. */
5515 nstrips32
= (uint32
)nstrips64
;
5517 newcounts
= (uint64
*) _TIFFCheckMalloc(tif
, nstrips32
, sizeof (uint64
),
5518 "for chopped \"StripByteCounts\" array");
5519 newoffsets
= (uint64
*) _TIFFCheckMalloc(tif
, nstrips32
, sizeof (uint64
),
5520 "for chopped \"StripOffsets\" array");
5521 if (newcounts
== NULL
|| newoffsets
== NULL
) {
5523 * Unable to allocate new strip information, give up and use
5524 * the original one strip information.
5526 if (newcounts
!= NULL
)
5527 _TIFFfree(newcounts
);
5528 if (newoffsets
!= NULL
)
5529 _TIFFfree(newoffsets
);
5533 * Fill the strip information arrays with new bytecounts and offsets
5534 * that reflect the broken-up format.
5536 for (strip
= 0; strip
< nstrips32
; strip
++) {
5537 if (stripbytes
> bytecount
)
5538 stripbytes
= bytecount
;
5539 newcounts
[strip
] = stripbytes
;
5540 newoffsets
[strip
] = offset
;
5541 offset
+= stripbytes
;
5542 bytecount
-= stripbytes
;
5545 * Replace old single strip info with multi-strip info.
5547 td
->td_stripsperimage
= td
->td_nstrips
= nstrips32
;
5548 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rowsperstrip
);
5550 _TIFFfree(td
->td_stripbytecount
);
5551 _TIFFfree(td
->td_stripoffset
);
5552 td
->td_stripbytecount
= newcounts
;
5553 td
->td_stripoffset
= newoffsets
;
5554 td
->td_stripbytecountsorted
= 1;
5557 int _TIFFFillStriles( TIFF
*tif
)
5559 #if defined(DEFER_STRILE_LOAD)
5560 register TIFFDirectory
*td
= &tif
->tif_dir
;
5561 int return_value
= 1;
5563 if( td
->td_stripoffset
!= NULL
)
5566 if( td
->td_stripoffset_entry
.tdir_count
== 0 )
5569 if (!TIFFFetchStripThing(tif
,&(td
->td_stripoffset_entry
),
5570 td
->td_nstrips
,&td
->td_stripoffset
))
5575 if (!TIFFFetchStripThing(tif
,&(td
->td_stripbytecount_entry
),
5576 td
->td_nstrips
,&td
->td_stripbytecount
))
5581 _TIFFmemset( &(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
5582 _TIFFmemset( &(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
5584 if (tif
->tif_dir
.td_nstrips
> 1 && return_value
== 1 ) {
5587 tif
->tif_dir
.td_stripbytecountsorted
= 1;
5588 for (strip
= 1; strip
< tif
->tif_dir
.td_nstrips
; strip
++) {
5589 if (tif
->tif_dir
.td_stripoffset
[strip
- 1] >
5590 tif
->tif_dir
.td_stripoffset
[strip
]) {
5591 tif
->tif_dir
.td_stripbytecountsorted
= 0;
5597 return return_value
;
5598 #else /* !defined(DEFER_STRILE_LOAD) */
5605 /* vim: set ts=8 sts=8 sw=8 noet: */