Debian Codegraph @

frmts/gtiff/libtiff/tif_dirread.c

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