Line data Source code
1 : /*
2 : * Copyright (c) 1988-1997 Sam Leffler
3 : * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 : *
5 : * Permission to use, copy, modify, distribute, and sell this software and
6 : * its documentation for any purpose is hereby granted without fee, provided
7 : * that (i) the above copyright notices and this permission notice appear in
8 : * all copies of the software and related documentation, and (ii) the names of
9 : * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 : * publicity relating to the software without the specific, prior written
11 : * permission of Sam Leffler and Silicon Graphics.
12 : *
13 : * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 : * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 : * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 : *
17 : * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 : * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 : * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 : * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 : * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 : * OF THIS SOFTWARE.
23 : */
24 :
25 : /*
26 : * TIFF Library.
27 : *
28 : * Directory Read Support Routines.
29 : */
30 :
31 : /* Suggested pending improvements:
32 : * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33 : * the pointer to the appropriate TIFFField structure early on in
34 : * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
35 : */
36 :
37 : #include "tiffconf.h"
38 : #include "tiffiop.h"
39 : #include <float.h>
40 : #include <limits.h>
41 : #include <stdlib.h>
42 : #include <string.h>
43 :
44 : #define FAILED_FII ((uint32_t)-1)
45 :
46 : #ifdef HAVE_IEEEFP
47 : #define TIFFCvtIEEEFloatToNative(tif, n, fp)
48 : #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
49 : #else
50 : /* If your machine does not support IEEE floating point then you will need to
51 : * add support to tif_machdep.c to convert between the native format and
52 : * IEEE format. */
53 : extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *);
54 : extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *);
55 : #endif
56 :
57 : enum TIFFReadDirEntryErr
58 : {
59 : TIFFReadDirEntryErrOk = 0,
60 : TIFFReadDirEntryErrCount = 1,
61 : TIFFReadDirEntryErrType = 2,
62 : TIFFReadDirEntryErrIo = 3,
63 : TIFFReadDirEntryErrRange = 4,
64 : TIFFReadDirEntryErrPsdif = 5,
65 : TIFFReadDirEntryErrSizesan = 6,
66 : TIFFReadDirEntryErrAlloc = 7,
67 : };
68 :
69 : static enum TIFFReadDirEntryErr
70 : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value);
71 : static enum TIFFReadDirEntryErr
72 : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value);
73 : static enum TIFFReadDirEntryErr
74 : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value);
75 : static enum TIFFReadDirEntryErr
76 : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value);
77 : static enum TIFFReadDirEntryErr
78 : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value);
79 : static enum TIFFReadDirEntryErr
80 : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value);
81 : static enum TIFFReadDirEntryErr
82 : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
83 : static enum TIFFReadDirEntryErr
84 : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value);
85 : static enum TIFFReadDirEntryErr
86 : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value);
87 : static enum TIFFReadDirEntryErr
88 : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
89 : static enum TIFFReadDirEntryErr
90 : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
91 :
92 : static enum TIFFReadDirEntryErr
93 : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
94 : uint32_t desttypesize, void **value);
95 : static enum TIFFReadDirEntryErr
96 : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value);
97 : static enum TIFFReadDirEntryErr
98 : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value);
99 : static enum TIFFReadDirEntryErr
100 : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value);
101 : static enum TIFFReadDirEntryErr
102 : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value);
103 : static enum TIFFReadDirEntryErr
104 : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value);
105 : static enum TIFFReadDirEntryErr
106 : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value);
107 : static enum TIFFReadDirEntryErr
108 : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
109 : static enum TIFFReadDirEntryErr
110 : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value);
111 : static enum TIFFReadDirEntryErr
112 : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value);
113 : static enum TIFFReadDirEntryErr
114 : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value);
115 : static enum TIFFReadDirEntryErr
116 : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
117 :
118 : static enum TIFFReadDirEntryErr
119 : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
120 : uint16_t *value);
121 :
122 : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
123 : uint8_t *value);
124 : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
125 : int8_t *value);
126 : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
127 : uint16_t *value);
128 : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
129 : int16_t *value);
130 : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
131 : uint32_t *value);
132 : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
133 : int32_t *value);
134 : static enum TIFFReadDirEntryErr
135 : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry,
136 : uint64_t *value);
137 : static enum TIFFReadDirEntryErr
138 : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry,
139 : int64_t *value);
140 : static enum TIFFReadDirEntryErr
141 : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
142 : double *value);
143 : static enum TIFFReadDirEntryErr
144 : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
145 : double *value);
146 : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
147 : float *value);
148 : static enum TIFFReadDirEntryErr
149 : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
150 : #if 0
151 : static enum TIFFReadDirEntryErr
152 : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
153 : TIFFRational_t *value);
154 : #endif
155 : static enum TIFFReadDirEntryErr
156 : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
157 : static enum TIFFReadDirEntryErr
158 : TIFFReadDirEntryCheckRangeByteShort(uint16_t value);
159 : static enum TIFFReadDirEntryErr
160 : TIFFReadDirEntryCheckRangeByteSshort(int16_t value);
161 : static enum TIFFReadDirEntryErr
162 : TIFFReadDirEntryCheckRangeByteLong(uint32_t value);
163 : static enum TIFFReadDirEntryErr
164 : TIFFReadDirEntryCheckRangeByteSlong(int32_t value);
165 : static enum TIFFReadDirEntryErr
166 : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value);
167 : static enum TIFFReadDirEntryErr
168 : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value);
169 :
170 : static enum TIFFReadDirEntryErr
171 : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value);
172 : static enum TIFFReadDirEntryErr
173 : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value);
174 : static enum TIFFReadDirEntryErr
175 : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value);
176 : static enum TIFFReadDirEntryErr
177 : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value);
178 : static enum TIFFReadDirEntryErr
179 : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value);
180 : static enum TIFFReadDirEntryErr
181 : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value);
182 : static enum TIFFReadDirEntryErr
183 : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value);
184 :
185 : static enum TIFFReadDirEntryErr
186 : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value);
187 : static enum TIFFReadDirEntryErr
188 : TIFFReadDirEntryCheckRangeShortSshort(int16_t value);
189 : static enum TIFFReadDirEntryErr
190 : TIFFReadDirEntryCheckRangeShortLong(uint32_t value);
191 : static enum TIFFReadDirEntryErr
192 : TIFFReadDirEntryCheckRangeShortSlong(int32_t value);
193 : static enum TIFFReadDirEntryErr
194 : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value);
195 : static enum TIFFReadDirEntryErr
196 : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value);
197 :
198 : static enum TIFFReadDirEntryErr
199 : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value);
200 : static enum TIFFReadDirEntryErr
201 : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value);
202 : static enum TIFFReadDirEntryErr
203 : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value);
204 : static enum TIFFReadDirEntryErr
205 : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value);
206 : static enum TIFFReadDirEntryErr
207 : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value);
208 :
209 : static enum TIFFReadDirEntryErr
210 : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value);
211 : static enum TIFFReadDirEntryErr
212 : TIFFReadDirEntryCheckRangeLongSshort(int16_t value);
213 : static enum TIFFReadDirEntryErr
214 : TIFFReadDirEntryCheckRangeLongSlong(int32_t value);
215 : static enum TIFFReadDirEntryErr
216 : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value);
217 : static enum TIFFReadDirEntryErr
218 : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value);
219 :
220 : static enum TIFFReadDirEntryErr
221 : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value);
222 : static enum TIFFReadDirEntryErr
223 : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value);
224 : static enum TIFFReadDirEntryErr
225 : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value);
226 :
227 : static enum TIFFReadDirEntryErr
228 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value);
229 : static enum TIFFReadDirEntryErr
230 : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value);
231 : static enum TIFFReadDirEntryErr
232 : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value);
233 : static enum TIFFReadDirEntryErr
234 : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value);
235 :
236 : static enum TIFFReadDirEntryErr
237 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value);
238 :
239 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
240 : tmsize_t size, void *dest);
241 : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
242 : const char *module, const char *tagname,
243 : int recover);
244 :
245 : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
246 : uint16_t dircount);
247 : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
248 : uint16_t dircount,
249 : uint16_t tagid);
250 : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
251 : uint32_t *fii);
252 :
253 : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
254 : uint16_t dircount);
255 : static void MissingRequired(TIFF *, const char *);
256 : static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
257 : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
258 : TIFFDirEntry **pdir, uint64_t *nextdiroff);
259 : static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover);
260 : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
261 : uint64_t **lpp);
262 : static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *);
263 : static void ChopUpSingleUncompressedStrip(TIFF *);
264 : static void TryChopUpUncompressedBigTiff(TIFF *);
265 : static uint64_t TIFFReadUInt64(const uint8_t *value);
266 : static int _TIFFGetMaxColorChannels(uint16_t photometric);
267 :
268 : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount);
269 :
270 : typedef union _UInt64Aligned_t
271 : {
272 : double d;
273 : uint64_t l;
274 : uint32_t i[2];
275 : uint16_t s[4];
276 : uint8_t c[8];
277 : } UInt64Aligned_t;
278 :
279 : /*
280 : Unaligned safe copy of a uint64_t value from an octet array.
281 : */
282 24494 : static uint64_t TIFFReadUInt64(const uint8_t *value)
283 : {
284 : UInt64Aligned_t result;
285 :
286 24494 : result.c[0] = value[0];
287 24494 : result.c[1] = value[1];
288 24494 : result.c[2] = value[2];
289 24494 : result.c[3] = value[3];
290 24494 : result.c[4] = value[4];
291 24494 : result.c[5] = value[5];
292 24494 : result.c[6] = value[6];
293 24494 : result.c[7] = value[7];
294 :
295 24494 : return result.l;
296 : }
297 :
298 : static enum TIFFReadDirEntryErr
299 0 : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
300 : {
301 : enum TIFFReadDirEntryErr err;
302 0 : if (direntry->tdir_count != 1)
303 0 : return (TIFFReadDirEntryErrCount);
304 0 : switch (direntry->tdir_type)
305 : {
306 0 : case TIFF_BYTE:
307 : case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
308 : field_readcount==1 */
309 0 : TIFFReadDirEntryCheckedByte(tif, direntry, value);
310 0 : return (TIFFReadDirEntryErrOk);
311 0 : case TIFF_SBYTE:
312 : {
313 : int8_t m;
314 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
315 0 : err = TIFFReadDirEntryCheckRangeByteSbyte(m);
316 0 : if (err != TIFFReadDirEntryErrOk)
317 0 : return (err);
318 0 : *value = (uint8_t)m;
319 0 : return (TIFFReadDirEntryErrOk);
320 : }
321 0 : case TIFF_SHORT:
322 : {
323 : uint16_t m;
324 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
325 0 : err = TIFFReadDirEntryCheckRangeByteShort(m);
326 0 : if (err != TIFFReadDirEntryErrOk)
327 0 : return (err);
328 0 : *value = (uint8_t)m;
329 0 : return (TIFFReadDirEntryErrOk);
330 : }
331 0 : case TIFF_SSHORT:
332 : {
333 : int16_t m;
334 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
335 0 : err = TIFFReadDirEntryCheckRangeByteSshort(m);
336 0 : if (err != TIFFReadDirEntryErrOk)
337 0 : return (err);
338 0 : *value = (uint8_t)m;
339 0 : return (TIFFReadDirEntryErrOk);
340 : }
341 0 : case TIFF_LONG:
342 : {
343 : uint32_t m;
344 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
345 0 : err = TIFFReadDirEntryCheckRangeByteLong(m);
346 0 : if (err != TIFFReadDirEntryErrOk)
347 0 : return (err);
348 0 : *value = (uint8_t)m;
349 0 : return (TIFFReadDirEntryErrOk);
350 : }
351 0 : case TIFF_SLONG:
352 : {
353 : int32_t m;
354 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
355 0 : err = TIFFReadDirEntryCheckRangeByteSlong(m);
356 0 : if (err != TIFFReadDirEntryErrOk)
357 0 : return (err);
358 0 : *value = (uint8_t)m;
359 0 : return (TIFFReadDirEntryErrOk);
360 : }
361 0 : case TIFF_LONG8:
362 : {
363 : uint64_t m;
364 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
365 0 : if (err != TIFFReadDirEntryErrOk)
366 0 : return (err);
367 0 : err = TIFFReadDirEntryCheckRangeByteLong8(m);
368 0 : if (err != TIFFReadDirEntryErrOk)
369 0 : return (err);
370 0 : *value = (uint8_t)m;
371 0 : return (TIFFReadDirEntryErrOk);
372 : }
373 0 : case TIFF_SLONG8:
374 : {
375 : int64_t m;
376 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
377 0 : if (err != TIFFReadDirEntryErrOk)
378 0 : return (err);
379 0 : err = TIFFReadDirEntryCheckRangeByteSlong8(m);
380 0 : if (err != TIFFReadDirEntryErrOk)
381 0 : return (err);
382 0 : *value = (uint8_t)m;
383 0 : return (TIFFReadDirEntryErrOk);
384 : }
385 0 : default:
386 0 : return (TIFFReadDirEntryErrType);
387 : }
388 : }
389 :
390 : static enum TIFFReadDirEntryErr
391 0 : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
392 : {
393 : enum TIFFReadDirEntryErr err;
394 0 : if (direntry->tdir_count != 1)
395 0 : return (TIFFReadDirEntryErrCount);
396 0 : switch (direntry->tdir_type)
397 : {
398 0 : case TIFF_BYTE:
399 : case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
400 : field_readcount==1 */
401 : {
402 : uint8_t m;
403 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
404 0 : err = TIFFReadDirEntryCheckRangeSbyteByte(m);
405 0 : if (err != TIFFReadDirEntryErrOk)
406 0 : return (err);
407 0 : *value = (int8_t)m;
408 0 : return (TIFFReadDirEntryErrOk);
409 : }
410 0 : case TIFF_SBYTE:
411 : {
412 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
413 0 : return (TIFFReadDirEntryErrOk);
414 : }
415 0 : case TIFF_SHORT:
416 : {
417 : uint16_t m;
418 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
419 0 : err = TIFFReadDirEntryCheckRangeSbyteShort(m);
420 0 : if (err != TIFFReadDirEntryErrOk)
421 0 : return (err);
422 0 : *value = (int8_t)m;
423 0 : return (TIFFReadDirEntryErrOk);
424 : }
425 0 : case TIFF_SSHORT:
426 : {
427 : int16_t m;
428 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
429 0 : err = TIFFReadDirEntryCheckRangeSbyteSshort(m);
430 0 : if (err != TIFFReadDirEntryErrOk)
431 0 : return (err);
432 0 : *value = (int8_t)m;
433 0 : return (TIFFReadDirEntryErrOk);
434 : }
435 0 : case TIFF_LONG:
436 : {
437 : uint32_t m;
438 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
439 0 : err = TIFFReadDirEntryCheckRangeSbyteLong(m);
440 0 : if (err != TIFFReadDirEntryErrOk)
441 0 : return (err);
442 0 : *value = (int8_t)m;
443 0 : return (TIFFReadDirEntryErrOk);
444 : }
445 0 : case TIFF_SLONG:
446 : {
447 : int32_t m;
448 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
449 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong(m);
450 0 : if (err != TIFFReadDirEntryErrOk)
451 0 : return (err);
452 0 : *value = (int8_t)m;
453 0 : return (TIFFReadDirEntryErrOk);
454 : }
455 0 : case TIFF_LONG8:
456 : {
457 : uint64_t m;
458 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
459 0 : if (err != TIFFReadDirEntryErrOk)
460 0 : return (err);
461 0 : err = TIFFReadDirEntryCheckRangeSbyteLong8(m);
462 0 : if (err != TIFFReadDirEntryErrOk)
463 0 : return (err);
464 0 : *value = (int8_t)m;
465 0 : return (TIFFReadDirEntryErrOk);
466 : }
467 0 : case TIFF_SLONG8:
468 : {
469 : int64_t m;
470 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
471 0 : if (err != TIFFReadDirEntryErrOk)
472 0 : return (err);
473 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong8(m);
474 0 : if (err != TIFFReadDirEntryErrOk)
475 0 : return (err);
476 0 : *value = (int8_t)m;
477 0 : return (TIFFReadDirEntryErrOk);
478 : }
479 0 : default:
480 0 : return (TIFFReadDirEntryErrType);
481 : }
482 : } /*-- TIFFReadDirEntrySbyte() --*/
483 :
484 : static enum TIFFReadDirEntryErr
485 329736 : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
486 : {
487 : enum TIFFReadDirEntryErr err;
488 329736 : if (direntry->tdir_count != 1)
489 32614 : return (TIFFReadDirEntryErrCount);
490 297122 : switch (direntry->tdir_type)
491 : {
492 0 : case TIFF_BYTE:
493 : {
494 : uint8_t m;
495 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
496 0 : *value = (uint16_t)m;
497 0 : return (TIFFReadDirEntryErrOk);
498 : }
499 0 : case TIFF_SBYTE:
500 : {
501 : int8_t m;
502 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
503 0 : err = TIFFReadDirEntryCheckRangeShortSbyte(m);
504 0 : if (err != TIFFReadDirEntryErrOk)
505 0 : return (err);
506 0 : *value = (uint16_t)m;
507 0 : return (TIFFReadDirEntryErrOk);
508 : }
509 297124 : case TIFF_SHORT:
510 297124 : TIFFReadDirEntryCheckedShort(tif, direntry, value);
511 297136 : return (TIFFReadDirEntryErrOk);
512 0 : case TIFF_SSHORT:
513 : {
514 : int16_t m;
515 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
516 0 : err = TIFFReadDirEntryCheckRangeShortSshort(m);
517 0 : if (err != TIFFReadDirEntryErrOk)
518 0 : return (err);
519 0 : *value = (uint16_t)m;
520 0 : return (TIFFReadDirEntryErrOk);
521 : }
522 1 : case TIFF_LONG:
523 : {
524 : uint32_t m;
525 1 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
526 1 : err = TIFFReadDirEntryCheckRangeShortLong(m);
527 1 : if (err != TIFFReadDirEntryErrOk)
528 0 : return (err);
529 1 : *value = (uint16_t)m;
530 1 : return (TIFFReadDirEntryErrOk);
531 : }
532 0 : case TIFF_SLONG:
533 : {
534 : int32_t m;
535 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
536 0 : err = TIFFReadDirEntryCheckRangeShortSlong(m);
537 0 : if (err != TIFFReadDirEntryErrOk)
538 0 : return (err);
539 0 : *value = (uint16_t)m;
540 0 : return (TIFFReadDirEntryErrOk);
541 : }
542 0 : case TIFF_LONG8:
543 : {
544 : uint64_t m;
545 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
546 0 : if (err != TIFFReadDirEntryErrOk)
547 0 : return (err);
548 0 : err = TIFFReadDirEntryCheckRangeShortLong8(m);
549 0 : if (err != TIFFReadDirEntryErrOk)
550 0 : return (err);
551 0 : *value = (uint16_t)m;
552 0 : return (TIFFReadDirEntryErrOk);
553 : }
554 0 : case TIFF_SLONG8:
555 : {
556 : int64_t m;
557 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
558 0 : if (err != TIFFReadDirEntryErrOk)
559 0 : return (err);
560 0 : err = TIFFReadDirEntryCheckRangeShortSlong8(m);
561 0 : if (err != TIFFReadDirEntryErrOk)
562 0 : return (err);
563 0 : *value = (uint16_t)m;
564 0 : return (TIFFReadDirEntryErrOk);
565 : }
566 0 : default:
567 0 : return (TIFFReadDirEntryErrType);
568 : }
569 : } /*-- TIFFReadDirEntryShort() --*/
570 :
571 : static enum TIFFReadDirEntryErr
572 0 : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
573 : {
574 : enum TIFFReadDirEntryErr err;
575 0 : if (direntry->tdir_count != 1)
576 0 : return (TIFFReadDirEntryErrCount);
577 0 : switch (direntry->tdir_type)
578 : {
579 0 : case TIFF_BYTE:
580 : {
581 : uint8_t m;
582 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
583 0 : *value = (int16_t)m;
584 0 : return (TIFFReadDirEntryErrOk);
585 : }
586 0 : case TIFF_SBYTE:
587 : {
588 : int8_t m;
589 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
590 0 : *value = (int16_t)m;
591 0 : return (TIFFReadDirEntryErrOk);
592 : }
593 0 : case TIFF_SHORT:
594 : {
595 : uint16_t m;
596 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
597 0 : err = TIFFReadDirEntryCheckRangeSshortShort(m);
598 0 : if (err != TIFFReadDirEntryErrOk)
599 0 : return (err);
600 0 : *value = (int16_t)m;
601 0 : return (TIFFReadDirEntryErrOk);
602 : }
603 0 : case TIFF_SSHORT:
604 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, value);
605 0 : return (TIFFReadDirEntryErrOk);
606 0 : case TIFF_LONG:
607 : {
608 : uint32_t m;
609 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
610 0 : err = TIFFReadDirEntryCheckRangeSshortLong(m);
611 0 : if (err != TIFFReadDirEntryErrOk)
612 0 : return (err);
613 0 : *value = (int16_t)m;
614 0 : return (TIFFReadDirEntryErrOk);
615 : }
616 0 : case TIFF_SLONG:
617 : {
618 : int32_t m;
619 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
620 0 : err = TIFFReadDirEntryCheckRangeSshortSlong(m);
621 0 : if (err != TIFFReadDirEntryErrOk)
622 0 : return (err);
623 0 : *value = (int16_t)m;
624 0 : return (TIFFReadDirEntryErrOk);
625 : }
626 0 : case TIFF_LONG8:
627 : {
628 : uint64_t m;
629 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
630 0 : if (err != TIFFReadDirEntryErrOk)
631 0 : return (err);
632 0 : err = TIFFReadDirEntryCheckRangeSshortLong8(m);
633 0 : if (err != TIFFReadDirEntryErrOk)
634 0 : return (err);
635 0 : *value = (int16_t)m;
636 0 : return (TIFFReadDirEntryErrOk);
637 : }
638 0 : case TIFF_SLONG8:
639 : {
640 : int64_t m;
641 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
642 0 : if (err != TIFFReadDirEntryErrOk)
643 0 : return (err);
644 0 : err = TIFFReadDirEntryCheckRangeSshortSlong8(m);
645 0 : if (err != TIFFReadDirEntryErrOk)
646 0 : return (err);
647 0 : *value = (int16_t)m;
648 0 : return (TIFFReadDirEntryErrOk);
649 : }
650 0 : default:
651 0 : return (TIFFReadDirEntryErrType);
652 : }
653 : } /*-- TIFFReadDirEntrySshort() --*/
654 :
655 : static enum TIFFReadDirEntryErr
656 177379 : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
657 : {
658 : enum TIFFReadDirEntryErr err;
659 177379 : if (direntry->tdir_count != 1)
660 0 : return (TIFFReadDirEntryErrCount);
661 177379 : switch (direntry->tdir_type)
662 : {
663 0 : case TIFF_BYTE:
664 : {
665 : uint8_t m;
666 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
667 0 : *value = (uint32_t)m;
668 0 : return (TIFFReadDirEntryErrOk);
669 : }
670 0 : case TIFF_SBYTE:
671 : {
672 : int8_t m;
673 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
674 0 : err = TIFFReadDirEntryCheckRangeLongSbyte(m);
675 0 : if (err != TIFFReadDirEntryErrOk)
676 0 : return (err);
677 0 : *value = (uint32_t)m;
678 0 : return (TIFFReadDirEntryErrOk);
679 : }
680 171488 : case TIFF_SHORT:
681 : {
682 : uint16_t m;
683 171488 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
684 171495 : *value = (uint32_t)m;
685 171495 : return (TIFFReadDirEntryErrOk);
686 : }
687 0 : case TIFF_SSHORT:
688 : {
689 : int16_t m;
690 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
691 0 : err = TIFFReadDirEntryCheckRangeLongSshort(m);
692 0 : if (err != TIFFReadDirEntryErrOk)
693 0 : return (err);
694 0 : *value = (uint32_t)m;
695 0 : return (TIFFReadDirEntryErrOk);
696 : }
697 5881 : case TIFF_LONG:
698 : case TIFF_IFD:
699 5881 : TIFFReadDirEntryCheckedLong(tif, direntry, value);
700 5880 : return (TIFFReadDirEntryErrOk);
701 0 : case TIFF_SLONG:
702 : {
703 : int32_t m;
704 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
705 0 : err = TIFFReadDirEntryCheckRangeLongSlong(m);
706 0 : if (err != TIFFReadDirEntryErrOk)
707 0 : return (err);
708 0 : *value = (uint32_t)m;
709 0 : return (TIFFReadDirEntryErrOk);
710 : }
711 0 : case TIFF_LONG8:
712 : case TIFF_IFD8:
713 : {
714 : uint64_t m;
715 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
716 0 : if (err != TIFFReadDirEntryErrOk)
717 0 : return (err);
718 0 : err = TIFFReadDirEntryCheckRangeLongLong8(m);
719 0 : if (err != TIFFReadDirEntryErrOk)
720 0 : return (err);
721 0 : *value = (uint32_t)m;
722 0 : return (TIFFReadDirEntryErrOk);
723 : }
724 0 : case TIFF_SLONG8:
725 : {
726 : int64_t m;
727 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
728 0 : if (err != TIFFReadDirEntryErrOk)
729 0 : return (err);
730 0 : err = TIFFReadDirEntryCheckRangeLongSlong8(m);
731 0 : if (err != TIFFReadDirEntryErrOk)
732 0 : return (err);
733 0 : *value = (uint32_t)m;
734 0 : return (TIFFReadDirEntryErrOk);
735 : }
736 10 : default:
737 10 : return (TIFFReadDirEntryErrType);
738 : }
739 : } /*-- TIFFReadDirEntryLong() --*/
740 :
741 : static enum TIFFReadDirEntryErr
742 0 : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
743 : {
744 : enum TIFFReadDirEntryErr err;
745 0 : if (direntry->tdir_count != 1)
746 0 : return (TIFFReadDirEntryErrCount);
747 0 : switch (direntry->tdir_type)
748 : {
749 0 : case TIFF_BYTE:
750 : {
751 : uint8_t m;
752 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
753 0 : *value = (int32_t)m;
754 0 : return (TIFFReadDirEntryErrOk);
755 : }
756 0 : case TIFF_SBYTE:
757 : {
758 : int8_t m;
759 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
760 0 : *value = (int32_t)m;
761 0 : return (TIFFReadDirEntryErrOk);
762 : }
763 0 : case TIFF_SHORT:
764 : {
765 : uint16_t m;
766 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
767 0 : *value = (int32_t)m;
768 0 : return (TIFFReadDirEntryErrOk);
769 : }
770 0 : case TIFF_SSHORT:
771 : {
772 : int16_t m;
773 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
774 0 : *value = (int32_t)m;
775 0 : return (TIFFReadDirEntryErrOk);
776 : }
777 0 : case TIFF_LONG:
778 : {
779 : uint32_t m;
780 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
781 0 : err = TIFFReadDirEntryCheckRangeSlongLong(m);
782 0 : if (err != TIFFReadDirEntryErrOk)
783 0 : return (err);
784 0 : *value = (int32_t)m;
785 0 : return (TIFFReadDirEntryErrOk);
786 : }
787 0 : case TIFF_SLONG:
788 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, value);
789 0 : return (TIFFReadDirEntryErrOk);
790 0 : case TIFF_LONG8:
791 : {
792 : uint64_t m;
793 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
794 0 : if (err != TIFFReadDirEntryErrOk)
795 0 : return (err);
796 0 : err = TIFFReadDirEntryCheckRangeSlongLong8(m);
797 0 : if (err != TIFFReadDirEntryErrOk)
798 0 : return (err);
799 0 : *value = (int32_t)m;
800 0 : return (TIFFReadDirEntryErrOk);
801 : }
802 0 : case TIFF_SLONG8:
803 : {
804 : int64_t m;
805 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
806 0 : if (err != TIFFReadDirEntryErrOk)
807 0 : return (err);
808 0 : err = TIFFReadDirEntryCheckRangeSlongSlong8(m);
809 0 : if (err != TIFFReadDirEntryErrOk)
810 0 : return (err);
811 0 : *value = (int32_t)m;
812 0 : return (TIFFReadDirEntryErrOk);
813 : }
814 0 : default:
815 0 : return (TIFFReadDirEntryErrType);
816 : }
817 : } /*-- TIFFReadDirEntrySlong() --*/
818 :
819 : static enum TIFFReadDirEntryErr
820 8 : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
821 : {
822 : enum TIFFReadDirEntryErr err;
823 8 : if (direntry->tdir_count != 1)
824 0 : return (TIFFReadDirEntryErrCount);
825 8 : switch (direntry->tdir_type)
826 : {
827 0 : case TIFF_BYTE:
828 : {
829 : uint8_t m;
830 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
831 0 : *value = (uint64_t)m;
832 0 : return (TIFFReadDirEntryErrOk);
833 : }
834 0 : case TIFF_SBYTE:
835 : {
836 : int8_t m;
837 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
838 0 : err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
839 0 : if (err != TIFFReadDirEntryErrOk)
840 0 : return (err);
841 0 : *value = (uint64_t)m;
842 0 : return (TIFFReadDirEntryErrOk);
843 : }
844 0 : case TIFF_SHORT:
845 : {
846 : uint16_t m;
847 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
848 0 : *value = (uint64_t)m;
849 0 : return (TIFFReadDirEntryErrOk);
850 : }
851 0 : case TIFF_SSHORT:
852 : {
853 : int16_t m;
854 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
855 0 : err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
856 0 : if (err != TIFFReadDirEntryErrOk)
857 0 : return (err);
858 0 : *value = (uint64_t)m;
859 0 : return (TIFFReadDirEntryErrOk);
860 : }
861 8 : case TIFF_LONG:
862 : case TIFF_IFD:
863 : {
864 : uint32_t m;
865 8 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
866 8 : *value = (uint64_t)m;
867 8 : return (TIFFReadDirEntryErrOk);
868 : }
869 0 : case TIFF_SLONG:
870 : {
871 : int32_t m;
872 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
873 0 : err = TIFFReadDirEntryCheckRangeLong8Slong(m);
874 0 : if (err != TIFFReadDirEntryErrOk)
875 0 : return (err);
876 0 : *value = (uint64_t)m;
877 0 : return (TIFFReadDirEntryErrOk);
878 : }
879 0 : case TIFF_LONG8:
880 : case TIFF_IFD8:
881 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
882 0 : return (err);
883 0 : case TIFF_SLONG8:
884 : {
885 : int64_t m;
886 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
887 0 : if (err != TIFFReadDirEntryErrOk)
888 0 : return (err);
889 0 : err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
890 0 : if (err != TIFFReadDirEntryErrOk)
891 0 : return (err);
892 0 : *value = (uint64_t)m;
893 0 : return (TIFFReadDirEntryErrOk);
894 : }
895 0 : default:
896 0 : return (TIFFReadDirEntryErrType);
897 : }
898 : } /*-- TIFFReadDirEntryLong8() --*/
899 :
900 : static enum TIFFReadDirEntryErr
901 0 : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
902 : {
903 : enum TIFFReadDirEntryErr err;
904 0 : if (direntry->tdir_count != 1)
905 0 : return (TIFFReadDirEntryErrCount);
906 0 : switch (direntry->tdir_type)
907 : {
908 0 : case TIFF_BYTE:
909 : {
910 : uint8_t m;
911 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
912 0 : *value = (int64_t)m;
913 0 : return (TIFFReadDirEntryErrOk);
914 : }
915 0 : case TIFF_SBYTE:
916 : {
917 : int8_t m;
918 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
919 0 : *value = (int64_t)m;
920 0 : return (TIFFReadDirEntryErrOk);
921 : }
922 0 : case TIFF_SHORT:
923 : {
924 : uint16_t m;
925 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
926 0 : *value = (int64_t)m;
927 0 : return (TIFFReadDirEntryErrOk);
928 : }
929 0 : case TIFF_SSHORT:
930 : {
931 : int16_t m;
932 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
933 0 : *value = (int64_t)m;
934 0 : return (TIFFReadDirEntryErrOk);
935 : }
936 0 : case TIFF_LONG:
937 : {
938 : uint32_t m;
939 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
940 0 : *value = (int64_t)m;
941 0 : return (TIFFReadDirEntryErrOk);
942 : }
943 0 : case TIFF_SLONG:
944 : {
945 : int32_t m;
946 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
947 0 : *value = (int64_t)m;
948 0 : return (TIFFReadDirEntryErrOk);
949 : }
950 0 : case TIFF_LONG8:
951 : {
952 : uint64_t m;
953 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
954 0 : if (err != TIFFReadDirEntryErrOk)
955 0 : return (err);
956 0 : err = TIFFReadDirEntryCheckRangeSlong8Long8(m);
957 0 : if (err != TIFFReadDirEntryErrOk)
958 0 : return (err);
959 0 : *value = (int64_t)m;
960 0 : return (TIFFReadDirEntryErrOk);
961 : }
962 0 : case TIFF_SLONG8:
963 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
964 0 : return (err);
965 0 : default:
966 0 : return (TIFFReadDirEntryErrType);
967 : }
968 : } /*-- TIFFReadDirEntrySlong8() --*/
969 :
970 : static enum TIFFReadDirEntryErr
971 248 : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
972 : {
973 : enum TIFFReadDirEntryErr err;
974 248 : if (direntry->tdir_count != 1)
975 0 : return (TIFFReadDirEntryErrCount);
976 248 : switch (direntry->tdir_type)
977 : {
978 0 : case TIFF_BYTE:
979 : {
980 : uint8_t m;
981 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
982 0 : *value = (float)m;
983 0 : return (TIFFReadDirEntryErrOk);
984 : }
985 0 : case TIFF_SBYTE:
986 : {
987 : int8_t m;
988 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
989 0 : *value = (float)m;
990 0 : return (TIFFReadDirEntryErrOk);
991 : }
992 0 : case TIFF_SHORT:
993 : {
994 : uint16_t m;
995 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
996 0 : *value = (float)m;
997 0 : return (TIFFReadDirEntryErrOk);
998 : }
999 0 : case TIFF_SSHORT:
1000 : {
1001 : int16_t m;
1002 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1003 0 : *value = (float)m;
1004 0 : return (TIFFReadDirEntryErrOk);
1005 : }
1006 0 : case TIFF_LONG:
1007 : {
1008 : uint32_t m;
1009 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1010 0 : *value = (float)m;
1011 0 : return (TIFFReadDirEntryErrOk);
1012 : }
1013 0 : case TIFF_SLONG:
1014 : {
1015 : int32_t m;
1016 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1017 0 : *value = (float)m;
1018 0 : return (TIFFReadDirEntryErrOk);
1019 : }
1020 0 : case TIFF_LONG8:
1021 : {
1022 : uint64_t m;
1023 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1024 0 : if (err != TIFFReadDirEntryErrOk)
1025 0 : return (err);
1026 0 : *value = (float)m;
1027 0 : return (TIFFReadDirEntryErrOk);
1028 : }
1029 0 : case TIFF_SLONG8:
1030 : {
1031 : int64_t m;
1032 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1033 0 : if (err != TIFFReadDirEntryErrOk)
1034 0 : return (err);
1035 0 : *value = (float)m;
1036 0 : return (TIFFReadDirEntryErrOk);
1037 : }
1038 248 : case TIFF_RATIONAL:
1039 : {
1040 : double m;
1041 248 : err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
1042 248 : if (err != TIFFReadDirEntryErrOk)
1043 0 : return (err);
1044 248 : *value = (float)m;
1045 248 : return (TIFFReadDirEntryErrOk);
1046 : }
1047 0 : case TIFF_SRATIONAL:
1048 : {
1049 : double m;
1050 0 : err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
1051 0 : if (err != TIFFReadDirEntryErrOk)
1052 0 : return (err);
1053 0 : *value = (float)m;
1054 0 : return (TIFFReadDirEntryErrOk);
1055 : }
1056 0 : case TIFF_FLOAT:
1057 0 : TIFFReadDirEntryCheckedFloat(tif, direntry, value);
1058 0 : return (TIFFReadDirEntryErrOk);
1059 0 : case TIFF_DOUBLE:
1060 : {
1061 : double m;
1062 0 : err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
1063 0 : if (err != TIFFReadDirEntryErrOk)
1064 0 : return (err);
1065 0 : if ((m > FLT_MAX) || (m < -FLT_MAX))
1066 0 : return (TIFFReadDirEntryErrRange);
1067 0 : *value = (float)m;
1068 0 : return (TIFFReadDirEntryErrOk);
1069 : }
1070 0 : default:
1071 0 : return (TIFFReadDirEntryErrType);
1072 : }
1073 : }
1074 :
1075 : static enum TIFFReadDirEntryErr
1076 0 : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
1077 : {
1078 : enum TIFFReadDirEntryErr err;
1079 0 : if (direntry->tdir_count != 1)
1080 0 : return (TIFFReadDirEntryErrCount);
1081 0 : switch (direntry->tdir_type)
1082 : {
1083 0 : case TIFF_BYTE:
1084 : {
1085 : uint8_t m;
1086 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1087 0 : *value = (double)m;
1088 0 : return (TIFFReadDirEntryErrOk);
1089 : }
1090 0 : case TIFF_SBYTE:
1091 : {
1092 : int8_t m;
1093 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1094 0 : *value = (double)m;
1095 0 : return (TIFFReadDirEntryErrOk);
1096 : }
1097 0 : case TIFF_SHORT:
1098 : {
1099 : uint16_t m;
1100 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1101 0 : *value = (double)m;
1102 0 : return (TIFFReadDirEntryErrOk);
1103 : }
1104 0 : case TIFF_SSHORT:
1105 : {
1106 : int16_t m;
1107 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1108 0 : *value = (double)m;
1109 0 : return (TIFFReadDirEntryErrOk);
1110 : }
1111 0 : case TIFF_LONG:
1112 : {
1113 : uint32_t m;
1114 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1115 0 : *value = (double)m;
1116 0 : return (TIFFReadDirEntryErrOk);
1117 : }
1118 0 : case TIFF_SLONG:
1119 : {
1120 : int32_t m;
1121 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1122 0 : *value = (double)m;
1123 0 : return (TIFFReadDirEntryErrOk);
1124 : }
1125 0 : case TIFF_LONG8:
1126 : {
1127 : uint64_t m;
1128 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1129 0 : if (err != TIFFReadDirEntryErrOk)
1130 0 : return (err);
1131 0 : *value = (double)m;
1132 0 : return (TIFFReadDirEntryErrOk);
1133 : }
1134 0 : case TIFF_SLONG8:
1135 : {
1136 : int64_t m;
1137 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1138 0 : if (err != TIFFReadDirEntryErrOk)
1139 0 : return (err);
1140 0 : *value = (double)m;
1141 0 : return (TIFFReadDirEntryErrOk);
1142 : }
1143 0 : case TIFF_RATIONAL:
1144 0 : err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
1145 0 : return (err);
1146 0 : case TIFF_SRATIONAL:
1147 0 : err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
1148 0 : return (err);
1149 0 : case TIFF_FLOAT:
1150 : {
1151 : float m;
1152 0 : TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
1153 0 : *value = (double)m;
1154 0 : return (TIFFReadDirEntryErrOk);
1155 : }
1156 0 : case TIFF_DOUBLE:
1157 0 : err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
1158 0 : return (err);
1159 0 : default:
1160 0 : return (TIFFReadDirEntryErrType);
1161 : }
1162 : }
1163 :
1164 : static enum TIFFReadDirEntryErr
1165 0 : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1166 : {
1167 : enum TIFFReadDirEntryErr err;
1168 0 : if (direntry->tdir_count != 1)
1169 0 : return (TIFFReadDirEntryErrCount);
1170 0 : switch (direntry->tdir_type)
1171 : {
1172 0 : case TIFF_LONG:
1173 : case TIFF_IFD:
1174 : {
1175 : uint32_t m;
1176 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1177 0 : *value = (uint64_t)m;
1178 0 : return (TIFFReadDirEntryErrOk);
1179 : }
1180 0 : case TIFF_LONG8:
1181 : case TIFF_IFD8:
1182 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1183 0 : return (err);
1184 0 : default:
1185 0 : return (TIFFReadDirEntryErrType);
1186 : }
1187 : }
1188 :
1189 : #define INITIAL_THRESHOLD (1024 * 1024)
1190 : #define THRESHOLD_MULTIPLIER 10
1191 : #define MAX_THRESHOLD \
1192 : (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \
1193 : INITIAL_THRESHOLD)
1194 :
1195 182124 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
1196 : uint64_t offset,
1197 : tmsize_t size,
1198 : void **pdest)
1199 : {
1200 : #if SIZEOF_SIZE_T == 8
1201 182124 : tmsize_t threshold = INITIAL_THRESHOLD;
1202 : #endif
1203 182124 : tmsize_t already_read = 0;
1204 :
1205 182124 : assert(!isMapped(tif));
1206 :
1207 182124 : if (!SeekOK(tif, offset))
1208 0 : return (TIFFReadDirEntryErrIo);
1209 :
1210 : /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
1211 : /* so as to avoid allocating too much memory in case the file is too */
1212 : /* short. We could ask for the file size, but this might be */
1213 : /* expensive with some I/O layers (think of reading a gzipped file) */
1214 : /* Restrict to 64 bit processes, so as to avoid reallocs() */
1215 : /* on 32 bit processes where virtual memory is scarce. */
1216 364273 : while (already_read < size)
1217 : {
1218 : void *new_dest;
1219 : tmsize_t bytes_read;
1220 182145 : tmsize_t to_read = size - already_read;
1221 : #if SIZEOF_SIZE_T == 8
1222 182145 : if (to_read >= threshold && threshold < MAX_THRESHOLD)
1223 : {
1224 18 : to_read = threshold;
1225 18 : threshold *= THRESHOLD_MULTIPLIER;
1226 : }
1227 : #endif
1228 :
1229 : new_dest =
1230 182145 : (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
1231 182159 : if (new_dest == NULL)
1232 : {
1233 0 : TIFFErrorExtR(tif, tif->tif_name,
1234 : "Failed to allocate memory for %s "
1235 : "(%" TIFF_SSIZE_FORMAT
1236 : " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
1237 : "TIFFReadDirEntryArray", (tmsize_t)1,
1238 : already_read + to_read);
1239 0 : return TIFFReadDirEntryErrAlloc;
1240 : }
1241 182159 : *pdest = new_dest;
1242 :
1243 182159 : bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
1244 182165 : already_read += bytes_read;
1245 182165 : if (bytes_read != to_read)
1246 : {
1247 21 : return TIFFReadDirEntryErrIo;
1248 : }
1249 : }
1250 182128 : return TIFFReadDirEntryErrOk;
1251 : }
1252 :
1253 : /* Caution: if raising that value, make sure int32 / uint32 overflows can't
1254 : * occur elsewhere */
1255 : #define MAX_SIZE_TAG_DATA 2147483647U
1256 :
1257 : static enum TIFFReadDirEntryErr
1258 266156 : TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
1259 : uint32_t *count, uint32_t desttypesize,
1260 : void **value, uint64_t maxcount)
1261 : {
1262 : int typesize;
1263 : uint32_t datasize;
1264 : void *data;
1265 : uint64_t target_count64;
1266 : int original_datasize_clamped;
1267 266156 : typesize = TIFFDataWidth((TIFFDataType)direntry->tdir_type);
1268 :
1269 266125 : target_count64 =
1270 266125 : (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
1271 :
1272 266125 : if ((target_count64 == 0) || (typesize == 0))
1273 : {
1274 0 : *value = 0;
1275 0 : return (TIFFReadDirEntryErrOk);
1276 : }
1277 : (void)desttypesize;
1278 :
1279 : /* We just want to know if the original tag size is more than 4 bytes
1280 : * (classic TIFF) or 8 bytes (BigTIFF)
1281 : */
1282 266136 : original_datasize_clamped =
1283 266136 : ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
1284 : typesize;
1285 :
1286 : /*
1287 : * As a sanity check, make sure we have no more than a 2GB tag array
1288 : * in either the current data type or the dest data type. This also
1289 : * avoids problems with overflow of tmsize_t on 32bit systems.
1290 : */
1291 266136 : if ((uint64_t)(MAX_SIZE_TAG_DATA / (unsigned int)typesize) < target_count64)
1292 3 : return (TIFFReadDirEntryErrSizesan);
1293 266133 : if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
1294 0 : return (TIFFReadDirEntryErrSizesan);
1295 :
1296 266133 : *count = (uint32_t)target_count64;
1297 266133 : datasize = (uint32_t)(*count) * (unsigned int)typesize;
1298 266133 : assert((tmsize_t)datasize > 0);
1299 :
1300 266133 : if (datasize > 100 * 1024 * 1024)
1301 : {
1302 : /* Before allocating a huge amount of memory for corrupted files, check
1303 : * if size of requested memory is not greater than file size.
1304 : */
1305 15 : const uint64_t filesize = TIFFGetFileSize(tif);
1306 15 : if (datasize > filesize)
1307 : {
1308 15 : TIFFWarningExtR(tif, "ReadDirEntryArray",
1309 : "Requested memory size for tag %d (0x%x) %" PRIu32
1310 : " is greater than filesize %" PRIu64
1311 : ". Memory not allocated, tag not read",
1312 15 : direntry->tdir_tag, direntry->tdir_tag, datasize,
1313 : filesize);
1314 15 : return (TIFFReadDirEntryErrAlloc);
1315 : }
1316 : }
1317 :
1318 266118 : if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
1319 0 : return TIFFReadDirEntryErrIo;
1320 :
1321 266118 : if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
1322 265145 : (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
1323 : {
1324 182112 : data = NULL;
1325 : }
1326 : else
1327 : {
1328 84006 : data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
1329 84023 : if (data == 0)
1330 0 : return (TIFFReadDirEntryErrAlloc);
1331 : }
1332 266135 : if (!(tif->tif_flags & TIFF_BIGTIFF))
1333 : {
1334 : /* Only the condition on original_datasize_clamped. The second
1335 : * one is implied, but Coverity Scan cannot see it. */
1336 263898 : if (original_datasize_clamped <= 4 && datasize <= 4)
1337 82695 : _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1338 : else
1339 : {
1340 : enum TIFFReadDirEntryErr err;
1341 181203 : uint32_t offset = direntry->tdir_offset.toff_long;
1342 181203 : if (tif->tif_flags & TIFF_SWAB)
1343 1941 : TIFFSwabLong(&offset);
1344 181171 : if (isMapped(tif))
1345 32 : err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1346 : (tmsize_t)datasize, data);
1347 : else
1348 181139 : err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1349 : (tmsize_t)datasize, &data);
1350 181204 : if (err != TIFFReadDirEntryErrOk)
1351 : {
1352 21 : _TIFFfreeExt(tif, data);
1353 21 : return (err);
1354 : }
1355 : }
1356 : }
1357 : else
1358 : {
1359 : /* See above comment for the Classic TIFF case */
1360 2237 : if (original_datasize_clamped <= 8 && datasize <= 8)
1361 1287 : _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1362 : else
1363 : {
1364 : enum TIFFReadDirEntryErr err;
1365 950 : uint64_t offset = direntry->tdir_offset.toff_long8;
1366 950 : if (tif->tif_flags & TIFF_SWAB)
1367 14 : TIFFSwabLong8(&offset);
1368 959 : if (isMapped(tif))
1369 0 : err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1370 : (tmsize_t)datasize, data);
1371 : else
1372 959 : err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1373 : (tmsize_t)datasize, &data);
1374 959 : if (err != TIFFReadDirEntryErrOk)
1375 : {
1376 0 : _TIFFfreeExt(tif, data);
1377 0 : return (err);
1378 : }
1379 : }
1380 : }
1381 266145 : *value = data;
1382 266145 : return (TIFFReadDirEntryErrOk);
1383 : }
1384 :
1385 : static enum TIFFReadDirEntryErr
1386 181278 : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
1387 : uint32_t desttypesize, void **value)
1388 : {
1389 181278 : return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
1390 : value, ~((uint64_t)0));
1391 : }
1392 :
1393 : static enum TIFFReadDirEntryErr
1394 40478 : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
1395 : {
1396 : enum TIFFReadDirEntryErr err;
1397 : uint32_t count;
1398 : void *origdata;
1399 : uint8_t *data;
1400 40478 : switch (direntry->tdir_type)
1401 : {
1402 40471 : case TIFF_ASCII:
1403 : case TIFF_UNDEFINED:
1404 : case TIFF_BYTE:
1405 : case TIFF_SBYTE:
1406 : case TIFF_SHORT:
1407 : case TIFF_SSHORT:
1408 : case TIFF_LONG:
1409 : case TIFF_SLONG:
1410 : case TIFF_LONG8:
1411 : case TIFF_SLONG8:
1412 40471 : break;
1413 7 : default:
1414 7 : return (TIFFReadDirEntryErrType);
1415 : }
1416 40471 : err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1417 40464 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1418 : {
1419 16 : *value = 0;
1420 16 : return (err);
1421 : }
1422 40448 : switch (direntry->tdir_type)
1423 : {
1424 40430 : case TIFF_ASCII:
1425 : case TIFF_UNDEFINED:
1426 : case TIFF_BYTE:
1427 40430 : *value = (uint8_t *)origdata;
1428 40430 : return (TIFFReadDirEntryErrOk);
1429 0 : case TIFF_SBYTE:
1430 : {
1431 : int8_t *m;
1432 : uint32_t n;
1433 0 : m = (int8_t *)origdata;
1434 0 : for (n = 0; n < count; n++)
1435 : {
1436 0 : err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
1437 0 : if (err != TIFFReadDirEntryErrOk)
1438 : {
1439 0 : _TIFFfreeExt(tif, origdata);
1440 0 : return (err);
1441 : }
1442 0 : m++;
1443 : }
1444 0 : *value = (uint8_t *)origdata;
1445 0 : return (TIFFReadDirEntryErrOk);
1446 : }
1447 18 : default:
1448 18 : break;
1449 : }
1450 18 : data = (uint8_t *)_TIFFmallocExt(tif, count);
1451 0 : if (data == 0)
1452 : {
1453 0 : _TIFFfreeExt(tif, origdata);
1454 0 : return (TIFFReadDirEntryErrAlloc);
1455 : }
1456 0 : switch (direntry->tdir_type)
1457 : {
1458 0 : case TIFF_SHORT:
1459 : {
1460 : uint16_t *ma;
1461 : uint8_t *mb;
1462 : uint32_t n;
1463 0 : ma = (uint16_t *)origdata;
1464 0 : mb = data;
1465 0 : for (n = 0; n < count; n++)
1466 : {
1467 0 : if (tif->tif_flags & TIFF_SWAB)
1468 0 : TIFFSwabShort(ma);
1469 0 : err = TIFFReadDirEntryCheckRangeByteShort(*ma);
1470 0 : if (err != TIFFReadDirEntryErrOk)
1471 0 : break;
1472 0 : *mb++ = (uint8_t)(*ma++);
1473 : }
1474 : }
1475 0 : break;
1476 0 : case TIFF_SSHORT:
1477 : {
1478 : int16_t *ma;
1479 : uint8_t *mb;
1480 : uint32_t n;
1481 0 : ma = (int16_t *)origdata;
1482 0 : mb = data;
1483 0 : for (n = 0; n < count; n++)
1484 : {
1485 0 : if (tif->tif_flags & TIFF_SWAB)
1486 0 : TIFFSwabShort((uint16_t *)ma);
1487 0 : err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
1488 0 : if (err != TIFFReadDirEntryErrOk)
1489 0 : break;
1490 0 : *mb++ = (uint8_t)(*ma++);
1491 : }
1492 : }
1493 0 : break;
1494 0 : case TIFF_LONG:
1495 : {
1496 : uint32_t *ma;
1497 : uint8_t *mb;
1498 : uint32_t n;
1499 0 : ma = (uint32_t *)origdata;
1500 0 : mb = data;
1501 0 : for (n = 0; n < count; n++)
1502 : {
1503 0 : if (tif->tif_flags & TIFF_SWAB)
1504 0 : TIFFSwabLong(ma);
1505 0 : err = TIFFReadDirEntryCheckRangeByteLong(*ma);
1506 0 : if (err != TIFFReadDirEntryErrOk)
1507 0 : break;
1508 0 : *mb++ = (uint8_t)(*ma++);
1509 : }
1510 : }
1511 0 : break;
1512 0 : case TIFF_SLONG:
1513 : {
1514 : int32_t *ma;
1515 : uint8_t *mb;
1516 : uint32_t n;
1517 0 : ma = (int32_t *)origdata;
1518 0 : mb = data;
1519 0 : for (n = 0; n < count; n++)
1520 : {
1521 0 : if (tif->tif_flags & TIFF_SWAB)
1522 0 : TIFFSwabLong((uint32_t *)ma);
1523 0 : err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
1524 0 : if (err != TIFFReadDirEntryErrOk)
1525 0 : break;
1526 0 : *mb++ = (uint8_t)(*ma++);
1527 : }
1528 : }
1529 0 : break;
1530 0 : case TIFF_LONG8:
1531 : {
1532 : uint64_t *ma;
1533 : uint8_t *mb;
1534 : uint32_t n;
1535 0 : ma = (uint64_t *)origdata;
1536 0 : mb = data;
1537 0 : for (n = 0; n < count; n++)
1538 : {
1539 0 : if (tif->tif_flags & TIFF_SWAB)
1540 0 : TIFFSwabLong8(ma);
1541 0 : err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
1542 0 : if (err != TIFFReadDirEntryErrOk)
1543 0 : break;
1544 0 : *mb++ = (uint8_t)(*ma++);
1545 : }
1546 : }
1547 0 : break;
1548 0 : case TIFF_SLONG8:
1549 : {
1550 : int64_t *ma;
1551 : uint8_t *mb;
1552 : uint32_t n;
1553 0 : ma = (int64_t *)origdata;
1554 0 : mb = data;
1555 0 : for (n = 0; n < count; n++)
1556 : {
1557 0 : if (tif->tif_flags & TIFF_SWAB)
1558 0 : TIFFSwabLong8((uint64_t *)ma);
1559 0 : err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1560 0 : if (err != TIFFReadDirEntryErrOk)
1561 0 : break;
1562 0 : *mb++ = (uint8_t)(*ma++);
1563 : }
1564 : }
1565 0 : break;
1566 0 : default:
1567 0 : break;
1568 : }
1569 0 : _TIFFfreeExt(tif, origdata);
1570 0 : if (err != TIFFReadDirEntryErrOk)
1571 : {
1572 0 : _TIFFfreeExt(tif, data);
1573 0 : return (err);
1574 : }
1575 0 : *value = data;
1576 0 : return (TIFFReadDirEntryErrOk);
1577 : }
1578 :
1579 : static enum TIFFReadDirEntryErr
1580 0 : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
1581 : {
1582 : enum TIFFReadDirEntryErr err;
1583 : uint32_t count;
1584 : void *origdata;
1585 : int8_t *data;
1586 0 : switch (direntry->tdir_type)
1587 : {
1588 0 : case TIFF_UNDEFINED:
1589 : case TIFF_BYTE:
1590 : case TIFF_SBYTE:
1591 : case TIFF_SHORT:
1592 : case TIFF_SSHORT:
1593 : case TIFF_LONG:
1594 : case TIFF_SLONG:
1595 : case TIFF_LONG8:
1596 : case TIFF_SLONG8:
1597 0 : break;
1598 0 : default:
1599 0 : return (TIFFReadDirEntryErrType);
1600 : }
1601 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1602 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1603 : {
1604 0 : *value = 0;
1605 0 : return (err);
1606 : }
1607 0 : switch (direntry->tdir_type)
1608 : {
1609 0 : case TIFF_UNDEFINED:
1610 : case TIFF_BYTE:
1611 : {
1612 : uint8_t *m;
1613 : uint32_t n;
1614 0 : m = (uint8_t *)origdata;
1615 0 : for (n = 0; n < count; n++)
1616 : {
1617 0 : err = TIFFReadDirEntryCheckRangeSbyteByte(*m);
1618 0 : if (err != TIFFReadDirEntryErrOk)
1619 : {
1620 0 : _TIFFfreeExt(tif, origdata);
1621 0 : return (err);
1622 : }
1623 0 : m++;
1624 : }
1625 0 : *value = (int8_t *)origdata;
1626 0 : return (TIFFReadDirEntryErrOk);
1627 : }
1628 0 : case TIFF_SBYTE:
1629 0 : *value = (int8_t *)origdata;
1630 0 : return (TIFFReadDirEntryErrOk);
1631 0 : default:
1632 0 : break;
1633 : }
1634 0 : data = (int8_t *)_TIFFmallocExt(tif, count);
1635 0 : if (data == 0)
1636 : {
1637 0 : _TIFFfreeExt(tif, origdata);
1638 0 : return (TIFFReadDirEntryErrAlloc);
1639 : }
1640 0 : switch (direntry->tdir_type)
1641 : {
1642 0 : case TIFF_SHORT:
1643 : {
1644 : uint16_t *ma;
1645 : int8_t *mb;
1646 : uint32_t n;
1647 0 : ma = (uint16_t *)origdata;
1648 0 : mb = data;
1649 0 : for (n = 0; n < count; n++)
1650 : {
1651 0 : if (tif->tif_flags & TIFF_SWAB)
1652 0 : TIFFSwabShort(ma);
1653 0 : err = TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1654 0 : if (err != TIFFReadDirEntryErrOk)
1655 0 : break;
1656 0 : *mb++ = (int8_t)(*ma++);
1657 : }
1658 : }
1659 0 : break;
1660 0 : case TIFF_SSHORT:
1661 : {
1662 : int16_t *ma;
1663 : int8_t *mb;
1664 : uint32_t n;
1665 0 : ma = (int16_t *)origdata;
1666 0 : mb = data;
1667 0 : for (n = 0; n < count; n++)
1668 : {
1669 0 : if (tif->tif_flags & TIFF_SWAB)
1670 0 : TIFFSwabShort((uint16_t *)ma);
1671 0 : err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1672 0 : if (err != TIFFReadDirEntryErrOk)
1673 0 : break;
1674 0 : *mb++ = (int8_t)(*ma++);
1675 : }
1676 : }
1677 0 : break;
1678 0 : case TIFF_LONG:
1679 : {
1680 : uint32_t *ma;
1681 : int8_t *mb;
1682 : uint32_t n;
1683 0 : ma = (uint32_t *)origdata;
1684 0 : mb = data;
1685 0 : for (n = 0; n < count; n++)
1686 : {
1687 0 : if (tif->tif_flags & TIFF_SWAB)
1688 0 : TIFFSwabLong(ma);
1689 0 : err = TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1690 0 : if (err != TIFFReadDirEntryErrOk)
1691 0 : break;
1692 0 : *mb++ = (int8_t)(*ma++);
1693 : }
1694 : }
1695 0 : break;
1696 0 : case TIFF_SLONG:
1697 : {
1698 : int32_t *ma;
1699 : int8_t *mb;
1700 : uint32_t n;
1701 0 : ma = (int32_t *)origdata;
1702 0 : mb = data;
1703 0 : for (n = 0; n < count; n++)
1704 : {
1705 0 : if (tif->tif_flags & TIFF_SWAB)
1706 0 : TIFFSwabLong((uint32_t *)ma);
1707 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1708 0 : if (err != TIFFReadDirEntryErrOk)
1709 0 : break;
1710 0 : *mb++ = (int8_t)(*ma++);
1711 : }
1712 : }
1713 0 : break;
1714 0 : case TIFF_LONG8:
1715 : {
1716 : uint64_t *ma;
1717 : int8_t *mb;
1718 : uint32_t n;
1719 0 : ma = (uint64_t *)origdata;
1720 0 : mb = data;
1721 0 : for (n = 0; n < count; n++)
1722 : {
1723 0 : if (tif->tif_flags & TIFF_SWAB)
1724 0 : TIFFSwabLong8(ma);
1725 0 : err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1726 0 : if (err != TIFFReadDirEntryErrOk)
1727 0 : break;
1728 0 : *mb++ = (int8_t)(*ma++);
1729 : }
1730 : }
1731 0 : break;
1732 0 : case TIFF_SLONG8:
1733 : {
1734 : int64_t *ma;
1735 : int8_t *mb;
1736 : uint32_t n;
1737 0 : ma = (int64_t *)origdata;
1738 0 : mb = data;
1739 0 : for (n = 0; n < count; n++)
1740 : {
1741 0 : if (tif->tif_flags & TIFF_SWAB)
1742 0 : TIFFSwabLong8((uint64_t *)ma);
1743 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1744 0 : if (err != TIFFReadDirEntryErrOk)
1745 0 : break;
1746 0 : *mb++ = (int8_t)(*ma++);
1747 : }
1748 : }
1749 0 : break;
1750 0 : default:
1751 0 : break;
1752 : }
1753 0 : _TIFFfreeExt(tif, origdata);
1754 0 : if (err != TIFFReadDirEntryErrOk)
1755 : {
1756 0 : _TIFFfreeExt(tif, data);
1757 0 : return (err);
1758 : }
1759 0 : *value = data;
1760 0 : return (TIFFReadDirEntryErrOk);
1761 : }
1762 :
1763 : static enum TIFFReadDirEntryErr
1764 68318 : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
1765 : {
1766 : enum TIFFReadDirEntryErr err;
1767 : uint32_t count;
1768 : void *origdata;
1769 : uint16_t *data;
1770 68318 : switch (direntry->tdir_type)
1771 : {
1772 68303 : case TIFF_BYTE:
1773 : case TIFF_SBYTE:
1774 : case TIFF_SHORT:
1775 : case TIFF_SSHORT:
1776 : case TIFF_LONG:
1777 : case TIFF_SLONG:
1778 : case TIFF_LONG8:
1779 : case TIFF_SLONG8:
1780 68303 : break;
1781 15 : default:
1782 15 : return (TIFFReadDirEntryErrType);
1783 : }
1784 68303 : err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1785 68309 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1786 : {
1787 14 : *value = 0;
1788 14 : return (err);
1789 : }
1790 68295 : switch (direntry->tdir_type)
1791 : {
1792 68294 : case TIFF_SHORT:
1793 68294 : *value = (uint16_t *)origdata;
1794 68294 : if (tif->tif_flags & TIFF_SWAB)
1795 1014 : TIFFSwabArrayOfShort(*value, count);
1796 68310 : return (TIFFReadDirEntryErrOk);
1797 0 : case TIFF_SSHORT:
1798 : {
1799 : int16_t *m;
1800 : uint32_t n;
1801 0 : m = (int16_t *)origdata;
1802 0 : for (n = 0; n < count; n++)
1803 : {
1804 0 : if (tif->tif_flags & TIFF_SWAB)
1805 0 : TIFFSwabShort((uint16_t *)m);
1806 0 : err = TIFFReadDirEntryCheckRangeShortSshort(*m);
1807 0 : if (err != TIFFReadDirEntryErrOk)
1808 : {
1809 0 : _TIFFfreeExt(tif, origdata);
1810 0 : return (err);
1811 : }
1812 0 : m++;
1813 : }
1814 0 : *value = (uint16_t *)origdata;
1815 0 : return (TIFFReadDirEntryErrOk);
1816 : }
1817 1 : default:
1818 1 : break;
1819 : }
1820 1 : data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
1821 0 : if (data == 0)
1822 : {
1823 0 : _TIFFfreeExt(tif, origdata);
1824 0 : return (TIFFReadDirEntryErrAlloc);
1825 : }
1826 0 : switch (direntry->tdir_type)
1827 : {
1828 0 : case TIFF_BYTE:
1829 : {
1830 : uint8_t *ma;
1831 : uint16_t *mb;
1832 : uint32_t n;
1833 0 : ma = (uint8_t *)origdata;
1834 0 : mb = data;
1835 0 : for (n = 0; n < count; n++)
1836 0 : *mb++ = (uint16_t)(*ma++);
1837 : }
1838 0 : break;
1839 0 : case TIFF_SBYTE:
1840 : {
1841 : int8_t *ma;
1842 : uint16_t *mb;
1843 : uint32_t n;
1844 0 : ma = (int8_t *)origdata;
1845 0 : mb = data;
1846 0 : for (n = 0; n < count; n++)
1847 : {
1848 0 : err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1849 0 : if (err != TIFFReadDirEntryErrOk)
1850 0 : break;
1851 0 : *mb++ = (uint16_t)(*ma++);
1852 : }
1853 : }
1854 0 : break;
1855 0 : case TIFF_LONG:
1856 : {
1857 : uint32_t *ma;
1858 : uint16_t *mb;
1859 : uint32_t n;
1860 0 : ma = (uint32_t *)origdata;
1861 0 : mb = data;
1862 0 : for (n = 0; n < count; n++)
1863 : {
1864 0 : if (tif->tif_flags & TIFF_SWAB)
1865 0 : TIFFSwabLong(ma);
1866 0 : err = TIFFReadDirEntryCheckRangeShortLong(*ma);
1867 0 : if (err != TIFFReadDirEntryErrOk)
1868 0 : break;
1869 0 : *mb++ = (uint16_t)(*ma++);
1870 : }
1871 : }
1872 0 : break;
1873 0 : case TIFF_SLONG:
1874 : {
1875 : int32_t *ma;
1876 : uint16_t *mb;
1877 : uint32_t n;
1878 0 : ma = (int32_t *)origdata;
1879 0 : mb = data;
1880 0 : for (n = 0; n < count; n++)
1881 : {
1882 0 : if (tif->tif_flags & TIFF_SWAB)
1883 0 : TIFFSwabLong((uint32_t *)ma);
1884 0 : err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
1885 0 : if (err != TIFFReadDirEntryErrOk)
1886 0 : break;
1887 0 : *mb++ = (uint16_t)(*ma++);
1888 : }
1889 : }
1890 0 : break;
1891 0 : case TIFF_LONG8:
1892 : {
1893 : uint64_t *ma;
1894 : uint16_t *mb;
1895 : uint32_t n;
1896 0 : ma = (uint64_t *)origdata;
1897 0 : mb = data;
1898 0 : for (n = 0; n < count; n++)
1899 : {
1900 0 : if (tif->tif_flags & TIFF_SWAB)
1901 0 : TIFFSwabLong8(ma);
1902 0 : err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
1903 0 : if (err != TIFFReadDirEntryErrOk)
1904 0 : break;
1905 0 : *mb++ = (uint16_t)(*ma++);
1906 : }
1907 : }
1908 0 : break;
1909 0 : case TIFF_SLONG8:
1910 : {
1911 : int64_t *ma;
1912 : uint16_t *mb;
1913 : uint32_t n;
1914 0 : ma = (int64_t *)origdata;
1915 0 : mb = data;
1916 0 : for (n = 0; n < count; n++)
1917 : {
1918 0 : if (tif->tif_flags & TIFF_SWAB)
1919 0 : TIFFSwabLong8((uint64_t *)ma);
1920 0 : err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1921 0 : if (err != TIFFReadDirEntryErrOk)
1922 0 : break;
1923 0 : *mb++ = (uint16_t)(*ma++);
1924 : }
1925 : }
1926 0 : break;
1927 0 : default:
1928 0 : break;
1929 : }
1930 0 : _TIFFfreeExt(tif, origdata);
1931 0 : if (err != TIFFReadDirEntryErrOk)
1932 : {
1933 0 : _TIFFfreeExt(tif, data);
1934 0 : return (err);
1935 : }
1936 0 : *value = data;
1937 0 : return (TIFFReadDirEntryErrOk);
1938 : }
1939 :
1940 : static enum TIFFReadDirEntryErr
1941 0 : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
1942 : {
1943 : enum TIFFReadDirEntryErr err;
1944 : uint32_t count;
1945 : void *origdata;
1946 : int16_t *data;
1947 0 : switch (direntry->tdir_type)
1948 : {
1949 0 : case TIFF_BYTE:
1950 : case TIFF_SBYTE:
1951 : case TIFF_SHORT:
1952 : case TIFF_SSHORT:
1953 : case TIFF_LONG:
1954 : case TIFF_SLONG:
1955 : case TIFF_LONG8:
1956 : case TIFF_SLONG8:
1957 0 : break;
1958 0 : default:
1959 0 : return (TIFFReadDirEntryErrType);
1960 : }
1961 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1962 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1963 : {
1964 0 : *value = 0;
1965 0 : return (err);
1966 : }
1967 0 : switch (direntry->tdir_type)
1968 : {
1969 0 : case TIFF_SHORT:
1970 : {
1971 : uint16_t *m;
1972 : uint32_t n;
1973 0 : m = (uint16_t *)origdata;
1974 0 : for (n = 0; n < count; n++)
1975 : {
1976 0 : if (tif->tif_flags & TIFF_SWAB)
1977 0 : TIFFSwabShort(m);
1978 0 : err = TIFFReadDirEntryCheckRangeSshortShort(*m);
1979 0 : if (err != TIFFReadDirEntryErrOk)
1980 : {
1981 0 : _TIFFfreeExt(tif, origdata);
1982 0 : return (err);
1983 : }
1984 0 : m++;
1985 : }
1986 0 : *value = (int16_t *)origdata;
1987 0 : return (TIFFReadDirEntryErrOk);
1988 : }
1989 0 : case TIFF_SSHORT:
1990 0 : *value = (int16_t *)origdata;
1991 0 : if (tif->tif_flags & TIFF_SWAB)
1992 0 : TIFFSwabArrayOfShort((uint16_t *)(*value), count);
1993 0 : return (TIFFReadDirEntryErrOk);
1994 0 : default:
1995 0 : break;
1996 : }
1997 0 : data = (int16_t *)_TIFFmallocExt(tif, count * 2);
1998 0 : if (data == 0)
1999 : {
2000 0 : _TIFFfreeExt(tif, origdata);
2001 0 : return (TIFFReadDirEntryErrAlloc);
2002 : }
2003 0 : switch (direntry->tdir_type)
2004 : {
2005 0 : case TIFF_BYTE:
2006 : {
2007 : uint8_t *ma;
2008 : int16_t *mb;
2009 : uint32_t n;
2010 0 : ma = (uint8_t *)origdata;
2011 0 : mb = data;
2012 0 : for (n = 0; n < count; n++)
2013 0 : *mb++ = (int16_t)(*ma++);
2014 : }
2015 0 : break;
2016 0 : case TIFF_SBYTE:
2017 : {
2018 : int8_t *ma;
2019 : int16_t *mb;
2020 : uint32_t n;
2021 0 : ma = (int8_t *)origdata;
2022 0 : mb = data;
2023 0 : for (n = 0; n < count; n++)
2024 0 : *mb++ = (int16_t)(*ma++);
2025 : }
2026 0 : break;
2027 0 : case TIFF_LONG:
2028 : {
2029 : uint32_t *ma;
2030 : int16_t *mb;
2031 : uint32_t n;
2032 0 : ma = (uint32_t *)origdata;
2033 0 : mb = data;
2034 0 : for (n = 0; n < count; n++)
2035 : {
2036 0 : if (tif->tif_flags & TIFF_SWAB)
2037 0 : TIFFSwabLong(ma);
2038 0 : err = TIFFReadDirEntryCheckRangeSshortLong(*ma);
2039 0 : if (err != TIFFReadDirEntryErrOk)
2040 0 : break;
2041 0 : *mb++ = (int16_t)(*ma++);
2042 : }
2043 : }
2044 0 : break;
2045 0 : case TIFF_SLONG:
2046 : {
2047 : int32_t *ma;
2048 : int16_t *mb;
2049 : uint32_t n;
2050 0 : ma = (int32_t *)origdata;
2051 0 : mb = data;
2052 0 : for (n = 0; n < count; n++)
2053 : {
2054 0 : if (tif->tif_flags & TIFF_SWAB)
2055 0 : TIFFSwabLong((uint32_t *)ma);
2056 0 : err = TIFFReadDirEntryCheckRangeSshortSlong(*ma);
2057 0 : if (err != TIFFReadDirEntryErrOk)
2058 0 : break;
2059 0 : *mb++ = (int16_t)(*ma++);
2060 : }
2061 : }
2062 0 : break;
2063 0 : case TIFF_LONG8:
2064 : {
2065 : uint64_t *ma;
2066 : int16_t *mb;
2067 : uint32_t n;
2068 0 : ma = (uint64_t *)origdata;
2069 0 : mb = data;
2070 0 : for (n = 0; n < count; n++)
2071 : {
2072 0 : if (tif->tif_flags & TIFF_SWAB)
2073 0 : TIFFSwabLong8(ma);
2074 0 : err = TIFFReadDirEntryCheckRangeSshortLong8(*ma);
2075 0 : if (err != TIFFReadDirEntryErrOk)
2076 0 : break;
2077 0 : *mb++ = (int16_t)(*ma++);
2078 : }
2079 : }
2080 0 : break;
2081 0 : case TIFF_SLONG8:
2082 : {
2083 : int64_t *ma;
2084 : int16_t *mb;
2085 : uint32_t n;
2086 0 : ma = (int64_t *)origdata;
2087 0 : mb = data;
2088 0 : for (n = 0; n < count; n++)
2089 : {
2090 0 : if (tif->tif_flags & TIFF_SWAB)
2091 0 : TIFFSwabLong8((uint64_t *)ma);
2092 0 : err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
2093 0 : if (err != TIFFReadDirEntryErrOk)
2094 0 : break;
2095 0 : *mb++ = (int16_t)(*ma++);
2096 : }
2097 : }
2098 0 : break;
2099 0 : default:
2100 0 : break;
2101 : }
2102 0 : _TIFFfreeExt(tif, origdata);
2103 0 : if (err != TIFFReadDirEntryErrOk)
2104 : {
2105 0 : _TIFFfreeExt(tif, data);
2106 0 : return (err);
2107 : }
2108 0 : *value = data;
2109 0 : return (TIFFReadDirEntryErrOk);
2110 : }
2111 :
2112 : static enum TIFFReadDirEntryErr
2113 801 : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
2114 : {
2115 : enum TIFFReadDirEntryErr err;
2116 : uint32_t count;
2117 : void *origdata;
2118 : uint32_t *data;
2119 801 : switch (direntry->tdir_type)
2120 : {
2121 801 : case TIFF_BYTE:
2122 : case TIFF_SBYTE:
2123 : case TIFF_SHORT:
2124 : case TIFF_SSHORT:
2125 : case TIFF_LONG:
2126 : case TIFF_SLONG:
2127 : case TIFF_LONG8:
2128 : case TIFF_SLONG8:
2129 : case TIFF_IFD:
2130 : case TIFF_IFD8:
2131 801 : break;
2132 0 : default:
2133 0 : return (TIFFReadDirEntryErrType);
2134 : }
2135 801 : err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2136 801 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2137 : {
2138 9 : *value = 0;
2139 9 : return (err);
2140 : }
2141 792 : switch (direntry->tdir_type)
2142 : {
2143 792 : case TIFF_LONG:
2144 : case TIFF_IFD:
2145 792 : *value = (uint32_t *)origdata;
2146 792 : if (tif->tif_flags & TIFF_SWAB)
2147 0 : TIFFSwabArrayOfLong(*value, count);
2148 792 : return (TIFFReadDirEntryErrOk);
2149 0 : case TIFF_SLONG:
2150 : {
2151 : int32_t *m;
2152 : uint32_t n;
2153 0 : m = (int32_t *)origdata;
2154 0 : for (n = 0; n < count; n++)
2155 : {
2156 0 : if (tif->tif_flags & TIFF_SWAB)
2157 0 : TIFFSwabLong((uint32_t *)m);
2158 0 : err = TIFFReadDirEntryCheckRangeLongSlong(*m);
2159 0 : if (err != TIFFReadDirEntryErrOk)
2160 : {
2161 0 : _TIFFfreeExt(tif, origdata);
2162 0 : return (err);
2163 : }
2164 0 : m++;
2165 : }
2166 0 : *value = (uint32_t *)origdata;
2167 0 : return (TIFFReadDirEntryErrOk);
2168 : }
2169 0 : default:
2170 0 : break;
2171 : }
2172 0 : data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
2173 0 : if (data == 0)
2174 : {
2175 0 : _TIFFfreeExt(tif, origdata);
2176 0 : return (TIFFReadDirEntryErrAlloc);
2177 : }
2178 0 : switch (direntry->tdir_type)
2179 : {
2180 0 : case TIFF_BYTE:
2181 : {
2182 : uint8_t *ma;
2183 : uint32_t *mb;
2184 : uint32_t n;
2185 0 : ma = (uint8_t *)origdata;
2186 0 : mb = data;
2187 0 : for (n = 0; n < count; n++)
2188 0 : *mb++ = (uint32_t)(*ma++);
2189 : }
2190 0 : break;
2191 0 : case TIFF_SBYTE:
2192 : {
2193 : int8_t *ma;
2194 : uint32_t *mb;
2195 : uint32_t n;
2196 0 : ma = (int8_t *)origdata;
2197 0 : mb = data;
2198 0 : for (n = 0; n < count; n++)
2199 : {
2200 0 : err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
2201 0 : if (err != TIFFReadDirEntryErrOk)
2202 0 : break;
2203 0 : *mb++ = (uint32_t)(*ma++);
2204 : }
2205 : }
2206 0 : break;
2207 0 : case TIFF_SHORT:
2208 : {
2209 : uint16_t *ma;
2210 : uint32_t *mb;
2211 : uint32_t n;
2212 0 : ma = (uint16_t *)origdata;
2213 0 : mb = data;
2214 0 : for (n = 0; n < count; n++)
2215 : {
2216 0 : if (tif->tif_flags & TIFF_SWAB)
2217 0 : TIFFSwabShort(ma);
2218 0 : *mb++ = (uint32_t)(*ma++);
2219 : }
2220 : }
2221 0 : break;
2222 0 : case TIFF_SSHORT:
2223 : {
2224 : int16_t *ma;
2225 : uint32_t *mb;
2226 : uint32_t n;
2227 0 : ma = (int16_t *)origdata;
2228 0 : mb = data;
2229 0 : for (n = 0; n < count; n++)
2230 : {
2231 0 : if (tif->tif_flags & TIFF_SWAB)
2232 0 : TIFFSwabShort((uint16_t *)ma);
2233 0 : err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
2234 0 : if (err != TIFFReadDirEntryErrOk)
2235 0 : break;
2236 0 : *mb++ = (uint32_t)(*ma++);
2237 : }
2238 : }
2239 0 : break;
2240 0 : case TIFF_LONG8:
2241 : case TIFF_IFD8:
2242 : {
2243 : uint64_t *ma;
2244 : uint32_t *mb;
2245 : uint32_t n;
2246 0 : ma = (uint64_t *)origdata;
2247 0 : mb = data;
2248 0 : for (n = 0; n < count; n++)
2249 : {
2250 0 : if (tif->tif_flags & TIFF_SWAB)
2251 0 : TIFFSwabLong8(ma);
2252 0 : err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
2253 0 : if (err != TIFFReadDirEntryErrOk)
2254 0 : break;
2255 0 : *mb++ = (uint32_t)(*ma++);
2256 : }
2257 : }
2258 0 : break;
2259 0 : case TIFF_SLONG8:
2260 : {
2261 : int64_t *ma;
2262 : uint32_t *mb;
2263 : uint32_t n;
2264 0 : ma = (int64_t *)origdata;
2265 0 : mb = data;
2266 0 : for (n = 0; n < count; n++)
2267 : {
2268 0 : if (tif->tif_flags & TIFF_SWAB)
2269 0 : TIFFSwabLong8((uint64_t *)ma);
2270 0 : err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
2271 0 : if (err != TIFFReadDirEntryErrOk)
2272 0 : break;
2273 0 : *mb++ = (uint32_t)(*ma++);
2274 : }
2275 : }
2276 0 : break;
2277 0 : default:
2278 0 : break;
2279 : }
2280 0 : _TIFFfreeExt(tif, origdata);
2281 0 : if (err != TIFFReadDirEntryErrOk)
2282 : {
2283 0 : _TIFFfreeExt(tif, data);
2284 0 : return (err);
2285 : }
2286 0 : *value = data;
2287 0 : return (TIFFReadDirEntryErrOk);
2288 : }
2289 :
2290 : static enum TIFFReadDirEntryErr
2291 0 : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
2292 : {
2293 : enum TIFFReadDirEntryErr err;
2294 : uint32_t count;
2295 : void *origdata;
2296 : int32_t *data;
2297 0 : switch (direntry->tdir_type)
2298 : {
2299 0 : case TIFF_BYTE:
2300 : case TIFF_SBYTE:
2301 : case TIFF_SHORT:
2302 : case TIFF_SSHORT:
2303 : case TIFF_LONG:
2304 : case TIFF_SLONG:
2305 : case TIFF_LONG8:
2306 : case TIFF_SLONG8:
2307 0 : break;
2308 0 : default:
2309 0 : return (TIFFReadDirEntryErrType);
2310 : }
2311 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2312 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2313 : {
2314 0 : *value = 0;
2315 0 : return (err);
2316 : }
2317 0 : switch (direntry->tdir_type)
2318 : {
2319 0 : case TIFF_LONG:
2320 : {
2321 : uint32_t *m;
2322 : uint32_t n;
2323 0 : m = (uint32_t *)origdata;
2324 0 : for (n = 0; n < count; n++)
2325 : {
2326 0 : if (tif->tif_flags & TIFF_SWAB)
2327 0 : TIFFSwabLong((uint32_t *)m);
2328 0 : err = TIFFReadDirEntryCheckRangeSlongLong(*m);
2329 0 : if (err != TIFFReadDirEntryErrOk)
2330 : {
2331 0 : _TIFFfreeExt(tif, origdata);
2332 0 : return (err);
2333 : }
2334 0 : m++;
2335 : }
2336 0 : *value = (int32_t *)origdata;
2337 0 : return (TIFFReadDirEntryErrOk);
2338 : }
2339 0 : case TIFF_SLONG:
2340 0 : *value = (int32_t *)origdata;
2341 0 : if (tif->tif_flags & TIFF_SWAB)
2342 0 : TIFFSwabArrayOfLong((uint32_t *)(*value), count);
2343 0 : return (TIFFReadDirEntryErrOk);
2344 0 : default:
2345 0 : break;
2346 : }
2347 0 : data = (int32_t *)_TIFFmallocExt(tif, count * 4);
2348 0 : if (data == 0)
2349 : {
2350 0 : _TIFFfreeExt(tif, origdata);
2351 0 : return (TIFFReadDirEntryErrAlloc);
2352 : }
2353 0 : switch (direntry->tdir_type)
2354 : {
2355 0 : case TIFF_BYTE:
2356 : {
2357 : uint8_t *ma;
2358 : int32_t *mb;
2359 : uint32_t n;
2360 0 : ma = (uint8_t *)origdata;
2361 0 : mb = data;
2362 0 : for (n = 0; n < count; n++)
2363 0 : *mb++ = (int32_t)(*ma++);
2364 : }
2365 0 : break;
2366 0 : case TIFF_SBYTE:
2367 : {
2368 : int8_t *ma;
2369 : int32_t *mb;
2370 : uint32_t n;
2371 0 : ma = (int8_t *)origdata;
2372 0 : mb = data;
2373 0 : for (n = 0; n < count; n++)
2374 0 : *mb++ = (int32_t)(*ma++);
2375 : }
2376 0 : break;
2377 0 : case TIFF_SHORT:
2378 : {
2379 : uint16_t *ma;
2380 : int32_t *mb;
2381 : uint32_t n;
2382 0 : ma = (uint16_t *)origdata;
2383 0 : mb = data;
2384 0 : for (n = 0; n < count; n++)
2385 : {
2386 0 : if (tif->tif_flags & TIFF_SWAB)
2387 0 : TIFFSwabShort(ma);
2388 0 : *mb++ = (int32_t)(*ma++);
2389 : }
2390 : }
2391 0 : break;
2392 0 : case TIFF_SSHORT:
2393 : {
2394 : int16_t *ma;
2395 : int32_t *mb;
2396 : uint32_t n;
2397 0 : ma = (int16_t *)origdata;
2398 0 : mb = data;
2399 0 : for (n = 0; n < count; n++)
2400 : {
2401 0 : if (tif->tif_flags & TIFF_SWAB)
2402 0 : TIFFSwabShort((uint16_t *)ma);
2403 0 : *mb++ = (int32_t)(*ma++);
2404 : }
2405 : }
2406 0 : break;
2407 0 : case TIFF_LONG8:
2408 : {
2409 : uint64_t *ma;
2410 : int32_t *mb;
2411 : uint32_t n;
2412 0 : ma = (uint64_t *)origdata;
2413 0 : mb = data;
2414 0 : for (n = 0; n < count; n++)
2415 : {
2416 0 : if (tif->tif_flags & TIFF_SWAB)
2417 0 : TIFFSwabLong8(ma);
2418 0 : err = TIFFReadDirEntryCheckRangeSlongLong8(*ma);
2419 0 : if (err != TIFFReadDirEntryErrOk)
2420 0 : break;
2421 0 : *mb++ = (int32_t)(*ma++);
2422 : }
2423 : }
2424 0 : break;
2425 0 : case TIFF_SLONG8:
2426 : {
2427 : int64_t *ma;
2428 : int32_t *mb;
2429 : uint32_t n;
2430 0 : ma = (int64_t *)origdata;
2431 0 : mb = data;
2432 0 : for (n = 0; n < count; n++)
2433 : {
2434 0 : if (tif->tif_flags & TIFF_SWAB)
2435 0 : TIFFSwabLong8((uint64_t *)ma);
2436 0 : err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
2437 0 : if (err != TIFFReadDirEntryErrOk)
2438 0 : break;
2439 0 : *mb++ = (int32_t)(*ma++);
2440 : }
2441 : }
2442 0 : break;
2443 0 : default:
2444 0 : break;
2445 : }
2446 0 : _TIFFfreeExt(tif, origdata);
2447 0 : if (err != TIFFReadDirEntryErrOk)
2448 : {
2449 0 : _TIFFfreeExt(tif, data);
2450 0 : return (err);
2451 : }
2452 0 : *value = data;
2453 0 : return (TIFFReadDirEntryErrOk);
2454 : }
2455 :
2456 : static enum TIFFReadDirEntryErr
2457 84913 : TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
2458 : uint64_t **value, uint64_t maxcount)
2459 : {
2460 : enum TIFFReadDirEntryErr err;
2461 : uint32_t count;
2462 : void *origdata;
2463 : uint64_t *data;
2464 84913 : switch (direntry->tdir_type)
2465 : {
2466 84881 : case TIFF_BYTE:
2467 : case TIFF_SBYTE:
2468 : case TIFF_SHORT:
2469 : case TIFF_SSHORT:
2470 : case TIFF_LONG:
2471 : case TIFF_SLONG:
2472 : case TIFF_LONG8:
2473 : case TIFF_SLONG8:
2474 : case TIFF_IFD:
2475 : case TIFF_IFD8:
2476 84881 : break;
2477 32 : default:
2478 32 : return (TIFFReadDirEntryErrType);
2479 : }
2480 84881 : err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
2481 : maxcount);
2482 84914 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2483 : {
2484 18 : *value = 0;
2485 18 : return (err);
2486 : }
2487 84896 : switch (direntry->tdir_type)
2488 : {
2489 527 : case TIFF_LONG8:
2490 : case TIFF_IFD8:
2491 527 : *value = (uint64_t *)origdata;
2492 527 : if (tif->tif_flags & TIFF_SWAB)
2493 15 : TIFFSwabArrayOfLong8(*value, count);
2494 527 : return (TIFFReadDirEntryErrOk);
2495 1 : case TIFF_SLONG8:
2496 : {
2497 : int64_t *m;
2498 : uint32_t n;
2499 1 : m = (int64_t *)origdata;
2500 2 : for (n = 0; n < count; n++)
2501 : {
2502 1 : if (tif->tif_flags & TIFF_SWAB)
2503 0 : TIFFSwabLong8((uint64_t *)m);
2504 1 : err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2505 1 : if (err != TIFFReadDirEntryErrOk)
2506 : {
2507 0 : _TIFFfreeExt(tif, origdata);
2508 0 : return (err);
2509 : }
2510 1 : m++;
2511 : }
2512 1 : *value = (uint64_t *)origdata;
2513 1 : return (TIFFReadDirEntryErrOk);
2514 : }
2515 84368 : default:
2516 84368 : break;
2517 : }
2518 84368 : data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
2519 84391 : if (data == 0)
2520 : {
2521 0 : _TIFFfreeExt(tif, origdata);
2522 0 : return (TIFFReadDirEntryErrAlloc);
2523 : }
2524 84399 : switch (direntry->tdir_type)
2525 : {
2526 1 : case TIFF_BYTE:
2527 : {
2528 : uint8_t *ma;
2529 : uint64_t *mb;
2530 : uint32_t n;
2531 1 : ma = (uint8_t *)origdata;
2532 1 : mb = data;
2533 2 : for (n = 0; n < count; n++)
2534 1 : *mb++ = (uint64_t)(*ma++);
2535 : }
2536 1 : break;
2537 0 : case TIFF_SBYTE:
2538 : {
2539 : int8_t *ma;
2540 : uint64_t *mb;
2541 : uint32_t n;
2542 0 : ma = (int8_t *)origdata;
2543 0 : mb = data;
2544 0 : for (n = 0; n < count; n++)
2545 : {
2546 0 : err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2547 0 : if (err != TIFFReadDirEntryErrOk)
2548 0 : break;
2549 0 : *mb++ = (uint64_t)(*ma++);
2550 : }
2551 : }
2552 0 : break;
2553 4114 : case TIFF_SHORT:
2554 : {
2555 : uint16_t *ma;
2556 : uint64_t *mb;
2557 : uint32_t n;
2558 4114 : ma = (uint16_t *)origdata;
2559 4114 : mb = data;
2560 3429470 : for (n = 0; n < count; n++)
2561 : {
2562 3425360 : if (tif->tif_flags & TIFF_SWAB)
2563 13767 : TIFFSwabShort(ma);
2564 3425360 : *mb++ = (uint64_t)(*ma++);
2565 : }
2566 : }
2567 4114 : break;
2568 0 : case TIFF_SSHORT:
2569 : {
2570 : int16_t *ma;
2571 : uint64_t *mb;
2572 : uint32_t n;
2573 0 : ma = (int16_t *)origdata;
2574 0 : mb = data;
2575 0 : for (n = 0; n < count; n++)
2576 : {
2577 0 : if (tif->tif_flags & TIFF_SWAB)
2578 0 : TIFFSwabShort((uint16_t *)ma);
2579 0 : err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2580 0 : if (err != TIFFReadDirEntryErrOk)
2581 0 : break;
2582 0 : *mb++ = (uint64_t)(*ma++);
2583 : }
2584 : }
2585 0 : break;
2586 80284 : case TIFF_LONG:
2587 : case TIFF_IFD:
2588 : {
2589 : uint32_t *ma;
2590 : uint64_t *mb;
2591 : uint32_t n;
2592 80284 : ma = (uint32_t *)origdata;
2593 80284 : mb = data;
2594 7850310 : for (n = 0; n < count; n++)
2595 : {
2596 7770050 : if (tif->tif_flags & TIFF_SWAB)
2597 415494 : TIFFSwabLong(ma);
2598 7770020 : *mb++ = (uint64_t)(*ma++);
2599 : }
2600 : }
2601 80260 : break;
2602 0 : case TIFF_SLONG:
2603 : {
2604 : int32_t *ma;
2605 : uint64_t *mb;
2606 : uint32_t n;
2607 0 : ma = (int32_t *)origdata;
2608 0 : mb = data;
2609 0 : for (n = 0; n < count; n++)
2610 : {
2611 0 : if (tif->tif_flags & TIFF_SWAB)
2612 0 : TIFFSwabLong((uint32_t *)ma);
2613 0 : err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2614 0 : if (err != TIFFReadDirEntryErrOk)
2615 0 : break;
2616 0 : *mb++ = (uint64_t)(*ma++);
2617 : }
2618 : }
2619 0 : break;
2620 0 : default:
2621 0 : break;
2622 : }
2623 84375 : _TIFFfreeExt(tif, origdata);
2624 84391 : if (err != TIFFReadDirEntryErrOk)
2625 : {
2626 9 : _TIFFfreeExt(tif, data);
2627 0 : return (err);
2628 : }
2629 84382 : *value = data;
2630 84382 : return (TIFFReadDirEntryErrOk);
2631 : }
2632 :
2633 : static enum TIFFReadDirEntryErr
2634 6 : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
2635 : {
2636 6 : return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
2637 : ~((uint64_t)0));
2638 : }
2639 :
2640 : static enum TIFFReadDirEntryErr
2641 0 : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
2642 : {
2643 : enum TIFFReadDirEntryErr err;
2644 : uint32_t count;
2645 : void *origdata;
2646 : int64_t *data;
2647 0 : switch (direntry->tdir_type)
2648 : {
2649 0 : case TIFF_BYTE:
2650 : case TIFF_SBYTE:
2651 : case TIFF_SHORT:
2652 : case TIFF_SSHORT:
2653 : case TIFF_LONG:
2654 : case TIFF_SLONG:
2655 : case TIFF_LONG8:
2656 : case TIFF_SLONG8:
2657 0 : break;
2658 0 : default:
2659 0 : return (TIFFReadDirEntryErrType);
2660 : }
2661 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
2662 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2663 : {
2664 0 : *value = 0;
2665 0 : return (err);
2666 : }
2667 0 : switch (direntry->tdir_type)
2668 : {
2669 0 : case TIFF_LONG8:
2670 : {
2671 : uint64_t *m;
2672 : uint32_t n;
2673 0 : m = (uint64_t *)origdata;
2674 0 : for (n = 0; n < count; n++)
2675 : {
2676 0 : if (tif->tif_flags & TIFF_SWAB)
2677 0 : TIFFSwabLong8(m);
2678 0 : err = TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2679 0 : if (err != TIFFReadDirEntryErrOk)
2680 : {
2681 0 : _TIFFfreeExt(tif, origdata);
2682 0 : return (err);
2683 : }
2684 0 : m++;
2685 : }
2686 0 : *value = (int64_t *)origdata;
2687 0 : return (TIFFReadDirEntryErrOk);
2688 : }
2689 0 : case TIFF_SLONG8:
2690 0 : *value = (int64_t *)origdata;
2691 0 : if (tif->tif_flags & TIFF_SWAB)
2692 0 : TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
2693 0 : return (TIFFReadDirEntryErrOk);
2694 0 : default:
2695 0 : break;
2696 : }
2697 0 : data = (int64_t *)_TIFFmallocExt(tif, count * 8);
2698 0 : if (data == 0)
2699 : {
2700 0 : _TIFFfreeExt(tif, origdata);
2701 0 : return (TIFFReadDirEntryErrAlloc);
2702 : }
2703 0 : switch (direntry->tdir_type)
2704 : {
2705 0 : case TIFF_BYTE:
2706 : {
2707 : uint8_t *ma;
2708 : int64_t *mb;
2709 : uint32_t n;
2710 0 : ma = (uint8_t *)origdata;
2711 0 : mb = data;
2712 0 : for (n = 0; n < count; n++)
2713 0 : *mb++ = (int64_t)(*ma++);
2714 : }
2715 0 : break;
2716 0 : case TIFF_SBYTE:
2717 : {
2718 : int8_t *ma;
2719 : int64_t *mb;
2720 : uint32_t n;
2721 0 : ma = (int8_t *)origdata;
2722 0 : mb = data;
2723 0 : for (n = 0; n < count; n++)
2724 0 : *mb++ = (int64_t)(*ma++);
2725 : }
2726 0 : break;
2727 0 : case TIFF_SHORT:
2728 : {
2729 : uint16_t *ma;
2730 : int64_t *mb;
2731 : uint32_t n;
2732 0 : ma = (uint16_t *)origdata;
2733 0 : mb = data;
2734 0 : for (n = 0; n < count; n++)
2735 : {
2736 0 : if (tif->tif_flags & TIFF_SWAB)
2737 0 : TIFFSwabShort(ma);
2738 0 : *mb++ = (int64_t)(*ma++);
2739 : }
2740 : }
2741 0 : break;
2742 0 : case TIFF_SSHORT:
2743 : {
2744 : int16_t *ma;
2745 : int64_t *mb;
2746 : uint32_t n;
2747 0 : ma = (int16_t *)origdata;
2748 0 : mb = data;
2749 0 : for (n = 0; n < count; n++)
2750 : {
2751 0 : if (tif->tif_flags & TIFF_SWAB)
2752 0 : TIFFSwabShort((uint16_t *)ma);
2753 0 : *mb++ = (int64_t)(*ma++);
2754 : }
2755 : }
2756 0 : break;
2757 0 : case TIFF_LONG:
2758 : {
2759 : uint32_t *ma;
2760 : int64_t *mb;
2761 : uint32_t n;
2762 0 : ma = (uint32_t *)origdata;
2763 0 : mb = data;
2764 0 : for (n = 0; n < count; n++)
2765 : {
2766 0 : if (tif->tif_flags & TIFF_SWAB)
2767 0 : TIFFSwabLong(ma);
2768 0 : *mb++ = (int64_t)(*ma++);
2769 : }
2770 : }
2771 0 : break;
2772 0 : case TIFF_SLONG:
2773 : {
2774 : int32_t *ma;
2775 : int64_t *mb;
2776 : uint32_t n;
2777 0 : ma = (int32_t *)origdata;
2778 0 : mb = data;
2779 0 : for (n = 0; n < count; n++)
2780 : {
2781 0 : if (tif->tif_flags & TIFF_SWAB)
2782 0 : TIFFSwabLong((uint32_t *)ma);
2783 0 : *mb++ = (int64_t)(*ma++);
2784 : }
2785 : }
2786 0 : break;
2787 0 : default:
2788 0 : break;
2789 : }
2790 0 : _TIFFfreeExt(tif, origdata);
2791 0 : *value = data;
2792 0 : return (TIFFReadDirEntryErrOk);
2793 : }
2794 :
2795 : static enum TIFFReadDirEntryErr
2796 1027 : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
2797 : {
2798 : enum TIFFReadDirEntryErr err;
2799 : uint32_t count;
2800 : void *origdata;
2801 : float *data;
2802 1027 : switch (direntry->tdir_type)
2803 : {
2804 1027 : case TIFF_BYTE:
2805 : case TIFF_SBYTE:
2806 : case TIFF_SHORT:
2807 : case TIFF_SSHORT:
2808 : case TIFF_LONG:
2809 : case TIFF_SLONG:
2810 : case TIFF_LONG8:
2811 : case TIFF_SLONG8:
2812 : case TIFF_RATIONAL:
2813 : case TIFF_SRATIONAL:
2814 : case TIFF_FLOAT:
2815 : case TIFF_DOUBLE:
2816 1027 : break;
2817 0 : default:
2818 0 : return (TIFFReadDirEntryErrType);
2819 : }
2820 1027 : err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2821 1027 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2822 : {
2823 0 : *value = 0;
2824 0 : return (err);
2825 : }
2826 1027 : switch (direntry->tdir_type)
2827 : {
2828 0 : case TIFF_FLOAT:
2829 0 : if (tif->tif_flags & TIFF_SWAB)
2830 0 : TIFFSwabArrayOfLong((uint32_t *)origdata, count);
2831 : TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
2832 0 : *value = (float *)origdata;
2833 0 : return (TIFFReadDirEntryErrOk);
2834 1027 : default:
2835 1027 : break;
2836 : }
2837 1027 : data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
2838 1027 : if (data == 0)
2839 : {
2840 0 : _TIFFfreeExt(tif, origdata);
2841 0 : return (TIFFReadDirEntryErrAlloc);
2842 : }
2843 1027 : switch (direntry->tdir_type)
2844 : {
2845 0 : case TIFF_BYTE:
2846 : {
2847 : uint8_t *ma;
2848 : float *mb;
2849 : uint32_t n;
2850 0 : ma = (uint8_t *)origdata;
2851 0 : mb = data;
2852 0 : for (n = 0; n < count; n++)
2853 0 : *mb++ = (float)(*ma++);
2854 : }
2855 0 : break;
2856 0 : case TIFF_SBYTE:
2857 : {
2858 : int8_t *ma;
2859 : float *mb;
2860 : uint32_t n;
2861 0 : ma = (int8_t *)origdata;
2862 0 : mb = data;
2863 0 : for (n = 0; n < count; n++)
2864 0 : *mb++ = (float)(*ma++);
2865 : }
2866 0 : break;
2867 0 : case TIFF_SHORT:
2868 : {
2869 : uint16_t *ma;
2870 : float *mb;
2871 : uint32_t n;
2872 0 : ma = (uint16_t *)origdata;
2873 0 : mb = data;
2874 0 : for (n = 0; n < count; n++)
2875 : {
2876 0 : if (tif->tif_flags & TIFF_SWAB)
2877 0 : TIFFSwabShort(ma);
2878 0 : *mb++ = (float)(*ma++);
2879 : }
2880 : }
2881 0 : break;
2882 0 : case TIFF_SSHORT:
2883 : {
2884 : int16_t *ma;
2885 : float *mb;
2886 : uint32_t n;
2887 0 : ma = (int16_t *)origdata;
2888 0 : mb = data;
2889 0 : for (n = 0; n < count; n++)
2890 : {
2891 0 : if (tif->tif_flags & TIFF_SWAB)
2892 0 : TIFFSwabShort((uint16_t *)ma);
2893 0 : *mb++ = (float)(*ma++);
2894 : }
2895 : }
2896 0 : break;
2897 2 : case TIFF_LONG:
2898 : {
2899 : uint32_t *ma;
2900 : float *mb;
2901 : uint32_t n;
2902 2 : ma = (uint32_t *)origdata;
2903 2 : mb = data;
2904 14 : for (n = 0; n < count; n++)
2905 : {
2906 12 : if (tif->tif_flags & TIFF_SWAB)
2907 12 : TIFFSwabLong(ma);
2908 12 : *mb++ = (float)(*ma++);
2909 : }
2910 : }
2911 2 : break;
2912 0 : case TIFF_SLONG:
2913 : {
2914 : int32_t *ma;
2915 : float *mb;
2916 : uint32_t n;
2917 0 : ma = (int32_t *)origdata;
2918 0 : mb = data;
2919 0 : for (n = 0; n < count; n++)
2920 : {
2921 0 : if (tif->tif_flags & TIFF_SWAB)
2922 0 : TIFFSwabLong((uint32_t *)ma);
2923 0 : *mb++ = (float)(*ma++);
2924 : }
2925 : }
2926 0 : break;
2927 0 : case TIFF_LONG8:
2928 : {
2929 : uint64_t *ma;
2930 : float *mb;
2931 : uint32_t n;
2932 0 : ma = (uint64_t *)origdata;
2933 0 : mb = data;
2934 0 : for (n = 0; n < count; n++)
2935 : {
2936 0 : if (tif->tif_flags & TIFF_SWAB)
2937 0 : TIFFSwabLong8(ma);
2938 0 : *mb++ = (float)(*ma++);
2939 : }
2940 : }
2941 0 : break;
2942 0 : case TIFF_SLONG8:
2943 : {
2944 : int64_t *ma;
2945 : float *mb;
2946 : uint32_t n;
2947 0 : ma = (int64_t *)origdata;
2948 0 : mb = data;
2949 0 : for (n = 0; n < count; n++)
2950 : {
2951 0 : if (tif->tif_flags & TIFF_SWAB)
2952 0 : TIFFSwabLong8((uint64_t *)ma);
2953 0 : *mb++ = (float)(*ma++);
2954 : }
2955 : }
2956 0 : break;
2957 1025 : case TIFF_RATIONAL:
2958 : {
2959 : uint32_t *ma;
2960 : uint32_t maa;
2961 : uint32_t mab;
2962 : float *mb;
2963 : uint32_t n;
2964 1025 : ma = (uint32_t *)origdata;
2965 1025 : mb = data;
2966 7056 : for (n = 0; n < count; n++)
2967 : {
2968 6031 : if (tif->tif_flags & TIFF_SWAB)
2969 96 : TIFFSwabLong(ma);
2970 6031 : maa = *ma++;
2971 6031 : if (tif->tif_flags & TIFF_SWAB)
2972 96 : TIFFSwabLong(ma);
2973 6031 : mab = *ma++;
2974 6031 : if (mab == 0)
2975 0 : *mb++ = 0.0;
2976 : else
2977 6031 : *mb++ = (float)maa / (float)mab;
2978 : }
2979 : }
2980 1025 : break;
2981 0 : case TIFF_SRATIONAL:
2982 : {
2983 : uint32_t *ma;
2984 : int32_t maa;
2985 : uint32_t mab;
2986 : float *mb;
2987 : uint32_t n;
2988 0 : ma = (uint32_t *)origdata;
2989 0 : mb = data;
2990 0 : for (n = 0; n < count; n++)
2991 : {
2992 0 : if (tif->tif_flags & TIFF_SWAB)
2993 0 : TIFFSwabLong(ma);
2994 0 : maa = *(int32_t *)ma;
2995 0 : ma++;
2996 0 : if (tif->tif_flags & TIFF_SWAB)
2997 0 : TIFFSwabLong(ma);
2998 0 : mab = *ma++;
2999 0 : if (mab == 0)
3000 0 : *mb++ = 0.0;
3001 : else
3002 0 : *mb++ = (float)maa / (float)mab;
3003 : }
3004 : }
3005 0 : break;
3006 0 : case TIFF_DOUBLE:
3007 : {
3008 : double *ma;
3009 : float *mb;
3010 : uint32_t n;
3011 0 : if (tif->tif_flags & TIFF_SWAB)
3012 0 : TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3013 : TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3014 0 : ma = (double *)origdata;
3015 0 : mb = data;
3016 0 : for (n = 0; n < count; n++)
3017 : {
3018 0 : double val = *ma++;
3019 0 : if (val > FLT_MAX)
3020 0 : val = FLT_MAX;
3021 0 : else if (val < -FLT_MAX)
3022 0 : val = -FLT_MAX;
3023 0 : *mb++ = (float)val;
3024 : }
3025 : }
3026 0 : break;
3027 0 : default:
3028 0 : break;
3029 : }
3030 1027 : _TIFFfreeExt(tif, origdata);
3031 1027 : *value = data;
3032 1027 : return (TIFFReadDirEntryErrOk);
3033 : }
3034 :
3035 : static enum TIFFReadDirEntryErr
3036 70596 : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
3037 : {
3038 : enum TIFFReadDirEntryErr err;
3039 : uint32_t count;
3040 : void *origdata;
3041 : double *data;
3042 70596 : switch (direntry->tdir_type)
3043 : {
3044 70585 : case TIFF_BYTE:
3045 : case TIFF_SBYTE:
3046 : case TIFF_SHORT:
3047 : case TIFF_SSHORT:
3048 : case TIFF_LONG:
3049 : case TIFF_SLONG:
3050 : case TIFF_LONG8:
3051 : case TIFF_SLONG8:
3052 : case TIFF_RATIONAL:
3053 : case TIFF_SRATIONAL:
3054 : case TIFF_FLOAT:
3055 : case TIFF_DOUBLE:
3056 70585 : break;
3057 11 : default:
3058 11 : return (TIFFReadDirEntryErrType);
3059 : }
3060 70585 : err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3061 70573 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3062 : {
3063 4 : *value = 0;
3064 4 : return (err);
3065 : }
3066 70569 : switch (direntry->tdir_type)
3067 : {
3068 70563 : case TIFF_DOUBLE:
3069 70563 : if (tif->tif_flags & TIFF_SWAB)
3070 291 : TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3071 : TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3072 70555 : *value = (double *)origdata;
3073 70555 : return (TIFFReadDirEntryErrOk);
3074 6 : default:
3075 6 : break;
3076 : }
3077 6 : data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
3078 0 : if (data == 0)
3079 : {
3080 0 : _TIFFfreeExt(tif, origdata);
3081 0 : return (TIFFReadDirEntryErrAlloc);
3082 : }
3083 0 : switch (direntry->tdir_type)
3084 : {
3085 0 : case TIFF_BYTE:
3086 : {
3087 : uint8_t *ma;
3088 : double *mb;
3089 : uint32_t n;
3090 0 : ma = (uint8_t *)origdata;
3091 0 : mb = data;
3092 0 : for (n = 0; n < count; n++)
3093 0 : *mb++ = (double)(*ma++);
3094 : }
3095 0 : break;
3096 0 : case TIFF_SBYTE:
3097 : {
3098 : int8_t *ma;
3099 : double *mb;
3100 : uint32_t n;
3101 0 : ma = (int8_t *)origdata;
3102 0 : mb = data;
3103 0 : for (n = 0; n < count; n++)
3104 0 : *mb++ = (double)(*ma++);
3105 : }
3106 0 : break;
3107 0 : case TIFF_SHORT:
3108 : {
3109 : uint16_t *ma;
3110 : double *mb;
3111 : uint32_t n;
3112 0 : ma = (uint16_t *)origdata;
3113 0 : mb = data;
3114 0 : for (n = 0; n < count; n++)
3115 : {
3116 0 : if (tif->tif_flags & TIFF_SWAB)
3117 0 : TIFFSwabShort(ma);
3118 0 : *mb++ = (double)(*ma++);
3119 : }
3120 : }
3121 0 : break;
3122 0 : case TIFF_SSHORT:
3123 : {
3124 : int16_t *ma;
3125 : double *mb;
3126 : uint32_t n;
3127 0 : ma = (int16_t *)origdata;
3128 0 : mb = data;
3129 0 : for (n = 0; n < count; n++)
3130 : {
3131 0 : if (tif->tif_flags & TIFF_SWAB)
3132 0 : TIFFSwabShort((uint16_t *)ma);
3133 0 : *mb++ = (double)(*ma++);
3134 : }
3135 : }
3136 0 : break;
3137 0 : case TIFF_LONG:
3138 : {
3139 : uint32_t *ma;
3140 : double *mb;
3141 : uint32_t n;
3142 0 : ma = (uint32_t *)origdata;
3143 0 : mb = data;
3144 0 : for (n = 0; n < count; n++)
3145 : {
3146 0 : if (tif->tif_flags & TIFF_SWAB)
3147 0 : TIFFSwabLong(ma);
3148 0 : *mb++ = (double)(*ma++);
3149 : }
3150 : }
3151 0 : break;
3152 0 : case TIFF_SLONG:
3153 : {
3154 : int32_t *ma;
3155 : double *mb;
3156 : uint32_t n;
3157 0 : ma = (int32_t *)origdata;
3158 0 : mb = data;
3159 0 : for (n = 0; n < count; n++)
3160 : {
3161 0 : if (tif->tif_flags & TIFF_SWAB)
3162 0 : TIFFSwabLong((uint32_t *)ma);
3163 0 : *mb++ = (double)(*ma++);
3164 : }
3165 : }
3166 0 : break;
3167 0 : case TIFF_LONG8:
3168 : {
3169 : uint64_t *ma;
3170 : double *mb;
3171 : uint32_t n;
3172 0 : ma = (uint64_t *)origdata;
3173 0 : mb = data;
3174 0 : for (n = 0; n < count; n++)
3175 : {
3176 0 : if (tif->tif_flags & TIFF_SWAB)
3177 0 : TIFFSwabLong8(ma);
3178 0 : *mb++ = (double)(*ma++);
3179 : }
3180 : }
3181 0 : break;
3182 0 : case TIFF_SLONG8:
3183 : {
3184 : int64_t *ma;
3185 : double *mb;
3186 : uint32_t n;
3187 0 : ma = (int64_t *)origdata;
3188 0 : mb = data;
3189 0 : for (n = 0; n < count; n++)
3190 : {
3191 0 : if (tif->tif_flags & TIFF_SWAB)
3192 0 : TIFFSwabLong8((uint64_t *)ma);
3193 0 : *mb++ = (double)(*ma++);
3194 : }
3195 : }
3196 0 : break;
3197 0 : case TIFF_RATIONAL:
3198 : {
3199 : uint32_t *ma;
3200 : uint32_t maa;
3201 : uint32_t mab;
3202 : double *mb;
3203 : uint32_t n;
3204 0 : ma = (uint32_t *)origdata;
3205 0 : mb = data;
3206 0 : for (n = 0; n < count; n++)
3207 : {
3208 0 : if (tif->tif_flags & TIFF_SWAB)
3209 0 : TIFFSwabLong(ma);
3210 0 : maa = *ma++;
3211 0 : if (tif->tif_flags & TIFF_SWAB)
3212 0 : TIFFSwabLong(ma);
3213 0 : mab = *ma++;
3214 0 : if (mab == 0)
3215 0 : *mb++ = 0.0;
3216 : else
3217 0 : *mb++ = (double)maa / (double)mab;
3218 : }
3219 : }
3220 0 : break;
3221 0 : case TIFF_SRATIONAL:
3222 : {
3223 : uint32_t *ma;
3224 : int32_t maa;
3225 : uint32_t mab;
3226 : double *mb;
3227 : uint32_t n;
3228 0 : ma = (uint32_t *)origdata;
3229 0 : mb = data;
3230 0 : for (n = 0; n < count; n++)
3231 : {
3232 0 : if (tif->tif_flags & TIFF_SWAB)
3233 0 : TIFFSwabLong(ma);
3234 0 : maa = *(int32_t *)ma;
3235 0 : ma++;
3236 0 : if (tif->tif_flags & TIFF_SWAB)
3237 0 : TIFFSwabLong(ma);
3238 0 : mab = *ma++;
3239 0 : if (mab == 0)
3240 0 : *mb++ = 0.0;
3241 : else
3242 0 : *mb++ = (double)maa / (double)mab;
3243 : }
3244 : }
3245 0 : break;
3246 0 : case TIFF_FLOAT:
3247 : {
3248 : float *ma;
3249 : double *mb;
3250 : uint32_t n;
3251 0 : if (tif->tif_flags & TIFF_SWAB)
3252 0 : TIFFSwabArrayOfLong((uint32_t *)origdata, count);
3253 : TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3254 0 : ma = (float *)origdata;
3255 0 : mb = data;
3256 0 : for (n = 0; n < count; n++)
3257 0 : *mb++ = (double)(*ma++);
3258 : }
3259 0 : break;
3260 0 : default:
3261 0 : break;
3262 : }
3263 0 : _TIFFfreeExt(tif, origdata);
3264 0 : *value = data;
3265 0 : return (TIFFReadDirEntryErrOk);
3266 : }
3267 :
3268 : static enum TIFFReadDirEntryErr
3269 81 : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
3270 : {
3271 : enum TIFFReadDirEntryErr err;
3272 : uint32_t count;
3273 : void *origdata;
3274 : uint64_t *data;
3275 81 : switch (direntry->tdir_type)
3276 : {
3277 81 : case TIFF_LONG:
3278 : case TIFF_LONG8:
3279 : case TIFF_IFD:
3280 : case TIFF_IFD8:
3281 81 : break;
3282 0 : default:
3283 0 : return (TIFFReadDirEntryErrType);
3284 : }
3285 81 : err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3286 81 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3287 : {
3288 0 : *value = 0;
3289 0 : return (err);
3290 : }
3291 81 : switch (direntry->tdir_type)
3292 : {
3293 0 : case TIFF_LONG8:
3294 : case TIFF_IFD8:
3295 0 : *value = (uint64_t *)origdata;
3296 0 : if (tif->tif_flags & TIFF_SWAB)
3297 0 : TIFFSwabArrayOfLong8(*value, count);
3298 0 : return (TIFFReadDirEntryErrOk);
3299 81 : default:
3300 81 : break;
3301 : }
3302 81 : data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
3303 81 : if (data == 0)
3304 : {
3305 0 : _TIFFfreeExt(tif, origdata);
3306 0 : return (TIFFReadDirEntryErrAlloc);
3307 : }
3308 81 : switch (direntry->tdir_type)
3309 : {
3310 81 : case TIFF_LONG:
3311 : case TIFF_IFD:
3312 : {
3313 : uint32_t *ma;
3314 : uint64_t *mb;
3315 : uint32_t n;
3316 81 : ma = (uint32_t *)origdata;
3317 81 : mb = data;
3318 333 : for (n = 0; n < count; n++)
3319 : {
3320 252 : if (tif->tif_flags & TIFF_SWAB)
3321 0 : TIFFSwabLong(ma);
3322 252 : *mb++ = (uint64_t)(*ma++);
3323 : }
3324 : }
3325 81 : break;
3326 0 : default:
3327 0 : break;
3328 : }
3329 81 : _TIFFfreeExt(tif, origdata);
3330 81 : *value = data;
3331 81 : return (TIFFReadDirEntryErrOk);
3332 : }
3333 :
3334 : static enum TIFFReadDirEntryErr
3335 32615 : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
3336 : uint16_t *value)
3337 : {
3338 : enum TIFFReadDirEntryErr err;
3339 : uint16_t *m;
3340 : uint16_t *na;
3341 : uint16_t nb;
3342 32615 : if (direntry->tdir_count != (uint64_t)tif->tif_dir.td_samplesperpixel)
3343 : {
3344 0 : const TIFFField *fip = TIFFFieldWithTag(tif, direntry->tdir_tag);
3345 0 : if (direntry->tdir_count == 0)
3346 : {
3347 0 : return TIFFReadDirEntryErrCount;
3348 : }
3349 0 : else if (direntry->tdir_count <
3350 0 : (uint64_t)tif->tif_dir.td_samplesperpixel)
3351 : {
3352 0 : TIFFWarningExtR(
3353 : tif, "TIFFReadDirEntryPersampleShort",
3354 : "Tag %s entry count is %" PRIu64
3355 : " , whereas it should be SamplesPerPixel=%d. Assuming that "
3356 : "missing entries are all at the value of the first one",
3357 : fip ? fip->field_name : "unknown tagname", direntry->tdir_count,
3358 0 : tif->tif_dir.td_samplesperpixel);
3359 : }
3360 : else
3361 : {
3362 0 : TIFFWarningExtR(tif, "TIFFReadDirEntryPersampleShort",
3363 : "Tag %s entry count is %" PRIu64
3364 : " , whereas it should be SamplesPerPixel=%d. "
3365 : "Ignoring extra entries",
3366 : fip ? fip->field_name : "unknown tagname",
3367 : direntry->tdir_count,
3368 0 : tif->tif_dir.td_samplesperpixel);
3369 : }
3370 : }
3371 32615 : err = TIFFReadDirEntryShortArray(tif, direntry, &m);
3372 32615 : if (err != TIFFReadDirEntryErrOk || m == NULL)
3373 2 : return (err);
3374 32613 : na = m;
3375 32613 : nb = tif->tif_dir.td_samplesperpixel;
3376 32613 : if (direntry->tdir_count < nb)
3377 0 : nb = (uint16_t)direntry->tdir_count;
3378 32613 : *value = *na++;
3379 32613 : nb--;
3380 2592930 : while (nb > 0)
3381 : {
3382 2560310 : if (*na++ != *value)
3383 : {
3384 0 : err = TIFFReadDirEntryErrPsdif;
3385 0 : break;
3386 : }
3387 2560310 : nb--;
3388 : }
3389 32613 : _TIFFfreeExt(tif, m);
3390 32614 : return (err);
3391 : }
3392 :
3393 0 : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
3394 : uint8_t *value)
3395 : {
3396 : (void)tif;
3397 0 : *value = *(uint8_t *)(&direntry->tdir_offset);
3398 0 : }
3399 :
3400 0 : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
3401 : int8_t *value)
3402 : {
3403 : (void)tif;
3404 0 : *value = *(int8_t *)(&direntry->tdir_offset);
3405 0 : }
3406 :
3407 468617 : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
3408 : uint16_t *value)
3409 : {
3410 468617 : *value = direntry->tdir_offset.toff_short;
3411 : /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3412 468617 : if (tif->tif_flags & TIFF_SWAB)
3413 4781 : TIFFSwabShort(value);
3414 468617 : }
3415 :
3416 0 : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
3417 : int16_t *value)
3418 : {
3419 0 : *value = *(int16_t *)(&direntry->tdir_offset);
3420 0 : if (tif->tif_flags & TIFF_SWAB)
3421 0 : TIFFSwabShort((uint16_t *)value);
3422 0 : }
3423 :
3424 5891 : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
3425 : uint32_t *value)
3426 : {
3427 5891 : *value = *(uint32_t *)(&direntry->tdir_offset);
3428 5891 : if (tif->tif_flags & TIFF_SWAB)
3429 114 : TIFFSwabLong(value);
3430 5891 : }
3431 :
3432 0 : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
3433 : int32_t *value)
3434 : {
3435 0 : *value = *(int32_t *)(&direntry->tdir_offset);
3436 0 : if (tif->tif_flags & TIFF_SWAB)
3437 0 : TIFFSwabLong((uint32_t *)value);
3438 0 : }
3439 :
3440 : static enum TIFFReadDirEntryErr
3441 0 : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
3442 : {
3443 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3444 : {
3445 : enum TIFFReadDirEntryErr err;
3446 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3447 0 : if (tif->tif_flags & TIFF_SWAB)
3448 0 : TIFFSwabLong(&offset);
3449 0 : err = TIFFReadDirEntryData(tif, offset, 8, value);
3450 0 : if (err != TIFFReadDirEntryErrOk)
3451 0 : return (err);
3452 : }
3453 : else
3454 0 : *value = direntry->tdir_offset.toff_long8;
3455 0 : if (tif->tif_flags & TIFF_SWAB)
3456 0 : TIFFSwabLong8(value);
3457 0 : return (TIFFReadDirEntryErrOk);
3458 : }
3459 :
3460 : static enum TIFFReadDirEntryErr
3461 0 : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
3462 : {
3463 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3464 : {
3465 : enum TIFFReadDirEntryErr err;
3466 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3467 0 : if (tif->tif_flags & TIFF_SWAB)
3468 0 : TIFFSwabLong(&offset);
3469 0 : err = TIFFReadDirEntryData(tif, offset, 8, value);
3470 0 : if (err != TIFFReadDirEntryErrOk)
3471 0 : return (err);
3472 : }
3473 : else
3474 0 : *value = *(int64_t *)(&direntry->tdir_offset);
3475 0 : if (tif->tif_flags & TIFF_SWAB)
3476 0 : TIFFSwabLong8((uint64_t *)value);
3477 0 : return (TIFFReadDirEntryErrOk);
3478 : }
3479 :
3480 : static enum TIFFReadDirEntryErr
3481 248 : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
3482 : double *value)
3483 : {
3484 : UInt64Aligned_t m;
3485 :
3486 : assert(sizeof(double) == 8);
3487 : assert(sizeof(uint64_t) == 8);
3488 : assert(sizeof(uint32_t) == 4);
3489 248 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3490 : {
3491 : enum TIFFReadDirEntryErr err;
3492 248 : uint32_t offset = direntry->tdir_offset.toff_long;
3493 248 : if (tif->tif_flags & TIFF_SWAB)
3494 8 : TIFFSwabLong(&offset);
3495 248 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3496 248 : if (err != TIFFReadDirEntryErrOk)
3497 0 : return (err);
3498 : }
3499 : else
3500 0 : m.l = direntry->tdir_offset.toff_long8;
3501 248 : if (tif->tif_flags & TIFF_SWAB)
3502 8 : TIFFSwabArrayOfLong(m.i, 2);
3503 : /* Not completely sure what we should do when m.i[1]==0, but some */
3504 : /* sanitizers do not like division by 0.0: */
3505 : /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3506 248 : if (m.i[0] == 0 || m.i[1] == 0)
3507 36 : *value = 0.0;
3508 : else
3509 212 : *value = (double)m.i[0] / (double)m.i[1];
3510 248 : return (TIFFReadDirEntryErrOk);
3511 : }
3512 :
3513 : static enum TIFFReadDirEntryErr
3514 0 : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
3515 : double *value)
3516 : {
3517 : UInt64Aligned_t m;
3518 : assert(sizeof(double) == 8);
3519 : assert(sizeof(uint64_t) == 8);
3520 : assert(sizeof(int32_t) == 4);
3521 : assert(sizeof(uint32_t) == 4);
3522 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3523 : {
3524 : enum TIFFReadDirEntryErr err;
3525 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3526 0 : if (tif->tif_flags & TIFF_SWAB)
3527 0 : TIFFSwabLong(&offset);
3528 0 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3529 0 : if (err != TIFFReadDirEntryErrOk)
3530 0 : return (err);
3531 : }
3532 : else
3533 0 : m.l = direntry->tdir_offset.toff_long8;
3534 0 : if (tif->tif_flags & TIFF_SWAB)
3535 0 : TIFFSwabArrayOfLong(m.i, 2);
3536 : /* Not completely sure what we should do when m.i[1]==0, but some */
3537 : /* sanitizers do not like division by 0.0: */
3538 : /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3539 0 : if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
3540 0 : *value = 0.0;
3541 : else
3542 0 : *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
3543 0 : return (TIFFReadDirEntryErrOk);
3544 : }
3545 :
3546 : #if 0
3547 : static enum TIFFReadDirEntryErr
3548 : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
3549 : TIFFRational_t *value)
3550 : { /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
3551 : directly --*/
3552 : UInt64Aligned_t m;
3553 :
3554 : assert(sizeof(double) == 8);
3555 : assert(sizeof(uint64_t) == 8);
3556 : assert(sizeof(uint32_t) == 4);
3557 :
3558 : if (direntry->tdir_count != 1)
3559 : return (TIFFReadDirEntryErrCount);
3560 :
3561 : if (direntry->tdir_type != TIFF_RATIONAL &&
3562 : direntry->tdir_type != TIFF_SRATIONAL)
3563 : return (TIFFReadDirEntryErrType);
3564 :
3565 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3566 : {
3567 : enum TIFFReadDirEntryErr err;
3568 : uint32_t offset = direntry->tdir_offset.toff_long;
3569 : if (tif->tif_flags & TIFF_SWAB)
3570 : TIFFSwabLong(&offset);
3571 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3572 : if (err != TIFFReadDirEntryErrOk)
3573 : return (err);
3574 : }
3575 : else
3576 : {
3577 : m.l = direntry->tdir_offset.toff_long8;
3578 : }
3579 :
3580 : if (tif->tif_flags & TIFF_SWAB)
3581 : TIFFSwabArrayOfLong(m.i, 2);
3582 :
3583 : value->uNum = m.i[0];
3584 : value->uDenom = m.i[1];
3585 : return (TIFFReadDirEntryErrOk);
3586 : } /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
3587 : #endif
3588 :
3589 0 : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
3590 : float *value)
3591 : {
3592 : union
3593 : {
3594 : float f;
3595 : uint32_t i;
3596 : } float_union;
3597 : assert(sizeof(float) == 4);
3598 : assert(sizeof(uint32_t) == 4);
3599 : assert(sizeof(float_union) == 4);
3600 0 : float_union.i = *(uint32_t *)(&direntry->tdir_offset);
3601 0 : *value = float_union.f;
3602 0 : if (tif->tif_flags & TIFF_SWAB)
3603 0 : TIFFSwabLong((uint32_t *)value);
3604 0 : }
3605 :
3606 : static enum TIFFReadDirEntryErr
3607 0 : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
3608 : {
3609 : assert(sizeof(double) == 8);
3610 : assert(sizeof(uint64_t) == 8);
3611 : assert(sizeof(UInt64Aligned_t) == 8);
3612 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3613 : {
3614 : enum TIFFReadDirEntryErr err;
3615 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3616 0 : if (tif->tif_flags & TIFF_SWAB)
3617 0 : TIFFSwabLong(&offset);
3618 0 : err = TIFFReadDirEntryData(tif, offset, 8, value);
3619 0 : if (err != TIFFReadDirEntryErrOk)
3620 0 : return (err);
3621 : }
3622 : else
3623 : {
3624 : UInt64Aligned_t uint64_union;
3625 0 : uint64_union.l = direntry->tdir_offset.toff_long8;
3626 0 : *value = uint64_union.d;
3627 : }
3628 0 : if (tif->tif_flags & TIFF_SWAB)
3629 0 : TIFFSwabLong8((uint64_t *)value);
3630 0 : return (TIFFReadDirEntryErrOk);
3631 : }
3632 :
3633 : static enum TIFFReadDirEntryErr
3634 0 : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
3635 : {
3636 0 : if (value < 0)
3637 0 : return (TIFFReadDirEntryErrRange);
3638 : else
3639 0 : return (TIFFReadDirEntryErrOk);
3640 : }
3641 :
3642 : static enum TIFFReadDirEntryErr
3643 0 : TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
3644 : {
3645 0 : if (value > 0xFF)
3646 0 : return (TIFFReadDirEntryErrRange);
3647 : else
3648 0 : return (TIFFReadDirEntryErrOk);
3649 : }
3650 :
3651 : static enum TIFFReadDirEntryErr
3652 0 : TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
3653 : {
3654 0 : if ((value < 0) || (value > 0xFF))
3655 0 : return (TIFFReadDirEntryErrRange);
3656 : else
3657 0 : return (TIFFReadDirEntryErrOk);
3658 : }
3659 :
3660 : static enum TIFFReadDirEntryErr
3661 0 : TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
3662 : {
3663 0 : if (value > 0xFF)
3664 0 : return (TIFFReadDirEntryErrRange);
3665 : else
3666 0 : return (TIFFReadDirEntryErrOk);
3667 : }
3668 :
3669 : static enum TIFFReadDirEntryErr
3670 0 : TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
3671 : {
3672 0 : if ((value < 0) || (value > 0xFF))
3673 0 : return (TIFFReadDirEntryErrRange);
3674 : else
3675 0 : return (TIFFReadDirEntryErrOk);
3676 : }
3677 :
3678 : static enum TIFFReadDirEntryErr
3679 0 : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
3680 : {
3681 0 : if (value > 0xFF)
3682 0 : return (TIFFReadDirEntryErrRange);
3683 : else
3684 0 : return (TIFFReadDirEntryErrOk);
3685 : }
3686 :
3687 : static enum TIFFReadDirEntryErr
3688 0 : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
3689 : {
3690 0 : if ((value < 0) || (value > 0xFF))
3691 0 : return (TIFFReadDirEntryErrRange);
3692 : else
3693 0 : return (TIFFReadDirEntryErrOk);
3694 : }
3695 :
3696 : static enum TIFFReadDirEntryErr
3697 0 : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
3698 : {
3699 0 : if (value > 0x7F)
3700 0 : return (TIFFReadDirEntryErrRange);
3701 : else
3702 0 : return (TIFFReadDirEntryErrOk);
3703 : }
3704 :
3705 : static enum TIFFReadDirEntryErr
3706 0 : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
3707 : {
3708 0 : if (value > 0x7F)
3709 0 : return (TIFFReadDirEntryErrRange);
3710 : else
3711 0 : return (TIFFReadDirEntryErrOk);
3712 : }
3713 :
3714 : static enum TIFFReadDirEntryErr
3715 0 : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
3716 : {
3717 0 : if ((value < -0x80) || (value > 0x7F))
3718 0 : return (TIFFReadDirEntryErrRange);
3719 : else
3720 0 : return (TIFFReadDirEntryErrOk);
3721 : }
3722 :
3723 : static enum TIFFReadDirEntryErr
3724 0 : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
3725 : {
3726 0 : if (value > 0x7F)
3727 0 : return (TIFFReadDirEntryErrRange);
3728 : else
3729 0 : return (TIFFReadDirEntryErrOk);
3730 : }
3731 :
3732 : static enum TIFFReadDirEntryErr
3733 0 : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
3734 : {
3735 0 : if ((value < -0x80) || (value > 0x7F))
3736 0 : return (TIFFReadDirEntryErrRange);
3737 : else
3738 0 : return (TIFFReadDirEntryErrOk);
3739 : }
3740 :
3741 : static enum TIFFReadDirEntryErr
3742 0 : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
3743 : {
3744 0 : if (value > 0x7F)
3745 0 : return (TIFFReadDirEntryErrRange);
3746 : else
3747 0 : return (TIFFReadDirEntryErrOk);
3748 : }
3749 :
3750 : static enum TIFFReadDirEntryErr
3751 0 : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
3752 : {
3753 0 : if ((value < -0x80) || (value > 0x7F))
3754 0 : return (TIFFReadDirEntryErrRange);
3755 : else
3756 0 : return (TIFFReadDirEntryErrOk);
3757 : }
3758 :
3759 : static enum TIFFReadDirEntryErr
3760 0 : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
3761 : {
3762 0 : if (value < 0)
3763 0 : return (TIFFReadDirEntryErrRange);
3764 : else
3765 0 : return (TIFFReadDirEntryErrOk);
3766 : }
3767 :
3768 : static enum TIFFReadDirEntryErr
3769 0 : TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
3770 : {
3771 0 : if (value < 0)
3772 0 : return (TIFFReadDirEntryErrRange);
3773 : else
3774 0 : return (TIFFReadDirEntryErrOk);
3775 : }
3776 :
3777 : static enum TIFFReadDirEntryErr
3778 1 : TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
3779 : {
3780 1 : if (value > 0xFFFF)
3781 0 : return (TIFFReadDirEntryErrRange);
3782 : else
3783 1 : return (TIFFReadDirEntryErrOk);
3784 : }
3785 :
3786 : static enum TIFFReadDirEntryErr
3787 0 : TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
3788 : {
3789 0 : if ((value < 0) || (value > 0xFFFF))
3790 0 : return (TIFFReadDirEntryErrRange);
3791 : else
3792 0 : return (TIFFReadDirEntryErrOk);
3793 : }
3794 :
3795 : static enum TIFFReadDirEntryErr
3796 0 : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
3797 : {
3798 0 : if (value > 0xFFFF)
3799 0 : return (TIFFReadDirEntryErrRange);
3800 : else
3801 0 : return (TIFFReadDirEntryErrOk);
3802 : }
3803 :
3804 : static enum TIFFReadDirEntryErr
3805 0 : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
3806 : {
3807 0 : if ((value < 0) || (value > 0xFFFF))
3808 0 : return (TIFFReadDirEntryErrRange);
3809 : else
3810 0 : return (TIFFReadDirEntryErrOk);
3811 : }
3812 :
3813 : static enum TIFFReadDirEntryErr
3814 0 : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
3815 : {
3816 0 : if (value > 0x7FFF)
3817 0 : return (TIFFReadDirEntryErrRange);
3818 : else
3819 0 : return (TIFFReadDirEntryErrOk);
3820 : }
3821 :
3822 : static enum TIFFReadDirEntryErr
3823 0 : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
3824 : {
3825 0 : if (value > 0x7FFF)
3826 0 : return (TIFFReadDirEntryErrRange);
3827 : else
3828 0 : return (TIFFReadDirEntryErrOk);
3829 : }
3830 :
3831 : static enum TIFFReadDirEntryErr
3832 0 : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
3833 : {
3834 0 : if ((value < -0x8000) || (value > 0x7FFF))
3835 0 : return (TIFFReadDirEntryErrRange);
3836 : else
3837 0 : return (TIFFReadDirEntryErrOk);
3838 : }
3839 :
3840 : static enum TIFFReadDirEntryErr
3841 0 : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
3842 : {
3843 0 : if (value > 0x7FFF)
3844 0 : return (TIFFReadDirEntryErrRange);
3845 : else
3846 0 : return (TIFFReadDirEntryErrOk);
3847 : }
3848 :
3849 : static enum TIFFReadDirEntryErr
3850 0 : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
3851 : {
3852 0 : if ((value < -0x8000) || (value > 0x7FFF))
3853 0 : return (TIFFReadDirEntryErrRange);
3854 : else
3855 0 : return (TIFFReadDirEntryErrOk);
3856 : }
3857 :
3858 : static enum TIFFReadDirEntryErr
3859 0 : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
3860 : {
3861 0 : if (value < 0)
3862 0 : return (TIFFReadDirEntryErrRange);
3863 : else
3864 0 : return (TIFFReadDirEntryErrOk);
3865 : }
3866 :
3867 : static enum TIFFReadDirEntryErr
3868 0 : TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
3869 : {
3870 0 : if (value < 0)
3871 0 : return (TIFFReadDirEntryErrRange);
3872 : else
3873 0 : return (TIFFReadDirEntryErrOk);
3874 : }
3875 :
3876 : static enum TIFFReadDirEntryErr
3877 0 : TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
3878 : {
3879 0 : if (value < 0)
3880 0 : return (TIFFReadDirEntryErrRange);
3881 : else
3882 0 : return (TIFFReadDirEntryErrOk);
3883 : }
3884 :
3885 : static enum TIFFReadDirEntryErr
3886 0 : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
3887 : {
3888 0 : if (value > UINT32_MAX)
3889 0 : return (TIFFReadDirEntryErrRange);
3890 : else
3891 0 : return (TIFFReadDirEntryErrOk);
3892 : }
3893 :
3894 : static enum TIFFReadDirEntryErr
3895 0 : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
3896 : {
3897 0 : if ((value < 0) || (value > (int64_t)UINT32_MAX))
3898 0 : return (TIFFReadDirEntryErrRange);
3899 : else
3900 0 : return (TIFFReadDirEntryErrOk);
3901 : }
3902 :
3903 : static enum TIFFReadDirEntryErr
3904 0 : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
3905 : {
3906 0 : if (value > 0x7FFFFFFFUL)
3907 0 : return (TIFFReadDirEntryErrRange);
3908 : else
3909 0 : return (TIFFReadDirEntryErrOk);
3910 : }
3911 :
3912 : /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3913 : static enum TIFFReadDirEntryErr
3914 0 : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
3915 : {
3916 0 : if (value > 0x7FFFFFFF)
3917 0 : return (TIFFReadDirEntryErrRange);
3918 : else
3919 0 : return (TIFFReadDirEntryErrOk);
3920 : }
3921 :
3922 : /* Check that the 8-byte signed value can fit in a 4-byte signed range */
3923 : static enum TIFFReadDirEntryErr
3924 0 : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
3925 : {
3926 0 : if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
3927 0 : return (TIFFReadDirEntryErrRange);
3928 : else
3929 0 : return (TIFFReadDirEntryErrOk);
3930 : }
3931 :
3932 : static enum TIFFReadDirEntryErr
3933 0 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
3934 : {
3935 0 : if (value < 0)
3936 0 : return (TIFFReadDirEntryErrRange);
3937 : else
3938 0 : return (TIFFReadDirEntryErrOk);
3939 : }
3940 :
3941 : static enum TIFFReadDirEntryErr
3942 0 : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
3943 : {
3944 0 : if (value < 0)
3945 0 : return (TIFFReadDirEntryErrRange);
3946 : else
3947 0 : return (TIFFReadDirEntryErrOk);
3948 : }
3949 :
3950 : static enum TIFFReadDirEntryErr
3951 0 : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
3952 : {
3953 0 : if (value < 0)
3954 0 : return (TIFFReadDirEntryErrRange);
3955 : else
3956 0 : return (TIFFReadDirEntryErrOk);
3957 : }
3958 :
3959 : static enum TIFFReadDirEntryErr
3960 1 : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
3961 : {
3962 1 : if (value < 0)
3963 0 : return (TIFFReadDirEntryErrRange);
3964 : else
3965 1 : return (TIFFReadDirEntryErrOk);
3966 : }
3967 :
3968 : static enum TIFFReadDirEntryErr
3969 0 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
3970 : {
3971 0 : if (value > INT64_MAX)
3972 0 : return (TIFFReadDirEntryErrRange);
3973 : else
3974 0 : return (TIFFReadDirEntryErrOk);
3975 : }
3976 :
3977 280 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
3978 : tmsize_t size, void *dest)
3979 : {
3980 280 : assert(size > 0);
3981 280 : if (!isMapped(tif))
3982 : {
3983 248 : if (!SeekOK(tif, offset))
3984 0 : return (TIFFReadDirEntryErrIo);
3985 248 : if (!ReadOK(tif, dest, size))
3986 0 : return (TIFFReadDirEntryErrIo);
3987 : }
3988 : else
3989 : {
3990 : size_t ma, mb;
3991 32 : ma = (size_t)offset;
3992 32 : if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
3993 : {
3994 0 : return TIFFReadDirEntryErrIo;
3995 : }
3996 32 : mb = (uint64_t)ma + (uint64_t)size;
3997 32 : if (mb > (uint64_t)tif->tif_size)
3998 0 : return (TIFFReadDirEntryErrIo);
3999 32 : _TIFFmemcpy(dest, tif->tif_base + ma, size);
4000 : }
4001 280 : return (TIFFReadDirEntryErrOk);
4002 : }
4003 :
4004 43 : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
4005 : const char *module, const char *tagname,
4006 : int recover)
4007 : {
4008 43 : if (!recover)
4009 : {
4010 2 : switch (err)
4011 : {
4012 0 : case TIFFReadDirEntryErrCount:
4013 0 : TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
4014 : tagname);
4015 0 : break;
4016 0 : case TIFFReadDirEntryErrType:
4017 0 : TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
4018 : tagname);
4019 0 : break;
4020 2 : case TIFFReadDirEntryErrIo:
4021 2 : TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
4022 : tagname);
4023 2 : break;
4024 0 : case TIFFReadDirEntryErrRange:
4025 0 : TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
4026 : tagname);
4027 0 : break;
4028 0 : case TIFFReadDirEntryErrPsdif:
4029 0 : TIFFErrorExtR(
4030 : tif, module,
4031 : "Cannot handle different values per sample for \"%s\"",
4032 : tagname);
4033 0 : break;
4034 0 : case TIFFReadDirEntryErrSizesan:
4035 0 : TIFFErrorExtR(tif, module,
4036 : "Sanity check on size of \"%s\" value failed",
4037 : tagname);
4038 0 : break;
4039 0 : case TIFFReadDirEntryErrAlloc:
4040 0 : TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
4041 : tagname);
4042 0 : break;
4043 0 : case TIFFReadDirEntryErrOk:
4044 : default:
4045 0 : assert(0); /* we should never get here */
4046 : break;
4047 : }
4048 : }
4049 : else
4050 : {
4051 41 : switch (err)
4052 : {
4053 4 : case TIFFReadDirEntryErrCount:
4054 4 : TIFFWarningExtR(tif, module,
4055 : "Incorrect count for \"%s\"; tag ignored",
4056 : tagname);
4057 4 : break;
4058 0 : case TIFFReadDirEntryErrType:
4059 0 : TIFFWarningExtR(tif, module,
4060 : "Incompatible type for \"%s\"; tag ignored",
4061 : tagname);
4062 0 : break;
4063 19 : case TIFFReadDirEntryErrIo:
4064 19 : TIFFWarningExtR(
4065 : tif, module,
4066 : "IO error during reading of \"%s\"; tag ignored", tagname);
4067 19 : break;
4068 0 : case TIFFReadDirEntryErrRange:
4069 0 : TIFFWarningExtR(tif, module,
4070 : "Incorrect value for \"%s\"; tag ignored",
4071 : tagname);
4072 0 : break;
4073 0 : case TIFFReadDirEntryErrPsdif:
4074 0 : TIFFWarningExtR(tif, module,
4075 : "Cannot handle different values per sample for "
4076 : "\"%s\"; tag ignored",
4077 : tagname);
4078 0 : break;
4079 3 : case TIFFReadDirEntryErrSizesan:
4080 3 : TIFFWarningExtR(
4081 : tif, module,
4082 : "Sanity check on size of \"%s\" value failed; tag ignored",
4083 : tagname);
4084 3 : break;
4085 15 : case TIFFReadDirEntryErrAlloc:
4086 15 : TIFFWarningExtR(tif, module,
4087 : "Out of memory reading of \"%s\"; tag ignored",
4088 : tagname);
4089 15 : break;
4090 0 : case TIFFReadDirEntryErrOk:
4091 : default:
4092 0 : assert(0); /* we should never get here */
4093 : break;
4094 : }
4095 : }
4096 43 : }
4097 :
4098 : /*
4099 : * Return the maximum number of color channels specified for a given photometric
4100 : * type. 0 is returned if photometric type isn't supported or no default value
4101 : * is defined by the specification.
4102 : */
4103 54141 : static int _TIFFGetMaxColorChannels(uint16_t photometric)
4104 : {
4105 54141 : switch (photometric)
4106 : {
4107 39073 : case PHOTOMETRIC_PALETTE:
4108 : case PHOTOMETRIC_MINISWHITE:
4109 : case PHOTOMETRIC_MINISBLACK:
4110 39073 : return 1;
4111 14303 : case PHOTOMETRIC_YCBCR:
4112 : case PHOTOMETRIC_RGB:
4113 : case PHOTOMETRIC_CIELAB:
4114 : case PHOTOMETRIC_LOGLUV:
4115 : case PHOTOMETRIC_ITULAB:
4116 : case PHOTOMETRIC_ICCLAB:
4117 14303 : return 3;
4118 750 : case PHOTOMETRIC_SEPARATED:
4119 : case PHOTOMETRIC_MASK:
4120 750 : return 4;
4121 15 : case PHOTOMETRIC_LOGL:
4122 : case PHOTOMETRIC_CFA:
4123 : default:
4124 15 : return 0;
4125 : }
4126 : }
4127 :
4128 33748 : static int ByteCountLooksBad(TIFF *tif)
4129 : {
4130 : /*
4131 : * Assume we have wrong StripByteCount value (in case
4132 : * of single strip) in following cases:
4133 : * - it is equal to zero along with StripOffset;
4134 : * - it is larger than file itself (in case of uncompressed
4135 : * image);
4136 : * - it is smaller than the size of the bytes per row
4137 : * multiplied on the number of rows. The last case should
4138 : * not be checked in the case of writing new image,
4139 : * because we may do not know the exact strip size
4140 : * until the whole image will be written and directory
4141 : * dumped out.
4142 : */
4143 33748 : uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
4144 33743 : uint64_t offset = TIFFGetStrileOffset(tif, 0);
4145 : uint64_t filesize;
4146 :
4147 33740 : if (offset == 0)
4148 10952 : return 0;
4149 22788 : if (bytecount == 0)
4150 1 : return 1;
4151 22787 : if (tif->tif_dir.td_compression != COMPRESSION_NONE)
4152 763 : return 0;
4153 22024 : filesize = TIFFGetFileSize(tif);
4154 22037 : if (offset <= filesize && bytecount > filesize - offset)
4155 2 : return 1;
4156 22035 : if (tif->tif_mode == O_RDONLY)
4157 : {
4158 20902 : uint64_t scanlinesize = TIFFScanlineSize64(tif);
4159 20899 : if (tif->tif_dir.td_imagelength > 0 &&
4160 20900 : scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
4161 : {
4162 0 : return 1;
4163 : }
4164 20899 : if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
4165 2 : return 1;
4166 : }
4167 22030 : return 0;
4168 : }
4169 :
4170 : /*
4171 : * To evaluate the IFD data size when reading, save the offset and data size of
4172 : * all data that does not fit into the IFD entries themselves.
4173 : */
4174 363170 : static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp)
4175 : {
4176 363193 : const uint64_t data_width =
4177 363170 : (uint64_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
4178 363193 : if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
4179 : {
4180 1 : TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4181 : "Too large IFD data size");
4182 1 : return false;
4183 : }
4184 363192 : const uint64_t datalength = dp->tdir_count * data_width;
4185 363192 : if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
4186 : {
4187 198277 : if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
4188 : {
4189 0 : TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4190 : "Too large IFD data size");
4191 0 : return false;
4192 : }
4193 198277 : tif->tif_dir.td_dirdatasize_read += datalength;
4194 198277 : if (!(tif->tif_flags & TIFF_BIGTIFF))
4195 : {
4196 : /* The offset of TIFFDirEntry are not swapped when read in. That has
4197 : * to be done when used. */
4198 196858 : uint32_t offset = dp->tdir_offset.toff_long;
4199 196858 : if (tif->tif_flags & TIFF_SWAB)
4200 2100 : TIFFSwabLong(&offset);
4201 : tif->tif_dir
4202 196826 : .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4203 196826 : .offset = (uint64_t)offset;
4204 : }
4205 : else
4206 : {
4207 : tif->tif_dir
4208 1419 : .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4209 1419 : .offset = dp->tdir_offset.toff_long8;
4210 1419 : if (tif->tif_flags & TIFF_SWAB)
4211 30 : TIFFSwabLong8(
4212 : &tif->tif_dir
4213 30 : .td_dirdatasize_offsets[tif->tif_dir
4214 30 : .td_dirdatasize_Noffsets]
4215 : .offset);
4216 : }
4217 : tif->tif_dir
4218 198291 : .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4219 198291 : .length = datalength;
4220 198291 : tif->tif_dir.td_dirdatasize_Noffsets++;
4221 : }
4222 363206 : return true;
4223 : }
4224 :
4225 : /*
4226 : * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
4227 : */
4228 73554 : static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
4229 : {
4230 73554 : const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a;
4231 73554 : const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b;
4232 : /* Compare offsets */
4233 73554 : if (ta->offset > tb->offset)
4234 9768 : return 1;
4235 63786 : else if (ta->offset < tb->offset)
4236 63786 : return -1;
4237 : else
4238 0 : return 0;
4239 : }
4240 :
4241 : /*
4242 : * Determine the IFD data size after reading an IFD from the file that can be
4243 : * overwritten and saving it in tif_dir.td_dirdatasize_read. This data size
4244 : * includes the IFD entries themselves as well as the data that does not fit
4245 : * directly into the IFD entries but is located directly after the IFD entries
4246 : * in the file.
4247 : */
4248 54134 : static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount)
4249 : {
4250 : /* IFD data size is only needed if file-writing is enabled.
4251 : * This also avoids the seek() to EOF to determine the file size, which
4252 : * causes the stdin-streaming-friendly mode of libtiff for GDAL to fail. */
4253 54134 : if (tif->tif_mode == O_RDONLY)
4254 35353 : return;
4255 :
4256 : /* Sort TIFFEntryOffsetAndLength array in ascending order. */
4257 18781 : qsort(tif->tif_dir.td_dirdatasize_offsets,
4258 18781 : tif->tif_dir.td_dirdatasize_Noffsets,
4259 : sizeof(TIFFEntryOffsetAndLength), cmpTIFFEntryOffsetAndLength);
4260 :
4261 : /* Get offset of end of IFD entry space. */
4262 : uint64_t IFDendoffset;
4263 18775 : if (!(tif->tif_flags & TIFF_BIGTIFF))
4264 18314 : IFDendoffset = tif->tif_diroff + 2 + dircount * 12 + 4;
4265 : else
4266 461 : IFDendoffset = tif->tif_diroff + 8 + dircount * 20 + 8;
4267 :
4268 : /* Check which offsets are right behind IFD entries. However, LibTIFF
4269 : * increments the writing address for every external data to an even offset.
4270 : * Thus gaps of 1 byte can occur. */
4271 18775 : uint64_t size = 0;
4272 : uint64_t offset;
4273 : uint32_t i;
4274 81105 : for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
4275 : {
4276 62366 : offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
4277 62366 : if (offset == IFDendoffset)
4278 : {
4279 59909 : size += tif->tif_dir.td_dirdatasize_offsets[i].length;
4280 59909 : IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
4281 : }
4282 2457 : else if (offset == IFDendoffset + 1)
4283 : {
4284 : /* Add gap byte after previous IFD data set. */
4285 2421 : size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
4286 2421 : IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
4287 : }
4288 : else
4289 : {
4290 : /* Further data is no more continuously after IFD */
4291 36 : break;
4292 : }
4293 : }
4294 : /* Check for gap byte of some easy cases. This should cover 90% of cases.
4295 : * Otherwise, IFD will be re-written even it might be safely overwritten. */
4296 18775 : if (tif->tif_nextdiroff != 0)
4297 : {
4298 2778 : if (tif->tif_nextdiroff == IFDendoffset + 1)
4299 759 : size++;
4300 : }
4301 : else
4302 : {
4303 : /* Check for IFD data ends at EOF. Then IFD can always be safely
4304 : * overwritten. */
4305 15997 : offset = TIFFSeekFile(tif, 0, SEEK_END);
4306 15997 : if (offset == IFDendoffset)
4307 : {
4308 14535 : tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
4309 14535 : return;
4310 : }
4311 : }
4312 :
4313 : /* Finally, add the size of the IFD tag entries themselves. */
4314 4240 : if (!(tif->tif_flags & TIFF_BIGTIFF))
4315 4045 : tif->tif_dir.td_dirdatasize_read = 2U + dircount * 12U + 4U + size;
4316 : else
4317 195 : tif->tif_dir.td_dirdatasize_read = 8U + dircount * 20U + 8U + size;
4318 : } /*-- CalcFinalIFDdatasizeReading() --*/
4319 :
4320 : /*
4321 : * Read the next TIFF directory from a file and convert it to the internal
4322 : * format. We read directories sequentially.
4323 : */
4324 62667 : int TIFFReadDirectory(TIFF *tif)
4325 : {
4326 : static const char module[] = "TIFFReadDirectory";
4327 : TIFFDirEntry *dir;
4328 : uint16_t dircount;
4329 : TIFFDirEntry *dp;
4330 : uint16_t di;
4331 : const TIFFField *fip;
4332 62667 : uint32_t fii = FAILED_FII;
4333 : toff_t nextdiroff;
4334 62667 : int bitspersample_read = FALSE;
4335 : int color_channels;
4336 :
4337 62667 : if (tif->tif_nextdiroff == 0)
4338 : {
4339 : /* In this special case, tif_diroff needs also to be set to 0.
4340 : * This is behind the last IFD, thus no checking or reading necessary.
4341 : */
4342 8504 : tif->tif_diroff = tif->tif_nextdiroff;
4343 8504 : return 0;
4344 : }
4345 :
4346 54163 : nextdiroff = tif->tif_nextdiroff;
4347 : /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
4348 : * reading of the directory. Otherwise, invalid IFD offsets could corrupt
4349 : * the IFD list. */
4350 54163 : if (!_TIFFCheckDirNumberAndOffset(tif,
4351 54163 : tif->tif_curdir ==
4352 : TIFF_NON_EXISTENT_DIR_NUMBER
4353 : ? 0
4354 3700 : : tif->tif_curdir + 1,
4355 : nextdiroff))
4356 : {
4357 0 : return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
4358 : IFDs) */
4359 : }
4360 54146 : dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
4361 54141 : if (!dircount)
4362 : {
4363 13 : TIFFErrorExtR(tif, module,
4364 : "Failed to read directory at offset %" PRIu64,
4365 : nextdiroff);
4366 17 : return 0;
4367 : }
4368 : /* Set global values after a valid directory has been fetched.
4369 : * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
4370 : * beginning. */
4371 54128 : if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
4372 50427 : tif->tif_curdir = 0;
4373 : else
4374 3701 : tif->tif_curdir++;
4375 :
4376 54128 : TIFFReadDirectoryCheckOrder(tif, dir, dircount);
4377 :
4378 : /*
4379 : * Mark duplicates of any tag to be ignored (bugzilla 1994)
4380 : * to avoid certain pathological problems.
4381 : */
4382 : {
4383 : TIFFDirEntry *ma;
4384 : uint16_t mb;
4385 819241 : for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
4386 : {
4387 : TIFFDirEntry *na;
4388 : uint16_t nb;
4389 5952390 : for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
4390 : {
4391 5187280 : if (ma->tdir_tag == na->tdir_tag)
4392 : {
4393 12237 : na->tdir_ignore = TRUE;
4394 : }
4395 : }
4396 : }
4397 : }
4398 :
4399 54131 : tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
4400 54131 : tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
4401 54131 : tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
4402 :
4403 : /* When changing directory, in deferred strile loading mode, we must also
4404 : * unset the TIFF_LAZYSTRILELOAD_DONE bit if it was initially set,
4405 : * to make sure the strile offset/bytecount are read again (when they fit
4406 : * in the tag data area).
4407 : */
4408 54131 : tif->tif_flags &= ~TIFF_LAZYSTRILELOAD_DONE;
4409 :
4410 : /* Free any old stuff and reinit i/o and other parameters within
4411 : * TIFFDefaultDirectory() since we are starting on a new directory. */
4412 54131 : TIFFFreeDirectory(tif);
4413 54121 : TIFFDefaultDirectory(tif);
4414 :
4415 : /* After setup a fresh directory indicate that now active IFD is also
4416 : * present on file, even if its entries could not be read successfully
4417 : * below. */
4418 54138 : tif->tif_dir.td_iswrittentofile = TRUE;
4419 :
4420 : /* Allocate arrays for offset values outside IFD entry for IFD data size
4421 : * checking. Note: Counter are reset within TIFFFreeDirectory(). */
4422 54143 : tif->tif_dir.td_dirdatasize_offsets =
4423 54138 : (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
4424 54138 : tif, dircount * sizeof(TIFFEntryOffsetAndLength));
4425 54143 : if (tif->tif_dir.td_dirdatasize_offsets == NULL)
4426 : {
4427 0 : TIFFErrorExtR(
4428 : tif, module,
4429 : "Failed to allocate memory for counting IFD data size at reading");
4430 0 : goto bad;
4431 : }
4432 : /*
4433 : * Electronic Arts writes gray-scale TIFF files
4434 : * without a PlanarConfiguration directory entry.
4435 : * Thus we setup a default value here, even though
4436 : * the TIFF spec says there is no default value.
4437 : * After PlanarConfiguration is preset in TIFFDefaultDirectory()
4438 : * the following setting is not needed, but does not harm either.
4439 : */
4440 54143 : TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
4441 : /*
4442 : * Setup default value and then make a pass over
4443 : * the fields to check type and tag information,
4444 : * and to extract info required to size data
4445 : * structures. A second pass is made afterwards
4446 : * to read in everything not taken in the first pass.
4447 : * But we must process the Compression tag first
4448 : * in order to merge in codec-private tag definitions (otherwise
4449 : * we may get complaints about unknown tags). However, the
4450 : * Compression tag may be dependent on the SamplesPerPixel
4451 : * tag value because older TIFF specs permitted Compression
4452 : * to be written as a SamplesPerPixel-count tag entry.
4453 : * Thus if we don't first figure out the correct SamplesPerPixel
4454 : * tag value then we may end up ignoring the Compression tag
4455 : * value because it has an incorrect count value (if the
4456 : * true value of SamplesPerPixel is not 1).
4457 : */
4458 : dp =
4459 54136 : TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
4460 54130 : if (dp)
4461 : {
4462 54104 : if (!TIFFFetchNormalTag(tif, dp, 0))
4463 0 : goto bad;
4464 54104 : dp->tdir_ignore = TRUE;
4465 : }
4466 54130 : dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
4467 54133 : if (dp)
4468 : {
4469 : /*
4470 : * The 5.0 spec says the Compression tag has one value, while
4471 : * earlier specs say it has one value per sample. Because of
4472 : * this, we accept the tag if one value is supplied with either
4473 : * count.
4474 : */
4475 : uint16_t value;
4476 : enum TIFFReadDirEntryErr err;
4477 54120 : err = TIFFReadDirEntryShort(tif, dp, &value);
4478 54122 : if (err == TIFFReadDirEntryErrCount)
4479 0 : err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
4480 54107 : if (err != TIFFReadDirEntryErrOk)
4481 : {
4482 0 : TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
4483 0 : goto bad;
4484 : }
4485 54107 : if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
4486 0 : goto bad;
4487 54120 : dp->tdir_ignore = TRUE;
4488 : }
4489 : else
4490 : {
4491 13 : if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
4492 0 : goto bad;
4493 : }
4494 : /*
4495 : * First real pass over the directory.
4496 : */
4497 819439 : for (di = 0, dp = dir; di < dircount; di++, dp++)
4498 : {
4499 765221 : if (!dp->tdir_ignore)
4500 : {
4501 656400 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4502 656466 : if (fii == FAILED_FII)
4503 : {
4504 437 : if (tif->tif_warn_about_unknown_tags)
4505 : {
4506 0 : TIFFWarningExtR(tif, module,
4507 : "Unknown field with tag %" PRIu16
4508 : " (0x%" PRIx16 ") encountered",
4509 0 : dp->tdir_tag, dp->tdir_tag);
4510 : }
4511 : /* the following knowingly leaks the
4512 : anonymous field structure */
4513 437 : const TIFFField *fld = _TIFFCreateAnonField(
4514 437 : tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
4515 437 : if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
4516 : {
4517 0 : TIFFWarningExtR(
4518 : tif, module,
4519 : "Registering anonymous field with tag %" PRIu16
4520 : " (0x%" PRIx16 ") failed",
4521 0 : dp->tdir_tag, dp->tdir_tag);
4522 0 : dp->tdir_ignore = TRUE;
4523 : }
4524 : else
4525 : {
4526 437 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4527 437 : assert(fii != FAILED_FII);
4528 : }
4529 : }
4530 : }
4531 765310 : if (!dp->tdir_ignore)
4532 : {
4533 656452 : fip = tif->tif_fields[fii];
4534 656452 : if (fip->field_bit == FIELD_IGNORE)
4535 0 : dp->tdir_ignore = TRUE;
4536 : else
4537 : {
4538 656452 : switch (dp->tdir_tag)
4539 : {
4540 108269 : case TIFFTAG_STRIPOFFSETS:
4541 : case TIFFTAG_STRIPBYTECOUNTS:
4542 : case TIFFTAG_TILEOFFSETS:
4543 : case TIFFTAG_TILEBYTECOUNTS:
4544 108269 : TIFFSetFieldBit(tif, fip->field_bit);
4545 108269 : break;
4546 231448 : case TIFFTAG_IMAGEWIDTH:
4547 : case TIFFTAG_IMAGELENGTH:
4548 : case TIFFTAG_IMAGEDEPTH:
4549 : case TIFFTAG_TILELENGTH:
4550 : case TIFFTAG_TILEWIDTH:
4551 : case TIFFTAG_TILEDEPTH:
4552 : case TIFFTAG_PLANARCONFIG:
4553 : case TIFFTAG_ROWSPERSTRIP:
4554 : case TIFFTAG_EXTRASAMPLES:
4555 231448 : if (!TIFFFetchNormalTag(tif, dp, 0))
4556 0 : goto bad;
4557 231434 : dp->tdir_ignore = TRUE;
4558 231434 : break;
4559 316735 : default:
4560 316735 : if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
4561 9 : dp->tdir_ignore = TRUE;
4562 316747 : break;
4563 : }
4564 108858 : }
4565 : }
4566 : }
4567 : /*
4568 : * XXX: OJPEG hack.
4569 : * If a) compression is OJPEG, b) planarconfig tag says it's separate,
4570 : * c) strip offsets/bytecounts tag are both present and
4571 : * d) both contain exactly one value, then we consistently find
4572 : * that the buggy implementation of the buggy compression scheme
4573 : * matches contig planarconfig best. So we 'fix-up' the tag here
4574 : */
4575 54218 : if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4576 2 : (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
4577 : {
4578 0 : if (!_TIFFFillStriles(tif))
4579 0 : goto bad;
4580 0 : dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4581 : TIFFTAG_STRIPOFFSETS);
4582 0 : if ((dp != 0) && (dp->tdir_count == 1))
4583 : {
4584 0 : dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4585 : TIFFTAG_STRIPBYTECOUNTS);
4586 0 : if ((dp != 0) && (dp->tdir_count == 1))
4587 : {
4588 0 : tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
4589 0 : TIFFWarningExtR(tif, module,
4590 : "Planarconfig tag value assumed incorrect, "
4591 : "assuming data is contig instead of chunky");
4592 : }
4593 : }
4594 : }
4595 : /*
4596 : * Allocate directory structure and setup defaults.
4597 : */
4598 54132 : if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
4599 : {
4600 0 : MissingRequired(tif, "ImageLength");
4601 0 : goto bad;
4602 : }
4603 :
4604 : /*
4605 : * Second pass: extract other information.
4606 : */
4607 819338 : for (di = 0, dp = dir; di < dircount; di++, dp++)
4608 : {
4609 765149 : if (!dp->tdir_ignore)
4610 : {
4611 424914 : switch (dp->tdir_tag)
4612 : {
4613 107482 : case TIFFTAG_MINSAMPLEVALUE:
4614 : case TIFFTAG_MAXSAMPLEVALUE:
4615 : case TIFFTAG_BITSPERSAMPLE:
4616 : case TIFFTAG_DATATYPE:
4617 : case TIFFTAG_SAMPLEFORMAT:
4618 : /*
4619 : * The MinSampleValue, MaxSampleValue, BitsPerSample
4620 : * DataType and SampleFormat tags are supposed to be
4621 : * written as one value/sample, but some vendors
4622 : * incorrectly write one value only -- so we accept
4623 : * that as well (yuck). Other vendors write correct
4624 : * value for NumberOfSamples, but incorrect one for
4625 : * BitsPerSample and friends, and we will read this
4626 : * too.
4627 : */
4628 : {
4629 : uint16_t value;
4630 : enum TIFFReadDirEntryErr err;
4631 107482 : err = TIFFReadDirEntryShort(tif, dp, &value);
4632 107491 : if (!EvaluateIFDdatasizeReading(tif, dp))
4633 0 : goto bad;
4634 107472 : if (err == TIFFReadDirEntryErrCount)
4635 : err =
4636 32613 : TIFFReadDirEntryPersampleShort(tif, dp, &value);
4637 107478 : if (err != TIFFReadDirEntryErrOk)
4638 : {
4639 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4640 0 : TIFFReadDirEntryOutputErr(
4641 : tif, err, module,
4642 : fip ? fip->field_name : "unknown tagname", 0);
4643 0 : goto bad;
4644 : }
4645 107478 : if (!TIFFSetField(tif, dp->tdir_tag, value))
4646 0 : goto bad;
4647 107484 : if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
4648 54115 : bitspersample_read = TRUE;
4649 : }
4650 107484 : break;
4651 0 : case TIFFTAG_SMINSAMPLEVALUE:
4652 : case TIFFTAG_SMAXSAMPLEVALUE:
4653 : {
4654 :
4655 0 : double *data = NULL;
4656 : enum TIFFReadDirEntryErr err;
4657 : uint32_t saved_flags;
4658 : int m;
4659 0 : if (dp->tdir_count !=
4660 0 : (uint64_t)tif->tif_dir.td_samplesperpixel)
4661 0 : err = TIFFReadDirEntryErrCount;
4662 : else
4663 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
4664 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
4665 0 : goto bad;
4666 0 : if (err != TIFFReadDirEntryErrOk)
4667 : {
4668 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4669 0 : TIFFReadDirEntryOutputErr(
4670 : tif, err, module,
4671 : fip ? fip->field_name : "unknown tagname", 0);
4672 0 : goto bad;
4673 : }
4674 0 : saved_flags = tif->tif_flags;
4675 0 : tif->tif_flags |= TIFF_PERSAMPLE;
4676 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
4677 0 : tif->tif_flags = saved_flags;
4678 0 : _TIFFfreeExt(tif, data);
4679 0 : if (!m)
4680 0 : goto bad;
4681 : }
4682 0 : break;
4683 54133 : case TIFFTAG_STRIPOFFSETS:
4684 : case TIFFTAG_TILEOFFSETS:
4685 : {
4686 54133 : switch (dp->tdir_type)
4687 : {
4688 52750 : case TIFF_SHORT:
4689 : case TIFF_LONG:
4690 : case TIFF_LONG8:
4691 52750 : break;
4692 1383 : default:
4693 : /* Warn except if directory typically created with
4694 : * TIFFDeferStrileArrayWriting() */
4695 1383 : if (!(tif->tif_mode == O_RDWR &&
4696 1371 : dp->tdir_count == 0 && dp->tdir_type == 0 &&
4697 1371 : dp->tdir_offset.toff_long8 == 0))
4698 : {
4699 12 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4700 11 : TIFFWarningExtR(
4701 : tif, module, "Invalid data type for tag %s",
4702 : fip ? fip->field_name : "unknown tagname");
4703 : }
4704 1382 : break;
4705 : }
4706 54132 : _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
4707 : sizeof(TIFFDirEntry));
4708 54136 : if (!EvaluateIFDdatasizeReading(tif, dp))
4709 1 : goto bad;
4710 : }
4711 54124 : break;
4712 54112 : case TIFFTAG_STRIPBYTECOUNTS:
4713 : case TIFFTAG_TILEBYTECOUNTS:
4714 : {
4715 54112 : switch (dp->tdir_type)
4716 : {
4717 52734 : case TIFF_SHORT:
4718 : case TIFF_LONG:
4719 : case TIFF_LONG8:
4720 52734 : break;
4721 1378 : default:
4722 : /* Warn except if directory typically created with
4723 : * TIFFDeferStrileArrayWriting() */
4724 1378 : if (!(tif->tif_mode == O_RDWR &&
4725 1371 : dp->tdir_count == 0 && dp->tdir_type == 0 &&
4726 1371 : dp->tdir_offset.toff_long8 == 0))
4727 : {
4728 7 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4729 9 : TIFFWarningExtR(
4730 : tif, module, "Invalid data type for tag %s",
4731 : fip ? fip->field_name : "unknown tagname");
4732 : }
4733 1380 : break;
4734 : }
4735 54114 : _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
4736 : sizeof(TIFFDirEntry));
4737 54129 : if (!EvaluateIFDdatasizeReading(tif, dp))
4738 0 : goto bad;
4739 : }
4740 54116 : break;
4741 376 : case TIFFTAG_COLORMAP:
4742 : case TIFFTAG_TRANSFERFUNCTION:
4743 : {
4744 : enum TIFFReadDirEntryErr err;
4745 : uint32_t countpersample;
4746 : uint32_t countrequired;
4747 : uint32_t incrementpersample;
4748 376 : uint16_t *value = NULL;
4749 : /* It would be dangerous to instantiate those tag values */
4750 : /* since if td_bitspersample has not yet been read (due to
4751 : */
4752 : /* unordered tags), it could be read afterwards with a */
4753 : /* values greater than the default one (1), which may cause
4754 : */
4755 : /* crashes in user code */
4756 376 : if (!bitspersample_read)
4757 : {
4758 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4759 0 : TIFFWarningExtR(
4760 : tif, module,
4761 : "Ignoring %s since BitsPerSample tag not found",
4762 : fip ? fip->field_name : "unknown tagname");
4763 0 : continue;
4764 : }
4765 : /* ColorMap or TransferFunction for high bit */
4766 : /* depths do not make much sense and could be */
4767 : /* used as a denial of service vector */
4768 376 : if (tif->tif_dir.td_bitspersample > 24)
4769 : {
4770 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4771 0 : TIFFWarningExtR(
4772 : tif, module,
4773 : "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
4774 : fip ? fip->field_name : "unknown tagname",
4775 0 : tif->tif_dir.td_bitspersample);
4776 0 : continue;
4777 : }
4778 376 : countpersample = (1U << tif->tif_dir.td_bitspersample);
4779 376 : if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
4780 15 : (dp->tdir_count == (uint64_t)countpersample))
4781 : {
4782 0 : countrequired = countpersample;
4783 0 : incrementpersample = 0;
4784 : }
4785 : else
4786 : {
4787 376 : countrequired = 3 * countpersample;
4788 376 : incrementpersample = countpersample;
4789 : }
4790 376 : if (dp->tdir_count != (uint64_t)countrequired)
4791 0 : err = TIFFReadDirEntryErrCount;
4792 : else
4793 376 : err = TIFFReadDirEntryShortArray(tif, dp, &value);
4794 376 : if (!EvaluateIFDdatasizeReading(tif, dp))
4795 0 : goto bad;
4796 376 : if (err != TIFFReadDirEntryErrOk)
4797 : {
4798 1 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4799 1 : TIFFReadDirEntryOutputErr(
4800 : tif, err, module,
4801 : fip ? fip->field_name : "unknown tagname", 1);
4802 : }
4803 : else
4804 : {
4805 375 : TIFFSetField(tif, dp->tdir_tag, value,
4806 375 : value + incrementpersample,
4807 375 : value + 2 * incrementpersample);
4808 375 : _TIFFfreeExt(tif, value);
4809 : }
4810 : }
4811 376 : break;
4812 : /* BEGIN REV 4.0 COMPATIBILITY */
4813 0 : case TIFFTAG_OSUBFILETYPE:
4814 : {
4815 : uint16_t valueo;
4816 : uint32_t value;
4817 0 : if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
4818 : TIFFReadDirEntryErrOk)
4819 : {
4820 0 : switch (valueo)
4821 : {
4822 0 : case OFILETYPE_REDUCEDIMAGE:
4823 0 : value = FILETYPE_REDUCEDIMAGE;
4824 0 : break;
4825 0 : case OFILETYPE_PAGE:
4826 0 : value = FILETYPE_PAGE;
4827 0 : break;
4828 0 : default:
4829 0 : value = 0;
4830 0 : break;
4831 : }
4832 0 : if (value != 0)
4833 0 : TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
4834 : }
4835 : }
4836 0 : break;
4837 : /* END REV 4.0 COMPATIBILITY */
4838 : #if 0
4839 : case TIFFTAG_EP_BATTERYLEVEL:
4840 : /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
4841 : * LibTiff defines it as ASCII and converts RATIONAL to an
4842 : * ASCII string. */
4843 : switch (dp->tdir_type)
4844 : {
4845 : case TIFF_RATIONAL:
4846 : {
4847 : /* Read rational and convert to ASCII*/
4848 : enum TIFFReadDirEntryErr err;
4849 : TIFFRational_t rValue;
4850 : err = TIFFReadDirEntryCheckedRationalDirect(
4851 : tif, dp, &rValue);
4852 : if (err != TIFFReadDirEntryErrOk)
4853 : {
4854 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4855 : TIFFReadDirEntryOutputErr(
4856 : tif, err, module,
4857 : fip ? fip->field_name : "unknown tagname",
4858 : 1);
4859 : }
4860 : else
4861 : {
4862 : char szAux[32];
4863 : snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
4864 : rValue.uNum, rValue.uDenom);
4865 : TIFFSetField(tif, dp->tdir_tag, szAux);
4866 : }
4867 : }
4868 : break;
4869 : case TIFF_ASCII:
4870 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
4871 : break;
4872 : default:
4873 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4874 : TIFFWarningExtR(tif, module,
4875 : "Invalid data type for tag %s. "
4876 : "ASCII or RATIONAL expected",
4877 : fip ? fip->field_name
4878 : : "unknown tagname");
4879 : break;
4880 : }
4881 : break;
4882 : #endif
4883 208811 : default:
4884 208811 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
4885 208871 : break;
4886 : } /* -- switch (dp->tdir_tag) -- */
4887 340235 : } /* -- if (!dp->tdir_ignore) */
4888 : } /* -- for-loop -- */
4889 :
4890 : /* Evaluate final IFD data size. */
4891 54189 : CalcFinalIFDdatasizeReading(tif, dircount);
4892 :
4893 : /*
4894 : * OJPEG hack:
4895 : * - If a) compression is OJPEG, and b) photometric tag is missing,
4896 : * then we consistently find that photometric should be YCbCr
4897 : * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
4898 : * then we consistently find that the buggy implementation of the
4899 : * buggy compression scheme matches photometric YCbCr instead.
4900 : * - If a) compression is OJPEG, and b) bitspersample tag is missing,
4901 : * then we consistently find bitspersample should be 8.
4902 : * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4903 : * and c) photometric is RGB or YCbCr, then we consistently find
4904 : * samplesperpixel should be 3
4905 : * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4906 : * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
4907 : * find samplesperpixel should be 3
4908 : */
4909 54135 : if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
4910 : {
4911 2 : if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
4912 : {
4913 0 : TIFFWarningExtR(
4914 : tif, module,
4915 : "Photometric tag is missing, assuming data is YCbCr");
4916 0 : if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
4917 0 : goto bad;
4918 : }
4919 2 : else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4920 : {
4921 0 : tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
4922 0 : TIFFWarningExtR(tif, module,
4923 : "Photometric tag value assumed incorrect, "
4924 : "assuming data is YCbCr instead of RGB");
4925 : }
4926 2 : if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
4927 : {
4928 0 : TIFFWarningExtR(
4929 : tif, module,
4930 : "BitsPerSample tag is missing, assuming 8 bits per sample");
4931 0 : if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
4932 0 : goto bad;
4933 : }
4934 2 : if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
4935 : {
4936 0 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4937 : {
4938 0 : TIFFWarningExtR(tif, module,
4939 : "SamplesPerPixel tag is missing, "
4940 : "assuming correct SamplesPerPixel value is 3");
4941 0 : if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4942 0 : goto bad;
4943 : }
4944 0 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
4945 : {
4946 0 : TIFFWarningExtR(tif, module,
4947 : "SamplesPerPixel tag is missing, "
4948 : "applying correct SamplesPerPixel value of 3");
4949 0 : if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4950 0 : goto bad;
4951 : }
4952 0 : else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
4953 0 : (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
4954 : {
4955 : /*
4956 : * SamplesPerPixel tag is missing, but is not required
4957 : * by spec. Assume correct SamplesPerPixel value of 1.
4958 : */
4959 3 : if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
4960 0 : goto bad;
4961 : }
4962 : }
4963 : }
4964 :
4965 : /*
4966 : * Setup appropriate structures (by strip or by tile)
4967 : * We do that only after the above OJPEG hack which alters SamplesPerPixel
4968 : * and thus influences the number of strips in the separate planarconfig.
4969 : */
4970 54132 : if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
4971 : {
4972 44832 : tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
4973 44810 : tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
4974 44810 : tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
4975 44810 : tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
4976 44810 : tif->tif_flags &= ~TIFF_ISTILED;
4977 : }
4978 : else
4979 : {
4980 9300 : tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
4981 9302 : tif->tif_flags |= TIFF_ISTILED;
4982 : }
4983 54112 : if (!tif->tif_dir.td_nstrips)
4984 : {
4985 0 : TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
4986 0 : isTiled(tif) ? "tiles" : "strips");
4987 0 : goto bad;
4988 : }
4989 54112 : tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
4990 54112 : if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
4991 9339 : tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
4992 54112 : if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
4993 : {
4994 : #ifdef OJPEG_SUPPORT
4995 0 : if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4996 0 : (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
4997 : {
4998 : /*
4999 : * XXX: OJPEG hack.
5000 : * If a) compression is OJPEG, b) it's not a tiled TIFF,
5001 : * and c) the number of strips is 1,
5002 : * then we tolerate the absence of stripoffsets tag,
5003 : * because, presumably, all required data is in the
5004 : * JpegInterchangeFormat stream.
5005 : */
5006 0 : TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
5007 : }
5008 : else
5009 : #endif
5010 : {
5011 0 : MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
5012 0 : goto bad;
5013 : }
5014 : }
5015 :
5016 54112 : if (tif->tif_mode == O_RDWR &&
5017 18775 : tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
5018 18775 : tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
5019 1371 : tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
5020 1371 : tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
5021 1371 : tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
5022 1371 : tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
5023 1371 : tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
5024 1371 : tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
5025 : {
5026 : /* Directory typically created with TIFFDeferStrileArrayWriting() */
5027 1371 : TIFFSetupStrips(tif);
5028 : }
5029 52741 : else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
5030 : {
5031 17647 : if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
5032 : {
5033 17646 : if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
5034 : tif->tif_dir.td_nstrips,
5035 : &tif->tif_dir.td_stripoffset_p))
5036 : {
5037 0 : goto bad;
5038 : }
5039 : }
5040 17648 : if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
5041 : {
5042 17646 : if (!TIFFFetchStripThing(
5043 : tif, &(tif->tif_dir.td_stripbytecount_entry),
5044 : tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
5045 : {
5046 0 : goto bad;
5047 : }
5048 : }
5049 : }
5050 :
5051 : /*
5052 : * Make sure all non-color channels are extrasamples.
5053 : * If it's not the case, define them as such.
5054 : */
5055 54115 : color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
5056 54137 : if (color_channels &&
5057 54117 : tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
5058 : color_channels)
5059 : {
5060 : uint16_t old_extrasamples;
5061 : uint16_t *new_sampleinfo;
5062 :
5063 3 : TIFFWarningExtR(
5064 : tif, module,
5065 : "Sum of Photometric type-related "
5066 : "color channels and ExtraSamples doesn't match SamplesPerPixel. "
5067 : "Defining non-color channels as ExtraSamples.");
5068 :
5069 3 : old_extrasamples = tif->tif_dir.td_extrasamples;
5070 3 : tif->tif_dir.td_extrasamples =
5071 3 : (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
5072 :
5073 : // sampleinfo should contain information relative to these new extra
5074 : // samples
5075 3 : new_sampleinfo = (uint16_t *)_TIFFcallocExt(
5076 3 : tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
5077 3 : if (!new_sampleinfo)
5078 : {
5079 0 : TIFFErrorExtR(tif, module,
5080 : "Failed to allocate memory for "
5081 : "temporary new sampleinfo array "
5082 : "(%" PRIu16 " 16 bit elements)",
5083 0 : tif->tif_dir.td_extrasamples);
5084 0 : goto bad;
5085 : }
5086 :
5087 3 : if (old_extrasamples > 0)
5088 0 : memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
5089 : old_extrasamples * sizeof(uint16_t));
5090 3 : _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
5091 3 : tif->tif_dir.td_extrasamples);
5092 3 : _TIFFfreeExt(tif, new_sampleinfo);
5093 : }
5094 :
5095 : /*
5096 : * Verify Palette image has a Colormap.
5097 : */
5098 54110 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
5099 357 : !TIFFFieldSet(tif, FIELD_COLORMAP))
5100 : {
5101 1 : if (tif->tif_dir.td_bitspersample >= 8 &&
5102 1 : tif->tif_dir.td_samplesperpixel == 3)
5103 0 : tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
5104 1 : else if (tif->tif_dir.td_bitspersample >= 8)
5105 1 : tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
5106 : else
5107 : {
5108 0 : MissingRequired(tif, "Colormap");
5109 0 : goto bad;
5110 : }
5111 : }
5112 : /*
5113 : * OJPEG hack:
5114 : * We do no further messing with strip/tile offsets/bytecounts in OJPEG
5115 : * TIFFs
5116 : */
5117 54110 : if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
5118 : {
5119 : /*
5120 : * Attempt to deal with a missing StripByteCounts tag.
5121 : */
5122 54129 : if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
5123 : {
5124 : /*
5125 : * Some manufacturers violate the spec by not giving
5126 : * the size of the strips. In this case, assume there
5127 : * is one uncompressed strip of data.
5128 : */
5129 5 : if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5130 5 : tif->tif_dir.td_nstrips > 1) ||
5131 5 : (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
5132 0 : tif->tif_dir.td_nstrips !=
5133 0 : (uint32_t)tif->tif_dir.td_samplesperpixel))
5134 : {
5135 0 : MissingRequired(tif, "StripByteCounts");
5136 0 : goto bad;
5137 : }
5138 5 : TIFFWarningExtR(
5139 : tif, module,
5140 : "TIFF directory is missing required "
5141 : "\"StripByteCounts\" field, calculating from imagelength");
5142 5 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5143 0 : goto bad;
5144 : }
5145 54124 : else if (tif->tif_dir.td_nstrips == 1 &&
5146 38333 : !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
5147 : {
5148 : /*
5149 : * XXX: Plexus (and others) sometimes give a value of
5150 : * zero for a tag when they don't know what the
5151 : * correct value is! Try and handle the simple case
5152 : * of estimating the size of a one strip image.
5153 : */
5154 5 : TIFFWarningExtR(tif, module,
5155 : "Bogus \"StripByteCounts\" field, ignoring and "
5156 : "calculating from imagelength");
5157 5 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5158 0 : goto bad;
5159 : }
5160 54119 : else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
5161 19019 : tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5162 14559 : tif->tif_dir.td_nstrips > 2 &&
5163 3681 : tif->tif_dir.td_compression == COMPRESSION_NONE &&
5164 1258 : TIFFGetStrileByteCount(tif, 0) !=
5165 1258 : TIFFGetStrileByteCount(tif, 1) &&
5166 0 : TIFFGetStrileByteCount(tif, 0) != 0 &&
5167 0 : TIFFGetStrileByteCount(tif, 1) != 0)
5168 : {
5169 : /*
5170 : * XXX: Some vendors fill StripByteCount array with
5171 : * absolutely wrong values (it can be equal to
5172 : * StripOffset array, for example). Catch this case
5173 : * here.
5174 : *
5175 : * We avoid this check if deferring strile loading
5176 : * as it would always force us to load the strip/tile
5177 : * information.
5178 : */
5179 0 : TIFFWarningExtR(tif, module,
5180 : "Wrong \"StripByteCounts\" field, ignoring and "
5181 : "calculating from imagelength");
5182 0 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5183 0 : goto bad;
5184 : }
5185 : }
5186 54110 : if (dir)
5187 : {
5188 54132 : _TIFFfreeExt(tif, dir);
5189 54132 : dir = NULL;
5190 : }
5191 54110 : if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
5192 : {
5193 54126 : if (tif->tif_dir.td_bitspersample >= 16)
5194 10903 : tif->tif_dir.td_maxsamplevalue = 0xFFFF;
5195 : else
5196 43223 : tif->tif_dir.td_maxsamplevalue =
5197 43223 : (uint16_t)((1 << tif->tif_dir.td_bitspersample) - 1);
5198 : }
5199 :
5200 : #ifdef STRIPBYTECOUNTSORTED_UNUSED
5201 : /*
5202 : * XXX: We can optimize checking for the strip bounds using the sorted
5203 : * bytecounts array. See also comments for TIFFAppendToStrip()
5204 : * function in tif_write.c.
5205 : */
5206 : if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1)
5207 : {
5208 : uint32_t strip;
5209 :
5210 : tif->tif_dir.td_stripbytecountsorted = 1;
5211 : for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
5212 : {
5213 : if (TIFFGetStrileOffset(tif, strip - 1) >
5214 : TIFFGetStrileOffset(tif, strip))
5215 : {
5216 : tif->tif_dir.td_stripbytecountsorted = 0;
5217 : break;
5218 : }
5219 : }
5220 : }
5221 : #endif
5222 :
5223 : /*
5224 : * An opportunity for compression mode dependent tag fixup
5225 : */
5226 54110 : (*tif->tif_fixuptags)(tif);
5227 :
5228 : /*
5229 : * Some manufacturers make life difficult by writing
5230 : * large amounts of uncompressed data as a single strip.
5231 : * This is contrary to the recommendations of the spec.
5232 : * The following makes an attempt at breaking such images
5233 : * into strips closer to the recommended 8k bytes. A
5234 : * side effect, however, is that the RowsPerStrip tag
5235 : * value may be changed.
5236 : */
5237 54130 : if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
5238 44777 : (tif->tif_dir.td_nstrips == 1) &&
5239 36349 : (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
5240 29809 : ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
5241 : {
5242 22083 : ChopUpSingleUncompressedStrip(tif);
5243 : }
5244 :
5245 : /* There are also uncompressed striped files with strips larger than */
5246 : /* 2 GB, which make them unfriendly with a lot of code. If possible, */
5247 : /* try to expose smaller "virtual" strips. */
5248 54142 : if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5249 44768 : tif->tif_dir.td_compression == COMPRESSION_NONE &&
5250 59632 : (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
5251 24950 : TIFFStripSize64(tif) > 0x7FFFFFFFUL)
5252 : {
5253 5 : TryChopUpUncompressedBigTiff(tif);
5254 : }
5255 :
5256 : /*
5257 : * Clear the dirty directory flag.
5258 : */
5259 54154 : tif->tif_flags &= ~TIFF_DIRTYDIRECT;
5260 54154 : tif->tif_flags &= ~TIFF_DIRTYSTRIP;
5261 :
5262 : /*
5263 : * Reinitialize some further i/o since we are starting on a new directory.
5264 : */
5265 54154 : tif->tif_dir.td_scanlinesize = TIFFScanlineSize(tif);
5266 54122 : if (!tif->tif_dir.td_scanlinesize)
5267 : {
5268 7 : TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
5269 0 : return (0);
5270 : }
5271 :
5272 54115 : if (isTiled(tif))
5273 : {
5274 9303 : tif->tif_dir.td_tilesize = TIFFTileSize(tif);
5275 9301 : if (!tif->tif_dir.td_tilesize)
5276 : {
5277 0 : TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
5278 0 : return (0);
5279 : }
5280 : }
5281 : else
5282 : {
5283 44812 : if (!TIFFStripSize(tif))
5284 : {
5285 5 : TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
5286 0 : return (0);
5287 : }
5288 : }
5289 54123 : return (1);
5290 1 : bad:
5291 1 : if (dir)
5292 1 : _TIFFfreeExt(tif, dir);
5293 1 : return (0);
5294 : } /*-- TIFFReadDirectory() --*/
5295 :
5296 54145 : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
5297 : uint16_t dircount)
5298 : {
5299 : static const char module[] = "TIFFReadDirectoryCheckOrder";
5300 : uint32_t m;
5301 : uint16_t n;
5302 : TIFFDirEntry *o;
5303 54145 : m = 0;
5304 817944 : for (n = 0, o = dir; n < dircount; n++, o++)
5305 : {
5306 763811 : if (o->tdir_tag < m)
5307 : {
5308 12 : TIFFWarningExtR(tif, module,
5309 : "Invalid TIFF directory; tags are not sorted in "
5310 : "ascending order");
5311 12 : break;
5312 : }
5313 763799 : m = o->tdir_tag + 1;
5314 : }
5315 54145 : }
5316 :
5317 108269 : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
5318 : uint16_t dircount,
5319 : uint16_t tagid)
5320 : {
5321 : TIFFDirEntry *m;
5322 : uint16_t n;
5323 : (void)tif;
5324 599264 : for (m = dir, n = 0; n < dircount; m++, n++)
5325 : {
5326 599220 : if (m->tdir_tag == tagid)
5327 108225 : return (m);
5328 : }
5329 44 : return (0);
5330 : }
5331 :
5332 1151180 : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
5333 : uint32_t *fii)
5334 : {
5335 : int32_t ma, mb, mc;
5336 1151180 : ma = -1;
5337 1151180 : mc = (int32_t)tif->tif_nfields;
5338 : while (1)
5339 : {
5340 7337480 : if (ma + 1 == mc)
5341 : {
5342 437 : *fii = FAILED_FII;
5343 437 : return;
5344 : }
5345 7337040 : mb = (ma + mc) / 2;
5346 7337040 : if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
5347 1150740 : break;
5348 6186300 : if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
5349 2343020 : ma = mb;
5350 : else
5351 3843280 : mc = mb;
5352 : }
5353 : while (1)
5354 : {
5355 1150770 : if (mb == 0)
5356 11408 : break;
5357 1139360 : if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
5358 1139330 : break;
5359 28 : mb--;
5360 : }
5361 1150740 : *fii = (uint32_t)mb;
5362 : }
5363 :
5364 : /*
5365 : * Read custom directory from the arbitrary offset.
5366 : * The code is very similar to TIFFReadDirectory().
5367 : */
5368 0 : int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff,
5369 : const TIFFFieldArray *infoarray)
5370 : {
5371 : static const char module[] = "TIFFReadCustomDirectory";
5372 : TIFFDirEntry *dir;
5373 : uint16_t dircount;
5374 : TIFFDirEntry *dp;
5375 : uint16_t di;
5376 : const TIFFField *fip;
5377 : uint32_t fii;
5378 :
5379 0 : assert(infoarray != NULL);
5380 0 : dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
5381 0 : if (!dircount)
5382 : {
5383 0 : TIFFErrorExtR(tif, module,
5384 : "Failed to read custom directory at offset %" PRIu64,
5385 : diroff);
5386 0 : return 0;
5387 : }
5388 0 : TIFFReadDirectoryCheckOrder(tif, dir, dircount);
5389 :
5390 : /*
5391 : * Mark duplicates of any tag to be ignored (bugzilla 1994)
5392 : * to avoid certain pathological problems.
5393 : */
5394 : {
5395 : TIFFDirEntry *ma;
5396 : uint16_t mb;
5397 0 : for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
5398 : {
5399 : TIFFDirEntry *na;
5400 : uint16_t nb;
5401 0 : for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
5402 : {
5403 0 : if (ma->tdir_tag == na->tdir_tag)
5404 : {
5405 0 : na->tdir_ignore = TRUE;
5406 : }
5407 : }
5408 : }
5409 : }
5410 :
5411 : /* Free any old stuff and reinit. */
5412 0 : TIFFFreeDirectory(tif);
5413 : /* Even if custom directories do not need the default settings of a standard
5414 : * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e.
5415 : * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to
5416 : * be initialized, which is done in TIFFDefaultDirectory().
5417 : * After that, the field array for the custom tags needs to be setup again.
5418 : */
5419 0 : TIFFDefaultDirectory(tif);
5420 0 : _TIFFSetupFields(tif, infoarray);
5421 :
5422 : /* Allocate arrays for offset values outside IFD entry for IFD data size
5423 : * checking. Note: Counter are reset within TIFFFreeDirectory(). */
5424 0 : tif->tif_dir.td_dirdatasize_offsets =
5425 0 : (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
5426 0 : tif, dircount * sizeof(TIFFEntryOffsetAndLength));
5427 0 : if (tif->tif_dir.td_dirdatasize_offsets == NULL)
5428 : {
5429 0 : TIFFErrorExtR(
5430 : tif, module,
5431 : "Failed to allocate memory for counting IFD data size at reading");
5432 0 : if (dir)
5433 0 : _TIFFfreeExt(tif, dir);
5434 0 : return 0;
5435 : }
5436 :
5437 0 : for (di = 0, dp = dir; di < dircount; di++, dp++)
5438 : {
5439 0 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5440 0 : if (fii == FAILED_FII)
5441 : {
5442 0 : if (tif->tif_warn_about_unknown_tags)
5443 : {
5444 0 : TIFFWarningExtR(tif, module,
5445 : "Unknown field with tag %" PRIu16 " (0x%" PRIx16
5446 : ") encountered",
5447 0 : dp->tdir_tag, dp->tdir_tag);
5448 : }
5449 0 : const TIFFField *fld = _TIFFCreateAnonField(
5450 0 : tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
5451 0 : if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
5452 : {
5453 0 : if (tif->tif_warn_about_unknown_tags)
5454 : {
5455 0 : TIFFWarningExtR(
5456 : tif, module,
5457 : "Registering anonymous field with tag %" PRIu16
5458 : " (0x%" PRIx16 ") failed",
5459 0 : dp->tdir_tag, dp->tdir_tag);
5460 : }
5461 0 : dp->tdir_ignore = TRUE;
5462 : }
5463 : else
5464 : {
5465 0 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5466 0 : assert(fii != FAILED_FII);
5467 : }
5468 : }
5469 0 : if (!dp->tdir_ignore)
5470 : {
5471 0 : fip = tif->tif_fields[fii];
5472 0 : if (fip->field_bit == FIELD_IGNORE)
5473 0 : dp->tdir_ignore = TRUE;
5474 : else
5475 : {
5476 : /* check data type */
5477 0 : while ((fip->field_type != TIFF_ANY) &&
5478 0 : (fip->field_type != dp->tdir_type))
5479 : {
5480 0 : fii++;
5481 0 : if ((fii == tif->tif_nfields) ||
5482 0 : (tif->tif_fields[fii]->field_tag !=
5483 0 : (uint32_t)dp->tdir_tag))
5484 : {
5485 0 : fii = 0xFFFF;
5486 0 : break;
5487 : }
5488 0 : fip = tif->tif_fields[fii];
5489 : }
5490 0 : if (fii == 0xFFFF)
5491 : {
5492 0 : TIFFWarningExtR(tif, module,
5493 : "Wrong data type %" PRIu16
5494 : " for \"%s\"; tag ignored",
5495 0 : dp->tdir_type, fip->field_name);
5496 0 : dp->tdir_ignore = TRUE;
5497 : }
5498 : else
5499 : {
5500 : /* check count if known in advance */
5501 0 : if ((fip->field_readcount != TIFF_VARIABLE) &&
5502 0 : (fip->field_readcount != TIFF_VARIABLE2))
5503 : {
5504 : uint32_t expected;
5505 0 : if (fip->field_readcount == TIFF_SPP)
5506 0 : expected =
5507 0 : (uint32_t)tif->tif_dir.td_samplesperpixel;
5508 : else
5509 0 : expected = (uint32_t)fip->field_readcount;
5510 0 : if (!CheckDirCount(tif, dp, expected))
5511 0 : dp->tdir_ignore = TRUE;
5512 : }
5513 : }
5514 : }
5515 0 : if (!dp->tdir_ignore)
5516 : {
5517 0 : switch (dp->tdir_tag)
5518 : {
5519 0 : case EXIFTAG_SUBJECTDISTANCE:
5520 0 : if (!TIFFFieldIsAnonymous(fip))
5521 : {
5522 : /* should only be called on a Exif directory */
5523 : /* when exifFields[] is active */
5524 0 : (void)TIFFFetchSubjectDistance(tif, dp);
5525 : }
5526 : else
5527 : {
5528 0 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
5529 : }
5530 0 : break;
5531 0 : default:
5532 0 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
5533 0 : break;
5534 : }
5535 0 : } /*-- if (!dp->tdir_ignore) */
5536 : }
5537 : }
5538 : /* Evaluate final IFD data size. */
5539 0 : CalcFinalIFDdatasizeReading(tif, dircount);
5540 :
5541 : /* To be able to return from SubIFD or custom-IFD to main-IFD */
5542 0 : tif->tif_setdirectory_force_absolute = TRUE;
5543 0 : if (dir)
5544 0 : _TIFFfreeExt(tif, dir);
5545 0 : return 1;
5546 : }
5547 :
5548 : /*
5549 : * EXIF is important special case of custom IFD, so we have a special
5550 : * function to read it.
5551 : */
5552 0 : int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
5553 : {
5554 0 : return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
5555 : }
5556 :
5557 : /*
5558 : *--: EXIF-GPS custom directory reading as another special case of custom IFD.
5559 : */
5560 0 : int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
5561 : {
5562 0 : return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
5563 : }
5564 :
5565 10 : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
5566 : uint16_t dircount)
5567 : {
5568 : static const char module[] = "EstimateStripByteCounts";
5569 :
5570 : TIFFDirEntry *dp;
5571 10 : TIFFDirectory *td = &tif->tif_dir;
5572 : uint32_t strip;
5573 :
5574 : /* Do not try to load stripbytecount as we will compute it */
5575 10 : if (!_TIFFFillStrilesInternal(tif, 0))
5576 0 : return -1;
5577 :
5578 10 : const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
5579 10 : uint64_t filesize = 0;
5580 10 : if (allocsize > 100 * 1024 * 1024)
5581 : {
5582 : /* Before allocating a huge amount of memory for corrupted files, check
5583 : * if size of requested memory is not greater than file size. */
5584 0 : filesize = TIFFGetFileSize(tif);
5585 0 : if (allocsize > filesize)
5586 : {
5587 0 : TIFFWarningExtR(
5588 : tif, module,
5589 : "Requested memory size for StripByteCounts of %" PRIu64
5590 : " is greater than filesize %" PRIu64 ". Memory not allocated",
5591 : allocsize, filesize);
5592 0 : return -1;
5593 : }
5594 : }
5595 :
5596 10 : if (td->td_stripbytecount_p)
5597 5 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
5598 20 : td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
5599 10 : tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
5600 10 : if (td->td_stripbytecount_p == NULL)
5601 0 : return -1;
5602 :
5603 10 : if (td->td_compression != COMPRESSION_NONE)
5604 : {
5605 : uint64_t space;
5606 : uint16_t n;
5607 2 : if (!(tif->tif_flags & TIFF_BIGTIFF))
5608 2 : space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
5609 : else
5610 0 : space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
5611 : /* calculate amount of space used by indirect values */
5612 17 : for (dp = dir, n = dircount; n > 0; n--, dp++)
5613 : {
5614 : uint32_t typewidth;
5615 : uint64_t datasize;
5616 15 : typewidth = (uint32_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
5617 15 : if (typewidth == 0)
5618 : {
5619 0 : TIFFErrorExtR(
5620 : tif, module,
5621 : "Cannot determine size of unknown tag type %" PRIu16,
5622 0 : dp->tdir_type);
5623 0 : return -1;
5624 : }
5625 15 : if (dp->tdir_count > UINT64_MAX / typewidth)
5626 0 : return -1;
5627 15 : datasize = (uint64_t)typewidth * dp->tdir_count;
5628 15 : if (!(tif->tif_flags & TIFF_BIGTIFF))
5629 : {
5630 15 : if (datasize <= 4)
5631 11 : datasize = 0;
5632 : }
5633 : else
5634 : {
5635 0 : if (datasize <= 8)
5636 0 : datasize = 0;
5637 : }
5638 15 : if (space > UINT64_MAX - datasize)
5639 0 : return -1;
5640 15 : space += datasize;
5641 : }
5642 2 : if (filesize == 0)
5643 2 : filesize = TIFFGetFileSize(tif);
5644 2 : if (filesize < space)
5645 : /* we should perhaps return in error ? */
5646 1 : space = filesize;
5647 : else
5648 1 : space = filesize - space;
5649 2 : if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
5650 0 : space /= td->td_samplesperpixel;
5651 4 : for (strip = 0; strip < td->td_nstrips; strip++)
5652 2 : td->td_stripbytecount_p[strip] = space;
5653 : /*
5654 : * This gross hack handles the case were the offset to
5655 : * the last strip is past the place where we think the strip
5656 : * should begin. Since a strip of data must be contiguous,
5657 : * it's safe to assume that we've overestimated the amount
5658 : * of data in the strip and trim this number back accordingly.
5659 : */
5660 2 : strip--;
5661 2 : if (td->td_stripoffset_p[strip] >
5662 2 : UINT64_MAX - td->td_stripbytecount_p[strip])
5663 0 : return -1;
5664 2 : if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
5665 : filesize)
5666 : {
5667 1 : if (td->td_stripoffset_p[strip] >= filesize)
5668 : {
5669 : /* Not sure what we should in that case... */
5670 0 : td->td_stripbytecount_p[strip] = 0;
5671 : }
5672 : else
5673 : {
5674 1 : td->td_stripbytecount_p[strip] =
5675 1 : filesize - td->td_stripoffset_p[strip];
5676 : }
5677 : }
5678 : }
5679 8 : else if (isTiled(tif))
5680 : {
5681 0 : uint64_t bytespertile = TIFFTileSize64(tif);
5682 :
5683 0 : for (strip = 0; strip < td->td_nstrips; strip++)
5684 0 : td->td_stripbytecount_p[strip] = bytespertile;
5685 : }
5686 : else
5687 : {
5688 8 : uint64_t rowbytes = TIFFScanlineSize64(tif);
5689 8 : uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
5690 16 : for (strip = 0; strip < td->td_nstrips; strip++)
5691 : {
5692 8 : if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
5693 0 : return -1;
5694 8 : td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
5695 : }
5696 : }
5697 10 : TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
5698 10 : if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
5699 5 : td->td_rowsperstrip = td->td_imagelength;
5700 10 : return 1;
5701 : }
5702 :
5703 0 : static void MissingRequired(TIFF *tif, const char *tagname)
5704 : {
5705 : static const char module[] = "MissingRequired";
5706 :
5707 0 : TIFFErrorExtR(tif, module,
5708 : "TIFF directory is missing required \"%s\" field", tagname);
5709 0 : }
5710 :
5711 291400 : static unsigned long hashFuncOffsetToNumber(const void *elt)
5712 : {
5713 291400 : const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5714 : (const TIFFOffsetAndDirNumber *)elt;
5715 291400 : const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
5716 291400 : ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
5717 291400 : return hash;
5718 : }
5719 :
5720 54888 : static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
5721 : {
5722 54888 : const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5723 : (const TIFFOffsetAndDirNumber *)elt1;
5724 54888 : const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5725 : (const TIFFOffsetAndDirNumber *)elt2;
5726 54888 : return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
5727 : }
5728 :
5729 249112 : static unsigned long hashFuncNumberToOffset(const void *elt)
5730 : {
5731 249112 : const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5732 : (const TIFFOffsetAndDirNumber *)elt;
5733 249112 : return offsetAndDirNumber->dirNumber;
5734 : }
5735 :
5736 13902 : static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
5737 : {
5738 13902 : const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5739 : (const TIFFOffsetAndDirNumber *)elt1;
5740 13902 : const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5741 : (const TIFFOffsetAndDirNumber *)elt2;
5742 13902 : return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
5743 : }
5744 :
5745 : /*
5746 : * Check the directory number and offset against the list of already seen
5747 : * directory numbers and offsets. This is a trick to prevent IFD looping.
5748 : * The one can create TIFF file with looped directory pointers. We will
5749 : * maintain a list of already seen directories and check every IFD offset
5750 : * and its IFD number against that list. However, the offset of an IFD number
5751 : * can change - e.g. when writing updates to file.
5752 : * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
5753 : * or an error has occurred.
5754 : */
5755 118182 : int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
5756 : {
5757 118182 : if (diroff == 0) /* no more directories */
5758 0 : return 0;
5759 :
5760 118182 : if (tif->tif_map_dir_offset_to_number == NULL)
5761 : {
5762 70690 : tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
5763 : hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
5764 70697 : if (tif->tif_map_dir_offset_to_number == NULL)
5765 : {
5766 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5767 : "Not enough memory");
5768 0 : return 1;
5769 : }
5770 : }
5771 :
5772 118189 : if (tif->tif_map_dir_number_to_offset == NULL)
5773 : {
5774 : /* No free callback for this map, as it shares the same items as
5775 : * tif->tif_map_dir_offset_to_number. */
5776 70696 : tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
5777 : hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
5778 70714 : if (tif->tif_map_dir_number_to_offset == NULL)
5779 : {
5780 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5781 : "Not enough memory");
5782 0 : return 1;
5783 : }
5784 : }
5785 :
5786 : /* Check if offset is already in the list:
5787 : * - yes: check, if offset is at the same IFD number - if not, it is an IFD
5788 : * loop
5789 : * - no: add to list or update offset at that IFD number
5790 : */
5791 : TIFFOffsetAndDirNumber entry;
5792 118207 : entry.offset = diroff;
5793 118207 : entry.dirNumber = dirn;
5794 :
5795 : TIFFOffsetAndDirNumber *foundEntry =
5796 118207 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5797 : tif->tif_map_dir_offset_to_number, &entry);
5798 118173 : if (foundEntry)
5799 : {
5800 39730 : if (foundEntry->dirNumber == dirn)
5801 : {
5802 39730 : return 1;
5803 : }
5804 : else
5805 : {
5806 0 : TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
5807 : "TIFF directory %d has IFD looping to directory %u "
5808 : "at offset 0x%" PRIx64 " (%" PRIu64 ")",
5809 0 : (int)dirn - 1, foundEntry->dirNumber, diroff,
5810 : diroff);
5811 0 : return 0;
5812 : }
5813 : }
5814 :
5815 : /* Check if offset of an IFD has been changed and update offset of that IFD
5816 : * number. */
5817 78443 : foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5818 : tif->tif_map_dir_number_to_offset, &entry);
5819 78444 : if (foundEntry)
5820 : {
5821 82 : if (foundEntry->offset != diroff)
5822 : {
5823 : TIFFOffsetAndDirNumber entryOld;
5824 82 : entryOld.offset = foundEntry->offset;
5825 82 : entryOld.dirNumber = dirn;
5826 : /* We must remove first from tif_map_dir_number_to_offset as the */
5827 : /* entry is owned (and thus freed) by */
5828 : /* tif_map_dir_offset_to_number */
5829 : TIFFOffsetAndDirNumber *foundEntryOld =
5830 82 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5831 : tif->tif_map_dir_number_to_offset, &entryOld);
5832 82 : if (foundEntryOld)
5833 : {
5834 82 : TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
5835 : foundEntryOld);
5836 : }
5837 82 : foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5838 : tif->tif_map_dir_offset_to_number, &entryOld);
5839 82 : if (foundEntryOld)
5840 : {
5841 82 : TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
5842 : foundEntryOld);
5843 : }
5844 :
5845 82 : TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
5846 : sizeof(TIFFOffsetAndDirNumber));
5847 82 : if (entryPtr == NULL)
5848 : {
5849 0 : return 0;
5850 : }
5851 :
5852 : /* Add IFD offset and dirn to IFD directory list */
5853 82 : *entryPtr = entry;
5854 :
5855 82 : if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5856 : {
5857 0 : TIFFErrorExtR(
5858 : tif, "_TIFFCheckDirNumberAndOffset",
5859 : "Insertion in tif_map_dir_offset_to_number failed");
5860 0 : return 0;
5861 : }
5862 82 : if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5863 : {
5864 0 : TIFFErrorExtR(
5865 : tif, "_TIFFCheckDirNumberAndOffset",
5866 : "Insertion in tif_map_dir_number_to_offset failed");
5867 0 : return 0;
5868 : }
5869 : }
5870 82 : return 1;
5871 : }
5872 :
5873 : /* Arbitrary (hopefully big enough) limit */
5874 78362 : if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
5875 : TIFF_MAX_DIR_COUNT)
5876 : {
5877 6 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5878 : "Cannot handle more than %u TIFF directories",
5879 : (unsigned)TIFF_MAX_DIR_COUNT);
5880 0 : return 0;
5881 : }
5882 :
5883 : TIFFOffsetAndDirNumber *entryPtr =
5884 78341 : (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
5885 78341 : if (entryPtr == NULL)
5886 : {
5887 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5888 : "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
5889 0 : return 0;
5890 : }
5891 :
5892 : /* Add IFD offset and dirn to IFD directory list */
5893 78341 : *entryPtr = entry;
5894 :
5895 78341 : if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5896 : {
5897 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5898 : "Insertion in tif_map_dir_offset_to_number failed");
5899 0 : return 0;
5900 : }
5901 78356 : if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5902 : {
5903 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5904 : "Insertion in tif_map_dir_number_to_offset failed");
5905 0 : return 0;
5906 : }
5907 :
5908 78361 : return 1;
5909 : } /* --- _TIFFCheckDirNumberAndOffset() ---*/
5910 :
5911 : /*
5912 : * Retrieve the matching IFD directory number of a given IFD offset
5913 : * from the list of directories already seen.
5914 : * Returns 1 if the offset was in the list and the directory number
5915 : * can be returned.
5916 : * Otherwise returns 0 or if an error occurred.
5917 : */
5918 11528 : int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
5919 : {
5920 11528 : if (diroff == 0) /* no more directories */
5921 0 : return 0;
5922 :
5923 : /* Check if offset is already in the list and return matching directory
5924 : * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
5925 : * search again in IFD list.
5926 : */
5927 11528 : if (tif->tif_map_dir_offset_to_number == NULL)
5928 5 : return 0;
5929 : TIFFOffsetAndDirNumber entry;
5930 11523 : entry.offset = diroff;
5931 11523 : entry.dirNumber = 0; /* not used */
5932 :
5933 : TIFFOffsetAndDirNumber *foundEntry =
5934 11523 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5935 : tif->tif_map_dir_offset_to_number, &entry);
5936 11522 : if (foundEntry)
5937 : {
5938 9699 : *dirn = foundEntry->dirNumber;
5939 9699 : return 1;
5940 : }
5941 :
5942 : /* This updates the directory list for all main-IFDs in the file. */
5943 1823 : TIFFNumberOfDirectories(tif);
5944 :
5945 1825 : foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5946 : tif->tif_map_dir_offset_to_number, &entry);
5947 1823 : if (foundEntry)
5948 : {
5949 1769 : *dirn = foundEntry->dirNumber;
5950 1769 : return 1;
5951 : }
5952 :
5953 54 : return 0;
5954 : } /*--- _TIFFGetDirNumberFromOffset() ---*/
5955 :
5956 : /*
5957 : * Retrieve the matching IFD directory offset of a given IFD number
5958 : * from the list of directories already seen.
5959 : * Returns 1 if the offset was in the list of already seen IFDs and the
5960 : * directory offset can be returned. The directory list is not updated.
5961 : * Otherwise returns 0 or if an error occurred.
5962 : */
5963 10824 : int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
5964 : {
5965 :
5966 10824 : if (tif->tif_map_dir_number_to_offset == NULL)
5967 0 : return 0;
5968 : TIFFOffsetAndDirNumber entry;
5969 10824 : entry.offset = 0; /* not used */
5970 10824 : entry.dirNumber = dirn;
5971 :
5972 : TIFFOffsetAndDirNumber *foundEntry =
5973 10824 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5974 : tif->tif_map_dir_number_to_offset, &entry);
5975 10824 : if (foundEntry)
5976 : {
5977 10824 : *diroff = foundEntry->offset;
5978 10824 : return 1;
5979 : }
5980 :
5981 0 : return 0;
5982 : } /*--- _TIFFGetOffsetFromDirNumber() ---*/
5983 :
5984 : /*
5985 : * Remove an entry from the directory list of already seen directories
5986 : * by directory offset.
5987 : * If an entry is to be removed from the list, it is also okay if the entry
5988 : * is not in the list or the list does not exist.
5989 : */
5990 1427 : int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
5991 : {
5992 1427 : if (tif->tif_map_dir_offset_to_number == NULL)
5993 0 : return 1;
5994 :
5995 : TIFFOffsetAndDirNumber entryOld;
5996 1427 : entryOld.offset = diroff;
5997 1427 : entryOld.dirNumber = 0;
5998 : /* We must remove first from tif_map_dir_number_to_offset as the
5999 : * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
6000 : * However, we need firstly to find the directory number from offset. */
6001 :
6002 : TIFFOffsetAndDirNumber *foundEntryOldOff =
6003 1427 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
6004 : tif->tif_map_dir_offset_to_number, &entryOld);
6005 1427 : if (foundEntryOldOff)
6006 : {
6007 1416 : entryOld.dirNumber = foundEntryOldOff->dirNumber;
6008 1416 : if (tif->tif_map_dir_number_to_offset != NULL)
6009 : {
6010 : TIFFOffsetAndDirNumber *foundEntryOldDir =
6011 1416 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
6012 : tif->tif_map_dir_number_to_offset, &entryOld);
6013 1416 : if (foundEntryOldDir)
6014 : {
6015 1416 : TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
6016 : foundEntryOldDir);
6017 1416 : TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
6018 : foundEntryOldOff);
6019 1416 : return 1;
6020 : }
6021 : }
6022 : else
6023 : {
6024 0 : TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
6025 : "Unexpectedly tif_map_dir_number_to_offset is "
6026 : "missing but tif_map_dir_offset_to_number exists.");
6027 0 : return 0;
6028 : }
6029 : }
6030 11 : return 1;
6031 : } /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
6032 :
6033 : /*
6034 : * Check the count field of a directory entry against a known value. The
6035 : * caller is expected to skip/ignore the tag if there is a mismatch.
6036 : */
6037 0 : static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
6038 : {
6039 0 : if ((uint64_t)count > dir->tdir_count)
6040 : {
6041 0 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
6042 0 : TIFFWarningExtR(tif, tif->tif_name,
6043 : "incorrect count for field \"%s\" (%" PRIu64
6044 : ", expecting %" PRIu32 "); tag ignored",
6045 : fip ? fip->field_name : "unknown tagname",
6046 : dir->tdir_count, count);
6047 0 : return (0);
6048 : }
6049 0 : else if ((uint64_t)count < dir->tdir_count)
6050 : {
6051 0 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
6052 0 : TIFFWarningExtR(tif, tif->tif_name,
6053 : "incorrect count for field \"%s\" (%" PRIu64
6054 : ", expecting %" PRIu32 "); tag trimmed",
6055 : fip ? fip->field_name : "unknown tagname",
6056 : dir->tdir_count, count);
6057 0 : dir->tdir_count = count;
6058 0 : return (1);
6059 : }
6060 0 : return (1);
6061 : }
6062 :
6063 : /*
6064 : * Read IFD structure from the specified offset. If the pointer to
6065 : * nextdiroff variable has been specified, read it too. Function returns a
6066 : * number of fields in the directory or 0 if failed.
6067 : */
6068 54162 : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
6069 : TIFFDirEntry **pdir, uint64_t *nextdiroff)
6070 : {
6071 : static const char module[] = "TIFFFetchDirectory";
6072 :
6073 : void *origdir;
6074 : uint16_t dircount16;
6075 : uint32_t dirsize;
6076 : TIFFDirEntry *dir;
6077 : uint8_t *ma;
6078 : TIFFDirEntry *mb;
6079 : uint16_t n;
6080 :
6081 54162 : assert(pdir);
6082 :
6083 54162 : tif->tif_diroff = diroff;
6084 54162 : if (nextdiroff)
6085 54152 : *nextdiroff = 0;
6086 54162 : if (!isMapped(tif))
6087 : {
6088 54135 : if (!SeekOK(tif, tif->tif_diroff))
6089 : {
6090 1 : TIFFErrorExtR(tif, module,
6091 : "%s: Seek error accessing TIFF directory",
6092 : tif->tif_name);
6093 0 : return 0;
6094 : }
6095 54135 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6096 : {
6097 53289 : if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
6098 : {
6099 12 : TIFFErrorExtR(tif, module,
6100 : "%s: Can not read TIFF directory count",
6101 : tif->tif_name);
6102 12 : return 0;
6103 : }
6104 53288 : if (tif->tif_flags & TIFF_SWAB)
6105 561 : TIFFSwabShort(&dircount16);
6106 53281 : if (dircount16 > 4096)
6107 : {
6108 0 : TIFFErrorExtR(tif, module,
6109 : "Sanity check on directory count failed, this is "
6110 : "probably not a valid IFD offset");
6111 0 : return 0;
6112 : }
6113 53281 : dirsize = 12;
6114 : }
6115 : else
6116 : {
6117 : uint64_t dircount64;
6118 846 : if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
6119 : {
6120 0 : TIFFErrorExtR(tif, module,
6121 : "%s: Can not read TIFF directory count",
6122 : tif->tif_name);
6123 0 : return 0;
6124 : }
6125 846 : if (tif->tif_flags & TIFF_SWAB)
6126 19 : TIFFSwabLong8(&dircount64);
6127 846 : if (dircount64 > 4096)
6128 : {
6129 0 : TIFFErrorExtR(tif, module,
6130 : "Sanity check on directory count failed, this is "
6131 : "probably not a valid IFD offset");
6132 0 : return 0;
6133 : }
6134 846 : dircount16 = (uint16_t)dircount64;
6135 846 : dirsize = 20;
6136 : }
6137 54127 : origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6138 : "to read TIFF directory");
6139 54138 : if (origdir == NULL)
6140 4 : return 0;
6141 54134 : if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
6142 : {
6143 1 : TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
6144 : tif->tif_name);
6145 1 : _TIFFfreeExt(tif, origdir);
6146 1 : return 0;
6147 : }
6148 : /*
6149 : * Read offset to next directory for sequential scans if
6150 : * needed.
6151 : */
6152 54123 : if (nextdiroff)
6153 : {
6154 54101 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6155 : {
6156 : uint32_t nextdiroff32;
6157 53274 : if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
6158 4 : nextdiroff32 = 0;
6159 53284 : if (tif->tif_flags & TIFF_SWAB)
6160 561 : TIFFSwabLong(&nextdiroff32);
6161 53281 : *nextdiroff = nextdiroff32;
6162 : }
6163 : else
6164 : {
6165 827 : if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
6166 0 : *nextdiroff = 0;
6167 846 : if (tif->tif_flags & TIFF_SWAB)
6168 19 : TIFFSwabLong8(nextdiroff);
6169 : }
6170 : }
6171 : }
6172 : else
6173 : {
6174 : tmsize_t m;
6175 : tmsize_t off;
6176 27 : if (tif->tif_diroff > (uint64_t)INT64_MAX)
6177 : {
6178 0 : TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6179 0 : return (0);
6180 : }
6181 27 : off = (tmsize_t)tif->tif_diroff;
6182 :
6183 : /*
6184 : * Check for integer overflow when validating the dir_off,
6185 : * otherwise a very high offset may cause an OOB read and
6186 : * crash the client. Make two comparisons instead of
6187 : *
6188 : * off + sizeof(uint16_t) > tif->tif_size
6189 : *
6190 : * to avoid overflow.
6191 : */
6192 27 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6193 : {
6194 8 : m = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
6195 8 : if ((m < off) || ((uint64_t)m < sizeof(uint16_t)) ||
6196 8 : ((uint64_t)m > (uint64_t)tif->tif_size))
6197 : {
6198 0 : TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6199 0 : return 0;
6200 : }
6201 : else
6202 : {
6203 8 : _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
6204 : }
6205 8 : off = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
6206 8 : if (tif->tif_flags & TIFF_SWAB)
6207 0 : TIFFSwabShort(&dircount16);
6208 8 : if (dircount16 > 4096)
6209 : {
6210 0 : TIFFErrorExtR(tif, module,
6211 : "Sanity check on directory count failed, this is "
6212 : "probably not a valid IFD offset");
6213 0 : return 0;
6214 : }
6215 8 : dirsize = 12;
6216 : }
6217 : else
6218 : {
6219 : uint64_t dircount64;
6220 19 : m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
6221 19 : if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
6222 0 : ((uint64_t)m > (uint64_t)tif->tif_size))
6223 : {
6224 19 : TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6225 0 : return 0;
6226 : }
6227 : else
6228 : {
6229 0 : _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
6230 : }
6231 0 : off = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
6232 0 : if (tif->tif_flags & TIFF_SWAB)
6233 0 : TIFFSwabLong8(&dircount64);
6234 0 : if (dircount64 > 4096)
6235 : {
6236 0 : TIFFErrorExtR(tif, module,
6237 : "Sanity check on directory count failed, this is "
6238 : "probably not a valid IFD offset");
6239 0 : return 0;
6240 : }
6241 0 : dircount16 = (uint16_t)dircount64;
6242 0 : dirsize = 20;
6243 : }
6244 8 : if (dircount16 == 0)
6245 : {
6246 0 : TIFFErrorExtR(tif, module,
6247 : "Sanity check on directory count failed, zero tag "
6248 : "directories not supported");
6249 0 : return 0;
6250 : }
6251 : /* Before allocating a huge amount of memory for corrupted files, check
6252 : * if size of requested memory is not greater than file size. */
6253 8 : uint64_t filesize = TIFFGetFileSize(tif);
6254 8 : uint64_t allocsize = (uint64_t)dircount16 * dirsize;
6255 8 : if (allocsize > filesize)
6256 : {
6257 0 : TIFFWarningExtR(
6258 : tif, module,
6259 : "Requested memory size for TIFF directory of %" PRIu64
6260 : " is greater than filesize %" PRIu64
6261 : ". Memory not allocated, TIFF directory not read",
6262 : allocsize, filesize);
6263 0 : return 0;
6264 : }
6265 8 : origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6266 : "to read TIFF directory");
6267 8 : if (origdir == NULL)
6268 0 : return 0;
6269 8 : m = off + dircount16 * dirsize;
6270 8 : if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
6271 8 : (m > tif->tif_size))
6272 : {
6273 0 : TIFFErrorExtR(tif, module, "Can not read TIFF directory");
6274 0 : _TIFFfreeExt(tif, origdir);
6275 0 : return 0;
6276 : }
6277 : else
6278 : {
6279 8 : _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
6280 : }
6281 8 : if (nextdiroff)
6282 : {
6283 8 : off += dircount16 * dirsize;
6284 8 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6285 : {
6286 : uint32_t nextdiroff32;
6287 8 : m = (tmsize_t)((uint64_t)off + sizeof(uint32_t));
6288 8 : if ((m < off) || ((uint64_t)m < sizeof(uint32_t)) ||
6289 8 : ((uint64_t)m > (uint64_t)tif->tif_size))
6290 0 : nextdiroff32 = 0;
6291 : else
6292 8 : _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
6293 : sizeof(uint32_t));
6294 8 : if (tif->tif_flags & TIFF_SWAB)
6295 0 : TIFFSwabLong(&nextdiroff32);
6296 8 : *nextdiroff = nextdiroff32;
6297 : }
6298 : else
6299 : {
6300 0 : m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
6301 0 : if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
6302 0 : ((uint64_t)m > (uint64_t)tif->tif_size))
6303 0 : *nextdiroff = 0;
6304 : else
6305 0 : _TIFFmemcpy(nextdiroff, tif->tif_base + off,
6306 : sizeof(uint64_t));
6307 0 : if (tif->tif_flags & TIFF_SWAB)
6308 0 : TIFFSwabLong8(nextdiroff);
6309 : }
6310 : }
6311 : }
6312 : /* No check against filesize needed here because "dir" should have same size
6313 : * than "origdir" checked above. */
6314 54157 : dir = (TIFFDirEntry *)_TIFFCheckMalloc(
6315 : tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
6316 54140 : if (dir == 0)
6317 : {
6318 3 : _TIFFfreeExt(tif, origdir);
6319 0 : return 0;
6320 : }
6321 54137 : ma = (uint8_t *)origdir;
6322 54137 : mb = dir;
6323 819190 : for (n = 0; n < dircount16; n++)
6324 : {
6325 765107 : mb->tdir_ignore = FALSE;
6326 765107 : if (tif->tif_flags & TIFF_SWAB)
6327 7746 : TIFFSwabShort((uint16_t *)ma);
6328 765107 : mb->tdir_tag = *(uint16_t *)ma;
6329 765107 : ma += sizeof(uint16_t);
6330 765107 : if (tif->tif_flags & TIFF_SWAB)
6331 7746 : TIFFSwabShort((uint16_t *)ma);
6332 765052 : mb->tdir_type = *(uint16_t *)ma;
6333 765052 : ma += sizeof(uint16_t);
6334 765052 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6335 : {
6336 752805 : if (tif->tif_flags & TIFF_SWAB)
6337 7537 : TIFFSwabLong((uint32_t *)ma);
6338 752793 : mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
6339 752793 : ma += sizeof(uint32_t);
6340 752793 : mb->tdir_offset.toff_long8 = 0;
6341 752793 : *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
6342 752793 : ma += sizeof(uint32_t);
6343 : }
6344 : else
6345 : {
6346 12247 : if (tif->tif_flags & TIFF_SWAB)
6347 209 : TIFFSwabLong8((uint64_t *)ma);
6348 12247 : mb->tdir_count = TIFFReadUInt64(ma);
6349 12247 : ma += sizeof(uint64_t);
6350 12247 : mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
6351 12260 : ma += sizeof(uint64_t);
6352 : }
6353 765053 : mb++;
6354 : }
6355 54083 : _TIFFfreeExt(tif, origdir);
6356 54144 : *pdir = dir;
6357 54144 : return dircount16;
6358 : }
6359 :
6360 : /*
6361 : * Fetch a tag that is not handled by special case code.
6362 : */
6363 494412 : static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
6364 : {
6365 : static const char module[] = "TIFFFetchNormalTag";
6366 : enum TIFFReadDirEntryErr err;
6367 : uint32_t fii;
6368 494412 : const TIFFField *fip = NULL;
6369 494412 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
6370 494404 : if (fii == FAILED_FII)
6371 : {
6372 16 : TIFFErrorExtR(tif, "TIFFFetchNormalTag",
6373 16 : "No definition found for tag %" PRIu16, dp->tdir_tag);
6374 0 : return 0;
6375 : }
6376 494388 : fip = tif->tif_fields[fii];
6377 494388 : assert(fip != NULL); /* should not happen */
6378 494388 : assert(fip->set_get_field_type !=
6379 : TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
6380 : this in specialized code */
6381 494388 : assert(fip->set_get_field_type !=
6382 : TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
6383 : the case for pseudo-tags */
6384 494388 : err = TIFFReadDirEntryErrOk;
6385 494388 : switch (fip->set_get_field_type)
6386 : {
6387 386 : case TIFF_SETGET_UNDEFINED:
6388 386 : TIFFErrorExtR(
6389 : tif, "TIFFFetchNormalTag",
6390 : "Defined set_get_field_type of custom tag %u (%s) is "
6391 : "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
6392 : fip->field_tag, fip->field_name);
6393 386 : break;
6394 38368 : case TIFF_SETGET_ASCII:
6395 : {
6396 : uint8_t *data;
6397 38368 : assert(fip->field_passcount == 0);
6398 38368 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
6399 38350 : if (err == TIFFReadDirEntryErrOk)
6400 : {
6401 38346 : size_t mb = 0;
6402 : int n;
6403 38346 : if (data != NULL)
6404 : {
6405 38346 : if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
6406 : {
6407 : /* optimization: if data is known to be 0 terminated, we
6408 : * can use strlen() */
6409 38344 : mb = strlen((const char *)data);
6410 : }
6411 : else
6412 : {
6413 : /* general case. equivalent to non-portable */
6414 : /* mb = strnlen((const char*)data,
6415 : * (uint32_t)dp->tdir_count); */
6416 2 : uint8_t *ma = data;
6417 29 : while (mb < (uint32_t)dp->tdir_count)
6418 : {
6419 27 : if (*ma == 0)
6420 0 : break;
6421 27 : ma++;
6422 27 : mb++;
6423 : }
6424 : }
6425 : }
6426 38346 : if (!EvaluateIFDdatasizeReading(tif, dp))
6427 : {
6428 16 : if (data != NULL)
6429 0 : _TIFFfreeExt(tif, data);
6430 0 : return (0);
6431 : }
6432 38345 : if (mb + 1 < (uint32_t)dp->tdir_count)
6433 0 : TIFFWarningExtR(
6434 : tif, module,
6435 : "ASCII value for tag \"%s\" contains null byte in "
6436 : "value; value incorrectly truncated during reading due "
6437 : "to implementation limitations",
6438 : fip->field_name);
6439 38345 : else if (mb + 1 > (uint32_t)dp->tdir_count)
6440 : {
6441 1 : TIFFWarningExtR(tif, module,
6442 : "ASCII value for tag \"%s\" does not end "
6443 : "in null byte. Forcing it to be null",
6444 : fip->field_name);
6445 : /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
6446 : * larger than MAX_SIZE_TAG_DATA */
6447 1 : assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
6448 1 : uint8_t *o = (uint8_t *)_TIFFmallocExt(
6449 1 : tif, (uint32_t)dp->tdir_count + 1);
6450 1 : if (o == NULL)
6451 : {
6452 0 : if (data != NULL)
6453 0 : _TIFFfreeExt(tif, data);
6454 0 : return (0);
6455 : }
6456 1 : if (dp->tdir_count > 0)
6457 : {
6458 1 : _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6459 : }
6460 1 : o[(uint32_t)dp->tdir_count] = 0;
6461 1 : if (data != 0)
6462 1 : _TIFFfreeExt(tif, data);
6463 1 : data = o;
6464 : }
6465 38345 : n = TIFFSetField(tif, dp->tdir_tag, data);
6466 38365 : if (data != 0)
6467 38364 : _TIFFfreeExt(tif, data);
6468 38356 : if (!n)
6469 0 : return (0);
6470 : }
6471 : }
6472 38360 : break;
6473 0 : case TIFF_SETGET_UINT8:
6474 : {
6475 0 : uint8_t data = 0;
6476 0 : assert(fip->field_readcount == 1);
6477 0 : assert(fip->field_passcount == 0);
6478 0 : err = TIFFReadDirEntryByte(tif, dp, &data);
6479 0 : if (err == TIFFReadDirEntryErrOk)
6480 : {
6481 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6482 0 : return (0);
6483 : }
6484 : }
6485 0 : break;
6486 0 : case TIFF_SETGET_SINT8:
6487 : {
6488 0 : int8_t data = 0;
6489 0 : assert(fip->field_readcount == 1);
6490 0 : assert(fip->field_passcount == 0);
6491 0 : err = TIFFReadDirEntrySbyte(tif, dp, &data);
6492 0 : if (err == TIFFReadDirEntryErrOk)
6493 : {
6494 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6495 0 : return (0);
6496 : }
6497 : }
6498 0 : break;
6499 168113 : case TIFF_SETGET_UINT16:
6500 : {
6501 : uint16_t data;
6502 168113 : assert(fip->field_readcount == 1);
6503 168113 : assert(fip->field_passcount == 0);
6504 168113 : err = TIFFReadDirEntryShort(tif, dp, &data);
6505 168104 : if (err == TIFFReadDirEntryErrOk)
6506 : {
6507 168104 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6508 0 : return (0);
6509 : }
6510 : }
6511 168119 : break;
6512 0 : case TIFF_SETGET_SINT16:
6513 : {
6514 : int16_t data;
6515 0 : assert(fip->field_readcount == 1);
6516 0 : assert(fip->field_passcount == 0);
6517 0 : err = TIFFReadDirEntrySshort(tif, dp, &data);
6518 0 : if (err == TIFFReadDirEntryErrOk)
6519 : {
6520 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6521 0 : return (0);
6522 : }
6523 : }
6524 0 : break;
6525 177352 : case TIFF_SETGET_UINT32:
6526 : {
6527 : uint32_t data;
6528 177352 : assert(fip->field_readcount == 1);
6529 177352 : assert(fip->field_passcount == 0);
6530 177352 : err = TIFFReadDirEntryLong(tif, dp, &data);
6531 177343 : if (err == TIFFReadDirEntryErrOk)
6532 : {
6533 177375 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6534 0 : return (0);
6535 : }
6536 : }
6537 177331 : break;
6538 0 : case TIFF_SETGET_SINT32:
6539 : {
6540 : int32_t data;
6541 0 : assert(fip->field_readcount == 1);
6542 0 : assert(fip->field_passcount == 0);
6543 0 : err = TIFFReadDirEntrySlong(tif, dp, &data);
6544 0 : if (err == TIFFReadDirEntryErrOk)
6545 : {
6546 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6547 0 : return (0);
6548 : }
6549 : }
6550 0 : break;
6551 8 : case TIFF_SETGET_UINT64:
6552 : {
6553 : uint64_t data;
6554 8 : assert(fip->field_readcount == 1);
6555 8 : assert(fip->field_passcount == 0);
6556 8 : err = TIFFReadDirEntryLong8(tif, dp, &data);
6557 8 : if (err == TIFFReadDirEntryErrOk)
6558 : {
6559 8 : if (!EvaluateIFDdatasizeReading(tif, dp))
6560 0 : return 0;
6561 8 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6562 0 : return (0);
6563 : }
6564 : }
6565 8 : break;
6566 0 : case TIFF_SETGET_SINT64:
6567 : {
6568 : int64_t data;
6569 0 : assert(fip->field_readcount == 1);
6570 0 : assert(fip->field_passcount == 0);
6571 0 : err = TIFFReadDirEntrySlong8(tif, dp, &data);
6572 0 : if (err == TIFFReadDirEntryErrOk)
6573 : {
6574 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6575 0 : return 0;
6576 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6577 0 : return (0);
6578 : }
6579 : }
6580 0 : break;
6581 248 : case TIFF_SETGET_FLOAT:
6582 : {
6583 : float data;
6584 248 : assert(fip->field_readcount == 1);
6585 248 : assert(fip->field_passcount == 0);
6586 248 : err = TIFFReadDirEntryFloat(tif, dp, &data);
6587 248 : if (err == TIFFReadDirEntryErrOk)
6588 : {
6589 248 : if (!EvaluateIFDdatasizeReading(tif, dp))
6590 0 : return 0;
6591 248 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6592 0 : return (0);
6593 : }
6594 : }
6595 248 : break;
6596 0 : case TIFF_SETGET_DOUBLE:
6597 : {
6598 : double data;
6599 0 : assert(fip->field_readcount == 1);
6600 0 : assert(fip->field_passcount == 0);
6601 0 : err = TIFFReadDirEntryDouble(tif, dp, &data);
6602 0 : if (err == TIFFReadDirEntryErrOk)
6603 : {
6604 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6605 0 : return 0;
6606 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6607 0 : return (0);
6608 : }
6609 : }
6610 0 : break;
6611 0 : case TIFF_SETGET_IFD8:
6612 : {
6613 : uint64_t data;
6614 0 : assert(fip->field_readcount == 1);
6615 0 : assert(fip->field_passcount == 0);
6616 0 : err = TIFFReadDirEntryIfd8(tif, dp, &data);
6617 0 : if (err == TIFFReadDirEntryErrOk)
6618 : {
6619 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6620 0 : return 0;
6621 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6622 0 : return (0);
6623 : }
6624 : }
6625 0 : break;
6626 1339 : case TIFF_SETGET_UINT16_PAIR:
6627 : {
6628 : uint16_t *data;
6629 1339 : assert(fip->field_readcount == 2);
6630 1339 : assert(fip->field_passcount == 0);
6631 1339 : if (dp->tdir_count != 2)
6632 : {
6633 0 : TIFFWarningExtR(tif, module,
6634 : "incorrect count for field \"%s\", expected 2, "
6635 : "got %" PRIu64,
6636 : fip->field_name, dp->tdir_count);
6637 0 : return (0);
6638 : }
6639 1339 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
6640 1339 : if (err == TIFFReadDirEntryErrOk)
6641 : {
6642 : int m;
6643 1339 : assert(data); /* avoid CLang static Analyzer false positive */
6644 1339 : m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
6645 1339 : _TIFFfreeExt(tif, data);
6646 1339 : if (!m)
6647 0 : return (0);
6648 : }
6649 : }
6650 1339 : break;
6651 0 : case TIFF_SETGET_C0_UINT8:
6652 : {
6653 : uint8_t *data;
6654 0 : assert(fip->field_readcount >= 1);
6655 0 : assert(fip->field_passcount == 0);
6656 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6657 : {
6658 0 : TIFFWarningExtR(tif, module,
6659 : "incorrect count for field \"%s\", expected "
6660 : "%d, got %" PRIu64,
6661 0 : fip->field_name, (int)fip->field_readcount,
6662 : dp->tdir_count);
6663 0 : return (0);
6664 : }
6665 : else
6666 : {
6667 0 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
6668 0 : if (err == TIFFReadDirEntryErrOk)
6669 : {
6670 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6671 : {
6672 0 : if (data != 0)
6673 0 : _TIFFfreeExt(tif, data);
6674 0 : return 0;
6675 : }
6676 : int m;
6677 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6678 0 : if (data != 0)
6679 0 : _TIFFfreeExt(tif, data);
6680 0 : if (!m)
6681 0 : return (0);
6682 : }
6683 : }
6684 : }
6685 0 : break;
6686 0 : case TIFF_SETGET_C0_SINT8:
6687 : {
6688 : int8_t *data;
6689 0 : assert(fip->field_readcount >= 1);
6690 0 : assert(fip->field_passcount == 0);
6691 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6692 : {
6693 0 : TIFFWarningExtR(tif, module,
6694 : "incorrect count for field \"%s\", expected "
6695 : "%d, got %" PRIu64,
6696 0 : fip->field_name, (int)fip->field_readcount,
6697 : dp->tdir_count);
6698 0 : return (0);
6699 : }
6700 : else
6701 : {
6702 0 : err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
6703 0 : if (err == TIFFReadDirEntryErrOk)
6704 : {
6705 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6706 : {
6707 0 : if (data != 0)
6708 0 : _TIFFfreeExt(tif, data);
6709 0 : return 0;
6710 : }
6711 : int m;
6712 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6713 0 : if (data != 0)
6714 0 : _TIFFfreeExt(tif, data);
6715 0 : if (!m)
6716 0 : return (0);
6717 : }
6718 : }
6719 : }
6720 0 : break;
6721 0 : case TIFF_SETGET_C0_UINT16:
6722 : {
6723 : uint16_t *data;
6724 0 : assert(fip->field_readcount >= 1);
6725 0 : assert(fip->field_passcount == 0);
6726 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6727 : {
6728 0 : TIFFWarningExtR(tif, module,
6729 : "incorrect count for field \"%s\", expected "
6730 : "%d, got %" PRIu64,
6731 0 : fip->field_name, (int)fip->field_readcount,
6732 : dp->tdir_count);
6733 0 : return (0);
6734 : }
6735 : else
6736 : {
6737 0 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
6738 0 : if (err == TIFFReadDirEntryErrOk)
6739 : {
6740 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6741 : {
6742 0 : if (data != 0)
6743 0 : _TIFFfreeExt(tif, data);
6744 0 : return 0;
6745 : }
6746 : int m;
6747 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6748 0 : if (data != 0)
6749 0 : _TIFFfreeExt(tif, data);
6750 0 : if (!m)
6751 0 : return (0);
6752 : }
6753 : }
6754 : }
6755 0 : break;
6756 0 : case TIFF_SETGET_C0_SINT16:
6757 : {
6758 : int16_t *data;
6759 0 : assert(fip->field_readcount >= 1);
6760 0 : assert(fip->field_passcount == 0);
6761 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6762 : {
6763 0 : TIFFWarningExtR(tif, module,
6764 : "incorrect count for field \"%s\", expected "
6765 : "%d, got %" PRIu64,
6766 0 : fip->field_name, (int)fip->field_readcount,
6767 : dp->tdir_count);
6768 0 : return (0);
6769 : }
6770 : else
6771 : {
6772 0 : err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6773 0 : if (err == TIFFReadDirEntryErrOk)
6774 : {
6775 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6776 : {
6777 0 : if (data != 0)
6778 0 : _TIFFfreeExt(tif, data);
6779 0 : return 0;
6780 : }
6781 : int m;
6782 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6783 0 : if (data != 0)
6784 0 : _TIFFfreeExt(tif, data);
6785 0 : if (!m)
6786 0 : return (0);
6787 : }
6788 : }
6789 : }
6790 0 : break;
6791 0 : case TIFF_SETGET_C0_UINT32:
6792 : {
6793 : uint32_t *data;
6794 0 : assert(fip->field_readcount >= 1);
6795 0 : assert(fip->field_passcount == 0);
6796 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6797 : {
6798 0 : TIFFWarningExtR(tif, module,
6799 : "incorrect count for field \"%s\", expected "
6800 : "%d, got %" PRIu64,
6801 0 : fip->field_name, (int)fip->field_readcount,
6802 : dp->tdir_count);
6803 0 : return (0);
6804 : }
6805 : else
6806 : {
6807 0 : err = TIFFReadDirEntryLongArray(tif, dp, &data);
6808 0 : if (err == TIFFReadDirEntryErrOk)
6809 : {
6810 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6811 : {
6812 0 : if (data != 0)
6813 0 : _TIFFfreeExt(tif, data);
6814 0 : return 0;
6815 : }
6816 : int m;
6817 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6818 0 : if (data != 0)
6819 0 : _TIFFfreeExt(tif, data);
6820 0 : if (!m)
6821 0 : return (0);
6822 : }
6823 : }
6824 : }
6825 0 : break;
6826 0 : case TIFF_SETGET_C0_SINT32:
6827 : {
6828 : int32_t *data;
6829 0 : assert(fip->field_readcount >= 1);
6830 0 : assert(fip->field_passcount == 0);
6831 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6832 : {
6833 0 : TIFFWarningExtR(tif, module,
6834 : "incorrect count for field \"%s\", expected "
6835 : "%d, got %" PRIu64,
6836 0 : fip->field_name, (int)fip->field_readcount,
6837 : dp->tdir_count);
6838 0 : return (0);
6839 : }
6840 : else
6841 : {
6842 0 : err = TIFFReadDirEntrySlongArray(tif, dp, &data);
6843 0 : if (err == TIFFReadDirEntryErrOk)
6844 : {
6845 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6846 : {
6847 0 : if (data != 0)
6848 0 : _TIFFfreeExt(tif, data);
6849 0 : return 0;
6850 : }
6851 : int m;
6852 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6853 0 : if (data != 0)
6854 0 : _TIFFfreeExt(tif, data);
6855 0 : if (!m)
6856 0 : return (0);
6857 : }
6858 : }
6859 : }
6860 0 : break;
6861 0 : case TIFF_SETGET_C0_UINT64:
6862 : {
6863 : uint64_t *data;
6864 0 : assert(fip->field_readcount >= 1);
6865 0 : assert(fip->field_passcount == 0);
6866 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6867 : {
6868 0 : TIFFWarningExtR(tif, module,
6869 : "incorrect count for field \"%s\", expected "
6870 : "%d, got %" PRIu64,
6871 0 : fip->field_name, (int)fip->field_readcount,
6872 : dp->tdir_count);
6873 0 : return (0);
6874 : }
6875 : else
6876 : {
6877 0 : err = TIFFReadDirEntryLong8Array(tif, dp, &data);
6878 0 : if (err == TIFFReadDirEntryErrOk)
6879 : {
6880 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6881 : {
6882 0 : if (data != 0)
6883 0 : _TIFFfreeExt(tif, data);
6884 0 : return 0;
6885 : }
6886 : int m;
6887 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6888 0 : if (data != 0)
6889 0 : _TIFFfreeExt(tif, data);
6890 0 : if (!m)
6891 0 : return (0);
6892 : }
6893 : }
6894 : }
6895 0 : break;
6896 0 : case TIFF_SETGET_C0_SINT64:
6897 : {
6898 : int64_t *data;
6899 0 : assert(fip->field_readcount >= 1);
6900 0 : assert(fip->field_passcount == 0);
6901 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6902 : {
6903 0 : TIFFWarningExtR(tif, module,
6904 : "incorrect count for field \"%s\", expected "
6905 : "%d, got %" PRIu64,
6906 0 : fip->field_name, (int)fip->field_readcount,
6907 : dp->tdir_count);
6908 0 : return (0);
6909 : }
6910 : else
6911 : {
6912 0 : err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
6913 0 : if (err == TIFFReadDirEntryErrOk)
6914 : {
6915 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6916 : {
6917 0 : if (data != 0)
6918 0 : _TIFFfreeExt(tif, data);
6919 0 : return 0;
6920 : }
6921 : int m;
6922 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6923 0 : if (data != 0)
6924 0 : _TIFFfreeExt(tif, data);
6925 0 : if (!m)
6926 0 : return (0);
6927 : }
6928 : }
6929 : }
6930 0 : break;
6931 1027 : case TIFF_SETGET_C0_FLOAT:
6932 : {
6933 : float *data;
6934 1027 : assert(fip->field_readcount >= 1);
6935 1027 : assert(fip->field_passcount == 0);
6936 1027 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6937 : {
6938 0 : TIFFWarningExtR(tif, module,
6939 : "incorrect count for field \"%s\", expected "
6940 : "%d, got %" PRIu64,
6941 0 : fip->field_name, (int)fip->field_readcount,
6942 : dp->tdir_count);
6943 0 : return (0);
6944 : }
6945 : else
6946 : {
6947 1027 : err = TIFFReadDirEntryFloatArray(tif, dp, &data);
6948 1027 : if (err == TIFFReadDirEntryErrOk)
6949 : {
6950 1027 : if (!EvaluateIFDdatasizeReading(tif, dp))
6951 : {
6952 0 : if (data != 0)
6953 0 : _TIFFfreeExt(tif, data);
6954 0 : return 0;
6955 : }
6956 : int m;
6957 1027 : m = TIFFSetField(tif, dp->tdir_tag, data);
6958 1027 : if (data != 0)
6959 1027 : _TIFFfreeExt(tif, data);
6960 1027 : if (!m)
6961 0 : return (0);
6962 : }
6963 : }
6964 : }
6965 1027 : break;
6966 : /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6967 : * into Double-Arrays. */
6968 0 : case TIFF_SETGET_C0_DOUBLE:
6969 : {
6970 : double *data;
6971 0 : assert(fip->field_readcount >= 1);
6972 0 : assert(fip->field_passcount == 0);
6973 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6974 : {
6975 0 : TIFFWarningExtR(tif, module,
6976 : "incorrect count for field \"%s\", expected "
6977 : "%d, got %" PRIu64,
6978 0 : fip->field_name, (int)fip->field_readcount,
6979 : dp->tdir_count);
6980 0 : return (0);
6981 : }
6982 : else
6983 : {
6984 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6985 0 : if (err == TIFFReadDirEntryErrOk)
6986 : {
6987 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6988 : {
6989 0 : if (data != 0)
6990 0 : _TIFFfreeExt(tif, data);
6991 0 : return 0;
6992 : }
6993 : int m;
6994 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6995 0 : if (data != 0)
6996 0 : _TIFFfreeExt(tif, data);
6997 0 : if (!m)
6998 0 : return (0);
6999 : }
7000 : }
7001 : }
7002 0 : break;
7003 0 : case TIFF_SETGET_C0_IFD8:
7004 : {
7005 : uint64_t *data;
7006 0 : assert(fip->field_readcount >= 1);
7007 0 : assert(fip->field_passcount == 0);
7008 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
7009 : {
7010 0 : TIFFWarningExtR(tif, module,
7011 : "incorrect count for field \"%s\", expected "
7012 : "%d, got %" PRIu64,
7013 0 : fip->field_name, (int)fip->field_readcount,
7014 : dp->tdir_count);
7015 0 : return (0);
7016 : }
7017 : else
7018 : {
7019 0 : err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7020 0 : if (err == TIFFReadDirEntryErrOk)
7021 : {
7022 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7023 : {
7024 0 : if (data != 0)
7025 0 : _TIFFfreeExt(tif, data);
7026 0 : return 0;
7027 : }
7028 : int m;
7029 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
7030 0 : if (data != 0)
7031 0 : _TIFFfreeExt(tif, data);
7032 0 : if (!m)
7033 0 : return (0);
7034 : }
7035 : }
7036 : }
7037 0 : break;
7038 0 : case TIFF_SETGET_C16_ASCII:
7039 : {
7040 : uint8_t *data;
7041 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7042 0 : assert(fip->field_passcount == 1);
7043 0 : if (dp->tdir_count > 0xFFFF)
7044 0 : err = TIFFReadDirEntryErrCount;
7045 : else
7046 : {
7047 0 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7048 0 : if (err == TIFFReadDirEntryErrOk)
7049 : {
7050 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7051 : {
7052 0 : if (data != 0)
7053 0 : _TIFFfreeExt(tif, data);
7054 0 : return 0;
7055 : }
7056 : int m;
7057 0 : if (data != 0 && dp->tdir_count > 0 &&
7058 0 : data[dp->tdir_count - 1] != '\0')
7059 : {
7060 0 : TIFFWarningExtR(tif, module,
7061 : "ASCII value for ASCII array tag "
7062 : "\"%s\" does not end in null "
7063 : "byte. Forcing it to be null",
7064 : fip->field_name);
7065 : /* Enlarge buffer and add terminating null. */
7066 0 : uint8_t *o = (uint8_t *)_TIFFmallocExt(
7067 0 : tif, (uint32_t)dp->tdir_count + 1);
7068 0 : if (o == NULL)
7069 : {
7070 0 : if (data != NULL)
7071 0 : _TIFFfreeExt(tif, data);
7072 0 : return (0);
7073 : }
7074 0 : if (dp->tdir_count > 0)
7075 : {
7076 0 : _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
7077 : }
7078 0 : o[(uint32_t)dp->tdir_count] = 0;
7079 0 : dp->tdir_count++; /* Increment for added null. */
7080 0 : if (data != 0)
7081 0 : _TIFFfreeExt(tif, data);
7082 0 : data = o;
7083 : }
7084 0 : m = TIFFSetField(tif, dp->tdir_tag,
7085 0 : (uint16_t)(dp->tdir_count), data);
7086 0 : if (data != 0)
7087 0 : _TIFFfreeExt(tif, data);
7088 0 : if (!m)
7089 0 : return (0);
7090 : }
7091 : }
7092 : }
7093 0 : break;
7094 0 : case TIFF_SETGET_C16_UINT8:
7095 : {
7096 : uint8_t *data;
7097 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7098 0 : assert(fip->field_passcount == 1);
7099 0 : if (dp->tdir_count > 0xFFFF)
7100 0 : err = TIFFReadDirEntryErrCount;
7101 : else
7102 : {
7103 0 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7104 0 : if (err == TIFFReadDirEntryErrOk)
7105 : {
7106 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7107 : {
7108 0 : if (data != 0)
7109 0 : _TIFFfreeExt(tif, data);
7110 0 : return 0;
7111 : }
7112 : int m;
7113 0 : m = TIFFSetField(tif, dp->tdir_tag,
7114 0 : (uint16_t)(dp->tdir_count), data);
7115 0 : if (data != 0)
7116 0 : _TIFFfreeExt(tif, data);
7117 0 : if (!m)
7118 0 : return (0);
7119 : }
7120 : }
7121 : }
7122 0 : break;
7123 0 : case TIFF_SETGET_C16_SINT8:
7124 : {
7125 : int8_t *data;
7126 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7127 0 : assert(fip->field_passcount == 1);
7128 0 : if (dp->tdir_count > 0xFFFF)
7129 0 : err = TIFFReadDirEntryErrCount;
7130 : else
7131 : {
7132 0 : err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7133 0 : if (err == TIFFReadDirEntryErrOk)
7134 : {
7135 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7136 : {
7137 0 : if (data != 0)
7138 0 : _TIFFfreeExt(tif, data);
7139 0 : return 0;
7140 : }
7141 : int m;
7142 0 : m = TIFFSetField(tif, dp->tdir_tag,
7143 0 : (uint16_t)(dp->tdir_count), data);
7144 0 : if (data != 0)
7145 0 : _TIFFfreeExt(tif, data);
7146 0 : if (!m)
7147 0 : return (0);
7148 : }
7149 : }
7150 : }
7151 0 : break;
7152 33974 : case TIFF_SETGET_C16_UINT16:
7153 : {
7154 : uint16_t *data;
7155 33974 : assert(fip->field_readcount == TIFF_VARIABLE);
7156 33974 : assert(fip->field_passcount == 1);
7157 33974 : if (dp->tdir_count > 0xFFFF)
7158 0 : err = TIFFReadDirEntryErrCount;
7159 : else
7160 : {
7161 33974 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
7162 33951 : if (err == TIFFReadDirEntryErrOk)
7163 : {
7164 33950 : if (!EvaluateIFDdatasizeReading(tif, dp))
7165 : {
7166 0 : if (data != 0)
7167 0 : _TIFFfreeExt(tif, data);
7168 0 : return 0;
7169 : }
7170 : int m;
7171 33956 : m = TIFFSetField(tif, dp->tdir_tag,
7172 33956 : (uint16_t)(dp->tdir_count), data);
7173 33965 : if (data != 0)
7174 33964 : _TIFFfreeExt(tif, data);
7175 33963 : if (!m)
7176 0 : return (0);
7177 : }
7178 : }
7179 : }
7180 33964 : break;
7181 0 : case TIFF_SETGET_C16_SINT16:
7182 : {
7183 : int16_t *data;
7184 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7185 0 : assert(fip->field_passcount == 1);
7186 0 : if (dp->tdir_count > 0xFFFF)
7187 0 : err = TIFFReadDirEntryErrCount;
7188 : else
7189 : {
7190 0 : err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7191 0 : if (err == TIFFReadDirEntryErrOk)
7192 : {
7193 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7194 : {
7195 0 : if (data != 0)
7196 0 : _TIFFfreeExt(tif, data);
7197 0 : return 0;
7198 : }
7199 : int m;
7200 0 : m = TIFFSetField(tif, dp->tdir_tag,
7201 0 : (uint16_t)(dp->tdir_count), data);
7202 0 : if (data != 0)
7203 0 : _TIFFfreeExt(tif, data);
7204 0 : if (!m)
7205 0 : return (0);
7206 : }
7207 : }
7208 : }
7209 0 : break;
7210 0 : case TIFF_SETGET_C16_UINT32:
7211 : {
7212 : uint32_t *data;
7213 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7214 0 : assert(fip->field_passcount == 1);
7215 0 : if (dp->tdir_count > 0xFFFF)
7216 0 : err = TIFFReadDirEntryErrCount;
7217 : else
7218 : {
7219 0 : err = TIFFReadDirEntryLongArray(tif, dp, &data);
7220 0 : if (err == TIFFReadDirEntryErrOk)
7221 : {
7222 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7223 : {
7224 0 : if (data != 0)
7225 0 : _TIFFfreeExt(tif, data);
7226 0 : return 0;
7227 : }
7228 : int m;
7229 0 : m = TIFFSetField(tif, dp->tdir_tag,
7230 0 : (uint16_t)(dp->tdir_count), data);
7231 0 : if (data != 0)
7232 0 : _TIFFfreeExt(tif, data);
7233 0 : if (!m)
7234 0 : return (0);
7235 : }
7236 : }
7237 : }
7238 0 : break;
7239 0 : case TIFF_SETGET_C16_SINT32:
7240 : {
7241 : int32_t *data;
7242 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7243 0 : assert(fip->field_passcount == 1);
7244 0 : if (dp->tdir_count > 0xFFFF)
7245 0 : err = TIFFReadDirEntryErrCount;
7246 : else
7247 : {
7248 0 : err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7249 0 : if (err == TIFFReadDirEntryErrOk)
7250 : {
7251 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7252 : {
7253 0 : if (data != 0)
7254 0 : _TIFFfreeExt(tif, data);
7255 0 : return 0;
7256 : }
7257 : int m;
7258 0 : m = TIFFSetField(tif, dp->tdir_tag,
7259 0 : (uint16_t)(dp->tdir_count), data);
7260 0 : if (data != 0)
7261 0 : _TIFFfreeExt(tif, data);
7262 0 : if (!m)
7263 0 : return (0);
7264 : }
7265 : }
7266 : }
7267 0 : break;
7268 0 : case TIFF_SETGET_C16_UINT64:
7269 : {
7270 : uint64_t *data;
7271 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7272 0 : assert(fip->field_passcount == 1);
7273 0 : if (dp->tdir_count > 0xFFFF)
7274 0 : err = TIFFReadDirEntryErrCount;
7275 : else
7276 : {
7277 0 : err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7278 0 : if (err == TIFFReadDirEntryErrOk)
7279 : {
7280 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7281 : {
7282 0 : if (data != 0)
7283 0 : _TIFFfreeExt(tif, data);
7284 0 : return 0;
7285 : }
7286 : int m;
7287 0 : m = TIFFSetField(tif, dp->tdir_tag,
7288 0 : (uint16_t)(dp->tdir_count), data);
7289 0 : if (data != 0)
7290 0 : _TIFFfreeExt(tif, data);
7291 0 : if (!m)
7292 0 : return (0);
7293 : }
7294 : }
7295 : }
7296 0 : break;
7297 0 : case TIFF_SETGET_C16_SINT64:
7298 : {
7299 : int64_t *data;
7300 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7301 0 : assert(fip->field_passcount == 1);
7302 0 : if (dp->tdir_count > 0xFFFF)
7303 0 : err = TIFFReadDirEntryErrCount;
7304 : else
7305 : {
7306 0 : err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7307 0 : if (err == TIFFReadDirEntryErrOk)
7308 : {
7309 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7310 : {
7311 0 : if (data != 0)
7312 0 : _TIFFfreeExt(tif, data);
7313 0 : return 0;
7314 : }
7315 : int m;
7316 0 : m = TIFFSetField(tif, dp->tdir_tag,
7317 0 : (uint16_t)(dp->tdir_count), data);
7318 0 : if (data != 0)
7319 0 : _TIFFfreeExt(tif, data);
7320 0 : if (!m)
7321 0 : return (0);
7322 : }
7323 : }
7324 : }
7325 0 : break;
7326 0 : case TIFF_SETGET_C16_FLOAT:
7327 : {
7328 : float *data;
7329 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7330 0 : assert(fip->field_passcount == 1);
7331 0 : if (dp->tdir_count > 0xFFFF)
7332 0 : err = TIFFReadDirEntryErrCount;
7333 : else
7334 : {
7335 0 : err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7336 0 : if (err == TIFFReadDirEntryErrOk)
7337 : {
7338 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7339 : {
7340 0 : if (data != 0)
7341 0 : _TIFFfreeExt(tif, data);
7342 0 : return 0;
7343 : }
7344 : int m;
7345 0 : m = TIFFSetField(tif, dp->tdir_tag,
7346 0 : (uint16_t)(dp->tdir_count), data);
7347 0 : if (data != 0)
7348 0 : _TIFFfreeExt(tif, data);
7349 0 : if (!m)
7350 0 : return (0);
7351 : }
7352 : }
7353 : }
7354 0 : break;
7355 70567 : case TIFF_SETGET_C16_DOUBLE:
7356 : {
7357 : double *data;
7358 70567 : assert(fip->field_readcount == TIFF_VARIABLE);
7359 70567 : assert(fip->field_passcount == 1);
7360 70567 : if (dp->tdir_count > 0xFFFF)
7361 0 : err = TIFFReadDirEntryErrCount;
7362 : else
7363 : {
7364 70567 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7365 70574 : if (err == TIFFReadDirEntryErrOk)
7366 : {
7367 70553 : if (!EvaluateIFDdatasizeReading(tif, dp))
7368 : {
7369 0 : if (data != 0)
7370 0 : _TIFFfreeExt(tif, data);
7371 0 : return 0;
7372 : }
7373 : int m;
7374 70563 : m = TIFFSetField(tif, dp->tdir_tag,
7375 70563 : (uint16_t)(dp->tdir_count), data);
7376 70561 : if (data != 0)
7377 70559 : _TIFFfreeExt(tif, data);
7378 70549 : if (!m)
7379 0 : return (0);
7380 : }
7381 : }
7382 : }
7383 70570 : break;
7384 81 : case TIFF_SETGET_C16_IFD8:
7385 : {
7386 : uint64_t *data;
7387 81 : assert(fip->field_readcount == TIFF_VARIABLE);
7388 81 : assert(fip->field_passcount == 1);
7389 81 : if (dp->tdir_count > 0xFFFF)
7390 0 : err = TIFFReadDirEntryErrCount;
7391 : else
7392 : {
7393 81 : err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7394 81 : if (err == TIFFReadDirEntryErrOk)
7395 : {
7396 81 : if (!EvaluateIFDdatasizeReading(tif, dp))
7397 : {
7398 0 : if (data != 0)
7399 0 : _TIFFfreeExt(tif, data);
7400 0 : return 0;
7401 : }
7402 : int m;
7403 81 : m = TIFFSetField(tif, dp->tdir_tag,
7404 81 : (uint16_t)(dp->tdir_count), data);
7405 81 : if (data != 0)
7406 81 : _TIFFfreeExt(tif, data);
7407 81 : if (!m)
7408 0 : return (0);
7409 : }
7410 : }
7411 : }
7412 81 : break;
7413 9 : case TIFF_SETGET_C32_ASCII:
7414 : {
7415 : uint8_t *data;
7416 9 : assert(fip->field_readcount == TIFF_VARIABLE2);
7417 9 : assert(fip->field_passcount == 1);
7418 9 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7419 9 : if (err == TIFFReadDirEntryErrOk)
7420 : {
7421 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7422 : {
7423 0 : if (data != 0)
7424 0 : _TIFFfreeExt(tif, data);
7425 0 : return 0;
7426 : }
7427 : int m;
7428 0 : if (data != 0 && dp->tdir_count > 0 &&
7429 0 : data[dp->tdir_count - 1] != '\0')
7430 : {
7431 0 : TIFFWarningExtR(
7432 : tif, module,
7433 : "ASCII value for ASCII array tag \"%s\" does not end "
7434 : "in null byte. Forcing it to be null",
7435 : fip->field_name);
7436 : /* Enlarge buffer and add terminating null. */
7437 0 : uint8_t *o = (uint8_t *)_TIFFmallocExt(
7438 0 : tif, (uint32_t)dp->tdir_count + 1);
7439 0 : if (o == NULL)
7440 : {
7441 0 : if (data != NULL)
7442 0 : _TIFFfreeExt(tif, data);
7443 0 : return (0);
7444 : }
7445 0 : if (dp->tdir_count > 0)
7446 : {
7447 0 : _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
7448 : }
7449 0 : o[(uint32_t)dp->tdir_count] = 0;
7450 0 : dp->tdir_count++; /* Increment for added null. */
7451 0 : if (data != 0)
7452 0 : _TIFFfreeExt(tif, data);
7453 0 : data = o;
7454 : }
7455 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7456 : data);
7457 0 : if (data != 0)
7458 0 : _TIFFfreeExt(tif, data);
7459 0 : if (!m)
7460 0 : return (0);
7461 : }
7462 : }
7463 9 : break;
7464 2089 : case TIFF_SETGET_C32_UINT8:
7465 : {
7466 : uint8_t *data;
7467 2089 : uint32_t count = 0;
7468 2089 : assert(fip->field_readcount == TIFF_VARIABLE2);
7469 2089 : assert(fip->field_passcount == 1);
7470 2089 : if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
7471 0 : dp->tdir_type == TIFF_LONG)
7472 0 : {
7473 : /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
7474 : * data type LONG instead of UNDEFINED. Work around this
7475 : * frequently found issue */
7476 : void *origdata;
7477 0 : err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
7478 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
7479 : {
7480 0 : data = NULL;
7481 : }
7482 : else
7483 : {
7484 0 : if (tif->tif_flags & TIFF_SWAB)
7485 0 : TIFFSwabArrayOfLong((uint32_t *)origdata, count);
7486 0 : data = (uint8_t *)origdata;
7487 0 : count = (uint32_t)(count * 4);
7488 : }
7489 : }
7490 : else
7491 : {
7492 2089 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7493 2089 : count = (uint32_t)(dp->tdir_count);
7494 : }
7495 2089 : if (err == TIFFReadDirEntryErrOk)
7496 : {
7497 2080 : if (!EvaluateIFDdatasizeReading(tif, dp))
7498 : {
7499 0 : if (data != 0)
7500 0 : _TIFFfreeExt(tif, data);
7501 0 : return 0;
7502 : }
7503 : int m;
7504 2080 : m = TIFFSetField(tif, dp->tdir_tag, count, data);
7505 2080 : if (data != 0)
7506 2080 : _TIFFfreeExt(tif, data);
7507 2080 : if (!m)
7508 0 : return (0);
7509 : }
7510 : }
7511 2089 : break;
7512 0 : case TIFF_SETGET_C32_SINT8:
7513 : {
7514 0 : int8_t *data = NULL;
7515 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7516 0 : assert(fip->field_passcount == 1);
7517 0 : err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7518 0 : if (err == TIFFReadDirEntryErrOk)
7519 : {
7520 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7521 : {
7522 0 : if (data != 0)
7523 0 : _TIFFfreeExt(tif, data);
7524 0 : return 0;
7525 : }
7526 : int m;
7527 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7528 : data);
7529 0 : if (data != 0)
7530 0 : _TIFFfreeExt(tif, data);
7531 0 : if (!m)
7532 0 : return (0);
7533 : }
7534 : }
7535 0 : break;
7536 12 : case TIFF_SETGET_C32_UINT16:
7537 : {
7538 : uint16_t *data;
7539 12 : assert(fip->field_readcount == TIFF_VARIABLE2);
7540 12 : assert(fip->field_passcount == 1);
7541 12 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
7542 12 : if (err == TIFFReadDirEntryErrOk)
7543 : {
7544 12 : if (!EvaluateIFDdatasizeReading(tif, dp))
7545 : {
7546 0 : if (data != 0)
7547 0 : _TIFFfreeExt(tif, data);
7548 0 : return 0;
7549 : }
7550 : int m;
7551 12 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7552 : data);
7553 12 : if (data != 0)
7554 12 : _TIFFfreeExt(tif, data);
7555 12 : if (!m)
7556 0 : return (0);
7557 : }
7558 : }
7559 12 : break;
7560 0 : case TIFF_SETGET_C32_SINT16:
7561 : {
7562 0 : int16_t *data = NULL;
7563 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7564 0 : assert(fip->field_passcount == 1);
7565 0 : err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7566 0 : if (err == TIFFReadDirEntryErrOk)
7567 : {
7568 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7569 : {
7570 0 : if (data != 0)
7571 0 : _TIFFfreeExt(tif, data);
7572 0 : return 0;
7573 : }
7574 : int m;
7575 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7576 : data);
7577 0 : if (data != 0)
7578 0 : _TIFFfreeExt(tif, data);
7579 0 : if (!m)
7580 0 : return (0);
7581 : }
7582 : }
7583 0 : break;
7584 801 : case TIFF_SETGET_C32_UINT32:
7585 : {
7586 : uint32_t *data;
7587 801 : assert(fip->field_readcount == TIFF_VARIABLE2);
7588 801 : assert(fip->field_passcount == 1);
7589 801 : err = TIFFReadDirEntryLongArray(tif, dp, &data);
7590 801 : if (err == TIFFReadDirEntryErrOk)
7591 : {
7592 792 : if (!EvaluateIFDdatasizeReading(tif, dp))
7593 : {
7594 0 : if (data != 0)
7595 0 : _TIFFfreeExt(tif, data);
7596 0 : return 0;
7597 : }
7598 : int m;
7599 792 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7600 : data);
7601 792 : if (data != 0)
7602 792 : _TIFFfreeExt(tif, data);
7603 792 : if (!m)
7604 0 : return (0);
7605 : }
7606 : }
7607 801 : break;
7608 0 : case TIFF_SETGET_C32_SINT32:
7609 : {
7610 0 : int32_t *data = NULL;
7611 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7612 0 : assert(fip->field_passcount == 1);
7613 0 : err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7614 0 : if (err == TIFFReadDirEntryErrOk)
7615 : {
7616 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7617 : {
7618 0 : if (data != 0)
7619 0 : _TIFFfreeExt(tif, data);
7620 0 : return 0;
7621 : }
7622 : int m;
7623 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7624 : data);
7625 0 : if (data != 0)
7626 0 : _TIFFfreeExt(tif, data);
7627 0 : if (!m)
7628 0 : return (0);
7629 : }
7630 : }
7631 0 : break;
7632 6 : case TIFF_SETGET_C32_UINT64:
7633 : {
7634 : uint64_t *data;
7635 6 : assert(fip->field_readcount == TIFF_VARIABLE2);
7636 6 : assert(fip->field_passcount == 1);
7637 6 : err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7638 6 : if (err == TIFFReadDirEntryErrOk)
7639 : {
7640 6 : if (!EvaluateIFDdatasizeReading(tif, dp))
7641 : {
7642 0 : if (data != 0)
7643 0 : _TIFFfreeExt(tif, data);
7644 0 : return 0;
7645 : }
7646 : int m;
7647 6 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7648 : data);
7649 6 : if (data != 0)
7650 6 : _TIFFfreeExt(tif, data);
7651 6 : if (!m)
7652 0 : return (0);
7653 : }
7654 : }
7655 6 : break;
7656 0 : case TIFF_SETGET_C32_SINT64:
7657 : {
7658 0 : int64_t *data = NULL;
7659 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7660 0 : assert(fip->field_passcount == 1);
7661 0 : err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7662 0 : if (err == TIFFReadDirEntryErrOk)
7663 : {
7664 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7665 : {
7666 0 : if (data != 0)
7667 0 : _TIFFfreeExt(tif, data);
7668 0 : return 0;
7669 : }
7670 : int m;
7671 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7672 : data);
7673 0 : if (data != 0)
7674 0 : _TIFFfreeExt(tif, data);
7675 0 : if (!m)
7676 0 : return (0);
7677 : }
7678 : }
7679 0 : break;
7680 0 : case TIFF_SETGET_C32_FLOAT:
7681 : {
7682 : float *data;
7683 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7684 0 : assert(fip->field_passcount == 1);
7685 0 : err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7686 0 : if (err == TIFFReadDirEntryErrOk)
7687 : {
7688 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7689 : {
7690 0 : if (data != 0)
7691 0 : _TIFFfreeExt(tif, data);
7692 0 : return 0;
7693 : }
7694 : int m;
7695 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7696 : data);
7697 0 : if (data != 0)
7698 0 : _TIFFfreeExt(tif, data);
7699 0 : if (!m)
7700 0 : return (0);
7701 : }
7702 : }
7703 0 : break;
7704 0 : case TIFF_SETGET_C32_DOUBLE:
7705 : {
7706 : double *data;
7707 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7708 0 : assert(fip->field_passcount == 1);
7709 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7710 0 : if (err == TIFFReadDirEntryErrOk)
7711 : {
7712 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7713 : {
7714 0 : if (data != 0)
7715 0 : _TIFFfreeExt(tif, data);
7716 0 : return 0;
7717 : }
7718 : int m;
7719 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7720 : data);
7721 0 : if (data != 0)
7722 0 : _TIFFfreeExt(tif, data);
7723 0 : if (!m)
7724 0 : return (0);
7725 : }
7726 : }
7727 0 : break;
7728 0 : case TIFF_SETGET_C32_IFD8:
7729 : {
7730 : uint64_t *data;
7731 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7732 0 : assert(fip->field_passcount == 1);
7733 0 : err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7734 0 : if (err == TIFFReadDirEntryErrOk)
7735 : {
7736 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7737 : {
7738 0 : if (data != 0)
7739 0 : _TIFFfreeExt(tif, data);
7740 0 : return 0;
7741 : }
7742 : int m;
7743 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7744 : data);
7745 0 : if (data != 0)
7746 0 : _TIFFfreeExt(tif, data);
7747 0 : if (!m)
7748 0 : return (0);
7749 : }
7750 : }
7751 0 : break;
7752 0 : case TIFF_SETGET_INT:
7753 : case TIFF_SETGET_C0_ASCII:
7754 : case TIFF_SETGET_OTHER:
7755 0 : assert(0); /* these should not arrive here */
7756 : break;
7757 8 : default:
7758 8 : assert(0); /* we should never get here */
7759 : break;
7760 : }
7761 494350 : if (err != TIFFReadDirEntryErrOk)
7762 : {
7763 36 : TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
7764 36 : return (0);
7765 : }
7766 494314 : return (1);
7767 : }
7768 :
7769 : /*
7770 : * Fetch a set of offsets or lengths.
7771 : * While this routine says "strips", in fact it's also used for tiles.
7772 : */
7773 84907 : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
7774 : uint64_t **lpp)
7775 : {
7776 : static const char module[] = "TIFFFetchStripThing";
7777 : enum TIFFReadDirEntryErr err;
7778 : uint64_t *data;
7779 84907 : err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
7780 84914 : if (err != TIFFReadDirEntryErrOk)
7781 : {
7782 2 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7783 2 : TIFFReadDirEntryOutputErr(tif, err, module,
7784 : fip ? fip->field_name : "unknown tagname", 0);
7785 2 : return (0);
7786 : }
7787 84912 : if (dir->tdir_count < (uint64_t)nstrips)
7788 : {
7789 : uint64_t *resizeddata;
7790 4 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7791 4 : const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
7792 4 : uint32_t max_nstrips = 1000000;
7793 4 : if (pszMax)
7794 0 : max_nstrips = (uint32_t)atoi(pszMax);
7795 4 : TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
7796 : fip ? fip->field_name : "unknown tagname",
7797 : (nstrips <= max_nstrips));
7798 :
7799 4 : if (nstrips > max_nstrips)
7800 : {
7801 0 : _TIFFfreeExt(tif, data);
7802 0 : return (0);
7803 : }
7804 :
7805 4 : const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
7806 4 : if (allocsize > 100 * 1024 * 1024)
7807 : {
7808 : /* Before allocating a huge amount of memory for corrupted files,
7809 : * check if size of requested memory is not greater than file size.
7810 : */
7811 0 : const uint64_t filesize = TIFFGetFileSize(tif);
7812 0 : if (allocsize > filesize)
7813 : {
7814 0 : TIFFWarningExtR(
7815 : tif, module,
7816 : "Requested memory size for StripArray of %" PRIu64
7817 : " is greater than filesize %" PRIu64
7818 : ". Memory not allocated",
7819 : allocsize, filesize);
7820 0 : _TIFFfreeExt(tif, data);
7821 0 : return (0);
7822 : }
7823 : }
7824 4 : resizeddata = (uint64_t *)_TIFFCheckMalloc(
7825 : tif, nstrips, sizeof(uint64_t), "for strip array");
7826 4 : if (resizeddata == 0)
7827 : {
7828 0 : _TIFFfreeExt(tif, data);
7829 0 : return (0);
7830 : }
7831 4 : if (dir->tdir_count)
7832 4 : _TIFFmemcpy(resizeddata, data,
7833 4 : (uint32_t)dir->tdir_count * sizeof(uint64_t));
7834 4 : _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
7835 4 : (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
7836 4 : _TIFFfreeExt(tif, data);
7837 16 : data = resizeddata;
7838 : }
7839 84924 : *lpp = data;
7840 84924 : return (1);
7841 : }
7842 :
7843 : /*
7844 : * Fetch and set the SubjectDistance EXIF tag.
7845 : */
7846 0 : static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
7847 : {
7848 : static const char module[] = "TIFFFetchSubjectDistance";
7849 : enum TIFFReadDirEntryErr err;
7850 : UInt64Aligned_t m;
7851 0 : m.l = 0;
7852 : assert(sizeof(double) == 8);
7853 : assert(sizeof(uint64_t) == 8);
7854 : assert(sizeof(uint32_t) == 4);
7855 0 : if (dir->tdir_count != 1)
7856 0 : err = TIFFReadDirEntryErrCount;
7857 0 : else if (dir->tdir_type != TIFF_RATIONAL)
7858 0 : err = TIFFReadDirEntryErrType;
7859 : else
7860 : {
7861 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
7862 : {
7863 : uint32_t offset;
7864 0 : offset = *(uint32_t *)(&dir->tdir_offset);
7865 0 : if (tif->tif_flags & TIFF_SWAB)
7866 0 : TIFFSwabLong(&offset);
7867 0 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
7868 : }
7869 : else
7870 : {
7871 0 : m.l = dir->tdir_offset.toff_long8;
7872 0 : err = TIFFReadDirEntryErrOk;
7873 : }
7874 : }
7875 0 : if (err == TIFFReadDirEntryErrOk)
7876 : {
7877 : double n;
7878 0 : if (tif->tif_flags & TIFF_SWAB)
7879 0 : TIFFSwabArrayOfLong(m.i, 2);
7880 0 : if (m.i[0] == 0)
7881 0 : n = 0.0;
7882 0 : else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
7883 : /*
7884 : * XXX: Numerator 0xFFFFFFFF means that we have infinite
7885 : * distance. Indicate that with a negative floating point
7886 : * SubjectDistance value.
7887 : */
7888 0 : n = -1.0;
7889 : else
7890 0 : n = (double)m.i[0] / (double)m.i[1];
7891 0 : return (TIFFSetField(tif, dir->tdir_tag, n));
7892 : }
7893 : else
7894 : {
7895 0 : TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
7896 0 : return (0);
7897 : }
7898 : }
7899 :
7900 159 : static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
7901 : uint64_t stripbytes,
7902 : uint32_t rowsperstrip)
7903 : {
7904 159 : TIFFDirectory *td = &tif->tif_dir;
7905 : uint64_t bytecount;
7906 : uint64_t offset;
7907 : uint64_t last_offset;
7908 : uint64_t last_bytecount;
7909 : uint32_t i;
7910 : uint64_t *newcounts;
7911 : uint64_t *newoffsets;
7912 :
7913 159 : offset = TIFFGetStrileOffset(tif, 0);
7914 159 : last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
7915 159 : last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
7916 159 : if (last_offset > UINT64_MAX - last_bytecount ||
7917 159 : last_offset + last_bytecount < offset)
7918 : {
7919 0 : return;
7920 : }
7921 159 : bytecount = last_offset + last_bytecount - offset;
7922 :
7923 : /* Before allocating a huge amount of memory for corrupted files, check if
7924 : * size of StripByteCount and StripOffset tags is not greater than
7925 : * file size.
7926 : */
7927 159 : const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
7928 159 : if (allocsize > 100 * 1024 * 1024)
7929 : {
7930 0 : const uint64_t filesize = TIFFGetFileSize(tif);
7931 0 : if (allocsize > filesize)
7932 : {
7933 0 : TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
7934 : "Requested memory size for StripByteCount and "
7935 : "StripOffsets %" PRIu64
7936 : " is greater than filesize %" PRIu64
7937 : ". Memory not allocated",
7938 : allocsize, filesize);
7939 0 : return;
7940 : }
7941 : }
7942 :
7943 : newcounts =
7944 159 : (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
7945 : "for chopped \"StripByteCounts\" array");
7946 159 : newoffsets = (uint64_t *)_TIFFCheckMalloc(
7947 : tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7948 159 : if (newcounts == NULL || newoffsets == NULL)
7949 : {
7950 : /*
7951 : * Unable to allocate new strip information, give up and use
7952 : * the original one strip information.
7953 : */
7954 0 : if (newcounts != NULL)
7955 0 : _TIFFfreeExt(tif, newcounts);
7956 0 : if (newoffsets != NULL)
7957 0 : _TIFFfreeExt(tif, newoffsets);
7958 0 : return;
7959 : }
7960 :
7961 : /*
7962 : * Fill the strip information arrays with new bytecounts and offsets
7963 : * that reflect the broken-up format.
7964 : */
7965 106924 : for (i = 0; i < nstrips; i++)
7966 : {
7967 106765 : if (stripbytes > bytecount)
7968 53 : stripbytes = bytecount;
7969 106765 : newcounts[i] = stripbytes;
7970 106765 : newoffsets[i] = stripbytes ? offset : 0;
7971 106765 : offset += stripbytes;
7972 106765 : bytecount -= stripbytes;
7973 : }
7974 :
7975 : /*
7976 : * Replace old single strip info with multi-strip info.
7977 : */
7978 159 : td->td_stripsperimage = td->td_nstrips = nstrips;
7979 159 : TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
7980 :
7981 159 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
7982 159 : _TIFFfreeExt(tif, td->td_stripoffset_p);
7983 159 : td->td_stripbytecount_p = newcounts;
7984 159 : td->td_stripoffset_p = newoffsets;
7985 : #ifdef STRIPBYTECOUNTSORTED_UNUSED
7986 : td->td_stripbytecountsorted = 1;
7987 : #endif
7988 159 : tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
7989 : }
7990 :
7991 : /*
7992 : * Replace a single strip (tile) of uncompressed data by multiple strips
7993 : * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
7994 : * dealing with large images or for dealing with machines with a limited
7995 : * amount memory.
7996 : */
7997 22108 : static void ChopUpSingleUncompressedStrip(TIFF *tif)
7998 : {
7999 22108 : TIFFDirectory *td = &tif->tif_dir;
8000 : uint64_t bytecount;
8001 : uint64_t offset;
8002 : uint32_t rowblock;
8003 : uint64_t rowblockbytes;
8004 : uint64_t stripbytes;
8005 : uint32_t nstrips;
8006 : uint32_t rowsperstrip;
8007 :
8008 22108 : bytecount = TIFFGetStrileByteCount(tif, 0);
8009 : /* On a newly created file, just re-opened to be filled, we */
8010 : /* don't want strip chop to trigger as it is going to cause issues */
8011 : /* later ( StripOffsets and StripByteCounts improperly filled) . */
8012 22098 : if (bytecount == 0 && tif->tif_mode != O_RDONLY)
8013 555 : return;
8014 21543 : offset = TIFFGetStrileByteCount(tif, 0);
8015 21542 : assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
8016 21537 : if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
8017 1 : rowblock = td->td_ycbcrsubsampling[1];
8018 : else
8019 21536 : rowblock = 1;
8020 21537 : rowblockbytes = TIFFVTileSize64(tif, rowblock);
8021 : /*
8022 : * Make the rows hold at least one scanline, but fill specified amount
8023 : * of data if possible.
8024 : */
8025 21538 : if (rowblockbytes > STRIP_SIZE_DEFAULT)
8026 : {
8027 23 : stripbytes = rowblockbytes;
8028 23 : rowsperstrip = rowblock;
8029 : }
8030 21515 : else if (rowblockbytes > 0)
8031 : {
8032 : uint32_t rowblocksperstrip;
8033 21506 : rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
8034 21506 : rowsperstrip = rowblocksperstrip * rowblock;
8035 21506 : stripbytes = rowblocksperstrip * rowblockbytes;
8036 : }
8037 : else
8038 9 : return;
8039 :
8040 : /*
8041 : * never increase the number of rows per strip
8042 : */
8043 21529 : if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
8044 21371 : return;
8045 158 : nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
8046 158 : if (nstrips == 0)
8047 0 : return;
8048 :
8049 : /* If we are going to allocate a lot of memory, make sure that the */
8050 : /* file is as big as needed */
8051 159 : if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
8052 1 : (offset >= TIFFGetFileSize(tif) ||
8053 0 : stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
8054 : {
8055 1 : return;
8056 : }
8057 :
8058 157 : allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
8059 : }
8060 :
8061 : /*
8062 : * Replace a file with contiguous strips > 2 GB of uncompressed data by
8063 : * multiple smaller strips. This is useful for
8064 : * dealing with large images or for dealing with machines with a limited
8065 : * amount memory.
8066 : */
8067 5 : static void TryChopUpUncompressedBigTiff(TIFF *tif)
8068 : {
8069 5 : TIFFDirectory *td = &tif->tif_dir;
8070 : uint32_t rowblock;
8071 : uint64_t rowblockbytes;
8072 : uint32_t i;
8073 : uint64_t stripsize;
8074 : uint32_t rowblocksperstrip;
8075 : uint32_t rowsperstrip;
8076 : uint64_t stripbytes;
8077 : uint32_t nstrips;
8078 :
8079 5 : stripsize = TIFFStripSize64(tif);
8080 :
8081 5 : assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
8082 5 : assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
8083 5 : assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
8084 : TIFF_STRIPCHOP);
8085 5 : assert(stripsize > 0x7FFFFFFFUL);
8086 :
8087 : /* On a newly created file, just re-opened to be filled, we */
8088 : /* don't want strip chop to trigger as it is going to cause issues */
8089 : /* later ( StripOffsets and StripByteCounts improperly filled) . */
8090 5 : if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
8091 0 : return;
8092 :
8093 5 : if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
8094 0 : rowblock = td->td_ycbcrsubsampling[1];
8095 : else
8096 5 : rowblock = 1;
8097 5 : rowblockbytes = TIFFVStripSize64(tif, rowblock);
8098 5 : if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
8099 : {
8100 : /* In case of file with gigantic width */
8101 3 : return;
8102 : }
8103 :
8104 : /* Check that the strips are contiguous and of the expected size */
8105 5 : for (i = 0; i < td->td_nstrips; i++)
8106 : {
8107 3 : if (i == td->td_nstrips - 1)
8108 : {
8109 4 : if (TIFFGetStrileByteCount(tif, i) <
8110 2 : TIFFVStripSize64(tif,
8111 2 : td->td_imagelength - i * td->td_rowsperstrip))
8112 : {
8113 0 : return;
8114 : }
8115 : }
8116 : else
8117 : {
8118 1 : if (TIFFGetStrileByteCount(tif, i) != stripsize)
8119 : {
8120 0 : return;
8121 : }
8122 1 : if (i > 0 && TIFFGetStrileOffset(tif, i) !=
8123 0 : TIFFGetStrileOffset(tif, i - 1) +
8124 0 : TIFFGetStrileByteCount(tif, i - 1))
8125 : {
8126 0 : return;
8127 : }
8128 : }
8129 : }
8130 :
8131 : /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
8132 2 : rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
8133 2 : if (rowblocksperstrip == 0)
8134 0 : rowblocksperstrip = 1;
8135 2 : rowsperstrip = rowblocksperstrip * rowblock;
8136 2 : stripbytes = rowblocksperstrip * rowblockbytes;
8137 2 : assert(stripbytes <= 0x7FFFFFFFUL);
8138 :
8139 2 : if (rowsperstrip == 0)
8140 0 : return;
8141 2 : nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
8142 2 : if (nstrips == 0)
8143 0 : return;
8144 :
8145 : /* If we are going to allocate a lot of memory, make sure that the */
8146 : /* file is as big as needed */
8147 2 : if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
8148 : {
8149 0 : uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
8150 0 : uint64_t filesize = TIFFGetFileSize(tif);
8151 : uint64_t last_bytecount =
8152 0 : TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
8153 0 : if (last_offset > filesize || last_bytecount > filesize - last_offset)
8154 : {
8155 0 : return;
8156 : }
8157 : }
8158 :
8159 2 : allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
8160 : }
8161 :
8162 : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
8163 6336690 : static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
8164 : {
8165 6336690 : return a + (uint64_t)b;
8166 : }
8167 :
8168 : /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
8169 : * strip/tile of number strile. Also fetch the neighbouring values using a
8170 : * 4096 byte page size.
8171 : */
8172 6612 : static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent,
8173 : int strile, uint64_t *panVals)
8174 : {
8175 : static const char module[] = "_TIFFPartialReadStripArray";
8176 : #define IO_CACHE_PAGE_SIZE 4096
8177 :
8178 : size_t sizeofval;
8179 6612 : const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
8180 : int sizeofvalint;
8181 : uint64_t nBaseOffset;
8182 : uint64_t nOffset;
8183 : uint64_t nOffsetStartPage;
8184 : uint64_t nOffsetEndPage;
8185 : tmsize_t nToRead;
8186 : tmsize_t nRead;
8187 : uint64_t nLastStripOffset;
8188 : int iStartBefore;
8189 : int i;
8190 6612 : const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
8191 : unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
8192 :
8193 6612 : assert(dirent->tdir_count > 4);
8194 :
8195 6612 : if (dirent->tdir_type == TIFF_SHORT)
8196 : {
8197 1967 : sizeofval = sizeof(uint16_t);
8198 : }
8199 4645 : else if (dirent->tdir_type == TIFF_LONG)
8200 : {
8201 4610 : sizeofval = sizeof(uint32_t);
8202 : }
8203 35 : else if (dirent->tdir_type == TIFF_LONG8)
8204 : {
8205 35 : sizeofval = sizeof(uint64_t);
8206 : }
8207 0 : else if (dirent->tdir_type == TIFF_SLONG8)
8208 : {
8209 : /* Non conformant but used by some images as in */
8210 : /* https://github.com/OSGeo/gdal/issues/2165 */
8211 0 : sizeofval = sizeof(int64_t);
8212 : }
8213 : else
8214 : {
8215 0 : TIFFErrorExtR(tif, module,
8216 : "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
8217 0 : panVals[strile] = 0;
8218 0 : return 0;
8219 : }
8220 6612 : sizeofvalint = (int)(sizeofval);
8221 :
8222 6612 : if (tif->tif_flags & TIFF_BIGTIFF)
8223 : {
8224 64 : uint64_t offset = dirent->tdir_offset.toff_long8;
8225 64 : if (bSwab)
8226 0 : TIFFSwabLong8(&offset);
8227 64 : nBaseOffset = offset;
8228 : }
8229 : else
8230 : {
8231 6548 : uint32_t offset = dirent->tdir_offset.toff_long;
8232 6548 : if (bSwab)
8233 88 : TIFFSwabLong(&offset);
8234 6548 : nBaseOffset = offset;
8235 : }
8236 : /* To avoid later unsigned integer overflows */
8237 6612 : if (nBaseOffset > (uint64_t)INT64_MAX)
8238 : {
8239 0 : TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
8240 : strile);
8241 0 : panVals[strile] = 0;
8242 0 : return 0;
8243 : }
8244 6612 : nOffset = nBaseOffset + (uint64_t)sizeofval * (uint64_t)strile;
8245 6612 : nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
8246 6612 : nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
8247 :
8248 6612 : if (nOffset + sizeofval > nOffsetEndPage)
8249 1978 : nOffsetEndPage += IO_CACHE_PAGE_SIZE;
8250 : #undef IO_CACHE_PAGE_SIZE
8251 :
8252 6612 : nLastStripOffset = nBaseOffset + (uint64_t)arraySize * sizeofval;
8253 6612 : if (nLastStripOffset < nOffsetEndPage)
8254 3565 : nOffsetEndPage = nLastStripOffset;
8255 6612 : if (nOffsetStartPage >= nOffsetEndPage)
8256 : {
8257 0 : TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
8258 : strile);
8259 0 : panVals[strile] = 0;
8260 0 : return 0;
8261 : }
8262 6612 : if (!SeekOK(tif, nOffsetStartPage))
8263 : {
8264 0 : panVals[strile] = 0;
8265 0 : return 0;
8266 : }
8267 :
8268 6612 : nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
8269 6612 : nRead = TIFFReadFile(tif, buffer, nToRead);
8270 6612 : if (nRead < nToRead)
8271 : {
8272 2 : TIFFErrorExtR(tif, module,
8273 : "Cannot read offset/size for strile around ~%d", strile);
8274 2 : return 0;
8275 : }
8276 6610 : iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
8277 6610 : if (strile + iStartBefore < 0)
8278 3595 : iStartBefore = -strile;
8279 6610 : for (i = iStartBefore;
8280 12677100 : (uint32_t)(strile + i) < arraySize &&
8281 6336720 : _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
8282 : nOffsetEndPage;
8283 6333690 : ++i)
8284 : {
8285 6333730 : if (dirent->tdir_type == TIFF_SHORT)
8286 : {
8287 : uint16_t val;
8288 2131190 : memcpy(&val,
8289 2131190 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8290 : sizeof(val));
8291 2131190 : if (bSwab)
8292 5220 : TIFFSwabShort(&val);
8293 2131190 : panVals[strile + i] = val;
8294 : }
8295 4202540 : else if (dirent->tdir_type == TIFF_LONG)
8296 : {
8297 : uint32_t val;
8298 4201030 : memcpy(&val,
8299 4201030 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8300 : sizeof(val));
8301 4201030 : if (bSwab)
8302 5528 : TIFFSwabLong(&val);
8303 4201020 : panVals[strile + i] = val;
8304 : }
8305 1507 : else if (dirent->tdir_type == TIFF_LONG8)
8306 : {
8307 : uint64_t val;
8308 1479 : memcpy(&val,
8309 1479 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8310 : sizeof(val));
8311 1479 : if (bSwab)
8312 0 : TIFFSwabLong8(&val);
8313 1479 : panVals[strile + i] = val;
8314 : }
8315 : else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
8316 : {
8317 : /* Non conformant data type */
8318 : int64_t val;
8319 28 : memcpy(&val,
8320 28 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8321 : sizeof(val));
8322 28 : if (bSwab)
8323 0 : TIFFSwabLong8((uint64_t *)&val);
8324 0 : panVals[strile + i] = (uint64_t)val;
8325 : }
8326 : }
8327 6620 : return 1;
8328 : }
8329 :
8330 6395610 : static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
8331 : TIFFDirEntry *dirent, uint64_t **parray)
8332 : {
8333 : static const char module[] = "_TIFFFetchStrileValue";
8334 6395610 : TIFFDirectory *td = &tif->tif_dir;
8335 6395610 : if (strile >= dirent->tdir_count)
8336 : {
8337 0 : return 0;
8338 : }
8339 6395610 : if (strile >= td->td_stripoffsetbyteallocsize)
8340 : {
8341 1801 : uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
8342 : uint32_t nStripArrayAllocNew;
8343 : uint64_t nArraySize64;
8344 : size_t nArraySize;
8345 : uint64_t *offsetArray;
8346 : uint64_t *bytecountArray;
8347 :
8348 1801 : if (strile > 1000000)
8349 : {
8350 3 : uint64_t filesize = TIFFGetFileSize(tif);
8351 : /* Avoid excessive memory allocation attempt */
8352 : /* For such a big blockid we need at least a TIFF_LONG per strile */
8353 : /* for the offset array. */
8354 3 : if (strile > filesize / sizeof(uint32_t))
8355 : {
8356 2 : TIFFErrorExtR(tif, module, "File too short");
8357 2 : return 0;
8358 : }
8359 : }
8360 :
8361 1799 : if (td->td_stripoffsetbyteallocsize == 0 &&
8362 1798 : td->td_nstrips < 1024 * 1024)
8363 : {
8364 1797 : nStripArrayAllocNew = td->td_nstrips;
8365 : }
8366 : else
8367 : {
8368 : #define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
8369 : #define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
8370 2 : nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
8371 2 : if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
8372 2 : nStripArrayAllocNew *= 2;
8373 2 : nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
8374 : }
8375 1799 : assert(strile < nStripArrayAllocNew);
8376 1799 : nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
8377 1799 : nArraySize = (size_t)(nArraySize64);
8378 : #if SIZEOF_SIZE_T == 4
8379 : if (nArraySize != nArraySize64)
8380 : {
8381 : TIFFErrorExtR(tif, module,
8382 : "Cannot allocate strip offset and bytecount arrays");
8383 : return 0;
8384 : }
8385 : #endif
8386 1799 : offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
8387 : (tmsize_t)nArraySize));
8388 1798 : bytecountArray = (uint64_t *)(_TIFFreallocExt(
8389 1798 : tif, td->td_stripbytecount_p, (tmsize_t)nArraySize));
8390 1799 : if (offsetArray)
8391 1799 : td->td_stripoffset_p = offsetArray;
8392 1799 : if (bytecountArray)
8393 1799 : td->td_stripbytecount_p = bytecountArray;
8394 1799 : if (offsetArray && bytecountArray)
8395 : {
8396 1799 : td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
8397 : /* Initialize new entries to ~0 / -1 */
8398 : /* coverity[overrun-buffer-arg] */
8399 1799 : memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
8400 1799 : (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
8401 : sizeof(uint64_t));
8402 : /* coverity[overrun-buffer-arg] */
8403 1799 : memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
8404 1799 : (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
8405 : sizeof(uint64_t));
8406 : }
8407 : else
8408 : {
8409 0 : TIFFErrorExtR(tif, module,
8410 : "Cannot allocate strip offset and bytecount arrays");
8411 0 : _TIFFfreeExt(tif, td->td_stripoffset_p);
8412 0 : td->td_stripoffset_p = NULL;
8413 0 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
8414 0 : td->td_stripbytecount_p = NULL;
8415 0 : td->td_stripoffsetbyteallocsize = 0;
8416 : }
8417 : }
8418 6395600 : if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
8419 0 : return 0;
8420 :
8421 6395600 : if (~((*parray)[strile]) == 0)
8422 : {
8423 6612 : if (!_TIFFPartialReadStripArray(tif, dirent, (int)strile, *parray))
8424 : {
8425 2 : (*parray)[strile] = 0;
8426 2 : return 0;
8427 : }
8428 : }
8429 :
8430 6395600 : return 1;
8431 : }
8432 :
8433 6899820 : static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
8434 : TIFFDirEntry *dirent,
8435 : uint64_t **parray,
8436 : int *pbErr)
8437 : {
8438 6899820 : TIFFDirectory *td = &tif->tif_dir;
8439 6899820 : if (pbErr)
8440 4327760 : *pbErr = 0;
8441 :
8442 : /* Check that StripOffsets and StripByteCounts tags have the same number
8443 : * of declared entries. Otherwise we might take the "dirent->tdir_count <=
8444 : * 4" code path for one of them, and the other code path for the other one,
8445 : * which will lead to inconsistencies and potential out-of-bounds reads.
8446 : */
8447 6899820 : if (td->td_stripoffset_entry.tdir_count !=
8448 6899820 : td->td_stripbytecount_entry.tdir_count)
8449 : {
8450 23 : if (pbErr)
8451 3 : *pbErr = 1;
8452 23 : return 0;
8453 : }
8454 :
8455 6899790 : if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
8456 6730310 : !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
8457 : {
8458 6723770 : if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) ||
8459 : /* If the values may fit in the toff_long/toff_long8 member */
8460 : /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
8461 6713330 : dirent->tdir_count <= 4)
8462 : {
8463 328166 : if (!_TIFFFillStriles(tif))
8464 : {
8465 2 : if (pbErr)
8466 2 : *pbErr = 1;
8467 : /* Do not return, as we want this function to always */
8468 : /* return the same value if called several times with */
8469 : /* the same arguments */
8470 : }
8471 : }
8472 : else
8473 : {
8474 6395610 : if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
8475 : {
8476 4 : if (pbErr)
8477 4 : *pbErr = 1;
8478 4 : return 0;
8479 : }
8480 : }
8481 : }
8482 6899770 : if (*parray == NULL || strile >= td->td_nstrips)
8483 : {
8484 8 : if (pbErr)
8485 2 : *pbErr = 1;
8486 8 : return 0;
8487 : }
8488 6899760 : return (*parray)[strile];
8489 : }
8490 :
8491 : /* Return the value of the TileOffsets/StripOffsets array for the specified
8492 : * tile/strile */
8493 2166980 : uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
8494 : {
8495 2166980 : return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
8496 : }
8497 :
8498 : /* Return the value of the TileOffsets/StripOffsets array for the specified
8499 : * tile/strile */
8500 4309010 : uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8501 : {
8502 4309010 : TIFFDirectory *td = &tif->tif_dir;
8503 4309010 : return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
8504 : &(td->td_stripoffset_entry),
8505 : &(td->td_stripoffset_p), pbErr);
8506 : }
8507 :
8508 : /* Return the value of the TileByteCounts/StripByteCounts array for the
8509 : * specified tile/strile */
8510 405103 : uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
8511 : {
8512 405103 : return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
8513 : }
8514 :
8515 : /* Return the value of the TileByteCounts/StripByteCounts array for the
8516 : * specified tile/strile */
8517 2590820 : uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8518 : {
8519 2590820 : TIFFDirectory *td = &tif->tif_dir;
8520 2590820 : return _TIFFGetStrileOffsetOrByteCountValue(
8521 : tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
8522 : pbErr);
8523 : }
8524 :
8525 732393 : int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
8526 :
8527 732405 : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
8528 : {
8529 732405 : TIFFDirectory *td = &tif->tif_dir;
8530 732405 : int return_value = 1;
8531 :
8532 : /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
8533 732405 : if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
8534 350562 : (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
8535 381849 : return 1;
8536 :
8537 350556 : if ((tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) &&
8538 321272 : !(tif->tif_flags & TIFF_LAZYSTRILELOAD_DONE))
8539 : {
8540 : /* In case of lazy loading, reload completely the arrays */
8541 23436 : _TIFFfreeExt(tif, td->td_stripoffset_p);
8542 23448 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
8543 23443 : td->td_stripoffset_p = NULL;
8544 23443 : td->td_stripbytecount_p = NULL;
8545 23443 : td->td_stripoffsetbyteallocsize = 0;
8546 23443 : tif->tif_flags |= TIFF_LAZYSTRILELOAD_DONE;
8547 : }
8548 :
8549 : /* If stripoffset array is already loaded, exit with success */
8550 350563 : if (td->td_stripoffset_p != NULL)
8551 325628 : return 1;
8552 :
8553 : /* If tdir_count was canceled, then we already got there, but in error */
8554 24935 : if (td->td_stripoffset_entry.tdir_count == 0)
8555 129 : return 0;
8556 :
8557 24806 : if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
8558 : &td->td_stripoffset_p))
8559 : {
8560 1 : return_value = 0;
8561 : }
8562 :
8563 49596 : if (loadStripByteCount &&
8564 24782 : !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
8565 : td->td_nstrips, &td->td_stripbytecount_p))
8566 : {
8567 1 : return_value = 0;
8568 : }
8569 :
8570 24814 : _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
8571 24814 : _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
8572 :
8573 : #ifdef STRIPBYTECOUNTSORTED_UNUSED
8574 : if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
8575 : {
8576 : uint32_t strip;
8577 :
8578 : tif->tif_dir.td_stripbytecountsorted = 1;
8579 : for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
8580 : {
8581 : if (tif->tif_dir.td_stripoffset_p[strip - 1] >
8582 : tif->tif_dir.td_stripoffset_p[strip])
8583 : {
8584 : tif->tif_dir.td_stripbytecountsorted = 0;
8585 : break;
8586 : }
8587 : }
8588 : }
8589 : #endif
8590 :
8591 24807 : return return_value;
8592 : }
|