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