9c2685217593cb2da65c25f315bef5541fa55476
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_dirread.c
1 /* $Id: tif_dirread.c,v 1.204 2016-11-16 15:14:15 erouault Exp $ */
2
3 /*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library.
29 *
30 * Directory Read Support Routines.
31 */
32
33 /* Suggested pending improvements:
34 * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
35 * eliminating current use of the IGNORE value, and therefore eliminating
36 * current irrational behaviour on tags with tag id code 0
37 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
38 * the pointer to the appropriate TIFFField structure early on in
39 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
40 */
41
42 #include <precomp.h>
43
44 #define IGNORE 0 /* tag placeholder used below */
45 #define FAILED_FII ((uint32) -1)
46
47 #ifdef HAVE_IEEEFP
48 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
49 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
50 #else
51 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
52 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
53 #endif
54
55 enum TIFFReadDirEntryErr {
56 TIFFReadDirEntryErrOk = 0,
57 TIFFReadDirEntryErrCount = 1,
58 TIFFReadDirEntryErrType = 2,
59 TIFFReadDirEntryErrIo = 3,
60 TIFFReadDirEntryErrRange = 4,
61 TIFFReadDirEntryErrPsdif = 5,
62 TIFFReadDirEntryErrSizesan = 6,
63 TIFFReadDirEntryErrAlloc = 7,
64 };
65
66 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
67 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
68 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
69 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
70 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
71 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
72 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
73
74 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
75 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
76 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
77 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
78 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
79 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
80 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
81 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
82 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
83 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
84 static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
85 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
86
87 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
88 #if 0
89 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
90 #endif
91
92 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
93 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
94 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
95 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
96 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
97 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
98 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
99 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
100 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
101 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
102 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
103 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
104
105 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
106 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
107 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
108 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
109 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
110 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
111 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
112
113 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
114 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
115 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
116 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
117 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
118 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
119 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
120
121 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
122 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
123 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
124 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
125 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
126 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
127
128 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
129 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
130 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
131 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
132 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
133
134 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
135 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
136 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
137 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
138 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
139
140 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
141 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
142 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
143
144 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
145 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
146 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
147 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
148
149 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
150
151 static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
152 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
153
154 static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
155 static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
156 static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
157
158 static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
159 static void MissingRequired(TIFF*, const char*);
160 static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
161 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
162 static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
163 static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
164 static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
165 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
166 static void ChopUpSingleUncompressedStrip(TIFF*);
167 static uint64 TIFFReadUInt64(const uint8 *value);
168
169 static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
170
171 typedef union _UInt64Aligned_t
172 {
173 double d;
174 uint64 l;
175 uint32 i[2];
176 uint16 s[4];
177 uint8 c[8];
178 } UInt64Aligned_t;
179
180 /*
181 Unaligned safe copy of a uint64 value from an octet array.
182 */
183 static uint64 TIFFReadUInt64(const uint8 *value)
184 {
185 UInt64Aligned_t result;
186
187 result.c[0]=value[0];
188 result.c[1]=value[1];
189 result.c[2]=value[2];
190 result.c[3]=value[3];
191 result.c[4]=value[4];
192 result.c[5]=value[5];
193 result.c[6]=value[6];
194 result.c[7]=value[7];
195
196 return result.l;
197 }
198
199 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
200 {
201 enum TIFFReadDirEntryErr err;
202 if (direntry->tdir_count!=1)
203 return(TIFFReadDirEntryErrCount);
204 switch (direntry->tdir_type)
205 {
206 case TIFF_BYTE:
207 TIFFReadDirEntryCheckedByte(tif,direntry,value);
208 return(TIFFReadDirEntryErrOk);
209 case TIFF_SBYTE:
210 {
211 int8 m;
212 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
213 err=TIFFReadDirEntryCheckRangeByteSbyte(m);
214 if (err!=TIFFReadDirEntryErrOk)
215 return(err);
216 *value=(uint8)m;
217 return(TIFFReadDirEntryErrOk);
218 }
219 case TIFF_SHORT:
220 {
221 uint16 m;
222 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
223 err=TIFFReadDirEntryCheckRangeByteShort(m);
224 if (err!=TIFFReadDirEntryErrOk)
225 return(err);
226 *value=(uint8)m;
227 return(TIFFReadDirEntryErrOk);
228 }
229 case TIFF_SSHORT:
230 {
231 int16 m;
232 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
233 err=TIFFReadDirEntryCheckRangeByteSshort(m);
234 if (err!=TIFFReadDirEntryErrOk)
235 return(err);
236 *value=(uint8)m;
237 return(TIFFReadDirEntryErrOk);
238 }
239 case TIFF_LONG:
240 {
241 uint32 m;
242 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
243 err=TIFFReadDirEntryCheckRangeByteLong(m);
244 if (err!=TIFFReadDirEntryErrOk)
245 return(err);
246 *value=(uint8)m;
247 return(TIFFReadDirEntryErrOk);
248 }
249 case TIFF_SLONG:
250 {
251 int32 m;
252 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
253 err=TIFFReadDirEntryCheckRangeByteSlong(m);
254 if (err!=TIFFReadDirEntryErrOk)
255 return(err);
256 *value=(uint8)m;
257 return(TIFFReadDirEntryErrOk);
258 }
259 case TIFF_LONG8:
260 {
261 uint64 m;
262 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
263 if (err!=TIFFReadDirEntryErrOk)
264 return(err);
265 err=TIFFReadDirEntryCheckRangeByteLong8(m);
266 if (err!=TIFFReadDirEntryErrOk)
267 return(err);
268 *value=(uint8)m;
269 return(TIFFReadDirEntryErrOk);
270 }
271 case TIFF_SLONG8:
272 {
273 int64 m;
274 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
275 if (err!=TIFFReadDirEntryErrOk)
276 return(err);
277 err=TIFFReadDirEntryCheckRangeByteSlong8(m);
278 if (err!=TIFFReadDirEntryErrOk)
279 return(err);
280 *value=(uint8)m;
281 return(TIFFReadDirEntryErrOk);
282 }
283 default:
284 return(TIFFReadDirEntryErrType);
285 }
286 }
287
288 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
289 {
290 enum TIFFReadDirEntryErr err;
291 if (direntry->tdir_count!=1)
292 return(TIFFReadDirEntryErrCount);
293 switch (direntry->tdir_type)
294 {
295 case TIFF_BYTE:
296 {
297 uint8 m;
298 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
299 *value=(uint16)m;
300 return(TIFFReadDirEntryErrOk);
301 }
302 case TIFF_SBYTE:
303 {
304 int8 m;
305 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
306 err=TIFFReadDirEntryCheckRangeShortSbyte(m);
307 if (err!=TIFFReadDirEntryErrOk)
308 return(err);
309 *value=(uint16)m;
310 return(TIFFReadDirEntryErrOk);
311 }
312 case TIFF_SHORT:
313 TIFFReadDirEntryCheckedShort(tif,direntry,value);
314 return(TIFFReadDirEntryErrOk);
315 case TIFF_SSHORT:
316 {
317 int16 m;
318 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
319 err=TIFFReadDirEntryCheckRangeShortSshort(m);
320 if (err!=TIFFReadDirEntryErrOk)
321 return(err);
322 *value=(uint16)m;
323 return(TIFFReadDirEntryErrOk);
324 }
325 case TIFF_LONG:
326 {
327 uint32 m;
328 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
329 err=TIFFReadDirEntryCheckRangeShortLong(m);
330 if (err!=TIFFReadDirEntryErrOk)
331 return(err);
332 *value=(uint16)m;
333 return(TIFFReadDirEntryErrOk);
334 }
335 case TIFF_SLONG:
336 {
337 int32 m;
338 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
339 err=TIFFReadDirEntryCheckRangeShortSlong(m);
340 if (err!=TIFFReadDirEntryErrOk)
341 return(err);
342 *value=(uint16)m;
343 return(TIFFReadDirEntryErrOk);
344 }
345 case TIFF_LONG8:
346 {
347 uint64 m;
348 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
349 if (err!=TIFFReadDirEntryErrOk)
350 return(err);
351 err=TIFFReadDirEntryCheckRangeShortLong8(m);
352 if (err!=TIFFReadDirEntryErrOk)
353 return(err);
354 *value=(uint16)m;
355 return(TIFFReadDirEntryErrOk);
356 }
357 case TIFF_SLONG8:
358 {
359 int64 m;
360 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
361 if (err!=TIFFReadDirEntryErrOk)
362 return(err);
363 err=TIFFReadDirEntryCheckRangeShortSlong8(m);
364 if (err!=TIFFReadDirEntryErrOk)
365 return(err);
366 *value=(uint16)m;
367 return(TIFFReadDirEntryErrOk);
368 }
369 default:
370 return(TIFFReadDirEntryErrType);
371 }
372 }
373
374 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
375 {
376 enum TIFFReadDirEntryErr err;
377 if (direntry->tdir_count!=1)
378 return(TIFFReadDirEntryErrCount);
379 switch (direntry->tdir_type)
380 {
381 case TIFF_BYTE:
382 {
383 uint8 m;
384 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
385 *value=(uint32)m;
386 return(TIFFReadDirEntryErrOk);
387 }
388 case TIFF_SBYTE:
389 {
390 int8 m;
391 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
392 err=TIFFReadDirEntryCheckRangeLongSbyte(m);
393 if (err!=TIFFReadDirEntryErrOk)
394 return(err);
395 *value=(uint32)m;
396 return(TIFFReadDirEntryErrOk);
397 }
398 case TIFF_SHORT:
399 {
400 uint16 m;
401 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
402 *value=(uint32)m;
403 return(TIFFReadDirEntryErrOk);
404 }
405 case TIFF_SSHORT:
406 {
407 int16 m;
408 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
409 err=TIFFReadDirEntryCheckRangeLongSshort(m);
410 if (err!=TIFFReadDirEntryErrOk)
411 return(err);
412 *value=(uint32)m;
413 return(TIFFReadDirEntryErrOk);
414 }
415 case TIFF_LONG:
416 TIFFReadDirEntryCheckedLong(tif,direntry,value);
417 return(TIFFReadDirEntryErrOk);
418 case TIFF_SLONG:
419 {
420 int32 m;
421 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
422 err=TIFFReadDirEntryCheckRangeLongSlong(m);
423 if (err!=TIFFReadDirEntryErrOk)
424 return(err);
425 *value=(uint32)m;
426 return(TIFFReadDirEntryErrOk);
427 }
428 case TIFF_LONG8:
429 {
430 uint64 m;
431 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
432 if (err!=TIFFReadDirEntryErrOk)
433 return(err);
434 err=TIFFReadDirEntryCheckRangeLongLong8(m);
435 if (err!=TIFFReadDirEntryErrOk)
436 return(err);
437 *value=(uint32)m;
438 return(TIFFReadDirEntryErrOk);
439 }
440 case TIFF_SLONG8:
441 {
442 int64 m;
443 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
444 if (err!=TIFFReadDirEntryErrOk)
445 return(err);
446 err=TIFFReadDirEntryCheckRangeLongSlong8(m);
447 if (err!=TIFFReadDirEntryErrOk)
448 return(err);
449 *value=(uint32)m;
450 return(TIFFReadDirEntryErrOk);
451 }
452 default:
453 return(TIFFReadDirEntryErrType);
454 }
455 }
456
457 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
458 {
459 enum TIFFReadDirEntryErr err;
460 if (direntry->tdir_count!=1)
461 return(TIFFReadDirEntryErrCount);
462 switch (direntry->tdir_type)
463 {
464 case TIFF_BYTE:
465 {
466 uint8 m;
467 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
468 *value=(uint64)m;
469 return(TIFFReadDirEntryErrOk);
470 }
471 case TIFF_SBYTE:
472 {
473 int8 m;
474 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
475 err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
476 if (err!=TIFFReadDirEntryErrOk)
477 return(err);
478 *value=(uint64)m;
479 return(TIFFReadDirEntryErrOk);
480 }
481 case TIFF_SHORT:
482 {
483 uint16 m;
484 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
485 *value=(uint64)m;
486 return(TIFFReadDirEntryErrOk);
487 }
488 case TIFF_SSHORT:
489 {
490 int16 m;
491 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
492 err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
493 if (err!=TIFFReadDirEntryErrOk)
494 return(err);
495 *value=(uint64)m;
496 return(TIFFReadDirEntryErrOk);
497 }
498 case TIFF_LONG:
499 {
500 uint32 m;
501 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
502 *value=(uint64)m;
503 return(TIFFReadDirEntryErrOk);
504 }
505 case TIFF_SLONG:
506 {
507 int32 m;
508 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
509 err=TIFFReadDirEntryCheckRangeLong8Slong(m);
510 if (err!=TIFFReadDirEntryErrOk)
511 return(err);
512 *value=(uint64)m;
513 return(TIFFReadDirEntryErrOk);
514 }
515 case TIFF_LONG8:
516 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
517 return(err);
518 case TIFF_SLONG8:
519 {
520 int64 m;
521 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
522 if (err!=TIFFReadDirEntryErrOk)
523 return(err);
524 err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
525 if (err!=TIFFReadDirEntryErrOk)
526 return(err);
527 *value=(uint64)m;
528 return(TIFFReadDirEntryErrOk);
529 }
530 default:
531 return(TIFFReadDirEntryErrType);
532 }
533 }
534
535 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
536 {
537 enum TIFFReadDirEntryErr err;
538 if (direntry->tdir_count!=1)
539 return(TIFFReadDirEntryErrCount);
540 switch (direntry->tdir_type)
541 {
542 case TIFF_BYTE:
543 {
544 uint8 m;
545 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
546 *value=(float)m;
547 return(TIFFReadDirEntryErrOk);
548 }
549 case TIFF_SBYTE:
550 {
551 int8 m;
552 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
553 *value=(float)m;
554 return(TIFFReadDirEntryErrOk);
555 }
556 case TIFF_SHORT:
557 {
558 uint16 m;
559 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
560 *value=(float)m;
561 return(TIFFReadDirEntryErrOk);
562 }
563 case TIFF_SSHORT:
564 {
565 int16 m;
566 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
567 *value=(float)m;
568 return(TIFFReadDirEntryErrOk);
569 }
570 case TIFF_LONG:
571 {
572 uint32 m;
573 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
574 *value=(float)m;
575 return(TIFFReadDirEntryErrOk);
576 }
577 case TIFF_SLONG:
578 {
579 int32 m;
580 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
581 *value=(float)m;
582 return(TIFFReadDirEntryErrOk);
583 }
584 case TIFF_LONG8:
585 {
586 uint64 m;
587 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
588 if (err!=TIFFReadDirEntryErrOk)
589 return(err);
590 #if defined(__WIN32__) && (_MSC_VER < 1500)
591 /*
592 * XXX: MSVC 6.0 does not support conversion
593 * of 64-bit integers into floating point
594 * values.
595 */
596 *value = _TIFFUInt64ToFloat(m);
597 #else
598 *value=(float)m;
599 #endif
600 return(TIFFReadDirEntryErrOk);
601 }
602 case TIFF_SLONG8:
603 {
604 int64 m;
605 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
606 if (err!=TIFFReadDirEntryErrOk)
607 return(err);
608 *value=(float)m;
609 return(TIFFReadDirEntryErrOk);
610 }
611 case TIFF_RATIONAL:
612 {
613 double m;
614 err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
615 if (err!=TIFFReadDirEntryErrOk)
616 return(err);
617 *value=(float)m;
618 return(TIFFReadDirEntryErrOk);
619 }
620 case TIFF_SRATIONAL:
621 {
622 double m;
623 err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
624 if (err!=TIFFReadDirEntryErrOk)
625 return(err);
626 *value=(float)m;
627 return(TIFFReadDirEntryErrOk);
628 }
629 case TIFF_FLOAT:
630 TIFFReadDirEntryCheckedFloat(tif,direntry,value);
631 return(TIFFReadDirEntryErrOk);
632 case TIFF_DOUBLE:
633 {
634 double m;
635 err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
636 if (err!=TIFFReadDirEntryErrOk)
637 return(err);
638 *value=(float)m;
639 return(TIFFReadDirEntryErrOk);
640 }
641 default:
642 return(TIFFReadDirEntryErrType);
643 }
644 }
645
646 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
647 {
648 enum TIFFReadDirEntryErr err;
649 if (direntry->tdir_count!=1)
650 return(TIFFReadDirEntryErrCount);
651 switch (direntry->tdir_type)
652 {
653 case TIFF_BYTE:
654 {
655 uint8 m;
656 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
657 *value=(double)m;
658 return(TIFFReadDirEntryErrOk);
659 }
660 case TIFF_SBYTE:
661 {
662 int8 m;
663 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
664 *value=(double)m;
665 return(TIFFReadDirEntryErrOk);
666 }
667 case TIFF_SHORT:
668 {
669 uint16 m;
670 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
671 *value=(double)m;
672 return(TIFFReadDirEntryErrOk);
673 }
674 case TIFF_SSHORT:
675 {
676 int16 m;
677 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
678 *value=(double)m;
679 return(TIFFReadDirEntryErrOk);
680 }
681 case TIFF_LONG:
682 {
683 uint32 m;
684 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
685 *value=(double)m;
686 return(TIFFReadDirEntryErrOk);
687 }
688 case TIFF_SLONG:
689 {
690 int32 m;
691 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
692 *value=(double)m;
693 return(TIFFReadDirEntryErrOk);
694 }
695 case TIFF_LONG8:
696 {
697 uint64 m;
698 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
699 if (err!=TIFFReadDirEntryErrOk)
700 return(err);
701 #if defined(__WIN32__) && (_MSC_VER < 1500)
702 /*
703 * XXX: MSVC 6.0 does not support conversion
704 * of 64-bit integers into floating point
705 * values.
706 */
707 *value = _TIFFUInt64ToDouble(m);
708 #else
709 *value = (double)m;
710 #endif
711 return(TIFFReadDirEntryErrOk);
712 }
713 case TIFF_SLONG8:
714 {
715 int64 m;
716 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
717 if (err!=TIFFReadDirEntryErrOk)
718 return(err);
719 *value=(double)m;
720 return(TIFFReadDirEntryErrOk);
721 }
722 case TIFF_RATIONAL:
723 err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
724 return(err);
725 case TIFF_SRATIONAL:
726 err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
727 return(err);
728 case TIFF_FLOAT:
729 {
730 float m;
731 TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
732 *value=(double)m;
733 return(TIFFReadDirEntryErrOk);
734 }
735 case TIFF_DOUBLE:
736 err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
737 return(err);
738 default:
739 return(TIFFReadDirEntryErrType);
740 }
741 }
742
743 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
744 {
745 enum TIFFReadDirEntryErr err;
746 if (direntry->tdir_count!=1)
747 return(TIFFReadDirEntryErrCount);
748 switch (direntry->tdir_type)
749 {
750 case TIFF_LONG:
751 case TIFF_IFD:
752 {
753 uint32 m;
754 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
755 *value=(uint64)m;
756 return(TIFFReadDirEntryErrOk);
757 }
758 case TIFF_LONG8:
759 case TIFF_IFD8:
760 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
761 return(err);
762 default:
763 return(TIFFReadDirEntryErrType);
764 }
765 }
766
767 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
768 {
769 int typesize;
770 uint32 datasize;
771 void* data;
772 typesize=TIFFDataWidth(direntry->tdir_type);
773 if ((direntry->tdir_count==0)||(typesize==0))
774 {
775 *value=0;
776 return(TIFFReadDirEntryErrOk);
777 }
778 (void) desttypesize;
779
780 /*
781 * As a sanity check, make sure we have no more than a 2GB tag array
782 * in either the current data type or the dest data type. This also
783 * avoids problems with overflow of tmsize_t on 32bit systems.
784 */
785 if ((uint64)(2147483647/typesize)<direntry->tdir_count)
786 return(TIFFReadDirEntryErrSizesan);
787 if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
788 return(TIFFReadDirEntryErrSizesan);
789
790 *count=(uint32)direntry->tdir_count;
791 datasize=(*count)*typesize;
792 assert((tmsize_t)datasize>0);
793 data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
794 if (data==0)
795 return(TIFFReadDirEntryErrAlloc);
796 if (!(tif->tif_flags&TIFF_BIGTIFF))
797 {
798 if (datasize<=4)
799 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
800 else
801 {
802 enum TIFFReadDirEntryErr err;
803 uint32 offset = direntry->tdir_offset.toff_long;
804 if (tif->tif_flags&TIFF_SWAB)
805 TIFFSwabLong(&offset);
806 err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
807 if (err!=TIFFReadDirEntryErrOk)
808 {
809 _TIFFfree(data);
810 return(err);
811 }
812 }
813 }
814 else
815 {
816 if (datasize<=8)
817 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
818 else
819 {
820 enum TIFFReadDirEntryErr err;
821 uint64 offset = direntry->tdir_offset.toff_long8;
822 if (tif->tif_flags&TIFF_SWAB)
823 TIFFSwabLong8(&offset);
824 err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
825 if (err!=TIFFReadDirEntryErrOk)
826 {
827 _TIFFfree(data);
828 return(err);
829 }
830 }
831 }
832 *value=data;
833 return(TIFFReadDirEntryErrOk);
834 }
835
836 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
837 {
838 enum TIFFReadDirEntryErr err;
839 uint32 count;
840 void* origdata;
841 uint8* data;
842 switch (direntry->tdir_type)
843 {
844 case TIFF_ASCII:
845 case TIFF_UNDEFINED:
846 case TIFF_BYTE:
847 case TIFF_SBYTE:
848 case TIFF_SHORT:
849 case TIFF_SSHORT:
850 case TIFF_LONG:
851 case TIFF_SLONG:
852 case TIFF_LONG8:
853 case TIFF_SLONG8:
854 break;
855 default:
856 return(TIFFReadDirEntryErrType);
857 }
858 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
859 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
860 {
861 *value=0;
862 return(err);
863 }
864 switch (direntry->tdir_type)
865 {
866 case TIFF_ASCII:
867 case TIFF_UNDEFINED:
868 case TIFF_BYTE:
869 *value=(uint8*)origdata;
870 return(TIFFReadDirEntryErrOk);
871 case TIFF_SBYTE:
872 {
873 int8* m;
874 uint32 n;
875 m=(int8*)origdata;
876 for (n=0; n<count; n++)
877 {
878 err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
879 if (err!=TIFFReadDirEntryErrOk)
880 {
881 _TIFFfree(origdata);
882 return(err);
883 }
884 m++;
885 }
886 *value=(uint8*)origdata;
887 return(TIFFReadDirEntryErrOk);
888 }
889 }
890 data=(uint8*)_TIFFmalloc(count);
891 if (data==0)
892 {
893 _TIFFfree(origdata);
894 return(TIFFReadDirEntryErrAlloc);
895 }
896 switch (direntry->tdir_type)
897 {
898 case TIFF_SHORT:
899 {
900 uint16* ma;
901 uint8* mb;
902 uint32 n;
903 ma=(uint16*)origdata;
904 mb=data;
905 for (n=0; n<count; n++)
906 {
907 if (tif->tif_flags&TIFF_SWAB)
908 TIFFSwabShort(ma);
909 err=TIFFReadDirEntryCheckRangeByteShort(*ma);
910 if (err!=TIFFReadDirEntryErrOk)
911 break;
912 *mb++=(uint8)(*ma++);
913 }
914 }
915 break;
916 case TIFF_SSHORT:
917 {
918 int16* ma;
919 uint8* mb;
920 uint32 n;
921 ma=(int16*)origdata;
922 mb=data;
923 for (n=0; n<count; n++)
924 {
925 if (tif->tif_flags&TIFF_SWAB)
926 TIFFSwabShort((uint16*)ma);
927 err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
928 if (err!=TIFFReadDirEntryErrOk)
929 break;
930 *mb++=(uint8)(*ma++);
931 }
932 }
933 break;
934 case TIFF_LONG:
935 {
936 uint32* ma;
937 uint8* mb;
938 uint32 n;
939 ma=(uint32*)origdata;
940 mb=data;
941 for (n=0; n<count; n++)
942 {
943 if (tif->tif_flags&TIFF_SWAB)
944 TIFFSwabLong(ma);
945 err=TIFFReadDirEntryCheckRangeByteLong(*ma);
946 if (err!=TIFFReadDirEntryErrOk)
947 break;
948 *mb++=(uint8)(*ma++);
949 }
950 }
951 break;
952 case TIFF_SLONG:
953 {
954 int32* ma;
955 uint8* mb;
956 uint32 n;
957 ma=(int32*)origdata;
958 mb=data;
959 for (n=0; n<count; n++)
960 {
961 if (tif->tif_flags&TIFF_SWAB)
962 TIFFSwabLong((uint32*)ma);
963 err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
964 if (err!=TIFFReadDirEntryErrOk)
965 break;
966 *mb++=(uint8)(*ma++);
967 }
968 }
969 break;
970 case TIFF_LONG8:
971 {
972 uint64* ma;
973 uint8* mb;
974 uint32 n;
975 ma=(uint64*)origdata;
976 mb=data;
977 for (n=0; n<count; n++)
978 {
979 if (tif->tif_flags&TIFF_SWAB)
980 TIFFSwabLong8(ma);
981 err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
982 if (err!=TIFFReadDirEntryErrOk)
983 break;
984 *mb++=(uint8)(*ma++);
985 }
986 }
987 break;
988 case TIFF_SLONG8:
989 {
990 int64* ma;
991 uint8* mb;
992 uint32 n;
993 ma=(int64*)origdata;
994 mb=data;
995 for (n=0; n<count; n++)
996 {
997 if (tif->tif_flags&TIFF_SWAB)
998 TIFFSwabLong8((uint64*)ma);
999 err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1000 if (err!=TIFFReadDirEntryErrOk)
1001 break;
1002 *mb++=(uint8)(*ma++);
1003 }
1004 }
1005 break;
1006 }
1007 _TIFFfree(origdata);
1008 if (err!=TIFFReadDirEntryErrOk)
1009 {
1010 _TIFFfree(data);
1011 return(err);
1012 }
1013 *value=data;
1014 return(TIFFReadDirEntryErrOk);
1015 }
1016
1017 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
1018 {
1019 enum TIFFReadDirEntryErr err;
1020 uint32 count;
1021 void* origdata;
1022 int8* data;
1023 switch (direntry->tdir_type)
1024 {
1025 case TIFF_UNDEFINED:
1026 case TIFF_BYTE:
1027 case TIFF_SBYTE:
1028 case TIFF_SHORT:
1029 case TIFF_SSHORT:
1030 case TIFF_LONG:
1031 case TIFF_SLONG:
1032 case TIFF_LONG8:
1033 case TIFF_SLONG8:
1034 break;
1035 default:
1036 return(TIFFReadDirEntryErrType);
1037 }
1038 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1039 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1040 {
1041 *value=0;
1042 return(err);
1043 }
1044 switch (direntry->tdir_type)
1045 {
1046 case TIFF_UNDEFINED:
1047 case TIFF_BYTE:
1048 {
1049 uint8* m;
1050 uint32 n;
1051 m=(uint8*)origdata;
1052 for (n=0; n<count; n++)
1053 {
1054 err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1055 if (err!=TIFFReadDirEntryErrOk)
1056 {
1057 _TIFFfree(origdata);
1058 return(err);
1059 }
1060 m++;
1061 }
1062 *value=(int8*)origdata;
1063 return(TIFFReadDirEntryErrOk);
1064 }
1065 case TIFF_SBYTE:
1066 *value=(int8*)origdata;
1067 return(TIFFReadDirEntryErrOk);
1068 }
1069 data=(int8*)_TIFFmalloc(count);
1070 if (data==0)
1071 {
1072 _TIFFfree(origdata);
1073 return(TIFFReadDirEntryErrAlloc);
1074 }
1075 switch (direntry->tdir_type)
1076 {
1077 case TIFF_SHORT:
1078 {
1079 uint16* ma;
1080 int8* mb;
1081 uint32 n;
1082 ma=(uint16*)origdata;
1083 mb=data;
1084 for (n=0; n<count; n++)
1085 {
1086 if (tif->tif_flags&TIFF_SWAB)
1087 TIFFSwabShort(ma);
1088 err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1089 if (err!=TIFFReadDirEntryErrOk)
1090 break;
1091 *mb++=(int8)(*ma++);
1092 }
1093 }
1094 break;
1095 case TIFF_SSHORT:
1096 {
1097 int16* ma;
1098 int8* mb;
1099 uint32 n;
1100 ma=(int16*)origdata;
1101 mb=data;
1102 for (n=0; n<count; n++)
1103 {
1104 if (tif->tif_flags&TIFF_SWAB)
1105 TIFFSwabShort((uint16*)ma);
1106 err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1107 if (err!=TIFFReadDirEntryErrOk)
1108 break;
1109 *mb++=(int8)(*ma++);
1110 }
1111 }
1112 break;
1113 case TIFF_LONG:
1114 {
1115 uint32* ma;
1116 int8* mb;
1117 uint32 n;
1118 ma=(uint32*)origdata;
1119 mb=data;
1120 for (n=0; n<count; n++)
1121 {
1122 if (tif->tif_flags&TIFF_SWAB)
1123 TIFFSwabLong(ma);
1124 err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1125 if (err!=TIFFReadDirEntryErrOk)
1126 break;
1127 *mb++=(int8)(*ma++);
1128 }
1129 }
1130 break;
1131 case TIFF_SLONG:
1132 {
1133 int32* ma;
1134 int8* mb;
1135 uint32 n;
1136 ma=(int32*)origdata;
1137 mb=data;
1138 for (n=0; n<count; n++)
1139 {
1140 if (tif->tif_flags&TIFF_SWAB)
1141 TIFFSwabLong((uint32*)ma);
1142 err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1143 if (err!=TIFFReadDirEntryErrOk)
1144 break;
1145 *mb++=(int8)(*ma++);
1146 }
1147 }
1148 break;
1149 case TIFF_LONG8:
1150 {
1151 uint64* ma;
1152 int8* mb;
1153 uint32 n;
1154 ma=(uint64*)origdata;
1155 mb=data;
1156 for (n=0; n<count; n++)
1157 {
1158 if (tif->tif_flags&TIFF_SWAB)
1159 TIFFSwabLong8(ma);
1160 err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1161 if (err!=TIFFReadDirEntryErrOk)
1162 break;
1163 *mb++=(int8)(*ma++);
1164 }
1165 }
1166 break;
1167 case TIFF_SLONG8:
1168 {
1169 int64* ma;
1170 int8* mb;
1171 uint32 n;
1172 ma=(int64*)origdata;
1173 mb=data;
1174 for (n=0; n<count; n++)
1175 {
1176 if (tif->tif_flags&TIFF_SWAB)
1177 TIFFSwabLong8((uint64*)ma);
1178 err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1179 if (err!=TIFFReadDirEntryErrOk)
1180 break;
1181 *mb++=(int8)(*ma++);
1182 }
1183 }
1184 break;
1185 }
1186 _TIFFfree(origdata);
1187 if (err!=TIFFReadDirEntryErrOk)
1188 {
1189 _TIFFfree(data);
1190 return(err);
1191 }
1192 *value=data;
1193 return(TIFFReadDirEntryErrOk);
1194 }
1195
1196 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
1197 {
1198 enum TIFFReadDirEntryErr err;
1199 uint32 count;
1200 void* origdata;
1201 uint16* data;
1202 switch (direntry->tdir_type)
1203 {
1204 case TIFF_BYTE:
1205 case TIFF_SBYTE:
1206 case TIFF_SHORT:
1207 case TIFF_SSHORT:
1208 case TIFF_LONG:
1209 case TIFF_SLONG:
1210 case TIFF_LONG8:
1211 case TIFF_SLONG8:
1212 break;
1213 default:
1214 return(TIFFReadDirEntryErrType);
1215 }
1216 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1217 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1218 {
1219 *value=0;
1220 return(err);
1221 }
1222 switch (direntry->tdir_type)
1223 {
1224 case TIFF_SHORT:
1225 *value=(uint16*)origdata;
1226 if (tif->tif_flags&TIFF_SWAB)
1227 TIFFSwabArrayOfShort(*value,count);
1228 return(TIFFReadDirEntryErrOk);
1229 case TIFF_SSHORT:
1230 {
1231 int16* m;
1232 uint32 n;
1233 m=(int16*)origdata;
1234 for (n=0; n<count; n++)
1235 {
1236 if (tif->tif_flags&TIFF_SWAB)
1237 TIFFSwabShort((uint16*)m);
1238 err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1239 if (err!=TIFFReadDirEntryErrOk)
1240 {
1241 _TIFFfree(origdata);
1242 return(err);
1243 }
1244 m++;
1245 }
1246 *value=(uint16*)origdata;
1247 return(TIFFReadDirEntryErrOk);
1248 }
1249 }
1250 data=(uint16*)_TIFFmalloc(count*2);
1251 if (data==0)
1252 {
1253 _TIFFfree(origdata);
1254 return(TIFFReadDirEntryErrAlloc);
1255 }
1256 switch (direntry->tdir_type)
1257 {
1258 case TIFF_BYTE:
1259 {
1260 uint8* ma;
1261 uint16* mb;
1262 uint32 n;
1263 ma=(uint8*)origdata;
1264 mb=data;
1265 for (n=0; n<count; n++)
1266 *mb++=(uint16)(*ma++);
1267 }
1268 break;
1269 case TIFF_SBYTE:
1270 {
1271 int8* ma;
1272 uint16* mb;
1273 uint32 n;
1274 ma=(int8*)origdata;
1275 mb=data;
1276 for (n=0; n<count; n++)
1277 {
1278 err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1279 if (err!=TIFFReadDirEntryErrOk)
1280 break;
1281 *mb++=(uint16)(*ma++);
1282 }
1283 }
1284 break;
1285 case TIFF_LONG:
1286 {
1287 uint32* ma;
1288 uint16* mb;
1289 uint32 n;
1290 ma=(uint32*)origdata;
1291 mb=data;
1292 for (n=0; n<count; n++)
1293 {
1294 if (tif->tif_flags&TIFF_SWAB)
1295 TIFFSwabLong(ma);
1296 err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1297 if (err!=TIFFReadDirEntryErrOk)
1298 break;
1299 *mb++=(uint16)(*ma++);
1300 }
1301 }
1302 break;
1303 case TIFF_SLONG:
1304 {
1305 int32* ma;
1306 uint16* mb;
1307 uint32 n;
1308 ma=(int32*)origdata;
1309 mb=data;
1310 for (n=0; n<count; n++)
1311 {
1312 if (tif->tif_flags&TIFF_SWAB)
1313 TIFFSwabLong((uint32*)ma);
1314 err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1315 if (err!=TIFFReadDirEntryErrOk)
1316 break;
1317 *mb++=(uint16)(*ma++);
1318 }
1319 }
1320 break;
1321 case TIFF_LONG8:
1322 {
1323 uint64* ma;
1324 uint16* mb;
1325 uint32 n;
1326 ma=(uint64*)origdata;
1327 mb=data;
1328 for (n=0; n<count; n++)
1329 {
1330 if (tif->tif_flags&TIFF_SWAB)
1331 TIFFSwabLong8(ma);
1332 err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1333 if (err!=TIFFReadDirEntryErrOk)
1334 break;
1335 *mb++=(uint16)(*ma++);
1336 }
1337 }
1338 break;
1339 case TIFF_SLONG8:
1340 {
1341 int64* ma;
1342 uint16* mb;
1343 uint32 n;
1344 ma=(int64*)origdata;
1345 mb=data;
1346 for (n=0; n<count; n++)
1347 {
1348 if (tif->tif_flags&TIFF_SWAB)
1349 TIFFSwabLong8((uint64*)ma);
1350 err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1351 if (err!=TIFFReadDirEntryErrOk)
1352 break;
1353 *mb++=(uint16)(*ma++);
1354 }
1355 }
1356 break;
1357 }
1358 _TIFFfree(origdata);
1359 if (err!=TIFFReadDirEntryErrOk)
1360 {
1361 _TIFFfree(data);
1362 return(err);
1363 }
1364 *value=data;
1365 return(TIFFReadDirEntryErrOk);
1366 }
1367
1368 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
1369 {
1370 enum TIFFReadDirEntryErr err;
1371 uint32 count;
1372 void* origdata;
1373 int16* data;
1374 switch (direntry->tdir_type)
1375 {
1376 case TIFF_BYTE:
1377 case TIFF_SBYTE:
1378 case TIFF_SHORT:
1379 case TIFF_SSHORT:
1380 case TIFF_LONG:
1381 case TIFF_SLONG:
1382 case TIFF_LONG8:
1383 case TIFF_SLONG8:
1384 break;
1385 default:
1386 return(TIFFReadDirEntryErrType);
1387 }
1388 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1389 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1390 {
1391 *value=0;
1392 return(err);
1393 }
1394 switch (direntry->tdir_type)
1395 {
1396 case TIFF_SHORT:
1397 {
1398 uint16* m;
1399 uint32 n;
1400 m=(uint16*)origdata;
1401 for (n=0; n<count; n++)
1402 {
1403 if (tif->tif_flags&TIFF_SWAB)
1404 TIFFSwabShort(m);
1405 err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1406 if (err!=TIFFReadDirEntryErrOk)
1407 {
1408 _TIFFfree(origdata);
1409 return(err);
1410 }
1411 m++;
1412 }
1413 *value=(int16*)origdata;
1414 return(TIFFReadDirEntryErrOk);
1415 }
1416 case TIFF_SSHORT:
1417 *value=(int16*)origdata;
1418 if (tif->tif_flags&TIFF_SWAB)
1419 TIFFSwabArrayOfShort((uint16*)(*value),count);
1420 return(TIFFReadDirEntryErrOk);
1421 }
1422 data=(int16*)_TIFFmalloc(count*2);
1423 if (data==0)
1424 {
1425 _TIFFfree(origdata);
1426 return(TIFFReadDirEntryErrAlloc);
1427 }
1428 switch (direntry->tdir_type)
1429 {
1430 case TIFF_BYTE:
1431 {
1432 uint8* ma;
1433 int16* mb;
1434 uint32 n;
1435 ma=(uint8*)origdata;
1436 mb=data;
1437 for (n=0; n<count; n++)
1438 *mb++=(int16)(*ma++);
1439 }
1440 break;
1441 case TIFF_SBYTE:
1442 {
1443 int8* ma;
1444 int16* mb;
1445 uint32 n;
1446 ma=(int8*)origdata;
1447 mb=data;
1448 for (n=0; n<count; n++)
1449 *mb++=(int16)(*ma++);
1450 }
1451 break;
1452 case TIFF_LONG:
1453 {
1454 uint32* ma;
1455 int16* mb;
1456 uint32 n;
1457 ma=(uint32*)origdata;
1458 mb=data;
1459 for (n=0; n<count; n++)
1460 {
1461 if (tif->tif_flags&TIFF_SWAB)
1462 TIFFSwabLong(ma);
1463 err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1464 if (err!=TIFFReadDirEntryErrOk)
1465 break;
1466 *mb++=(int16)(*ma++);
1467 }
1468 }
1469 break;
1470 case TIFF_SLONG:
1471 {
1472 int32* ma;
1473 int16* mb;
1474 uint32 n;
1475 ma=(int32*)origdata;
1476 mb=data;
1477 for (n=0; n<count; n++)
1478 {
1479 if (tif->tif_flags&TIFF_SWAB)
1480 TIFFSwabLong((uint32*)ma);
1481 err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1482 if (err!=TIFFReadDirEntryErrOk)
1483 break;
1484 *mb++=(int16)(*ma++);
1485 }
1486 }
1487 break;
1488 case TIFF_LONG8:
1489 {
1490 uint64* ma;
1491 int16* mb;
1492 uint32 n;
1493 ma=(uint64*)origdata;
1494 mb=data;
1495 for (n=0; n<count; n++)
1496 {
1497 if (tif->tif_flags&TIFF_SWAB)
1498 TIFFSwabLong8(ma);
1499 err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1500 if (err!=TIFFReadDirEntryErrOk)
1501 break;
1502 *mb++=(int16)(*ma++);
1503 }
1504 }
1505 break;
1506 case TIFF_SLONG8:
1507 {
1508 int64* ma;
1509 int16* mb;
1510 uint32 n;
1511 ma=(int64*)origdata;
1512 mb=data;
1513 for (n=0; n<count; n++)
1514 {
1515 if (tif->tif_flags&TIFF_SWAB)
1516 TIFFSwabLong8((uint64*)ma);
1517 err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1518 if (err!=TIFFReadDirEntryErrOk)
1519 break;
1520 *mb++=(int16)(*ma++);
1521 }
1522 }
1523 break;
1524 }
1525 _TIFFfree(origdata);
1526 if (err!=TIFFReadDirEntryErrOk)
1527 {
1528 _TIFFfree(data);
1529 return(err);
1530 }
1531 *value=data;
1532 return(TIFFReadDirEntryErrOk);
1533 }
1534
1535 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
1536 {
1537 enum TIFFReadDirEntryErr err;
1538 uint32 count;
1539 void* origdata;
1540 uint32* data;
1541 switch (direntry->tdir_type)
1542 {
1543 case TIFF_BYTE:
1544 case TIFF_SBYTE:
1545 case TIFF_SHORT:
1546 case TIFF_SSHORT:
1547 case TIFF_LONG:
1548 case TIFF_SLONG:
1549 case TIFF_LONG8:
1550 case TIFF_SLONG8:
1551 break;
1552 default:
1553 return(TIFFReadDirEntryErrType);
1554 }
1555 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1556 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1557 {
1558 *value=0;
1559 return(err);
1560 }
1561 switch (direntry->tdir_type)
1562 {
1563 case TIFF_LONG:
1564 *value=(uint32*)origdata;
1565 if (tif->tif_flags&TIFF_SWAB)
1566 TIFFSwabArrayOfLong(*value,count);
1567 return(TIFFReadDirEntryErrOk);
1568 case TIFF_SLONG:
1569 {
1570 int32* m;
1571 uint32 n;
1572 m=(int32*)origdata;
1573 for (n=0; n<count; n++)
1574 {
1575 if (tif->tif_flags&TIFF_SWAB)
1576 TIFFSwabLong((uint32*)m);
1577 err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1578 if (err!=TIFFReadDirEntryErrOk)
1579 {
1580 _TIFFfree(origdata);
1581 return(err);
1582 }
1583 m++;
1584 }
1585 *value=(uint32*)origdata;
1586 return(TIFFReadDirEntryErrOk);
1587 }
1588 }
1589 data=(uint32*)_TIFFmalloc(count*4);
1590 if (data==0)
1591 {
1592 _TIFFfree(origdata);
1593 return(TIFFReadDirEntryErrAlloc);
1594 }
1595 switch (direntry->tdir_type)
1596 {
1597 case TIFF_BYTE:
1598 {
1599 uint8* ma;
1600 uint32* mb;
1601 uint32 n;
1602 ma=(uint8*)origdata;
1603 mb=data;
1604 for (n=0; n<count; n++)
1605 *mb++=(uint32)(*ma++);
1606 }
1607 break;
1608 case TIFF_SBYTE:
1609 {
1610 int8* ma;
1611 uint32* mb;
1612 uint32 n;
1613 ma=(int8*)origdata;
1614 mb=data;
1615 for (n=0; n<count; n++)
1616 {
1617 err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1618 if (err!=TIFFReadDirEntryErrOk)
1619 break;
1620 *mb++=(uint32)(*ma++);
1621 }
1622 }
1623 break;
1624 case TIFF_SHORT:
1625 {
1626 uint16* ma;
1627 uint32* mb;
1628 uint32 n;
1629 ma=(uint16*)origdata;
1630 mb=data;
1631 for (n=0; n<count; n++)
1632 {
1633 if (tif->tif_flags&TIFF_SWAB)
1634 TIFFSwabShort(ma);
1635 *mb++=(uint32)(*ma++);
1636 }
1637 }
1638 break;
1639 case TIFF_SSHORT:
1640 {
1641 int16* ma;
1642 uint32* mb;
1643 uint32 n;
1644 ma=(int16*)origdata;
1645 mb=data;
1646 for (n=0; n<count; n++)
1647 {
1648 if (tif->tif_flags&TIFF_SWAB)
1649 TIFFSwabShort((uint16*)ma);
1650 err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1651 if (err!=TIFFReadDirEntryErrOk)
1652 break;
1653 *mb++=(uint32)(*ma++);
1654 }
1655 }
1656 break;
1657 case TIFF_LONG8:
1658 {
1659 uint64* ma;
1660 uint32* mb;
1661 uint32 n;
1662 ma=(uint64*)origdata;
1663 mb=data;
1664 for (n=0; n<count; n++)
1665 {
1666 if (tif->tif_flags&TIFF_SWAB)
1667 TIFFSwabLong8(ma);
1668 err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1669 if (err!=TIFFReadDirEntryErrOk)
1670 break;
1671 *mb++=(uint32)(*ma++);
1672 }
1673 }
1674 break;
1675 case TIFF_SLONG8:
1676 {
1677 int64* ma;
1678 uint32* mb;
1679 uint32 n;
1680 ma=(int64*)origdata;
1681 mb=data;
1682 for (n=0; n<count; n++)
1683 {
1684 if (tif->tif_flags&TIFF_SWAB)
1685 TIFFSwabLong8((uint64*)ma);
1686 err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1687 if (err!=TIFFReadDirEntryErrOk)
1688 break;
1689 *mb++=(uint32)(*ma++);
1690 }
1691 }
1692 break;
1693 }
1694 _TIFFfree(origdata);
1695 if (err!=TIFFReadDirEntryErrOk)
1696 {
1697 _TIFFfree(data);
1698 return(err);
1699 }
1700 *value=data;
1701 return(TIFFReadDirEntryErrOk);
1702 }
1703
1704 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
1705 {
1706 enum TIFFReadDirEntryErr err;
1707 uint32 count;
1708 void* origdata;
1709 int32* data;
1710 switch (direntry->tdir_type)
1711 {
1712 case TIFF_BYTE:
1713 case TIFF_SBYTE:
1714 case TIFF_SHORT:
1715 case TIFF_SSHORT:
1716 case TIFF_LONG:
1717 case TIFF_SLONG:
1718 case TIFF_LONG8:
1719 case TIFF_SLONG8:
1720 break;
1721 default:
1722 return(TIFFReadDirEntryErrType);
1723 }
1724 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1725 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1726 {
1727 *value=0;
1728 return(err);
1729 }
1730 switch (direntry->tdir_type)
1731 {
1732 case TIFF_LONG:
1733 {
1734 uint32* m;
1735 uint32 n;
1736 m=(uint32*)origdata;
1737 for (n=0; n<count; n++)
1738 {
1739 if (tif->tif_flags&TIFF_SWAB)
1740 TIFFSwabLong((uint32*)m);
1741 err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1742 if (err!=TIFFReadDirEntryErrOk)
1743 {
1744 _TIFFfree(origdata);
1745 return(err);
1746 }
1747 m++;
1748 }
1749 *value=(int32*)origdata;
1750 return(TIFFReadDirEntryErrOk);
1751 }
1752 case TIFF_SLONG:
1753 *value=(int32*)origdata;
1754 if (tif->tif_flags&TIFF_SWAB)
1755 TIFFSwabArrayOfLong((uint32*)(*value),count);
1756 return(TIFFReadDirEntryErrOk);
1757 }
1758 data=(int32*)_TIFFmalloc(count*4);
1759 if (data==0)
1760 {
1761 _TIFFfree(origdata);
1762 return(TIFFReadDirEntryErrAlloc);
1763 }
1764 switch (direntry->tdir_type)
1765 {
1766 case TIFF_BYTE:
1767 {
1768 uint8* ma;
1769 int32* mb;
1770 uint32 n;
1771 ma=(uint8*)origdata;
1772 mb=data;
1773 for (n=0; n<count; n++)
1774 *mb++=(int32)(*ma++);
1775 }
1776 break;
1777 case TIFF_SBYTE:
1778 {
1779 int8* ma;
1780 int32* mb;
1781 uint32 n;
1782 ma=(int8*)origdata;
1783 mb=data;
1784 for (n=0; n<count; n++)
1785 *mb++=(int32)(*ma++);
1786 }
1787 break;
1788 case TIFF_SHORT:
1789 {
1790 uint16* ma;
1791 int32* mb;
1792 uint32 n;
1793 ma=(uint16*)origdata;
1794 mb=data;
1795 for (n=0; n<count; n++)
1796 {
1797 if (tif->tif_flags&TIFF_SWAB)
1798 TIFFSwabShort(ma);
1799 *mb++=(int32)(*ma++);
1800 }
1801 }
1802 break;
1803 case TIFF_SSHORT:
1804 {
1805 int16* ma;
1806 int32* mb;
1807 uint32 n;
1808 ma=(int16*)origdata;
1809 mb=data;
1810 for (n=0; n<count; n++)
1811 {
1812 if (tif->tif_flags&TIFF_SWAB)
1813 TIFFSwabShort((uint16*)ma);
1814 *mb++=(int32)(*ma++);
1815 }
1816 }
1817 break;
1818 case TIFF_LONG8:
1819 {
1820 uint64* ma;
1821 int32* mb;
1822 uint32 n;
1823 ma=(uint64*)origdata;
1824 mb=data;
1825 for (n=0; n<count; n++)
1826 {
1827 if (tif->tif_flags&TIFF_SWAB)
1828 TIFFSwabLong8(ma);
1829 err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1830 if (err!=TIFFReadDirEntryErrOk)
1831 break;
1832 *mb++=(int32)(*ma++);
1833 }
1834 }
1835 break;
1836 case TIFF_SLONG8:
1837 {
1838 int64* ma;
1839 int32* mb;
1840 uint32 n;
1841 ma=(int64*)origdata;
1842 mb=data;
1843 for (n=0; n<count; n++)
1844 {
1845 if (tif->tif_flags&TIFF_SWAB)
1846 TIFFSwabLong8((uint64*)ma);
1847 err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1848 if (err!=TIFFReadDirEntryErrOk)
1849 break;
1850 *mb++=(int32)(*ma++);
1851 }
1852 }
1853 break;
1854 }
1855 _TIFFfree(origdata);
1856 if (err!=TIFFReadDirEntryErrOk)
1857 {
1858 _TIFFfree(data);
1859 return(err);
1860 }
1861 *value=data;
1862 return(TIFFReadDirEntryErrOk);
1863 }
1864
1865 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
1866 {
1867 enum TIFFReadDirEntryErr err;
1868 uint32 count;
1869 void* origdata;
1870 uint64* data;
1871 switch (direntry->tdir_type)
1872 {
1873 case TIFF_BYTE:
1874 case TIFF_SBYTE:
1875 case TIFF_SHORT:
1876 case TIFF_SSHORT:
1877 case TIFF_LONG:
1878 case TIFF_SLONG:
1879 case TIFF_LONG8:
1880 case TIFF_SLONG8:
1881 break;
1882 default:
1883 return(TIFFReadDirEntryErrType);
1884 }
1885 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
1886 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1887 {
1888 *value=0;
1889 return(err);
1890 }
1891 switch (direntry->tdir_type)
1892 {
1893 case TIFF_LONG8:
1894 *value=(uint64*)origdata;
1895 if (tif->tif_flags&TIFF_SWAB)
1896 TIFFSwabArrayOfLong8(*value,count);
1897 return(TIFFReadDirEntryErrOk);
1898 case TIFF_SLONG8:
1899 {
1900 int64* m;
1901 uint32 n;
1902 m=(int64*)origdata;
1903 for (n=0; n<count; n++)
1904 {
1905 if (tif->tif_flags&TIFF_SWAB)
1906 TIFFSwabLong8((uint64*)m);
1907 err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
1908 if (err!=TIFFReadDirEntryErrOk)
1909 {
1910 _TIFFfree(origdata);
1911 return(err);
1912 }
1913 m++;
1914 }
1915 *value=(uint64*)origdata;
1916 return(TIFFReadDirEntryErrOk);
1917 }
1918 }
1919 data=(uint64*)_TIFFmalloc(count*8);
1920 if (data==0)
1921 {
1922 _TIFFfree(origdata);
1923 return(TIFFReadDirEntryErrAlloc);
1924 }
1925 switch (direntry->tdir_type)
1926 {
1927 case TIFF_BYTE:
1928 {
1929 uint8* ma;
1930 uint64* mb;
1931 uint32 n;
1932 ma=(uint8*)origdata;
1933 mb=data;
1934 for (n=0; n<count; n++)
1935 *mb++=(uint64)(*ma++);
1936 }
1937 break;
1938 case TIFF_SBYTE:
1939 {
1940 int8* ma;
1941 uint64* mb;
1942 uint32 n;
1943 ma=(int8*)origdata;
1944 mb=data;
1945 for (n=0; n<count; n++)
1946 {
1947 err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
1948 if (err!=TIFFReadDirEntryErrOk)
1949 break;
1950 *mb++=(uint64)(*ma++);
1951 }
1952 }
1953 break;
1954 case TIFF_SHORT:
1955 {
1956 uint16* ma;
1957 uint64* mb;
1958 uint32 n;
1959 ma=(uint16*)origdata;
1960 mb=data;
1961 for (n=0; n<count; n++)
1962 {
1963 if (tif->tif_flags&TIFF_SWAB)
1964 TIFFSwabShort(ma);
1965 *mb++=(uint64)(*ma++);
1966 }
1967 }
1968 break;
1969 case TIFF_SSHORT:
1970 {
1971 int16* ma;
1972 uint64* mb;
1973 uint32 n;
1974 ma=(int16*)origdata;
1975 mb=data;
1976 for (n=0; n<count; n++)
1977 {
1978 if (tif->tif_flags&TIFF_SWAB)
1979 TIFFSwabShort((uint16*)ma);
1980 err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
1981 if (err!=TIFFReadDirEntryErrOk)
1982 break;
1983 *mb++=(uint64)(*ma++);
1984 }
1985 }
1986 break;
1987 case TIFF_LONG:
1988 {
1989 uint32* ma;
1990 uint64* mb;
1991 uint32 n;
1992 ma=(uint32*)origdata;
1993 mb=data;
1994 for (n=0; n<count; n++)
1995 {
1996 if (tif->tif_flags&TIFF_SWAB)
1997 TIFFSwabLong(ma);
1998 *mb++=(uint64)(*ma++);
1999 }
2000 }
2001 break;
2002 case TIFF_SLONG:
2003 {
2004 int32* ma;
2005 uint64* mb;
2006 uint32 n;
2007 ma=(int32*)origdata;
2008 mb=data;
2009 for (n=0; n<count; n++)
2010 {
2011 if (tif->tif_flags&TIFF_SWAB)
2012 TIFFSwabLong((uint32*)ma);
2013 err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2014 if (err!=TIFFReadDirEntryErrOk)
2015 break;
2016 *mb++=(uint64)(*ma++);
2017 }
2018 }
2019 break;
2020 }
2021 _TIFFfree(origdata);
2022 if (err!=TIFFReadDirEntryErrOk)
2023 {
2024 _TIFFfree(data);
2025 return(err);
2026 }
2027 *value=data;
2028 return(TIFFReadDirEntryErrOk);
2029 }
2030
2031 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
2032 {
2033 enum TIFFReadDirEntryErr err;
2034 uint32 count;
2035 void* origdata;
2036 int64* data;
2037 switch (direntry->tdir_type)
2038 {
2039 case TIFF_BYTE:
2040 case TIFF_SBYTE:
2041 case TIFF_SHORT:
2042 case TIFF_SSHORT:
2043 case TIFF_LONG:
2044 case TIFF_SLONG:
2045 case TIFF_LONG8:
2046 case TIFF_SLONG8:
2047 break;
2048 default:
2049 return(TIFFReadDirEntryErrType);
2050 }
2051 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2052 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2053 {
2054 *value=0;
2055 return(err);
2056 }
2057 switch (direntry->tdir_type)
2058 {
2059 case TIFF_LONG8:
2060 {
2061 uint64* m;
2062 uint32 n;
2063 m=(uint64*)origdata;
2064 for (n=0; n<count; n++)
2065 {
2066 if (tif->tif_flags&TIFF_SWAB)
2067 TIFFSwabLong8(m);
2068 err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2069 if (err!=TIFFReadDirEntryErrOk)
2070 {
2071 _TIFFfree(origdata);
2072 return(err);
2073 }
2074 m++;
2075 }
2076 *value=(int64*)origdata;
2077 return(TIFFReadDirEntryErrOk);
2078 }
2079 case TIFF_SLONG8:
2080 *value=(int64*)origdata;
2081 if (tif->tif_flags&TIFF_SWAB)
2082 TIFFSwabArrayOfLong8((uint64*)(*value),count);
2083 return(TIFFReadDirEntryErrOk);
2084 }
2085 data=(int64*)_TIFFmalloc(count*8);
2086 if (data==0)
2087 {
2088 _TIFFfree(origdata);
2089 return(TIFFReadDirEntryErrAlloc);
2090 }
2091 switch (direntry->tdir_type)
2092 {
2093 case TIFF_BYTE:
2094 {
2095 uint8* ma;
2096 int64* mb;
2097 uint32 n;
2098 ma=(uint8*)origdata;
2099 mb=data;
2100 for (n=0; n<count; n++)
2101 *mb++=(int64)(*ma++);
2102 }
2103 break;
2104 case TIFF_SBYTE:
2105 {
2106 int8* ma;
2107 int64* mb;
2108 uint32 n;
2109 ma=(int8*)origdata;
2110 mb=data;
2111 for (n=0; n<count; n++)
2112 *mb++=(int64)(*ma++);
2113 }
2114 break;
2115 case TIFF_SHORT:
2116 {
2117 uint16* ma;
2118 int64* mb;
2119 uint32 n;
2120 ma=(uint16*)origdata;
2121 mb=data;
2122 for (n=0; n<count; n++)
2123 {
2124 if (tif->tif_flags&TIFF_SWAB)
2125 TIFFSwabShort(ma);
2126 *mb++=(int64)(*ma++);
2127 }
2128 }
2129 break;
2130 case TIFF_SSHORT:
2131 {
2132 int16* ma;
2133 int64* mb;
2134 uint32 n;
2135 ma=(int16*)origdata;
2136 mb=data;
2137 for (n=0; n<count; n++)
2138 {
2139 if (tif->tif_flags&TIFF_SWAB)
2140 TIFFSwabShort((uint16*)ma);
2141 *mb++=(int64)(*ma++);
2142 }
2143 }
2144 break;
2145 case TIFF_LONG:
2146 {
2147 uint32* ma;
2148 int64* mb;
2149 uint32 n;
2150 ma=(uint32*)origdata;
2151 mb=data;
2152 for (n=0; n<count; n++)
2153 {
2154 if (tif->tif_flags&TIFF_SWAB)
2155 TIFFSwabLong(ma);
2156 *mb++=(int64)(*ma++);
2157 }
2158 }
2159 break;
2160 case TIFF_SLONG:
2161 {
2162 int32* ma;
2163 int64* mb;
2164 uint32 n;
2165 ma=(int32*)origdata;
2166 mb=data;
2167 for (n=0; n<count; n++)
2168 {
2169 if (tif->tif_flags&TIFF_SWAB)
2170 TIFFSwabLong((uint32*)ma);
2171 *mb++=(int64)(*ma++);
2172 }
2173 }
2174 break;
2175 }
2176 _TIFFfree(origdata);
2177 *value=data;
2178 return(TIFFReadDirEntryErrOk);
2179 }
2180
2181 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2182 {
2183 enum TIFFReadDirEntryErr err;
2184 uint32 count;
2185 void* origdata;
2186 float* data;
2187 switch (direntry->tdir_type)
2188 {
2189 case TIFF_BYTE:
2190 case TIFF_SBYTE:
2191 case TIFF_SHORT:
2192 case TIFF_SSHORT:
2193 case TIFF_LONG:
2194 case TIFF_SLONG:
2195 case TIFF_LONG8:
2196 case TIFF_SLONG8:
2197 case TIFF_RATIONAL:
2198 case TIFF_SRATIONAL:
2199 case TIFF_FLOAT:
2200 case TIFF_DOUBLE:
2201 break;
2202 default:
2203 return(TIFFReadDirEntryErrType);
2204 }
2205 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2206 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2207 {
2208 *value=0;
2209 return(err);
2210 }
2211 switch (direntry->tdir_type)
2212 {
2213 case TIFF_FLOAT:
2214 if (tif->tif_flags&TIFF_SWAB)
2215 TIFFSwabArrayOfLong((uint32*)origdata,count);
2216 TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2217 *value=(float*)origdata;
2218 return(TIFFReadDirEntryErrOk);
2219 }
2220 data=(float*)_TIFFmalloc(count*sizeof(float));
2221 if (data==0)
2222 {
2223 _TIFFfree(origdata);
2224 return(TIFFReadDirEntryErrAlloc);
2225 }
2226 switch (direntry->tdir_type)
2227 {
2228 case TIFF_BYTE:
2229 {
2230 uint8* ma;
2231 float* mb;
2232 uint32 n;
2233 ma=(uint8*)origdata;
2234 mb=data;
2235 for (n=0; n<count; n++)
2236 *mb++=(float)(*ma++);
2237 }
2238 break;
2239 case TIFF_SBYTE:
2240 {
2241 int8* ma;
2242 float* mb;
2243 uint32 n;
2244 ma=(int8*)origdata;
2245 mb=data;
2246 for (n=0; n<count; n++)
2247 *mb++=(float)(*ma++);
2248 }
2249 break;
2250 case TIFF_SHORT:
2251 {
2252 uint16* ma;
2253 float* mb;
2254 uint32 n;
2255 ma=(uint16*)origdata;
2256 mb=data;
2257 for (n=0; n<count; n++)
2258 {
2259 if (tif->tif_flags&TIFF_SWAB)
2260 TIFFSwabShort(ma);
2261 *mb++=(float)(*ma++);
2262 }
2263 }
2264 break;
2265 case TIFF_SSHORT:
2266 {
2267 int16* ma;
2268 float* mb;
2269 uint32 n;
2270 ma=(int16*)origdata;
2271 mb=data;
2272 for (n=0; n<count; n++)
2273 {
2274 if (tif->tif_flags&TIFF_SWAB)
2275 TIFFSwabShort((uint16*)ma);
2276 *mb++=(float)(*ma++);
2277 }
2278 }
2279 break;
2280 case TIFF_LONG:
2281 {
2282 uint32* ma;
2283 float* mb;
2284 uint32 n;
2285 ma=(uint32*)origdata;
2286 mb=data;
2287 for (n=0; n<count; n++)
2288 {
2289 if (tif->tif_flags&TIFF_SWAB)
2290 TIFFSwabLong(ma);
2291 *mb++=(float)(*ma++);
2292 }
2293 }
2294 break;
2295 case TIFF_SLONG:
2296 {
2297 int32* ma;
2298 float* mb;
2299 uint32 n;
2300 ma=(int32*)origdata;
2301 mb=data;
2302 for (n=0; n<count; n++)
2303 {
2304 if (tif->tif_flags&TIFF_SWAB)
2305 TIFFSwabLong((uint32*)ma);
2306 *mb++=(float)(*ma++);
2307 }
2308 }
2309 break;
2310 case TIFF_LONG8:
2311 {
2312 uint64* ma;
2313 float* mb;
2314 uint32 n;
2315 ma=(uint64*)origdata;
2316 mb=data;
2317 for (n=0; n<count; n++)
2318 {
2319 if (tif->tif_flags&TIFF_SWAB)
2320 TIFFSwabLong8(ma);
2321 #if defined(__WIN32__) && (_MSC_VER < 1500)
2322 /*
2323 * XXX: MSVC 6.0 does not support
2324 * conversion of 64-bit integers into
2325 * floating point values.
2326 */
2327 *mb++ = _TIFFUInt64ToFloat(*ma++);
2328 #else
2329 *mb++ = (float)(*ma++);
2330 #endif
2331 }
2332 }
2333 break;
2334 case TIFF_SLONG8:
2335 {
2336 int64* ma;
2337 float* mb;
2338 uint32 n;
2339 ma=(int64*)origdata;
2340 mb=data;
2341 for (n=0; n<count; n++)
2342 {
2343 if (tif->tif_flags&TIFF_SWAB)
2344 TIFFSwabLong8((uint64*)ma);
2345 *mb++=(float)(*ma++);
2346 }
2347 }
2348 break;
2349 case TIFF_RATIONAL:
2350 {
2351 uint32* ma;
2352 uint32 maa;
2353 uint32 mab;
2354 float* mb;
2355 uint32 n;
2356 ma=(uint32*)origdata;
2357 mb=data;
2358 for (n=0; n<count; n++)
2359 {
2360 if (tif->tif_flags&TIFF_SWAB)
2361 TIFFSwabLong(ma);
2362 maa=*ma++;
2363 if (tif->tif_flags&TIFF_SWAB)
2364 TIFFSwabLong(ma);
2365 mab=*ma++;
2366 if (mab==0)
2367 *mb++=0.0;
2368 else
2369 *mb++=(float)maa/(float)mab;
2370 }
2371 }
2372 break;
2373 case TIFF_SRATIONAL:
2374 {
2375 uint32* ma;
2376 int32 maa;
2377 uint32 mab;
2378 float* mb;
2379 uint32 n;
2380 ma=(uint32*)origdata;
2381 mb=data;
2382 for (n=0; n<count; n++)
2383 {
2384 if (tif->tif_flags&TIFF_SWAB)
2385 TIFFSwabLong(ma);
2386 maa=*(int32*)ma;
2387 ma++;
2388 if (tif->tif_flags&TIFF_SWAB)
2389 TIFFSwabLong(ma);
2390 mab=*ma++;
2391 if (mab==0)
2392 *mb++=0.0;
2393 else
2394 *mb++=(float)maa/(float)mab;
2395 }
2396 }
2397 break;
2398 case TIFF_DOUBLE:
2399 {
2400 double* ma;
2401 float* mb;
2402 uint32 n;
2403 if (tif->tif_flags&TIFF_SWAB)
2404 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2405 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2406 ma=(double*)origdata;
2407 mb=data;
2408 for (n=0; n<count; n++)
2409 *mb++=(float)(*ma++);
2410 }
2411 break;
2412 }
2413 _TIFFfree(origdata);
2414 *value=data;
2415 return(TIFFReadDirEntryErrOk);
2416 }
2417
2418 static enum TIFFReadDirEntryErr
2419 TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2420 {
2421 enum TIFFReadDirEntryErr err;
2422 uint32 count;
2423 void* origdata;
2424 double* data;
2425 switch (direntry->tdir_type)
2426 {
2427 case TIFF_BYTE:
2428 case TIFF_SBYTE:
2429 case TIFF_SHORT:
2430 case TIFF_SSHORT:
2431 case TIFF_LONG:
2432 case TIFF_SLONG:
2433 case TIFF_LONG8:
2434 case TIFF_SLONG8:
2435 case TIFF_RATIONAL:
2436 case TIFF_SRATIONAL:
2437 case TIFF_FLOAT:
2438 case TIFF_DOUBLE:
2439 break;
2440 default:
2441 return(TIFFReadDirEntryErrType);
2442 }
2443 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2444 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2445 {
2446 *value=0;
2447 return(err);
2448 }
2449 switch (direntry->tdir_type)
2450 {
2451 case TIFF_DOUBLE:
2452 if (tif->tif_flags&TIFF_SWAB)
2453 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2454 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2455 *value=(double*)origdata;
2456 return(TIFFReadDirEntryErrOk);
2457 }
2458 data=(double*)_TIFFmalloc(count*sizeof(double));
2459 if (data==0)
2460 {
2461 _TIFFfree(origdata);
2462 return(TIFFReadDirEntryErrAlloc);
2463 }
2464 switch (direntry->tdir_type)
2465 {
2466 case TIFF_BYTE:
2467 {
2468 uint8* ma;
2469 double* mb;
2470 uint32 n;
2471 ma=(uint8*)origdata;
2472 mb=data;
2473 for (n=0; n<count; n++)
2474 *mb++=(double)(*ma++);
2475 }
2476 break;
2477 case TIFF_SBYTE:
2478 {
2479 int8* ma;
2480 double* mb;
2481 uint32 n;
2482 ma=(int8*)origdata;
2483 mb=data;
2484 for (n=0; n<count; n++)
2485 *mb++=(double)(*ma++);
2486 }
2487 break;
2488 case TIFF_SHORT:
2489 {
2490 uint16* ma;
2491 double* mb;
2492 uint32 n;
2493 ma=(uint16*)origdata;
2494 mb=data;
2495 for (n=0; n<count; n++)
2496 {
2497 if (tif->tif_flags&TIFF_SWAB)
2498 TIFFSwabShort(ma);
2499 *mb++=(double)(*ma++);
2500 }
2501 }
2502 break;
2503 case TIFF_SSHORT:
2504 {
2505 int16* ma;
2506 double* mb;
2507 uint32 n;
2508 ma=(int16*)origdata;
2509 mb=data;
2510 for (n=0; n<count; n++)
2511 {
2512 if (tif->tif_flags&TIFF_SWAB)
2513 TIFFSwabShort((uint16*)ma);
2514 *mb++=(double)(*ma++);
2515 }
2516 }
2517 break;
2518 case TIFF_LONG:
2519 {
2520 uint32* ma;
2521 double* mb;
2522 uint32 n;
2523 ma=(uint32*)origdata;
2524 mb=data;
2525 for (n=0; n<count; n++)
2526 {
2527 if (tif->tif_flags&TIFF_SWAB)
2528 TIFFSwabLong(ma);
2529 *mb++=(double)(*ma++);
2530 }
2531 }
2532 break;
2533 case TIFF_SLONG:
2534 {
2535 int32* ma;
2536 double* mb;
2537 uint32 n;
2538 ma=(int32*)origdata;
2539 mb=data;
2540 for (n=0; n<count; n++)
2541 {
2542 if (tif->tif_flags&TIFF_SWAB)
2543 TIFFSwabLong((uint32*)ma);
2544 *mb++=(double)(*ma++);
2545 }
2546 }
2547 break;
2548 case TIFF_LONG8:
2549 {
2550 uint64* ma;
2551 double* mb;
2552 uint32 n;
2553 ma=(uint64*)origdata;
2554 mb=data;
2555 for (n=0; n<count; n++)
2556 {
2557 if (tif->tif_flags&TIFF_SWAB)
2558 TIFFSwabLong8(ma);
2559 #if defined(__WIN32__) && (_MSC_VER < 1500)
2560 /*
2561 * XXX: MSVC 6.0 does not support
2562 * conversion of 64-bit integers into
2563 * floating point values.
2564 */
2565 *mb++ = _TIFFUInt64ToDouble(*ma++);
2566 #else
2567 *mb++ = (double)(*ma++);
2568 #endif
2569 }
2570 }
2571 break;
2572 case TIFF_SLONG8:
2573 {
2574 int64* ma;
2575 double* mb;
2576 uint32 n;
2577 ma=(int64*)origdata;
2578 mb=data;
2579 for (n=0; n<count; n++)
2580 {
2581 if (tif->tif_flags&TIFF_SWAB)
2582 TIFFSwabLong8((uint64*)ma);
2583 *mb++=(double)(*ma++);
2584 }
2585 }
2586 break;
2587 case TIFF_RATIONAL:
2588 {
2589 uint32* ma;
2590 uint32 maa;
2591 uint32 mab;
2592 double* mb;
2593 uint32 n;
2594 ma=(uint32*)origdata;
2595 mb=data;
2596 for (n=0; n<count; n++)
2597 {
2598 if (tif->tif_flags&TIFF_SWAB)
2599 TIFFSwabLong(ma);
2600 maa=*ma++;
2601 if (tif->tif_flags&TIFF_SWAB)
2602 TIFFSwabLong(ma);
2603 mab=*ma++;
2604 if (mab==0)
2605 *mb++=0.0;
2606 else
2607 *mb++=(double)maa/(double)mab;
2608 }
2609 }
2610 break;
2611 case TIFF_SRATIONAL:
2612 {
2613 uint32* ma;
2614 int32 maa;
2615 uint32 mab;
2616 double* mb;
2617 uint32 n;
2618 ma=(uint32*)origdata;
2619 mb=data;
2620 for (n=0; n<count; n++)
2621 {
2622 if (tif->tif_flags&TIFF_SWAB)
2623 TIFFSwabLong(ma);
2624 maa=*(int32*)ma;
2625 ma++;
2626 if (tif->tif_flags&TIFF_SWAB)
2627 TIFFSwabLong(ma);
2628 mab=*ma++;
2629 if (mab==0)
2630 *mb++=0.0;
2631 else
2632 *mb++=(double)maa/(double)mab;
2633 }
2634 }
2635 break;
2636 case TIFF_FLOAT:
2637 {
2638 float* ma;
2639 double* mb;
2640 uint32 n;
2641 if (tif->tif_flags&TIFF_SWAB)
2642 TIFFSwabArrayOfLong((uint32*)origdata,count);
2643 TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
2644 ma=(float*)origdata;
2645 mb=data;
2646 for (n=0; n<count; n++)
2647 *mb++=(double)(*ma++);
2648 }
2649 break;
2650 }
2651 _TIFFfree(origdata);
2652 *value=data;
2653 return(TIFFReadDirEntryErrOk);
2654 }
2655
2656 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
2657 {
2658 enum TIFFReadDirEntryErr err;
2659 uint32 count;
2660 void* origdata;
2661 uint64* data;
2662 switch (direntry->tdir_type)
2663 {
2664 case TIFF_LONG:
2665 case TIFF_LONG8:
2666 case TIFF_IFD:
2667 case TIFF_IFD8:
2668 break;
2669 default:
2670 return(TIFFReadDirEntryErrType);
2671 }
2672 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2673 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2674 {
2675 *value=0;
2676 return(err);
2677 }
2678 switch (direntry->tdir_type)
2679 {
2680 case TIFF_LONG8:
2681 case TIFF_IFD8:
2682 *value=(uint64*)origdata;
2683 if (tif->tif_flags&TIFF_SWAB)
2684 TIFFSwabArrayOfLong8(*value,count);
2685 return(TIFFReadDirEntryErrOk);
2686 }
2687 data=(uint64*)_TIFFmalloc(count*8);
2688 if (data==0)
2689 {
2690 _TIFFfree(origdata);
2691 return(TIFFReadDirEntryErrAlloc);
2692 }
2693 switch (direntry->tdir_type)
2694 {
2695 case TIFF_LONG:
2696 case TIFF_IFD:
2697 {
2698 uint32* ma;
2699 uint64* mb;
2700 uint32 n;
2701 ma=(uint32*)origdata;
2702 mb=data;
2703 for (n=0; n<count; n++)
2704 {
2705 if (tif->tif_flags&TIFF_SWAB)
2706 TIFFSwabLong(ma);
2707 *mb++=(uint64)(*ma++);
2708 }
2709 }
2710 break;
2711 }
2712 _TIFFfree(origdata);
2713 *value=data;
2714 return(TIFFReadDirEntryErrOk);
2715 }
2716
2717 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2718 {
2719 enum TIFFReadDirEntryErr err;
2720 uint16* m;
2721 uint16* na;
2722 uint16 nb;
2723 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2724 return(TIFFReadDirEntryErrCount);
2725 err=TIFFReadDirEntryShortArray(tif,direntry,&m);
2726 if (err!=TIFFReadDirEntryErrOk)
2727 return(err);
2728 na=m;
2729 nb=tif->tif_dir.td_samplesperpixel;
2730 *value=*na++;
2731 nb--;
2732 while (nb>0)
2733 {
2734 if (*na++!=*value)
2735 {
2736 err=TIFFReadDirEntryErrPsdif;
2737 break;
2738 }
2739 nb--;
2740 }
2741 _TIFFfree(m);
2742 return(err);
2743 }
2744
2745 #if 0
2746 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2747 {
2748 enum TIFFReadDirEntryErr err;
2749 double* m;
2750 double* na;
2751 uint16 nb;
2752 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2753 return(TIFFReadDirEntryErrCount);
2754 err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
2755 if (err!=TIFFReadDirEntryErrOk)
2756 return(err);
2757 na=m;
2758 nb=tif->tif_dir.td_samplesperpixel;
2759 *value=*na++;
2760 nb--;
2761 while (nb>0)
2762 {
2763 if (*na++!=*value)
2764 {
2765 err=TIFFReadDirEntryErrPsdif;
2766 break;
2767 }
2768 nb--;
2769 }
2770 _TIFFfree(m);
2771 return(err);
2772 }
2773 #endif
2774
2775 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
2776 {
2777 (void) tif;
2778 *value=*(uint8*)(&direntry->tdir_offset);
2779 }
2780
2781 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
2782 {
2783 (void) tif;
2784 *value=*(int8*)(&direntry->tdir_offset);
2785 }
2786
2787 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2788 {
2789 *value = direntry->tdir_offset.toff_short;
2790 /* *value=*(uint16*)(&direntry->tdir_offset); */
2791 if (tif->tif_flags&TIFF_SWAB)
2792 TIFFSwabShort(value);
2793 }
2794
2795 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
2796 {
2797 *value=*(int16*)(&direntry->tdir_offset);
2798 if (tif->tif_flags&TIFF_SWAB)
2799 TIFFSwabShort((uint16*)value);
2800 }
2801
2802 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
2803 {
2804 *value=*(uint32*)(&direntry->tdir_offset);
2805 if (tif->tif_flags&TIFF_SWAB)
2806 TIFFSwabLong(value);
2807 }
2808
2809 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
2810 {
2811 *value=*(int32*)(&direntry->tdir_offset);
2812 if (tif->tif_flags&TIFF_SWAB)
2813 TIFFSwabLong((uint32*)value);
2814 }
2815
2816 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
2817 {
2818 if (!(tif->tif_flags&TIFF_BIGTIFF))
2819 {
2820 enum TIFFReadDirEntryErr err;
2821 uint32 offset = direntry->tdir_offset.toff_long;
2822 if (tif->tif_flags&TIFF_SWAB)
2823 TIFFSwabLong(&offset);
2824 err=TIFFReadDirEntryData(tif,offset,8,value);
2825 if (err!=TIFFReadDirEntryErrOk)
2826 return(err);
2827 }
2828 else
2829 *value = direntry->tdir_offset.toff_long8;
2830 if (tif->tif_flags&TIFF_SWAB)
2831 TIFFSwabLong8(value);
2832 return(TIFFReadDirEntryErrOk);
2833 }
2834
2835 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
2836 {
2837 if (!(tif->tif_flags&TIFF_BIGTIFF))
2838 {
2839 enum TIFFReadDirEntryErr err;
2840 uint32 offset = direntry->tdir_offset.toff_long;
2841 if (tif->tif_flags&TIFF_SWAB)
2842 TIFFSwabLong(&offset);
2843 err=TIFFReadDirEntryData(tif,offset,8,value);
2844 if (err!=TIFFReadDirEntryErrOk)
2845 return(err);
2846 }
2847 else
2848 *value=*(int64*)(&direntry->tdir_offset);
2849 if (tif->tif_flags&TIFF_SWAB)
2850 TIFFSwabLong8((uint64*)value);
2851 return(TIFFReadDirEntryErrOk);
2852 }
2853
2854 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2855 {
2856 UInt64Aligned_t m;
2857
2858 assert(sizeof(double)==8);
2859 assert(sizeof(uint64)==8);
2860 assert(sizeof(uint32)==4);
2861 if (!(tif->tif_flags&TIFF_BIGTIFF))
2862 {
2863 enum TIFFReadDirEntryErr err;
2864 uint32 offset = direntry->tdir_offset.toff_long;
2865 if (tif->tif_flags&TIFF_SWAB)
2866 TIFFSwabLong(&offset);
2867 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2868 if (err!=TIFFReadDirEntryErrOk)
2869 return(err);
2870 }
2871 else
2872 m.l = direntry->tdir_offset.toff_long8;
2873 if (tif->tif_flags&TIFF_SWAB)
2874 TIFFSwabArrayOfLong(m.i,2);
2875 if (m.i[0]==0)
2876 *value=0.0;
2877 else
2878 *value=(double)m.i[0]/(double)m.i[1];
2879 return(TIFFReadDirEntryErrOk);
2880 }
2881
2882 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2883 {
2884 UInt64Aligned_t m;
2885 assert(sizeof(double)==8);
2886 assert(sizeof(uint64)==8);
2887 assert(sizeof(int32)==4);
2888 assert(sizeof(uint32)==4);
2889 if (!(tif->tif_flags&TIFF_BIGTIFF))
2890 {
2891 enum TIFFReadDirEntryErr err;
2892 uint32 offset = direntry->tdir_offset.toff_long;
2893 if (tif->tif_flags&TIFF_SWAB)
2894 TIFFSwabLong(&offset);
2895 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2896 if (err!=TIFFReadDirEntryErrOk)
2897 return(err);
2898 }
2899 else
2900 m.l=direntry->tdir_offset.toff_long8;
2901 if (tif->tif_flags&TIFF_SWAB)
2902 TIFFSwabArrayOfLong(m.i,2);
2903 if ((int32)m.i[0]==0)
2904 *value=0.0;
2905 else
2906 *value=(double)((int32)m.i[0])/(double)m.i[1];
2907 return(TIFFReadDirEntryErrOk);
2908 }
2909
2910 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
2911 {
2912 union
2913 {
2914 float f;
2915 uint32 i;
2916 } float_union;
2917 assert(sizeof(float)==4);
2918 assert(sizeof(uint32)==4);
2919 assert(sizeof(float_union)==4);
2920 float_union.i=*(uint32*)(&direntry->tdir_offset);
2921 *value=float_union.f;
2922 if (tif->tif_flags&TIFF_SWAB)
2923 TIFFSwabLong((uint32*)value);
2924 }
2925
2926 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2927 {
2928 assert(sizeof(double)==8);
2929 assert(sizeof(uint64)==8);
2930 assert(sizeof(UInt64Aligned_t)==8);
2931 if (!(tif->tif_flags&TIFF_BIGTIFF))
2932 {
2933 enum TIFFReadDirEntryErr err;
2934 uint32 offset = direntry->tdir_offset.toff_long;
2935 if (tif->tif_flags&TIFF_SWAB)
2936 TIFFSwabLong(&offset);
2937 err=TIFFReadDirEntryData(tif,offset,8,value);
2938 if (err!=TIFFReadDirEntryErrOk)
2939 return(err);
2940 }
2941 else
2942 {
2943 UInt64Aligned_t uint64_union;
2944 uint64_union.l=direntry->tdir_offset.toff_long8;
2945 *value=uint64_union.d;
2946 }
2947 if (tif->tif_flags&TIFF_SWAB)
2948 TIFFSwabLong8((uint64*)value);
2949 return(TIFFReadDirEntryErrOk);
2950 }
2951
2952 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
2953 {
2954 if (value<0)
2955 return(TIFFReadDirEntryErrRange);
2956 else
2957 return(TIFFReadDirEntryErrOk);
2958 }
2959
2960 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
2961 {
2962 if (value>0xFF)
2963 return(TIFFReadDirEntryErrRange);
2964 else
2965 return(TIFFReadDirEntryErrOk);
2966 }
2967
2968 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
2969 {
2970 if ((value<0)||(value>0xFF))
2971 return(TIFFReadDirEntryErrRange);
2972 else
2973 return(TIFFReadDirEntryErrOk);
2974 }
2975
2976 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
2977 {
2978 if (value>0xFF)
2979 return(TIFFReadDirEntryErrRange);
2980 else
2981 return(TIFFReadDirEntryErrOk);
2982 }
2983
2984 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
2985 {
2986 if ((value<0)||(value>0xFF))
2987 return(TIFFReadDirEntryErrRange);
2988 else
2989 return(TIFFReadDirEntryErrOk);
2990 }
2991
2992 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
2993 {
2994 if (value>0xFF)
2995 return(TIFFReadDirEntryErrRange);
2996 else
2997 return(TIFFReadDirEntryErrOk);
2998 }
2999
3000 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
3001 {
3002 if ((value<0)||(value>0xFF))
3003 return(TIFFReadDirEntryErrRange);
3004 else
3005 return(TIFFReadDirEntryErrOk);
3006 }
3007
3008 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
3009 {
3010 if (value>0x7F)
3011 return(TIFFReadDirEntryErrRange);
3012 else
3013 return(TIFFReadDirEntryErrOk);
3014 }
3015
3016 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
3017 {
3018 if (value>0x7F)
3019 return(TIFFReadDirEntryErrRange);
3020 else
3021 return(TIFFReadDirEntryErrOk);
3022 }
3023
3024 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
3025 {
3026 if ((value<-0x80)||(value>0x7F))
3027 return(TIFFReadDirEntryErrRange);
3028 else
3029 return(TIFFReadDirEntryErrOk);
3030 }
3031
3032 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
3033 {
3034 if (value>0x7F)
3035 return(TIFFReadDirEntryErrRange);
3036 else
3037 return(TIFFReadDirEntryErrOk);
3038 }
3039
3040 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
3041 {
3042 if ((value<-0x80)||(value>0x7F))
3043 return(TIFFReadDirEntryErrRange);
3044 else
3045 return(TIFFReadDirEntryErrOk);
3046 }
3047
3048 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
3049 {
3050 if (value>0x7F)
3051 return(TIFFReadDirEntryErrRange);
3052 else
3053 return(TIFFReadDirEntryErrOk);
3054 }
3055
3056 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
3057 {
3058 if ((value<-0x80)||(value>0x7F))
3059 return(TIFFReadDirEntryErrRange);
3060 else
3061 return(TIFFReadDirEntryErrOk);
3062 }
3063
3064 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
3065 {
3066 if (value<0)
3067 return(TIFFReadDirEntryErrRange);
3068 else
3069 return(TIFFReadDirEntryErrOk);
3070 }
3071
3072 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
3073 {
3074 if (value<0)
3075 return(TIFFReadDirEntryErrRange);
3076 else
3077 return(TIFFReadDirEntryErrOk);
3078 }
3079
3080 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
3081 {
3082 if (value>0xFFFF)
3083 return(TIFFReadDirEntryErrRange);
3084 else
3085 return(TIFFReadDirEntryErrOk);
3086 }
3087
3088 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
3089 {
3090 if ((value<0)||(value>0xFFFF))
3091 return(TIFFReadDirEntryErrRange);
3092 else
3093 return(TIFFReadDirEntryErrOk);
3094 }
3095
3096 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
3097 {
3098 if (value>0xFFFF)
3099 return(TIFFReadDirEntryErrRange);
3100 else
3101 return(TIFFReadDirEntryErrOk);
3102 }
3103
3104 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
3105 {
3106 if ((value<0)||(value>0xFFFF))
3107 return(TIFFReadDirEntryErrRange);
3108 else
3109 return(TIFFReadDirEntryErrOk);
3110 }
3111
3112 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
3113 {
3114 if (value>0x7FFF)
3115 return(TIFFReadDirEntryErrRange);
3116 else
3117 return(TIFFReadDirEntryErrOk);
3118 }
3119
3120 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
3121 {
3122 if (value>0x7FFF)
3123 return(TIFFReadDirEntryErrRange);
3124 else
3125 return(TIFFReadDirEntryErrOk);
3126 }
3127
3128 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
3129 {
3130 if ((value<-0x8000)||(value>0x7FFF))
3131 return(TIFFReadDirEntryErrRange);
3132 else
3133 return(TIFFReadDirEntryErrOk);
3134 }
3135
3136 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
3137 {
3138 if (value>0x7FFF)
3139 return(TIFFReadDirEntryErrRange);
3140 else
3141 return(TIFFReadDirEntryErrOk);
3142 }
3143
3144 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
3145 {
3146 if ((value<-0x8000)||(value>0x7FFF))
3147 return(TIFFReadDirEntryErrRange);
3148 else
3149 return(TIFFReadDirEntryErrOk);
3150 }
3151
3152 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
3153 {
3154 if (value<0)
3155 return(TIFFReadDirEntryErrRange);
3156 else
3157 return(TIFFReadDirEntryErrOk);
3158 }
3159
3160 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
3161 {
3162 if (value<0)
3163 return(TIFFReadDirEntryErrRange);
3164 else
3165 return(TIFFReadDirEntryErrOk);
3166 }
3167
3168 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
3169 {
3170 if (value<0)
3171 return(TIFFReadDirEntryErrRange);
3172 else
3173 return(TIFFReadDirEntryErrOk);
3174 }
3175
3176 /*
3177 * Largest 32-bit unsigned integer value.
3178 */
3179 #define TIFF_UINT32_MAX 0xFFFFFFFFU
3180
3181 static enum TIFFReadDirEntryErr
3182 TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
3183 {
3184 if (value > TIFF_UINT32_MAX)
3185 return(TIFFReadDirEntryErrRange);
3186 else
3187 return(TIFFReadDirEntryErrOk);
3188 }
3189
3190 static enum TIFFReadDirEntryErr
3191 TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
3192 {
3193 if ((value < 0) || (value > (int64) TIFF_UINT32_MAX))
3194 return(TIFFReadDirEntryErrRange);
3195 else
3196 return(TIFFReadDirEntryErrOk);
3197 }
3198
3199 #undef TIFF_UINT32_MAX
3200
3201 static enum TIFFReadDirEntryErr
3202 TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
3203 {
3204 if (value > 0x7FFFFFFFUL)
3205 return(TIFFReadDirEntryErrRange);
3206 else
3207 return(TIFFReadDirEntryErrOk);
3208 }
3209
3210 /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3211 static enum TIFFReadDirEntryErr
3212 TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
3213 {
3214 if (value > 0x7FFFFFFF)
3215 return(TIFFReadDirEntryErrRange);
3216 else
3217 return(TIFFReadDirEntryErrOk);
3218 }
3219
3220 /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3221 static enum TIFFReadDirEntryErr
3222 TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
3223 {
3224 if ((value < 0-((int64) 0x7FFFFFFF+1)) || (value > 0x7FFFFFFF))
3225 return(TIFFReadDirEntryErrRange);
3226 else
3227 return(TIFFReadDirEntryErrOk);
3228 }
3229
3230 static enum TIFFReadDirEntryErr
3231 TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
3232 {
3233 if (value < 0)
3234 return(TIFFReadDirEntryErrRange);
3235 else
3236 return(TIFFReadDirEntryErrOk);
3237 }
3238
3239 static enum TIFFReadDirEntryErr
3240 TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
3241 {
3242 if (value < 0)
3243 return(TIFFReadDirEntryErrRange);
3244 else
3245 return(TIFFReadDirEntryErrOk);
3246 }
3247
3248 static enum TIFFReadDirEntryErr
3249 TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
3250 {
3251 if (value < 0)
3252 return(TIFFReadDirEntryErrRange);
3253 else
3254 return(TIFFReadDirEntryErrOk);
3255 }
3256
3257 static enum TIFFReadDirEntryErr
3258 TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
3259 {
3260 if (value < 0)
3261 return(TIFFReadDirEntryErrRange);
3262 else
3263 return(TIFFReadDirEntryErrOk);
3264 }
3265
3266 /*
3267 * Largest 64-bit signed integer value.
3268 */
3269 #define TIFF_INT64_MAX ((int64)(((uint64) ~0) >> 1))
3270
3271 static enum TIFFReadDirEntryErr
3272 TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
3273 {
3274 if (value > TIFF_INT64_MAX)
3275 return(TIFFReadDirEntryErrRange);
3276 else
3277 return(TIFFReadDirEntryErrOk);
3278 }
3279
3280 #undef TIFF_INT64_MAX
3281
3282 static enum TIFFReadDirEntryErr
3283 TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
3284 {
3285 assert(size>0);
3286 if (!isMapped(tif)) {
3287 if (!SeekOK(tif,offset))
3288 return(TIFFReadDirEntryErrIo);
3289 if (!ReadOK(tif,dest,size))
3290 return(TIFFReadDirEntryErrIo);
3291 } else {
3292 size_t ma,mb;
3293 ma=(size_t)offset;
3294 mb=ma+size;
3295 if (((uint64)ma!=offset)
3296 || (mb < ma)
3297 || (mb - ma != (size_t) size)
3298 || (mb < (size_t)size)
3299 || (mb > (size_t)tif->tif_size)
3300 )
3301 return(TIFFReadDirEntryErrIo);
3302 _TIFFmemcpy(dest,tif->tif_base+ma,size);
3303 }
3304 return(TIFFReadDirEntryErrOk);
3305 }
3306
3307 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
3308 {
3309 if (!recover) {
3310 switch (err) {
3311 case TIFFReadDirEntryErrCount:
3312 TIFFErrorExt(tif->tif_clientdata, module,
3313 "Incorrect count for \"%s\"",
3314 tagname);
3315 break;
3316 case TIFFReadDirEntryErrType:
3317 TIFFErrorExt(tif->tif_clientdata, module,
3318 "Incompatible type for \"%s\"",
3319 tagname);
3320 break;
3321 case TIFFReadDirEntryErrIo:
3322 TIFFErrorExt(tif->tif_clientdata, module,
3323 "IO error during reading of \"%s\"",
3324 tagname);
3325 break;
3326 case TIFFReadDirEntryErrRange:
3327 TIFFErrorExt(tif->tif_clientdata, module,
3328 "Incorrect value for \"%s\"",
3329 tagname);
3330 break;
3331 case TIFFReadDirEntryErrPsdif:
3332 TIFFErrorExt(tif->tif_clientdata, module,
3333 "Cannot handle different values per sample for \"%s\"",
3334 tagname);
3335 break;
3336 case TIFFReadDirEntryErrSizesan:
3337 TIFFErrorExt(tif->tif_clientdata, module,
3338 "Sanity check on size of \"%s\" value failed",
3339 tagname);
3340 break;
3341 case TIFFReadDirEntryErrAlloc:
3342 TIFFErrorExt(tif->tif_clientdata, module,
3343 "Out of memory reading of \"%s\"",
3344 tagname);
3345 break;
3346 default:
3347 assert(0); /* we should never get here */
3348 break;
3349 }
3350 } else {
3351 switch (err) {
3352 case TIFFReadDirEntryErrCount:
3353 TIFFWarningExt(tif->tif_clientdata, module,
3354 "Incorrect count for \"%s\"; tag ignored",
3355 tagname);
3356 break;
3357 case TIFFReadDirEntryErrType:
3358 TIFFWarningExt(tif->tif_clientdata, module,
3359 "Incompatible type for \"%s\"; tag ignored",
3360 tagname);
3361 break;
3362 case TIFFReadDirEntryErrIo:
3363 TIFFWarningExt(tif->tif_clientdata, module,
3364 "IO error during reading of \"%s\"; tag ignored",
3365 tagname);
3366 break;
3367 case TIFFReadDirEntryErrRange:
3368 TIFFWarningExt(tif->tif_clientdata, module,
3369 "Incorrect value for \"%s\"; tag ignored",
3370 tagname);
3371 break;
3372 case TIFFReadDirEntryErrPsdif:
3373 TIFFWarningExt(tif->tif_clientdata, module,
3374 "Cannot handle different values per sample for \"%s\"; tag ignored",
3375 tagname);
3376 break;
3377 case TIFFReadDirEntryErrSizesan:
3378 TIFFWarningExt(tif->tif_clientdata, module,
3379 "Sanity check on size of \"%s\" value failed; tag ignored",
3380 tagname);
3381 break;
3382 case TIFFReadDirEntryErrAlloc:
3383 TIFFWarningExt(tif->tif_clientdata, module,
3384 "Out of memory reading of \"%s\"; tag ignored",
3385 tagname);
3386 break;
3387 default:
3388 assert(0); /* we should never get here */
3389 break;
3390 }
3391 }
3392 }
3393
3394 /*
3395 * Read the next TIFF directory from a file and convert it to the internal
3396 * format. We read directories sequentially.
3397 */
3398 int
3399 TIFFReadDirectory(TIFF* tif)
3400 {
3401 static const char module[] = "TIFFReadDirectory";
3402 TIFFDirEntry* dir;
3403 uint16 dircount;
3404 TIFFDirEntry* dp;
3405 uint16 di;
3406 const TIFFField* fip;
3407 uint32 fii=FAILED_FII;
3408 toff_t nextdiroff;
3409 int bitspersample_read = FALSE;
3410
3411 tif->tif_diroff=tif->tif_nextdiroff;
3412 if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
3413 return 0; /* last offset or bad offset (IFD looping) */
3414 (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
3415 tif->tif_curdir++;
3416 nextdiroff = tif->tif_nextdiroff;
3417 dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
3418 if (!dircount)
3419 {
3420 TIFFErrorExt(tif->tif_clientdata,module,
3421 "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
3422 return 0;
3423 }