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