]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/libtiff/tif_dirread.c
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / tiff / libtiff / tif_dirread.c
1
2 /*
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26 /*
27 * TIFF Library.
28 *
29 * Directory Read Support Routines.
30 */
31
32 /* Suggested pending improvements:
33 * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
34 * eliminating current use of the IGNORE value, and therefore eliminating
35 * current irrational behaviour on tags with tag id code 0
36 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
37 * the pointer to the appropriate TIFFField structure early on in
38 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
39 */
40
41 #include "tiffiop.h"
42
43 #define IGNORE 0 /* tag placeholder used below */
44 #define FAILED_FII ((uint32) -1)
45
46 #ifdef HAVE_IEEEFP
47 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
48 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
49 #else
50 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
51 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
52 #endif
53
54 enum TIFFReadDirEntryErr {
55 TIFFReadDirEntryErrOk = 0,
56 TIFFReadDirEntryErrCount = 1,
57 TIFFReadDirEntryErrType = 2,
58 TIFFReadDirEntryErrIo = 3,
59 TIFFReadDirEntryErrRange = 4,
60 TIFFReadDirEntryErrPsdif = 5,
61 TIFFReadDirEntryErrSizesan = 6,
62 TIFFReadDirEntryErrAlloc = 7,
63 };
64
65 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
66 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
67 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
68 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
69 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
70 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
71 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
72
73 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
74 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
75 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
76 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
77 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
78 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
79 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
80 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
81 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
82 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
83 static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
84 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
85
86 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
87 #if 0
88 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
89 #endif
90
91 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
92 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
93 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
94 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
95 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
96 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
97 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
98 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
99 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
100 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
101 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
102 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
103
104 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
105 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
106 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
107 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
108 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
109 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
110 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
111
112 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
113 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
114 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
115 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
116 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
117 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
118 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
119
120 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
121 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
122 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
123 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
124 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
125 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
126
127 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
128 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
129 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
130 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
131 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
132
133 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
134 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
135 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
136 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
137 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
138
139 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
140 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
141 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
142
143 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
144 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
145 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
146 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
147
148 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
149
150 static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
151 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
152
153 static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
154 static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
155 static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
156
157 static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
158 static void MissingRequired(TIFF*, const char*);
159 static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
160 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
161 static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
162 static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
163 static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
164 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
165 static void ChopUpSingleUncompressedStrip(TIFF*);
166 static uint64 TIFFReadUInt64(const uint8 *value);
167
168 typedef union _UInt64Aligned_t
169 {
170 double d;
171 uint64 l;
172 uint32 i[2];
173 uint16 s[4];
174 uint8 c[8];
175 } UInt64Aligned_t;
176
177 /*
178 Unaligned safe copy of a uint64 value from an octet array.
179 */
180 static uint64 TIFFReadUInt64(const uint8 *value)
181 {
182 UInt64Aligned_t result;
183
184 result.c[0]=value[0];
185 result.c[1]=value[1];
186 result.c[2]=value[2];
187 result.c[3]=value[3];
188 result.c[4]=value[4];
189 result.c[5]=value[5];
190 result.c[6]=value[6];
191 result.c[7]=value[7];
192
193 return result.l;
194 }
195
196 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
197 {
198 enum TIFFReadDirEntryErr err;
199 if (direntry->tdir_count!=1)
200 return(TIFFReadDirEntryErrCount);
201 switch (direntry->tdir_type)
202 {
203 case TIFF_BYTE:
204 TIFFReadDirEntryCheckedByte(tif,direntry,value);
205 return(TIFFReadDirEntryErrOk);
206 case TIFF_SBYTE:
207 {
208 int8 m;
209 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
210 err=TIFFReadDirEntryCheckRangeByteSbyte(m);
211 if (err!=TIFFReadDirEntryErrOk)
212 return(err);
213 *value=(uint8)m;
214 return(TIFFReadDirEntryErrOk);
215 }
216 case TIFF_SHORT:
217 {
218 uint16 m;
219 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
220 err=TIFFReadDirEntryCheckRangeByteShort(m);
221 if (err!=TIFFReadDirEntryErrOk)
222 return(err);
223 *value=(uint8)m;
224 return(TIFFReadDirEntryErrOk);
225 }
226 case TIFF_SSHORT:
227 {
228 int16 m;
229 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
230 err=TIFFReadDirEntryCheckRangeByteSshort(m);
231 if (err!=TIFFReadDirEntryErrOk)
232 return(err);
233 *value=(uint8)m;
234 return(TIFFReadDirEntryErrOk);
235 }
236 case TIFF_LONG:
237 {
238 uint32 m;
239 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
240 err=TIFFReadDirEntryCheckRangeByteLong(m);
241 if (err!=TIFFReadDirEntryErrOk)
242 return(err);
243 *value=(uint8)m;
244 return(TIFFReadDirEntryErrOk);
245 }
246 case TIFF_SLONG:
247 {
248 int32 m;
249 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
250 err=TIFFReadDirEntryCheckRangeByteSlong(m);
251 if (err!=TIFFReadDirEntryErrOk)
252 return(err);
253 *value=(uint8)m;
254 return(TIFFReadDirEntryErrOk);
255 }
256 case TIFF_LONG8:
257 {
258 uint64 m;
259 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
260 if (err!=TIFFReadDirEntryErrOk)
261 return(err);
262 err=TIFFReadDirEntryCheckRangeByteLong8(m);
263 if (err!=TIFFReadDirEntryErrOk)
264 return(err);
265 *value=(uint8)m;
266 return(TIFFReadDirEntryErrOk);
267 }
268 case TIFF_SLONG8:
269 {
270 int64 m;
271 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
272 if (err!=TIFFReadDirEntryErrOk)
273 return(err);
274 err=TIFFReadDirEntryCheckRangeByteSlong8(m);
275 if (err!=TIFFReadDirEntryErrOk)
276 return(err);
277 *value=(uint8)m;
278 return(TIFFReadDirEntryErrOk);
279 }
280 default:
281 return(TIFFReadDirEntryErrType);
282 }
283 }
284
285 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
286 {
287 enum TIFFReadDirEntryErr err;
288 if (direntry->tdir_count!=1)
289 return(TIFFReadDirEntryErrCount);
290 switch (direntry->tdir_type)
291 {
292 case TIFF_BYTE:
293 {
294 uint8 m;
295 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
296 *value=(uint16)m;
297 return(TIFFReadDirEntryErrOk);
298 }
299 case TIFF_SBYTE:
300 {
301 int8 m;
302 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
303 err=TIFFReadDirEntryCheckRangeShortSbyte(m);
304 if (err!=TIFFReadDirEntryErrOk)
305 return(err);
306 *value=(uint16)m;
307 return(TIFFReadDirEntryErrOk);
308 }
309 case TIFF_SHORT:
310 TIFFReadDirEntryCheckedShort(tif,direntry,value);
311 return(TIFFReadDirEntryErrOk);
312 case TIFF_SSHORT:
313 {
314 int16 m;
315 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
316 err=TIFFReadDirEntryCheckRangeShortSshort(m);
317 if (err!=TIFFReadDirEntryErrOk)
318 return(err);
319 *value=(uint16)m;
320 return(TIFFReadDirEntryErrOk);
321 }
322 case TIFF_LONG:
323 {
324 uint32 m;
325 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
326 err=TIFFReadDirEntryCheckRangeShortLong(m);
327 if (err!=TIFFReadDirEntryErrOk)
328 return(err);
329 *value=(uint16)m;
330 return(TIFFReadDirEntryErrOk);
331 }
332 case TIFF_SLONG:
333 {
334 int32 m;
335 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
336 err=TIFFReadDirEntryCheckRangeShortSlong(m);
337 if (err!=TIFFReadDirEntryErrOk)
338 return(err);
339 *value=(uint16)m;
340 return(TIFFReadDirEntryErrOk);
341 }
342 case TIFF_LONG8:
343 {
344 uint64 m;
345 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
346 if (err!=TIFFReadDirEntryErrOk)
347 return(err);
348 err=TIFFReadDirEntryCheckRangeShortLong8(m);
349 if (err!=TIFFReadDirEntryErrOk)
350 return(err);
351 *value=(uint16)m;
352 return(TIFFReadDirEntryErrOk);
353 }
354 case TIFF_SLONG8:
355 {
356 int64 m;
357 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
358 if (err!=TIFFReadDirEntryErrOk)
359 return(err);
360 err=TIFFReadDirEntryCheckRangeShortSlong8(m);
361 if (err!=TIFFReadDirEntryErrOk)
362 return(err);
363 *value=(uint16)m;
364 return(TIFFReadDirEntryErrOk);
365 }
366 default:
367 return(TIFFReadDirEntryErrType);
368 }
369 }
370
371 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
372 {
373 enum TIFFReadDirEntryErr err;
374 if (direntry->tdir_count!=1)
375 return(TIFFReadDirEntryErrCount);
376 switch (direntry->tdir_type)
377 {
378 case TIFF_BYTE:
379 {
380 uint8 m;
381 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
382 *value=(uint32)m;
383 return(TIFFReadDirEntryErrOk);
384 }
385 case TIFF_SBYTE:
386 {
387 int8 m;
388 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
389 err=TIFFReadDirEntryCheckRangeLongSbyte(m);
390 if (err!=TIFFReadDirEntryErrOk)
391 return(err);
392 *value=(uint32)m;
393 return(TIFFReadDirEntryErrOk);
394 }
395 case TIFF_SHORT:
396 {
397 uint16 m;
398 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
399 *value=(uint32)m;
400 return(TIFFReadDirEntryErrOk);
401 }
402 case TIFF_SSHORT:
403 {
404 int16 m;
405 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
406 err=TIFFReadDirEntryCheckRangeLongSshort(m);
407 if (err!=TIFFReadDirEntryErrOk)
408 return(err);
409 *value=(uint32)m;
410 return(TIFFReadDirEntryErrOk);
411 }
412 case TIFF_LONG:
413 TIFFReadDirEntryCheckedLong(tif,direntry,value);
414 return(TIFFReadDirEntryErrOk);
415 case TIFF_SLONG:
416 {
417 int32 m;
418 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
419 err=TIFFReadDirEntryCheckRangeLongSlong(m);
420 if (err!=TIFFReadDirEntryErrOk)
421 return(err);
422 *value=(uint32)m;
423 return(TIFFReadDirEntryErrOk);
424 }
425 case TIFF_LONG8:
426 {
427 uint64 m;
428 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
429 if (err!=TIFFReadDirEntryErrOk)
430 return(err);
431 err=TIFFReadDirEntryCheckRangeLongLong8(m);
432 if (err!=TIFFReadDirEntryErrOk)
433 return(err);
434 *value=(uint32)m;
435 return(TIFFReadDirEntryErrOk);
436 }
437 case TIFF_SLONG8:
438 {
439 int64 m;
440 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
441 if (err!=TIFFReadDirEntryErrOk)
442 return(err);
443 err=TIFFReadDirEntryCheckRangeLongSlong8(m);
444 if (err!=TIFFReadDirEntryErrOk)
445 return(err);
446 *value=(uint32)m;
447 return(TIFFReadDirEntryErrOk);
448 }
449 default:
450 return(TIFFReadDirEntryErrType);
451 }
452 }
453
454 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
455 {
456 enum TIFFReadDirEntryErr err;
457 if (direntry->tdir_count!=1)
458 return(TIFFReadDirEntryErrCount);
459 switch (direntry->tdir_type)
460 {
461 case TIFF_BYTE:
462 {
463 uint8 m;
464 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
465 *value=(uint64)m;
466 return(TIFFReadDirEntryErrOk);
467 }
468 case TIFF_SBYTE:
469 {
470 int8 m;
471 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
472 err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
473 if (err!=TIFFReadDirEntryErrOk)
474 return(err);
475 *value=(uint64)m;
476 return(TIFFReadDirEntryErrOk);
477 }
478 case TIFF_SHORT:
479 {
480 uint16 m;
481 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
482 *value=(uint64)m;
483 return(TIFFReadDirEntryErrOk);
484 }
485 case TIFF_SSHORT:
486 {
487 int16 m;
488 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
489 err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
490 if (err!=TIFFReadDirEntryErrOk)
491 return(err);
492 *value=(uint64)m;
493 return(TIFFReadDirEntryErrOk);
494 }
495 case TIFF_LONG:
496 {
497 uint32 m;
498 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
499 *value=(uint64)m;
500 return(TIFFReadDirEntryErrOk);
501 }
502 case TIFF_SLONG:
503 {
504 int32 m;
505 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
506 err=TIFFReadDirEntryCheckRangeLong8Slong(m);
507 if (err!=TIFFReadDirEntryErrOk)
508 return(err);
509 *value=(uint64)m;
510 return(TIFFReadDirEntryErrOk);
511 }
512 case TIFF_LONG8:
513 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
514 return(err);
515 case TIFF_SLONG8:
516 {
517 int64 m;
518 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
519 if (err!=TIFFReadDirEntryErrOk)
520 return(err);
521 err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
522 if (err!=TIFFReadDirEntryErrOk)
523 return(err);
524 *value=(uint64)m;
525 return(TIFFReadDirEntryErrOk);
526 }
527 default:
528 return(TIFFReadDirEntryErrType);
529 }
530 }
531
532 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
533 {
534 enum TIFFReadDirEntryErr err;
535 if (direntry->tdir_count!=1)
536 return(TIFFReadDirEntryErrCount);
537 switch (direntry->tdir_type)
538 {
539 case TIFF_BYTE:
540 {
541 uint8 m;
542 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
543 *value=(float)m;
544 return(TIFFReadDirEntryErrOk);
545 }
546 case TIFF_SBYTE:
547 {
548 int8 m;
549 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
550 *value=(float)m;
551 return(TIFFReadDirEntryErrOk);
552 }
553 case TIFF_SHORT:
554 {
555 uint16 m;
556 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
557 *value=(float)m;
558 return(TIFFReadDirEntryErrOk);
559 }
560 case TIFF_SSHORT:
561 {
562 int16 m;
563 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
564 *value=(float)m;
565 return(TIFFReadDirEntryErrOk);
566 }
567 case TIFF_LONG:
568 {
569 uint32 m;
570 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
571 *value=(float)m;
572 return(TIFFReadDirEntryErrOk);
573 }
574 case TIFF_SLONG:
575 {
576 int32 m;
577 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
578 *value=(float)m;
579 return(TIFFReadDirEntryErrOk);
580 }
581 case TIFF_LONG8:
582 {
583 uint64 m;
584 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
585 if (err!=TIFFReadDirEntryErrOk)
586 return(err);
587 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
588 /*
589 * XXX: MSVC 6.0 does not support conversion
590 * of 64-bit integers into floating point
591 * values.
592 */
593 *value = _TIFFUInt64ToFloat(m);
594 #else
595 *value=(float)m;
596 #endif
597 return(TIFFReadDirEntryErrOk);
598 }
599 case TIFF_SLONG8:
600 {
601 int64 m;
602 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
603 if (err!=TIFFReadDirEntryErrOk)
604 return(err);
605 *value=(float)m;
606 return(TIFFReadDirEntryErrOk);
607 }
608 case TIFF_RATIONAL:
609 {
610 double m;
611 err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
612 if (err!=TIFFReadDirEntryErrOk)
613 return(err);
614 *value=(float)m;
615 return(TIFFReadDirEntryErrOk);
616 }
617 case TIFF_SRATIONAL:
618 {
619 double m;
620 err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
621 if (err!=TIFFReadDirEntryErrOk)
622 return(err);
623 *value=(float)m;
624 return(TIFFReadDirEntryErrOk);
625 }
626 case TIFF_FLOAT:
627 TIFFReadDirEntryCheckedFloat(tif,direntry,value);
628 return(TIFFReadDirEntryErrOk);
629 case TIFF_DOUBLE:
630 {
631 double m;
632 err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
633 if (err!=TIFFReadDirEntryErrOk)
634 return(err);
635 *value=(float)m;
636 return(TIFFReadDirEntryErrOk);
637 }
638 default:
639 return(TIFFReadDirEntryErrType);
640 }
641 }
642
643 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
644 {
645 enum TIFFReadDirEntryErr err;
646 if (direntry->tdir_count!=1)
647 return(TIFFReadDirEntryErrCount);
648 switch (direntry->tdir_type)
649 {
650 case TIFF_BYTE:
651 {
652 uint8 m;
653 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
654 *value=(double)m;
655 return(TIFFReadDirEntryErrOk);
656 }
657 case TIFF_SBYTE:
658 {
659 int8 m;
660 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
661 *value=(double)m;
662 return(TIFFReadDirEntryErrOk);
663 }
664 case TIFF_SHORT:
665 {
666 uint16 m;
667 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
668 *value=(double)m;
669 return(TIFFReadDirEntryErrOk);
670 }
671 case TIFF_SSHORT:
672 {
673 int16 m;
674 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
675 *value=(double)m;
676 return(TIFFReadDirEntryErrOk);
677 }
678 case TIFF_LONG:
679 {
680 uint32 m;
681 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
682 *value=(double)m;
683 return(TIFFReadDirEntryErrOk);
684 }
685 case TIFF_SLONG:
686 {
687 int32 m;
688 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
689 *value=(double)m;
690 return(TIFFReadDirEntryErrOk);
691 }
692 case TIFF_LONG8:
693 {
694 uint64 m;
695 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
696 if (err!=TIFFReadDirEntryErrOk)
697 return(err);
698 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
699 /*
700 * XXX: MSVC 6.0 does not support conversion
701 * of 64-bit integers into floating point
702 * values.
703 */
704 *value = _TIFFUInt64ToDouble(m);
705 #else
706 *value = (double)m;
707 #endif
708 return(TIFFReadDirEntryErrOk);
709 }
710 case TIFF_SLONG8:
711 {
712 int64 m;
713 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
714 if (err!=TIFFReadDirEntryErrOk)
715 return(err);
716 *value=(double)m;
717 return(TIFFReadDirEntryErrOk);
718 }
719 case TIFF_RATIONAL:
720 err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
721 return(err);
722 case TIFF_SRATIONAL:
723 err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
724 return(err);
725 case TIFF_FLOAT:
726 {
727 float m;
728 TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
729 *value=(double)m;
730 return(TIFFReadDirEntryErrOk);
731 }
732 case TIFF_DOUBLE:
733 err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
734 return(err);
735 default:
736 return(TIFFReadDirEntryErrType);
737 }
738 }
739
740 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
741 {
742 enum TIFFReadDirEntryErr err;
743 if (direntry->tdir_count!=1)
744 return(TIFFReadDirEntryErrCount);
745 switch (direntry->tdir_type)
746 {
747 case TIFF_LONG:
748 case TIFF_IFD:
749 {
750 uint32 m;
751 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
752 *value=(uint64)m;
753 return(TIFFReadDirEntryErrOk);
754 }
755 case TIFF_LONG8:
756 case TIFF_IFD8:
757 err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
758 return(err);
759 default:
760 return(TIFFReadDirEntryErrType);
761 }
762 }
763
764 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
765 {
766 int typesize;
767 uint32 datasize;
768 void* data;
769 typesize=TIFFDataWidth(direntry->tdir_type);
770 if ((direntry->tdir_count==0)||(typesize==0))
771 {
772 *value=0;
773 return(TIFFReadDirEntryErrOk);
774 }
775 (void) desttypesize;
776
777 /*
778 * As a sanity check, make sure we have no more than a 2GB tag array
779 * in either the current data type or the dest data type. This also
780 * avoids problems with overflow of tmsize_t on 32bit systems.
781 */
782 if ((uint64)(2147483647/typesize)<direntry->tdir_count)
783 return(TIFFReadDirEntryErrSizesan);
784 if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
785 return(TIFFReadDirEntryErrSizesan);
786
787 *count=(uint32)direntry->tdir_count;
788 datasize=(*count)*typesize;
789 assert((tmsize_t)datasize>0);
790 data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
791 if (data==0)
792 return(TIFFReadDirEntryErrAlloc);
793 if (!(tif->tif_flags&TIFF_BIGTIFF))
794 {
795 if (datasize<=4)
796 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
797 else
798 {
799 enum TIFFReadDirEntryErr err;
800 uint32 offset = direntry->tdir_offset.toff_long;
801 if (tif->tif_flags&TIFF_SWAB)
802 TIFFSwabLong(&offset);
803 err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
804 if (err!=TIFFReadDirEntryErrOk)
805 {
806 _TIFFfree(data);
807 return(err);
808 }
809 }
810 }
811 else
812 {
813 if (datasize<=8)
814 _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
815 else
816 {
817 enum TIFFReadDirEntryErr err;
818 uint64 offset = direntry->tdir_offset.toff_long8;
819 if (tif->tif_flags&TIFF_SWAB)
820 TIFFSwabLong8(&offset);
821 err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
822 if (err!=TIFFReadDirEntryErrOk)
823 {
824 _TIFFfree(data);
825 return(err);
826 }
827 }
828 }
829 *value=data;
830 return(TIFFReadDirEntryErrOk);
831 }
832
833 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
834 {
835 enum TIFFReadDirEntryErr err;
836 uint32 count;
837 void* origdata;
838 uint8* data;
839 switch (direntry->tdir_type)
840 {
841 case TIFF_ASCII:
842 case TIFF_UNDEFINED:
843 case TIFF_BYTE:
844 case TIFF_SBYTE:
845 case TIFF_SHORT:
846 case TIFF_SSHORT:
847 case TIFF_LONG:
848 case TIFF_SLONG:
849 case TIFF_LONG8:
850 case TIFF_SLONG8:
851 break;
852 default:
853 return(TIFFReadDirEntryErrType);
854 }
855 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
856 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
857 {
858 *value=0;
859 return(err);
860 }
861 switch (direntry->tdir_type)
862 {
863 case TIFF_ASCII:
864 case TIFF_UNDEFINED:
865 case TIFF_BYTE:
866 *value=(uint8*)origdata;
867 return(TIFFReadDirEntryErrOk);
868 case TIFF_SBYTE:
869 {
870 int8* m;
871 uint32 n;
872 m=(int8*)origdata;
873 for (n=0; n<count; n++)
874 {
875 err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
876 if (err!=TIFFReadDirEntryErrOk)
877 {
878 _TIFFfree(origdata);
879 return(err);
880 }
881 m++;
882 }
883 *value=(uint8*)origdata;
884 return(TIFFReadDirEntryErrOk);
885 }
886 }
887 data=(uint8*)_TIFFmalloc(count);
888 if (data==0)
889 {
890 _TIFFfree(origdata);
891 return(TIFFReadDirEntryErrAlloc);
892 }
893 switch (direntry->tdir_type)
894 {
895 case TIFF_SHORT:
896 {
897 uint16* ma;
898 uint8* mb;
899 uint32 n;
900 ma=(uint16*)origdata;
901 mb=data;
902 for (n=0; n<count; n++)
903 {
904 if (tif->tif_flags&TIFF_SWAB)
905 TIFFSwabShort(ma);
906 err=TIFFReadDirEntryCheckRangeByteShort(*ma);
907 if (err!=TIFFReadDirEntryErrOk)
908 break;
909 *mb++=(uint8)(*ma++);
910 }
911 }
912 break;
913 case TIFF_SSHORT:
914 {
915 int16* ma;
916 uint8* mb;
917 uint32 n;
918 ma=(int16*)origdata;
919 mb=data;
920 for (n=0; n<count; n++)
921 {
922 if (tif->tif_flags&TIFF_SWAB)
923 TIFFSwabShort((uint16*)ma);
924 err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
925 if (err!=TIFFReadDirEntryErrOk)
926 break;
927 *mb++=(uint8)(*ma++);
928 }
929 }
930 break;
931 case TIFF_LONG:
932 {
933 uint32* ma;
934 uint8* mb;
935 uint32 n;
936 ma=(uint32*)origdata;
937 mb=data;
938 for (n=0; n<count; n++)
939 {
940 if (tif->tif_flags&TIFF_SWAB)
941 TIFFSwabLong(ma);
942 err=TIFFReadDirEntryCheckRangeByteLong(*ma);
943 if (err!=TIFFReadDirEntryErrOk)
944 break;
945 *mb++=(uint8)(*ma++);
946 }
947 }
948 break;
949 case TIFF_SLONG:
950 {
951 int32* ma;
952 uint8* mb;
953 uint32 n;
954 ma=(int32*)origdata;
955 mb=data;
956 for (n=0; n<count; n++)
957 {
958 if (tif->tif_flags&TIFF_SWAB)
959 TIFFSwabLong((uint32*)ma);
960 err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
961 if (err!=TIFFReadDirEntryErrOk)
962 break;
963 *mb++=(uint8)(*ma++);
964 }
965 }
966 break;
967 case TIFF_LONG8:
968 {
969 uint64* ma;
970 uint8* mb;
971 uint32 n;
972 ma=(uint64*)origdata;
973 mb=data;
974 for (n=0; n<count; n++)
975 {
976 if (tif->tif_flags&TIFF_SWAB)
977 TIFFSwabLong8(ma);
978 err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
979 if (err!=TIFFReadDirEntryErrOk)
980 break;
981 *mb++=(uint8)(*ma++);
982 }
983 }
984 break;
985 case TIFF_SLONG8:
986 {
987 int64* ma;
988 uint8* mb;
989 uint32 n;
990 ma=(int64*)origdata;
991 mb=data;
992 for (n=0; n<count; n++)
993 {
994 if (tif->tif_flags&TIFF_SWAB)
995 TIFFSwabLong8((uint64*)ma);
996 err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
997 if (err!=TIFFReadDirEntryErrOk)
998 break;
999 *mb++=(uint8)(*ma++);
1000 }
1001 }
1002 break;
1003 }
1004 _TIFFfree(origdata);
1005 if (err!=TIFFReadDirEntryErrOk)
1006 {
1007 _TIFFfree(data);
1008 return(err);
1009 }
1010 *value=data;
1011 return(TIFFReadDirEntryErrOk);
1012 }
1013
1014 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
1015 {
1016 enum TIFFReadDirEntryErr err;
1017 uint32 count;
1018 void* origdata;
1019 int8* data;
1020 switch (direntry->tdir_type)
1021 {
1022 case TIFF_UNDEFINED:
1023 case TIFF_BYTE:
1024 case TIFF_SBYTE:
1025 case TIFF_SHORT:
1026 case TIFF_SSHORT:
1027 case TIFF_LONG:
1028 case TIFF_SLONG:
1029 case TIFF_LONG8:
1030 case TIFF_SLONG8:
1031 break;
1032 default:
1033 return(TIFFReadDirEntryErrType);
1034 }
1035 err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1036 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1037 {
1038 *value=0;
1039 return(err);
1040 }
1041 switch (direntry->tdir_type)
1042 {
1043 case TIFF_UNDEFINED:
1044 case TIFF_BYTE:
1045 {
1046 uint8* m;
1047 uint32 n;
1048 m=(uint8*)origdata;
1049 for (n=0; n<count; n++)
1050 {
1051 err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1052 if (err!=TIFFReadDirEntryErrOk)
1053 {
1054 _TIFFfree(origdata);
1055 return(err);
1056 }
1057 m++;
1058 }
1059 *value=(int8*)origdata;
1060 return(TIFFReadDirEntryErrOk);
1061 }
1062 case TIFF_SBYTE:
1063 *value=(int8*)origdata;
1064 return(TIFFReadDirEntryErrOk);
1065 }
1066 data=(int8*)_TIFFmalloc(count);
1067 if (data==0)
1068 {
1069 _TIFFfree(origdata);
1070 return(TIFFReadDirEntryErrAlloc);
1071 }
1072 switch (direntry->tdir_type)
1073 {
1074 case TIFF_SHORT:
1075 {
1076 uint16* ma;
1077 int8* mb;
1078 uint32 n;
1079 ma=(uint16*)origdata;
1080 mb=data;
1081 for (n=0; n<count; n++)
1082 {
1083 if (tif->tif_flags&TIFF_SWAB)
1084 TIFFSwabShort(ma);
1085 err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1086 if (err!=TIFFReadDirEntryErrOk)
1087 break;
1088 *mb++=(int8)(*ma++);
1089 }
1090 }
1091 break;
1092 case TIFF_SSHORT:
1093 {
1094 int16* ma;
1095 int8* mb;
1096 uint32 n;
1097 ma=(int16*)origdata;
1098 mb=data;
1099 for (n=0; n<count; n++)
1100 {
1101 if (tif->tif_flags&TIFF_SWAB)
1102 TIFFSwabShort((uint16*)ma);
1103 err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1104 if (err!=TIFFReadDirEntryErrOk)
1105 break;
1106 *mb++=(int8)(*ma++);
1107 }
1108 }
1109 break;
1110 case TIFF_LONG:
1111 {
1112 uint32* ma;
1113 int8* mb;
1114 uint32 n;
1115 ma=(uint32*)origdata;
1116 mb=data;
1117 for (n=0; n<count; n++)
1118 {
1119 if (tif->tif_flags&TIFF_SWAB)
1120 TIFFSwabLong(ma);
1121 err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1122 if (err!=TIFFReadDirEntryErrOk)
1123 break;
1124 *mb++=(int8)(*ma++);
1125 }
1126 }
1127 break;
1128 case TIFF_SLONG:
1129 {
1130 int32* ma;
1131 int8* mb;
1132 uint32 n;
1133 ma=(int32*)origdata;
1134 mb=data;
1135 for (n=0; n<count; n++)
1136 {
1137 if (tif->tif_flags&TIFF_SWAB)
1138 TIFFSwabLong((uint32*)ma);
1139 err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1140 if (err!=TIFFReadDirEntryErrOk)
1141 break;
1142 *mb++=(int8)(*ma++);
1143 }
1144 }
1145 break;
1146 case TIFF_LONG8:
1147 {
1148 uint64* ma;
1149 int8* mb;
1150 uint32 n;
1151 ma=(uint64*)origdata;
1152 mb=data;
1153 for (n=0; n<count; n++)
1154 {
1155 if (tif->tif_flags&TIFF_SWAB)
1156 TIFFSwabLong8(ma);
1157 err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1158 if (err!=TIFFReadDirEntryErrOk)
1159 break;
1160 *mb++=(int8)(*ma++);
1161 }
1162 }
1163 break;
1164 case TIFF_SLONG8:
1165 {
1166 int64* ma;
1167 int8* mb;
1168 uint32 n;
1169 ma=(int64*)origdata;
1170 mb=data;
1171 for (n=0; n<count; n++)
1172 {
1173 if (tif->tif_flags&TIFF_SWAB)
1174 TIFFSwabLong8((uint64*)ma);
1175 err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1176 if (err!=TIFFReadDirEntryErrOk)
1177 break;
1178 *mb++=(int8)(*ma++);
1179 }
1180 }
1181 break;
1182 }
1183 _TIFFfree(origdata);
1184 if (err!=TIFFReadDirEntryErrOk)
1185 {
1186 _TIFFfree(data);
1187 return(err);
1188 }
1189 *value=data;
1190 return(TIFFReadDirEntryErrOk);
1191 }
1192
1193 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
1194 {
1195 enum TIFFReadDirEntryErr err;
1196 uint32 count;
1197 void* origdata;
1198 uint16* data;
1199 switch (direntry->tdir_type)
1200 {
1201 case TIFF_BYTE:
1202 case TIFF_SBYTE:
1203 case TIFF_SHORT:
1204 case TIFF_SSHORT:
1205 case TIFF_LONG:
1206 case TIFF_SLONG:
1207 case TIFF_LONG8:
1208 case TIFF_SLONG8:
1209 break;
1210 default:
1211 return(TIFFReadDirEntryErrType);
1212 }
1213 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1214 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1215 {
1216 *value=0;
1217 return(err);
1218 }
1219 switch (direntry->tdir_type)
1220 {
1221 case TIFF_SHORT:
1222 *value=(uint16*)origdata;
1223 if (tif->tif_flags&TIFF_SWAB)
1224 TIFFSwabArrayOfShort(*value,count);
1225 return(TIFFReadDirEntryErrOk);
1226 case TIFF_SSHORT:
1227 {
1228 int16* m;
1229 uint32 n;
1230 m=(int16*)origdata;
1231 for (n=0; n<count; n++)
1232 {
1233 if (tif->tif_flags&TIFF_SWAB)
1234 TIFFSwabShort((uint16*)m);
1235 err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1236 if (err!=TIFFReadDirEntryErrOk)
1237 {
1238 _TIFFfree(origdata);
1239 return(err);
1240 }
1241 m++;
1242 }
1243 *value=(uint16*)origdata;
1244 return(TIFFReadDirEntryErrOk);
1245 }
1246 }
1247 data=(uint16*)_TIFFmalloc(count*2);
1248 if (data==0)
1249 {
1250 _TIFFfree(origdata);
1251 return(TIFFReadDirEntryErrAlloc);
1252 }
1253 switch (direntry->tdir_type)
1254 {
1255 case TIFF_BYTE:
1256 {
1257 uint8* ma;
1258 uint16* mb;
1259 uint32 n;
1260 ma=(uint8*)origdata;
1261 mb=data;
1262 for (n=0; n<count; n++)
1263 *mb++=(uint16)(*ma++);
1264 }
1265 break;
1266 case TIFF_SBYTE:
1267 {
1268 int8* ma;
1269 uint16* mb;
1270 uint32 n;
1271 ma=(int8*)origdata;
1272 mb=data;
1273 for (n=0; n<count; n++)
1274 {
1275 err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1276 if (err!=TIFFReadDirEntryErrOk)
1277 break;
1278 *mb++=(uint16)(*ma++);
1279 }
1280 }
1281 break;
1282 case TIFF_LONG:
1283 {
1284 uint32* ma;
1285 uint16* mb;
1286 uint32 n;
1287 ma=(uint32*)origdata;
1288 mb=data;
1289 for (n=0; n<count; n++)
1290 {
1291 if (tif->tif_flags&TIFF_SWAB)
1292 TIFFSwabLong(ma);
1293 err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1294 if (err!=TIFFReadDirEntryErrOk)
1295 break;
1296 *mb++=(uint16)(*ma++);
1297 }
1298 }
1299 break;
1300 case TIFF_SLONG:
1301 {
1302 int32* ma;
1303 uint16* mb;
1304 uint32 n;
1305 ma=(int32*)origdata;
1306 mb=data;
1307 for (n=0; n<count; n++)
1308 {
1309 if (tif->tif_flags&TIFF_SWAB)
1310 TIFFSwabLong((uint32*)ma);
1311 err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1312 if (err!=TIFFReadDirEntryErrOk)
1313 break;
1314 *mb++=(uint16)(*ma++);
1315 }
1316 }
1317 break;
1318 case TIFF_LONG8:
1319 {
1320 uint64* ma;
1321 uint16* mb;
1322 uint32 n;
1323 ma=(uint64*)origdata;
1324 mb=data;
1325 for (n=0; n<count; n++)
1326 {
1327 if (tif->tif_flags&TIFF_SWAB)
1328 TIFFSwabLong8(ma);
1329 err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1330 if (err!=TIFFReadDirEntryErrOk)
1331 break;
1332 *mb++=(uint16)(*ma++);
1333 }
1334 }
1335 break;
1336 case TIFF_SLONG8:
1337 {
1338 int64* ma;
1339 uint16* mb;
1340 uint32 n;
1341 ma=(int64*)origdata;
1342 mb=data;
1343 for (n=0; n<count; n++)
1344 {
1345 if (tif->tif_flags&TIFF_SWAB)
1346 TIFFSwabLong8((uint64*)ma);
1347 err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1348 if (err!=TIFFReadDirEntryErrOk)
1349 break;
1350 *mb++=(uint16)(*ma++);
1351 }
1352 }
1353 break;
1354 }
1355 _TIFFfree(origdata);
1356 if (err!=TIFFReadDirEntryErrOk)
1357 {
1358 _TIFFfree(data);
1359 return(err);
1360 }
1361 *value=data;
1362 return(TIFFReadDirEntryErrOk);
1363 }
1364
1365 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
1366 {
1367 enum TIFFReadDirEntryErr err;
1368 uint32 count;
1369 void* origdata;
1370 int16* data;
1371 switch (direntry->tdir_type)
1372 {
1373 case TIFF_BYTE:
1374 case TIFF_SBYTE:
1375 case TIFF_SHORT:
1376 case TIFF_SSHORT:
1377 case TIFF_LONG:
1378 case TIFF_SLONG:
1379 case TIFF_LONG8:
1380 case TIFF_SLONG8:
1381 break;
1382 default:
1383 return(TIFFReadDirEntryErrType);
1384 }
1385 err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1386 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1387 {
1388 *value=0;
1389 return(err);
1390 }
1391 switch (direntry->tdir_type)
1392 {
1393 case TIFF_SHORT:
1394 {
1395 uint16* m;
1396 uint32 n;
1397 m=(uint16*)origdata;
1398 for (n=0; n<count; n++)
1399 {
1400 if (tif->tif_flags&TIFF_SWAB)
1401 TIFFSwabShort(m);
1402 err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1403 if (err!=TIFFReadDirEntryErrOk)
1404 {
1405 _TIFFfree(origdata);
1406 return(err);
1407 }
1408 m++;
1409 }
1410 *value=(int16*)origdata;
1411 return(TIFFReadDirEntryErrOk);
1412 }
1413 case TIFF_SSHORT:
1414 *value=(int16*)origdata;
1415 if (tif->tif_flags&TIFF_SWAB)
1416 TIFFSwabArrayOfShort((uint16*)(*value),count);
1417 return(TIFFReadDirEntryErrOk);
1418 }
1419 data=(int16*)_TIFFmalloc(count*2);
1420 if (data==0)
1421 {
1422 _TIFFfree(origdata);
1423 return(TIFFReadDirEntryErrAlloc);
1424 }
1425 switch (direntry->tdir_type)
1426 {
1427 case TIFF_BYTE:
1428 {
1429 uint8* ma;
1430 int16* mb;
1431 uint32 n;
1432 ma=(uint8*)origdata;
1433 mb=data;
1434 for (n=0; n<count; n++)
1435 *mb++=(int16)(*ma++);
1436 }
1437 break;
1438 case TIFF_SBYTE:
1439 {
1440 int8* ma;
1441 int16* mb;
1442 uint32 n;
1443 ma=(int8*)origdata;
1444 mb=data;
1445 for (n=0; n<count; n++)
1446 *mb++=(int16)(*ma++);
1447 }
1448 break;
1449 case TIFF_LONG:
1450 {
1451 uint32* ma;
1452 int16* mb;
1453 uint32 n;
1454 ma=(uint32*)origdata;
1455 mb=data;
1456 for (n=0; n<count; n++)
1457 {
1458 if (tif->tif_flags&TIFF_SWAB)
1459 TIFFSwabLong(ma);
1460 err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1461 if (err!=TIFFReadDirEntryErrOk)
1462 break;
1463 *mb++=(int16)(*ma++);
1464 }
1465 }
1466 break;
1467 case TIFF_SLONG:
1468 {
1469 int32* ma;
1470 int16* mb;
1471 uint32 n;
1472 ma=(int32*)origdata;
1473 mb=data;
1474 for (n=0; n<count; n++)
1475 {
1476 if (tif->tif_flags&TIFF_SWAB)
1477 TIFFSwabLong((uint32*)ma);
1478 err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1479 if (err!=TIFFReadDirEntryErrOk)
1480 break;
1481 *mb++=(int16)(*ma++);
1482 }
1483 }
1484 break;
1485 case TIFF_LONG8:
1486 {
1487 uint64* ma;
1488 int16* mb;
1489 uint32 n;
1490 ma=(uint64*)origdata;
1491 mb=data;
1492 for (n=0; n<count; n++)
1493 {
1494 if (tif->tif_flags&TIFF_SWAB)
1495 TIFFSwabLong8(ma);
1496 err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1497 if (err!=TIFFReadDirEntryErrOk)
1498 break;
1499 *mb++=(int16)(*ma++);
1500 }
1501 }
1502 break;
1503 case TIFF_SLONG8:
1504 {
1505 int64* ma;
1506 int16* mb;
1507 uint32 n;
1508 ma=(int64*)origdata;
1509 mb=data;
1510 for (n=0; n<count; n++)
1511 {
1512 if (tif->tif_flags&TIFF_SWAB)
1513 TIFFSwabLong8((uint64*)ma);
1514 err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1515 if (err!=TIFFReadDirEntryErrOk)
1516 break;
1517 *mb++=(int16)(*ma++);
1518 }
1519 }
1520 break;
1521 }
1522 _TIFFfree(origdata);
1523 if (err!=TIFFReadDirEntryErrOk)
1524 {
1525 _TIFFfree(data);
1526 return(err);
1527 }
1528 *value=data;
1529 return(TIFFReadDirEntryErrOk);
1530 }
1531
1532 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
1533 {
1534 enum TIFFReadDirEntryErr err;
1535 uint32 count;
1536 void* origdata;
1537 uint32* data;
1538 switch (direntry->tdir_type)
1539 {
1540 case TIFF_BYTE:
1541 case TIFF_SBYTE:
1542 case TIFF_SHORT:
1543 case TIFF_SSHORT:
1544 case TIFF_LONG:
1545 case TIFF_SLONG:
1546 case TIFF_LONG8:
1547 case TIFF_SLONG8:
1548 break;
1549 default:
1550 return(TIFFReadDirEntryErrType);
1551 }
1552 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1553 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1554 {
1555 *value=0;
1556 return(err);
1557 }
1558 switch (direntry->tdir_type)
1559 {
1560 case TIFF_LONG:
1561 *value=(uint32*)origdata;
1562 if (tif->tif_flags&TIFF_SWAB)
1563 TIFFSwabArrayOfLong(*value,count);
1564 return(TIFFReadDirEntryErrOk);
1565 case TIFF_SLONG:
1566 {
1567 int32* m;
1568 uint32 n;
1569 m=(int32*)origdata;
1570 for (n=0; n<count; n++)
1571 {
1572 if (tif->tif_flags&TIFF_SWAB)
1573 TIFFSwabLong((uint32*)m);
1574 err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1575 if (err!=TIFFReadDirEntryErrOk)
1576 {
1577 _TIFFfree(origdata);
1578 return(err);
1579 }
1580 m++;
1581 }
1582 *value=(uint32*)origdata;
1583 return(TIFFReadDirEntryErrOk);
1584 }
1585 }
1586 data=(uint32*)_TIFFmalloc(count*4);
1587 if (data==0)
1588 {
1589 _TIFFfree(origdata);
1590 return(TIFFReadDirEntryErrAlloc);
1591 }
1592 switch (direntry->tdir_type)
1593 {
1594 case TIFF_BYTE:
1595 {
1596 uint8* ma;
1597 uint32* mb;
1598 uint32 n;
1599 ma=(uint8*)origdata;
1600 mb=data;
1601 for (n=0; n<count; n++)
1602 *mb++=(uint32)(*ma++);
1603 }
1604 break;
1605 case TIFF_SBYTE:
1606 {
1607 int8* ma;
1608 uint32* mb;
1609 uint32 n;
1610 ma=(int8*)origdata;
1611 mb=data;
1612 for (n=0; n<count; n++)
1613 {
1614 err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1615 if (err!=TIFFReadDirEntryErrOk)
1616 break;
1617 *mb++=(uint32)(*ma++);
1618 }
1619 }
1620 break;
1621 case TIFF_SHORT:
1622 {
1623 uint16* ma;
1624 uint32* mb;
1625 uint32 n;
1626 ma=(uint16*)origdata;
1627 mb=data;
1628 for (n=0; n<count; n++)
1629 {
1630 if (tif->tif_flags&TIFF_SWAB)
1631 TIFFSwabShort(ma);
1632 *mb++=(uint32)(*ma++);
1633 }
1634 }
1635 break;
1636 case TIFF_SSHORT:
1637 {
1638 int16* ma;
1639 uint32* mb;
1640 uint32 n;
1641 ma=(int16*)origdata;
1642 mb=data;
1643 for (n=0; n<count; n++)
1644 {
1645 if (tif->tif_flags&TIFF_SWAB)
1646 TIFFSwabShort((uint16*)ma);
1647 err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1648 if (err!=TIFFReadDirEntryErrOk)
1649 break;
1650 *mb++=(uint32)(*ma++);
1651 }
1652 }
1653 break;
1654 case TIFF_LONG8:
1655 {
1656 uint64* ma;
1657 uint32* mb;
1658 uint32 n;
1659 ma=(uint64*)origdata;
1660 mb=data;
1661 for (n=0; n<count; n++)
1662 {
1663 if (tif->tif_flags&TIFF_SWAB)
1664 TIFFSwabLong8(ma);
1665 err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1666 if (err!=TIFFReadDirEntryErrOk)
1667 break;
1668 *mb++=(uint32)(*ma++);
1669 }
1670 }
1671 break;
1672 case TIFF_SLONG8:
1673 {
1674 int64* ma;
1675 uint32* mb;
1676 uint32 n;
1677 ma=(int64*)origdata;
1678 mb=data;
1679 for (n=0; n<count; n++)
1680 {
1681 if (tif->tif_flags&TIFF_SWAB)
1682 TIFFSwabLong8((uint64*)ma);
1683 err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1684 if (err!=TIFFReadDirEntryErrOk)
1685 break;
1686 *mb++=(uint32)(*ma++);
1687 }
1688 }
1689 break;
1690 }
1691 _TIFFfree(origdata);
1692 if (err!=TIFFReadDirEntryErrOk)
1693 {
1694 _TIFFfree(data);
1695 return(err);
1696 }
1697 *value=data;
1698 return(TIFFReadDirEntryErrOk);
1699 }
1700
1701 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
1702 {
1703 enum TIFFReadDirEntryErr err;
1704 uint32 count;
1705 void* origdata;
1706 int32* data;
1707 switch (direntry->tdir_type)
1708 {
1709 case TIFF_BYTE:
1710 case TIFF_SBYTE:
1711 case TIFF_SHORT:
1712 case TIFF_SSHORT:
1713 case TIFF_LONG:
1714 case TIFF_SLONG:
1715 case TIFF_LONG8:
1716 case TIFF_SLONG8:
1717 break;
1718 default:
1719 return(TIFFReadDirEntryErrType);
1720 }
1721 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1722 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1723 {
1724 *value=0;
1725 return(err);
1726 }
1727 switch (direntry->tdir_type)
1728 {
1729 case TIFF_LONG:
1730 {
1731 uint32* m;
1732 uint32 n;
1733 m=(uint32*)origdata;
1734 for (n=0; n<count; n++)
1735 {
1736 if (tif->tif_flags&TIFF_SWAB)
1737 TIFFSwabLong((uint32*)m);
1738 err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1739 if (err!=TIFFReadDirEntryErrOk)
1740 {
1741 _TIFFfree(origdata);
1742 return(err);
1743 }
1744 m++;
1745 }
1746 *value=(int32*)origdata;
1747 return(TIFFReadDirEntryErrOk);
1748 }
1749 case TIFF_SLONG:
1750 *value=(int32*)origdata;
1751 if (tif->tif_flags&TIFF_SWAB)
1752 TIFFSwabArrayOfLong((uint32*)(*value),count);
1753 return(TIFFReadDirEntryErrOk);
1754 }
1755 data=(int32*)_TIFFmalloc(count*4);
1756 if (data==0)
1757 {
1758 _TIFFfree(origdata);
1759 return(TIFFReadDirEntryErrAlloc);
1760 }
1761 switch (direntry->tdir_type)
1762 {
1763 case TIFF_BYTE:
1764 {
1765 uint8* ma;
1766 int32* mb;
1767 uint32 n;
1768 ma=(uint8*)origdata;
1769 mb=data;
1770 for (n=0; n<count; n++)
1771 *mb++=(int32)(*ma++);
1772 }
1773 break;
1774 case TIFF_SBYTE:
1775 {
1776 int8* ma;
1777 int32* mb;
1778 uint32 n;
1779 ma=(int8*)origdata;
1780 mb=data;
1781 for (n=0; n<count; n++)
1782 *mb++=(int32)(*ma++);
1783 }
1784 break;
1785 case TIFF_SHORT:
1786 {
1787 uint16* ma;
1788 int32* mb;
1789 uint32 n;
1790 ma=(uint16*)origdata;
1791 mb=data;
1792 for (n=0; n<count; n++)
1793 {
1794 if (tif->tif_flags&TIFF_SWAB)
1795 TIFFSwabShort(ma);
1796 *mb++=(int32)(*ma++);
1797 }
1798 }
1799 break;
1800 case TIFF_SSHORT:
1801 {
1802 int16* ma;
1803 int32* mb;
1804 uint32 n;
1805 ma=(int16*)origdata;
1806 mb=data;
1807 for (n=0; n<count; n++)
1808 {
1809 if (tif->tif_flags&TIFF_SWAB)
1810 TIFFSwabShort((uint16*)ma);
1811 *mb++=(int32)(*ma++);
1812 }
1813 }
1814 break;
1815 case TIFF_LONG8:
1816 {
1817 uint64* ma;
1818 int32* mb;
1819 uint32 n;
1820 ma=(uint64*)origdata;
1821 mb=data;
1822 for (n=0; n<count; n++)
1823 {
1824 if (tif->tif_flags&TIFF_SWAB)
1825 TIFFSwabLong8(ma);
1826 err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1827 if (err!=TIFFReadDirEntryErrOk)
1828 break;
1829 *mb++=(int32)(*ma++);
1830 }
1831 }
1832 break;
1833 case TIFF_SLONG8:
1834 {
1835 int64* ma;
1836 int32* mb;
1837 uint32 n;
1838 ma=(int64*)origdata;
1839 mb=data;
1840 for (n=0; n<count; n++)
1841 {
1842 if (tif->tif_flags&TIFF_SWAB)
1843 TIFFSwabLong8((uint64*)ma);
1844 err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1845 if (err!=TIFFReadDirEntryErrOk)
1846 break;
1847 *mb++=(int32)(*ma++);
1848 }
1849 }
1850 break;
1851 }
1852 _TIFFfree(origdata);
1853 if (err!=TIFFReadDirEntryErrOk)
1854 {
1855 _TIFFfree(data);
1856 return(err);
1857 }
1858 *value=data;
1859 return(TIFFReadDirEntryErrOk);
1860 }
1861
1862 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
1863 {
1864 enum TIFFReadDirEntryErr err;
1865 uint32 count;
1866 void* origdata;
1867 uint64* data;
1868 switch (direntry->tdir_type)
1869 {
1870 case TIFF_BYTE:
1871 case TIFF_SBYTE:
1872 case TIFF_SHORT:
1873 case TIFF_SSHORT:
1874 case TIFF_LONG:
1875 case TIFF_SLONG:
1876 case TIFF_LONG8:
1877 case TIFF_SLONG8:
1878 break;
1879 default:
1880 return(TIFFReadDirEntryErrType);
1881 }
1882 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
1883 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1884 {
1885 *value=0;
1886 return(err);
1887 }
1888 switch (direntry->tdir_type)
1889 {
1890 case TIFF_LONG8:
1891 *value=(uint64*)origdata;
1892 if (tif->tif_flags&TIFF_SWAB)
1893 TIFFSwabArrayOfLong8(*value,count);
1894 return(TIFFReadDirEntryErrOk);
1895 case TIFF_SLONG8:
1896 {
1897 int64* m;
1898 uint32 n;
1899 m=(int64*)origdata;
1900 for (n=0; n<count; n++)
1901 {
1902 if (tif->tif_flags&TIFF_SWAB)
1903 TIFFSwabLong8((uint64*)m);
1904 err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
1905 if (err!=TIFFReadDirEntryErrOk)
1906 {
1907 _TIFFfree(origdata);
1908 return(err);
1909 }
1910 m++;
1911 }
1912 *value=(uint64*)origdata;
1913 return(TIFFReadDirEntryErrOk);
1914 }
1915 }
1916 data=(uint64*)_TIFFmalloc(count*8);
1917 if (data==0)
1918 {
1919 _TIFFfree(origdata);
1920 return(TIFFReadDirEntryErrAlloc);
1921 }
1922 switch (direntry->tdir_type)
1923 {
1924 case TIFF_BYTE:
1925 {
1926 uint8* ma;
1927 uint64* mb;
1928 uint32 n;
1929 ma=(uint8*)origdata;
1930 mb=data;
1931 for (n=0; n<count; n++)
1932 *mb++=(uint64)(*ma++);
1933 }
1934 break;
1935 case TIFF_SBYTE:
1936 {
1937 int8* ma;
1938 uint64* mb;
1939 uint32 n;
1940 ma=(int8*)origdata;
1941 mb=data;
1942 for (n=0; n<count; n++)
1943 {
1944 err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
1945 if (err!=TIFFReadDirEntryErrOk)
1946 break;
1947 *mb++=(uint64)(*ma++);
1948 }
1949 }
1950 break;
1951 case TIFF_SHORT:
1952 {
1953 uint16* ma;
1954 uint64* mb;
1955 uint32 n;
1956 ma=(uint16*)origdata;
1957 mb=data;
1958 for (n=0; n<count; n++)
1959 {
1960 if (tif->tif_flags&TIFF_SWAB)
1961 TIFFSwabShort(ma);
1962 *mb++=(uint64)(*ma++);
1963 }
1964 }
1965 break;
1966 case TIFF_SSHORT:
1967 {
1968 int16* ma;
1969 uint64* mb;
1970 uint32 n;
1971 ma=(int16*)origdata;
1972 mb=data;
1973 for (n=0; n<count; n++)
1974 {
1975 if (tif->tif_flags&TIFF_SWAB)
1976 TIFFSwabShort((uint16*)ma);
1977 err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
1978 if (err!=TIFFReadDirEntryErrOk)
1979 break;
1980 *mb++=(uint64)(*ma++);
1981 }
1982 }
1983 break;
1984 case TIFF_LONG:
1985 {
1986 uint32* ma;
1987 uint64* mb;
1988 uint32 n;
1989 ma=(uint32*)origdata;
1990 mb=data;
1991 for (n=0; n<count; n++)
1992 {
1993 if (tif->tif_flags&TIFF_SWAB)
1994 TIFFSwabLong(ma);
1995 *mb++=(uint64)(*ma++);
1996 }
1997 }
1998 break;
1999 case TIFF_SLONG:
2000 {
2001 int32* ma;
2002 uint64* mb;
2003 uint32 n;
2004 ma=(int32*)origdata;
2005 mb=data;
2006 for (n=0; n<count; n++)
2007 {
2008 if (tif->tif_flags&TIFF_SWAB)
2009 TIFFSwabLong((uint32*)ma);
2010 err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2011 if (err!=TIFFReadDirEntryErrOk)
2012 break;
2013 *mb++=(uint64)(*ma++);
2014 }
2015 }
2016 break;
2017 }
2018 _TIFFfree(origdata);
2019 if (err!=TIFFReadDirEntryErrOk)
2020 {
2021 _TIFFfree(data);
2022 return(err);
2023 }
2024 *value=data;
2025 return(TIFFReadDirEntryErrOk);
2026 }
2027
2028 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
2029 {
2030 enum TIFFReadDirEntryErr err;
2031 uint32 count;
2032 void* origdata;
2033 int64* data;
2034 switch (direntry->tdir_type)
2035 {
2036 case TIFF_BYTE:
2037 case TIFF_SBYTE:
2038 case TIFF_SHORT:
2039 case TIFF_SSHORT:
2040 case TIFF_LONG:
2041 case TIFF_SLONG:
2042 case TIFF_LONG8:
2043 case TIFF_SLONG8:
2044 break;
2045 default:
2046 return(TIFFReadDirEntryErrType);
2047 }
2048 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2049 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2050 {
2051 *value=0;
2052 return(err);
2053 }
2054 switch (direntry->tdir_type)
2055 {
2056 case TIFF_LONG8:
2057 {
2058 uint64* m;
2059 uint32 n;
2060 m=(uint64*)origdata;
2061 for (n=0; n<count; n++)
2062 {
2063 if (tif->tif_flags&TIFF_SWAB)
2064 TIFFSwabLong8(m);
2065 err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2066 if (err!=TIFFReadDirEntryErrOk)
2067 {
2068 _TIFFfree(origdata);
2069 return(err);
2070 }
2071 m++;
2072 }
2073 *value=(int64*)origdata;
2074 return(TIFFReadDirEntryErrOk);
2075 }
2076 case TIFF_SLONG8:
2077 *value=(int64*)origdata;
2078 if (tif->tif_flags&TIFF_SWAB)
2079 TIFFSwabArrayOfLong8((uint64*)(*value),count);
2080 return(TIFFReadDirEntryErrOk);
2081 }
2082 data=(int64*)_TIFFmalloc(count*8);
2083 if (data==0)
2084 {
2085 _TIFFfree(origdata);
2086 return(TIFFReadDirEntryErrAlloc);
2087 }
2088 switch (direntry->tdir_type)
2089 {
2090 case TIFF_BYTE:
2091 {
2092 uint8* ma;
2093 int64* mb;
2094 uint32 n;
2095 ma=(uint8*)origdata;
2096 mb=data;
2097 for (n=0; n<count; n++)
2098 *mb++=(int64)(*ma++);
2099 }
2100 break;
2101 case TIFF_SBYTE:
2102 {
2103 int8* ma;
2104 int64* mb;
2105 uint32 n;
2106 ma=(int8*)origdata;
2107 mb=data;
2108 for (n=0; n<count; n++)
2109 *mb++=(int64)(*ma++);
2110 }
2111 break;
2112 case TIFF_SHORT:
2113 {
2114 uint16* ma;
2115 int64* mb;
2116 uint32 n;
2117 ma=(uint16*)origdata;
2118 mb=data;
2119 for (n=0; n<count; n++)
2120 {
2121 if (tif->tif_flags&TIFF_SWAB)
2122 TIFFSwabShort(ma);
2123 *mb++=(int64)(*ma++);
2124 }
2125 }
2126 break;
2127 case TIFF_SSHORT:
2128 {
2129 int16* ma;
2130 int64* mb;
2131 uint32 n;
2132 ma=(int16*)origdata;
2133 mb=data;
2134 for (n=0; n<count; n++)
2135 {
2136 if (tif->tif_flags&TIFF_SWAB)
2137 TIFFSwabShort((uint16*)ma);
2138 *mb++=(int64)(*ma++);
2139 }
2140 }
2141 break;
2142 case TIFF_LONG:
2143 {
2144 uint32* ma;
2145 int64* mb;
2146 uint32 n;
2147 ma=(uint32*)origdata;
2148 mb=data;
2149 for (n=0; n<count; n++)
2150 {
2151 if (tif->tif_flags&TIFF_SWAB)
2152 TIFFSwabLong(ma);
2153 *mb++=(int64)(*ma++);
2154 }
2155 }
2156 break;
2157 case TIFF_SLONG:
2158 {
2159 int32* ma;
2160 int64* mb;
2161 uint32 n;
2162 ma=(int32*)origdata;
2163 mb=data;
2164 for (n=0; n<count; n++)
2165 {
2166 if (tif->tif_flags&TIFF_SWAB)
2167 TIFFSwabLong((uint32*)ma);
2168 *mb++=(int64)(*ma++);
2169 }
2170 }
2171 break;
2172 }
2173 _TIFFfree(origdata);
2174 if (err!=TIFFReadDirEntryErrOk)
2175 {
2176 _TIFFfree(data);
2177 return(err);
2178 }
2179 *value=data;
2180 return(TIFFReadDirEntryErrOk);
2181 }
2182
2183 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2184 {
2185 enum TIFFReadDirEntryErr err;
2186 uint32 count;
2187 void* origdata;
2188 float* data;
2189 switch (direntry->tdir_type)
2190 {
2191 case TIFF_BYTE:
2192 case TIFF_SBYTE:
2193 case TIFF_SHORT:
2194 case TIFF_SSHORT:
2195 case TIFF_LONG:
2196 case TIFF_SLONG:
2197 case TIFF_LONG8:
2198 case TIFF_SLONG8:
2199 case TIFF_RATIONAL:
2200 case TIFF_SRATIONAL:
2201 case TIFF_FLOAT:
2202 case TIFF_DOUBLE:
2203 break;
2204 default:
2205 return(TIFFReadDirEntryErrType);
2206 }
2207 err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2208 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2209 {
2210 *value=0;
2211 return(err);
2212 }
2213 switch (direntry->tdir_type)
2214 {
2215 case TIFF_FLOAT:
2216 if (tif->tif_flags&TIFF_SWAB)
2217 TIFFSwabArrayOfLong((uint32*)origdata,count);
2218 TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2219 *value=(float*)origdata;
2220 return(TIFFReadDirEntryErrOk);
2221 }
2222 data=(float*)_TIFFmalloc(count*sizeof(float));
2223 if (data==0)
2224 {
2225 _TIFFfree(origdata);
2226 return(TIFFReadDirEntryErrAlloc);
2227 }
2228 switch (direntry->tdir_type)
2229 {
2230 case TIFF_BYTE:
2231 {
2232 uint8* ma;
2233 float* mb;
2234 uint32 n;
2235 ma=(uint8*)origdata;
2236 mb=data;
2237 for (n=0; n<count; n++)
2238 *mb++=(float)(*ma++);
2239 }
2240 break;
2241 case TIFF_SBYTE:
2242 {
2243 int8* ma;
2244 float* mb;
2245 uint32 n;
2246 ma=(int8*)origdata;
2247 mb=data;
2248 for (n=0; n<count; n++)
2249 *mb++=(float)(*ma++);
2250 }
2251 break;
2252 case TIFF_SHORT:
2253 {
2254 uint16* ma;
2255 float* mb;
2256 uint32 n;
2257 ma=(uint16*)origdata;
2258 mb=data;
2259 for (n=0; n<count; n++)
2260 {
2261 if (tif->tif_flags&TIFF_SWAB)
2262 TIFFSwabShort(ma);
2263 *mb++=(float)(*ma++);
2264 }
2265 }
2266 break;
2267 case TIFF_SSHORT:
2268 {
2269 int16* ma;
2270 float* mb;
2271 uint32 n;
2272 ma=(int16*)origdata;
2273 mb=data;
2274 for (n=0; n<count; n++)
2275 {
2276 if (tif->tif_flags&TIFF_SWAB)
2277 TIFFSwabShort((uint16*)ma);
2278 *mb++=(float)(*ma++);
2279 }
2280 }
2281 break;
2282 case TIFF_LONG:
2283 {
2284 uint32* ma;
2285 float* mb;
2286 uint32 n;
2287 ma=(uint32*)origdata;
2288 mb=data;
2289 for (n=0; n<count; n++)
2290 {
2291 if (tif->tif_flags&TIFF_SWAB)
2292 TIFFSwabLong(ma);
2293 *mb++=(float)(*ma++);
2294 }
2295 }
2296 break;
2297 case TIFF_SLONG:
2298 {
2299 int32* ma;
2300 float* mb;
2301 uint32 n;
2302 ma=(int32*)origdata;
2303 mb=data;
2304 for (n=0; n<count; n++)
2305 {
2306 if (tif->tif_flags&TIFF_SWAB)
2307 TIFFSwabLong((uint32*)ma);
2308 *mb++=(float)(*ma++);
2309 }
2310 }
2311 break;
2312 case TIFF_LONG8:
2313 {
2314 uint64* ma;
2315 float* mb;
2316 uint32 n;
2317 ma=(uint64*)origdata;
2318 mb=data;
2319 for (n=0; n<count; n++)
2320 {
2321 if (tif->tif_flags&TIFF_SWAB)
2322 TIFFSwabLong8(ma);
2323 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
2324 /*
2325 * XXX: MSVC 6.0 does not support
2326 * conversion of 64-bit integers into
2327 * floating point values.
2328 */
2329 *mb++ = _TIFFUInt64ToFloat(*ma++);
2330 #else
2331 *mb++ = (float)(*ma++);
2332 #endif
2333 }
2334 }
2335 break;
2336 case TIFF_SLONG8:
2337 {
2338 int64* ma;
2339 float* mb;
2340 uint32 n;
2341 ma=(int64*)origdata;
2342 mb=data;
2343 for (n=0; n<count; n++)
2344 {
2345 if (tif->tif_flags&TIFF_SWAB)
2346 TIFFSwabLong8((uint64*)ma);
2347 *mb++=(float)(*ma++);
2348 }
2349 }
2350 break;
2351 case TIFF_RATIONAL:
2352 {
2353 uint32* ma;
2354 uint32 maa;
2355 uint32 mab;
2356 float* mb;
2357 uint32 n;
2358 ma=(uint32*)origdata;
2359 mb=data;
2360 for (n=0; n<count; n++)
2361 {
2362 if (tif->tif_flags&TIFF_SWAB)
2363 TIFFSwabLong(ma);
2364 maa=*ma++;
2365 if (tif->tif_flags&TIFF_SWAB)
2366 TIFFSwabLong(ma);
2367 mab=*ma++;
2368 if (mab==0)
2369 *mb++=0.0;
2370 else
2371 *mb++=(float)maa/(float)mab;
2372 }
2373 }
2374 break;
2375 case TIFF_SRATIONAL:
2376 {
2377 uint32* ma;
2378 int32 maa;
2379 uint32 mab;
2380 float* mb;
2381 uint32 n;
2382 ma=(uint32*)origdata;
2383 mb=data;
2384 for (n=0; n<count; n++)
2385 {
2386 if (tif->tif_flags&TIFF_SWAB)
2387 TIFFSwabLong(ma);
2388 maa=*(int32*)ma;
2389 ma++;
2390 if (tif->tif_flags&TIFF_SWAB)
2391 TIFFSwabLong(ma);
2392 mab=*ma++;
2393 if (mab==0)
2394 *mb++=0.0;
2395 else
2396 *mb++=(float)maa/(float)mab;
2397 }
2398 }
2399 break;
2400 case TIFF_DOUBLE:
2401 {
2402 double* ma;
2403 float* mb;
2404 uint32 n;
2405 if (tif->tif_flags&TIFF_SWAB)
2406 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2407 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2408 ma=(double*)origdata;
2409 mb=data;
2410 for (n=0; n<count; n++)
2411 *mb++=(float)(*ma++);
2412 }
2413 break;
2414 }
2415 _TIFFfree(origdata);
2416 if (err!=TIFFReadDirEntryErrOk)
2417 {
2418 _TIFFfree(data);
2419 return(err);
2420 }
2421 *value=data;
2422 return(TIFFReadDirEntryErrOk);
2423 }
2424
2425 static enum TIFFReadDirEntryErr
2426 TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2427 {
2428 enum TIFFReadDirEntryErr err;
2429 uint32 count;
2430 void* origdata;
2431 double* data;
2432 switch (direntry->tdir_type)
2433 {
2434 case TIFF_BYTE:
2435 case TIFF_SBYTE:
2436 case TIFF_SHORT:
2437 case TIFF_SSHORT:
2438 case TIFF_LONG:
2439 case TIFF_SLONG:
2440 case TIFF_LONG8:
2441 case TIFF_SLONG8:
2442 case TIFF_RATIONAL:
2443 case TIFF_SRATIONAL:
2444 case TIFF_FLOAT:
2445 case TIFF_DOUBLE:
2446 break;
2447 default:
2448 return(TIFFReadDirEntryErrType);
2449 }
2450 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2451 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2452 {
2453 *value=0;
2454 return(err);
2455 }
2456 switch (direntry->tdir_type)
2457 {
2458 case TIFF_DOUBLE:
2459 if (tif->tif_flags&TIFF_SWAB)
2460 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2461 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2462 *value=(double*)origdata;
2463 return(TIFFReadDirEntryErrOk);
2464 }
2465 data=(double*)_TIFFmalloc(count*sizeof(double));
2466 if (data==0)
2467 {
2468 _TIFFfree(origdata);
2469 return(TIFFReadDirEntryErrAlloc);
2470 }
2471 switch (direntry->tdir_type)
2472 {
2473 case TIFF_BYTE:
2474 {
2475 uint8* ma;
2476 double* mb;
2477 uint32 n;
2478 ma=(uint8*)origdata;
2479 mb=data;
2480 for (n=0; n<count; n++)
2481 *mb++=(double)(*ma++);
2482 }
2483 break;
2484 case TIFF_SBYTE:
2485 {
2486 int8* ma;
2487 double* mb;
2488 uint32 n;
2489 ma=(int8*)origdata;
2490 mb=data;
2491 for (n=0; n<count; n++)
2492 *mb++=(double)(*ma++);
2493 }
2494 break;
2495 case TIFF_SHORT:
2496 {
2497 uint16* ma;
2498 double* mb;
2499 uint32 n;
2500 ma=(uint16*)origdata;
2501 mb=data;
2502 for (n=0; n<count; n++)
2503 {
2504 if (tif->tif_flags&TIFF_SWAB)
2505 TIFFSwabShort(ma);
2506 *mb++=(double)(*ma++);
2507 }
2508 }
2509 break;
2510 case TIFF_SSHORT:
2511 {
2512 int16* ma;
2513 double* mb;
2514 uint32 n;
2515 ma=(int16*)origdata;
2516 mb=data;
2517 for (n=0; n<count; n++)
2518 {
2519 if (tif->tif_flags&TIFF_SWAB)
2520 TIFFSwabShort((uint16*)ma);
2521 *mb++=(double)(*ma++);
2522 }
2523 }
2524 break;
2525 case TIFF_LONG:
2526 {
2527 uint32* ma;
2528 double* mb;
2529 uint32 n;
2530 ma=(uint32*)origdata;
2531 mb=data;
2532 for (n=0; n<count; n++)
2533 {
2534 if (tif->tif_flags&TIFF_SWAB)
2535 TIFFSwabLong(ma);
2536 *mb++=(double)(*ma++);
2537 }
2538 }
2539 break;
2540 case TIFF_SLONG:
2541 {
2542 int32* ma;
2543 double* mb;
2544 uint32 n;
2545 ma=(int32*)origdata;
2546 mb=data;
2547 for (n=0; n<count; n++)
2548 {
2549 if (tif->tif_flags&TIFF_SWAB)
2550 TIFFSwabLong((uint32*)ma);
2551 *mb++=(double)(*ma++);
2552 }
2553 }
2554 break;
2555 case TIFF_LONG8:
2556 {
2557 uint64* ma;
2558 double* mb;
2559 uint32 n;
2560 ma=(uint64*)origdata;
2561 mb=data;
2562 for (n=0; n<count; n++)
2563 {
2564 if (tif->tif_flags&TIFF_SWAB)
2565 TIFFSwabLong8(ma);
2566 #if defined(__WIN32__) && defined(_MSC_VER) && (_MSC_VER < 1500)
2567 /*
2568 * XXX: MSVC 6.0 does not support
2569 * conversion of 64-bit integers into
2570 * floating point values.
2571 */
2572 *mb++ = _TIFFUInt64ToDouble(*ma++);
2573 #else
2574 *mb++ = (double)(*ma++);
2575 #endif
2576 }
2577 }
2578 break;
2579 case TIFF_SLONG8:
2580 {
2581 int64* ma;
2582 double* mb;
2583 uint32 n;
2584 ma=(int64*)origdata;
2585 mb=data;
2586 for (n=0; n<count; n++)
2587 {
2588 if (tif->tif_flags&TIFF_SWAB)
2589 TIFFSwabLong8((uint64*)ma);
2590 *mb++=(double)(*ma++);
2591 }
2592 }
2593 break;
2594 case TIFF_RATIONAL:
2595 {
2596 uint32* ma;
2597 uint32 maa;
2598 uint32 mab;
2599 double* mb;
2600 uint32 n;
2601 ma=(uint32*)origdata;
2602 mb=data;
2603 for (n=0; n<count; n++)
2604 {
2605 if (tif->tif_flags&TIFF_SWAB)
2606 TIFFSwabLong(ma);
2607 maa=*ma++;
2608 if (tif->tif_flags&TIFF_SWAB)
2609 TIFFSwabLong(ma);
2610 mab=*ma++;
2611 if (mab==0)
2612 *mb++=0.0;
2613 else
2614 *mb++=(double)maa/(double)mab;
2615 }
2616 }
2617 break;
2618 case TIFF_SRATIONAL:
2619 {
2620 uint32* ma;
2621 int32 maa;
2622 uint32 mab;
2623 double* mb;
2624 uint32 n;
2625 ma=(uint32*)origdata;
2626 mb=data;
2627 for (n=0; n<count; n++)
2628 {
2629 if (tif->tif_flags&TIFF_SWAB)
2630 TIFFSwabLong(ma);
2631 maa=*(int32*)ma;
2632 ma++;
2633 if (tif->tif_flags&TIFF_SWAB)
2634 TIFFSwabLong(ma);
2635 mab=*ma++;
2636 if (mab==0)
2637 *mb++=0.0;
2638 else
2639 *mb++=(double)maa/(double)mab;
2640 }
2641 }
2642 break;
2643 case TIFF_FLOAT:
2644 {
2645 float* ma;
2646 double* mb;
2647 uint32 n;
2648 if (tif->tif_flags&TIFF_SWAB)
2649 TIFFSwabArrayOfLong((uint32*)origdata,count);
2650 TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
2651 ma=(float*)origdata;
2652 mb=data;
2653 for (n=0; n<count; n++)
2654 *mb++=(double)(*ma++);
2655 }
2656 break;
2657 }
2658 _TIFFfree(origdata);
2659 if (err!=TIFFReadDirEntryErrOk)
2660 {
2661 _TIFFfree(data);
2662 return(err);
2663 }
2664 *value=data;
2665 return(TIFFReadDirEntryErrOk);
2666 }
2667
2668 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
2669 {
2670 enum TIFFReadDirEntryErr err;
2671 uint32 count;
2672 void* origdata;
2673 uint64* data;
2674 switch (direntry->tdir_type)
2675 {
2676 case TIFF_LONG:
2677 case TIFF_LONG8:
2678 case TIFF_IFD:
2679 case TIFF_IFD8:
2680 break;
2681 default:
2682 return(TIFFReadDirEntryErrType);
2683 }
2684 err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2685 if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2686 {
2687 *value=0;
2688 return(err);
2689 }
2690 switch (direntry->tdir_type)
2691 {
2692 case TIFF_LONG8:
2693 case TIFF_IFD8:
2694 *value=(uint64*)origdata;
2695 if (tif->tif_flags&TIFF_SWAB)
2696 TIFFSwabArrayOfLong8(*value,count);
2697 return(TIFFReadDirEntryErrOk);
2698 }
2699 data=(uint64*)_TIFFmalloc(count*8);
2700 if (data==0)
2701 {
2702 _TIFFfree(origdata);
2703 return(TIFFReadDirEntryErrAlloc);
2704 }
2705 switch (direntry->tdir_type)
2706 {
2707 case TIFF_LONG:
2708 case TIFF_IFD:
2709 {
2710 uint32* ma;
2711 uint64* mb;
2712 uint32 n;
2713 ma=(uint32*)origdata;
2714 mb=data;
2715 for (n=0; n<count; n++)
2716 {
2717 if (tif->tif_flags&TIFF_SWAB)
2718 TIFFSwabLong(ma);
2719 *mb++=(uint64)(*ma++);
2720 }
2721 }
2722 break;
2723 }
2724 _TIFFfree(origdata);
2725 if (err!=TIFFReadDirEntryErrOk)
2726 {
2727 _TIFFfree(data);
2728 return(err);
2729 }
2730 *value=data;
2731 return(TIFFReadDirEntryErrOk);
2732 }
2733
2734 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2735 {
2736 enum TIFFReadDirEntryErr err;
2737 uint16* m;
2738 uint16* na;
2739 uint16 nb;
2740 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2741 return(TIFFReadDirEntryErrCount);
2742 err=TIFFReadDirEntryShortArray(tif,direntry,&m);
2743 if (err!=TIFFReadDirEntryErrOk)
2744 return(err);
2745 na=m;
2746 nb=tif->tif_dir.td_samplesperpixel;
2747 *value=*na++;
2748 nb--;
2749 while (nb>0)
2750 {
2751 if (*na++!=*value)
2752 {
2753 err=TIFFReadDirEntryErrPsdif;
2754 break;
2755 }
2756 nb--;
2757 }
2758 _TIFFfree(m);
2759 return(err);
2760 }
2761
2762 #if 0
2763 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2764 {
2765 enum TIFFReadDirEntryErr err;
2766 double* m;
2767 double* na;
2768 uint16 nb;
2769 if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
2770 return(TIFFReadDirEntryErrCount);
2771 err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
2772 if (err!=TIFFReadDirEntryErrOk)
2773 return(err);
2774 na=m;
2775 nb=tif->tif_dir.td_samplesperpixel;
2776 *value=*na++;
2777 nb--;
2778 while (nb>0)
2779 {
2780 if (*na++!=*value)
2781 {
2782 err=TIFFReadDirEntryErrPsdif;
2783 break;
2784 }
2785 nb--;
2786 }
2787 _TIFFfree(m);
2788 return(err);
2789 }
2790 #endif
2791
2792 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
2793 {
2794 (void) tif;
2795 *value=*(uint8*)(&direntry->tdir_offset);
2796 }
2797
2798 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
2799 {
2800 (void) tif;
2801 *value=*(int8*)(&direntry->tdir_offset);
2802 }
2803
2804 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
2805 {
2806 *value = direntry->tdir_offset.toff_short;
2807 /* *value=*(uint16*)(&direntry->tdir_offset); */
2808 if (tif->tif_flags&TIFF_SWAB)
2809 TIFFSwabShort(value);
2810 }
2811
2812 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
2813 {
2814 *value=*(int16*)(&direntry->tdir_offset);
2815 if (tif->tif_flags&TIFF_SWAB)
2816 TIFFSwabShort((uint16*)value);
2817 }
2818
2819 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
2820 {
2821 *value=*(uint32*)(&direntry->tdir_offset);
2822 if (tif->tif_flags&TIFF_SWAB)
2823 TIFFSwabLong(value);
2824 }
2825
2826 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
2827 {
2828 *value=*(int32*)(&direntry->tdir_offset);
2829 if (tif->tif_flags&TIFF_SWAB)
2830 TIFFSwabLong((uint32*)value);
2831 }
2832
2833 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
2834 {
2835 if (!(tif->tif_flags&TIFF_BIGTIFF))
2836 {
2837 enum TIFFReadDirEntryErr err;
2838 uint32 offset = direntry->tdir_offset.toff_long;
2839 if (tif->tif_flags&TIFF_SWAB)
2840 TIFFSwabLong(&offset);
2841 err=TIFFReadDirEntryData(tif,offset,8,value);
2842 if (err!=TIFFReadDirEntryErrOk)
2843 return(err);
2844 }
2845 else
2846 *value = direntry->tdir_offset.toff_long8;
2847 if (tif->tif_flags&TIFF_SWAB)
2848 TIFFSwabLong8(value);
2849 return(TIFFReadDirEntryErrOk);
2850 }
2851
2852 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
2853 {
2854 if (!(tif->tif_flags&TIFF_BIGTIFF))
2855 {
2856 enum TIFFReadDirEntryErr err;
2857 uint32 offset = direntry->tdir_offset.toff_long;
2858 if (tif->tif_flags&TIFF_SWAB)
2859 TIFFSwabLong(&offset);
2860 err=TIFFReadDirEntryData(tif,offset,8,value);
2861 if (err!=TIFFReadDirEntryErrOk)
2862 return(err);
2863 }
2864 else
2865 *value=*(int64*)(&direntry->tdir_offset);
2866 if (tif->tif_flags&TIFF_SWAB)
2867 TIFFSwabLong8((uint64*)value);
2868 return(TIFFReadDirEntryErrOk);
2869 }
2870
2871 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2872 {
2873 UInt64Aligned_t m;
2874
2875 assert(sizeof(double)==8);
2876 assert(sizeof(uint64)==8);
2877 assert(sizeof(uint32)==4);
2878 if (!(tif->tif_flags&TIFF_BIGTIFF))
2879 {
2880 enum TIFFReadDirEntryErr err;
2881 uint32 offset = direntry->tdir_offset.toff_long;
2882 if (tif->tif_flags&TIFF_SWAB)
2883 TIFFSwabLong(&offset);
2884 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2885 if (err!=TIFFReadDirEntryErrOk)
2886 return(err);
2887 }
2888 else
2889 m.l = direntry->tdir_offset.toff_long8;
2890 if (tif->tif_flags&TIFF_SWAB)
2891 TIFFSwabArrayOfLong(m.i,2);
2892 if (m.i[0]==0)
2893 *value=0.0;
2894 else
2895 *value=(double)m.i[0]/(double)m.i[1];
2896 return(TIFFReadDirEntryErrOk);
2897 }
2898
2899 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
2900 {
2901 UInt64Aligned_t m;
2902 assert(sizeof(double)==8);
2903 assert(sizeof(uint64)==8);
2904 assert(sizeof(int32)==4);
2905 assert(sizeof(uint32)==4);
2906 if (!(tif->tif_flags&TIFF_BIGTIFF))
2907 {
2908 enum TIFFReadDirEntryErr err;
2909 uint32 offset = direntry->tdir_offset.toff_long;
2910 if (tif->tif_flags&TIFF_SWAB)
2911 TIFFSwabLong(&offset);
2912 err=TIFFReadDirEntryData(tif,offset,8,m.i);
2913 if (err!=TIFFReadDirEntryErrOk)
2914 return(err);
2915 }
2916 else
2917 m.l=direntry->tdir_offset.toff_long8;
2918 if (tif->tif_flags&TIFF_SWAB)
2919 TIFFSwabArrayOfLong(m.i,2);
2920 if ((int32)m.i[0]==0)
2921 *value=0.0;
2922 else
2923 *value=(double)((int32)m.i[0])/(double)m.i[1];
2924 return(TIFFReadDirEntryErrOk);
2925 }
2926
2927 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
2928 {
2929 union
2930 {
2931 float f;
2932 uint32 i;
2933 } float_union;
2934 assert(sizeof(float)==4);
2935 assert(sizeof(uint32)==4);
2936 assert(sizeof(float_union)==4);
2937 float_union.i=*(uint32*)(&direntry->tdir_offset);
2938 *value=float_union.f;
2939 if (tif->tif_flags&TIFF_SWAB)
2940 TIFFSwabLong((uint32*)value);
2941 }
2942
2943 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
2944 {
2945 assert(sizeof(double)==8);
2946 assert(sizeof(uint64)==8);
2947 assert(sizeof(UInt64Aligned_t)==8);
2948 if (!(tif->tif_flags&TIFF_BIGTIFF))
2949 {
2950 enum TIFFReadDirEntryErr err;
2951 uint32 offset = direntry->tdir_offset.toff_long;
2952 if (tif->tif_flags&TIFF_SWAB)
2953 TIFFSwabLong(&offset);
2954 err=TIFFReadDirEntryData(tif,offset,8,value);
2955 if (err!=TIFFReadDirEntryErrOk)
2956 return(err);
2957 }
2958 else
2959 {
2960 UInt64Aligned_t uint64_union;
2961 uint64_union.l=direntry->tdir_offset.toff_long8;
2962 *value=uint64_union.d;
2963 }
2964 if (tif->tif_flags&TIFF_SWAB)
2965 TIFFSwabLong8((uint64*)value);
2966 return(TIFFReadDirEntryErrOk);
2967 }
2968
2969 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
2970 {
2971 if (value<0)
2972 return(TIFFReadDirEntryErrRange);
2973 else
2974 return(TIFFReadDirEntryErrOk);
2975 }
2976
2977 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
2978 {
2979 if (value>0xFF)
2980 return(TIFFReadDirEntryErrRange);
2981 else
2982 return(TIFFReadDirEntryErrOk);
2983 }
2984
2985 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
2986 {
2987 if ((value<0)||(value>0xFF))
2988 return(TIFFReadDirEntryErrRange);
2989 else
2990 return(TIFFReadDirEntryErrOk);
2991 }
2992
2993 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
2994 {
2995 if (value>0xFF)
2996 return(TIFFReadDirEntryErrRange);
2997 else
2998 return(TIFFReadDirEntryErrOk);
2999 }
3000
3001 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
3002 {
3003 if ((value<0)||(value>0xFF))
3004 return(TIFFReadDirEntryErrRange);
3005 else
3006 return(TIFFReadDirEntryErrOk);
3007 }
3008
3009 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
3010 {
3011 if (value>0xFF)
3012 return(TIFFReadDirEntryErrRange);
3013 else
3014 return(TIFFReadDirEntryErrOk);
3015 }
3016
3017 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
3018 {
3019 if ((value<0)||(value>0xFF))
3020 return(TIFFReadDirEntryErrRange);
3021 else
3022 return(TIFFReadDirEntryErrOk);
3023 }
3024
3025 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
3026 {
3027 if (value>0x7F)
3028 return(TIFFReadDirEntryErrRange);
3029 else
3030 return(TIFFReadDirEntryErrOk);
3031 }
3032
3033 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
3034 {
3035 if (value>0x7F)
3036 return(TIFFReadDirEntryErrRange);
3037 else
3038 return(TIFFReadDirEntryErrOk);
3039 }
3040
3041 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
3042 {
3043 if ((value<-0x80)||(value>0x7F))
3044 return(TIFFReadDirEntryErrRange);
3045 else
3046 return(TIFFReadDirEntryErrOk);
3047 }
3048
3049 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
3050 {
3051 if (value>0x7F)
3052 return(TIFFReadDirEntryErrRange);
3053 else
3054 return(TIFFReadDirEntryErrOk);
3055 }
3056
3057 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
3058 {
3059 if ((value<-0x80)||(value>0x7F))
3060 return(TIFFReadDirEntryErrRange);
3061 else
3062 return(TIFFReadDirEntryErrOk);
3063 }
3064
3065 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
3066 {
3067 if (value>0x7F)
3068 return(TIFFReadDirEntryErrRange);
3069 else
3070 return(TIFFReadDirEntryErrOk);
3071 }
3072
3073 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
3074 {
3075 if ((value<-0x80)||(value>0x7F))
3076 return(TIFFReadDirEntryErrRange);
3077 else
3078 return(TIFFReadDirEntryErrOk);
3079 }
3080
3081 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
3082 {
3083 if (value<0)
3084 return(TIFFReadDirEntryErrRange);
3085 else
3086 return(TIFFReadDirEntryErrOk);
3087 }
3088
3089 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
3090 {
3091 if (value<0)
3092 return(TIFFReadDirEntryErrRange);
3093 else
3094 return(TIFFReadDirEntryErrOk);
3095 }
3096
3097 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
3098 {
3099 if (value>0xFFFF)
3100 return(TIFFReadDirEntryErrRange);
3101 else
3102 return(TIFFReadDirEntryErrOk);
3103 }
3104
3105 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
3106 {
3107 if ((value<0)||(value>0xFFFF))
3108 return(TIFFReadDirEntryErrRange);
3109 else
3110 return(TIFFReadDirEntryErrOk);
3111 }
3112
3113 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
3114 {
3115 if (value>0xFFFF)
3116 return(TIFFReadDirEntryErrRange);
3117 else
3118 return(TIFFReadDirEntryErrOk);
3119 }
3120
3121 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
3122 {
3123 if ((value<0)||(value>0xFFFF))
3124 return(TIFFReadDirEntryErrRange);
3125 else
3126 return(TIFFReadDirEntryErrOk);
3127 }
3128
3129 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
3130 {
3131 if (value>0x7FFF)
3132 return(TIFFReadDirEntryErrRange);
3133 else
3134 return(TIFFReadDirEntryErrOk);
3135 }
3136
3137 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
3138 {
3139 if (value>0x7FFF)
3140 return(TIFFReadDirEntryErrRange);
3141 else
3142 return(TIFFReadDirEntryErrOk);
3143 }
3144
3145 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
3146 {
3147 if ((value<-0x8000)||(value>0x7FFF))
3148 return(TIFFReadDirEntryErrRange);
3149 else
3150 return(TIFFReadDirEntryErrOk);
3151 }
3152
3153 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
3154 {
3155 if (value>0x7FFF)
3156 return(TIFFReadDirEntryErrRange);
3157 else
3158 return(TIFFReadDirEntryErrOk);
3159 }
3160
3161 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
3162 {
3163 if ((value<-0x8000)||(value>0x7FFF))
3164 return(TIFFReadDirEntryErrRange);
3165 else
3166 return(TIFFReadDirEntryErrOk);
3167 }
3168
3169 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
3170 {
3171 if (value<0)
3172 return(TIFFReadDirEntryErrRange);
3173 else
3174 return(TIFFReadDirEntryErrOk);
3175 }
3176
3177 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
3178 {
3179 if (value<0)
3180 return(TIFFReadDirEntryErrRange);
3181 else
3182 return(TIFFReadDirEntryErrOk);
3183 }
3184
3185 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
3186 {
3187 if (value<0)
3188 return(TIFFReadDirEntryErrRange);
3189 else
3190 return(TIFFReadDirEntryErrOk);
3191 }
3192
3193 /*
3194 * Largest 32-bit unsigned integer value.
3195 */
3196 #if defined(__WIN32__) && defined(_MSC_VER)
3197 # define TIFF_UINT32_MAX 0xFFFFFFFFI64
3198 #else
3199 # define TIFF_UINT32_MAX 0xFFFFFFFFLL
3200 #endif
3201
3202 static enum TIFFReadDirEntryErr
3203 TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
3204 {
3205 if (value > TIFF_UINT32_MAX)
3206 return(TIFFReadDirEntryErrRange);
3207 else
3208 return(TIFFReadDirEntryErrOk);
3209 }
3210
3211 static enum TIFFReadDirEntryErr
3212 TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
3213 {
3214 if ((value<0) || (value > TIFF_UINT32_MAX))
3215 return(TIFFReadDirEntryErrRange);
3216 else
3217 return(TIFFReadDirEntryErrOk);
3218 }
3219
3220 #undef TIFF_UINT32_MAX
3221
3222 static enum TIFFReadDirEntryErr
3223 TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
3224 {
3225 if (value > 0x7FFFFFFFUL)
3226 return(TIFFReadDirEntryErrRange);
3227 else
3228 return(TIFFReadDirEntryErrOk);
3229 }
3230
3231 static enum TIFFReadDirEntryErr
3232 TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
3233 {
3234 if (value > 0x7FFFFFFFUL)
3235 return(TIFFReadDirEntryErrRange);
3236 else
3237 return(TIFFReadDirEntryErrOk);
3238 }
3239
3240 static enum TIFFReadDirEntryErr
3241 TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
3242 {
3243 if ((value < 0L-0x80000000L) || (value > 0x7FFFFFFFL))
3244 return(TIFFReadDirEntryErrRange);
3245 else
3246 return(TIFFReadDirEntryErrOk);
3247 }
3248
3249 static enum TIFFReadDirEntryErr
3250 TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
3251 {
3252 if (value < 0)
3253 return(TIFFReadDirEntryErrRange);
3254 else
3255 return(TIFFReadDirEntryErrOk);
3256 }
3257
3258 static enum TIFFReadDirEntryErr
3259 TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
3260 {
3261 if (value < 0)
3262 return(TIFFReadDirEntryErrRange);
3263 else
3264 return(TIFFReadDirEntryErrOk);
3265 }
3266
3267 static enum TIFFReadDirEntryErr
3268 TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
3269 {
3270 if (value < 0)
3271 return(TIFFReadDirEntryErrRange);
3272 else
3273 return(TIFFReadDirEntryErrOk);
3274 }
3275
3276 static enum TIFFReadDirEntryErr
3277 TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
3278 {
3279 if (value < 0)
3280 return(TIFFReadDirEntryErrRange);
3281 else
3282 return(TIFFReadDirEntryErrOk);
3283 }
3284
3285 /*
3286 * Largest 64-bit signed integer value.
3287 */
3288 #if defined(__WIN32__) && defined(_MSC_VER)
3289 # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFI64
3290 #else
3291 # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFLL
3292 #endif
3293
3294 static enum TIFFReadDirEntryErr
3295 TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
3296 {
3297 if (value > TIFF_INT64_MAX)
3298 return(TIFFReadDirEntryErrRange);
3299 else
3300 return(TIFFReadDirEntryErrOk);
3301 }
3302
3303 #undef TIFF_INT64_MAX
3304
3305 static enum TIFFReadDirEntryErr
3306 TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
3307 {
3308 assert(size>0);
3309 if (!isMapped(tif)) {
3310 if (!SeekOK(tif,offset))
3311 return(TIFFReadDirEntryErrIo);
3312 if (!ReadOK(tif,dest,size))
3313 return(TIFFReadDirEntryErrIo);
3314 } else {
3315 size_t ma,mb;
3316 ma=(size_t)offset;
3317 mb=ma+size;
3318 if (((uint64)ma!=offset)
3319 || (mb < ma)
3320 || (mb - ma != (size_t) size)
3321 || (mb < (size_t)size)
3322 || (mb > (size_t)tif->tif_size)
3323 )
3324 return(TIFFReadDirEntryErrIo);
3325 _TIFFmemcpy(dest,tif->tif_base+ma,size);
3326 }
3327 return(TIFFReadDirEntryErrOk);
3328 }
3329
3330 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
3331 {
3332 if (!recover) {
3333 switch (err) {
3334 case TIFFReadDirEntryErrCount:
3335 TIFFErrorExt(tif->tif_clientdata, module,
3336 "Incorrect count for \"%s\"",
3337 tagname);
3338 break;
3339 case TIFFReadDirEntryErrType:
3340 TIFFErrorExt(tif->tif_clientdata, module,
3341 "Incompatible type for \"%s\"",
3342 tagname);
3343 break;
3344 case TIFFReadDirEntryErrIo:
3345 TIFFErrorExt(tif->tif_clientdata, module,
3346 "IO error during reading of \"%s\"",
3347 tagname);
3348 break;
3349 case TIFFReadDirEntryErrRange:
3350 TIFFErrorExt(tif->tif_clientdata, module,
3351 "Incorrect value for \"%s\"",
3352 tagname);
3353 break;
3354 case TIFFReadDirEntryErrPsdif:
3355 TIFFErrorExt(tif->tif_clientdata, module,
3356 "Cannot handle different values per sample for \"%s\"",
3357 tagname);
3358 break;
3359 case TIFFReadDirEntryErrSizesan:
3360 TIFFErrorExt(tif->tif_clientdata, module,
3361 "Sanity check on size of \"%s\" value failed",
3362 tagname);
3363 break;
3364 case TIFFReadDirEntryErrAlloc:
3365 TIFFErrorExt(tif->tif_clientdata, module,
3366 "Out of memory reading of \"%s\"",
3367 tagname);
3368 break;
3369 default:
3370 assert(0); /* we should never get here */
3371 break;
3372 }
3373 } else {
3374 switch (err) {
3375 case TIFFReadDirEntryErrCount:
3376 TIFFErrorExt(tif->tif_clientdata, module,
3377 "Incorrect count for \"%s\"; tag ignored",
3378 tagname);
3379 break;
3380 case TIFFReadDirEntryErrType:
3381 TIFFWarningExt(tif->tif_clientdata, module,
3382 "Incompatible type for \"%s\"; tag ignored",
3383 tagname);
3384 break;
3385 case TIFFReadDirEntryErrIo:
3386 TIFFWarningExt(tif->tif_clientdata, module,
3387 "IO error during reading of \"%s\"; tag ignored",
3388 tagname);
3389 break;
3390 case TIFFReadDirEntryErrRange:
3391 TIFFWarningExt(tif->tif_clientdata, module,
3392 "Incorrect value for \"%s\"; tag ignored",
3393 tagname);
3394 break;
3395 case TIFFReadDirEntryErrPsdif:
3396 TIFFWarningExt(tif->tif_clientdata, module,
3397 "Cannot handle different values per sample for \"%s\"; tag ignored",
3398 tagname);
3399 break;
3400 case TIFFReadDirEntryErrSizesan:
3401 TIFFWarningExt(tif->tif_clientdata, module,
3402 "Sanity check on size of \"%s\" value failed; tag ignored",
3403 tagname);
3404 break;
3405 case TIFFReadDirEntryErrAlloc:
3406 TIFFWarningExt(tif->tif_clientdata, module,
3407 "Out of memory reading of \"%s\"; tag ignored",
3408 tagname);
3409 break;
3410 default:
3411 assert(0); /* we should never get here */
3412 break;
3413 }
3414 }
3415 }
3416
3417 /*
3418 * Read the next TIFF directory from a file and convert it to the internal
3419 * format. We read directories sequentially.
3420 */
3421 int
3422 TIFFReadDirectory(TIFF* tif)
3423 {
3424 static const char module[] = "TIFFReadDirectory";
3425 TIFFDirEntry* dir;
3426 uint16 dircount;
3427 TIFFDirEntry* dp;
3428 uint16 di;
3429 const TIFFField* fip;
3430 uint32 fii=FAILED_FII;
3431 toff_t nextdiroff;
3432 tif->tif_diroff=tif->tif_nextdiroff;
3433 if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
3434 return 0; /* last offset or bad offset (IFD looping) */
3435 (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
3436 tif->tif_curdir++;
3437 nextdiroff = tif->tif_nextdiroff;
3438 dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
3439 if (!dircount)
3440 {
3441 TIFFErrorExt(tif->tif_clientdata,module,
3442 "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
3443 return 0;
3444 }
3445 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
3446
3447 /*
3448 * Mark duplicates of any tag to be ignored (bugzilla 1994)
3449 * to avoid certain pathological problems.
3450 */
3451 {
3452 TIFFDirEntry* ma;
3453 uint16 mb;
3454 for (ma=dir, mb=0; mb<dircount; ma++, mb++)
3455 {
3456 TIFFDirEntry* na;
3457 uint16 nb;
3458 for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
3459 {
3460 if (ma->tdir_tag==na->tdir_tag)
3461 na->tdir_tag=IGNORE;
3462 }
3463 }
3464 }
3465
3466 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
3467 tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
3468 /* free any old stuff and reinit */
3469 TIFFFreeDirectory(tif);
3470 TIFFDefaultDirectory(tif);
3471 /*
3472 * Electronic Arts writes gray-scale TIFF files
3473 * without a PlanarConfiguration directory entry.
3474 * Thus we setup a default value here, even though
3475 * the TIFF spec says there is no default value.
3476 */
3477 TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3478 /*
3479 * Setup default value and then make a pass over
3480 * the fields to check type and tag information,
3481 * and to extract info required to size data
3482 * structures. A second pass is made afterwards
3483 * to read in everthing not taken in the first pass.
3484 * But we must process the Compression tag first
3485 * in order to merge in codec-private tag definitions (otherwise
3486 * we may get complaints about unknown tags). However, the
3487 * Compression tag may be dependent on the SamplesPerPixel
3488 * tag value because older TIFF specs permited Compression
3489 * to be written as a SamplesPerPixel-count tag entry.
3490 * Thus if we don't first figure out the correct SamplesPerPixel
3491 * tag value then we may end up ignoring the Compression tag
3492 * value because it has an incorrect count value (if the
3493 * true value of SamplesPerPixel is not 1).
3494 */
3495 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
3496 if (dp)
3497 {
3498 if (!TIFFFetchNormalTag(tif,dp,0))
3499 goto bad;
3500 dp->tdir_tag=IGNORE;
3501 }
3502 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
3503 if (dp)
3504 {
3505 /*
3506 * The 5.0 spec says the Compression tag has one value, while
3507 * earlier specs say it has one value per sample. Because of
3508 * this, we accept the tag if one value is supplied with either
3509 * count.
3510 */
3511 uint16 value;
3512 enum TIFFReadDirEntryErr err;
3513 err=TIFFReadDirEntryShort(tif,dp,&value);
3514 if (err==TIFFReadDirEntryErrCount)
3515 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3516 if (err!=TIFFReadDirEntryErrOk)
3517 {
3518 TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
3519 goto bad;
3520 }
3521 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
3522 goto bad;
3523 dp->tdir_tag=IGNORE;
3524 }
3525 else
3526 {
3527 if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
3528 goto bad;
3529 }
3530 /*
3531 * First real pass over the directory.
3532 */
3533 for (di=0, dp=dir; di<dircount; di++, dp++)
3534 {
3535 if (dp->tdir_tag!=IGNORE)
3536 {
3537 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3538 if (fii == FAILED_FII)
3539 {
3540 TIFFWarningExt(tif->tif_clientdata, module,
3541 "Unknown field with tag %d (0x%x) encountered",
3542 dp->tdir_tag,dp->tdir_tag);
3543 /* the following knowingly leaks the
3544 anonymous field structure */
3545 if (!_TIFFMergeFields(tif,
3546 _TIFFCreateAnonField(tif,
3547 dp->tdir_tag,
3548 (TIFFDataType) dp->tdir_type),
3549 1)) {
3550 TIFFWarningExt(tif->tif_clientdata,
3551 module,
3552 "Registering anonymous field with tag %d (0x%x) failed",
3553 dp->tdir_tag,
3554 dp->tdir_tag);
3555 dp->tdir_tag=IGNORE;
3556 } else {
3557 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
3558 assert(fii != FAILED_FII);
3559 }
3560 }
3561 }
3562 if (dp->tdir_tag!=IGNORE)
3563 {
3564 fip=tif->tif_fields[fii];
3565 if (fip->field_bit==FIELD_IGNORE)
3566 dp->tdir_tag=IGNORE;
3567 else
3568 {
3569 switch (dp->tdir_tag)
3570 {
3571 case TIFFTAG_STRIPOFFSETS:
3572 case TIFFTAG_STRIPBYTECOUNTS:
3573 case TIFFTAG_TILEOFFSETS:
3574 case TIFFTAG_TILEBYTECOUNTS:
3575 TIFFSetFieldBit(tif,fip->field_bit);
3576 break;
3577 case TIFFTAG_IMAGEWIDTH:
3578 case TIFFTAG_IMAGELENGTH:
3579 case TIFFTAG_IMAGEDEPTH:
3580 case TIFFTAG_TILELENGTH:
3581 case TIFFTAG_TILEWIDTH:
3582 case TIFFTAG_TILEDEPTH:
3583 case TIFFTAG_PLANARCONFIG:
3584 case TIFFTAG_ROWSPERSTRIP:
3585 case TIFFTAG_EXTRASAMPLES:
3586 if (!TIFFFetchNormalTag(tif,dp,0))
3587 goto bad;
3588 dp->tdir_tag=IGNORE;
3589 break;
3590 }
3591 }
3592 }
3593 }
3594 /*
3595 * XXX: OJPEG hack.
3596 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
3597 * c) strip offsets/bytecounts tag are both present and
3598 * d) both contain exactly one value, then we consistently find
3599 * that the buggy implementation of the buggy compression scheme
3600 * matches contig planarconfig best. So we 'fix-up' the tag here
3601 */
3602 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
3603 (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
3604 {
3605 if (!_TIFFFillStriles(tif))
3606 goto bad;
3607 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
3608 if ((dp!=0)&&(dp->tdir_count==1))
3609 {
3610 dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
3611 TIFFTAG_STRIPBYTECOUNTS);
3612 if ((dp!=0)&&(dp->tdir_count==1))
3613 {
3614 tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
3615 TIFFWarningExt(tif->tif_clientdata,module,
3616 "Planarconfig tag value assumed incorrect, "
3617 "assuming data is contig instead of chunky");
3618 }
3619 }
3620 }
3621 /*
3622 * Allocate directory structure and setup defaults.
3623 */
3624 if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
3625 {
3626 MissingRequired(tif,"ImageLength");
3627 goto bad;
3628 }
3629 /*
3630 * Setup appropriate structures (by strip or by tile)
3631 */
3632 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3633 tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
3634 tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3635 tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
3636 tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
3637 tif->tif_flags &= ~TIFF_ISTILED;
3638 } else {
3639 tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
3640 tif->tif_flags |= TIFF_ISTILED;
3641 }
3642 if (!tif->tif_dir.td_nstrips) {
3643 TIFFErrorExt(tif->tif_clientdata, module,
3644 "Cannot handle zero number of %s",
3645 isTiled(tif) ? "tiles" : "strips");
3646 goto bad;
3647 }
3648 tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
3649 if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
3650 tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
3651 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
3652 if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
3653 (isTiled(tif)==0) &&
3654 (tif->tif_dir.td_nstrips==1)) {
3655 /*
3656 * XXX: OJPEG hack.
3657 * If a) compression is OJPEG, b) it's not a tiled TIFF,
3658 * and c) the number of strips is 1,
3659 * then we tolerate the absence of stripoffsets tag,
3660 * because, presumably, all required data is in the
3661 * JpegInterchangeFormat stream.
3662 */
3663 TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
3664 } else {
3665 MissingRequired(tif,
3666 isTiled(tif) ? "TileOffsets" : "StripOffsets");
3667 goto bad;
3668 }
3669 }
3670 /*
3671 * Second pass: extract other information.
3672 */
3673 for (di=0, dp=dir; di<dircount; di++, dp++)
3674 {
3675 switch (dp->tdir_tag)
3676 {
3677 case IGNORE:
3678 break;
3679 case TIFFTAG_MINSAMPLEVALUE:
3680 case TIFFTAG_MAXSAMPLEVALUE:
3681 case TIFFTAG_BITSPERSAMPLE:
3682 case TIFFTAG_DATATYPE:
3683 case TIFFTAG_SAMPLEFORMAT:
3684 /*
3685 * The MinSampleValue, MaxSampleValue, BitsPerSample
3686 * DataType and SampleFormat tags are supposed to be
3687 * written as one value/sample, but some vendors
3688 * incorrectly write one value only -- so we accept
3689 * that as well (yech). Other vendors write correct
3690 * value for NumberOfSamples, but incorrect one for
3691 * BitsPerSample and friends, and we will read this
3692 * too.
3693 */
3694 {
3695 uint16 value;
3696 enum TIFFReadDirEntryErr err;
3697 err=TIFFReadDirEntryShort(tif,dp,&value);
3698 if (err==TIFFReadDirEntryErrCount)
3699 err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
3700 if (err!=TIFFReadDirEntryErrOk)
3701 {
3702 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3703 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3704 goto bad;
3705 }
3706 if (!TIFFSetField(tif,dp->tdir_tag,value))
3707 goto bad;
3708 }
3709 break;
3710 case TIFFTAG_SMINSAMPLEVALUE:
3711 case TIFFTAG_SMAXSAMPLEVALUE:
3712 {
3713
3714 double *data;
3715 enum TIFFReadDirEntryErr err;
3716 uint32 saved_flags;
3717 int m;
3718 if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
3719 err = TIFFReadDirEntryErrCount;
3720 else
3721 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
3722 if (err!=TIFFReadDirEntryErrOk)
3723 {
3724 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3725 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
3726 goto bad;
3727 }
3728 saved_flags = tif->tif_flags;
3729 tif->tif_flags |= TIFF_PERSAMPLE;
3730 m = TIFFSetField(tif,dp->tdir_tag,data);
3731 tif->tif_flags = saved_flags;
3732 _TIFFfree(data);
3733 if (!m)
3734 goto bad;
3735 }
3736 break;
3737 case TIFFTAG_STRIPOFFSETS:
3738 case TIFFTAG_TILEOFFSETS:
3739 #if defined(DEFER_STRILE_LOAD)
3740 _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
3741 dp, sizeof(TIFFDirEntry) );
3742 #else
3743 if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
3744 goto bad;
3745 #endif
3746 break;
3747 case TIFFTAG_STRIPBYTECOUNTS:
3748 case TIFFTAG_TILEBYTECOUNTS:
3749 #if defined(DEFER_STRILE_LOAD)
3750 _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
3751 dp, sizeof(TIFFDirEntry) );
3752 #else
3753 if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
3754 goto bad;
3755 #endif
3756 break;
3757 case TIFFTAG_COLORMAP:
3758 case TIFFTAG_TRANSFERFUNCTION:
3759 {
3760 enum TIFFReadDirEntryErr err;
3761 uint32 countpersample;
3762 uint32 countrequired;
3763 uint32 incrementpersample;
3764 uint16* value=NULL;
3765 countpersample=(1L<<tif->tif_dir.td_bitspersample);
3766 if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
3767 {
3768 countrequired=countpersample;
3769 incrementpersample=0;
3770 }
3771 else
3772 {
3773 countrequired=3*countpersample;
3774 incrementpersample=countpersample;
3775 }
3776 if (dp->tdir_count!=(uint64)countrequired)
3777 err=TIFFReadDirEntryErrCount;
3778 else
3779 err=TIFFReadDirEntryShortArray(tif,dp,&value);
3780 if (err!=TIFFReadDirEntryErrOk)
3781 {
3782 fip = TIFFFieldWithTag(tif,dp->tdir_tag);
3783 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
3784 }
3785 else
3786 {
3787 TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
3788 _TIFFfree(value);
3789 }
3790 }
3791 break;
3792 /* BEGIN REV 4.0 COMPATIBILITY */
3793 case TIFFTAG_OSUBFILETYPE:
3794 {
3795 uint16 valueo;
3796 uint32 value;
3797 if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
3798 {
3799 switch (valueo)
3800 {
3801 case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
3802 case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
3803 default: value=0; break;
3804 }
3805 if (value!=0)
3806 TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
3807 }
3808 }
3809 break;
3810 /* END REV 4.0 COMPATIBILITY */
3811 default:
3812 (void) TIFFFetchNormalTag(tif, dp, TRUE);
3813 break;
3814 }
3815 }
3816 /*
3817 * OJPEG hack:
3818 * - If a) compression is OJPEG, and b) photometric tag is missing,
3819 * then we consistently find that photometric should be YCbCr
3820 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
3821 * then we consistently find that the buggy implementation of the
3822 * buggy compression scheme matches photometric YCbCr instead.
3823 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
3824 * then we consistently find bitspersample should be 8.
3825 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3826 * and c) photometric is RGB or YCbCr, then we consistently find
3827 * samplesperpixel should be 3
3828 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
3829 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
3830 * find samplesperpixel should be 3
3831 */
3832 if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
3833 {
3834 if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
3835 {
3836 TIFFWarningExt(tif->tif_clientdata, module,
3837 "Photometric tag is missing, assuming data is YCbCr");
3838 if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
3839 goto bad;
3840 }
3841 else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
3842 {
3843 tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
3844 TIFFWarningExt(tif->tif_clientdata, module,
3845 "Photometric tag value assumed incorrect, "
3846 "assuming data is YCbCr instead of RGB");
3847 }
3848 if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
3849 {
3850 TIFFWarningExt(tif->tif_clientdata,module,
3851 "BitsPerSample tag is missing, assuming 8 bits per sample");
3852 if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
3853 goto bad;
3854 }
3855 if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
3856 {
3857 if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
3858 {
3859 TIFFWarningExt(tif->tif_clientdata,module,
3860 "SamplesPerPixel tag is missing, "
3861 "assuming correct SamplesPerPixel value is 3");
3862 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
3863 goto bad;
3864 }
3865 if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
3866 {
3867 TIFFWarningExt(tif->tif_clientdata,module,
3868 "SamplesPerPixel tag is missing, "
3869 "applying correct SamplesPerPixel value of 3");
3870 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
3871 goto bad;
3872 }
3873 else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
3874 || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
3875 {
3876 /*
3877 * SamplesPerPixel tag is missing, but is not required
3878 * by spec. Assume correct SamplesPerPixel value of 1.
3879 */
3880 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
3881 goto bad;
3882 }
3883 }
3884 }
3885 /*
3886 * Verify Palette image has a Colormap.
3887 */
3888 if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
3889 !TIFFFieldSet(tif, FIELD_COLORMAP)) {
3890 if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
3891 tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
3892 else if (tif->tif_dir.td_bitspersample>=8)
3893 tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
3894 else {
3895 MissingRequired(tif, "Colormap");
3896 goto bad;
3897 }
3898 }
3899 /*
3900 * OJPEG hack:
3901 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
3902 * TIFFs
3903 */
3904 if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
3905 {
3906 /*
3907 * Attempt to deal with a missing StripByteCounts tag.
3908 */
3909 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
3910 /*
3911 * Some manufacturers violate the spec by not giving
3912 * the size of the strips. In this case, assume there
3913 * is one uncompressed strip of data.
3914 */
3915 if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
3916 tif->tif_dir.td_nstrips > 1) ||
3917 (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
3918 tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
3919 MissingRequired(tif, "StripByteCounts");
3920 goto bad;
3921 }
3922 TIFFWarningExt(tif->tif_clientdata, module,
3923 "TIFF directory is missing required "
3924 "\"StripByteCounts\" field, calculating from imagelength");
3925 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
3926 goto bad;
3927 /*
3928 * Assume we have wrong StripByteCount value (in case
3929 * of single strip) in following cases:
3930 * - it is equal to zero along with StripOffset;
3931 * - it is larger than file itself (in case of uncompressed
3932 * image);
3933 * - it is smaller than the size of the bytes per row
3934 * multiplied on the number of rows. The last case should
3935 * not be checked in the case of writing new image,
3936 * because we may do not know the exact strip size
3937 * until the whole image will be written and directory
3938 * dumped out.
3939 */
3940 #define BYTECOUNTLOOKSBAD \
3941 ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
3942 (tif->tif_dir.td_compression == COMPRESSION_NONE && \
3943 tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
3944 (tif->tif_mode == O_RDONLY && \
3945 tif->tif_dir.td_compression == COMPRESSION_NONE && \
3946 tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
3947
3948 } else if (tif->tif_dir.td_nstrips == 1
3949 && _TIFFFillStriles(tif)
3950 && tif->tif_dir.td_stripoffset[0] != 0
3951 && BYTECOUNTLOOKSBAD) {
3952 /*
3953 * XXX: Plexus (and others) sometimes give a value of
3954 * zero for a tag when they don't know what the
3955 * correct value is! Try and handle the simple case
3956 * of estimating the size of a one strip image.
3957 */
3958 TIFFWarningExt(tif->tif_clientdata, module,
3959 "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
3960 if(EstimateStripByteCounts(tif, dir, dircount) < 0)
3961 goto bad;
3962
3963 #if !defined(DEFER_STRILE_LOAD)
3964 } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
3965 && tif->tif_dir.td_nstrips > 2
3966 && tif->tif_dir.td_compression == COMPRESSION_NONE
3967 && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
3968 && tif->tif_dir.td_stripbytecount[0] != 0
3969 && tif->tif_dir.td_stripbytecount[1] != 0 ) {
3970 /*
3971 * XXX: Some vendors fill StripByteCount array with
3972 * absolutely wrong values (it can be equal to
3973 * StripOffset array, for example). Catch this case
3974 * here.
3975 *
3976 * We avoid this check if deferring strile loading
3977 * as it would always force us to load the strip/tile
3978 * information.
3979 */
3980 TIFFWarningExt(tif->tif_clientdata, module,
3981 "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
3982 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
3983 goto bad;
3984 #endif /* !defined(DEFER_STRILE_LOAD) */
3985 }
3986 }
3987 if (dir)
3988 {
3989 _TIFFfree(dir);
3990 dir=NULL;
3991 }
3992 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
3993 {
3994 if (tif->tif_dir.td_bitspersample>=16)
3995 tif->tif_dir.td_maxsamplevalue=0xFFFF;
3996 else
3997 tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
3998 }
3999 /*
4000 * XXX: We can optimize checking for the strip bounds using the sorted
4001 * bytecounts array. See also comments for TIFFAppendToStrip()
4002 * function in tif_write.c.
4003 */
4004 #if !defined(DEFER_STRILE_LOAD)
4005 if (tif->tif_dir.td_nstrips > 1) {
4006 uint32 strip;
4007
4008 tif->tif_dir.td_stripbytecountsorted = 1;
4009 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
4010 if (tif->tif_dir.td_stripoffset[strip - 1] >
4011 tif->tif_dir.td_stripoffset[strip]) {
4012 tif->tif_dir.td_stripbytecountsorted = 0;
4013 break;
4014 }
4015 }
4016 }
4017 #endif /* !defined(DEFER_STRILE_LOAD) */
4018
4019 /*
4020 * An opportunity for compression mode dependent tag fixup
4021 */
4022 (*tif->tif_fixuptags)(tif);
4023
4024 /*
4025 * Some manufacturers make life difficult by writing
4026 * large amounts of uncompressed data as a single strip.
4027 * This is contrary to the recommendations of the spec.
4028 * The following makes an attempt at breaking such images
4029 * into strips closer to the recommended 8k bytes. A
4030 * side effect, however, is that the RowsPerStrip tag
4031 * value may be changed.
4032 */
4033 if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
4034 (tif->tif_dir.td_nstrips==1)&&
4035 (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
4036 ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
4037 {
4038 if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
4039 return 0;
4040 ChopUpSingleUncompressedStrip(tif);
4041 }
4042
4043 /*
4044 * Clear the dirty directory flag.
4045 */
4046 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
4047 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
4048
4049 /*
4050 * Reinitialize i/o since we are starting on a new directory.
4051 */
4052 tif->tif_row = (uint32) -1;
4053 tif->tif_curstrip = (uint32) -1;
4054 tif->tif_col = (uint32) -1;
4055 tif->tif_curtile = (uint32) -1;
4056 tif->tif_tilesize = (tmsize_t) -1;
4057
4058 tif->tif_scanlinesize = TIFFScanlineSize(tif);
4059 if (!tif->tif_scanlinesize) {
4060 TIFFErrorExt(tif->tif_clientdata, module,
4061 "Cannot handle zero scanline size");
4062 return (0);
4063 }
4064
4065 if (isTiled(tif)) {
4066 tif->tif_tilesize = TIFFTileSize(tif);
4067 if (!tif->tif_tilesize) {
4068 TIFFErrorExt(tif->tif_clientdata, module,
4069 "Cannot handle zero tile size");
4070 return (0);
4071 }
4072 } else {
4073 if (!TIFFStripSize(tif)) {
4074 TIFFErrorExt(tif->tif_clientdata, module,
4075 "Cannot handle zero strip size");
4076 return (0);
4077 }
4078 }
4079 return (1);
4080 bad:
4081 if (dir)
4082 _TIFFfree(dir);
4083 return (0);
4084 }
4085
4086 static void
4087 TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4088 {
4089 static const char module[] = "TIFFReadDirectoryCheckOrder";
4090 uint16 m;
4091 uint16 n;
4092 TIFFDirEntry* o;
4093 m=0;
4094 for (n=0, o=dir; n<dircount; n++, o++)
4095 {
4096 if (o->tdir_tag<m)
4097 {
4098 TIFFWarningExt(tif->tif_clientdata,module,
4099 "Invalid TIFF directory; tags are not sorted in ascending order");
4100 break;
4101 }
4102 m=o->tdir_tag+1;
4103 }
4104 }
4105
4106 static TIFFDirEntry*
4107 TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
4108 {
4109 TIFFDirEntry* m;
4110 uint16 n;
4111 (void) tif;
4112 for (m=dir, n=0; n<dircount; m++, n++)
4113 {
4114 if (m->tdir_tag==tagid)
4115 return(m);
4116 }
4117 return(0);
4118 }
4119
4120 static void
4121 TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
4122 {
4123 int32 ma,mb,mc;
4124 ma=-1;
4125 mc=(int32)tif->tif_nfields;
4126 while (1)
4127 {
4128 if (ma+1==mc)
4129 {
4130 *fii = FAILED_FII;
4131 return;
4132 }
4133 mb=(ma+mc)/2;
4134 if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
4135 break;
4136 if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
4137 ma=mb;
4138 else
4139 mc=mb;
4140 }
4141 while (1)
4142 {
4143 if (mb==0)
4144 break;
4145 if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
4146 break;
4147 mb--;
4148 }
4149 *fii=mb;
4150 }
4151
4152 /*
4153 * Read custom directory from the arbitarry offset.
4154 * The code is very similar to TIFFReadDirectory().
4155 */
4156 int
4157 TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
4158 const TIFFFieldArray* infoarray)
4159 {
4160 static const char module[] = "TIFFReadCustomDirectory";
4161 TIFFDirEntry* dir;
4162 uint16 dircount;
4163 TIFFDirEntry* dp;
4164 uint16 di;
4165 const TIFFField* fip;
4166 uint32 fii;
4167 _TIFFSetupFields(tif, infoarray);
4168 dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
4169 if (!dircount)
4170 {
4171 TIFFErrorExt(tif->tif_clientdata,module,
4172 "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
4173 return 0;
4174 }
4175 TIFFFreeDirectory(tif);
4176 _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
4177 TIFFReadDirectoryCheckOrder(tif,dir,dircount);
4178 for (di=0, dp=dir; di<dircount; di++, dp++)
4179 {
4180 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4181 if (fii == FAILED_FII)
4182 {
4183 TIFFWarningExt(tif->tif_clientdata, module,
4184 "Unknown field with tag %d (0x%x) encountered",
4185 dp->tdir_tag, dp->tdir_tag);
4186 if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
4187 dp->tdir_tag,
4188 (TIFFDataType) dp->tdir_type),
4189 1)) {
4190 TIFFWarningExt(tif->tif_clientdata, module,
4191 "Registering anonymous field with tag %d (0x%x) failed",
4192 dp->tdir_tag, dp->tdir_tag);
4193 dp->tdir_tag=IGNORE;
4194 } else {
4195 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4196 assert( fii != FAILED_FII );
4197 }
4198 }
4199 if (dp->tdir_tag!=IGNORE)
4200 {
4201 fip=tif->tif_fields[fii];
4202 if (fip->field_bit==FIELD_IGNORE)
4203 dp->tdir_tag=IGNORE;
4204 else
4205 {
4206 /* check data type */
4207 while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
4208 {
4209 fii++;
4210 if ((fii==tif->tif_nfields)||
4211 (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
4212 {
4213 fii=0xFFFF;
4214 break;
4215 }
4216 fip=tif->tif_fields[fii];
4217 }
4218 if (fii==0xFFFF)
4219 {
4220 TIFFWarningExt(tif->tif_clientdata, module,
4221 "Wrong data type %d for \"%s\"; tag ignored",
4222 dp->tdir_type,fip->field_name);
4223 dp->tdir_tag=IGNORE;
4224 }
4225 else
4226 {
4227 /* check count if known in advance */
4228 if ((fip->field_readcount!=TIFF_VARIABLE)&&
4229 (fip->field_readcount!=TIFF_VARIABLE2))
4230 {
4231 uint32 expected;
4232 if (fip->field_readcount==TIFF_SPP)
4233 expected=(uint32)tif->tif_dir.td_samplesperpixel;
4234 else
4235 expected=(uint32)fip->field_readcount;
4236 if (!CheckDirCount(tif,dp,expected))
4237 dp->tdir_tag=IGNORE;
4238 }
4239 }
4240 }
4241 switch (dp->tdir_tag)
4242 {
4243 case IGNORE:
4244 break;
4245 case EXIFTAG_SUBJECTDISTANCE:
4246 (void) TIFFFetchSubjectDistance(tif,dp);
4247 break;
4248 default:
4249 (void) TIFFFetchNormalTag(tif, dp, TRUE);
4250 break;
4251 }
4252 }
4253 }
4254 if (dir)
4255 _TIFFfree(dir);
4256 return 1;
4257 }
4258
4259 /*
4260 * EXIF is important special case of custom IFD, so we have a special
4261 * function to read it.
4262 */
4263 int
4264 TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
4265 {
4266 const TIFFFieldArray* exifFieldArray;
4267 exifFieldArray = _TIFFGetExifFields();
4268 return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
4269 }
4270
4271 static int
4272 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4273 {
4274 static const char module[] = "EstimateStripByteCounts";
4275
4276 TIFFDirEntry *dp;
4277 TIFFDirectory *td = &tif->tif_dir;
4278 uint32 strip;
4279
4280 _TIFFFillStriles( tif );
4281
4282 if (td->td_stripbytecount)
4283 _TIFFfree(td->td_stripbytecount);
4284 td->td_stripbytecount = (uint64*)
4285 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
4286 "for \"StripByteCounts\" array");
4287 if( td->td_stripbytecount == NULL )
4288 return -1;
4289
4290 if (td->td_compression != COMPRESSION_NONE) {
4291 uint64 space;
4292 uint64 filesize;
4293 uint16 n;
4294 filesize = TIFFGetFileSize(tif);
4295 if (!(tif->tif_flags&TIFF_BIGTIFF))
4296 space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
4297 else
4298 space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
4299 /* calculate amount of space used by indirect values */
4300 for (dp = dir, n = dircount; n > 0; n--, dp++)
4301 {
4302 uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4303 uint64 datasize;
4304 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
4305 if (typewidth == 0) {
4306 TIFFErrorExt(tif->tif_clientdata, module,
4307 "Cannot determine size of unknown tag type %d",
4308 dp->tdir_type);
4309 return -1;
4310 }
4311 datasize=(uint64)typewidth*dp->tdir_count;
4312 if (!(tif->tif_flags&TIFF_BIGTIFF))
4313 {
4314 if (datasize<=4)
4315 datasize=0;
4316 }
4317 else
4318 {
4319 if (datasize<=8)
4320 datasize=0;
4321 }
4322 space+=datasize;
4323 }
4324 space = filesize - space;
4325 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
4326 space /= td->td_samplesperpixel;
4327 for (strip = 0; strip < td->td_nstrips; strip++)
4328 td->td_stripbytecount[strip] = space;
4329 /*
4330 * This gross hack handles the case were the offset to
4331 * the last strip is past the place where we think the strip
4332 * should begin. Since a strip of data must be contiguous,
4333 * it's safe to assume that we've overestimated the amount
4334 * of data in the strip and trim this number back accordingly.
4335 */
4336 strip--;
4337 if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
4338 td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
4339 } else if (isTiled(tif)) {
4340 uint64 bytespertile = TIFFTileSize64(tif);
4341
4342 for (strip = 0; strip < td->td_nstrips; strip++)
4343 td->td_stripbytecount[strip] = bytespertile;
4344 } else {
4345 uint64 rowbytes = TIFFScanlineSize64(tif);
4346 uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
4347 for (strip = 0; strip < td->td_nstrips; strip++)
4348 td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
4349 }
4350 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
4351 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
4352 td->td_rowsperstrip = td->td_imagelength;
4353 return 1;
4354 }
4355
4356 static void
4357 MissingRequired(TIFF* tif, const char* tagname)
4358 {
4359 static const char module[] = "MissingRequired";
4360
4361 TIFFErrorExt(tif->tif_clientdata, module,
4362 "TIFF directory is missing required \"%s\" field",
4363 tagname);
4364 }
4365
4366 /*
4367 * Check the directory offset against the list of already seen directory
4368 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
4369 * file with looped directory pointers. We will maintain a list of already
4370 * seen directories and check every IFD offset against that list.
4371 */
4372 static int
4373 TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
4374 {
4375 uint16 n;
4376
4377 if (diroff == 0) /* no more directories */
4378 return 0;
4379
4380 for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
4381 if (tif->tif_dirlist[n] == diroff)
4382 return 0;
4383 }
4384
4385 tif->tif_dirnumber++;
4386
4387 if (tif->tif_dirnumber > tif->tif_dirlistsize) {
4388 uint64* new_dirlist;
4389
4390 /*
4391 * XXX: Reduce memory allocation granularity of the dirlist
4392 * array.
4393 */
4394 new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
4395 tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
4396 if (!new_dirlist)
4397 return 0;
4398 tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
4399 tif->tif_dirlist = new_dirlist;
4400 }
4401
4402 tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
4403
4404 return 1;
4405 }
4406
4407 /*
4408 * Check the count field of a directory entry against a known value. The
4409 * caller is expected to skip/ignore the tag if there is a mismatch.
4410 */
4411 static int
4412 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
4413 {
4414 if ((uint64)count > dir->tdir_count) {
4415 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4416 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4417 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
4418 fip ? fip->field_name : "unknown tagname",
4419 dir->tdir_count, count);
4420 return (0);
4421 } else if ((uint64)count < dir->tdir_count) {
4422 const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
4423 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
4424 "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
4425 fip ? fip->field_name : "unknown tagname",
4426 dir->tdir_count, count);
4427 dir->tdir_count = count;
4428 return (1);
4429 }
4430 return (1);
4431 }
4432
4433 /*
4434 * Read IFD structure from the specified offset. If the pointer to
4435 * nextdiroff variable has been specified, read it too. Function returns a
4436 * number of fields in the directory or 0 if failed.
4437 */
4438 static uint16
4439 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
4440 uint64 *nextdiroff)
4441 {
4442 static const char module[] = "TIFFFetchDirectory";
4443
4444 void* origdir;
4445 uint16 dircount16;
4446 uint32 dirsize;
4447 TIFFDirEntry* dir;
4448 uint8* ma;
4449 TIFFDirEntry* mb;
4450 uint16 n;
4451
4452 assert(pdir);
4453
4454 tif->tif_diroff = diroff;
4455 if (nextdiroff)
4456 *nextdiroff = 0;
4457 if (!isMapped(tif)) {
4458 if (!SeekOK(tif, tif->tif_diroff)) {
4459 TIFFErrorExt(tif->tif_clientdata, module,
4460 "%s: Seek error accessing TIFF directory",
4461 tif->tif_name);
4462 return 0;
4463 }
4464 if (!(tif->tif_flags&TIFF_BIGTIFF))
4465 {
4466 if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
4467 TIFFErrorExt(tif->tif_clientdata, module,
4468 "%s: Can not read TIFF directory count",
4469 tif->tif_name);
4470 return 0;
4471 }
4472 if (tif->tif_flags & TIFF_SWAB)
4473 TIFFSwabShort(&dircount16);
4474 if (dircount16>4096)
4475 {
4476 TIFFErrorExt(tif->tif_clientdata, module,
4477 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4478 return 0;
4479 }
4480 dirsize = 12;
4481 } else {
4482 uint64 dircount64;
4483 if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
4484 TIFFErrorExt(tif->tif_clientdata, module,
4485 "%s: Can not read TIFF directory count",
4486 tif->tif_name);
4487 return 0;
4488 }
4489 if (tif->tif_flags & TIFF_SWAB)
4490 TIFFSwabLong8(&dircount64);
4491 if (dircount64>4096)
4492 {
4493 TIFFErrorExt(tif->tif_clientdata, module,
4494 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4495 return 0;
4496 }
4497 dircount16 = (uint16)dircount64;
4498 dirsize = 20;
4499 }
4500 origdir = _TIFFCheckMalloc(tif, dircount16,
4501 dirsize, "to read TIFF directory");
4502 if (origdir == NULL)
4503 return 0;
4504 if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
4505 TIFFErrorExt(tif->tif_clientdata, module,
4506 "%.100s: Can not read TIFF directory",
4507 tif->tif_name);
4508 _TIFFfree(origdir);
4509 return 0;
4510 }
4511 /*
4512 * Read offset to next directory for sequential scans if
4513 * needed.
4514 */
4515 if (nextdiroff)
4516 {
4517 if (!(tif->tif_flags&TIFF_BIGTIFF))
4518 {
4519 uint32 nextdiroff32;
4520 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
4521 nextdiroff32 = 0;
4522 if (tif->tif_flags&TIFF_SWAB)
4523 TIFFSwabLong(&nextdiroff32);
4524 *nextdiroff=nextdiroff32;
4525 } else {
4526 if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
4527 *nextdiroff = 0;
4528 if (tif->tif_flags&TIFF_SWAB)
4529 TIFFSwabLong8(nextdiroff);
4530 }
4531 }
4532 } else {
4533 tmsize_t m;
4534 tmsize_t off = (tmsize_t) tif->tif_diroff;
4535 if ((uint64)off!=tif->tif_diroff)
4536 {
4537 TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
4538 return(0);
4539 }
4540
4541 /*
4542 * Check for integer overflow when validating the dir_off,
4543 * otherwise a very high offset may cause an OOB read and
4544 * crash the client. Make two comparisons instead of
4545 *
4546 * off + sizeof(uint16) > tif->tif_size
4547 *
4548 * to avoid overflow.
4549 */
4550 if (!(tif->tif_flags&TIFF_BIGTIFF))
4551 {
4552 m=off+sizeof(uint16);
4553 if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
4554 TIFFErrorExt(tif->tif_clientdata, module,
4555 "Can not read TIFF directory count");
4556 return 0;
4557 } else {
4558 _TIFFmemcpy(&dircount16, tif->tif_base + off,
4559 sizeof(uint16));
4560 }
4561 off += sizeof (uint16);
4562 if (tif->tif_flags & TIFF_SWAB)
4563 TIFFSwabShort(&dircount16);
4564 if (dircount16>4096)
4565 {
4566 TIFFErrorExt(tif->tif_clientdata, module,
4567 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4568 return 0;
4569 }
4570 dirsize = 12;
4571 }
4572 else
4573 {
4574 tmsize_t m;
4575 uint64 dircount64;
4576 m=off+sizeof(uint64);
4577 if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
4578 TIFFErrorExt(tif->tif_clientdata, module,
4579 "Can not read TIFF directory count");
4580 return 0;
4581 } else {
4582 _TIFFmemcpy(&dircount64, tif->tif_base + off,
4583 sizeof(uint64));
4584 }
4585 off += sizeof (uint64);
4586 if (tif->tif_flags & TIFF_SWAB)
4587 TIFFSwabLong8(&dircount64);
4588 if (dircount64>4096)
4589 {
4590 TIFFErrorExt(tif->tif_clientdata, module,
4591 "Sanity check on directory count failed, this is probably not a valid IFD offset");
4592 return 0;
4593 }
4594 dircount16 = (uint16)dircount64;
4595 dirsize = 20;
4596 }
4597 if (dircount16 == 0 )
4598 {
4599 TIFFErrorExt(tif->tif_clientdata, module,
4600 "Sanity check on directory count failed, zero tag directories not supported");
4601 return 0;
4602 }
4603 origdir = _TIFFCheckMalloc(tif, dircount16,
4604 dirsize,
4605 "to read TIFF directory");
4606 if (origdir == NULL)
4607 return 0;
4608 m=off+dircount16*dirsize;
4609 if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
4610 TIFFErrorExt(tif->tif_clientdata, module,
4611 "Can not read TIFF directory");
4612 _TIFFfree(origdir);
4613 return 0;
4614 } else {
4615 _TIFFmemcpy(origdir, tif->tif_base + off,
4616 dircount16 * dirsize);
4617 }
4618 if (nextdiroff) {
4619 off += dircount16 * dirsize;
4620 if (!(tif->tif_flags&TIFF_BIGTIFF))
4621 {
4622 uint32 nextdiroff32;
4623 m=off+sizeof(uint32);
4624 if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
4625 nextdiroff32 = 0;
4626 else
4627 _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
4628 sizeof (uint32));
4629 if (tif->tif_flags&TIFF_SWAB)
4630 TIFFSwabLong(&nextdiroff32);
4631 *nextdiroff = nextdiroff32;
4632 }
4633 else
4634 {
4635 m=off+sizeof(uint64);
4636 if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
4637 *nextdiroff = 0;
4638 else
4639 _TIFFmemcpy(nextdiroff, tif->tif_base + off,
4640 sizeof (uint64));
4641 if (tif->tif_flags&TIFF_SWAB)
4642 TIFFSwabLong8(nextdiroff);
4643 }
4644 }
4645 }
4646 dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
4647 sizeof(TIFFDirEntry),
4648 "to read TIFF directory");
4649 if (dir==0)
4650 {
4651 _TIFFfree(origdir);
4652 return 0;
4653 }
4654 ma=(uint8*)origdir;
4655 mb=dir;
4656 for (n=0; n<dircount16; n++)
4657 {
4658 if (tif->tif_flags&TIFF_SWAB)
4659 TIFFSwabShort((uint16*)ma);
4660 mb->tdir_tag=*(uint16*)ma;
4661 ma+=sizeof(uint16);
4662 if (tif->tif_flags&TIFF_SWAB)
4663 TIFFSwabShort((uint16*)ma);
4664 mb->tdir_type=*(uint16*)ma;
4665 ma+=sizeof(uint16);
4666 if (!(tif->tif_flags&TIFF_BIGTIFF))
4667 {
4668 if (tif->tif_flags&TIFF_SWAB)
4669 TIFFSwabLong((uint32*)ma);
4670 mb->tdir_count=(uint64)(*(uint32*)ma);
4671 ma+=sizeof(uint32);
4672 *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
4673 ma+=sizeof(uint32);
4674 }
4675 else
4676 {
4677 if (tif->tif_flags&TIFF_SWAB)
4678 TIFFSwabLong8((uint64*)ma);
4679 mb->tdir_count=TIFFReadUInt64(ma);
4680 ma+=sizeof(uint64);
4681 mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
4682 ma+=sizeof(uint64);
4683 }
4684 mb++;
4685 }
4686 _TIFFfree(origdir);
4687 *pdir = dir;
4688 return dircount16;
4689 }
4690
4691 /*
4692 * Fetch a tag that is not handled by special case code.
4693 */
4694 static int
4695 TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
4696 {
4697 static const char module[] = "TIFFFetchNormalTag";
4698 enum TIFFReadDirEntryErr err;
4699 uint32 fii;
4700 const TIFFField* fip = NULL;
4701 TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
4702 if( fii == FAILED_FII )
4703 {
4704 TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
4705 "No definition found for tag %d",
4706 dp->tdir_tag);
4707 return 0;
4708 }
4709 fip=tif->tif_fields[fii];
4710 assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
4711 assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
4712 err=TIFFReadDirEntryErrOk;
4713 switch (fip->set_field_type)
4714 {
4715 case TIFF_SETGET_UNDEFINED:
4716 break;
4717 case TIFF_SETGET_ASCII:
4718 {
4719 uint8* data;
4720 assert(fip->field_passcount==0);
4721 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4722 if (err==TIFFReadDirEntryErrOk)
4723 {
4724 uint8* ma;
4725 uint32 mb;
4726 int n;
4727 ma=data;
4728 mb=0;
4729 while (mb<(uint32)dp->tdir_count)
4730 {
4731 if (*ma==0)
4732 break;
4733 ma++;
4734 mb++;
4735 }
4736 if (mb+1<(uint32)dp->tdir_count)
4737 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip->field_name);
4738 else if (mb+1>(uint32)dp->tdir_count)
4739 {
4740 uint8* o;
4741 TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
4742 if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
4743 o=NULL;
4744 else
4745 o=_TIFFmalloc((uint32)dp->tdir_count+1);
4746 if (o==NULL)
4747 {
4748 if (data!=NULL)
4749 _TIFFfree(data);
4750 return(0);
4751 }
4752 _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
4753 o[(uint32)dp->tdir_count]=0;
4754 if (data!=0)
4755 _TIFFfree(data);
4756 data=o;
4757 }
4758 n=TIFFSetField(tif,dp->tdir_tag,data);
4759 if (data!=0)
4760 _TIFFfree(data);
4761 if (!n)
4762 return(0);
4763 }
4764 }
4765 break;
4766 case TIFF_SETGET_UINT8:
4767 {
4768 uint8 data=0;
4769 assert(fip->field_readcount==1);
4770 assert(fip->field_passcount==0);
4771 err=TIFFReadDirEntryByte(tif,dp,&data);
4772 if (err==TIFFReadDirEntryErrOk)
4773 {
4774 if (!TIFFSetField(tif,dp->tdir_tag,data))
4775 return(0);
4776 }
4777 }
4778 break;
4779 case TIFF_SETGET_UINT16:
4780 {
4781 uint16 data;
4782 assert(fip->field_readcount==1);
4783 assert(fip->field_passcount==0);
4784 err=TIFFReadDirEntryShort(tif,dp,&data);
4785 if (err==TIFFReadDirEntryErrOk)
4786 {
4787 if (!TIFFSetField(tif,dp->tdir_tag,data))
4788 return(0);
4789 }
4790 }
4791 break;
4792 case TIFF_SETGET_UINT32:
4793 {
4794 uint32 data;
4795 assert(fip->field_readcount==1);
4796 assert(fip->field_passcount==0);
4797 err=TIFFReadDirEntryLong(tif,dp,&data);
4798 if (err==TIFFReadDirEntryErrOk)
4799 {
4800 if (!TIFFSetField(tif,dp->tdir_tag,data))
4801 return(0);
4802 }
4803 }
4804 break;
4805 case TIFF_SETGET_UINT64:
4806 {
4807 uint64 data;
4808 assert(fip->field_readcount==1);
4809 assert(fip->field_passcount==0);
4810 err=TIFFReadDirEntryLong8(tif,dp,&data);
4811 if (err==TIFFReadDirEntryErrOk)
4812 {
4813 if (!TIFFSetField(tif,dp->tdir_tag,data))
4814 return(0);
4815 }
4816 }
4817 break;
4818 case TIFF_SETGET_FLOAT:
4819 {
4820 float data;
4821 assert(fip->field_readcount==1);
4822 assert(fip->field_passcount==0);
4823 err=TIFFReadDirEntryFloat(tif,dp,&data);
4824 if (err==TIFFReadDirEntryErrOk)
4825 {
4826 if (!TIFFSetField(tif,dp->tdir_tag,data))
4827 return(0);
4828 }
4829 }
4830 break;
4831 case TIFF_SETGET_DOUBLE:
4832 {
4833 double data;
4834 assert(fip->field_readcount==1);
4835 assert(fip->field_passcount==0);
4836 err=TIFFReadDirEntryDouble(tif,dp,&data);
4837 if (err==TIFFReadDirEntryErrOk)
4838 {
4839 if (!TIFFSetField(tif,dp->tdir_tag,data))
4840 return(0);
4841 }
4842 }
4843 break;
4844 case TIFF_SETGET_IFD8:
4845 {
4846 uint64 data;
4847 assert(fip->field_readcount==1);
4848 assert(fip->field_passcount==0);
4849 err=TIFFReadDirEntryIfd8(tif,dp,&data);
4850 if (err==TIFFReadDirEntryErrOk)
4851 {
4852 if (!TIFFSetField(tif,dp->tdir_tag,data))
4853 return(0);
4854 }
4855 }
4856 break;
4857 case TIFF_SETGET_UINT16_PAIR:
4858 {
4859 uint16* data;
4860 assert(fip->field_readcount==2);
4861 assert(fip->field_passcount==0);
4862 if (dp->tdir_count!=2) {
4863 TIFFWarningExt(tif->tif_clientdata,module,
4864 "incorrect count for field \"%s\", expected 2, got %d",
4865 fip->field_name,(int)dp->tdir_count);
4866 return(0);
4867 }
4868 err=TIFFReadDirEntryShortArray(tif,dp,&data);
4869 if (err==TIFFReadDirEntryErrOk)
4870 {
4871 int m;
4872 m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
4873 _TIFFfree(data);
4874 if (!m)
4875 return(0);
4876 }
4877 }
4878 break;
4879 case TIFF_SETGET_C0_UINT8:
4880 {
4881 uint8* data;
4882 assert(fip->field_readcount>=1);
4883 assert(fip->field_passcount==0);
4884 if (dp->tdir_count!=(uint64)fip->field_readcount) {
4885 TIFFWarningExt(tif->tif_clientdata,module,
4886 "incorrect count for field \"%s\", expected %d, got %d",
4887 fip->field_name,(int) fip->field_readcount, (int)dp->tdir_count);
4888 return 0;
4889 }
4890 else
4891 {
4892 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4893 if (err==TIFFReadDirEntryErrOk)
4894 {
4895 int m;
4896 m=TIFFSetField(tif,dp->tdir_tag,data);
4897 if (data!=0)
4898 _TIFFfree(data);
4899 if (!m)
4900 return(0);
4901 }
4902 }
4903 }
4904 break;
4905 case TIFF_SETGET_C0_UINT16:
4906 {
4907 uint16* data;
4908 assert(fip->field_readcount>=1);
4909 assert(fip->field_passcount==0);
4910 if (dp->tdir_count!=(uint64)fip->field_readcount)
4911 /* corrupt file */;
4912 else
4913 {
4914 err=TIFFReadDirEntryShortArray(tif,dp,&data);
4915 if (err==TIFFReadDirEntryErrOk)
4916 {
4917 int m;
4918 m=TIFFSetField(tif,dp->tdir_tag,data);
4919 if (data!=0)
4920 _TIFFfree(data);
4921 if (!m)
4922 return(0);
4923 }
4924 }
4925 }
4926 break;
4927 case TIFF_SETGET_C0_UINT32:
4928 {
4929 uint32* data;
4930 assert(fip->field_readcount>=1);
4931 assert(fip->field_passcount==0);
4932 if (dp->tdir_count!=(uint64)fip->field_readcount)
4933 /* corrupt file */;
4934 else
4935 {
4936 err=TIFFReadDirEntryLongArray(tif,dp,&data);
4937 if (err==TIFFReadDirEntryErrOk)
4938 {
4939 int m;
4940 m=TIFFSetField(tif,dp->tdir_tag,data);
4941 if (data!=0)
4942 _TIFFfree(data);
4943 if (!m)
4944 return(0);
4945 }
4946 }
4947 }
4948 break;
4949 case TIFF_SETGET_C0_FLOAT:
4950 {
4951 float* data;
4952 assert(fip->field_readcount>=1);
4953 assert(fip->field_passcount==0);
4954 if (dp->tdir_count!=(uint64)fip->field_readcount)
4955 /* corrupt file */;
4956 else
4957 {
4958 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
4959 if (err==TIFFReadDirEntryErrOk)
4960 {
4961 int m;
4962 m=TIFFSetField(tif,dp->tdir_tag,data);
4963 if (data!=0)
4964 _TIFFfree(data);
4965 if (!m)
4966 return(0);
4967 }
4968 }
4969 }
4970 break;
4971 case TIFF_SETGET_C16_ASCII:
4972 {
4973 uint8* data;
4974 assert(fip->field_readcount==TIFF_VARIABLE);
4975 assert(fip->field_passcount==1);
4976 if (dp->tdir_count>0xFFFF)
4977 err=TIFFReadDirEntryErrCount;
4978 else
4979 {
4980 err=TIFFReadDirEntryByteArray(tif,dp,&data);
4981 if (err==TIFFReadDirEntryErrOk)
4982 {
4983 int m;
4984 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
4985 if (data!=0)
4986 _TIFFfree(data);
4987 if (!m)
4988 return(0);
4989 }
4990 }
4991 }
4992 break;
4993 case TIFF_SETGET_C16_UINT8:
4994 {
4995 uint8* data;
4996 assert(fip->field_readcount==TIFF_VARIABLE);
4997 assert(fip->field_passcount==1);
4998 if (dp->tdir_count>0xFFFF)
4999 err=TIFFReadDirEntryErrCount;
5000 else
5001 {
5002 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5003 if (err==TIFFReadDirEntryErrOk)
5004 {
5005 int m;
5006 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5007 if (data!=0)
5008 _TIFFfree(data);
5009 if (!m)
5010 return(0);
5011 }
5012 }
5013 }
5014 break;
5015 case TIFF_SETGET_C16_UINT16:
5016 {
5017 uint16* data;
5018 assert(fip->field_readcount==TIFF_VARIABLE);
5019 assert(fip->field_passcount==1);
5020 if (dp->tdir_count>0xFFFF)
5021 err=TIFFReadDirEntryErrCount;
5022 else
5023 {
5024 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5025 if (err==TIFFReadDirEntryErrOk)
5026 {
5027 int m;
5028 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5029 if (data!=0)
5030 _TIFFfree(data);
5031 if (!m)
5032 return(0);
5033 }
5034 }
5035 }
5036 break;
5037 case TIFF_SETGET_C16_UINT32:
5038 {
5039 uint32* data;
5040 assert(fip->field_readcount==TIFF_VARIABLE);
5041 assert(fip->field_passcount==1);
5042 if (dp->tdir_count>0xFFFF)
5043 err=TIFFReadDirEntryErrCount;
5044 else
5045 {
5046 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5047 if (err==TIFFReadDirEntryErrOk)
5048 {
5049 int m;
5050 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5051 if (data!=0)
5052 _TIFFfree(data);
5053 if (!m)
5054 return(0);
5055 }
5056 }
5057 }
5058 break;
5059 case TIFF_SETGET_C16_UINT64:
5060 {
5061 uint64* data;
5062 assert(fip->field_readcount==TIFF_VARIABLE);
5063 assert(fip->field_passcount==1);
5064 if (dp->tdir_count>0xFFFF)
5065 err=TIFFReadDirEntryErrCount;
5066 else
5067 {
5068 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5069 if (err==TIFFReadDirEntryErrOk)
5070 {
5071 int m;
5072 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5073 if (data!=0)
5074 _TIFFfree(data);
5075 if (!m)
5076 return(0);
5077 }
5078 }
5079 }
5080 break;
5081 case TIFF_SETGET_C16_FLOAT:
5082 {
5083 float* data;
5084 assert(fip->field_readcount==TIFF_VARIABLE);
5085 assert(fip->field_passcount==1);
5086 if (dp->tdir_count>0xFFFF)
5087 err=TIFFReadDirEntryErrCount;
5088 else
5089 {
5090 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5091 if (err==TIFFReadDirEntryErrOk)
5092 {
5093 int m;
5094 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5095 if (data!=0)
5096 _TIFFfree(data);
5097 if (!m)
5098 return(0);
5099 }
5100 }
5101 }
5102 break;
5103 case TIFF_SETGET_C16_DOUBLE:
5104 {
5105 double* data;
5106 assert(fip->field_readcount==TIFF_VARIABLE);
5107 assert(fip->field_passcount==1);
5108 if (dp->tdir_count>0xFFFF)
5109 err=TIFFReadDirEntryErrCount;
5110 else
5111 {
5112 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5113 if (err==TIFFReadDirEntryErrOk)
5114 {
5115 int m;
5116 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5117 if (data!=0)
5118 _TIFFfree(data);
5119 if (!m)
5120 return(0);
5121 }
5122 }
5123 }
5124 break;
5125 case TIFF_SETGET_C16_IFD8:
5126 {
5127 uint64* data;
5128 assert(fip->field_readcount==TIFF_VARIABLE);
5129 assert(fip->field_passcount==1);
5130 if (dp->tdir_count>0xFFFF)
5131 err=TIFFReadDirEntryErrCount;
5132 else
5133 {
5134 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5135 if (err==TIFFReadDirEntryErrOk)
5136 {
5137 int m;
5138 m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
5139 if (data!=0)
5140 _TIFFfree(data);
5141 if (!m)
5142 return(0);
5143 }
5144 }
5145 }
5146 break;
5147 case TIFF_SETGET_C32_ASCII:
5148 {
5149 uint8* data;
5150 assert(fip->field_readcount==TIFF_VARIABLE2);
5151 assert(fip->field_passcount==1);
5152 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5153 if (err==TIFFReadDirEntryErrOk)
5154 {
5155 int m;
5156 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5157 if (data!=0)
5158 _TIFFfree(data);
5159 if (!m)
5160 return(0);
5161 }
5162 }
5163 break;
5164 case TIFF_SETGET_C32_UINT8:
5165 {
5166 uint8* data;
5167 assert(fip->field_readcount==TIFF_VARIABLE2);
5168 assert(fip->field_passcount==1);
5169 err=TIFFReadDirEntryByteArray(tif,dp,&data);
5170 if (err==TIFFReadDirEntryErrOk)
5171 {
5172 int m;
5173 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5174 if (data!=0)
5175 _TIFFfree(data);
5176 if (!m)
5177 return(0);
5178 }
5179 }
5180 break;
5181 case TIFF_SETGET_C32_SINT8:
5182 {
5183 int8* data = NULL;
5184 assert(fip->field_readcount==TIFF_VARIABLE2);
5185 assert(fip->field_passcount==1);
5186 err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
5187 if (err==TIFFReadDirEntryErrOk)
5188 {
5189 int m;
5190 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5191 if (data!=0)
5192 _TIFFfree(data);
5193 if (!m)
5194 return(0);
5195 }
5196 }
5197 break;
5198 case TIFF_SETGET_C32_UINT16:
5199 {
5200 uint16* data;
5201 assert(fip->field_readcount==TIFF_VARIABLE2);
5202 assert(fip->field_passcount==1);
5203 err=TIFFReadDirEntryShortArray(tif,dp,&data);
5204 if (err==TIFFReadDirEntryErrOk)
5205 {
5206 int m;
5207 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5208 if (data!=0)
5209 _TIFFfree(data);
5210 if (!m)
5211 return(0);
5212 }
5213 }
5214 break;
5215 case TIFF_SETGET_C32_SINT16:
5216 {
5217 int16* data = NULL;
5218 assert(fip->field_readcount==TIFF_VARIABLE2);
5219 assert(fip->field_passcount==1);
5220 err=TIFFReadDirEntrySshortArray(tif,dp,&data);
5221 if (err==TIFFReadDirEntryErrOk)
5222 {
5223 int m;
5224 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5225 if (data!=0)
5226 _TIFFfree(data);
5227 if (!m)
5228 return(0);
5229 }
5230 }
5231 break;
5232 case TIFF_SETGET_C32_UINT32:
5233 {
5234 uint32* data;
5235 assert(fip->field_readcount==TIFF_VARIABLE2);
5236 assert(fip->field_passcount==1);
5237 err=TIFFReadDirEntryLongArray(tif,dp,&data);
5238 if (err==TIFFReadDirEntryErrOk)
5239 {
5240 int m;
5241 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5242 if (data!=0)
5243 _TIFFfree(data);
5244 if (!m)
5245 return(0);
5246 }
5247 }
5248 break;
5249 case TIFF_SETGET_C32_SINT32:
5250 {
5251 int32* data = NULL;
5252 assert(fip->field_readcount==TIFF_VARIABLE2);
5253 assert(fip->field_passcount==1);
5254 err=TIFFReadDirEntrySlongArray(tif,dp,&data);
5255 if (err==TIFFReadDirEntryErrOk)
5256 {
5257 int m;
5258 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5259 if (data!=0)
5260 _TIFFfree(data);
5261 if (!m)
5262 return(0);
5263 }
5264 }
5265 break;
5266 case TIFF_SETGET_C32_UINT64:
5267 {
5268 uint64* data;
5269 assert(fip->field_readcount==TIFF_VARIABLE2);
5270 assert(fip->field_passcount==1);
5271 err=TIFFReadDirEntryLong8Array(tif,dp,&data);
5272 if (err==TIFFReadDirEntryErrOk)
5273 {
5274 int m;
5275 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5276 if (data!=0)
5277 _TIFFfree(data);
5278 if (!m)
5279 return(0);
5280 }
5281 }
5282 break;
5283 case TIFF_SETGET_C32_SINT64:
5284 {
5285 int64* data = NULL;
5286 assert(fip->field_readcount==TIFF_VARIABLE2);
5287 assert(fip->field_passcount==1);
5288 err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
5289 if (err==TIFFReadDirEntryErrOk)
5290 {
5291 int m;
5292 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5293 if (data!=0)
5294 _TIFFfree(data);
5295 if (!m)
5296 return(0);
5297 }
5298 }
5299 break;
5300 case TIFF_SETGET_C32_FLOAT:
5301 {
5302 float* data;
5303 assert(fip->field_readcount==TIFF_VARIABLE2);
5304 assert(fip->field_passcount==1);
5305 err=TIFFReadDirEntryFloatArray(tif,dp,&data);
5306 if (err==TIFFReadDirEntryErrOk)
5307 {
5308 int m;
5309 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5310 if (data!=0)
5311 _TIFFfree(data);
5312 if (!m)
5313 return(0);
5314 }
5315 }
5316 break;
5317 case TIFF_SETGET_C32_DOUBLE:
5318 {
5319 double* data;
5320 assert(fip->field_readcount==TIFF_VARIABLE2);
5321 assert(fip->field_passcount==1);
5322 err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
5323 if (err==TIFFReadDirEntryErrOk)
5324 {
5325 int m;
5326 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5327 if (data!=0)
5328 _TIFFfree(data);
5329 if (!m)
5330 return(0);
5331 }
5332 }
5333 break;
5334 case TIFF_SETGET_C32_IFD8:
5335 {
5336 uint64* data;
5337 assert(fip->field_readcount==TIFF_VARIABLE2);
5338 assert(fip->field_passcount==1);
5339 err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
5340 if (err==TIFFReadDirEntryErrOk)
5341 {
5342 int m;
5343 m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
5344 if (data!=0)
5345 _TIFFfree(data);
5346 if (!m)
5347 return(0);
5348 }
5349 }
5350 break;
5351 default:
5352 assert(0); /* we should never get here */
5353 break;
5354 }
5355 if (err!=TIFFReadDirEntryErrOk)
5356 {
5357 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",recover);
5358 return(0);
5359 }
5360 return(1);
5361 }
5362
5363 /*
5364 * Fetch a set of offsets or lengths.
5365 * While this routine says "strips", in fact it's also used for tiles.
5366 */
5367 static int
5368 TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
5369 {
5370 static const char module[] = "TIFFFetchStripThing";
5371 enum TIFFReadDirEntryErr err;
5372 uint64* data;
5373 err=TIFFReadDirEntryLong8Array(tif,dir,&data);
5374 if (err!=TIFFReadDirEntryErrOk)
5375 {
5376 const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
5377 TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
5378 return(0);
5379 }
5380 if (dir->tdir_count!=(uint64)nstrips)
5381 {
5382 uint64* resizeddata;
5383 resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
5384 if (resizeddata==0) {
5385 _TIFFfree(data);
5386 return(0);
5387 }
5388 if (dir->tdir_count<(uint64)nstrips)
5389 {
5390 _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
5391 _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
5392 }
5393 else
5394 _TIFFmemcpy(resizeddata,data,nstrips*sizeof(uint64));
5395 _TIFFfree(data);
5396 data=resizeddata;
5397 }
5398 *lpp=data;
5399 return(1);
5400 }
5401
5402 /*
5403 * Fetch and set the SubjectDistance EXIF tag.
5404 */
5405 static int
5406 TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
5407 {
5408 static const char module[] = "TIFFFetchSubjectDistance";
5409 enum TIFFReadDirEntryErr err;
5410 UInt64Aligned_t m;
5411 m.l=0;
5412 assert(sizeof(double)==8);
5413 assert(sizeof(uint64)==8);
5414 assert(sizeof(uint32)==4);
5415 if (dir->tdir_count!=1)
5416 err=TIFFReadDirEntryErrCount;
5417 else if (dir->tdir_type!=TIFF_RATIONAL)
5418 err=TIFFReadDirEntryErrType;
5419 else
5420 {
5421 if (!(tif->tif_flags&TIFF_BIGTIFF))
5422 {
5423 uint32 offset;
5424 offset=*(uint32*)(&dir->tdir_offset);
5425 if (tif->tif_flags&TIFF_SWAB)
5426 TIFFSwabLong(&offset);
5427 err=TIFFReadDirEntryData(tif,offset,8,m.i);
5428 }
5429 else
5430 {
5431 m.l=dir->tdir_offset.toff_long8;
5432 err=TIFFReadDirEntryErrOk;
5433 }
5434 }
5435 if (err==TIFFReadDirEntryErrOk)
5436 {
5437 double n;
5438 if (tif->tif_flags&TIFF_SWAB)
5439 TIFFSwabArrayOfLong(m.i,2);
5440 if (m.i[0]==0)
5441 n=0.0;
5442 else if (m.i[0]==0xFFFFFFFF)
5443 /*
5444 * XXX: Numerator 0xFFFFFFFF means that we have infinite
5445 * distance. Indicate that with a negative floating point
5446 * SubjectDistance value.
5447 */
5448 n=-1.0;
5449 else
5450 n=(double)m.i[0]/(double)m.i[1];
5451 return(TIFFSetField(tif,dir->tdir_tag,n));
5452 }
5453 else
5454 {
5455 TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
5456 return(0);
5457 }
5458 }
5459
5460 /*
5461 * Replace a single strip (tile) of uncompressed data by multiple strips
5462 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
5463 * dealing with large images or for dealing with machines with a limited
5464 * amount memory.
5465 */
5466 static void
5467 ChopUpSingleUncompressedStrip(TIFF* tif)
5468 {
5469 register TIFFDirectory *td = &tif->tif_dir;
5470 uint64 bytecount;
5471 uint64 offset;
5472 uint32 rowblock;
5473 uint64 rowblockbytes;
5474 uint64 stripbytes;
5475 uint32 strip;
5476 uint64 nstrips64;
5477 uint32 nstrips32;
5478 uint32 rowsperstrip;
5479 uint64* newcounts;
5480 uint64* newoffsets;
5481
5482 bytecount = td->td_stripbytecount[0];
5483 offset = td->td_stripoffset[0];
5484 assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
5485 if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
5486 (!isUpSampled(tif)))
5487 rowblock = td->td_ycbcrsubsampling[1];
5488 else
5489 rowblock = 1;
5490 rowblockbytes = TIFFVTileSize64(tif, rowblock);
5491 /*
5492 * Make the rows hold at least one scanline, but fill specified amount
5493 * of data if possible.
5494 */
5495 if (rowblockbytes > STRIP_SIZE_DEFAULT) {
5496 stripbytes = rowblockbytes;
5497 rowsperstrip = rowblock;
5498 } else if (rowblockbytes > 0 ) {
5499 uint32 rowblocksperstrip;
5500 rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
5501 rowsperstrip = rowblocksperstrip * rowblock;
5502 stripbytes = rowblocksperstrip * rowblockbytes;
5503 }
5504 else
5505 return;
5506
5507 /*
5508 * never increase the number of strips in an image
5509 */
5510 if (rowsperstrip >= td->td_rowsperstrip)
5511 return;
5512 nstrips64 = TIFFhowmany_64(bytecount, stripbytes);
5513 if ((nstrips64==0)||(nstrips64>0xFFFFFFFF)) /* something is wonky, do nothing. */
5514 return;
5515 nstrips32 = (uint32)nstrips64;
5516
5517 newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
5518 "for chopped \"StripByteCounts\" array");
5519 newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
5520 "for chopped \"StripOffsets\" array");
5521 if (newcounts == NULL || newoffsets == NULL) {
5522 /*
5523 * Unable to allocate new strip information, give up and use
5524 * the original one strip information.
5525 */
5526 if (newcounts != NULL)
5527 _TIFFfree(newcounts);
5528 if (newoffsets != NULL)
5529 _TIFFfree(newoffsets);
5530 return;
5531 }
5532 /*
5533 * Fill the strip information arrays with new bytecounts and offsets
5534 * that reflect the broken-up format.
5535 */
5536 for (strip = 0; strip < nstrips32; strip++) {
5537 if (stripbytes > bytecount)
5538 stripbytes = bytecount;
5539 newcounts[strip] = stripbytes;
5540 newoffsets[strip] = offset;
5541 offset += stripbytes;
5542 bytecount -= stripbytes;
5543 }
5544 /*
5545 * Replace old single strip info with multi-strip info.
5546 */
5547 td->td_stripsperimage = td->td_nstrips = nstrips32;
5548 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
5549
5550 _TIFFfree(td->td_stripbytecount);
5551 _TIFFfree(td->td_stripoffset);
5552 td->td_stripbytecount = newcounts;
5553 td->td_stripoffset = newoffsets;
5554 td->td_stripbytecountsorted = 1;
5555 }
5556
5557 int _TIFFFillStriles( TIFF *tif )
5558 {
5559 #if defined(DEFER_STRILE_LOAD)
5560 register TIFFDirectory *td = &tif->tif_dir;
5561 int return_value = 1;
5562
5563 if( td->td_stripoffset != NULL )
5564 return 1;
5565
5566 if( td->td_stripoffset_entry.tdir_count == 0 )
5567 return 0;
5568
5569 if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
5570 td->td_nstrips,&td->td_stripoffset))
5571 {
5572 return_value = 0;
5573 }
5574
5575 if (!TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
5576 td->td_nstrips,&td->td_stripbytecount))
5577 {
5578 return_value = 0;
5579 }
5580
5581 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
5582 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
5583
5584 if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
5585 uint32 strip;
5586
5587 tif->tif_dir.td_stripbytecountsorted = 1;
5588 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
5589 if (tif->tif_dir.td_stripoffset[strip - 1] >
5590 tif->tif_dir.td_stripoffset[strip]) {
5591 tif->tif_dir.td_stripbytecountsorted = 0;
5592 break;
5593 }
5594 }
5595 }
5596
5597 return return_value;
5598 #else /* !defined(DEFER_STRILE_LOAD) */
5599 (void) tif;
5600 return 1;
5601 #endif
5602 }
5603
5604
5605 /* vim: set ts=8 sts=8 sw=8 noet: */
5606 /*
5607 * Local Variables:
5608 * mode: c
5609 * c-basic-offset: 8
5610 * fill-column: 78
5611 * End:
5612 */