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