Line data Source code
1 : /*
2 : * Copyright (c) 1988-1997 Sam Leffler
3 : * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 : *
5 : * Permission to use, copy, modify, distribute, and sell this software and
6 : * its documentation for any purpose is hereby granted without fee, provided
7 : * that (i) the above copyright notices and this permission notice appear in
8 : * all copies of the software and related documentation, and (ii) the names of
9 : * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 : * publicity relating to the software without the specific, prior written
11 : * permission of Sam Leffler and Silicon Graphics.
12 : *
13 : * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 : * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 : * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 : *
17 : * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 : * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 : * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 : * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 : * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 : * OF THIS SOFTWARE.
23 : */
24 :
25 : /*
26 : * TIFF Library.
27 : *
28 : * Directory Read Support Routines.
29 : */
30 :
31 : /* Suggested pending improvements:
32 : * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33 : * the pointer to the appropriate TIFFField structure early on in
34 : * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
35 : */
36 :
37 : #include "tiffconf.h"
38 : #include "tiffiop.h"
39 : #include <float.h>
40 : #include <limits.h>
41 : #include <stdlib.h>
42 : #include <string.h>
43 :
44 : #define FAILED_FII ((uint32_t)-1)
45 :
46 : #ifdef HAVE_IEEEFP
47 : #define TIFFCvtIEEEFloatToNative(tif, n, fp)
48 : #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
49 : #else
50 : /* If your machine does not support IEEE floating point then you will need to
51 : * add support to tif_machdep.c to convert between the native format and
52 : * IEEE format. */
53 : extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *);
54 : extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *);
55 : #endif
56 :
57 : enum TIFFReadDirEntryErr
58 : {
59 : TIFFReadDirEntryErrOk = 0,
60 : TIFFReadDirEntryErrCount = 1,
61 : TIFFReadDirEntryErrType = 2,
62 : TIFFReadDirEntryErrIo = 3,
63 : TIFFReadDirEntryErrRange = 4,
64 : TIFFReadDirEntryErrPsdif = 5,
65 : TIFFReadDirEntryErrSizesan = 6,
66 : TIFFReadDirEntryErrAlloc = 7,
67 : };
68 :
69 : static enum TIFFReadDirEntryErr
70 : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value);
71 : static enum TIFFReadDirEntryErr
72 : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value);
73 : static enum TIFFReadDirEntryErr
74 : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value);
75 : static enum TIFFReadDirEntryErr
76 : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value);
77 : static enum TIFFReadDirEntryErr
78 : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value);
79 : static enum TIFFReadDirEntryErr
80 : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value);
81 : static enum TIFFReadDirEntryErr
82 : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
83 : static enum TIFFReadDirEntryErr
84 : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value);
85 : static enum TIFFReadDirEntryErr
86 : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value);
87 : static enum TIFFReadDirEntryErr
88 : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
89 : static enum TIFFReadDirEntryErr
90 : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
91 :
92 : static enum TIFFReadDirEntryErr
93 : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
94 : uint32_t desttypesize, void **value);
95 : static enum TIFFReadDirEntryErr
96 : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value);
97 : static enum TIFFReadDirEntryErr
98 : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value);
99 : static enum TIFFReadDirEntryErr
100 : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value);
101 : static enum TIFFReadDirEntryErr
102 : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value);
103 : static enum TIFFReadDirEntryErr
104 : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value);
105 : static enum TIFFReadDirEntryErr
106 : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value);
107 : static enum TIFFReadDirEntryErr
108 : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
109 : static enum TIFFReadDirEntryErr
110 : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value);
111 : static enum TIFFReadDirEntryErr
112 : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value);
113 : static enum TIFFReadDirEntryErr
114 : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value);
115 : static enum TIFFReadDirEntryErr
116 : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
117 :
118 : static enum TIFFReadDirEntryErr
119 : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
120 : uint16_t *value);
121 :
122 : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
123 : uint8_t *value);
124 : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
125 : int8_t *value);
126 : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
127 : uint16_t *value);
128 : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
129 : int16_t *value);
130 : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
131 : uint32_t *value);
132 : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
133 : int32_t *value);
134 : static enum TIFFReadDirEntryErr
135 : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry,
136 : uint64_t *value);
137 : static enum TIFFReadDirEntryErr
138 : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry,
139 : int64_t *value);
140 : static enum TIFFReadDirEntryErr
141 : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
142 : double *value);
143 : static enum TIFFReadDirEntryErr
144 : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
145 : double *value);
146 : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
147 : float *value);
148 : static enum TIFFReadDirEntryErr
149 : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
150 : #if 0
151 : static enum TIFFReadDirEntryErr
152 : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
153 : TIFFRational_t *value);
154 : #endif
155 : static enum TIFFReadDirEntryErr
156 : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
157 : static enum TIFFReadDirEntryErr
158 : TIFFReadDirEntryCheckRangeByteShort(uint16_t value);
159 : static enum TIFFReadDirEntryErr
160 : TIFFReadDirEntryCheckRangeByteSshort(int16_t value);
161 : static enum TIFFReadDirEntryErr
162 : TIFFReadDirEntryCheckRangeByteLong(uint32_t value);
163 : static enum TIFFReadDirEntryErr
164 : TIFFReadDirEntryCheckRangeByteSlong(int32_t value);
165 : static enum TIFFReadDirEntryErr
166 : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value);
167 : static enum TIFFReadDirEntryErr
168 : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value);
169 :
170 : static enum TIFFReadDirEntryErr
171 : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value);
172 : static enum TIFFReadDirEntryErr
173 : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value);
174 : static enum TIFFReadDirEntryErr
175 : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value);
176 : static enum TIFFReadDirEntryErr
177 : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value);
178 : static enum TIFFReadDirEntryErr
179 : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value);
180 : static enum TIFFReadDirEntryErr
181 : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value);
182 : static enum TIFFReadDirEntryErr
183 : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value);
184 :
185 : static enum TIFFReadDirEntryErr
186 : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value);
187 : static enum TIFFReadDirEntryErr
188 : TIFFReadDirEntryCheckRangeShortSshort(int16_t value);
189 : static enum TIFFReadDirEntryErr
190 : TIFFReadDirEntryCheckRangeShortLong(uint32_t value);
191 : static enum TIFFReadDirEntryErr
192 : TIFFReadDirEntryCheckRangeShortSlong(int32_t value);
193 : static enum TIFFReadDirEntryErr
194 : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value);
195 : static enum TIFFReadDirEntryErr
196 : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value);
197 :
198 : static enum TIFFReadDirEntryErr
199 : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value);
200 : static enum TIFFReadDirEntryErr
201 : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value);
202 : static enum TIFFReadDirEntryErr
203 : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value);
204 : static enum TIFFReadDirEntryErr
205 : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value);
206 : static enum TIFFReadDirEntryErr
207 : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value);
208 :
209 : static enum TIFFReadDirEntryErr
210 : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value);
211 : static enum TIFFReadDirEntryErr
212 : TIFFReadDirEntryCheckRangeLongSshort(int16_t value);
213 : static enum TIFFReadDirEntryErr
214 : TIFFReadDirEntryCheckRangeLongSlong(int32_t value);
215 : static enum TIFFReadDirEntryErr
216 : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value);
217 : static enum TIFFReadDirEntryErr
218 : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value);
219 :
220 : static enum TIFFReadDirEntryErr
221 : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value);
222 : static enum TIFFReadDirEntryErr
223 : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value);
224 : static enum TIFFReadDirEntryErr
225 : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value);
226 :
227 : static enum TIFFReadDirEntryErr
228 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value);
229 : static enum TIFFReadDirEntryErr
230 : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value);
231 : static enum TIFFReadDirEntryErr
232 : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value);
233 : static enum TIFFReadDirEntryErr
234 : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value);
235 :
236 : static enum TIFFReadDirEntryErr
237 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value);
238 :
239 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
240 : tmsize_t size, void *dest);
241 : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
242 : const char *module, const char *tagname,
243 : int recover);
244 :
245 : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
246 : uint16_t dircount);
247 : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
248 : uint16_t dircount,
249 : uint16_t tagid);
250 : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
251 : uint32_t *fii);
252 :
253 : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
254 : uint16_t dircount);
255 : static void MissingRequired(TIFF *, const char *);
256 : static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
257 : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
258 : TIFFDirEntry **pdir, uint64_t *nextdiroff);
259 : static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover);
260 : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
261 : uint64_t **lpp);
262 : static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *);
263 : static void ChopUpSingleUncompressedStrip(TIFF *);
264 : static void TryChopUpUncompressedBigTiff(TIFF *);
265 : static uint64_t TIFFReadUInt64(const uint8_t *value);
266 : static int _TIFFGetMaxColorChannels(uint16_t photometric);
267 :
268 : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount);
269 :
270 : typedef union _UInt64Aligned_t
271 : {
272 : double d;
273 : uint64_t l;
274 : uint32_t i[2];
275 : uint16_t s[4];
276 : uint8_t c[8];
277 : } UInt64Aligned_t;
278 :
279 : /*
280 : Unaligned safe copy of a uint64_t value from an octet array.
281 : */
282 25164 : static uint64_t TIFFReadUInt64(const uint8_t *value)
283 : {
284 : UInt64Aligned_t result;
285 :
286 25164 : result.c[0] = value[0];
287 25164 : result.c[1] = value[1];
288 25164 : result.c[2] = value[2];
289 25164 : result.c[3] = value[3];
290 25164 : result.c[4] = value[4];
291 25164 : result.c[5] = value[5];
292 25164 : result.c[6] = value[6];
293 25164 : result.c[7] = value[7];
294 :
295 25164 : return result.l;
296 : }
297 :
298 : static enum TIFFReadDirEntryErr
299 0 : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
300 : {
301 : enum TIFFReadDirEntryErr err;
302 0 : if (direntry->tdir_count != 1)
303 0 : return (TIFFReadDirEntryErrCount);
304 0 : switch (direntry->tdir_type)
305 : {
306 0 : case TIFF_BYTE:
307 : case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
308 : field_readcount==1 */
309 0 : TIFFReadDirEntryCheckedByte(tif, direntry, value);
310 0 : return (TIFFReadDirEntryErrOk);
311 0 : case TIFF_SBYTE:
312 : {
313 : int8_t m;
314 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
315 0 : err = TIFFReadDirEntryCheckRangeByteSbyte(m);
316 0 : if (err != TIFFReadDirEntryErrOk)
317 0 : return (err);
318 0 : *value = (uint8_t)m;
319 0 : return (TIFFReadDirEntryErrOk);
320 : }
321 0 : case TIFF_SHORT:
322 : {
323 : uint16_t m;
324 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
325 0 : err = TIFFReadDirEntryCheckRangeByteShort(m);
326 0 : if (err != TIFFReadDirEntryErrOk)
327 0 : return (err);
328 0 : *value = (uint8_t)m;
329 0 : return (TIFFReadDirEntryErrOk);
330 : }
331 0 : case TIFF_SSHORT:
332 : {
333 : int16_t m;
334 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
335 0 : err = TIFFReadDirEntryCheckRangeByteSshort(m);
336 0 : if (err != TIFFReadDirEntryErrOk)
337 0 : return (err);
338 0 : *value = (uint8_t)m;
339 0 : return (TIFFReadDirEntryErrOk);
340 : }
341 0 : case TIFF_LONG:
342 : {
343 : uint32_t m;
344 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
345 0 : err = TIFFReadDirEntryCheckRangeByteLong(m);
346 0 : if (err != TIFFReadDirEntryErrOk)
347 0 : return (err);
348 0 : *value = (uint8_t)m;
349 0 : return (TIFFReadDirEntryErrOk);
350 : }
351 0 : case TIFF_SLONG:
352 : {
353 : int32_t m;
354 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
355 0 : err = TIFFReadDirEntryCheckRangeByteSlong(m);
356 0 : if (err != TIFFReadDirEntryErrOk)
357 0 : return (err);
358 0 : *value = (uint8_t)m;
359 0 : return (TIFFReadDirEntryErrOk);
360 : }
361 0 : case TIFF_LONG8:
362 : {
363 : uint64_t m;
364 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
365 0 : if (err != TIFFReadDirEntryErrOk)
366 0 : return (err);
367 0 : err = TIFFReadDirEntryCheckRangeByteLong8(m);
368 0 : if (err != TIFFReadDirEntryErrOk)
369 0 : return (err);
370 0 : *value = (uint8_t)m;
371 0 : return (TIFFReadDirEntryErrOk);
372 : }
373 0 : case TIFF_SLONG8:
374 : {
375 : int64_t m;
376 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
377 0 : if (err != TIFFReadDirEntryErrOk)
378 0 : return (err);
379 0 : err = TIFFReadDirEntryCheckRangeByteSlong8(m);
380 0 : if (err != TIFFReadDirEntryErrOk)
381 0 : return (err);
382 0 : *value = (uint8_t)m;
383 0 : return (TIFFReadDirEntryErrOk);
384 : }
385 0 : default:
386 0 : return (TIFFReadDirEntryErrType);
387 : }
388 : }
389 :
390 : static enum TIFFReadDirEntryErr
391 0 : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
392 : {
393 : enum TIFFReadDirEntryErr err;
394 0 : if (direntry->tdir_count != 1)
395 0 : return (TIFFReadDirEntryErrCount);
396 0 : switch (direntry->tdir_type)
397 : {
398 0 : case TIFF_BYTE:
399 : case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
400 : field_readcount==1 */
401 : {
402 : uint8_t m;
403 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
404 0 : err = TIFFReadDirEntryCheckRangeSbyteByte(m);
405 0 : if (err != TIFFReadDirEntryErrOk)
406 0 : return (err);
407 0 : *value = (int8_t)m;
408 0 : return (TIFFReadDirEntryErrOk);
409 : }
410 0 : case TIFF_SBYTE:
411 : {
412 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
413 0 : return (TIFFReadDirEntryErrOk);
414 : }
415 0 : case TIFF_SHORT:
416 : {
417 : uint16_t m;
418 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
419 0 : err = TIFFReadDirEntryCheckRangeSbyteShort(m);
420 0 : if (err != TIFFReadDirEntryErrOk)
421 0 : return (err);
422 0 : *value = (int8_t)m;
423 0 : return (TIFFReadDirEntryErrOk);
424 : }
425 0 : case TIFF_SSHORT:
426 : {
427 : int16_t m;
428 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
429 0 : err = TIFFReadDirEntryCheckRangeSbyteSshort(m);
430 0 : if (err != TIFFReadDirEntryErrOk)
431 0 : return (err);
432 0 : *value = (int8_t)m;
433 0 : return (TIFFReadDirEntryErrOk);
434 : }
435 0 : case TIFF_LONG:
436 : {
437 : uint32_t m;
438 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
439 0 : err = TIFFReadDirEntryCheckRangeSbyteLong(m);
440 0 : if (err != TIFFReadDirEntryErrOk)
441 0 : return (err);
442 0 : *value = (int8_t)m;
443 0 : return (TIFFReadDirEntryErrOk);
444 : }
445 0 : case TIFF_SLONG:
446 : {
447 : int32_t m;
448 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
449 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong(m);
450 0 : if (err != TIFFReadDirEntryErrOk)
451 0 : return (err);
452 0 : *value = (int8_t)m;
453 0 : return (TIFFReadDirEntryErrOk);
454 : }
455 0 : case TIFF_LONG8:
456 : {
457 : uint64_t m;
458 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
459 0 : if (err != TIFFReadDirEntryErrOk)
460 0 : return (err);
461 0 : err = TIFFReadDirEntryCheckRangeSbyteLong8(m);
462 0 : if (err != TIFFReadDirEntryErrOk)
463 0 : return (err);
464 0 : *value = (int8_t)m;
465 0 : return (TIFFReadDirEntryErrOk);
466 : }
467 0 : case TIFF_SLONG8:
468 : {
469 : int64_t m;
470 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
471 0 : if (err != TIFFReadDirEntryErrOk)
472 0 : return (err);
473 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong8(m);
474 0 : if (err != TIFFReadDirEntryErrOk)
475 0 : return (err);
476 0 : *value = (int8_t)m;
477 0 : return (TIFFReadDirEntryErrOk);
478 : }
479 0 : default:
480 0 : return (TIFFReadDirEntryErrType);
481 : }
482 : } /*-- TIFFReadDirEntrySbyte() --*/
483 :
484 : static enum TIFFReadDirEntryErr
485 334333 : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
486 : {
487 : enum TIFFReadDirEntryErr err;
488 334333 : if (direntry->tdir_count != 1)
489 32780 : return (TIFFReadDirEntryErrCount);
490 301553 : switch (direntry->tdir_type)
491 : {
492 0 : case TIFF_BYTE:
493 : {
494 : uint8_t m;
495 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
496 0 : *value = (uint16_t)m;
497 0 : return (TIFFReadDirEntryErrOk);
498 : }
499 0 : case TIFF_SBYTE:
500 : {
501 : int8_t m;
502 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
503 0 : err = TIFFReadDirEntryCheckRangeShortSbyte(m);
504 0 : if (err != TIFFReadDirEntryErrOk)
505 0 : return (err);
506 0 : *value = (uint16_t)m;
507 0 : return (TIFFReadDirEntryErrOk);
508 : }
509 301411 : case TIFF_SHORT:
510 301411 : TIFFReadDirEntryCheckedShort(tif, direntry, value);
511 301394 : return (TIFFReadDirEntryErrOk);
512 0 : case TIFF_SSHORT:
513 : {
514 : int16_t m;
515 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
516 0 : err = TIFFReadDirEntryCheckRangeShortSshort(m);
517 0 : if (err != TIFFReadDirEntryErrOk)
518 0 : return (err);
519 0 : *value = (uint16_t)m;
520 0 : return (TIFFReadDirEntryErrOk);
521 : }
522 1 : case TIFF_LONG:
523 : {
524 : uint32_t m;
525 1 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
526 1 : err = TIFFReadDirEntryCheckRangeShortLong(m);
527 1 : if (err != TIFFReadDirEntryErrOk)
528 0 : return (err);
529 1 : *value = (uint16_t)m;
530 1 : return (TIFFReadDirEntryErrOk);
531 : }
532 0 : case TIFF_SLONG:
533 : {
534 : int32_t m;
535 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
536 0 : err = TIFFReadDirEntryCheckRangeShortSlong(m);
537 0 : if (err != TIFFReadDirEntryErrOk)
538 0 : return (err);
539 0 : *value = (uint16_t)m;
540 0 : return (TIFFReadDirEntryErrOk);
541 : }
542 0 : case TIFF_LONG8:
543 : {
544 : uint64_t m;
545 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
546 0 : if (err != TIFFReadDirEntryErrOk)
547 0 : return (err);
548 0 : err = TIFFReadDirEntryCheckRangeShortLong8(m);
549 0 : if (err != TIFFReadDirEntryErrOk)
550 0 : return (err);
551 0 : *value = (uint16_t)m;
552 0 : return (TIFFReadDirEntryErrOk);
553 : }
554 0 : case TIFF_SLONG8:
555 : {
556 : int64_t m;
557 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
558 0 : if (err != TIFFReadDirEntryErrOk)
559 0 : return (err);
560 0 : err = TIFFReadDirEntryCheckRangeShortSlong8(m);
561 0 : if (err != TIFFReadDirEntryErrOk)
562 0 : return (err);
563 0 : *value = (uint16_t)m;
564 0 : return (TIFFReadDirEntryErrOk);
565 : }
566 141 : default:
567 141 : return (TIFFReadDirEntryErrType);
568 : }
569 : } /*-- TIFFReadDirEntryShort() --*/
570 :
571 : static enum TIFFReadDirEntryErr
572 0 : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
573 : {
574 : enum TIFFReadDirEntryErr err;
575 0 : if (direntry->tdir_count != 1)
576 0 : return (TIFFReadDirEntryErrCount);
577 0 : switch (direntry->tdir_type)
578 : {
579 0 : case TIFF_BYTE:
580 : {
581 : uint8_t m;
582 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
583 0 : *value = (int16_t)m;
584 0 : return (TIFFReadDirEntryErrOk);
585 : }
586 0 : case TIFF_SBYTE:
587 : {
588 : int8_t m;
589 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
590 0 : *value = (int16_t)m;
591 0 : return (TIFFReadDirEntryErrOk);
592 : }
593 0 : case TIFF_SHORT:
594 : {
595 : uint16_t m;
596 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
597 0 : err = TIFFReadDirEntryCheckRangeSshortShort(m);
598 0 : if (err != TIFFReadDirEntryErrOk)
599 0 : return (err);
600 0 : *value = (int16_t)m;
601 0 : return (TIFFReadDirEntryErrOk);
602 : }
603 0 : case TIFF_SSHORT:
604 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, value);
605 0 : return (TIFFReadDirEntryErrOk);
606 0 : case TIFF_LONG:
607 : {
608 : uint32_t m;
609 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
610 0 : err = TIFFReadDirEntryCheckRangeSshortLong(m);
611 0 : if (err != TIFFReadDirEntryErrOk)
612 0 : return (err);
613 0 : *value = (int16_t)m;
614 0 : return (TIFFReadDirEntryErrOk);
615 : }
616 0 : case TIFF_SLONG:
617 : {
618 : int32_t m;
619 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
620 0 : err = TIFFReadDirEntryCheckRangeSshortSlong(m);
621 0 : if (err != TIFFReadDirEntryErrOk)
622 0 : return (err);
623 0 : *value = (int16_t)m;
624 0 : return (TIFFReadDirEntryErrOk);
625 : }
626 0 : case TIFF_LONG8:
627 : {
628 : uint64_t m;
629 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
630 0 : if (err != TIFFReadDirEntryErrOk)
631 0 : return (err);
632 0 : err = TIFFReadDirEntryCheckRangeSshortLong8(m);
633 0 : if (err != TIFFReadDirEntryErrOk)
634 0 : return (err);
635 0 : *value = (int16_t)m;
636 0 : return (TIFFReadDirEntryErrOk);
637 : }
638 0 : case TIFF_SLONG8:
639 : {
640 : int64_t m;
641 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
642 0 : if (err != TIFFReadDirEntryErrOk)
643 0 : return (err);
644 0 : err = TIFFReadDirEntryCheckRangeSshortSlong8(m);
645 0 : if (err != TIFFReadDirEntryErrOk)
646 0 : return (err);
647 0 : *value = (int16_t)m;
648 0 : return (TIFFReadDirEntryErrOk);
649 : }
650 0 : default:
651 0 : return (TIFFReadDirEntryErrType);
652 : }
653 : } /*-- TIFFReadDirEntrySshort() --*/
654 :
655 : static enum TIFFReadDirEntryErr
656 180156 : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
657 : {
658 : enum TIFFReadDirEntryErr err;
659 180156 : if (direntry->tdir_count != 1)
660 0 : return (TIFFReadDirEntryErrCount);
661 180156 : switch (direntry->tdir_type)
662 : {
663 0 : case TIFF_BYTE:
664 : {
665 : uint8_t m;
666 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
667 0 : *value = (uint32_t)m;
668 0 : return (TIFFReadDirEntryErrOk);
669 : }
670 0 : case TIFF_SBYTE:
671 : {
672 : int8_t m;
673 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
674 0 : err = TIFFReadDirEntryCheckRangeLongSbyte(m);
675 0 : if (err != TIFFReadDirEntryErrOk)
676 0 : return (err);
677 0 : *value = (uint32_t)m;
678 0 : return (TIFFReadDirEntryErrOk);
679 : }
680 173979 : case TIFF_SHORT:
681 : {
682 : uint16_t m;
683 173979 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
684 173986 : *value = (uint32_t)m;
685 173986 : return (TIFFReadDirEntryErrOk);
686 : }
687 0 : case TIFF_SSHORT:
688 : {
689 : int16_t m;
690 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
691 0 : err = TIFFReadDirEntryCheckRangeLongSshort(m);
692 0 : if (err != TIFFReadDirEntryErrOk)
693 0 : return (err);
694 0 : *value = (uint32_t)m;
695 0 : return (TIFFReadDirEntryErrOk);
696 : }
697 6116 : case TIFF_LONG:
698 : case TIFF_IFD:
699 6116 : TIFFReadDirEntryCheckedLong(tif, direntry, value);
700 6115 : return (TIFFReadDirEntryErrOk);
701 0 : case TIFF_SLONG:
702 : {
703 : int32_t m;
704 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
705 0 : err = TIFFReadDirEntryCheckRangeLongSlong(m);
706 0 : if (err != TIFFReadDirEntryErrOk)
707 0 : return (err);
708 0 : *value = (uint32_t)m;
709 0 : return (TIFFReadDirEntryErrOk);
710 : }
711 0 : case TIFF_LONG8:
712 : case TIFF_IFD8:
713 : {
714 : uint64_t m;
715 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
716 0 : if (err != TIFFReadDirEntryErrOk)
717 0 : return (err);
718 0 : err = TIFFReadDirEntryCheckRangeLongLong8(m);
719 0 : if (err != TIFFReadDirEntryErrOk)
720 0 : return (err);
721 0 : *value = (uint32_t)m;
722 0 : return (TIFFReadDirEntryErrOk);
723 : }
724 0 : case TIFF_SLONG8:
725 : {
726 : int64_t m;
727 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
728 0 : if (err != TIFFReadDirEntryErrOk)
729 0 : return (err);
730 0 : err = TIFFReadDirEntryCheckRangeLongSlong8(m);
731 0 : if (err != TIFFReadDirEntryErrOk)
732 0 : return (err);
733 0 : *value = (uint32_t)m;
734 0 : return (TIFFReadDirEntryErrOk);
735 : }
736 61 : default:
737 61 : return (TIFFReadDirEntryErrType);
738 : }
739 : } /*-- TIFFReadDirEntryLong() --*/
740 :
741 : static enum TIFFReadDirEntryErr
742 0 : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
743 : {
744 : enum TIFFReadDirEntryErr err;
745 0 : if (direntry->tdir_count != 1)
746 0 : return (TIFFReadDirEntryErrCount);
747 0 : switch (direntry->tdir_type)
748 : {
749 0 : case TIFF_BYTE:
750 : {
751 : uint8_t m;
752 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
753 0 : *value = (int32_t)m;
754 0 : return (TIFFReadDirEntryErrOk);
755 : }
756 0 : case TIFF_SBYTE:
757 : {
758 : int8_t m;
759 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
760 0 : *value = (int32_t)m;
761 0 : return (TIFFReadDirEntryErrOk);
762 : }
763 0 : case TIFF_SHORT:
764 : {
765 : uint16_t m;
766 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
767 0 : *value = (int32_t)m;
768 0 : return (TIFFReadDirEntryErrOk);
769 : }
770 0 : case TIFF_SSHORT:
771 : {
772 : int16_t m;
773 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
774 0 : *value = (int32_t)m;
775 0 : return (TIFFReadDirEntryErrOk);
776 : }
777 0 : case TIFF_LONG:
778 : {
779 : uint32_t m;
780 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
781 0 : err = TIFFReadDirEntryCheckRangeSlongLong(m);
782 0 : if (err != TIFFReadDirEntryErrOk)
783 0 : return (err);
784 0 : *value = (int32_t)m;
785 0 : return (TIFFReadDirEntryErrOk);
786 : }
787 0 : case TIFF_SLONG:
788 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, value);
789 0 : return (TIFFReadDirEntryErrOk);
790 0 : case TIFF_LONG8:
791 : {
792 : uint64_t m;
793 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
794 0 : if (err != TIFFReadDirEntryErrOk)
795 0 : return (err);
796 0 : err = TIFFReadDirEntryCheckRangeSlongLong8(m);
797 0 : if (err != TIFFReadDirEntryErrOk)
798 0 : return (err);
799 0 : *value = (int32_t)m;
800 0 : return (TIFFReadDirEntryErrOk);
801 : }
802 0 : case TIFF_SLONG8:
803 : {
804 : int64_t m;
805 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
806 0 : if (err != TIFFReadDirEntryErrOk)
807 0 : return (err);
808 0 : err = TIFFReadDirEntryCheckRangeSlongSlong8(m);
809 0 : if (err != TIFFReadDirEntryErrOk)
810 0 : return (err);
811 0 : *value = (int32_t)m;
812 0 : return (TIFFReadDirEntryErrOk);
813 : }
814 0 : default:
815 0 : return (TIFFReadDirEntryErrType);
816 : }
817 : } /*-- TIFFReadDirEntrySlong() --*/
818 :
819 : static enum TIFFReadDirEntryErr
820 8 : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
821 : {
822 : enum TIFFReadDirEntryErr err;
823 8 : if (direntry->tdir_count != 1)
824 0 : return (TIFFReadDirEntryErrCount);
825 8 : switch (direntry->tdir_type)
826 : {
827 0 : case TIFF_BYTE:
828 : {
829 : uint8_t m;
830 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
831 0 : *value = (uint64_t)m;
832 0 : return (TIFFReadDirEntryErrOk);
833 : }
834 0 : case TIFF_SBYTE:
835 : {
836 : int8_t m;
837 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
838 0 : err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
839 0 : if (err != TIFFReadDirEntryErrOk)
840 0 : return (err);
841 0 : *value = (uint64_t)m;
842 0 : return (TIFFReadDirEntryErrOk);
843 : }
844 0 : case TIFF_SHORT:
845 : {
846 : uint16_t m;
847 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
848 0 : *value = (uint64_t)m;
849 0 : return (TIFFReadDirEntryErrOk);
850 : }
851 0 : case TIFF_SSHORT:
852 : {
853 : int16_t m;
854 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
855 0 : err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
856 0 : if (err != TIFFReadDirEntryErrOk)
857 0 : return (err);
858 0 : *value = (uint64_t)m;
859 0 : return (TIFFReadDirEntryErrOk);
860 : }
861 8 : case TIFF_LONG:
862 : case TIFF_IFD:
863 : {
864 : uint32_t m;
865 8 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
866 8 : *value = (uint64_t)m;
867 8 : return (TIFFReadDirEntryErrOk);
868 : }
869 0 : case TIFF_SLONG:
870 : {
871 : int32_t m;
872 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
873 0 : err = TIFFReadDirEntryCheckRangeLong8Slong(m);
874 0 : if (err != TIFFReadDirEntryErrOk)
875 0 : return (err);
876 0 : *value = (uint64_t)m;
877 0 : return (TIFFReadDirEntryErrOk);
878 : }
879 0 : case TIFF_LONG8:
880 : case TIFF_IFD8:
881 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
882 0 : return (err);
883 0 : case TIFF_SLONG8:
884 : {
885 : int64_t m;
886 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
887 0 : if (err != TIFFReadDirEntryErrOk)
888 0 : return (err);
889 0 : err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
890 0 : if (err != TIFFReadDirEntryErrOk)
891 0 : return (err);
892 0 : *value = (uint64_t)m;
893 0 : return (TIFFReadDirEntryErrOk);
894 : }
895 0 : default:
896 0 : return (TIFFReadDirEntryErrType);
897 : }
898 : } /*-- TIFFReadDirEntryLong8() --*/
899 :
900 : static enum TIFFReadDirEntryErr
901 0 : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
902 : {
903 : enum TIFFReadDirEntryErr err;
904 0 : if (direntry->tdir_count != 1)
905 0 : return (TIFFReadDirEntryErrCount);
906 0 : switch (direntry->tdir_type)
907 : {
908 0 : case TIFF_BYTE:
909 : {
910 : uint8_t m;
911 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
912 0 : *value = (int64_t)m;
913 0 : return (TIFFReadDirEntryErrOk);
914 : }
915 0 : case TIFF_SBYTE:
916 : {
917 : int8_t m;
918 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
919 0 : *value = (int64_t)m;
920 0 : return (TIFFReadDirEntryErrOk);
921 : }
922 0 : case TIFF_SHORT:
923 : {
924 : uint16_t m;
925 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
926 0 : *value = (int64_t)m;
927 0 : return (TIFFReadDirEntryErrOk);
928 : }
929 0 : case TIFF_SSHORT:
930 : {
931 : int16_t m;
932 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
933 0 : *value = (int64_t)m;
934 0 : return (TIFFReadDirEntryErrOk);
935 : }
936 0 : case TIFF_LONG:
937 : {
938 : uint32_t m;
939 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
940 0 : *value = (int64_t)m;
941 0 : return (TIFFReadDirEntryErrOk);
942 : }
943 0 : case TIFF_SLONG:
944 : {
945 : int32_t m;
946 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
947 0 : *value = (int64_t)m;
948 0 : return (TIFFReadDirEntryErrOk);
949 : }
950 0 : case TIFF_LONG8:
951 : {
952 : uint64_t m;
953 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
954 0 : if (err != TIFFReadDirEntryErrOk)
955 0 : return (err);
956 0 : err = TIFFReadDirEntryCheckRangeSlong8Long8(m);
957 0 : if (err != TIFFReadDirEntryErrOk)
958 0 : return (err);
959 0 : *value = (int64_t)m;
960 0 : return (TIFFReadDirEntryErrOk);
961 : }
962 0 : case TIFF_SLONG8:
963 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
964 0 : return (err);
965 0 : default:
966 0 : return (TIFFReadDirEntryErrType);
967 : }
968 : } /*-- TIFFReadDirEntrySlong8() --*/
969 :
970 : static enum TIFFReadDirEntryErr
971 248 : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
972 : {
973 : enum TIFFReadDirEntryErr err;
974 248 : if (direntry->tdir_count != 1)
975 0 : return (TIFFReadDirEntryErrCount);
976 248 : switch (direntry->tdir_type)
977 : {
978 0 : case TIFF_BYTE:
979 : {
980 : uint8_t m;
981 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
982 0 : *value = (float)m;
983 0 : return (TIFFReadDirEntryErrOk);
984 : }
985 0 : case TIFF_SBYTE:
986 : {
987 : int8_t m;
988 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
989 0 : *value = (float)m;
990 0 : return (TIFFReadDirEntryErrOk);
991 : }
992 0 : case TIFF_SHORT:
993 : {
994 : uint16_t m;
995 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
996 0 : *value = (float)m;
997 0 : return (TIFFReadDirEntryErrOk);
998 : }
999 0 : case TIFF_SSHORT:
1000 : {
1001 : int16_t m;
1002 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1003 0 : *value = (float)m;
1004 0 : return (TIFFReadDirEntryErrOk);
1005 : }
1006 0 : case TIFF_LONG:
1007 : {
1008 : uint32_t m;
1009 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1010 0 : *value = (float)m;
1011 0 : return (TIFFReadDirEntryErrOk);
1012 : }
1013 0 : case TIFF_SLONG:
1014 : {
1015 : int32_t m;
1016 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1017 0 : *value = (float)m;
1018 0 : return (TIFFReadDirEntryErrOk);
1019 : }
1020 0 : case TIFF_LONG8:
1021 : {
1022 : uint64_t m;
1023 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1024 0 : if (err != TIFFReadDirEntryErrOk)
1025 0 : return (err);
1026 0 : *value = (float)m;
1027 0 : return (TIFFReadDirEntryErrOk);
1028 : }
1029 0 : case TIFF_SLONG8:
1030 : {
1031 : int64_t m;
1032 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1033 0 : if (err != TIFFReadDirEntryErrOk)
1034 0 : return (err);
1035 0 : *value = (float)m;
1036 0 : return (TIFFReadDirEntryErrOk);
1037 : }
1038 248 : case TIFF_RATIONAL:
1039 : {
1040 : double m;
1041 248 : err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
1042 248 : if (err != TIFFReadDirEntryErrOk)
1043 0 : return (err);
1044 248 : *value = (float)m;
1045 248 : return (TIFFReadDirEntryErrOk);
1046 : }
1047 0 : case TIFF_SRATIONAL:
1048 : {
1049 : double m;
1050 0 : err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
1051 0 : if (err != TIFFReadDirEntryErrOk)
1052 0 : return (err);
1053 0 : *value = (float)m;
1054 0 : return (TIFFReadDirEntryErrOk);
1055 : }
1056 0 : case TIFF_FLOAT:
1057 0 : TIFFReadDirEntryCheckedFloat(tif, direntry, value);
1058 0 : return (TIFFReadDirEntryErrOk);
1059 0 : case TIFF_DOUBLE:
1060 : {
1061 : double m;
1062 0 : err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
1063 0 : if (err != TIFFReadDirEntryErrOk)
1064 0 : return (err);
1065 0 : if ((m > (double)FLT_MAX) || (m < -(double)FLT_MAX))
1066 0 : return (TIFFReadDirEntryErrRange);
1067 0 : *value = (float)m;
1068 0 : return (TIFFReadDirEntryErrOk);
1069 : }
1070 0 : default:
1071 0 : return (TIFFReadDirEntryErrType);
1072 : }
1073 : }
1074 :
1075 : static enum TIFFReadDirEntryErr
1076 0 : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
1077 : {
1078 : enum TIFFReadDirEntryErr err;
1079 0 : if (direntry->tdir_count != 1)
1080 0 : return (TIFFReadDirEntryErrCount);
1081 0 : switch (direntry->tdir_type)
1082 : {
1083 0 : case TIFF_BYTE:
1084 : {
1085 : uint8_t m;
1086 0 : TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1087 0 : *value = (double)m;
1088 0 : return (TIFFReadDirEntryErrOk);
1089 : }
1090 0 : case TIFF_SBYTE:
1091 : {
1092 : int8_t m;
1093 0 : TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1094 0 : *value = (double)m;
1095 0 : return (TIFFReadDirEntryErrOk);
1096 : }
1097 0 : case TIFF_SHORT:
1098 : {
1099 : uint16_t m;
1100 0 : TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1101 0 : *value = (double)m;
1102 0 : return (TIFFReadDirEntryErrOk);
1103 : }
1104 0 : case TIFF_SSHORT:
1105 : {
1106 : int16_t m;
1107 0 : TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1108 0 : *value = (double)m;
1109 0 : return (TIFFReadDirEntryErrOk);
1110 : }
1111 0 : case TIFF_LONG:
1112 : {
1113 : uint32_t m;
1114 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1115 0 : *value = (double)m;
1116 0 : return (TIFFReadDirEntryErrOk);
1117 : }
1118 0 : case TIFF_SLONG:
1119 : {
1120 : int32_t m;
1121 0 : TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1122 0 : *value = (double)m;
1123 0 : return (TIFFReadDirEntryErrOk);
1124 : }
1125 0 : case TIFF_LONG8:
1126 : {
1127 : uint64_t m;
1128 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1129 0 : if (err != TIFFReadDirEntryErrOk)
1130 0 : return (err);
1131 0 : *value = (double)m;
1132 0 : return (TIFFReadDirEntryErrOk);
1133 : }
1134 0 : case TIFF_SLONG8:
1135 : {
1136 : int64_t m;
1137 0 : err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1138 0 : if (err != TIFFReadDirEntryErrOk)
1139 0 : return (err);
1140 0 : *value = (double)m;
1141 0 : return (TIFFReadDirEntryErrOk);
1142 : }
1143 0 : case TIFF_RATIONAL:
1144 0 : err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
1145 0 : return (err);
1146 0 : case TIFF_SRATIONAL:
1147 0 : err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
1148 0 : return (err);
1149 0 : case TIFF_FLOAT:
1150 : {
1151 : float m;
1152 0 : TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
1153 0 : *value = (double)m;
1154 0 : return (TIFFReadDirEntryErrOk);
1155 : }
1156 0 : case TIFF_DOUBLE:
1157 0 : err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
1158 0 : return (err);
1159 0 : default:
1160 0 : return (TIFFReadDirEntryErrType);
1161 : }
1162 : }
1163 :
1164 : static enum TIFFReadDirEntryErr
1165 0 : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1166 : {
1167 : enum TIFFReadDirEntryErr err;
1168 0 : if (direntry->tdir_count != 1)
1169 0 : return (TIFFReadDirEntryErrCount);
1170 0 : switch (direntry->tdir_type)
1171 : {
1172 0 : case TIFF_LONG:
1173 : case TIFF_IFD:
1174 : {
1175 : uint32_t m;
1176 0 : TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1177 0 : *value = (uint64_t)m;
1178 0 : return (TIFFReadDirEntryErrOk);
1179 : }
1180 0 : case TIFF_LONG8:
1181 : case TIFF_IFD8:
1182 0 : err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1183 0 : return (err);
1184 0 : default:
1185 0 : return (TIFFReadDirEntryErrType);
1186 : }
1187 : }
1188 :
1189 : #define INITIAL_THRESHOLD (1024 * 1024)
1190 : #define THRESHOLD_MULTIPLIER 10
1191 : #define MAX_THRESHOLD \
1192 : (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \
1193 : INITIAL_THRESHOLD)
1194 :
1195 184189 : 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 184189 : tmsize_t threshold = INITIAL_THRESHOLD;
1202 : #endif
1203 184189 : tmsize_t already_read = 0;
1204 :
1205 184189 : assert(!isMapped(tif));
1206 :
1207 184189 : if (!SeekOK(tif, offset))
1208 0 : return (TIFFReadDirEntryErrIo);
1209 :
1210 : /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
1211 : /* so as to avoid allocating too much memory in case the file is too */
1212 : /* short. We could ask for the file size, but this might be */
1213 : /* expensive with some I/O layers (think of reading a gzipped file) */
1214 : /* Restrict to 64 bit processes, so as to avoid reallocs() */
1215 : /* on 32 bit processes where virtual memory is scarce. */
1216 368666 : while (already_read < size)
1217 : {
1218 : void *new_dest;
1219 : tmsize_t bytes_read;
1220 184356 : tmsize_t to_read = size - already_read;
1221 : #if SIZEOF_SIZE_T == 8
1222 184356 : if (to_read >= threshold && threshold < MAX_THRESHOLD)
1223 : {
1224 18 : to_read = threshold;
1225 18 : threshold *= THRESHOLD_MULTIPLIER;
1226 : }
1227 : #endif
1228 :
1229 : new_dest =
1230 184356 : (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
1231 184337 : if (new_dest == NULL)
1232 : {
1233 0 : TIFFErrorExtR(tif, tif->tif_name,
1234 : "Failed to allocate memory for %s "
1235 : "(%" TIFF_SSIZE_FORMAT
1236 : " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
1237 : "TIFFReadDirEntryArray", (tmsize_t)1,
1238 : already_read + to_read);
1239 0 : return TIFFReadDirEntryErrAlloc;
1240 : }
1241 184337 : *pdest = new_dest;
1242 :
1243 184337 : bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
1244 184331 : if (bytes_read < 0)
1245 0 : return TIFFReadDirEntryErrIo;
1246 184331 : already_read += bytes_read;
1247 184331 : if (bytes_read != to_read)
1248 : {
1249 21 : return TIFFReadDirEntryErrIo;
1250 : }
1251 : }
1252 184310 : 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 269253 : 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 269253 : typesize = TIFFDataWidth((TIFFDataType)direntry->tdir_type);
1270 :
1271 269058 : target_count64 =
1272 269058 : (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
1273 :
1274 269058 : if ((target_count64 == 0) || (typesize == 0))
1275 : {
1276 0 : *value = 0;
1277 0 : 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 269133 : original_datasize_clamped =
1285 269133 : ((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 269133 : if ((uint64_t)(MAX_SIZE_TAG_DATA / (unsigned int)typesize) < target_count64)
1294 3 : return (TIFFReadDirEntryErrSizesan);
1295 269130 : if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
1296 0 : return (TIFFReadDirEntryErrSizesan);
1297 :
1298 269130 : *count = (uint32_t)target_count64;
1299 269130 : datasize = (uint32_t)(*count) * (unsigned int)typesize;
1300 269130 : assert((tmsize_t)datasize > 0);
1301 :
1302 269130 : 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 15 : const uint64_t filesize = TIFFGetFileSize(tif);
1308 15 : if (datasize > filesize)
1309 : {
1310 15 : 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 15 : direntry->tdir_tag, direntry->tdir_tag, datasize,
1315 : filesize);
1316 15 : return (TIFFReadDirEntryErrAlloc);
1317 : }
1318 : }
1319 :
1320 269115 : if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
1321 0 : return TIFFReadDirEntryErrIo;
1322 :
1323 269115 : if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
1324 268236 : (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
1325 : {
1326 184016 : data = NULL;
1327 : }
1328 : else
1329 : {
1330 85099 : data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
1331 85110 : if (data == 0)
1332 0 : return (TIFFReadDirEntryErrAlloc);
1333 : }
1334 269126 : 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 266694 : if (original_datasize_clamped <= 4 && datasize <= 4)
1339 83732 : _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1340 : else
1341 : {
1342 : enum TIFFReadDirEntryErr err;
1343 182962 : uint32_t offset = direntry->tdir_offset.toff_long;
1344 182962 : if (tif->tif_flags & TIFF_SWAB)
1345 1941 : TIFFSwabLong(&offset);
1346 183042 : if (isMapped(tif))
1347 32 : err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1348 : (tmsize_t)datasize, data);
1349 : else
1350 183010 : err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1351 : (tmsize_t)datasize, &data);
1352 183278 : if (err != TIFFReadDirEntryErrOk)
1353 : {
1354 21 : _TIFFfreeExt(tif, data);
1355 21 : return (err);
1356 : }
1357 : }
1358 : }
1359 : else
1360 : {
1361 : /* See above comment for the Classic TIFF case */
1362 2432 : if (original_datasize_clamped <= 8 && datasize <= 8)
1363 1299 : _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1364 : else
1365 : {
1366 : enum TIFFReadDirEntryErr err;
1367 1133 : uint64_t offset = direntry->tdir_offset.toff_long8;
1368 1133 : if (tif->tif_flags & TIFF_SWAB)
1369 14 : TIFFSwabLong8(&offset);
1370 1006 : if (isMapped(tif))
1371 0 : err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1372 : (tmsize_t)datasize, data);
1373 : else
1374 1006 : err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1375 : (tmsize_t)datasize, &data);
1376 1006 : if (err != TIFFReadDirEntryErrOk)
1377 : {
1378 0 : _TIFFfreeExt(tif, data);
1379 0 : return (err);
1380 : }
1381 : }
1382 : }
1383 269234 : *value = data;
1384 269234 : return (TIFFReadDirEntryErrOk);
1385 : }
1386 :
1387 : static enum TIFFReadDirEntryErr
1388 183512 : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
1389 : uint32_t desttypesize, void **value)
1390 : {
1391 183512 : return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
1392 : value, ~((uint64_t)0));
1393 : }
1394 :
1395 : static enum TIFFReadDirEntryErr
1396 41075 : 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 41075 : switch (direntry->tdir_type)
1403 : {
1404 41072 : 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 41072 : break;
1415 3 : default:
1416 3 : return (TIFFReadDirEntryErrType);
1417 : }
1418 41072 : err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1419 41033 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1420 : {
1421 90 : *value = 0;
1422 90 : return (err);
1423 : }
1424 40943 : switch (direntry->tdir_type)
1425 : {
1426 40932 : case TIFF_ASCII:
1427 : case TIFF_UNDEFINED:
1428 : case TIFF_BYTE:
1429 40932 : *value = (uint8_t *)origdata;
1430 40932 : return (TIFFReadDirEntryErrOk);
1431 0 : case TIFF_SBYTE:
1432 : {
1433 : int8_t *m;
1434 : uint32_t n;
1435 0 : m = (int8_t *)origdata;
1436 0 : for (n = 0; n < count; n++)
1437 : {
1438 0 : err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
1439 0 : if (err != TIFFReadDirEntryErrOk)
1440 : {
1441 0 : _TIFFfreeExt(tif, origdata);
1442 0 : return (err);
1443 : }
1444 0 : m++;
1445 : }
1446 0 : *value = (uint8_t *)origdata;
1447 0 : return (TIFFReadDirEntryErrOk);
1448 : }
1449 11 : default:
1450 11 : break;
1451 : }
1452 11 : data = (uint8_t *)_TIFFmallocExt(tif, count);
1453 0 : if (data == 0)
1454 : {
1455 0 : _TIFFfreeExt(tif, origdata);
1456 0 : return (TIFFReadDirEntryErrAlloc);
1457 : }
1458 0 : switch (direntry->tdir_type)
1459 : {
1460 0 : case TIFF_SHORT:
1461 : {
1462 : uint16_t *ma;
1463 : uint8_t *mb;
1464 : uint32_t n;
1465 0 : ma = (uint16_t *)origdata;
1466 0 : mb = data;
1467 0 : for (n = 0; n < count; n++)
1468 : {
1469 0 : if (tif->tif_flags & TIFF_SWAB)
1470 0 : TIFFSwabShort(ma);
1471 0 : err = TIFFReadDirEntryCheckRangeByteShort(*ma);
1472 0 : if (err != TIFFReadDirEntryErrOk)
1473 0 : break;
1474 0 : *mb++ = (uint8_t)(*ma++);
1475 : }
1476 : }
1477 0 : break;
1478 0 : case TIFF_SSHORT:
1479 : {
1480 : int16_t *ma;
1481 : uint8_t *mb;
1482 : uint32_t n;
1483 0 : ma = (int16_t *)origdata;
1484 0 : mb = data;
1485 0 : for (n = 0; n < count; n++)
1486 : {
1487 0 : if (tif->tif_flags & TIFF_SWAB)
1488 0 : TIFFSwabShort((uint16_t *)ma);
1489 0 : err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
1490 0 : if (err != TIFFReadDirEntryErrOk)
1491 0 : break;
1492 0 : *mb++ = (uint8_t)(*ma++);
1493 : }
1494 : }
1495 0 : break;
1496 0 : case TIFF_LONG:
1497 : {
1498 : uint32_t *ma;
1499 : uint8_t *mb;
1500 : uint32_t n;
1501 0 : ma = (uint32_t *)origdata;
1502 0 : mb = data;
1503 0 : for (n = 0; n < count; n++)
1504 : {
1505 0 : if (tif->tif_flags & TIFF_SWAB)
1506 0 : TIFFSwabLong(ma);
1507 0 : err = TIFFReadDirEntryCheckRangeByteLong(*ma);
1508 0 : if (err != TIFFReadDirEntryErrOk)
1509 0 : break;
1510 0 : *mb++ = (uint8_t)(*ma++);
1511 : }
1512 : }
1513 0 : break;
1514 0 : case TIFF_SLONG:
1515 : {
1516 : int32_t *ma;
1517 : uint8_t *mb;
1518 : uint32_t n;
1519 0 : ma = (int32_t *)origdata;
1520 0 : mb = data;
1521 0 : for (n = 0; n < count; n++)
1522 : {
1523 0 : if (tif->tif_flags & TIFF_SWAB)
1524 0 : TIFFSwabLong((uint32_t *)ma);
1525 0 : err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
1526 0 : if (err != TIFFReadDirEntryErrOk)
1527 0 : break;
1528 0 : *mb++ = (uint8_t)(*ma++);
1529 : }
1530 : }
1531 0 : break;
1532 0 : case TIFF_LONG8:
1533 : {
1534 : uint64_t *ma;
1535 : uint8_t *mb;
1536 : uint32_t n;
1537 0 : ma = (uint64_t *)origdata;
1538 0 : mb = data;
1539 0 : for (n = 0; n < count; n++)
1540 : {
1541 0 : if (tif->tif_flags & TIFF_SWAB)
1542 0 : TIFFSwabLong8(ma);
1543 0 : err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
1544 0 : if (err != TIFFReadDirEntryErrOk)
1545 0 : break;
1546 0 : *mb++ = (uint8_t)(*ma++);
1547 : }
1548 : }
1549 0 : break;
1550 0 : case TIFF_SLONG8:
1551 : {
1552 : int64_t *ma;
1553 : uint8_t *mb;
1554 : uint32_t n;
1555 0 : ma = (int64_t *)origdata;
1556 0 : mb = data;
1557 0 : for (n = 0; n < count; n++)
1558 : {
1559 0 : if (tif->tif_flags & TIFF_SWAB)
1560 0 : TIFFSwabLong8((uint64_t *)ma);
1561 0 : err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1562 0 : if (err != TIFFReadDirEntryErrOk)
1563 0 : break;
1564 0 : *mb++ = (uint8_t)(*ma++);
1565 : }
1566 : }
1567 0 : break;
1568 0 : default:
1569 0 : break;
1570 : }
1571 0 : _TIFFfreeExt(tif, origdata);
1572 0 : if (err != TIFFReadDirEntryErrOk)
1573 : {
1574 0 : _TIFFfreeExt(tif, data);
1575 0 : return (err);
1576 : }
1577 0 : *value = data;
1578 0 : return (TIFFReadDirEntryErrOk);
1579 : }
1580 :
1581 : static enum TIFFReadDirEntryErr
1582 0 : 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 0 : switch (direntry->tdir_type)
1589 : {
1590 0 : 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 0 : break;
1600 0 : default:
1601 0 : return (TIFFReadDirEntryErrType);
1602 : }
1603 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1604 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1605 : {
1606 0 : *value = 0;
1607 0 : return (err);
1608 : }
1609 0 : switch (direntry->tdir_type)
1610 : {
1611 0 : case TIFF_UNDEFINED:
1612 : case TIFF_BYTE:
1613 : {
1614 : uint8_t *m;
1615 : uint32_t n;
1616 0 : m = (uint8_t *)origdata;
1617 0 : for (n = 0; n < count; n++)
1618 : {
1619 0 : err = TIFFReadDirEntryCheckRangeSbyteByte(*m);
1620 0 : if (err != TIFFReadDirEntryErrOk)
1621 : {
1622 0 : _TIFFfreeExt(tif, origdata);
1623 0 : return (err);
1624 : }
1625 0 : m++;
1626 : }
1627 0 : *value = (int8_t *)origdata;
1628 0 : return (TIFFReadDirEntryErrOk);
1629 : }
1630 0 : case TIFF_SBYTE:
1631 0 : *value = (int8_t *)origdata;
1632 0 : return (TIFFReadDirEntryErrOk);
1633 0 : default:
1634 0 : break;
1635 : }
1636 0 : data = (int8_t *)_TIFFmallocExt(tif, count);
1637 0 : if (data == 0)
1638 : {
1639 0 : _TIFFfreeExt(tif, origdata);
1640 0 : return (TIFFReadDirEntryErrAlloc);
1641 : }
1642 0 : switch (direntry->tdir_type)
1643 : {
1644 0 : case TIFF_SHORT:
1645 : {
1646 : uint16_t *ma;
1647 : int8_t *mb;
1648 : uint32_t n;
1649 0 : ma = (uint16_t *)origdata;
1650 0 : mb = data;
1651 0 : for (n = 0; n < count; n++)
1652 : {
1653 0 : if (tif->tif_flags & TIFF_SWAB)
1654 0 : TIFFSwabShort(ma);
1655 0 : err = TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1656 0 : if (err != TIFFReadDirEntryErrOk)
1657 0 : break;
1658 0 : *mb++ = (int8_t)(*ma++);
1659 : }
1660 : }
1661 0 : break;
1662 0 : case TIFF_SSHORT:
1663 : {
1664 : int16_t *ma;
1665 : int8_t *mb;
1666 : uint32_t n;
1667 0 : ma = (int16_t *)origdata;
1668 0 : mb = data;
1669 0 : for (n = 0; n < count; n++)
1670 : {
1671 0 : if (tif->tif_flags & TIFF_SWAB)
1672 0 : TIFFSwabShort((uint16_t *)ma);
1673 0 : err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1674 0 : if (err != TIFFReadDirEntryErrOk)
1675 0 : break;
1676 0 : *mb++ = (int8_t)(*ma++);
1677 : }
1678 : }
1679 0 : break;
1680 0 : case TIFF_LONG:
1681 : {
1682 : uint32_t *ma;
1683 : int8_t *mb;
1684 : uint32_t n;
1685 0 : ma = (uint32_t *)origdata;
1686 0 : mb = data;
1687 0 : for (n = 0; n < count; n++)
1688 : {
1689 0 : if (tif->tif_flags & TIFF_SWAB)
1690 0 : TIFFSwabLong(ma);
1691 0 : err = TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1692 0 : if (err != TIFFReadDirEntryErrOk)
1693 0 : break;
1694 0 : *mb++ = (int8_t)(*ma++);
1695 : }
1696 : }
1697 0 : break;
1698 0 : case TIFF_SLONG:
1699 : {
1700 : int32_t *ma;
1701 : int8_t *mb;
1702 : uint32_t n;
1703 0 : ma = (int32_t *)origdata;
1704 0 : mb = data;
1705 0 : for (n = 0; n < count; n++)
1706 : {
1707 0 : if (tif->tif_flags & TIFF_SWAB)
1708 0 : TIFFSwabLong((uint32_t *)ma);
1709 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1710 0 : if (err != TIFFReadDirEntryErrOk)
1711 0 : break;
1712 0 : *mb++ = (int8_t)(*ma++);
1713 : }
1714 : }
1715 0 : break;
1716 0 : case TIFF_LONG8:
1717 : {
1718 : uint64_t *ma;
1719 : int8_t *mb;
1720 : uint32_t n;
1721 0 : ma = (uint64_t *)origdata;
1722 0 : mb = data;
1723 0 : for (n = 0; n < count; n++)
1724 : {
1725 0 : if (tif->tif_flags & TIFF_SWAB)
1726 0 : TIFFSwabLong8(ma);
1727 0 : err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1728 0 : if (err != TIFFReadDirEntryErrOk)
1729 0 : break;
1730 0 : *mb++ = (int8_t)(*ma++);
1731 : }
1732 : }
1733 0 : break;
1734 0 : case TIFF_SLONG8:
1735 : {
1736 : int64_t *ma;
1737 : int8_t *mb;
1738 : uint32_t n;
1739 0 : ma = (int64_t *)origdata;
1740 0 : mb = data;
1741 0 : for (n = 0; n < count; n++)
1742 : {
1743 0 : if (tif->tif_flags & TIFF_SWAB)
1744 0 : TIFFSwabLong8((uint64_t *)ma);
1745 0 : err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1746 0 : if (err != TIFFReadDirEntryErrOk)
1747 0 : break;
1748 0 : *mb++ = (int8_t)(*ma++);
1749 : }
1750 : }
1751 0 : break;
1752 0 : default:
1753 0 : break;
1754 : }
1755 0 : _TIFFfreeExt(tif, origdata);
1756 0 : if (err != TIFFReadDirEntryErrOk)
1757 : {
1758 0 : _TIFFfreeExt(tif, data);
1759 0 : return (err);
1760 : }
1761 0 : *value = data;
1762 0 : return (TIFFReadDirEntryErrOk);
1763 : }
1764 :
1765 : static enum TIFFReadDirEntryErr
1766 68938 : 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 68938 : switch (direntry->tdir_type)
1773 : {
1774 68910 : 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 68910 : break;
1783 28 : default:
1784 28 : return (TIFFReadDirEntryErrType);
1785 : }
1786 68910 : err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1787 68896 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1788 : {
1789 0 : *value = 0;
1790 0 : return (err);
1791 : }
1792 68929 : switch (direntry->tdir_type)
1793 : {
1794 68853 : case TIFF_SHORT:
1795 68853 : *value = (uint16_t *)origdata;
1796 68853 : if (tif->tif_flags & TIFF_SWAB)
1797 1014 : TIFFSwabArrayOfShort(*value, count);
1798 68792 : return (TIFFReadDirEntryErrOk);
1799 0 : case TIFF_SSHORT:
1800 : {
1801 : int16_t *m;
1802 : uint32_t n;
1803 0 : m = (int16_t *)origdata;
1804 0 : for (n = 0; n < count; n++)
1805 : {
1806 0 : if (tif->tif_flags & TIFF_SWAB)
1807 0 : TIFFSwabShort((uint16_t *)m);
1808 0 : err = TIFFReadDirEntryCheckRangeShortSshort(*m);
1809 0 : if (err != TIFFReadDirEntryErrOk)
1810 : {
1811 0 : _TIFFfreeExt(tif, origdata);
1812 0 : return (err);
1813 : }
1814 0 : m++;
1815 : }
1816 0 : *value = (uint16_t *)origdata;
1817 0 : return (TIFFReadDirEntryErrOk);
1818 : }
1819 76 : default:
1820 76 : break;
1821 : }
1822 76 : data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
1823 0 : if (data == 0)
1824 : {
1825 0 : _TIFFfreeExt(tif, origdata);
1826 0 : return (TIFFReadDirEntryErrAlloc);
1827 : }
1828 0 : switch (direntry->tdir_type)
1829 : {
1830 0 : case TIFF_BYTE:
1831 : {
1832 : uint8_t *ma;
1833 : uint16_t *mb;
1834 : uint32_t n;
1835 0 : ma = (uint8_t *)origdata;
1836 0 : mb = data;
1837 0 : for (n = 0; n < count; n++)
1838 0 : *mb++ = (uint16_t)(*ma++);
1839 : }
1840 0 : break;
1841 0 : case TIFF_SBYTE:
1842 : {
1843 : int8_t *ma;
1844 : uint16_t *mb;
1845 : uint32_t n;
1846 0 : ma = (int8_t *)origdata;
1847 0 : mb = data;
1848 0 : for (n = 0; n < count; n++)
1849 : {
1850 0 : err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1851 0 : if (err != TIFFReadDirEntryErrOk)
1852 0 : break;
1853 0 : *mb++ = (uint16_t)(*ma++);
1854 : }
1855 : }
1856 0 : break;
1857 0 : case TIFF_LONG:
1858 : {
1859 : uint32_t *ma;
1860 : uint16_t *mb;
1861 : uint32_t n;
1862 0 : ma = (uint32_t *)origdata;
1863 0 : mb = data;
1864 0 : for (n = 0; n < count; n++)
1865 : {
1866 0 : if (tif->tif_flags & TIFF_SWAB)
1867 0 : TIFFSwabLong(ma);
1868 0 : err = TIFFReadDirEntryCheckRangeShortLong(*ma);
1869 0 : if (err != TIFFReadDirEntryErrOk)
1870 0 : break;
1871 0 : *mb++ = (uint16_t)(*ma++);
1872 : }
1873 : }
1874 0 : break;
1875 0 : case TIFF_SLONG:
1876 : {
1877 : int32_t *ma;
1878 : uint16_t *mb;
1879 : uint32_t n;
1880 0 : ma = (int32_t *)origdata;
1881 0 : mb = data;
1882 0 : for (n = 0; n < count; n++)
1883 : {
1884 0 : if (tif->tif_flags & TIFF_SWAB)
1885 0 : TIFFSwabLong((uint32_t *)ma);
1886 0 : err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
1887 0 : if (err != TIFFReadDirEntryErrOk)
1888 0 : break;
1889 0 : *mb++ = (uint16_t)(*ma++);
1890 : }
1891 : }
1892 0 : break;
1893 0 : case TIFF_LONG8:
1894 : {
1895 : uint64_t *ma;
1896 : uint16_t *mb;
1897 : uint32_t n;
1898 0 : ma = (uint64_t *)origdata;
1899 0 : mb = data;
1900 0 : for (n = 0; n < count; n++)
1901 : {
1902 0 : if (tif->tif_flags & TIFF_SWAB)
1903 0 : TIFFSwabLong8(ma);
1904 0 : err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
1905 0 : if (err != TIFFReadDirEntryErrOk)
1906 0 : break;
1907 0 : *mb++ = (uint16_t)(*ma++);
1908 : }
1909 : }
1910 0 : break;
1911 0 : case TIFF_SLONG8:
1912 : {
1913 : int64_t *ma;
1914 : uint16_t *mb;
1915 : uint32_t n;
1916 0 : ma = (int64_t *)origdata;
1917 0 : mb = data;
1918 0 : for (n = 0; n < count; n++)
1919 : {
1920 0 : if (tif->tif_flags & TIFF_SWAB)
1921 0 : TIFFSwabLong8((uint64_t *)ma);
1922 0 : err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1923 0 : if (err != TIFFReadDirEntryErrOk)
1924 0 : break;
1925 0 : *mb++ = (uint16_t)(*ma++);
1926 : }
1927 : }
1928 0 : break;
1929 0 : default:
1930 0 : break;
1931 : }
1932 0 : _TIFFfreeExt(tif, origdata);
1933 0 : if (err != TIFFReadDirEntryErrOk)
1934 : {
1935 0 : _TIFFfreeExt(tif, data);
1936 0 : return (err);
1937 : }
1938 0 : *value = data;
1939 0 : return (TIFFReadDirEntryErrOk);
1940 : }
1941 :
1942 : static enum TIFFReadDirEntryErr
1943 0 : 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 0 : switch (direntry->tdir_type)
1950 : {
1951 0 : 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 0 : break;
1960 0 : default:
1961 0 : return (TIFFReadDirEntryErrType);
1962 : }
1963 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1964 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1965 : {
1966 0 : *value = 0;
1967 0 : return (err);
1968 : }
1969 0 : switch (direntry->tdir_type)
1970 : {
1971 0 : case TIFF_SHORT:
1972 : {
1973 : uint16_t *m;
1974 : uint32_t n;
1975 0 : m = (uint16_t *)origdata;
1976 0 : for (n = 0; n < count; n++)
1977 : {
1978 0 : if (tif->tif_flags & TIFF_SWAB)
1979 0 : TIFFSwabShort(m);
1980 0 : err = TIFFReadDirEntryCheckRangeSshortShort(*m);
1981 0 : if (err != TIFFReadDirEntryErrOk)
1982 : {
1983 0 : _TIFFfreeExt(tif, origdata);
1984 0 : return (err);
1985 : }
1986 0 : m++;
1987 : }
1988 0 : *value = (int16_t *)origdata;
1989 0 : return (TIFFReadDirEntryErrOk);
1990 : }
1991 0 : case TIFF_SSHORT:
1992 0 : *value = (int16_t *)origdata;
1993 0 : if (tif->tif_flags & TIFF_SWAB)
1994 0 : TIFFSwabArrayOfShort((uint16_t *)(*value), count);
1995 0 : return (TIFFReadDirEntryErrOk);
1996 0 : default:
1997 0 : break;
1998 : }
1999 0 : data = (int16_t *)_TIFFmallocExt(tif, count * 2);
2000 0 : if (data == 0)
2001 : {
2002 0 : _TIFFfreeExt(tif, origdata);
2003 0 : return (TIFFReadDirEntryErrAlloc);
2004 : }
2005 0 : switch (direntry->tdir_type)
2006 : {
2007 0 : case TIFF_BYTE:
2008 : {
2009 : uint8_t *ma;
2010 : int16_t *mb;
2011 : uint32_t n;
2012 0 : ma = (uint8_t *)origdata;
2013 0 : mb = data;
2014 0 : for (n = 0; n < count; n++)
2015 0 : *mb++ = (int16_t)(*ma++);
2016 : }
2017 0 : break;
2018 0 : case TIFF_SBYTE:
2019 : {
2020 : int8_t *ma;
2021 : int16_t *mb;
2022 : uint32_t n;
2023 0 : ma = (int8_t *)origdata;
2024 0 : mb = data;
2025 0 : for (n = 0; n < count; n++)
2026 0 : *mb++ = (int16_t)(*ma++);
2027 : }
2028 0 : break;
2029 0 : case TIFF_LONG:
2030 : {
2031 : uint32_t *ma;
2032 : int16_t *mb;
2033 : uint32_t n;
2034 0 : ma = (uint32_t *)origdata;
2035 0 : mb = data;
2036 0 : for (n = 0; n < count; n++)
2037 : {
2038 0 : if (tif->tif_flags & TIFF_SWAB)
2039 0 : TIFFSwabLong(ma);
2040 0 : err = TIFFReadDirEntryCheckRangeSshortLong(*ma);
2041 0 : if (err != TIFFReadDirEntryErrOk)
2042 0 : break;
2043 0 : *mb++ = (int16_t)(*ma++);
2044 : }
2045 : }
2046 0 : break;
2047 0 : case TIFF_SLONG:
2048 : {
2049 : int32_t *ma;
2050 : int16_t *mb;
2051 : uint32_t n;
2052 0 : ma = (int32_t *)origdata;
2053 0 : mb = data;
2054 0 : for (n = 0; n < count; n++)
2055 : {
2056 0 : if (tif->tif_flags & TIFF_SWAB)
2057 0 : TIFFSwabLong((uint32_t *)ma);
2058 0 : err = TIFFReadDirEntryCheckRangeSshortSlong(*ma);
2059 0 : if (err != TIFFReadDirEntryErrOk)
2060 0 : break;
2061 0 : *mb++ = (int16_t)(*ma++);
2062 : }
2063 : }
2064 0 : break;
2065 0 : case TIFF_LONG8:
2066 : {
2067 : uint64_t *ma;
2068 : int16_t *mb;
2069 : uint32_t n;
2070 0 : ma = (uint64_t *)origdata;
2071 0 : mb = data;
2072 0 : for (n = 0; n < count; n++)
2073 : {
2074 0 : if (tif->tif_flags & TIFF_SWAB)
2075 0 : TIFFSwabLong8(ma);
2076 0 : err = TIFFReadDirEntryCheckRangeSshortLong8(*ma);
2077 0 : if (err != TIFFReadDirEntryErrOk)
2078 0 : break;
2079 0 : *mb++ = (int16_t)(*ma++);
2080 : }
2081 : }
2082 0 : break;
2083 0 : case TIFF_SLONG8:
2084 : {
2085 : int64_t *ma;
2086 : int16_t *mb;
2087 : uint32_t n;
2088 0 : ma = (int64_t *)origdata;
2089 0 : mb = data;
2090 0 : for (n = 0; n < count; n++)
2091 : {
2092 0 : if (tif->tif_flags & TIFF_SWAB)
2093 0 : TIFFSwabLong8((uint64_t *)ma);
2094 0 : err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
2095 0 : if (err != TIFFReadDirEntryErrOk)
2096 0 : break;
2097 0 : *mb++ = (int16_t)(*ma++);
2098 : }
2099 : }
2100 0 : break;
2101 0 : default:
2102 0 : break;
2103 : }
2104 0 : _TIFFfreeExt(tif, origdata);
2105 0 : if (err != TIFFReadDirEntryErrOk)
2106 : {
2107 0 : _TIFFfreeExt(tif, data);
2108 0 : return (err);
2109 : }
2110 0 : *value = data;
2111 0 : return (TIFFReadDirEntryErrOk);
2112 : }
2113 :
2114 : static enum TIFFReadDirEntryErr
2115 800 : 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 800 : switch (direntry->tdir_type)
2122 : {
2123 800 : 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 800 : break;
2134 0 : default:
2135 0 : return (TIFFReadDirEntryErrType);
2136 : }
2137 800 : err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2138 800 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2139 : {
2140 9 : *value = 0;
2141 9 : return (err);
2142 : }
2143 791 : switch (direntry->tdir_type)
2144 : {
2145 791 : case TIFF_LONG:
2146 : case TIFF_IFD:
2147 791 : *value = (uint32_t *)origdata;
2148 791 : if (tif->tif_flags & TIFF_SWAB)
2149 0 : TIFFSwabArrayOfLong(*value, count);
2150 791 : return (TIFFReadDirEntryErrOk);
2151 0 : case TIFF_SLONG:
2152 : {
2153 : int32_t *m;
2154 : uint32_t n;
2155 0 : m = (int32_t *)origdata;
2156 0 : for (n = 0; n < count; n++)
2157 : {
2158 0 : if (tif->tif_flags & TIFF_SWAB)
2159 0 : TIFFSwabLong((uint32_t *)m);
2160 0 : err = TIFFReadDirEntryCheckRangeLongSlong(*m);
2161 0 : if (err != TIFFReadDirEntryErrOk)
2162 : {
2163 0 : _TIFFfreeExt(tif, origdata);
2164 0 : return (err);
2165 : }
2166 0 : m++;
2167 : }
2168 0 : *value = (uint32_t *)origdata;
2169 0 : return (TIFFReadDirEntryErrOk);
2170 : }
2171 0 : default:
2172 0 : break;
2173 : }
2174 0 : data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
2175 0 : if (data == 0)
2176 : {
2177 0 : _TIFFfreeExt(tif, origdata);
2178 0 : return (TIFFReadDirEntryErrAlloc);
2179 : }
2180 0 : switch (direntry->tdir_type)
2181 : {
2182 0 : case TIFF_BYTE:
2183 : {
2184 : uint8_t *ma;
2185 : uint32_t *mb;
2186 : uint32_t n;
2187 0 : ma = (uint8_t *)origdata;
2188 0 : mb = data;
2189 0 : for (n = 0; n < count; n++)
2190 0 : *mb++ = (uint32_t)(*ma++);
2191 : }
2192 0 : break;
2193 0 : case TIFF_SBYTE:
2194 : {
2195 : int8_t *ma;
2196 : uint32_t *mb;
2197 : uint32_t n;
2198 0 : ma = (int8_t *)origdata;
2199 0 : mb = data;
2200 0 : for (n = 0; n < count; n++)
2201 : {
2202 0 : err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
2203 0 : if (err != TIFFReadDirEntryErrOk)
2204 0 : break;
2205 0 : *mb++ = (uint32_t)(*ma++);
2206 : }
2207 : }
2208 0 : break;
2209 0 : case TIFF_SHORT:
2210 : {
2211 : uint16_t *ma;
2212 : uint32_t *mb;
2213 : uint32_t n;
2214 0 : ma = (uint16_t *)origdata;
2215 0 : mb = data;
2216 0 : for (n = 0; n < count; n++)
2217 : {
2218 0 : if (tif->tif_flags & TIFF_SWAB)
2219 0 : TIFFSwabShort(ma);
2220 0 : *mb++ = (uint32_t)(*ma++);
2221 : }
2222 : }
2223 0 : break;
2224 0 : case TIFF_SSHORT:
2225 : {
2226 : int16_t *ma;
2227 : uint32_t *mb;
2228 : uint32_t n;
2229 0 : ma = (int16_t *)origdata;
2230 0 : mb = data;
2231 0 : for (n = 0; n < count; n++)
2232 : {
2233 0 : if (tif->tif_flags & TIFF_SWAB)
2234 0 : TIFFSwabShort((uint16_t *)ma);
2235 0 : err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
2236 0 : if (err != TIFFReadDirEntryErrOk)
2237 0 : break;
2238 0 : *mb++ = (uint32_t)(*ma++);
2239 : }
2240 : }
2241 0 : break;
2242 0 : case TIFF_LONG8:
2243 : case TIFF_IFD8:
2244 : {
2245 : uint64_t *ma;
2246 : uint32_t *mb;
2247 : uint32_t n;
2248 0 : ma = (uint64_t *)origdata;
2249 0 : mb = data;
2250 0 : for (n = 0; n < count; n++)
2251 : {
2252 0 : if (tif->tif_flags & TIFF_SWAB)
2253 0 : TIFFSwabLong8(ma);
2254 0 : err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
2255 0 : if (err != TIFFReadDirEntryErrOk)
2256 0 : break;
2257 0 : *mb++ = (uint32_t)(*ma++);
2258 : }
2259 : }
2260 0 : break;
2261 0 : case TIFF_SLONG8:
2262 : {
2263 : int64_t *ma;
2264 : uint32_t *mb;
2265 : uint32_t n;
2266 0 : ma = (int64_t *)origdata;
2267 0 : mb = data;
2268 0 : for (n = 0; n < count; n++)
2269 : {
2270 0 : if (tif->tif_flags & TIFF_SWAB)
2271 0 : TIFFSwabLong8((uint64_t *)ma);
2272 0 : err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
2273 0 : if (err != TIFFReadDirEntryErrOk)
2274 0 : break;
2275 0 : *mb++ = (uint32_t)(*ma++);
2276 : }
2277 : }
2278 0 : break;
2279 0 : default:
2280 0 : break;
2281 : }
2282 0 : _TIFFfreeExt(tif, origdata);
2283 0 : if (err != TIFFReadDirEntryErrOk)
2284 : {
2285 0 : _TIFFfreeExt(tif, data);
2286 0 : return (err);
2287 : }
2288 0 : *value = data;
2289 0 : return (TIFFReadDirEntryErrOk);
2290 : }
2291 :
2292 : static enum TIFFReadDirEntryErr
2293 0 : 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 0 : switch (direntry->tdir_type)
2300 : {
2301 0 : 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 0 : break;
2310 0 : default:
2311 0 : return (TIFFReadDirEntryErrType);
2312 : }
2313 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2314 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2315 : {
2316 0 : *value = 0;
2317 0 : return (err);
2318 : }
2319 0 : switch (direntry->tdir_type)
2320 : {
2321 0 : case TIFF_LONG:
2322 : {
2323 : uint32_t *m;
2324 : uint32_t n;
2325 0 : m = (uint32_t *)origdata;
2326 0 : for (n = 0; n < count; n++)
2327 : {
2328 0 : if (tif->tif_flags & TIFF_SWAB)
2329 0 : TIFFSwabLong((uint32_t *)m);
2330 0 : err = TIFFReadDirEntryCheckRangeSlongLong(*m);
2331 0 : if (err != TIFFReadDirEntryErrOk)
2332 : {
2333 0 : _TIFFfreeExt(tif, origdata);
2334 0 : return (err);
2335 : }
2336 0 : m++;
2337 : }
2338 0 : *value = (int32_t *)origdata;
2339 0 : return (TIFFReadDirEntryErrOk);
2340 : }
2341 0 : case TIFF_SLONG:
2342 0 : *value = (int32_t *)origdata;
2343 0 : if (tif->tif_flags & TIFF_SWAB)
2344 0 : TIFFSwabArrayOfLong((uint32_t *)(*value), count);
2345 0 : return (TIFFReadDirEntryErrOk);
2346 0 : default:
2347 0 : break;
2348 : }
2349 0 : data = (int32_t *)_TIFFmallocExt(tif, count * 4);
2350 0 : if (data == 0)
2351 : {
2352 0 : _TIFFfreeExt(tif, origdata);
2353 0 : return (TIFFReadDirEntryErrAlloc);
2354 : }
2355 0 : switch (direntry->tdir_type)
2356 : {
2357 0 : case TIFF_BYTE:
2358 : {
2359 : uint8_t *ma;
2360 : int32_t *mb;
2361 : uint32_t n;
2362 0 : ma = (uint8_t *)origdata;
2363 0 : mb = data;
2364 0 : for (n = 0; n < count; n++)
2365 0 : *mb++ = (int32_t)(*ma++);
2366 : }
2367 0 : break;
2368 0 : case TIFF_SBYTE:
2369 : {
2370 : int8_t *ma;
2371 : int32_t *mb;
2372 : uint32_t n;
2373 0 : ma = (int8_t *)origdata;
2374 0 : mb = data;
2375 0 : for (n = 0; n < count; n++)
2376 0 : *mb++ = (int32_t)(*ma++);
2377 : }
2378 0 : break;
2379 0 : case TIFF_SHORT:
2380 : {
2381 : uint16_t *ma;
2382 : int32_t *mb;
2383 : uint32_t n;
2384 0 : ma = (uint16_t *)origdata;
2385 0 : mb = data;
2386 0 : for (n = 0; n < count; n++)
2387 : {
2388 0 : if (tif->tif_flags & TIFF_SWAB)
2389 0 : TIFFSwabShort(ma);
2390 0 : *mb++ = (int32_t)(*ma++);
2391 : }
2392 : }
2393 0 : break;
2394 0 : case TIFF_SSHORT:
2395 : {
2396 : int16_t *ma;
2397 : int32_t *mb;
2398 : uint32_t n;
2399 0 : ma = (int16_t *)origdata;
2400 0 : mb = data;
2401 0 : for (n = 0; n < count; n++)
2402 : {
2403 0 : if (tif->tif_flags & TIFF_SWAB)
2404 0 : TIFFSwabShort((uint16_t *)ma);
2405 0 : *mb++ = (int32_t)(*ma++);
2406 : }
2407 : }
2408 0 : break;
2409 0 : case TIFF_LONG8:
2410 : {
2411 : uint64_t *ma;
2412 : int32_t *mb;
2413 : uint32_t n;
2414 0 : ma = (uint64_t *)origdata;
2415 0 : mb = data;
2416 0 : for (n = 0; n < count; n++)
2417 : {
2418 0 : if (tif->tif_flags & TIFF_SWAB)
2419 0 : TIFFSwabLong8(ma);
2420 0 : err = TIFFReadDirEntryCheckRangeSlongLong8(*ma);
2421 0 : if (err != TIFFReadDirEntryErrOk)
2422 0 : break;
2423 0 : *mb++ = (int32_t)(*ma++);
2424 : }
2425 : }
2426 0 : break;
2427 0 : case TIFF_SLONG8:
2428 : {
2429 : int64_t *ma;
2430 : int32_t *mb;
2431 : uint32_t n;
2432 0 : ma = (int64_t *)origdata;
2433 0 : mb = data;
2434 0 : for (n = 0; n < count; n++)
2435 : {
2436 0 : if (tif->tif_flags & TIFF_SWAB)
2437 0 : TIFFSwabLong8((uint64_t *)ma);
2438 0 : err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
2439 0 : if (err != TIFFReadDirEntryErrOk)
2440 0 : break;
2441 0 : *mb++ = (int32_t)(*ma++);
2442 : }
2443 : }
2444 0 : break;
2445 0 : default:
2446 0 : break;
2447 : }
2448 0 : _TIFFfreeExt(tif, origdata);
2449 0 : if (err != TIFFReadDirEntryErrOk)
2450 : {
2451 0 : _TIFFfreeExt(tif, data);
2452 0 : return (err);
2453 : }
2454 0 : *value = data;
2455 0 : return (TIFFReadDirEntryErrOk);
2456 : }
2457 :
2458 : static enum TIFFReadDirEntryErr
2459 85969 : 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 85969 : switch (direntry->tdir_type)
2467 : {
2468 85945 : 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 85945 : break;
2479 24 : default:
2480 24 : return (TIFFReadDirEntryErrType);
2481 : }
2482 85945 : err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
2483 : maxcount);
2484 85935 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2485 : {
2486 23 : *value = 0;
2487 23 : return (err);
2488 : }
2489 85912 : switch (direntry->tdir_type)
2490 : {
2491 547 : case TIFF_LONG8:
2492 : case TIFF_IFD8:
2493 547 : *value = (uint64_t *)origdata;
2494 547 : if (tif->tif_flags & TIFF_SWAB)
2495 15 : TIFFSwabArrayOfLong8(*value, count);
2496 547 : return (TIFFReadDirEntryErrOk);
2497 1 : case TIFF_SLONG8:
2498 : {
2499 : int64_t *m;
2500 : uint32_t n;
2501 1 : m = (int64_t *)origdata;
2502 2 : for (n = 0; n < count; n++)
2503 : {
2504 1 : if (tif->tif_flags & TIFF_SWAB)
2505 0 : TIFFSwabLong8((uint64_t *)m);
2506 1 : err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2507 1 : if (err != TIFFReadDirEntryErrOk)
2508 : {
2509 0 : _TIFFfreeExt(tif, origdata);
2510 0 : return (err);
2511 : }
2512 1 : m++;
2513 : }
2514 1 : *value = (uint64_t *)origdata;
2515 1 : return (TIFFReadDirEntryErrOk);
2516 : }
2517 85364 : default:
2518 85364 : break;
2519 : }
2520 85364 : data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
2521 85488 : if (data == 0)
2522 : {
2523 51 : _TIFFfreeExt(tif, origdata);
2524 0 : return (TIFFReadDirEntryErrAlloc);
2525 : }
2526 85437 : switch (direntry->tdir_type)
2527 : {
2528 1 : case TIFF_BYTE:
2529 : {
2530 : uint8_t *ma;
2531 : uint64_t *mb;
2532 : uint32_t n;
2533 1 : ma = (uint8_t *)origdata;
2534 1 : mb = data;
2535 2 : for (n = 0; n < count; n++)
2536 1 : *mb++ = (uint64_t)(*ma++);
2537 : }
2538 1 : break;
2539 0 : case TIFF_SBYTE:
2540 : {
2541 : int8_t *ma;
2542 : uint64_t *mb;
2543 : uint32_t n;
2544 0 : ma = (int8_t *)origdata;
2545 0 : mb = data;
2546 0 : for (n = 0; n < count; n++)
2547 : {
2548 0 : err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2549 0 : if (err != TIFFReadDirEntryErrOk)
2550 0 : break;
2551 0 : *mb++ = (uint64_t)(*ma++);
2552 : }
2553 : }
2554 0 : break;
2555 4140 : case TIFF_SHORT:
2556 : {
2557 : uint16_t *ma;
2558 : uint64_t *mb;
2559 : uint32_t n;
2560 4140 : ma = (uint16_t *)origdata;
2561 4140 : mb = data;
2562 3438180 : for (n = 0; n < count; n++)
2563 : {
2564 3434040 : if (tif->tif_flags & TIFF_SWAB)
2565 13767 : TIFFSwabShort(ma);
2566 3434040 : *mb++ = (uint64_t)(*ma++);
2567 : }
2568 : }
2569 4140 : break;
2570 0 : case TIFF_SSHORT:
2571 : {
2572 : int16_t *ma;
2573 : uint64_t *mb;
2574 : uint32_t n;
2575 0 : ma = (int16_t *)origdata;
2576 0 : mb = data;
2577 0 : for (n = 0; n < count; n++)
2578 : {
2579 0 : if (tif->tif_flags & TIFF_SWAB)
2580 0 : TIFFSwabShort((uint16_t *)ma);
2581 0 : err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2582 0 : if (err != TIFFReadDirEntryErrOk)
2583 0 : break;
2584 0 : *mb++ = (uint64_t)(*ma++);
2585 : }
2586 : }
2587 0 : break;
2588 81296 : case TIFF_LONG:
2589 : case TIFF_IFD:
2590 : {
2591 : uint32_t *ma;
2592 : uint64_t *mb;
2593 : uint32_t n;
2594 81296 : ma = (uint32_t *)origdata;
2595 81296 : mb = data;
2596 7863590 : for (n = 0; n < count; n++)
2597 : {
2598 7782310 : if (tif->tif_flags & TIFF_SWAB)
2599 415494 : TIFFSwabLong(ma);
2600 7782290 : *mb++ = (uint64_t)(*ma++);
2601 : }
2602 : }
2603 81281 : break;
2604 0 : case TIFF_SLONG:
2605 : {
2606 : int32_t *ma;
2607 : uint64_t *mb;
2608 : uint32_t n;
2609 0 : ma = (int32_t *)origdata;
2610 0 : mb = data;
2611 0 : for (n = 0; n < count; n++)
2612 : {
2613 0 : if (tif->tif_flags & TIFF_SWAB)
2614 0 : TIFFSwabLong((uint32_t *)ma);
2615 0 : err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2616 0 : if (err != TIFFReadDirEntryErrOk)
2617 0 : break;
2618 0 : *mb++ = (uint64_t)(*ma++);
2619 : }
2620 : }
2621 0 : break;
2622 0 : default:
2623 0 : break;
2624 : }
2625 85422 : _TIFFfreeExt(tif, origdata);
2626 85444 : if (err != TIFFReadDirEntryErrOk)
2627 : {
2628 49 : _TIFFfreeExt(tif, data);
2629 0 : return (err);
2630 : }
2631 85395 : *value = data;
2632 85395 : return (TIFFReadDirEntryErrOk);
2633 : }
2634 :
2635 : static enum TIFFReadDirEntryErr
2636 6 : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
2637 : {
2638 6 : return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
2639 : ~((uint64_t)0));
2640 : }
2641 :
2642 : static enum TIFFReadDirEntryErr
2643 0 : 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 0 : switch (direntry->tdir_type)
2650 : {
2651 0 : 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 0 : break;
2660 0 : default:
2661 0 : return (TIFFReadDirEntryErrType);
2662 : }
2663 0 : err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
2664 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2665 : {
2666 0 : *value = 0;
2667 0 : return (err);
2668 : }
2669 0 : switch (direntry->tdir_type)
2670 : {
2671 0 : case TIFF_LONG8:
2672 : {
2673 : uint64_t *m;
2674 : uint32_t n;
2675 0 : m = (uint64_t *)origdata;
2676 0 : for (n = 0; n < count; n++)
2677 : {
2678 0 : if (tif->tif_flags & TIFF_SWAB)
2679 0 : TIFFSwabLong8(m);
2680 0 : err = TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2681 0 : if (err != TIFFReadDirEntryErrOk)
2682 : {
2683 0 : _TIFFfreeExt(tif, origdata);
2684 0 : return (err);
2685 : }
2686 0 : m++;
2687 : }
2688 0 : *value = (int64_t *)origdata;
2689 0 : return (TIFFReadDirEntryErrOk);
2690 : }
2691 0 : case TIFF_SLONG8:
2692 0 : *value = (int64_t *)origdata;
2693 0 : if (tif->tif_flags & TIFF_SWAB)
2694 0 : TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
2695 0 : return (TIFFReadDirEntryErrOk);
2696 0 : default:
2697 0 : break;
2698 : }
2699 0 : data = (int64_t *)_TIFFmallocExt(tif, count * 8);
2700 0 : if (data == 0)
2701 : {
2702 0 : _TIFFfreeExt(tif, origdata);
2703 0 : return (TIFFReadDirEntryErrAlloc);
2704 : }
2705 0 : switch (direntry->tdir_type)
2706 : {
2707 0 : case TIFF_BYTE:
2708 : {
2709 : uint8_t *ma;
2710 : int64_t *mb;
2711 : uint32_t n;
2712 0 : ma = (uint8_t *)origdata;
2713 0 : mb = data;
2714 0 : for (n = 0; n < count; n++)
2715 0 : *mb++ = (int64_t)(*ma++);
2716 : }
2717 0 : break;
2718 0 : case TIFF_SBYTE:
2719 : {
2720 : int8_t *ma;
2721 : int64_t *mb;
2722 : uint32_t n;
2723 0 : ma = (int8_t *)origdata;
2724 0 : mb = data;
2725 0 : for (n = 0; n < count; n++)
2726 0 : *mb++ = (int64_t)(*ma++);
2727 : }
2728 0 : break;
2729 0 : case TIFF_SHORT:
2730 : {
2731 : uint16_t *ma;
2732 : int64_t *mb;
2733 : uint32_t n;
2734 0 : ma = (uint16_t *)origdata;
2735 0 : mb = data;
2736 0 : for (n = 0; n < count; n++)
2737 : {
2738 0 : if (tif->tif_flags & TIFF_SWAB)
2739 0 : TIFFSwabShort(ma);
2740 0 : *mb++ = (int64_t)(*ma++);
2741 : }
2742 : }
2743 0 : break;
2744 0 : case TIFF_SSHORT:
2745 : {
2746 : int16_t *ma;
2747 : int64_t *mb;
2748 : uint32_t n;
2749 0 : ma = (int16_t *)origdata;
2750 0 : mb = data;
2751 0 : for (n = 0; n < count; n++)
2752 : {
2753 0 : if (tif->tif_flags & TIFF_SWAB)
2754 0 : TIFFSwabShort((uint16_t *)ma);
2755 0 : *mb++ = (int64_t)(*ma++);
2756 : }
2757 : }
2758 0 : break;
2759 0 : case TIFF_LONG:
2760 : {
2761 : uint32_t *ma;
2762 : int64_t *mb;
2763 : uint32_t n;
2764 0 : ma = (uint32_t *)origdata;
2765 0 : mb = data;
2766 0 : for (n = 0; n < count; n++)
2767 : {
2768 0 : if (tif->tif_flags & TIFF_SWAB)
2769 0 : TIFFSwabLong(ma);
2770 0 : *mb++ = (int64_t)(*ma++);
2771 : }
2772 : }
2773 0 : break;
2774 0 : case TIFF_SLONG:
2775 : {
2776 : int32_t *ma;
2777 : int64_t *mb;
2778 : uint32_t n;
2779 0 : ma = (int32_t *)origdata;
2780 0 : mb = data;
2781 0 : for (n = 0; n < count; n++)
2782 : {
2783 0 : if (tif->tif_flags & TIFF_SWAB)
2784 0 : TIFFSwabLong((uint32_t *)ma);
2785 0 : *mb++ = (int64_t)(*ma++);
2786 : }
2787 : }
2788 0 : break;
2789 0 : default:
2790 0 : break;
2791 : }
2792 0 : _TIFFfreeExt(tif, origdata);
2793 0 : *value = data;
2794 0 : return (TIFFReadDirEntryErrOk);
2795 : }
2796 :
2797 : static enum TIFFReadDirEntryErr
2798 1026 : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
2799 : {
2800 : enum TIFFReadDirEntryErr err;
2801 : uint32_t count;
2802 : void *origdata;
2803 : float *data;
2804 1026 : switch (direntry->tdir_type)
2805 : {
2806 1026 : 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 1026 : break;
2819 0 : default:
2820 0 : return (TIFFReadDirEntryErrType);
2821 : }
2822 1026 : err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2823 1026 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2824 : {
2825 0 : *value = 0;
2826 0 : return (err);
2827 : }
2828 1026 : switch (direntry->tdir_type)
2829 : {
2830 0 : case TIFF_FLOAT:
2831 0 : if (tif->tif_flags & TIFF_SWAB)
2832 0 : TIFFSwabArrayOfLong((uint32_t *)origdata, count);
2833 : TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
2834 0 : *value = (float *)origdata;
2835 0 : return (TIFFReadDirEntryErrOk);
2836 1026 : default:
2837 1026 : break;
2838 : }
2839 : data =
2840 1026 : (float *)_TIFFmallocExt(tif, (tmsize_t)((size_t)count * sizeof(float)));
2841 1026 : if (data == 0)
2842 : {
2843 0 : _TIFFfreeExt(tif, origdata);
2844 0 : return (TIFFReadDirEntryErrAlloc);
2845 : }
2846 1026 : switch (direntry->tdir_type)
2847 : {
2848 0 : case TIFF_BYTE:
2849 : {
2850 : uint8_t *ma;
2851 : float *mb;
2852 : uint32_t n;
2853 0 : ma = (uint8_t *)origdata;
2854 0 : mb = data;
2855 0 : for (n = 0; n < count; n++)
2856 0 : *mb++ = (float)(*ma++);
2857 : }
2858 0 : break;
2859 0 : case TIFF_SBYTE:
2860 : {
2861 : int8_t *ma;
2862 : float *mb;
2863 : uint32_t n;
2864 0 : ma = (int8_t *)origdata;
2865 0 : mb = data;
2866 0 : for (n = 0; n < count; n++)
2867 0 : *mb++ = (float)(*ma++);
2868 : }
2869 0 : break;
2870 0 : case TIFF_SHORT:
2871 : {
2872 : uint16_t *ma;
2873 : float *mb;
2874 : uint32_t n;
2875 0 : ma = (uint16_t *)origdata;
2876 0 : mb = data;
2877 0 : for (n = 0; n < count; n++)
2878 : {
2879 0 : if (tif->tif_flags & TIFF_SWAB)
2880 0 : TIFFSwabShort(ma);
2881 0 : *mb++ = (float)(*ma++);
2882 : }
2883 : }
2884 0 : break;
2885 0 : case TIFF_SSHORT:
2886 : {
2887 : int16_t *ma;
2888 : float *mb;
2889 : uint32_t n;
2890 0 : ma = (int16_t *)origdata;
2891 0 : mb = data;
2892 0 : for (n = 0; n < count; n++)
2893 : {
2894 0 : if (tif->tif_flags & TIFF_SWAB)
2895 0 : TIFFSwabShort((uint16_t *)ma);
2896 0 : *mb++ = (float)(*ma++);
2897 : }
2898 : }
2899 0 : break;
2900 2 : case TIFF_LONG:
2901 : {
2902 : uint32_t *ma;
2903 : float *mb;
2904 : uint32_t n;
2905 2 : ma = (uint32_t *)origdata;
2906 2 : mb = data;
2907 14 : for (n = 0; n < count; n++)
2908 : {
2909 12 : if (tif->tif_flags & TIFF_SWAB)
2910 12 : TIFFSwabLong(ma);
2911 12 : *mb++ = (float)(*ma++);
2912 : }
2913 : }
2914 2 : break;
2915 0 : case TIFF_SLONG:
2916 : {
2917 : int32_t *ma;
2918 : float *mb;
2919 : uint32_t n;
2920 0 : ma = (int32_t *)origdata;
2921 0 : mb = data;
2922 0 : for (n = 0; n < count; n++)
2923 : {
2924 0 : if (tif->tif_flags & TIFF_SWAB)
2925 0 : TIFFSwabLong((uint32_t *)ma);
2926 0 : *mb++ = (float)(*ma++);
2927 : }
2928 : }
2929 0 : break;
2930 0 : case TIFF_LONG8:
2931 : {
2932 : uint64_t *ma;
2933 : float *mb;
2934 : uint32_t n;
2935 0 : ma = (uint64_t *)origdata;
2936 0 : mb = data;
2937 0 : for (n = 0; n < count; n++)
2938 : {
2939 0 : if (tif->tif_flags & TIFF_SWAB)
2940 0 : TIFFSwabLong8(ma);
2941 0 : *mb++ = (float)(*ma++);
2942 : }
2943 : }
2944 0 : break;
2945 0 : case TIFF_SLONG8:
2946 : {
2947 : int64_t *ma;
2948 : float *mb;
2949 : uint32_t n;
2950 0 : ma = (int64_t *)origdata;
2951 0 : mb = data;
2952 0 : for (n = 0; n < count; n++)
2953 : {
2954 0 : if (tif->tif_flags & TIFF_SWAB)
2955 0 : TIFFSwabLong8((uint64_t *)ma);
2956 0 : *mb++ = (float)(*ma++);
2957 : }
2958 : }
2959 0 : break;
2960 1024 : case TIFF_RATIONAL:
2961 : {
2962 : uint32_t *ma;
2963 : uint32_t maa;
2964 : uint32_t mab;
2965 : float *mb;
2966 : uint32_t n;
2967 1024 : ma = (uint32_t *)origdata;
2968 1024 : mb = data;
2969 7049 : for (n = 0; n < count; n++)
2970 : {
2971 6025 : if (tif->tif_flags & TIFF_SWAB)
2972 96 : TIFFSwabLong(ma);
2973 6025 : maa = *ma++;
2974 6025 : if (tif->tif_flags & TIFF_SWAB)
2975 96 : TIFFSwabLong(ma);
2976 6025 : mab = *ma++;
2977 6025 : if (mab == 0)
2978 0 : *mb++ = 0.0;
2979 : else
2980 6025 : *mb++ = (float)maa / (float)mab;
2981 : }
2982 : }
2983 1024 : break;
2984 0 : case TIFF_SRATIONAL:
2985 : {
2986 : uint32_t *ma;
2987 : int32_t maa;
2988 : uint32_t mab;
2989 : float *mb;
2990 : uint32_t n;
2991 0 : ma = (uint32_t *)origdata;
2992 0 : mb = data;
2993 0 : for (n = 0; n < count; n++)
2994 : {
2995 0 : if (tif->tif_flags & TIFF_SWAB)
2996 0 : TIFFSwabLong(ma);
2997 0 : maa = *(int32_t *)ma;
2998 0 : ma++;
2999 0 : if (tif->tif_flags & TIFF_SWAB)
3000 0 : TIFFSwabLong(ma);
3001 0 : mab = *ma++;
3002 0 : if (mab == 0)
3003 0 : *mb++ = 0.0;
3004 : else
3005 0 : *mb++ = (float)maa / (float)mab;
3006 : }
3007 : }
3008 0 : break;
3009 0 : case TIFF_DOUBLE:
3010 : {
3011 : double *ma;
3012 : float *mb;
3013 : uint32_t n;
3014 0 : if (tif->tif_flags & TIFF_SWAB)
3015 0 : TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3016 : TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3017 0 : ma = (double *)origdata;
3018 0 : mb = data;
3019 0 : for (n = 0; n < count; n++)
3020 : {
3021 0 : double val = *ma++;
3022 0 : if (val > (double)FLT_MAX)
3023 0 : val = (double)FLT_MAX;
3024 0 : else if (val < -(double)FLT_MAX)
3025 0 : val = -(double)FLT_MAX;
3026 0 : *mb++ = (float)val;
3027 : }
3028 : }
3029 0 : break;
3030 0 : default:
3031 0 : break;
3032 : }
3033 1026 : _TIFFfreeExt(tif, origdata);
3034 1026 : *value = data;
3035 1026 : return (TIFFReadDirEntryErrOk);
3036 : }
3037 :
3038 : static enum TIFFReadDirEntryErr
3039 71634 : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
3040 : {
3041 : enum TIFFReadDirEntryErr err;
3042 : uint32_t count;
3043 : void *origdata;
3044 : double *data;
3045 71634 : switch (direntry->tdir_type)
3046 : {
3047 71547 : 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 71547 : break;
3060 87 : default:
3061 87 : return (TIFFReadDirEntryErrType);
3062 : }
3063 71547 : err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3064 71517 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3065 : {
3066 94 : *value = 0;
3067 94 : return (err);
3068 : }
3069 71423 : switch (direntry->tdir_type)
3070 : {
3071 71471 : case TIFF_DOUBLE:
3072 71471 : if (tif->tif_flags & TIFF_SWAB)
3073 291 : TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3074 : TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3075 71459 : *value = (double *)origdata;
3076 71459 : return (TIFFReadDirEntryErrOk);
3077 0 : default:
3078 0 : break;
3079 : }
3080 0 : data = (double *)_TIFFmallocExt(tif,
3081 0 : (tmsize_t)((size_t)count * sizeof(double)));
3082 0 : if (data == 0)
3083 : {
3084 0 : _TIFFfreeExt(tif, origdata);
3085 0 : return (TIFFReadDirEntryErrAlloc);
3086 : }
3087 0 : switch (direntry->tdir_type)
3088 : {
3089 0 : case TIFF_BYTE:
3090 : {
3091 : uint8_t *ma;
3092 : double *mb;
3093 : uint32_t n;
3094 0 : ma = (uint8_t *)origdata;
3095 0 : mb = data;
3096 0 : for (n = 0; n < count; n++)
3097 0 : *mb++ = (double)(*ma++);
3098 : }
3099 0 : break;
3100 0 : case TIFF_SBYTE:
3101 : {
3102 : int8_t *ma;
3103 : double *mb;
3104 : uint32_t n;
3105 0 : ma = (int8_t *)origdata;
3106 0 : mb = data;
3107 0 : for (n = 0; n < count; n++)
3108 0 : *mb++ = (double)(*ma++);
3109 : }
3110 0 : break;
3111 0 : case TIFF_SHORT:
3112 : {
3113 : uint16_t *ma;
3114 : double *mb;
3115 : uint32_t n;
3116 0 : ma = (uint16_t *)origdata;
3117 0 : mb = data;
3118 0 : for (n = 0; n < count; n++)
3119 : {
3120 0 : if (tif->tif_flags & TIFF_SWAB)
3121 0 : TIFFSwabShort(ma);
3122 0 : *mb++ = (double)(*ma++);
3123 : }
3124 : }
3125 0 : break;
3126 0 : case TIFF_SSHORT:
3127 : {
3128 : int16_t *ma;
3129 : double *mb;
3130 : uint32_t n;
3131 0 : ma = (int16_t *)origdata;
3132 0 : mb = data;
3133 0 : for (n = 0; n < count; n++)
3134 : {
3135 0 : if (tif->tif_flags & TIFF_SWAB)
3136 0 : TIFFSwabShort((uint16_t *)ma);
3137 0 : *mb++ = (double)(*ma++);
3138 : }
3139 : }
3140 0 : break;
3141 0 : case TIFF_LONG:
3142 : {
3143 : uint32_t *ma;
3144 : double *mb;
3145 : uint32_t n;
3146 0 : ma = (uint32_t *)origdata;
3147 0 : mb = data;
3148 0 : for (n = 0; n < count; n++)
3149 : {
3150 0 : if (tif->tif_flags & TIFF_SWAB)
3151 0 : TIFFSwabLong(ma);
3152 0 : *mb++ = (double)(*ma++);
3153 : }
3154 : }
3155 0 : break;
3156 0 : case TIFF_SLONG:
3157 : {
3158 : int32_t *ma;
3159 : double *mb;
3160 : uint32_t n;
3161 0 : ma = (int32_t *)origdata;
3162 0 : mb = data;
3163 0 : for (n = 0; n < count; n++)
3164 : {
3165 0 : if (tif->tif_flags & TIFF_SWAB)
3166 0 : TIFFSwabLong((uint32_t *)ma);
3167 0 : *mb++ = (double)(*ma++);
3168 : }
3169 : }
3170 0 : break;
3171 0 : case TIFF_LONG8:
3172 : {
3173 : uint64_t *ma;
3174 : double *mb;
3175 : uint32_t n;
3176 0 : ma = (uint64_t *)origdata;
3177 0 : mb = data;
3178 0 : for (n = 0; n < count; n++)
3179 : {
3180 0 : if (tif->tif_flags & TIFF_SWAB)
3181 0 : TIFFSwabLong8(ma);
3182 0 : *mb++ = (double)(*ma++);
3183 : }
3184 : }
3185 0 : break;
3186 0 : case TIFF_SLONG8:
3187 : {
3188 : int64_t *ma;
3189 : double *mb;
3190 : uint32_t n;
3191 0 : ma = (int64_t *)origdata;
3192 0 : mb = data;
3193 0 : for (n = 0; n < count; n++)
3194 : {
3195 0 : if (tif->tif_flags & TIFF_SWAB)
3196 0 : TIFFSwabLong8((uint64_t *)ma);
3197 0 : *mb++ = (double)(*ma++);
3198 : }
3199 : }
3200 0 : break;
3201 0 : case TIFF_RATIONAL:
3202 : {
3203 : uint32_t *ma;
3204 : uint32_t maa;
3205 : uint32_t mab;
3206 : double *mb;
3207 : uint32_t n;
3208 0 : ma = (uint32_t *)origdata;
3209 0 : mb = data;
3210 0 : for (n = 0; n < count; n++)
3211 : {
3212 0 : if (tif->tif_flags & TIFF_SWAB)
3213 0 : TIFFSwabLong(ma);
3214 0 : maa = *ma++;
3215 0 : if (tif->tif_flags & TIFF_SWAB)
3216 0 : TIFFSwabLong(ma);
3217 0 : mab = *ma++;
3218 0 : if (mab == 0)
3219 0 : *mb++ = 0.0;
3220 : else
3221 0 : *mb++ = (double)maa / (double)mab;
3222 : }
3223 : }
3224 0 : break;
3225 0 : case TIFF_SRATIONAL:
3226 : {
3227 : uint32_t *ma;
3228 : int32_t maa;
3229 : uint32_t mab;
3230 : double *mb;
3231 : uint32_t n;
3232 0 : ma = (uint32_t *)origdata;
3233 0 : mb = data;
3234 0 : for (n = 0; n < count; n++)
3235 : {
3236 0 : if (tif->tif_flags & TIFF_SWAB)
3237 0 : TIFFSwabLong(ma);
3238 0 : maa = *(int32_t *)ma;
3239 0 : ma++;
3240 0 : if (tif->tif_flags & TIFF_SWAB)
3241 0 : TIFFSwabLong(ma);
3242 0 : mab = *ma++;
3243 0 : if (mab == 0)
3244 0 : *mb++ = 0.0;
3245 : else
3246 0 : *mb++ = (double)maa / (double)mab;
3247 : }
3248 : }
3249 0 : break;
3250 0 : case TIFF_FLOAT:
3251 : {
3252 : float *ma;
3253 : double *mb;
3254 : uint32_t n;
3255 0 : if (tif->tif_flags & TIFF_SWAB)
3256 0 : TIFFSwabArrayOfLong((uint32_t *)origdata, count);
3257 : TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3258 0 : ma = (float *)origdata;
3259 0 : mb = data;
3260 0 : for (n = 0; n < count; n++)
3261 0 : *mb++ = (double)(*ma++);
3262 : }
3263 0 : break;
3264 0 : default:
3265 0 : break;
3266 : }
3267 0 : _TIFFfreeExt(tif, origdata);
3268 0 : *value = data;
3269 0 : return (TIFFReadDirEntryErrOk);
3270 : }
3271 :
3272 : static enum TIFFReadDirEntryErr
3273 81 : 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 81 : switch (direntry->tdir_type)
3280 : {
3281 81 : case TIFF_LONG:
3282 : case TIFF_LONG8:
3283 : case TIFF_IFD:
3284 : case TIFF_IFD8:
3285 81 : break;
3286 0 : default:
3287 0 : return (TIFFReadDirEntryErrType);
3288 : }
3289 81 : err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3290 81 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3291 : {
3292 0 : *value = 0;
3293 0 : return (err);
3294 : }
3295 81 : switch (direntry->tdir_type)
3296 : {
3297 0 : case TIFF_LONG8:
3298 : case TIFF_IFD8:
3299 0 : *value = (uint64_t *)origdata;
3300 0 : if (tif->tif_flags & TIFF_SWAB)
3301 0 : TIFFSwabArrayOfLong8(*value, count);
3302 0 : return (TIFFReadDirEntryErrOk);
3303 81 : default:
3304 81 : break;
3305 : }
3306 81 : data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
3307 81 : if (data == 0)
3308 : {
3309 0 : _TIFFfreeExt(tif, origdata);
3310 0 : return (TIFFReadDirEntryErrAlloc);
3311 : }
3312 81 : switch (direntry->tdir_type)
3313 : {
3314 81 : case TIFF_LONG:
3315 : case TIFF_IFD:
3316 : {
3317 : uint32_t *ma;
3318 : uint64_t *mb;
3319 : uint32_t n;
3320 81 : ma = (uint32_t *)origdata;
3321 81 : mb = data;
3322 333 : for (n = 0; n < count; n++)
3323 : {
3324 252 : if (tif->tif_flags & TIFF_SWAB)
3325 0 : TIFFSwabLong(ma);
3326 252 : *mb++ = (uint64_t)(*ma++);
3327 : }
3328 : }
3329 81 : break;
3330 0 : default:
3331 0 : break;
3332 : }
3333 81 : _TIFFfreeExt(tif, origdata);
3334 81 : *value = data;
3335 81 : return (TIFFReadDirEntryErrOk);
3336 : }
3337 :
3338 : static enum TIFFReadDirEntryErr
3339 32780 : 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 32780 : if (direntry->tdir_count != (uint64_t)tif->tif_dir.td_samplesperpixel)
3347 : {
3348 0 : const TIFFField *fip = TIFFFieldWithTag(tif, direntry->tdir_tag);
3349 0 : if (direntry->tdir_count == 0)
3350 : {
3351 0 : return TIFFReadDirEntryErrCount;
3352 : }
3353 0 : else if (direntry->tdir_count <
3354 0 : (uint64_t)tif->tif_dir.td_samplesperpixel)
3355 : {
3356 0 : 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 0 : tif->tif_dir.td_samplesperpixel);
3363 : }
3364 : else
3365 : {
3366 0 : 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 0 : tif->tif_dir.td_samplesperpixel);
3373 : }
3374 : }
3375 32780 : err = TIFFReadDirEntryShortArray(tif, direntry, &m);
3376 32781 : if (err != TIFFReadDirEntryErrOk || m == NULL)
3377 0 : return (err);
3378 32781 : na = m;
3379 32781 : nb = tif->tif_dir.td_samplesperpixel;
3380 32781 : if (direntry->tdir_count < nb)
3381 0 : nb = (uint16_t)direntry->tdir_count;
3382 32781 : *value = *na++;
3383 32781 : nb--;
3384 2593410 : while (nb > 0)
3385 : {
3386 2560630 : if (*na++ != *value)
3387 : {
3388 0 : err = TIFFReadDirEntryErrPsdif;
3389 0 : break;
3390 : }
3391 2560630 : nb--;
3392 : }
3393 32781 : _TIFFfreeExt(tif, m);
3394 32780 : return (err);
3395 : }
3396 :
3397 0 : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
3398 : uint8_t *value)
3399 : {
3400 : (void)tif;
3401 0 : *value = *(uint8_t *)(&direntry->tdir_offset);
3402 0 : }
3403 :
3404 0 : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
3405 : int8_t *value)
3406 : {
3407 : (void)tif;
3408 0 : *value = *(int8_t *)(&direntry->tdir_offset);
3409 0 : }
3410 :
3411 475368 : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
3412 : uint16_t *value)
3413 : {
3414 475368 : *value = direntry->tdir_offset.toff_short;
3415 : /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3416 475368 : if (tif->tif_flags & TIFF_SWAB)
3417 4781 : TIFFSwabShort(value);
3418 475368 : }
3419 :
3420 0 : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
3421 : int16_t *value)
3422 : {
3423 0 : *value = *(int16_t *)(&direntry->tdir_offset);
3424 0 : if (tif->tif_flags & TIFF_SWAB)
3425 0 : TIFFSwabShort((uint16_t *)value);
3426 0 : }
3427 :
3428 6125 : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
3429 : uint32_t *value)
3430 : {
3431 6125 : *value = *(uint32_t *)(&direntry->tdir_offset);
3432 6125 : if (tif->tif_flags & TIFF_SWAB)
3433 114 : TIFFSwabLong(value);
3434 6125 : }
3435 :
3436 0 : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
3437 : int32_t *value)
3438 : {
3439 0 : *value = *(int32_t *)(&direntry->tdir_offset);
3440 0 : if (tif->tif_flags & TIFF_SWAB)
3441 0 : TIFFSwabLong((uint32_t *)value);
3442 0 : }
3443 :
3444 : static enum TIFFReadDirEntryErr
3445 0 : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
3446 : {
3447 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3448 : {
3449 : enum TIFFReadDirEntryErr err;
3450 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3451 0 : if (tif->tif_flags & TIFF_SWAB)
3452 0 : TIFFSwabLong(&offset);
3453 0 : err = TIFFReadDirEntryData(tif, offset, 8, value);
3454 0 : if (err != TIFFReadDirEntryErrOk)
3455 0 : return (err);
3456 : }
3457 : else
3458 0 : *value = direntry->tdir_offset.toff_long8;
3459 0 : if (tif->tif_flags & TIFF_SWAB)
3460 0 : TIFFSwabLong8(value);
3461 0 : return (TIFFReadDirEntryErrOk);
3462 : }
3463 :
3464 : static enum TIFFReadDirEntryErr
3465 0 : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
3466 : {
3467 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3468 : {
3469 : enum TIFFReadDirEntryErr err;
3470 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3471 0 : if (tif->tif_flags & TIFF_SWAB)
3472 0 : TIFFSwabLong(&offset);
3473 0 : err = TIFFReadDirEntryData(tif, offset, 8, value);
3474 0 : if (err != TIFFReadDirEntryErrOk)
3475 0 : return (err);
3476 : }
3477 : else
3478 0 : *value = *(int64_t *)(&direntry->tdir_offset);
3479 0 : if (tif->tif_flags & TIFF_SWAB)
3480 0 : TIFFSwabLong8((uint64_t *)value);
3481 0 : return (TIFFReadDirEntryErrOk);
3482 : }
3483 :
3484 : static enum TIFFReadDirEntryErr
3485 248 : 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 248 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3494 : {
3495 : enum TIFFReadDirEntryErr err;
3496 248 : uint32_t offset = direntry->tdir_offset.toff_long;
3497 248 : if (tif->tif_flags & TIFF_SWAB)
3498 8 : TIFFSwabLong(&offset);
3499 248 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3500 248 : if (err != TIFFReadDirEntryErrOk)
3501 0 : return (err);
3502 : }
3503 : else
3504 0 : m.l = direntry->tdir_offset.toff_long8;
3505 248 : if (tif->tif_flags & TIFF_SWAB)
3506 8 : 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 248 : if (m.i[0] == 0 || m.i[1] == 0)
3511 36 : *value = 0.0;
3512 : else
3513 212 : *value = (double)m.i[0] / (double)m.i[1];
3514 248 : return (TIFFReadDirEntryErrOk);
3515 : }
3516 :
3517 : static enum TIFFReadDirEntryErr
3518 0 : 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 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3527 : {
3528 : enum TIFFReadDirEntryErr err;
3529 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3530 0 : if (tif->tif_flags & TIFF_SWAB)
3531 0 : TIFFSwabLong(&offset);
3532 0 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3533 0 : if (err != TIFFReadDirEntryErrOk)
3534 0 : return (err);
3535 : }
3536 : else
3537 0 : m.l = direntry->tdir_offset.toff_long8;
3538 0 : if (tif->tif_flags & TIFF_SWAB)
3539 0 : 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 0 : if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
3544 0 : *value = 0.0;
3545 : else
3546 0 : *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
3547 0 : 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 0 : 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 0 : float_union.i = *(uint32_t *)(&direntry->tdir_offset);
3605 0 : *value = float_union.f;
3606 0 : if (tif->tif_flags & TIFF_SWAB)
3607 0 : TIFFSwabLong((uint32_t *)value);
3608 0 : }
3609 :
3610 : static enum TIFFReadDirEntryErr
3611 0 : 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 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
3617 : {
3618 : enum TIFFReadDirEntryErr err;
3619 0 : uint32_t offset = direntry->tdir_offset.toff_long;
3620 0 : if (tif->tif_flags & TIFF_SWAB)
3621 0 : TIFFSwabLong(&offset);
3622 0 : err = TIFFReadDirEntryData(tif, offset, 8, value);
3623 0 : if (err != TIFFReadDirEntryErrOk)
3624 0 : return (err);
3625 : }
3626 : else
3627 : {
3628 : UInt64Aligned_t uint64_union;
3629 0 : uint64_union.l = direntry->tdir_offset.toff_long8;
3630 0 : *value = uint64_union.d;
3631 : }
3632 0 : if (tif->tif_flags & TIFF_SWAB)
3633 0 : TIFFSwabLong8((uint64_t *)value);
3634 0 : return (TIFFReadDirEntryErrOk);
3635 : }
3636 :
3637 : static enum TIFFReadDirEntryErr
3638 0 : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
3639 : {
3640 0 : if (value < 0)
3641 0 : return (TIFFReadDirEntryErrRange);
3642 : else
3643 0 : return (TIFFReadDirEntryErrOk);
3644 : }
3645 :
3646 : static enum TIFFReadDirEntryErr
3647 0 : TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
3648 : {
3649 0 : if (value > 0xFF)
3650 0 : return (TIFFReadDirEntryErrRange);
3651 : else
3652 0 : return (TIFFReadDirEntryErrOk);
3653 : }
3654 :
3655 : static enum TIFFReadDirEntryErr
3656 0 : TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
3657 : {
3658 0 : if ((value < 0) || (value > 0xFF))
3659 0 : return (TIFFReadDirEntryErrRange);
3660 : else
3661 0 : return (TIFFReadDirEntryErrOk);
3662 : }
3663 :
3664 : static enum TIFFReadDirEntryErr
3665 0 : TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
3666 : {
3667 0 : if (value > 0xFF)
3668 0 : return (TIFFReadDirEntryErrRange);
3669 : else
3670 0 : return (TIFFReadDirEntryErrOk);
3671 : }
3672 :
3673 : static enum TIFFReadDirEntryErr
3674 0 : TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
3675 : {
3676 0 : if ((value < 0) || (value > 0xFF))
3677 0 : return (TIFFReadDirEntryErrRange);
3678 : else
3679 0 : return (TIFFReadDirEntryErrOk);
3680 : }
3681 :
3682 : static enum TIFFReadDirEntryErr
3683 0 : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
3684 : {
3685 0 : if (value > 0xFF)
3686 0 : return (TIFFReadDirEntryErrRange);
3687 : else
3688 0 : return (TIFFReadDirEntryErrOk);
3689 : }
3690 :
3691 : static enum TIFFReadDirEntryErr
3692 0 : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
3693 : {
3694 0 : if ((value < 0) || (value > 0xFF))
3695 0 : return (TIFFReadDirEntryErrRange);
3696 : else
3697 0 : return (TIFFReadDirEntryErrOk);
3698 : }
3699 :
3700 : static enum TIFFReadDirEntryErr
3701 0 : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
3702 : {
3703 0 : if (value > 0x7F)
3704 0 : return (TIFFReadDirEntryErrRange);
3705 : else
3706 0 : return (TIFFReadDirEntryErrOk);
3707 : }
3708 :
3709 : static enum TIFFReadDirEntryErr
3710 0 : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
3711 : {
3712 0 : if (value > 0x7F)
3713 0 : return (TIFFReadDirEntryErrRange);
3714 : else
3715 0 : return (TIFFReadDirEntryErrOk);
3716 : }
3717 :
3718 : static enum TIFFReadDirEntryErr
3719 0 : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
3720 : {
3721 0 : if ((value < -0x80) || (value > 0x7F))
3722 0 : return (TIFFReadDirEntryErrRange);
3723 : else
3724 0 : return (TIFFReadDirEntryErrOk);
3725 : }
3726 :
3727 : static enum TIFFReadDirEntryErr
3728 0 : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
3729 : {
3730 0 : if (value > 0x7F)
3731 0 : return (TIFFReadDirEntryErrRange);
3732 : else
3733 0 : return (TIFFReadDirEntryErrOk);
3734 : }
3735 :
3736 : static enum TIFFReadDirEntryErr
3737 0 : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
3738 : {
3739 0 : if ((value < -0x80) || (value > 0x7F))
3740 0 : return (TIFFReadDirEntryErrRange);
3741 : else
3742 0 : return (TIFFReadDirEntryErrOk);
3743 : }
3744 :
3745 : static enum TIFFReadDirEntryErr
3746 0 : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
3747 : {
3748 0 : if (value > 0x7F)
3749 0 : return (TIFFReadDirEntryErrRange);
3750 : else
3751 0 : return (TIFFReadDirEntryErrOk);
3752 : }
3753 :
3754 : static enum TIFFReadDirEntryErr
3755 0 : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
3756 : {
3757 0 : if ((value < -0x80) || (value > 0x7F))
3758 0 : return (TIFFReadDirEntryErrRange);
3759 : else
3760 0 : return (TIFFReadDirEntryErrOk);
3761 : }
3762 :
3763 : static enum TIFFReadDirEntryErr
3764 0 : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
3765 : {
3766 0 : if (value < 0)
3767 0 : return (TIFFReadDirEntryErrRange);
3768 : else
3769 0 : return (TIFFReadDirEntryErrOk);
3770 : }
3771 :
3772 : static enum TIFFReadDirEntryErr
3773 0 : TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
3774 : {
3775 0 : if (value < 0)
3776 0 : return (TIFFReadDirEntryErrRange);
3777 : else
3778 0 : return (TIFFReadDirEntryErrOk);
3779 : }
3780 :
3781 : static enum TIFFReadDirEntryErr
3782 1 : TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
3783 : {
3784 1 : if (value > 0xFFFF)
3785 0 : return (TIFFReadDirEntryErrRange);
3786 : else
3787 1 : return (TIFFReadDirEntryErrOk);
3788 : }
3789 :
3790 : static enum TIFFReadDirEntryErr
3791 0 : TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
3792 : {
3793 0 : if ((value < 0) || (value > 0xFFFF))
3794 0 : return (TIFFReadDirEntryErrRange);
3795 : else
3796 0 : return (TIFFReadDirEntryErrOk);
3797 : }
3798 :
3799 : static enum TIFFReadDirEntryErr
3800 0 : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
3801 : {
3802 0 : if (value > 0xFFFF)
3803 0 : return (TIFFReadDirEntryErrRange);
3804 : else
3805 0 : return (TIFFReadDirEntryErrOk);
3806 : }
3807 :
3808 : static enum TIFFReadDirEntryErr
3809 0 : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
3810 : {
3811 0 : if ((value < 0) || (value > 0xFFFF))
3812 0 : return (TIFFReadDirEntryErrRange);
3813 : else
3814 0 : return (TIFFReadDirEntryErrOk);
3815 : }
3816 :
3817 : static enum TIFFReadDirEntryErr
3818 0 : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
3819 : {
3820 0 : if (value > 0x7FFF)
3821 0 : return (TIFFReadDirEntryErrRange);
3822 : else
3823 0 : return (TIFFReadDirEntryErrOk);
3824 : }
3825 :
3826 : static enum TIFFReadDirEntryErr
3827 0 : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
3828 : {
3829 0 : if (value > 0x7FFF)
3830 0 : return (TIFFReadDirEntryErrRange);
3831 : else
3832 0 : return (TIFFReadDirEntryErrOk);
3833 : }
3834 :
3835 : static enum TIFFReadDirEntryErr
3836 0 : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
3837 : {
3838 0 : if ((value < -0x8000) || (value > 0x7FFF))
3839 0 : return (TIFFReadDirEntryErrRange);
3840 : else
3841 0 : return (TIFFReadDirEntryErrOk);
3842 : }
3843 :
3844 : static enum TIFFReadDirEntryErr
3845 0 : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
3846 : {
3847 0 : if (value > 0x7FFF)
3848 0 : return (TIFFReadDirEntryErrRange);
3849 : else
3850 0 : return (TIFFReadDirEntryErrOk);
3851 : }
3852 :
3853 : static enum TIFFReadDirEntryErr
3854 0 : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
3855 : {
3856 0 : if ((value < -0x8000) || (value > 0x7FFF))
3857 0 : return (TIFFReadDirEntryErrRange);
3858 : else
3859 0 : return (TIFFReadDirEntryErrOk);
3860 : }
3861 :
3862 : static enum TIFFReadDirEntryErr
3863 0 : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
3864 : {
3865 0 : if (value < 0)
3866 0 : return (TIFFReadDirEntryErrRange);
3867 : else
3868 0 : return (TIFFReadDirEntryErrOk);
3869 : }
3870 :
3871 : static enum TIFFReadDirEntryErr
3872 0 : TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
3873 : {
3874 0 : if (value < 0)
3875 0 : return (TIFFReadDirEntryErrRange);
3876 : else
3877 0 : return (TIFFReadDirEntryErrOk);
3878 : }
3879 :
3880 : static enum TIFFReadDirEntryErr
3881 0 : TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
3882 : {
3883 0 : if (value < 0)
3884 0 : return (TIFFReadDirEntryErrRange);
3885 : else
3886 0 : return (TIFFReadDirEntryErrOk);
3887 : }
3888 :
3889 : static enum TIFFReadDirEntryErr
3890 0 : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
3891 : {
3892 0 : if (value > UINT32_MAX)
3893 0 : return (TIFFReadDirEntryErrRange);
3894 : else
3895 0 : return (TIFFReadDirEntryErrOk);
3896 : }
3897 :
3898 : static enum TIFFReadDirEntryErr
3899 0 : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
3900 : {
3901 0 : if ((value < 0) || (value > (int64_t)UINT32_MAX))
3902 0 : return (TIFFReadDirEntryErrRange);
3903 : else
3904 0 : return (TIFFReadDirEntryErrOk);
3905 : }
3906 :
3907 : static enum TIFFReadDirEntryErr
3908 0 : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
3909 : {
3910 0 : if (value > 0x7FFFFFFFUL)
3911 0 : return (TIFFReadDirEntryErrRange);
3912 : else
3913 0 : 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 0 : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
3919 : {
3920 0 : if (value > 0x7FFFFFFF)
3921 0 : return (TIFFReadDirEntryErrRange);
3922 : else
3923 0 : 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 0 : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
3929 : {
3930 0 : if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
3931 0 : return (TIFFReadDirEntryErrRange);
3932 : else
3933 0 : return (TIFFReadDirEntryErrOk);
3934 : }
3935 :
3936 : static enum TIFFReadDirEntryErr
3937 0 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
3938 : {
3939 0 : if (value < 0)
3940 0 : return (TIFFReadDirEntryErrRange);
3941 : else
3942 0 : return (TIFFReadDirEntryErrOk);
3943 : }
3944 :
3945 : static enum TIFFReadDirEntryErr
3946 0 : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
3947 : {
3948 0 : if (value < 0)
3949 0 : return (TIFFReadDirEntryErrRange);
3950 : else
3951 0 : return (TIFFReadDirEntryErrOk);
3952 : }
3953 :
3954 : static enum TIFFReadDirEntryErr
3955 0 : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
3956 : {
3957 0 : if (value < 0)
3958 0 : return (TIFFReadDirEntryErrRange);
3959 : else
3960 0 : return (TIFFReadDirEntryErrOk);
3961 : }
3962 :
3963 : static enum TIFFReadDirEntryErr
3964 1 : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
3965 : {
3966 1 : if (value < 0)
3967 0 : return (TIFFReadDirEntryErrRange);
3968 : else
3969 1 : return (TIFFReadDirEntryErrOk);
3970 : }
3971 :
3972 : static enum TIFFReadDirEntryErr
3973 0 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
3974 : {
3975 0 : if (value > INT64_MAX)
3976 0 : return (TIFFReadDirEntryErrRange);
3977 : else
3978 0 : return (TIFFReadDirEntryErrOk);
3979 : }
3980 :
3981 280 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
3982 : tmsize_t size, void *dest)
3983 : {
3984 280 : assert(size > 0);
3985 280 : if (!isMapped(tif))
3986 : {
3987 248 : if (!SeekOK(tif, offset))
3988 0 : return (TIFFReadDirEntryErrIo);
3989 248 : if (!ReadOK(tif, dest, size))
3990 0 : return (TIFFReadDirEntryErrIo);
3991 : }
3992 : else
3993 : {
3994 : size_t ma, mb;
3995 32 : ma = (size_t)offset;
3996 32 : if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
3997 : {
3998 0 : return TIFFReadDirEntryErrIo;
3999 : }
4000 32 : mb = (uint64_t)ma + (uint64_t)size;
4001 32 : if (mb > (uint64_t)tif->tif_size)
4002 0 : return (TIFFReadDirEntryErrIo);
4003 32 : _TIFFmemcpy(dest, tif->tif_base + ma, size);
4004 : }
4005 280 : return (TIFFReadDirEntryErrOk);
4006 : }
4007 :
4008 43 : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
4009 : const char *module, const char *tagname,
4010 : int recover)
4011 : {
4012 43 : if (!recover)
4013 : {
4014 2 : switch (err)
4015 : {
4016 0 : case TIFFReadDirEntryErrCount:
4017 0 : TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
4018 : tagname);
4019 0 : break;
4020 0 : case TIFFReadDirEntryErrType:
4021 0 : TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
4022 : tagname);
4023 0 : break;
4024 2 : case TIFFReadDirEntryErrIo:
4025 2 : TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
4026 : tagname);
4027 2 : break;
4028 0 : case TIFFReadDirEntryErrRange:
4029 0 : TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
4030 : tagname);
4031 0 : break;
4032 0 : case TIFFReadDirEntryErrPsdif:
4033 0 : TIFFErrorExtR(
4034 : tif, module,
4035 : "Cannot handle different values per sample for \"%s\"",
4036 : tagname);
4037 0 : break;
4038 0 : case TIFFReadDirEntryErrSizesan:
4039 0 : TIFFErrorExtR(tif, module,
4040 : "Sanity check on size of \"%s\" value failed",
4041 : tagname);
4042 0 : break;
4043 0 : case TIFFReadDirEntryErrAlloc:
4044 0 : TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
4045 : tagname);
4046 0 : break;
4047 0 : case TIFFReadDirEntryErrOk:
4048 : default:
4049 0 : assert(0); /* we should never get here */
4050 : break;
4051 : }
4052 : }
4053 : else
4054 : {
4055 41 : switch (err)
4056 : {
4057 4 : case TIFFReadDirEntryErrCount:
4058 4 : TIFFWarningExtR(tif, module,
4059 : "Incorrect count for \"%s\"; tag ignored",
4060 : tagname);
4061 4 : break;
4062 0 : case TIFFReadDirEntryErrType:
4063 0 : TIFFWarningExtR(tif, module,
4064 : "Incompatible type for \"%s\"; tag ignored",
4065 : tagname);
4066 0 : break;
4067 19 : case TIFFReadDirEntryErrIo:
4068 19 : TIFFWarningExtR(
4069 : tif, module,
4070 : "IO error during reading of \"%s\"; tag ignored", tagname);
4071 19 : break;
4072 0 : case TIFFReadDirEntryErrRange:
4073 0 : TIFFWarningExtR(tif, module,
4074 : "Incorrect value for \"%s\"; tag ignored",
4075 : tagname);
4076 0 : break;
4077 0 : case TIFFReadDirEntryErrPsdif:
4078 0 : TIFFWarningExtR(tif, module,
4079 : "Cannot handle different values per sample for "
4080 : "\"%s\"; tag ignored",
4081 : tagname);
4082 0 : break;
4083 3 : case TIFFReadDirEntryErrSizesan:
4084 3 : TIFFWarningExtR(
4085 : tif, module,
4086 : "Sanity check on size of \"%s\" value failed; tag ignored",
4087 : tagname);
4088 3 : break;
4089 15 : case TIFFReadDirEntryErrAlloc:
4090 15 : TIFFWarningExtR(tif, module,
4091 : "Out of memory reading of \"%s\"; tag ignored",
4092 : tagname);
4093 15 : break;
4094 0 : case TIFFReadDirEntryErrOk:
4095 : default:
4096 0 : assert(0); /* we should never get here */
4097 : break;
4098 : }
4099 : }
4100 43 : }
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 54853 : static int _TIFFGetMaxColorChannels(uint16_t photometric)
4108 : {
4109 54853 : switch (photometric)
4110 : {
4111 39649 : case PHOTOMETRIC_PALETTE:
4112 : case PHOTOMETRIC_MINISWHITE:
4113 : case PHOTOMETRIC_MINISBLACK:
4114 39649 : return 1;
4115 14365 : case PHOTOMETRIC_YCBCR:
4116 : case PHOTOMETRIC_RGB:
4117 : case PHOTOMETRIC_CIELAB:
4118 : case PHOTOMETRIC_LOGLUV:
4119 : case PHOTOMETRIC_ITULAB:
4120 : case PHOTOMETRIC_ICCLAB:
4121 14365 : return 3;
4122 824 : case PHOTOMETRIC_SEPARATED:
4123 : case PHOTOMETRIC_MASK:
4124 824 : return 4;
4125 15 : case PHOTOMETRIC_LOGL:
4126 : case PHOTOMETRIC_CFA:
4127 : default:
4128 15 : return 0;
4129 : }
4130 : }
4131 :
4132 34073 : 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 34073 : uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
4148 34156 : uint64_t offset = TIFFGetStrileOffset(tif, 0);
4149 : uint64_t filesize;
4150 :
4151 34118 : if (offset == 0)
4152 11077 : return 0;
4153 23041 : if (bytecount == 0)
4154 1 : return 1;
4155 23040 : if (tif->tif_dir.td_compression != COMPRESSION_NONE)
4156 774 : return 0;
4157 22266 : filesize = TIFFGetFileSize(tif);
4158 22317 : if (offset <= filesize && bytecount > filesize - offset)
4159 2 : return 1;
4160 22315 : if (tif->tif_mode == O_RDONLY)
4161 : {
4162 21169 : uint64_t scanlinesize = TIFFScanlineSize64(tif);
4163 21128 : if (tif->tif_dir.td_imagelength > 0 &&
4164 21115 : scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
4165 : {
4166 0 : return 1;
4167 : }
4168 21128 : if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
4169 2 : return 1;
4170 : }
4171 22272 : 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 368141 : static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp)
4179 : {
4180 367752 : const uint64_t data_width =
4181 368141 : (uint64_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
4182 367752 : if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
4183 : {
4184 1 : TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4185 : "Too large IFD data size");
4186 1 : return false;
4187 : }
4188 367751 : const uint64_t datalength = dp->tdir_count * data_width;
4189 367751 : if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
4190 : {
4191 200483 : if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
4192 : {
4193 0 : TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4194 : "Too large IFD data size");
4195 0 : return false;
4196 : }
4197 200483 : tif->tif_dir.td_dirdatasize_read += datalength;
4198 200483 : 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 198894 : uint32_t offset = dp->tdir_offset.toff_long;
4203 198894 : if (tif->tif_flags & TIFF_SWAB)
4204 2100 : TIFFSwabLong(&offset);
4205 : tif->tif_dir
4206 198813 : .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4207 198813 : .offset = (uint64_t)offset;
4208 : }
4209 : else
4210 : {
4211 : tif->tif_dir
4212 1589 : .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4213 1589 : .offset = dp->tdir_offset.toff_long8;
4214 1589 : if (tif->tif_flags & TIFF_SWAB)
4215 30 : TIFFSwabLong8(
4216 : &tif->tif_dir
4217 30 : .td_dirdatasize_offsets[tif->tif_dir
4218 30 : .td_dirdatasize_Noffsets]
4219 : .offset);
4220 : }
4221 : tif->tif_dir
4222 200448 : .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4223 200448 : .length = datalength;
4224 200448 : tif->tif_dir.td_dirdatasize_Noffsets++;
4225 : }
4226 367716 : return true;
4227 : }
4228 :
4229 : /*
4230 : * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
4231 : */
4232 74534 : static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
4233 : {
4234 74534 : const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a;
4235 74534 : const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b;
4236 : /* Compare offsets */
4237 74534 : if (ta->offset > tb->offset)
4238 9880 : return 1;
4239 64654 : else if (ta->offset < tb->offset)
4240 64654 : return -1;
4241 : else
4242 0 : 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 54820 : 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 54820 : if (tif->tif_mode == O_RDONLY)
4258 35673 : return;
4259 :
4260 : /* Sort TIFFEntryOffsetAndLength array in ascending order. */
4261 19147 : qsort(tif->tif_dir.td_dirdatasize_offsets,
4262 19147 : 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 19136 : if (!(tif->tif_flags & TIFF_BIGTIFF))
4268 18662 : IFDendoffset = tif->tif_diroff + 2 + (uint64_t)dircount * 12 + 4;
4269 : else
4270 474 : 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 19136 : uint64_t size = 0;
4276 : uint64_t offset;
4277 : uint32_t i;
4278 82416 : for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
4279 : {
4280 63316 : offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
4281 63316 : if (offset == IFDendoffset)
4282 : {
4283 60759 : size += tif->tif_dir.td_dirdatasize_offsets[i].length;
4284 60759 : IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
4285 : }
4286 2557 : else if (offset == IFDendoffset + 1)
4287 : {
4288 : /* Add gap byte after previous IFD data set. */
4289 2521 : size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
4290 2521 : IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
4291 : }
4292 : else
4293 : {
4294 : /* Further data is no more continuously after IFD */
4295 36 : 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 19136 : if (tif->tif_nextdiroff != 0)
4301 : {
4302 2896 : if (tif->tif_nextdiroff == IFDendoffset + 1)
4303 780 : size++;
4304 : }
4305 : else
4306 : {
4307 : /* Check for IFD data ends at EOF. Then IFD can always be safely
4308 : * overwritten. */
4309 16240 : offset = TIFFSeekFile(tif, 0, SEEK_END);
4310 16240 : if (offset == IFDendoffset)
4311 : {
4312 14739 : tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
4313 14739 : return;
4314 : }
4315 : }
4316 :
4317 : /* Finally, add the size of the IFD tag entries themselves. */
4318 4397 : if (!(tif->tif_flags & TIFF_BIGTIFF))
4319 4196 : tif->tif_dir.td_dirdatasize_read = 2U + dircount * 12U + 4U + size;
4320 : else
4321 201 : 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 63597 : 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 63597 : uint32_t fii = FAILED_FII;
4337 : toff_t nextdiroff;
4338 63597 : int bitspersample_read = FALSE;
4339 : int color_channels;
4340 :
4341 63597 : 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 8681 : tif->tif_diroff = tif->tif_nextdiroff;
4347 8681 : return 0;
4348 : }
4349 :
4350 54916 : 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 54916 : if (!_TIFFCheckDirNumberAndOffset(tif,
4355 54916 : tif->tif_curdir ==
4356 : TIFF_NON_EXISTENT_DIR_NUMBER
4357 : ? 0
4358 3824 : : tif->tif_curdir + 1,
4359 : nextdiroff))
4360 : {
4361 0 : return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
4362 : IFDs) */
4363 : }
4364 54850 : dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
4365 54830 : if (!dircount)
4366 : {
4367 10 : TIFFErrorExtR(tif, module,
4368 : "Failed to read directory at offset %" PRIu64,
4369 : nextdiroff);
4370 17 : 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 54820 : if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
4376 50997 : tif->tif_curdir = 0;
4377 : else
4378 3823 : tif->tif_curdir++;
4379 :
4380 54820 : 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 830864 : for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
4390 : {
4391 : TIFFDirEntry *na;
4392 : uint16_t nb;
4393 6032360 : for (na = ma + 1, nb = (uint16_t)(mb + 1); nb < dircount;
4394 5256390 : na++, nb++)
4395 : {
4396 5256390 : if (ma->tdir_tag == na->tdir_tag)
4397 : {
4398 12237 : na->tdir_ignore = TRUE;
4399 : }
4400 : }
4401 : }
4402 : }
4403 :
4404 54894 : tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
4405 54894 : tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
4406 54894 : 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 54894 : 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 54894 : TIFFFreeDirectory(tif);
4418 54812 : 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 54854 : 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 54814 : tif->tif_dir.td_dirdatasize_offsets =
4428 54854 : (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
4429 : tif,
4430 54854 : (tmsize_t)((size_t)dircount * sizeof(TIFFEntryOffsetAndLength)));
4431 54814 : if (tif->tif_dir.td_dirdatasize_offsets == NULL)
4432 : {
4433 0 : TIFFErrorExtR(
4434 : tif, module,
4435 : "Failed to allocate memory for counting IFD data size at reading");
4436 0 : 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 54814 : 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 54842 : TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
4466 54847 : if (dp)
4467 : {
4468 54814 : if (!TIFFFetchNormalTag(tif, dp, 0))
4469 0 : goto bad;
4470 54770 : dp->tdir_ignore = TRUE;
4471 : }
4472 54803 : dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
4473 54829 : 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 54813 : err = TIFFReadDirEntryShort(tif, dp, &value);
4484 54821 : if (err == TIFFReadDirEntryErrCount)
4485 0 : err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
4486 54764 : if (err != TIFFReadDirEntryErrOk)
4487 : {
4488 0 : TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
4489 0 : goto bad;
4490 : }
4491 54764 : if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
4492 0 : goto bad;
4493 54856 : dp->tdir_ignore = TRUE;
4494 : }
4495 : else
4496 : {
4497 16 : if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
4498 0 : goto bad;
4499 : }
4500 : /*
4501 : * First real pass over the directory.
4502 : */
4503 830970 : for (di = 0, dp = dir; di < dircount; di++, dp++)
4504 : {
4505 775752 : if (!dp->tdir_ignore)
4506 : {
4507 665535 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4508 666162 : if (fii == FAILED_FII)
4509 : {
4510 437 : if (tif->tif_warn_about_unknown_tags)
4511 : {
4512 0 : TIFFWarningExtR(tif, module,
4513 : "Unknown field with tag %" PRIu16
4514 : " (0x%" PRIx16 ") encountered",
4515 0 : dp->tdir_tag, dp->tdir_tag);
4516 : }
4517 : /* the following knowingly leaks the
4518 : anonymous field structure */
4519 437 : const TIFFField *fld = _TIFFCreateAnonField(
4520 437 : tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
4521 437 : if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
4522 : {
4523 0 : TIFFWarningExtR(
4524 : tif, module,
4525 : "Registering anonymous field with tag %" PRIu16
4526 : " (0x%" PRIx16 ") failed",
4527 0 : dp->tdir_tag, dp->tdir_tag);
4528 0 : dp->tdir_ignore = TRUE;
4529 : }
4530 : else
4531 : {
4532 437 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4533 437 : assert(fii != FAILED_FII);
4534 : }
4535 : }
4536 : }
4537 776299 : if (!dp->tdir_ignore)
4538 : {
4539 666017 : fip = tif->tif_fields[fii];
4540 666017 : if (fip->field_bit == FIELD_IGNORE)
4541 0 : dp->tdir_ignore = TRUE;
4542 : else
4543 : {
4544 666017 : switch (dp->tdir_tag)
4545 : {
4546 109739 : case TIFFTAG_STRIPOFFSETS:
4547 : case TIFFTAG_STRIPBYTECOUNTS:
4548 : case TIFFTAG_TILEOFFSETS:
4549 : case TIFFTAG_TILEBYTECOUNTS:
4550 109739 : TIFFSetFieldBit(tif, fip->field_bit);
4551 109739 : break;
4552 234765 : 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 234765 : if (!TIFFFetchNormalTag(tif, dp, 0))
4562 0 : goto bad;
4563 234641 : dp->tdir_ignore = TRUE;
4564 234641 : break;
4565 321513 : default:
4566 321513 : if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
4567 9 : dp->tdir_ignore = TRUE;
4568 321441 : break;
4569 : }
4570 110282 : }
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 55218 : if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4582 2 : (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
4583 : {
4584 0 : if (!_TIFFFillStriles(tif))
4585 0 : goto bad;
4586 0 : dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4587 : TIFFTAG_STRIPOFFSETS);
4588 0 : if ((dp != 0) && (dp->tdir_count == 1))
4589 : {
4590 0 : dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4591 : TIFFTAG_STRIPBYTECOUNTS);
4592 0 : if ((dp != 0) && (dp->tdir_count == 1))
4593 : {
4594 0 : tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
4595 0 : 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 54884 : if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
4605 : {
4606 0 : MissingRequired(tif, "ImageLength");
4607 0 : goto bad;
4608 : }
4609 :
4610 : /*
4611 : * Second pass: extract other information.
4612 : */
4613 830548 : for (di = 0, dp = dir; di < dircount; di++, dp++)
4614 : {
4615 775437 : if (!dp->tdir_ignore)
4616 : {
4617 430735 : switch (dp->tdir_tag)
4618 : {
4619 108911 : 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 108911 : err = TIFFReadDirEntryShort(tif, dp, &value);
4638 108861 : if (!EvaluateIFDdatasizeReading(tif, dp))
4639 0 : goto bad;
4640 108800 : if (err == TIFFReadDirEntryErrCount)
4641 : err =
4642 32780 : TIFFReadDirEntryPersampleShort(tif, dp, &value);
4643 108801 : if (err != TIFFReadDirEntryErrOk)
4644 : {
4645 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4646 0 : TIFFReadDirEntryOutputErr(
4647 : tif, err, module,
4648 : fip ? fip->field_name : "unknown tagname", 0);
4649 0 : goto bad;
4650 : }
4651 108801 : if (!TIFFSetField(tif, dp->tdir_tag, value))
4652 0 : goto bad;
4653 108901 : if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
4654 54817 : bitspersample_read = TRUE;
4655 : }
4656 108901 : break;
4657 0 : case TIFFTAG_SMINSAMPLEVALUE:
4658 : case TIFFTAG_SMAXSAMPLEVALUE:
4659 : {
4660 :
4661 0 : double *data = NULL;
4662 : enum TIFFReadDirEntryErr err;
4663 : uint32_t saved_flags;
4664 : int m;
4665 0 : if (dp->tdir_count !=
4666 0 : (uint64_t)tif->tif_dir.td_samplesperpixel)
4667 0 : err = TIFFReadDirEntryErrCount;
4668 : else
4669 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
4670 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
4671 0 : goto bad;
4672 0 : if (err != TIFFReadDirEntryErrOk)
4673 : {
4674 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4675 0 : TIFFReadDirEntryOutputErr(
4676 : tif, err, module,
4677 : fip ? fip->field_name : "unknown tagname", 0);
4678 0 : goto bad;
4679 : }
4680 0 : saved_flags = tif->tif_flags;
4681 0 : tif->tif_flags |= TIFF_PERSAMPLE;
4682 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
4683 0 : tif->tif_flags = saved_flags;
4684 0 : _TIFFfreeExt(tif, data);
4685 0 : if (!m)
4686 0 : goto bad;
4687 : }
4688 0 : break;
4689 54854 : case TIFFTAG_STRIPOFFSETS:
4690 : case TIFFTAG_TILEOFFSETS:
4691 : {
4692 54854 : switch (dp->tdir_type)
4693 : {
4694 53393 : case TIFF_SHORT:
4695 : case TIFF_LONG:
4696 : case TIFF_LONG8:
4697 53393 : break;
4698 1461 : default:
4699 : /* Warn except if directory typically created with
4700 : * TIFFDeferStrileArrayWriting() */
4701 1461 : if (!(tif->tif_mode == O_RDWR &&
4702 1400 : dp->tdir_count == 0 && dp->tdir_type == 0 &&
4703 1400 : dp->tdir_offset.toff_long8 == 0))
4704 : {
4705 61 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4706 11 : TIFFWarningExtR(
4707 : tif, module, "Invalid data type for tag %s",
4708 : fip ? fip->field_name : "unknown tagname");
4709 : }
4710 1411 : break;
4711 : }
4712 54804 : _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
4713 : sizeof(TIFFDirEntry));
4714 54729 : if (!EvaluateIFDdatasizeReading(tif, dp))
4715 1 : goto bad;
4716 : }
4717 54804 : break;
4718 54820 : case TIFFTAG_STRIPBYTECOUNTS:
4719 : case TIFFTAG_TILEBYTECOUNTS:
4720 : {
4721 54820 : switch (dp->tdir_type)
4722 : {
4723 53412 : case TIFF_SHORT:
4724 : case TIFF_LONG:
4725 : case TIFF_LONG8:
4726 53412 : break;
4727 1408 : default:
4728 : /* Warn except if directory typically created with
4729 : * TIFFDeferStrileArrayWriting() */
4730 1408 : if (!(tif->tif_mode == O_RDWR &&
4731 1400 : dp->tdir_count == 0 && dp->tdir_type == 0 &&
4732 1400 : dp->tdir_offset.toff_long8 == 0))
4733 : {
4734 8 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4735 9 : TIFFWarningExtR(
4736 : tif, module, "Invalid data type for tag %s",
4737 : fip ? fip->field_name : "unknown tagname");
4738 : }
4739 1409 : break;
4740 : }
4741 54821 : _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
4742 : sizeof(TIFFDirEntry));
4743 54760 : if (!EvaluateIFDdatasizeReading(tif, dp))
4744 0 : goto bad;
4745 : }
4746 54829 : break;
4747 376 : 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 376 : 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 376 : if (!bitspersample_read)
4763 : {
4764 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4765 0 : TIFFWarningExtR(
4766 : tif, module,
4767 : "Ignoring %s since BitsPerSample tag not found",
4768 : fip ? fip->field_name : "unknown tagname");
4769 0 : 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 376 : if (tif->tif_dir.td_bitspersample > 24)
4775 : {
4776 0 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4777 0 : TIFFWarningExtR(
4778 : tif, module,
4779 : "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
4780 : fip ? fip->field_name : "unknown tagname",
4781 0 : tif->tif_dir.td_bitspersample);
4782 0 : continue;
4783 : }
4784 376 : countpersample = (1U << tif->tif_dir.td_bitspersample);
4785 376 : if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
4786 15 : (dp->tdir_count == (uint64_t)countpersample))
4787 : {
4788 0 : countrequired = countpersample;
4789 0 : incrementpersample = 0;
4790 : }
4791 : else
4792 : {
4793 376 : countrequired = 3 * countpersample;
4794 376 : incrementpersample = countpersample;
4795 : }
4796 376 : if (dp->tdir_count != (uint64_t)countrequired)
4797 0 : err = TIFFReadDirEntryErrCount;
4798 : else
4799 376 : err = TIFFReadDirEntryShortArray(tif, dp, &value);
4800 376 : if (!EvaluateIFDdatasizeReading(tif, dp))
4801 0 : goto bad;
4802 376 : if (err != TIFFReadDirEntryErrOk)
4803 : {
4804 1 : fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4805 1 : TIFFReadDirEntryOutputErr(
4806 : tif, err, module,
4807 : fip ? fip->field_name : "unknown tagname", 1);
4808 : }
4809 : else
4810 : {
4811 375 : TIFFSetField(tif, dp->tdir_tag, value,
4812 375 : value + incrementpersample,
4813 375 : value + 2 * incrementpersample);
4814 375 : _TIFFfreeExt(tif, value);
4815 : }
4816 : }
4817 376 : break;
4818 : /* BEGIN REV 4.0 COMPATIBILITY */
4819 0 : case TIFFTAG_OSUBFILETYPE:
4820 : {
4821 : uint16_t valueo;
4822 : uint32_t value;
4823 0 : if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
4824 : TIFFReadDirEntryErrOk)
4825 : {
4826 0 : switch (valueo)
4827 : {
4828 0 : case OFILETYPE_REDUCEDIMAGE:
4829 0 : value = FILETYPE_REDUCEDIMAGE;
4830 0 : break;
4831 0 : case OFILETYPE_PAGE:
4832 0 : value = FILETYPE_PAGE;
4833 0 : break;
4834 0 : default:
4835 0 : value = 0;
4836 0 : break;
4837 : }
4838 0 : if (value != 0)
4839 0 : TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
4840 : }
4841 : }
4842 0 : 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 211774 : default:
4890 211774 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
4891 212052 : break;
4892 : } /* -- switch (dp->tdir_tag) -- */
4893 344702 : } /* -- if (!dp->tdir_ignore) */
4894 : } /* -- for-loop -- */
4895 :
4896 : /* Evaluate final IFD data size. */
4897 55111 : 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 54774 : if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
4916 : {
4917 2 : if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
4918 : {
4919 0 : TIFFWarningExtR(
4920 : tif, module,
4921 : "Photometric tag is missing, assuming data is YCbCr");
4922 0 : if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
4923 0 : goto bad;
4924 : }
4925 2 : else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4926 : {
4927 0 : tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
4928 0 : TIFFWarningExtR(tif, module,
4929 : "Photometric tag value assumed incorrect, "
4930 : "assuming data is YCbCr instead of RGB");
4931 : }
4932 2 : if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
4933 : {
4934 0 : TIFFWarningExtR(
4935 : tif, module,
4936 : "BitsPerSample tag is missing, assuming 8 bits per sample");
4937 0 : if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
4938 0 : goto bad;
4939 : }
4940 2 : if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
4941 : {
4942 0 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4943 : {
4944 0 : TIFFWarningExtR(tif, module,
4945 : "SamplesPerPixel tag is missing, "
4946 : "assuming correct SamplesPerPixel value is 3");
4947 0 : if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4948 0 : goto bad;
4949 : }
4950 0 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
4951 : {
4952 0 : TIFFWarningExtR(tif, module,
4953 : "SamplesPerPixel tag is missing, "
4954 : "applying correct SamplesPerPixel value of 3");
4955 0 : if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4956 0 : goto bad;
4957 : }
4958 0 : else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
4959 0 : (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 19 : if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
4966 0 : 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 54755 : if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
4977 : {
4978 45153 : tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
4979 45155 : tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
4980 45155 : tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
4981 45155 : tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
4982 45155 : tif->tif_flags &= ~TIFF_ISTILED;
4983 : }
4984 : else
4985 : {
4986 9602 : tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
4987 9602 : tif->tif_flags |= TIFF_ISTILED;
4988 : }
4989 54757 : if (!tif->tif_dir.td_nstrips)
4990 : {
4991 0 : TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
4992 0 : isTiled(tif) ? "tiles" : "strips");
4993 0 : goto bad;
4994 : }
4995 54757 : tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
4996 54757 : if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
4997 9357 : tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
4998 54757 : if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
4999 : {
5000 : #ifdef OJPEG_SUPPORT
5001 0 : if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
5002 0 : (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 0 : TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
5013 : }
5014 : else
5015 : #endif
5016 : {
5017 0 : MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
5018 0 : goto bad;
5019 : }
5020 : }
5021 :
5022 54757 : if (tif->tif_mode == O_RDWR &&
5023 19136 : tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
5024 19136 : tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
5025 1400 : tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
5026 1400 : tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
5027 1400 : tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
5028 1400 : tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
5029 1400 : tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
5030 1400 : tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
5031 : {
5032 : /* Directory typically created with TIFFDeferStrileArrayWriting() */
5033 1400 : TIFFSetupStrips(tif);
5034 : }
5035 53357 : else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
5036 : {
5037 17830 : if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
5038 : {
5039 17831 : if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
5040 : tif->tif_dir.td_nstrips,
5041 : &tif->tif_dir.td_stripoffset_p))
5042 : {
5043 0 : goto bad;
5044 : }
5045 : }
5046 17834 : if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
5047 : {
5048 17832 : if (!TIFFFetchStripThing(
5049 : tif, &(tif->tif_dir.td_stripbytecount_entry),
5050 : tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
5051 : {
5052 0 : 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 54765 : color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
5062 54710 : if (color_channels &&
5063 54720 : 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 3 : 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 3 : old_extrasamples = tif->tif_dir.td_extrasamples;
5076 3 : tif->tif_dir.td_extrasamples =
5077 3 : (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
5078 :
5079 : // sampleinfo should contain information relative to these new extra
5080 : // samples
5081 3 : new_sampleinfo = (uint16_t *)_TIFFcallocExt(
5082 3 : tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
5083 3 : if (!new_sampleinfo)
5084 : {
5085 0 : TIFFErrorExtR(tif, module,
5086 : "Failed to allocate memory for "
5087 : "temporary new sampleinfo array "
5088 : "(%" PRIu16 " 16 bit elements)",
5089 0 : tif->tif_dir.td_extrasamples);
5090 0 : goto bad;
5091 : }
5092 :
5093 3 : if (old_extrasamples > 0)
5094 0 : memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
5095 : old_extrasamples * sizeof(uint16_t));
5096 3 : _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
5097 3 : tif->tif_dir.td_extrasamples);
5098 3 : _TIFFfreeExt(tif, new_sampleinfo);
5099 : }
5100 :
5101 : /*
5102 : * Verify Palette image has a Colormap.
5103 : */
5104 54712 : if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
5105 357 : !TIFFFieldSet(tif, FIELD_COLORMAP))
5106 : {
5107 1 : if (tif->tif_dir.td_bitspersample >= 8 &&
5108 1 : tif->tif_dir.td_samplesperpixel == 3)
5109 0 : tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
5110 1 : else if (tif->tif_dir.td_bitspersample >= 8)
5111 1 : tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
5112 : else
5113 : {
5114 0 : MissingRequired(tif, "Colormap");
5115 0 : 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 54712 : if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
5124 : {
5125 : /*
5126 : * Attempt to deal with a missing StripByteCounts tag.
5127 : */
5128 54759 : 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 5 : if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5136 5 : tif->tif_dir.td_nstrips > 1) ||
5137 5 : (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
5138 0 : tif->tif_dir.td_nstrips !=
5139 0 : (uint32_t)tif->tif_dir.td_samplesperpixel))
5140 : {
5141 0 : MissingRequired(tif, "StripByteCounts");
5142 0 : goto bad;
5143 : }
5144 5 : TIFFWarningExtR(
5145 : tif, module,
5146 : "TIFF directory is missing required "
5147 : "\"StripByteCounts\" field, calculating from imagelength");
5148 5 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5149 0 : goto bad;
5150 : }
5151 54754 : else if (tif->tif_dir.td_nstrips == 1 &&
5152 38876 : !(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 5 : TIFFWarningExtR(tif, module,
5161 : "Bogus \"StripByteCounts\" field, ignoring and "
5162 : "calculating from imagelength");
5163 5 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5164 0 : goto bad;
5165 : }
5166 54786 : else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
5167 19230 : tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5168 14773 : tif->tif_dir.td_nstrips > 2 &&
5169 3767 : tif->tif_dir.td_compression == COMPRESSION_NONE &&
5170 1285 : TIFFGetStrileByteCount(tif, 0) !=
5171 1285 : TIFFGetStrileByteCount(tif, 1) &&
5172 0 : TIFFGetStrileByteCount(tif, 0) != 0 &&
5173 0 : 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 0 : TIFFWarningExtR(tif, module,
5186 : "Wrong \"StripByteCounts\" field, ignoring and "
5187 : "calculating from imagelength");
5188 0 : if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5189 0 : goto bad;
5190 : }
5191 : }
5192 54749 : if (dir)
5193 : {
5194 54880 : _TIFFfreeExt(tif, dir);
5195 54865 : dir = NULL;
5196 : }
5197 54734 : if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
5198 : {
5199 54855 : if (tif->tif_dir.td_bitspersample >= 16)
5200 10944 : tif->tif_dir.td_maxsamplevalue = 0xFFFF;
5201 : else
5202 43911 : tif->tif_dir.td_maxsamplevalue =
5203 43911 : (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 54734 : (*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 54846 : if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
5244 45459 : (tif->tif_dir.td_nstrips == 1) &&
5245 36932 : (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
5246 30300 : ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
5247 : {
5248 22377 : 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 54816 : if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5255 45430 : tif->tif_dir.td_compression == COMPRESSION_NONE &&
5256 60382 : (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
5257 25191 : TIFFStripSize64(tif) > 0x7FFFFFFFUL)
5258 : {
5259 5 : TryChopUpUncompressedBigTiff(tif);
5260 : }
5261 :
5262 : /*
5263 : * Clear the dirty directory flag.
5264 : */
5265 54881 : tif->tif_flags &= ~TIFF_DIRTYDIRECT;
5266 54881 : tif->tif_flags &= ~TIFF_DIRTYSTRIP;
5267 :
5268 : /*
5269 : * Reinitialize some further i/o since we are starting on a new directory.
5270 : */
5271 54881 : tif->tif_dir.td_scanlinesize = TIFFScanlineSize(tif);
5272 54843 : if (!tif->tif_dir.td_scanlinesize)
5273 : {
5274 26 : TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
5275 0 : return (0);
5276 : }
5277 :
5278 54817 : if (isTiled(tif))
5279 : {
5280 9602 : tif->tif_dir.td_tilesize = TIFFTileSize(tif);
5281 9603 : if (!tif->tif_dir.td_tilesize)
5282 : {
5283 0 : TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
5284 0 : return (0);
5285 : }
5286 : }
5287 : else
5288 : {
5289 45215 : if (!TIFFStripSize(tif))
5290 : {
5291 31 : TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
5292 0 : return (0);
5293 : }
5294 : }
5295 54853 : return (1);
5296 1 : bad:
5297 1 : if (dir)
5298 1 : _TIFFfreeExt(tif, dir);
5299 1 : return (0);
5300 : } /*-- TIFFReadDirectory() --*/
5301 :
5302 54855 : 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 54855 : m = 0;
5310 828817 : for (n = 0, o = dir; n < dircount; n++, o++)
5311 : {
5312 773974 : if (o->tdir_tag < m)
5313 : {
5314 12 : TIFFWarningExtR(tif, module,
5315 : "Invalid TIFF directory; tags are not sorted in "
5316 : "ascending order");
5317 12 : break;
5318 : }
5319 773962 : m = o->tdir_tag + 1U;
5320 : }
5321 54855 : }
5322 :
5323 109640 : 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 606635 : for (m = dir, n = 0; n < dircount; m++, n++)
5331 : {
5332 606661 : if (m->tdir_tag == tagid)
5333 109666 : return (m);
5334 : }
5335 0 : return (0);
5336 : }
5337 :
5338 1167460 : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
5339 : uint32_t *fii)
5340 : {
5341 : int32_t ma, mb, mc;
5342 1167460 : ma = -1;
5343 1167460 : mc = (int32_t)tif->tif_nfields;
5344 : while (1)
5345 : {
5346 7432950 : if (ma + 1 == mc)
5347 : {
5348 437 : *fii = FAILED_FII;
5349 437 : return;
5350 : }
5351 7432520 : mb = (ma + mc) / 2;
5352 7432520 : if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
5353 1167020 : break;
5354 6265500 : if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
5355 2375770 : ma = mb;
5356 : else
5357 3889720 : mc = mb;
5358 : }
5359 : while (1)
5360 : {
5361 1167880 : if (mb == 0)
5362 11876 : break;
5363 1156010 : if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
5364 1155140 : break;
5365 863 : mb--;
5366 : }
5367 1167020 : *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 0 : 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 0 : assert(infoarray != NULL);
5386 0 : dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
5387 0 : if (!dircount)
5388 : {
5389 0 : TIFFErrorExtR(tif, module,
5390 : "Failed to read custom directory at offset %" PRIu64,
5391 : diroff);
5392 0 : return 0;
5393 : }
5394 0 : 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 0 : for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
5404 : {
5405 : TIFFDirEntry *na;
5406 : uint16_t nb;
5407 0 : for (na = ma + 1, nb = (uint16_t)(mb + 1); nb < dircount;
5408 0 : na++, nb++)
5409 : {
5410 0 : if (ma->tdir_tag == na->tdir_tag)
5411 : {
5412 0 : na->tdir_ignore = TRUE;
5413 : }
5414 : }
5415 : }
5416 : }
5417 :
5418 : /* Free any old stuff and reinit. */
5419 0 : 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 0 : TIFFDefaultDirectory(tif);
5427 0 : _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 0 : tif->tif_dir.td_dirdatasize_offsets =
5432 0 : (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
5433 : tif,
5434 0 : (tmsize_t)((size_t)dircount * sizeof(TIFFEntryOffsetAndLength)));
5435 0 : if (tif->tif_dir.td_dirdatasize_offsets == NULL)
5436 : {
5437 0 : TIFFErrorExtR(
5438 : tif, module,
5439 : "Failed to allocate memory for counting IFD data size at reading");
5440 0 : if (dir)
5441 0 : _TIFFfreeExt(tif, dir);
5442 0 : return 0;
5443 : }
5444 :
5445 0 : for (di = 0, dp = dir; di < dircount; di++, dp++)
5446 : {
5447 0 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5448 0 : if (fii == FAILED_FII)
5449 : {
5450 0 : if (tif->tif_warn_about_unknown_tags)
5451 : {
5452 0 : TIFFWarningExtR(tif, module,
5453 : "Unknown field with tag %" PRIu16 " (0x%" PRIx16
5454 : ") encountered",
5455 0 : dp->tdir_tag, dp->tdir_tag);
5456 : }
5457 0 : const TIFFField *fld = _TIFFCreateAnonField(
5458 0 : tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
5459 0 : if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
5460 : {
5461 0 : if (tif->tif_warn_about_unknown_tags)
5462 : {
5463 0 : TIFFWarningExtR(
5464 : tif, module,
5465 : "Registering anonymous field with tag %" PRIu16
5466 : " (0x%" PRIx16 ") failed",
5467 0 : dp->tdir_tag, dp->tdir_tag);
5468 : }
5469 0 : dp->tdir_ignore = TRUE;
5470 : }
5471 : else
5472 : {
5473 0 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5474 0 : assert(fii != FAILED_FII);
5475 : }
5476 : }
5477 0 : if (!dp->tdir_ignore)
5478 : {
5479 0 : fip = tif->tif_fields[fii];
5480 0 : if (fip->field_bit == FIELD_IGNORE)
5481 0 : dp->tdir_ignore = TRUE;
5482 : else
5483 : {
5484 : /* check data type */
5485 0 : while ((fip->field_type != TIFF_ANY) &&
5486 0 : (fip->field_type != dp->tdir_type))
5487 : {
5488 0 : fii++;
5489 0 : if ((fii == tif->tif_nfields) ||
5490 0 : (tif->tif_fields[fii]->field_tag !=
5491 0 : (uint32_t)dp->tdir_tag))
5492 : {
5493 0 : fii = 0xFFFF;
5494 0 : break;
5495 : }
5496 0 : fip = tif->tif_fields[fii];
5497 : }
5498 0 : if (fii == 0xFFFF)
5499 : {
5500 0 : TIFFWarningExtR(tif, module,
5501 : "Wrong data type %" PRIu16
5502 : " for \"%s\"; tag ignored",
5503 0 : dp->tdir_type, fip->field_name);
5504 0 : dp->tdir_ignore = TRUE;
5505 : }
5506 : else
5507 : {
5508 : /* check count if known in advance */
5509 0 : if ((fip->field_readcount != TIFF_VARIABLE) &&
5510 0 : (fip->field_readcount != TIFF_VARIABLE2))
5511 : {
5512 : uint32_t expected;
5513 0 : if (fip->field_readcount == TIFF_SPP)
5514 0 : expected =
5515 0 : (uint32_t)tif->tif_dir.td_samplesperpixel;
5516 : else
5517 0 : expected = (uint32_t)fip->field_readcount;
5518 0 : if (!CheckDirCount(tif, dp, expected))
5519 0 : dp->tdir_ignore = TRUE;
5520 : }
5521 : }
5522 : }
5523 0 : if (!dp->tdir_ignore)
5524 : {
5525 0 : switch (dp->tdir_tag)
5526 : {
5527 0 : case EXIFTAG_SUBJECTDISTANCE:
5528 0 : if (!TIFFFieldIsAnonymous(fip))
5529 : {
5530 : /* should only be called on a Exif directory */
5531 : /* when exifFields[] is active */
5532 0 : (void)TIFFFetchSubjectDistance(tif, dp);
5533 : }
5534 : else
5535 : {
5536 0 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
5537 : }
5538 0 : break;
5539 0 : default:
5540 0 : (void)TIFFFetchNormalTag(tif, dp, TRUE);
5541 0 : break;
5542 : }
5543 0 : } /*-- if (!dp->tdir_ignore) */
5544 : }
5545 : }
5546 : /* Evaluate final IFD data size. */
5547 0 : CalcFinalIFDdatasizeReading(tif, dircount);
5548 :
5549 : /* To be able to return from SubIFD or custom-IFD to main-IFD */
5550 0 : tif->tif_setdirectory_force_absolute = TRUE;
5551 0 : if (dir)
5552 0 : _TIFFfreeExt(tif, dir);
5553 0 : 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 0 : int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
5561 : {
5562 0 : return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
5563 : }
5564 :
5565 : /*
5566 : *--: EXIF-GPS custom directory reading as another special case of custom IFD.
5567 : */
5568 0 : int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
5569 : {
5570 0 : return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
5571 : }
5572 :
5573 10 : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
5574 : uint16_t dircount)
5575 : {
5576 : static const char module[] = "EstimateStripByteCounts";
5577 :
5578 : TIFFDirEntry *dp;
5579 10 : TIFFDirectory *td = &tif->tif_dir;
5580 : uint32_t strip;
5581 :
5582 : /* Do not try to load stripbytecount as we will compute it */
5583 10 : if (!_TIFFFillStrilesInternal(tif, 0))
5584 0 : return -1;
5585 :
5586 10 : const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
5587 10 : uint64_t filesize = 0;
5588 10 : 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 0 : filesize = TIFFGetFileSize(tif);
5593 0 : if (allocsize > filesize)
5594 : {
5595 0 : 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 0 : return -1;
5601 : }
5602 : }
5603 :
5604 10 : if (td->td_stripbytecount_p)
5605 5 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
5606 20 : td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
5607 10 : tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
5608 10 : if (td->td_stripbytecount_p == NULL)
5609 0 : return -1;
5610 :
5611 10 : if (td->td_compression != COMPRESSION_NONE)
5612 : {
5613 : uint64_t space;
5614 : uint16_t n;
5615 2 : if (!(tif->tif_flags & TIFF_BIGTIFF))
5616 2 : space = sizeof(TIFFHeaderClassic) + 2 +
5617 2 : (unsigned long)dircount * 12 + 4;
5618 : else
5619 0 : space =
5620 0 : sizeof(TIFFHeaderBig) + 8 + (unsigned long)dircount * 20 + 8;
5621 : /* calculate amount of space used by indirect values */
5622 17 : for (dp = dir, n = dircount; n > 0; n--, dp++)
5623 : {
5624 : uint32_t typewidth;
5625 : uint64_t datasize;
5626 15 : typewidth = (uint32_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
5627 15 : if (typewidth == 0)
5628 : {
5629 0 : TIFFErrorExtR(
5630 : tif, module,
5631 : "Cannot determine size of unknown tag type %" PRIu16,
5632 0 : dp->tdir_type);
5633 0 : return -1;
5634 : }
5635 15 : if (dp->tdir_count > UINT64_MAX / typewidth)
5636 0 : return -1;
5637 15 : datasize = (uint64_t)typewidth * dp->tdir_count;
5638 15 : if (!(tif->tif_flags & TIFF_BIGTIFF))
5639 : {
5640 15 : if (datasize <= 4)
5641 11 : datasize = 0;
5642 : }
5643 : else
5644 : {
5645 0 : if (datasize <= 8)
5646 0 : datasize = 0;
5647 : }
5648 15 : if (space > UINT64_MAX - datasize)
5649 0 : return -1;
5650 15 : space += datasize;
5651 : }
5652 2 : if (filesize == 0)
5653 2 : filesize = TIFFGetFileSize(tif);
5654 2 : if (filesize < space)
5655 : /* we should perhaps return in error ? */
5656 1 : space = filesize;
5657 : else
5658 1 : space = filesize - space;
5659 2 : if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
5660 0 : space /= td->td_samplesperpixel;
5661 4 : for (strip = 0; strip < td->td_nstrips; strip++)
5662 2 : 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 2 : strip--;
5671 2 : if (td->td_stripoffset_p[strip] >
5672 2 : UINT64_MAX - td->td_stripbytecount_p[strip])
5673 0 : return -1;
5674 2 : if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
5675 : filesize)
5676 : {
5677 1 : if (td->td_stripoffset_p[strip] >= filesize)
5678 : {
5679 : /* Not sure what we should in that case... */
5680 0 : td->td_stripbytecount_p[strip] = 0;
5681 : }
5682 : else
5683 : {
5684 1 : td->td_stripbytecount_p[strip] =
5685 1 : filesize - td->td_stripoffset_p[strip];
5686 : }
5687 : }
5688 : }
5689 8 : else if (isTiled(tif))
5690 : {
5691 0 : uint64_t bytespertile = TIFFTileSize64(tif);
5692 :
5693 0 : for (strip = 0; strip < td->td_nstrips; strip++)
5694 0 : td->td_stripbytecount_p[strip] = bytespertile;
5695 : }
5696 : else
5697 : {
5698 8 : uint64_t rowbytes = TIFFScanlineSize64(tif);
5699 8 : uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
5700 16 : for (strip = 0; strip < td->td_nstrips; strip++)
5701 : {
5702 8 : if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
5703 0 : return -1;
5704 8 : td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
5705 : }
5706 : }
5707 10 : TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
5708 10 : if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
5709 5 : td->td_rowsperstrip = td->td_imagelength;
5710 10 : return 1;
5711 : }
5712 :
5713 0 : static void MissingRequired(TIFF *tif, const char *tagname)
5714 : {
5715 : static const char module[] = "MissingRequired";
5716 :
5717 0 : TIFFErrorExtR(tif, module,
5718 : "TIFF directory is missing required \"%s\" field", tagname);
5719 0 : }
5720 :
5721 295220 : static unsigned long hashFuncOffsetToNumber(const void *elt)
5722 : {
5723 295220 : const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5724 : (const TIFFOffsetAndDirNumber *)elt;
5725 295220 : const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
5726 295220 : ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
5727 295220 : return hash;
5728 : }
5729 :
5730 56689 : static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
5731 : {
5732 56689 : const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5733 : (const TIFFOffsetAndDirNumber *)elt1;
5734 56689 : const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5735 : (const TIFFOffsetAndDirNumber *)elt2;
5736 56689 : return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
5737 : }
5738 :
5739 251268 : static unsigned long hashFuncNumberToOffset(const void *elt)
5740 : {
5741 251268 : const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5742 : (const TIFFOffsetAndDirNumber *)elt;
5743 251268 : return offsetAndDirNumber->dirNumber;
5744 : }
5745 :
5746 14043 : static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
5747 : {
5748 14043 : const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5749 : (const TIFFOffsetAndDirNumber *)elt1;
5750 14043 : const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5751 : (const TIFFOffsetAndDirNumber *)elt2;
5752 14043 : 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 120440 : int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
5766 : {
5767 120440 : if (diroff == 0) /* no more directories */
5768 0 : return 0;
5769 :
5770 120440 : if (tif->tif_map_dir_offset_to_number == NULL)
5771 : {
5772 71273 : tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
5773 : hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
5774 71218 : if (tif->tif_map_dir_offset_to_number == NULL)
5775 : {
5776 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5777 : "Not enough memory");
5778 0 : return 1;
5779 : }
5780 : }
5781 :
5782 120385 : 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 71179 : tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
5787 : hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
5788 71255 : if (tif->tif_map_dir_number_to_offset == NULL)
5789 : {
5790 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5791 : "Not enough memory");
5792 0 : 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 120461 : entry.offset = diroff;
5803 120461 : entry.dirNumber = dirn;
5804 :
5805 : TIFFOffsetAndDirNumber *foundEntry =
5806 120461 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5807 : tif->tif_map_dir_offset_to_number, &entry);
5808 120338 : if (foundEntry)
5809 : {
5810 41263 : if (foundEntry->dirNumber == dirn)
5811 : {
5812 41263 : return 1;
5813 : }
5814 : else
5815 : {
5816 0 : TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
5817 : "TIFF directory %d has IFD looping to directory %u "
5818 : "at offset 0x%" PRIx64 " (%" PRIu64 ")",
5819 0 : (int)dirn - 1, foundEntry->dirNumber, diroff,
5820 : diroff);
5821 0 : return 0;
5822 : }
5823 : }
5824 :
5825 : /* Check if offset of an IFD has been changed and update offset of that IFD
5826 : * number. */
5827 79075 : foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5828 : tif->tif_map_dir_number_to_offset, &entry);
5829 79076 : if (foundEntry)
5830 : {
5831 83 : if (foundEntry->offset != diroff)
5832 : {
5833 : TIFFOffsetAndDirNumber entryOld;
5834 83 : entryOld.offset = foundEntry->offset;
5835 83 : 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 83 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5841 : tif->tif_map_dir_number_to_offset, &entryOld);
5842 83 : if (foundEntryOld)
5843 : {
5844 83 : TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
5845 : foundEntryOld);
5846 : }
5847 83 : foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5848 : tif->tif_map_dir_offset_to_number, &entryOld);
5849 83 : if (foundEntryOld)
5850 : {
5851 83 : TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
5852 : foundEntryOld);
5853 : }
5854 :
5855 83 : TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
5856 : sizeof(TIFFOffsetAndDirNumber));
5857 83 : if (entryPtr == NULL)
5858 : {
5859 0 : return 0;
5860 : }
5861 :
5862 : /* Add IFD offset and dirn to IFD directory list */
5863 83 : *entryPtr = entry;
5864 :
5865 83 : if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5866 : {
5867 0 : TIFFErrorExtR(
5868 : tif, "_TIFFCheckDirNumberAndOffset",
5869 : "Insertion in tif_map_dir_offset_to_number failed");
5870 0 : return 0;
5871 : }
5872 83 : if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5873 : {
5874 0 : TIFFErrorExtR(
5875 : tif, "_TIFFCheckDirNumberAndOffset",
5876 : "Insertion in tif_map_dir_number_to_offset failed");
5877 0 : return 0;
5878 : }
5879 : }
5880 83 : return 1;
5881 : }
5882 :
5883 : /* Arbitrary (hopefully big enough) limit */
5884 78993 : if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
5885 : TIFF_MAX_DIR_COUNT)
5886 : {
5887 39 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5888 : "Cannot handle more than %u TIFF directories",
5889 : (unsigned)TIFF_MAX_DIR_COUNT);
5890 0 : return 0;
5891 : }
5892 :
5893 : TIFFOffsetAndDirNumber *entryPtr =
5894 78984 : (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
5895 78984 : if (entryPtr == NULL)
5896 : {
5897 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5898 : "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
5899 0 : return 0;
5900 : }
5901 :
5902 : /* Add IFD offset and dirn to IFD directory list */
5903 78984 : *entryPtr = entry;
5904 :
5905 78984 : if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5906 : {
5907 0 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5908 : "Insertion in tif_map_dir_offset_to_number failed");
5909 0 : return 0;
5910 : }
5911 78985 : if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5912 : {
5913 55 : TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5914 : "Insertion in tif_map_dir_number_to_offset failed");
5915 0 : return 0;
5916 : }
5917 :
5918 79017 : 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 11774 : int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
5929 : {
5930 11774 : if (diroff == 0) /* no more directories */
5931 0 : 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 11774 : if (tif->tif_map_dir_offset_to_number == NULL)
5938 5 : return 0;
5939 : TIFFOffsetAndDirNumber entry;
5940 11769 : entry.offset = diroff;
5941 11769 : entry.dirNumber = 0; /* not used */
5942 :
5943 : TIFFOffsetAndDirNumber *foundEntry =
5944 11769 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5945 : tif->tif_map_dir_offset_to_number, &entry);
5946 11770 : if (foundEntry)
5947 : {
5948 9889 : *dirn = foundEntry->dirNumber;
5949 9889 : return 1;
5950 : }
5951 :
5952 : /* This updates the directory list for all main-IFDs in the file. */
5953 1881 : TIFFNumberOfDirectories(tif);
5954 :
5955 1881 : foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5956 : tif->tif_map_dir_offset_to_number, &entry);
5957 1881 : if (foundEntry)
5958 : {
5959 1827 : *dirn = foundEntry->dirNumber;
5960 1827 : return 1;
5961 : }
5962 :
5963 54 : 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 10960 : int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
5974 : {
5975 :
5976 10960 : if (tif->tif_map_dir_number_to_offset == NULL)
5977 0 : return 0;
5978 : TIFFOffsetAndDirNumber entry;
5979 10960 : entry.offset = 0; /* not used */
5980 10960 : entry.dirNumber = dirn;
5981 :
5982 : TIFFOffsetAndDirNumber *foundEntry =
5983 10960 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5984 : tif->tif_map_dir_number_to_offset, &entry);
5985 10960 : if (foundEntry)
5986 : {
5987 10960 : *diroff = foundEntry->offset;
5988 10960 : return 1;
5989 : }
5990 :
5991 0 : 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 1428 : int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
6001 : {
6002 1428 : if (tif->tif_map_dir_offset_to_number == NULL)
6003 0 : return 1;
6004 :
6005 : TIFFOffsetAndDirNumber entryOld;
6006 1428 : entryOld.offset = diroff;
6007 1428 : 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 1428 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
6014 : tif->tif_map_dir_offset_to_number, &entryOld);
6015 1428 : if (foundEntryOldOff)
6016 : {
6017 1417 : entryOld.dirNumber = foundEntryOldOff->dirNumber;
6018 1417 : if (tif->tif_map_dir_number_to_offset != NULL)
6019 : {
6020 : TIFFOffsetAndDirNumber *foundEntryOldDir =
6021 1417 : (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
6022 : tif->tif_map_dir_number_to_offset, &entryOld);
6023 1417 : if (foundEntryOldDir)
6024 : {
6025 1417 : TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
6026 : foundEntryOldDir);
6027 1417 : TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
6028 : foundEntryOldOff);
6029 1417 : return 1;
6030 : }
6031 : }
6032 : else
6033 : {
6034 0 : TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
6035 : "Unexpectedly tif_map_dir_number_to_offset is "
6036 : "missing but tif_map_dir_offset_to_number exists.");
6037 0 : return 0;
6038 : }
6039 : }
6040 11 : 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 0 : static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
6048 : {
6049 0 : if ((uint64_t)count > dir->tdir_count)
6050 : {
6051 0 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
6052 0 : TIFFWarningExtR(tif, tif->tif_name,
6053 : "incorrect count for field \"%s\" (%" PRIu64
6054 : ", expecting %" PRIu32 "); tag ignored",
6055 : fip ? fip->field_name : "unknown tagname",
6056 : dir->tdir_count, count);
6057 0 : return (0);
6058 : }
6059 0 : else if ((uint64_t)count < dir->tdir_count)
6060 : {
6061 0 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
6062 0 : 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 0 : dir->tdir_count = count;
6068 0 : return (1);
6069 : }
6070 0 : 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 54912 : 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 54912 : assert(pdir);
6092 :
6093 54912 : tif->tif_diroff = diroff;
6094 54912 : if (nextdiroff)
6095 54823 : *nextdiroff = 0;
6096 54912 : if (!isMapped(tif))
6097 : {
6098 54832 : if (!SeekOK(tif, tif->tif_diroff))
6099 : {
6100 25 : TIFFErrorExtR(tif, module,
6101 : "%s: Seek error accessing TIFF directory",
6102 : tif->tif_name);
6103 0 : return 0;
6104 : }
6105 54870 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6106 : {
6107 54003 : if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
6108 : {
6109 12 : TIFFErrorExtR(tif, module,
6110 : "%s: Can not read TIFF directory count",
6111 : tif->tif_name);
6112 12 : return 0;
6113 : }
6114 54027 : if (tif->tif_flags & TIFF_SWAB)
6115 561 : TIFFSwabShort(&dircount16);
6116 53977 : if (dircount16 > 4096)
6117 : {
6118 0 : TIFFErrorExtR(tif, module,
6119 : "Sanity check on directory count failed, this is "
6120 : "probably not a valid IFD offset");
6121 0 : return 0;
6122 : }
6123 53977 : dirsize = 12;
6124 : }
6125 : else
6126 : {
6127 : uint64_t dircount64;
6128 867 : if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
6129 : {
6130 0 : TIFFErrorExtR(tif, module,
6131 : "%s: Can not read TIFF directory count",
6132 : tif->tif_name);
6133 0 : return 0;
6134 : }
6135 867 : if (tif->tif_flags & TIFF_SWAB)
6136 19 : TIFFSwabLong8(&dircount64);
6137 867 : if (dircount64 > 4096)
6138 : {
6139 0 : TIFFErrorExtR(tif, module,
6140 : "Sanity check on directory count failed, this is "
6141 : "probably not a valid IFD offset");
6142 0 : return 0;
6143 : }
6144 867 : dircount16 = (uint16_t)dircount64;
6145 867 : dirsize = 20;
6146 : }
6147 54844 : origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6148 : "to read TIFF directory");
6149 54897 : if (origdir == NULL)
6150 4 : return 0;
6151 54893 : if (!ReadOK(tif, origdir, (tmsize_t)dircount16 * dirsize))
6152 : {
6153 1 : TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
6154 : tif->tif_name);
6155 1 : _TIFFfreeExt(tif, origdir);
6156 1 : return 0;
6157 : }
6158 : /*
6159 : * Read offset to next directory for sequential scans if
6160 : * needed.
6161 : */
6162 54878 : if (nextdiroff)
6163 : {
6164 54873 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6165 : {
6166 : uint32_t nextdiroff32;
6167 54004 : if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
6168 4 : nextdiroff32 = 0;
6169 54013 : if (tif->tif_flags & TIFF_SWAB)
6170 561 : TIFFSwabLong(&nextdiroff32);
6171 53996 : *nextdiroff = nextdiroff32;
6172 : }
6173 : else
6174 : {
6175 869 : if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
6176 0 : *nextdiroff = 0;
6177 867 : if (tif->tif_flags & TIFF_SWAB)
6178 19 : TIFFSwabLong8(nextdiroff);
6179 : }
6180 : }
6181 : }
6182 : else
6183 : {
6184 : tmsize_t m;
6185 : tmsize_t off;
6186 80 : if (tif->tif_diroff > (uint64_t)INT64_MAX)
6187 : {
6188 0 : TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6189 0 : return (0);
6190 : }
6191 80 : 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 80 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6203 : {
6204 8 : m = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
6205 8 : if ((m < off) || ((uint64_t)m < sizeof(uint16_t)) ||
6206 8 : ((uint64_t)m > (uint64_t)tif->tif_size))
6207 : {
6208 0 : TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6209 0 : return 0;
6210 : }
6211 : else
6212 : {
6213 8 : _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
6214 : }
6215 8 : off = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
6216 8 : if (tif->tif_flags & TIFF_SWAB)
6217 0 : TIFFSwabShort(&dircount16);
6218 8 : if (dircount16 > 4096)
6219 : {
6220 0 : TIFFErrorExtR(tif, module,
6221 : "Sanity check on directory count failed, this is "
6222 : "probably not a valid IFD offset");
6223 0 : return 0;
6224 : }
6225 8 : dirsize = 12;
6226 : }
6227 : else
6228 : {
6229 : uint64_t dircount64;
6230 72 : m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
6231 72 : if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
6232 0 : ((uint64_t)m > (uint64_t)tif->tif_size))
6233 : {
6234 72 : TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6235 0 : return 0;
6236 : }
6237 : else
6238 : {
6239 0 : _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
6240 : }
6241 0 : off = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
6242 0 : if (tif->tif_flags & TIFF_SWAB)
6243 0 : TIFFSwabLong8(&dircount64);
6244 0 : if (dircount64 > 4096)
6245 : {
6246 0 : TIFFErrorExtR(tif, module,
6247 : "Sanity check on directory count failed, this is "
6248 : "probably not a valid IFD offset");
6249 0 : return 0;
6250 : }
6251 0 : dircount16 = (uint16_t)dircount64;
6252 0 : dirsize = 20;
6253 : }
6254 8 : if (dircount16 == 0)
6255 : {
6256 0 : TIFFErrorExtR(tif, module,
6257 : "Sanity check on directory count failed, zero tag "
6258 : "directories not supported");
6259 0 : 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 8 : uint64_t filesize = TIFFGetFileSize(tif);
6264 8 : uint64_t allocsize = (uint64_t)dircount16 * dirsize;
6265 8 : if (allocsize > filesize)
6266 : {
6267 0 : 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 0 : return 0;
6274 : }
6275 8 : origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6276 : "to read TIFF directory");
6277 8 : if (origdir == NULL)
6278 0 : return 0;
6279 8 : m = off + dircount16 * dirsize;
6280 8 : if ((m < off) || (m < (tmsize_t)dircount16 * (tmsize_t)dirsize) ||
6281 8 : (m > tif->tif_size))
6282 : {
6283 0 : TIFFErrorExtR(tif, module, "Can not read TIFF directory");
6284 0 : _TIFFfreeExt(tif, origdir);
6285 0 : return 0;
6286 : }
6287 : else
6288 : {
6289 8 : _TIFFmemcpy(origdir, tif->tif_base + off,
6290 8 : (tmsize_t)dircount16 * dirsize);
6291 : }
6292 8 : if (nextdiroff)
6293 : {
6294 8 : off += dircount16 * dirsize;
6295 8 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6296 : {
6297 : uint32_t nextdiroff32;
6298 8 : m = (tmsize_t)((uint64_t)off + sizeof(uint32_t));
6299 8 : if ((m < off) || ((uint64_t)m < sizeof(uint32_t)) ||
6300 8 : ((uint64_t)m > (uint64_t)tif->tif_size))
6301 0 : nextdiroff32 = 0;
6302 : else
6303 8 : _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
6304 : sizeof(uint32_t));
6305 8 : if (tif->tif_flags & TIFF_SWAB)
6306 0 : TIFFSwabLong(&nextdiroff32);
6307 8 : *nextdiroff = nextdiroff32;
6308 : }
6309 : else
6310 : {
6311 0 : m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
6312 0 : if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
6313 0 : ((uint64_t)m > (uint64_t)tif->tif_size))
6314 0 : *nextdiroff = 0;
6315 : else
6316 0 : _TIFFmemcpy(nextdiroff, tif->tif_base + off,
6317 : sizeof(uint64_t));
6318 0 : if (tif->tif_flags & TIFF_SWAB)
6319 0 : TIFFSwabLong8(nextdiroff);
6320 : }
6321 : }
6322 : }
6323 : /* No check against filesize needed here because "dir" should have same size
6324 : * than "origdir" checked above. */
6325 54876 : dir = (TIFFDirEntry *)_TIFFCheckMalloc(
6326 : tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
6327 54902 : if (dir == 0)
6328 : {
6329 17 : _TIFFfreeExt(tif, origdir);
6330 0 : return 0;
6331 : }
6332 54885 : ma = (uint8_t *)origdir;
6333 54885 : mb = dir;
6334 830471 : for (n = 0; n < dircount16; n++)
6335 : {
6336 775553 : mb->tdir_ignore = FALSE;
6337 775553 : if (tif->tif_flags & TIFF_SWAB)
6338 7746 : TIFFSwabShort((uint16_t *)ma);
6339 775553 : mb->tdir_tag = *(uint16_t *)ma;
6340 775553 : ma += sizeof(uint16_t);
6341 775553 : if (tif->tif_flags & TIFF_SWAB)
6342 7746 : TIFFSwabShort((uint16_t *)ma);
6343 775627 : mb->tdir_type = *(uint16_t *)ma;
6344 775627 : ma += sizeof(uint16_t);
6345 775627 : if (!(tif->tif_flags & TIFF_BIGTIFF))
6346 : {
6347 763045 : if (tif->tif_flags & TIFF_SWAB)
6348 7537 : TIFFSwabLong((uint32_t *)ma);
6349 763043 : mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
6350 763043 : ma += sizeof(uint32_t);
6351 763043 : mb->tdir_offset.toff_long8 = 0;
6352 763043 : *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
6353 763043 : ma += sizeof(uint32_t);
6354 : }
6355 : else
6356 : {
6357 12582 : if (tif->tif_flags & TIFF_SWAB)
6358 209 : TIFFSwabLong8((uint64_t *)ma);
6359 12582 : mb->tdir_count = TIFFReadUInt64(ma);
6360 12582 : ma += sizeof(uint64_t);
6361 12582 : mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
6362 12543 : ma += sizeof(uint64_t);
6363 : }
6364 775586 : mb++;
6365 : }
6366 54918 : _TIFFfreeExt(tif, origdir);
6367 54817 : *pdir = dir;
6368 54817 : return dircount16;
6369 : }
6370 :
6371 : /*
6372 : * Fetch a tag that is not handled by special case code.
6373 : */
6374 501661 : 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 501661 : const TIFFField *fip = NULL;
6380 501661 : TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
6381 501742 : if (fii == FAILED_FII)
6382 : {
6383 282 : TIFFErrorExtR(tif, "TIFFFetchNormalTag",
6384 282 : "No definition found for tag %" PRIu16, dp->tdir_tag);
6385 0 : return 0;
6386 : }
6387 501460 : fip = tif->tif_fields[fii];
6388 501460 : assert(fip != NULL); /* should not happen */
6389 501460 : 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 501460 : 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 501460 : err = TIFFReadDirEntryErrOk;
6396 501460 : switch (fip->set_get_field_type)
6397 : {
6398 386 : case TIFF_SETGET_UNDEFINED:
6399 386 : 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 386 : break;
6405 38901 : case TIFF_SETGET_ASCII:
6406 : {
6407 : uint8_t *data;
6408 38901 : assert(fip->field_passcount == 0);
6409 38901 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
6410 38813 : if (err == TIFFReadDirEntryErrOk)
6411 : {
6412 38834 : size_t mb = 0;
6413 : int n;
6414 38834 : if (data != NULL)
6415 : {
6416 38802 : 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 38852 : 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 0 : uint8_t *ma = data;
6428 0 : while (mb < (uint32_t)dp->tdir_count)
6429 : {
6430 27 : if (*ma == 0)
6431 0 : break;
6432 27 : ma++;
6433 27 : mb++;
6434 : }
6435 : }
6436 : }
6437 38834 : if (!EvaluateIFDdatasizeReading(tif, dp))
6438 : {
6439 45 : if (data != NULL)
6440 0 : _TIFFfreeExt(tif, data);
6441 0 : return (0);
6442 : }
6443 38852 : if (mb + 1 < (uint32_t)dp->tdir_count)
6444 0 : 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 38852 : else if (mb + 1 > (uint32_t)dp->tdir_count)
6451 : {
6452 1 : 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 1 : assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
6459 1 : uint8_t *o = (uint8_t *)_TIFFmallocExt(
6460 1 : tif, (uint32_t)dp->tdir_count + 1);
6461 1 : if (o == NULL)
6462 : {
6463 0 : if (data != NULL)
6464 0 : _TIFFfreeExt(tif, data);
6465 0 : return (0);
6466 : }
6467 1 : if (dp->tdir_count > 0)
6468 : {
6469 1 : _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6470 : }
6471 1 : o[(uint32_t)dp->tdir_count] = 0;
6472 1 : if (data != 0)
6473 1 : _TIFFfreeExt(tif, data);
6474 1 : data = o;
6475 : }
6476 38852 : n = TIFFSetField(tif, dp->tdir_tag, data);
6477 38918 : if (data != 0)
6478 38901 : _TIFFfreeExt(tif, data);
6479 38895 : if (!n)
6480 0 : return (0);
6481 : }
6482 : }
6483 38874 : break;
6484 0 : case TIFF_SETGET_UINT8:
6485 : {
6486 0 : uint8_t data = 0;
6487 0 : assert(fip->field_readcount == 1);
6488 0 : assert(fip->field_passcount == 0);
6489 0 : err = TIFFReadDirEntryByte(tif, dp, &data);
6490 0 : if (err == TIFFReadDirEntryErrOk)
6491 : {
6492 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6493 0 : return (0);
6494 : }
6495 : }
6496 0 : break;
6497 0 : case TIFF_SETGET_SINT8:
6498 : {
6499 0 : int8_t data = 0;
6500 0 : assert(fip->field_readcount == 1);
6501 0 : assert(fip->field_passcount == 0);
6502 0 : err = TIFFReadDirEntrySbyte(tif, dp, &data);
6503 0 : if (err == TIFFReadDirEntryErrOk)
6504 : {
6505 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6506 0 : return (0);
6507 : }
6508 : }
6509 0 : break;
6510 170383 : case TIFF_SETGET_UINT16:
6511 : {
6512 : uint16_t data;
6513 170383 : assert(fip->field_readcount == 1);
6514 170383 : assert(fip->field_passcount == 0);
6515 170383 : err = TIFFReadDirEntryShort(tif, dp, &data);
6516 170360 : if (err == TIFFReadDirEntryErrOk)
6517 : {
6518 170380 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6519 0 : return (0);
6520 : }
6521 : }
6522 170455 : break;
6523 0 : case TIFF_SETGET_SINT16:
6524 : {
6525 : int16_t data;
6526 0 : assert(fip->field_readcount == 1);
6527 0 : assert(fip->field_passcount == 0);
6528 0 : err = TIFFReadDirEntrySshort(tif, dp, &data);
6529 0 : if (err == TIFFReadDirEntryErrOk)
6530 : {
6531 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6532 0 : return (0);
6533 : }
6534 : }
6535 0 : break;
6536 180107 : case TIFF_SETGET_UINT32:
6537 : {
6538 : uint32_t data;
6539 180107 : assert(fip->field_readcount == 1);
6540 180107 : assert(fip->field_passcount == 0);
6541 180107 : err = TIFFReadDirEntryLong(tif, dp, &data);
6542 179985 : if (err == TIFFReadDirEntryErrOk)
6543 : {
6544 179976 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6545 0 : return (0);
6546 : }
6547 : }
6548 180041 : break;
6549 0 : case TIFF_SETGET_SINT32:
6550 : {
6551 : int32_t data;
6552 0 : assert(fip->field_readcount == 1);
6553 0 : assert(fip->field_passcount == 0);
6554 0 : err = TIFFReadDirEntrySlong(tif, dp, &data);
6555 0 : if (err == TIFFReadDirEntryErrOk)
6556 : {
6557 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6558 0 : return (0);
6559 : }
6560 : }
6561 0 : break;
6562 8 : case TIFF_SETGET_UINT64:
6563 : {
6564 : uint64_t data;
6565 8 : assert(fip->field_readcount == 1);
6566 8 : assert(fip->field_passcount == 0);
6567 8 : err = TIFFReadDirEntryLong8(tif, dp, &data);
6568 8 : if (err == TIFFReadDirEntryErrOk)
6569 : {
6570 8 : if (!EvaluateIFDdatasizeReading(tif, dp))
6571 0 : return 0;
6572 8 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6573 0 : return (0);
6574 : }
6575 : }
6576 8 : break;
6577 0 : case TIFF_SETGET_SINT64:
6578 : {
6579 : int64_t data;
6580 0 : assert(fip->field_readcount == 1);
6581 0 : assert(fip->field_passcount == 0);
6582 0 : err = TIFFReadDirEntrySlong8(tif, dp, &data);
6583 0 : if (err == TIFFReadDirEntryErrOk)
6584 : {
6585 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6586 0 : return 0;
6587 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6588 0 : return (0);
6589 : }
6590 : }
6591 0 : break;
6592 248 : case TIFF_SETGET_FLOAT:
6593 : {
6594 : float data;
6595 248 : assert(fip->field_readcount == 1);
6596 248 : assert(fip->field_passcount == 0);
6597 248 : err = TIFFReadDirEntryFloat(tif, dp, &data);
6598 248 : if (err == TIFFReadDirEntryErrOk)
6599 : {
6600 248 : if (!EvaluateIFDdatasizeReading(tif, dp))
6601 0 : return 0;
6602 248 : if (!TIFFSetField(tif, dp->tdir_tag, (double)data))
6603 0 : return (0);
6604 : }
6605 : }
6606 248 : break;
6607 0 : case TIFF_SETGET_DOUBLE:
6608 : {
6609 : double data;
6610 0 : assert(fip->field_readcount == 1);
6611 0 : assert(fip->field_passcount == 0);
6612 0 : err = TIFFReadDirEntryDouble(tif, dp, &data);
6613 0 : if (err == TIFFReadDirEntryErrOk)
6614 : {
6615 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6616 0 : return 0;
6617 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6618 0 : return (0);
6619 : }
6620 : }
6621 0 : break;
6622 0 : case TIFF_SETGET_IFD8:
6623 : {
6624 : uint64_t data;
6625 0 : assert(fip->field_readcount == 1);
6626 0 : assert(fip->field_passcount == 0);
6627 0 : err = TIFFReadDirEntryIfd8(tif, dp, &data);
6628 0 : if (err == TIFFReadDirEntryErrOk)
6629 : {
6630 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6631 0 : return 0;
6632 0 : if (!TIFFSetField(tif, dp->tdir_tag, data))
6633 0 : return (0);
6634 : }
6635 : }
6636 0 : break;
6637 1338 : case TIFF_SETGET_UINT16_PAIR:
6638 : {
6639 : uint16_t *data;
6640 1338 : assert(fip->field_readcount == 2);
6641 1338 : assert(fip->field_passcount == 0);
6642 1338 : if (dp->tdir_count != 2)
6643 : {
6644 0 : TIFFWarningExtR(tif, module,
6645 : "incorrect count for field \"%s\", expected 2, "
6646 : "got %" PRIu64,
6647 : fip->field_name, dp->tdir_count);
6648 0 : return (0);
6649 : }
6650 1338 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
6651 1338 : if (err == TIFFReadDirEntryErrOk)
6652 : {
6653 : int m;
6654 1338 : assert(data); /* avoid CLang static Analyzer false positive */
6655 1338 : m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
6656 1338 : _TIFFfreeExt(tif, data);
6657 1338 : if (!m)
6658 0 : return (0);
6659 : }
6660 : }
6661 1338 : break;
6662 0 : case TIFF_SETGET_C0_UINT8:
6663 : {
6664 : uint8_t *data;
6665 0 : assert(fip->field_readcount >= 1);
6666 0 : assert(fip->field_passcount == 0);
6667 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6668 : {
6669 0 : TIFFWarningExtR(tif, module,
6670 : "incorrect count for field \"%s\", expected "
6671 : "%d, got %" PRIu64,
6672 0 : fip->field_name, (int)fip->field_readcount,
6673 : dp->tdir_count);
6674 0 : return (0);
6675 : }
6676 : else
6677 : {
6678 0 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
6679 0 : if (err == TIFFReadDirEntryErrOk)
6680 : {
6681 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6682 : {
6683 0 : if (data != 0)
6684 0 : _TIFFfreeExt(tif, data);
6685 0 : return 0;
6686 : }
6687 : int m;
6688 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6689 0 : if (data != 0)
6690 0 : _TIFFfreeExt(tif, data);
6691 0 : if (!m)
6692 0 : return (0);
6693 : }
6694 : }
6695 : }
6696 0 : break;
6697 0 : case TIFF_SETGET_C0_SINT8:
6698 : {
6699 : int8_t *data;
6700 0 : assert(fip->field_readcount >= 1);
6701 0 : assert(fip->field_passcount == 0);
6702 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6703 : {
6704 0 : TIFFWarningExtR(tif, module,
6705 : "incorrect count for field \"%s\", expected "
6706 : "%d, got %" PRIu64,
6707 0 : fip->field_name, (int)fip->field_readcount,
6708 : dp->tdir_count);
6709 0 : return (0);
6710 : }
6711 : else
6712 : {
6713 0 : err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
6714 0 : if (err == TIFFReadDirEntryErrOk)
6715 : {
6716 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6717 : {
6718 0 : if (data != 0)
6719 0 : _TIFFfreeExt(tif, data);
6720 0 : return 0;
6721 : }
6722 : int m;
6723 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6724 0 : if (data != 0)
6725 0 : _TIFFfreeExt(tif, data);
6726 0 : if (!m)
6727 0 : return (0);
6728 : }
6729 : }
6730 : }
6731 0 : break;
6732 0 : case TIFF_SETGET_C0_UINT16:
6733 : {
6734 : uint16_t *data;
6735 0 : assert(fip->field_readcount >= 1);
6736 0 : assert(fip->field_passcount == 0);
6737 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6738 : {
6739 0 : TIFFWarningExtR(tif, module,
6740 : "incorrect count for field \"%s\", expected "
6741 : "%d, got %" PRIu64,
6742 0 : fip->field_name, (int)fip->field_readcount,
6743 : dp->tdir_count);
6744 0 : return (0);
6745 : }
6746 : else
6747 : {
6748 0 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
6749 0 : if (err == TIFFReadDirEntryErrOk)
6750 : {
6751 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6752 : {
6753 0 : if (data != 0)
6754 0 : _TIFFfreeExt(tif, data);
6755 0 : return 0;
6756 : }
6757 : int m;
6758 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6759 0 : if (data != 0)
6760 0 : _TIFFfreeExt(tif, data);
6761 0 : if (!m)
6762 0 : return (0);
6763 : }
6764 : }
6765 : }
6766 0 : break;
6767 0 : case TIFF_SETGET_C0_SINT16:
6768 : {
6769 : int16_t *data;
6770 0 : assert(fip->field_readcount >= 1);
6771 0 : assert(fip->field_passcount == 0);
6772 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6773 : {
6774 0 : TIFFWarningExtR(tif, module,
6775 : "incorrect count for field \"%s\", expected "
6776 : "%d, got %" PRIu64,
6777 0 : fip->field_name, (int)fip->field_readcount,
6778 : dp->tdir_count);
6779 0 : return (0);
6780 : }
6781 : else
6782 : {
6783 0 : err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6784 0 : if (err == TIFFReadDirEntryErrOk)
6785 : {
6786 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6787 : {
6788 0 : if (data != 0)
6789 0 : _TIFFfreeExt(tif, data);
6790 0 : return 0;
6791 : }
6792 : int m;
6793 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6794 0 : if (data != 0)
6795 0 : _TIFFfreeExt(tif, data);
6796 0 : if (!m)
6797 0 : return (0);
6798 : }
6799 : }
6800 : }
6801 0 : break;
6802 0 : case TIFF_SETGET_C0_UINT32:
6803 : {
6804 : uint32_t *data;
6805 0 : assert(fip->field_readcount >= 1);
6806 0 : assert(fip->field_passcount == 0);
6807 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6808 : {
6809 0 : TIFFWarningExtR(tif, module,
6810 : "incorrect count for field \"%s\", expected "
6811 : "%d, got %" PRIu64,
6812 0 : fip->field_name, (int)fip->field_readcount,
6813 : dp->tdir_count);
6814 0 : return (0);
6815 : }
6816 : else
6817 : {
6818 0 : err = TIFFReadDirEntryLongArray(tif, dp, &data);
6819 0 : if (err == TIFFReadDirEntryErrOk)
6820 : {
6821 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6822 : {
6823 0 : if (data != 0)
6824 0 : _TIFFfreeExt(tif, data);
6825 0 : return 0;
6826 : }
6827 : int m;
6828 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6829 0 : if (data != 0)
6830 0 : _TIFFfreeExt(tif, data);
6831 0 : if (!m)
6832 0 : return (0);
6833 : }
6834 : }
6835 : }
6836 0 : break;
6837 0 : case TIFF_SETGET_C0_SINT32:
6838 : {
6839 : int32_t *data;
6840 0 : assert(fip->field_readcount >= 1);
6841 0 : assert(fip->field_passcount == 0);
6842 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6843 : {
6844 0 : TIFFWarningExtR(tif, module,
6845 : "incorrect count for field \"%s\", expected "
6846 : "%d, got %" PRIu64,
6847 0 : fip->field_name, (int)fip->field_readcount,
6848 : dp->tdir_count);
6849 0 : return (0);
6850 : }
6851 : else
6852 : {
6853 0 : err = TIFFReadDirEntrySlongArray(tif, dp, &data);
6854 0 : if (err == TIFFReadDirEntryErrOk)
6855 : {
6856 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6857 : {
6858 0 : if (data != 0)
6859 0 : _TIFFfreeExt(tif, data);
6860 0 : return 0;
6861 : }
6862 : int m;
6863 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6864 0 : if (data != 0)
6865 0 : _TIFFfreeExt(tif, data);
6866 0 : if (!m)
6867 0 : return (0);
6868 : }
6869 : }
6870 : }
6871 0 : break;
6872 0 : case TIFF_SETGET_C0_UINT64:
6873 : {
6874 : uint64_t *data;
6875 0 : assert(fip->field_readcount >= 1);
6876 0 : assert(fip->field_passcount == 0);
6877 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6878 : {
6879 0 : TIFFWarningExtR(tif, module,
6880 : "incorrect count for field \"%s\", expected "
6881 : "%d, got %" PRIu64,
6882 0 : fip->field_name, (int)fip->field_readcount,
6883 : dp->tdir_count);
6884 0 : return (0);
6885 : }
6886 : else
6887 : {
6888 0 : err = TIFFReadDirEntryLong8Array(tif, dp, &data);
6889 0 : if (err == TIFFReadDirEntryErrOk)
6890 : {
6891 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6892 : {
6893 0 : if (data != 0)
6894 0 : _TIFFfreeExt(tif, data);
6895 0 : return 0;
6896 : }
6897 : int m;
6898 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6899 0 : if (data != 0)
6900 0 : _TIFFfreeExt(tif, data);
6901 0 : if (!m)
6902 0 : return (0);
6903 : }
6904 : }
6905 : }
6906 0 : break;
6907 0 : case TIFF_SETGET_C0_SINT64:
6908 : {
6909 : int64_t *data;
6910 0 : assert(fip->field_readcount >= 1);
6911 0 : assert(fip->field_passcount == 0);
6912 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6913 : {
6914 0 : TIFFWarningExtR(tif, module,
6915 : "incorrect count for field \"%s\", expected "
6916 : "%d, got %" PRIu64,
6917 0 : fip->field_name, (int)fip->field_readcount,
6918 : dp->tdir_count);
6919 0 : return (0);
6920 : }
6921 : else
6922 : {
6923 0 : err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
6924 0 : if (err == TIFFReadDirEntryErrOk)
6925 : {
6926 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6927 : {
6928 0 : if (data != 0)
6929 0 : _TIFFfreeExt(tif, data);
6930 0 : return 0;
6931 : }
6932 : int m;
6933 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
6934 0 : if (data != 0)
6935 0 : _TIFFfreeExt(tif, data);
6936 0 : if (!m)
6937 0 : return (0);
6938 : }
6939 : }
6940 : }
6941 0 : break;
6942 1026 : case TIFF_SETGET_C0_FLOAT:
6943 : {
6944 : float *data;
6945 1026 : assert(fip->field_readcount >= 1);
6946 1026 : assert(fip->field_passcount == 0);
6947 1026 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6948 : {
6949 0 : TIFFWarningExtR(tif, module,
6950 : "incorrect count for field \"%s\", expected "
6951 : "%d, got %" PRIu64,
6952 0 : fip->field_name, (int)fip->field_readcount,
6953 : dp->tdir_count);
6954 0 : return (0);
6955 : }
6956 : else
6957 : {
6958 1026 : err = TIFFReadDirEntryFloatArray(tif, dp, &data);
6959 1026 : if (err == TIFFReadDirEntryErrOk)
6960 : {
6961 1026 : if (!EvaluateIFDdatasizeReading(tif, dp))
6962 : {
6963 0 : if (data != 0)
6964 0 : _TIFFfreeExt(tif, data);
6965 0 : return 0;
6966 : }
6967 : int m;
6968 1026 : m = TIFFSetField(tif, dp->tdir_tag, data);
6969 1026 : if (data != 0)
6970 1026 : _TIFFfreeExt(tif, data);
6971 1026 : if (!m)
6972 0 : return (0);
6973 : }
6974 : }
6975 : }
6976 1026 : break;
6977 : /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6978 : * into Double-Arrays. */
6979 0 : case TIFF_SETGET_C0_DOUBLE:
6980 : {
6981 : double *data;
6982 0 : assert(fip->field_readcount >= 1);
6983 0 : assert(fip->field_passcount == 0);
6984 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
6985 : {
6986 0 : TIFFWarningExtR(tif, module,
6987 : "incorrect count for field \"%s\", expected "
6988 : "%d, got %" PRIu64,
6989 0 : fip->field_name, (int)fip->field_readcount,
6990 : dp->tdir_count);
6991 0 : return (0);
6992 : }
6993 : else
6994 : {
6995 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6996 0 : if (err == TIFFReadDirEntryErrOk)
6997 : {
6998 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
6999 : {
7000 0 : if (data != 0)
7001 0 : _TIFFfreeExt(tif, data);
7002 0 : return 0;
7003 : }
7004 : int m;
7005 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
7006 0 : if (data != 0)
7007 0 : _TIFFfreeExt(tif, data);
7008 0 : if (!m)
7009 0 : return (0);
7010 : }
7011 : }
7012 : }
7013 0 : break;
7014 0 : case TIFF_SETGET_C0_IFD8:
7015 : {
7016 : uint64_t *data;
7017 0 : assert(fip->field_readcount >= 1);
7018 0 : assert(fip->field_passcount == 0);
7019 0 : if (dp->tdir_count != (uint64_t)fip->field_readcount)
7020 : {
7021 0 : TIFFWarningExtR(tif, module,
7022 : "incorrect count for field \"%s\", expected "
7023 : "%d, got %" PRIu64,
7024 0 : fip->field_name, (int)fip->field_readcount,
7025 : dp->tdir_count);
7026 0 : return (0);
7027 : }
7028 : else
7029 : {
7030 0 : err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7031 0 : if (err == TIFFReadDirEntryErrOk)
7032 : {
7033 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7034 : {
7035 0 : if (data != 0)
7036 0 : _TIFFfreeExt(tif, data);
7037 0 : return 0;
7038 : }
7039 : int m;
7040 0 : m = TIFFSetField(tif, dp->tdir_tag, data);
7041 0 : if (data != 0)
7042 0 : _TIFFfreeExt(tif, data);
7043 0 : if (!m)
7044 0 : return (0);
7045 : }
7046 : }
7047 : }
7048 0 : break;
7049 0 : case TIFF_SETGET_C16_ASCII:
7050 : {
7051 : uint8_t *data;
7052 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7053 0 : assert(fip->field_passcount == 1);
7054 0 : if (dp->tdir_count > 0xFFFF)
7055 0 : err = TIFFReadDirEntryErrCount;
7056 : else
7057 : {
7058 0 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7059 0 : if (err == TIFFReadDirEntryErrOk)
7060 : {
7061 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7062 : {
7063 0 : if (data != 0)
7064 0 : _TIFFfreeExt(tif, data);
7065 0 : return 0;
7066 : }
7067 : int m;
7068 0 : if (data != 0 && dp->tdir_count > 0 &&
7069 0 : data[dp->tdir_count - 1] != '\0')
7070 : {
7071 0 : 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 0 : uint8_t *o = (uint8_t *)_TIFFmallocExt(
7078 0 : tif, (uint32_t)dp->tdir_count + 1);
7079 0 : if (o == NULL)
7080 : {
7081 0 : if (data != NULL)
7082 0 : _TIFFfreeExt(tif, data);
7083 0 : return (0);
7084 : }
7085 0 : if (dp->tdir_count > 0)
7086 : {
7087 0 : _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
7088 : }
7089 0 : o[(uint32_t)dp->tdir_count] = 0;
7090 0 : dp->tdir_count++; /* Increment for added null. */
7091 0 : if (data != 0)
7092 0 : _TIFFfreeExt(tif, data);
7093 0 : data = o;
7094 : }
7095 0 : m = TIFFSetField(tif, dp->tdir_tag,
7096 0 : (uint16_t)(dp->tdir_count), data);
7097 0 : if (data != 0)
7098 0 : _TIFFfreeExt(tif, data);
7099 0 : if (!m)
7100 0 : return (0);
7101 : }
7102 : }
7103 : }
7104 0 : break;
7105 0 : case TIFF_SETGET_C16_UINT8:
7106 : {
7107 : uint8_t *data;
7108 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7109 0 : assert(fip->field_passcount == 1);
7110 0 : if (dp->tdir_count > 0xFFFF)
7111 0 : err = TIFFReadDirEntryErrCount;
7112 : else
7113 : {
7114 0 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7115 0 : if (err == TIFFReadDirEntryErrOk)
7116 : {
7117 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7118 : {
7119 0 : if (data != 0)
7120 0 : _TIFFfreeExt(tif, data);
7121 0 : return 0;
7122 : }
7123 : int m;
7124 0 : m = TIFFSetField(tif, dp->tdir_tag,
7125 0 : (uint16_t)(dp->tdir_count), data);
7126 0 : if (data != 0)
7127 0 : _TIFFfreeExt(tif, data);
7128 0 : if (!m)
7129 0 : return (0);
7130 : }
7131 : }
7132 : }
7133 0 : break;
7134 0 : case TIFF_SETGET_C16_SINT8:
7135 : {
7136 : int8_t *data;
7137 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7138 0 : assert(fip->field_passcount == 1);
7139 0 : if (dp->tdir_count > 0xFFFF)
7140 0 : err = TIFFReadDirEntryErrCount;
7141 : else
7142 : {
7143 0 : err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7144 0 : if (err == TIFFReadDirEntryErrOk)
7145 : {
7146 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7147 : {
7148 0 : if (data != 0)
7149 0 : _TIFFfreeExt(tif, data);
7150 0 : return 0;
7151 : }
7152 : int m;
7153 0 : m = TIFFSetField(tif, dp->tdir_tag,
7154 0 : (uint16_t)(dp->tdir_count), data);
7155 0 : if (data != 0)
7156 0 : _TIFFfreeExt(tif, data);
7157 0 : if (!m)
7158 0 : return (0);
7159 : }
7160 : }
7161 : }
7162 0 : break;
7163 34387 : case TIFF_SETGET_C16_UINT16:
7164 : {
7165 : uint16_t *data;
7166 34387 : assert(fip->field_readcount == TIFF_VARIABLE);
7167 34387 : assert(fip->field_passcount == 1);
7168 34387 : if (dp->tdir_count > 0xFFFF)
7169 0 : err = TIFFReadDirEntryErrCount;
7170 : else
7171 : {
7172 34387 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
7173 34317 : if (err == TIFFReadDirEntryErrOk)
7174 : {
7175 34289 : if (!EvaluateIFDdatasizeReading(tif, dp))
7176 : {
7177 0 : if (data != 0)
7178 0 : _TIFFfreeExt(tif, data);
7179 0 : return 0;
7180 : }
7181 : int m;
7182 34352 : m = TIFFSetField(tif, dp->tdir_tag,
7183 34352 : (uint16_t)(dp->tdir_count), data);
7184 34403 : if (data != 0)
7185 34399 : _TIFFfreeExt(tif, data);
7186 34327 : if (!m)
7187 0 : return (0);
7188 : }
7189 : }
7190 : }
7191 34355 : break;
7192 0 : case TIFF_SETGET_C16_SINT16:
7193 : {
7194 : int16_t *data;
7195 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7196 0 : assert(fip->field_passcount == 1);
7197 0 : if (dp->tdir_count > 0xFFFF)
7198 0 : err = TIFFReadDirEntryErrCount;
7199 : else
7200 : {
7201 0 : err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7202 0 : if (err == TIFFReadDirEntryErrOk)
7203 : {
7204 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7205 : {
7206 0 : if (data != 0)
7207 0 : _TIFFfreeExt(tif, data);
7208 0 : return 0;
7209 : }
7210 : int m;
7211 0 : m = TIFFSetField(tif, dp->tdir_tag,
7212 0 : (uint16_t)(dp->tdir_count), data);
7213 0 : if (data != 0)
7214 0 : _TIFFfreeExt(tif, data);
7215 0 : if (!m)
7216 0 : return (0);
7217 : }
7218 : }
7219 : }
7220 0 : break;
7221 0 : case TIFF_SETGET_C16_UINT32:
7222 : {
7223 : uint32_t *data;
7224 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7225 0 : assert(fip->field_passcount == 1);
7226 0 : if (dp->tdir_count > 0xFFFF)
7227 0 : err = TIFFReadDirEntryErrCount;
7228 : else
7229 : {
7230 0 : err = TIFFReadDirEntryLongArray(tif, dp, &data);
7231 0 : if (err == TIFFReadDirEntryErrOk)
7232 : {
7233 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7234 : {
7235 0 : if (data != 0)
7236 0 : _TIFFfreeExt(tif, data);
7237 0 : return 0;
7238 : }
7239 : int m;
7240 0 : m = TIFFSetField(tif, dp->tdir_tag,
7241 0 : (uint16_t)(dp->tdir_count), data);
7242 0 : if (data != 0)
7243 0 : _TIFFfreeExt(tif, data);
7244 0 : if (!m)
7245 0 : return (0);
7246 : }
7247 : }
7248 : }
7249 0 : break;
7250 0 : case TIFF_SETGET_C16_SINT32:
7251 : {
7252 : int32_t *data;
7253 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7254 0 : assert(fip->field_passcount == 1);
7255 0 : if (dp->tdir_count > 0xFFFF)
7256 0 : err = TIFFReadDirEntryErrCount;
7257 : else
7258 : {
7259 0 : err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7260 0 : if (err == TIFFReadDirEntryErrOk)
7261 : {
7262 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7263 : {
7264 0 : if (data != 0)
7265 0 : _TIFFfreeExt(tif, data);
7266 0 : return 0;
7267 : }
7268 : int m;
7269 0 : m = TIFFSetField(tif, dp->tdir_tag,
7270 0 : (uint16_t)(dp->tdir_count), data);
7271 0 : if (data != 0)
7272 0 : _TIFFfreeExt(tif, data);
7273 0 : if (!m)
7274 0 : return (0);
7275 : }
7276 : }
7277 : }
7278 0 : break;
7279 0 : case TIFF_SETGET_C16_UINT64:
7280 : {
7281 : uint64_t *data;
7282 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7283 0 : assert(fip->field_passcount == 1);
7284 0 : if (dp->tdir_count > 0xFFFF)
7285 0 : err = TIFFReadDirEntryErrCount;
7286 : else
7287 : {
7288 0 : err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7289 0 : if (err == TIFFReadDirEntryErrOk)
7290 : {
7291 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7292 : {
7293 0 : if (data != 0)
7294 0 : _TIFFfreeExt(tif, data);
7295 0 : return 0;
7296 : }
7297 : int m;
7298 0 : m = TIFFSetField(tif, dp->tdir_tag,
7299 0 : (uint16_t)(dp->tdir_count), data);
7300 0 : if (data != 0)
7301 0 : _TIFFfreeExt(tif, data);
7302 0 : if (!m)
7303 0 : return (0);
7304 : }
7305 : }
7306 : }
7307 0 : break;
7308 0 : case TIFF_SETGET_C16_SINT64:
7309 : {
7310 : int64_t *data;
7311 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7312 0 : assert(fip->field_passcount == 1);
7313 0 : if (dp->tdir_count > 0xFFFF)
7314 0 : err = TIFFReadDirEntryErrCount;
7315 : else
7316 : {
7317 0 : err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7318 0 : if (err == TIFFReadDirEntryErrOk)
7319 : {
7320 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7321 : {
7322 0 : if (data != 0)
7323 0 : _TIFFfreeExt(tif, data);
7324 0 : return 0;
7325 : }
7326 : int m;
7327 0 : m = TIFFSetField(tif, dp->tdir_tag,
7328 0 : (uint16_t)(dp->tdir_count), data);
7329 0 : if (data != 0)
7330 0 : _TIFFfreeExt(tif, data);
7331 0 : if (!m)
7332 0 : return (0);
7333 : }
7334 : }
7335 : }
7336 0 : break;
7337 0 : case TIFF_SETGET_C16_FLOAT:
7338 : {
7339 : float *data;
7340 0 : assert(fip->field_readcount == TIFF_VARIABLE);
7341 0 : assert(fip->field_passcount == 1);
7342 0 : if (dp->tdir_count > 0xFFFF)
7343 0 : err = TIFFReadDirEntryErrCount;
7344 : else
7345 : {
7346 0 : err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7347 0 : if (err == TIFFReadDirEntryErrOk)
7348 : {
7349 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7350 : {
7351 0 : if (data != 0)
7352 0 : _TIFFfreeExt(tif, data);
7353 0 : return 0;
7354 : }
7355 : int m;
7356 0 : m = TIFFSetField(tif, dp->tdir_tag,
7357 0 : (uint16_t)(dp->tdir_count), data);
7358 0 : if (data != 0)
7359 0 : _TIFFfreeExt(tif, data);
7360 0 : if (!m)
7361 0 : return (0);
7362 : }
7363 : }
7364 : }
7365 0 : break;
7366 71558 : case TIFF_SETGET_C16_DOUBLE:
7367 : {
7368 : double *data;
7369 71558 : assert(fip->field_readcount == TIFF_VARIABLE);
7370 71558 : assert(fip->field_passcount == 1);
7371 71558 : if (dp->tdir_count > 0xFFFF)
7372 0 : err = TIFFReadDirEntryErrCount;
7373 : else
7374 : {
7375 71558 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7376 71535 : if (err == TIFFReadDirEntryErrOk)
7377 : {
7378 71498 : if (!EvaluateIFDdatasizeReading(tif, dp))
7379 : {
7380 0 : if (data != 0)
7381 0 : _TIFFfreeExt(tif, data);
7382 0 : return 0;
7383 : }
7384 : int m;
7385 71490 : m = TIFFSetField(tif, dp->tdir_tag,
7386 71490 : (uint16_t)(dp->tdir_count), data);
7387 71602 : if (data != 0)
7388 71588 : _TIFFfreeExt(tif, data);
7389 71528 : if (!m)
7390 0 : return (0);
7391 : }
7392 : }
7393 : }
7394 71565 : break;
7395 81 : case TIFF_SETGET_C16_IFD8:
7396 : {
7397 : uint64_t *data;
7398 81 : assert(fip->field_readcount == TIFF_VARIABLE);
7399 81 : assert(fip->field_passcount == 1);
7400 81 : if (dp->tdir_count > 0xFFFF)
7401 0 : err = TIFFReadDirEntryErrCount;
7402 : else
7403 : {
7404 81 : err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7405 81 : if (err == TIFFReadDirEntryErrOk)
7406 : {
7407 81 : if (!EvaluateIFDdatasizeReading(tif, dp))
7408 : {
7409 0 : if (data != 0)
7410 0 : _TIFFfreeExt(tif, data);
7411 0 : return 0;
7412 : }
7413 : int m;
7414 81 : m = TIFFSetField(tif, dp->tdir_tag,
7415 81 : (uint16_t)(dp->tdir_count), data);
7416 81 : if (data != 0)
7417 81 : _TIFFfreeExt(tif, data);
7418 81 : if (!m)
7419 0 : return (0);
7420 : }
7421 : }
7422 : }
7423 81 : break;
7424 9 : case TIFF_SETGET_C32_ASCII:
7425 : {
7426 : uint8_t *data;
7427 9 : assert(fip->field_readcount == TIFF_VARIABLE2);
7428 9 : assert(fip->field_passcount == 1);
7429 9 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7430 9 : if (err == TIFFReadDirEntryErrOk)
7431 : {
7432 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7433 : {
7434 0 : if (data != 0)
7435 0 : _TIFFfreeExt(tif, data);
7436 0 : return 0;
7437 : }
7438 : int m;
7439 0 : if (data != 0 && dp->tdir_count > 0 &&
7440 0 : data[dp->tdir_count - 1] != '\0')
7441 : {
7442 0 : 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 0 : uint8_t *o = (uint8_t *)_TIFFmallocExt(
7449 0 : tif, (uint32_t)dp->tdir_count + 1);
7450 0 : if (o == NULL)
7451 : {
7452 0 : if (data != NULL)
7453 0 : _TIFFfreeExt(tif, data);
7454 0 : return (0);
7455 : }
7456 0 : if (dp->tdir_count > 0)
7457 : {
7458 0 : _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
7459 : }
7460 0 : o[(uint32_t)dp->tdir_count] = 0;
7461 0 : dp->tdir_count++; /* Increment for added null. */
7462 0 : if (data != 0)
7463 0 : _TIFFfreeExt(tif, data);
7464 0 : data = o;
7465 : }
7466 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7467 : data);
7468 0 : if (data != 0)
7469 0 : _TIFFfreeExt(tif, data);
7470 0 : if (!m)
7471 0 : return (0);
7472 : }
7473 : }
7474 9 : break;
7475 2087 : case TIFF_SETGET_C32_UINT8:
7476 : {
7477 : uint8_t *data;
7478 2087 : uint32_t count = 0;
7479 2087 : assert(fip->field_readcount == TIFF_VARIABLE2);
7480 2087 : assert(fip->field_passcount == 1);
7481 2087 : if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
7482 0 : dp->tdir_type == TIFF_LONG)
7483 0 : {
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 0 : err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
7489 0 : if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
7490 : {
7491 0 : data = NULL;
7492 : }
7493 : else
7494 : {
7495 0 : if (tif->tif_flags & TIFF_SWAB)
7496 0 : TIFFSwabArrayOfLong((uint32_t *)origdata, count);
7497 0 : data = (uint8_t *)origdata;
7498 0 : count = (uint32_t)(count * 4);
7499 : }
7500 : }
7501 : else
7502 : {
7503 2087 : err = TIFFReadDirEntryByteArray(tif, dp, &data);
7504 2087 : count = (uint32_t)(dp->tdir_count);
7505 : }
7506 2087 : if (err == TIFFReadDirEntryErrOk)
7507 : {
7508 2078 : if (!EvaluateIFDdatasizeReading(tif, dp))
7509 : {
7510 0 : if (data != 0)
7511 0 : _TIFFfreeExt(tif, data);
7512 0 : return 0;
7513 : }
7514 : int m;
7515 2078 : m = TIFFSetField(tif, dp->tdir_tag, count, data);
7516 2078 : if (data != 0)
7517 2078 : _TIFFfreeExt(tif, data);
7518 2078 : if (!m)
7519 0 : return (0);
7520 : }
7521 : }
7522 2087 : break;
7523 0 : case TIFF_SETGET_C32_SINT8:
7524 : {
7525 0 : int8_t *data = NULL;
7526 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7527 0 : assert(fip->field_passcount == 1);
7528 0 : err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7529 0 : if (err == TIFFReadDirEntryErrOk)
7530 : {
7531 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7532 : {
7533 0 : if (data != 0)
7534 0 : _TIFFfreeExt(tif, data);
7535 0 : return 0;
7536 : }
7537 : int m;
7538 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7539 : data);
7540 0 : if (data != 0)
7541 0 : _TIFFfreeExt(tif, data);
7542 0 : if (!m)
7543 0 : return (0);
7544 : }
7545 : }
7546 0 : break;
7547 12 : case TIFF_SETGET_C32_UINT16:
7548 : {
7549 : uint16_t *data;
7550 12 : assert(fip->field_readcount == TIFF_VARIABLE2);
7551 12 : assert(fip->field_passcount == 1);
7552 12 : err = TIFFReadDirEntryShortArray(tif, dp, &data);
7553 12 : if (err == TIFFReadDirEntryErrOk)
7554 : {
7555 12 : if (!EvaluateIFDdatasizeReading(tif, dp))
7556 : {
7557 0 : if (data != 0)
7558 0 : _TIFFfreeExt(tif, data);
7559 0 : return 0;
7560 : }
7561 : int m;
7562 12 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7563 : data);
7564 12 : if (data != 0)
7565 12 : _TIFFfreeExt(tif, data);
7566 12 : if (!m)
7567 0 : return (0);
7568 : }
7569 : }
7570 12 : break;
7571 0 : case TIFF_SETGET_C32_SINT16:
7572 : {
7573 0 : int16_t *data = NULL;
7574 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7575 0 : assert(fip->field_passcount == 1);
7576 0 : err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7577 0 : if (err == TIFFReadDirEntryErrOk)
7578 : {
7579 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7580 : {
7581 0 : if (data != 0)
7582 0 : _TIFFfreeExt(tif, data);
7583 0 : return 0;
7584 : }
7585 : int m;
7586 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7587 : data);
7588 0 : if (data != 0)
7589 0 : _TIFFfreeExt(tif, data);
7590 0 : if (!m)
7591 0 : return (0);
7592 : }
7593 : }
7594 0 : break;
7595 800 : case TIFF_SETGET_C32_UINT32:
7596 : {
7597 : uint32_t *data;
7598 800 : assert(fip->field_readcount == TIFF_VARIABLE2);
7599 800 : assert(fip->field_passcount == 1);
7600 800 : err = TIFFReadDirEntryLongArray(tif, dp, &data);
7601 800 : if (err == TIFFReadDirEntryErrOk)
7602 : {
7603 791 : if (!EvaluateIFDdatasizeReading(tif, dp))
7604 : {
7605 0 : if (data != 0)
7606 0 : _TIFFfreeExt(tif, data);
7607 0 : return 0;
7608 : }
7609 : int m;
7610 791 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7611 : data);
7612 791 : if (data != 0)
7613 791 : _TIFFfreeExt(tif, data);
7614 791 : if (!m)
7615 0 : return (0);
7616 : }
7617 : }
7618 800 : break;
7619 0 : case TIFF_SETGET_C32_SINT32:
7620 : {
7621 0 : int32_t *data = NULL;
7622 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7623 0 : assert(fip->field_passcount == 1);
7624 0 : err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7625 0 : if (err == TIFFReadDirEntryErrOk)
7626 : {
7627 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7628 : {
7629 0 : if (data != 0)
7630 0 : _TIFFfreeExt(tif, data);
7631 0 : return 0;
7632 : }
7633 : int m;
7634 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7635 : data);
7636 0 : if (data != 0)
7637 0 : _TIFFfreeExt(tif, data);
7638 0 : if (!m)
7639 0 : return (0);
7640 : }
7641 : }
7642 0 : break;
7643 6 : case TIFF_SETGET_C32_UINT64:
7644 : {
7645 : uint64_t *data;
7646 6 : assert(fip->field_readcount == TIFF_VARIABLE2);
7647 6 : assert(fip->field_passcount == 1);
7648 6 : err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7649 6 : if (err == TIFFReadDirEntryErrOk)
7650 : {
7651 6 : if (!EvaluateIFDdatasizeReading(tif, dp))
7652 : {
7653 0 : if (data != 0)
7654 0 : _TIFFfreeExt(tif, data);
7655 0 : return 0;
7656 : }
7657 : int m;
7658 6 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7659 : data);
7660 6 : if (data != 0)
7661 6 : _TIFFfreeExt(tif, data);
7662 6 : if (!m)
7663 0 : return (0);
7664 : }
7665 : }
7666 6 : break;
7667 0 : case TIFF_SETGET_C32_SINT64:
7668 : {
7669 0 : int64_t *data = NULL;
7670 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7671 0 : assert(fip->field_passcount == 1);
7672 0 : err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7673 0 : if (err == TIFFReadDirEntryErrOk)
7674 : {
7675 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7676 : {
7677 0 : if (data != 0)
7678 0 : _TIFFfreeExt(tif, data);
7679 0 : return 0;
7680 : }
7681 : int m;
7682 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7683 : data);
7684 0 : if (data != 0)
7685 0 : _TIFFfreeExt(tif, data);
7686 0 : if (!m)
7687 0 : return (0);
7688 : }
7689 : }
7690 0 : break;
7691 0 : case TIFF_SETGET_C32_FLOAT:
7692 : {
7693 : float *data;
7694 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7695 0 : assert(fip->field_passcount == 1);
7696 0 : err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7697 0 : if (err == TIFFReadDirEntryErrOk)
7698 : {
7699 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7700 : {
7701 0 : if (data != 0)
7702 0 : _TIFFfreeExt(tif, data);
7703 0 : return 0;
7704 : }
7705 : int m;
7706 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7707 : data);
7708 0 : if (data != 0)
7709 0 : _TIFFfreeExt(tif, data);
7710 0 : if (!m)
7711 0 : return (0);
7712 : }
7713 : }
7714 0 : break;
7715 0 : case TIFF_SETGET_C32_DOUBLE:
7716 : {
7717 : double *data;
7718 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7719 0 : assert(fip->field_passcount == 1);
7720 0 : err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7721 0 : if (err == TIFFReadDirEntryErrOk)
7722 : {
7723 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7724 : {
7725 0 : if (data != 0)
7726 0 : _TIFFfreeExt(tif, data);
7727 0 : return 0;
7728 : }
7729 : int m;
7730 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7731 : data);
7732 0 : if (data != 0)
7733 0 : _TIFFfreeExt(tif, data);
7734 0 : if (!m)
7735 0 : return (0);
7736 : }
7737 : }
7738 0 : break;
7739 0 : case TIFF_SETGET_C32_IFD8:
7740 : {
7741 : uint64_t *data;
7742 0 : assert(fip->field_readcount == TIFF_VARIABLE2);
7743 0 : assert(fip->field_passcount == 1);
7744 0 : err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7745 0 : if (err == TIFFReadDirEntryErrOk)
7746 : {
7747 0 : if (!EvaluateIFDdatasizeReading(tif, dp))
7748 : {
7749 0 : if (data != 0)
7750 0 : _TIFFfreeExt(tif, data);
7751 0 : return 0;
7752 : }
7753 : int m;
7754 0 : m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7755 : data);
7756 0 : if (data != 0)
7757 0 : _TIFFfreeExt(tif, data);
7758 0 : if (!m)
7759 0 : return (0);
7760 : }
7761 : }
7762 0 : break;
7763 0 : case TIFF_SETGET_INT:
7764 : case TIFF_SETGET_C0_ASCII:
7765 : case TIFF_SETGET_OTHER:
7766 0 : assert(0); /* these should not arrive here */
7767 : break;
7768 123 : default:
7769 123 : assert(0); /* we should never get here */
7770 : break;
7771 : }
7772 501291 : if (err != TIFFReadDirEntryErrOk)
7773 : {
7774 36 : TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
7775 36 : return (0);
7776 : }
7777 501255 : 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 85959 : 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 85959 : err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
7791 85947 : if (err != TIFFReadDirEntryErrOk)
7792 : {
7793 2 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7794 2 : TIFFReadDirEntryOutputErr(tif, err, module,
7795 : fip ? fip->field_name : "unknown tagname", 0);
7796 2 : return (0);
7797 : }
7798 85945 : if (dir->tdir_count < (uint64_t)nstrips)
7799 : {
7800 : uint64_t *resizeddata;
7801 4 : const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7802 4 : const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
7803 4 : uint32_t max_nstrips = 1000000;
7804 4 : if (pszMax)
7805 0 : max_nstrips = (uint32_t)atoi(pszMax);
7806 4 : TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
7807 : fip ? fip->field_name : "unknown tagname",
7808 : (nstrips <= max_nstrips));
7809 :
7810 4 : if (nstrips > max_nstrips)
7811 : {
7812 0 : _TIFFfreeExt(tif, data);
7813 0 : return (0);
7814 : }
7815 :
7816 4 : const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
7817 4 : 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 0 : const uint64_t filesize = TIFFGetFileSize(tif);
7823 0 : if (allocsize > filesize)
7824 : {
7825 0 : 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 0 : _TIFFfreeExt(tif, data);
7832 0 : return (0);
7833 : }
7834 : }
7835 4 : resizeddata = (uint64_t *)_TIFFCheckMalloc(
7836 : tif, nstrips, sizeof(uint64_t), "for strip array");
7837 4 : if (resizeddata == 0)
7838 : {
7839 0 : _TIFFfreeExt(tif, data);
7840 0 : return (0);
7841 : }
7842 4 : if (dir->tdir_count)
7843 4 : _TIFFmemcpy(resizeddata, data,
7844 4 : (tmsize_t)((size_t)dir->tdir_count * sizeof(uint64_t)));
7845 4 : _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
7846 4 : (tmsize_t)((size_t)(nstrips - (uint32_t)dir->tdir_count) *
7847 : sizeof(uint64_t)));
7848 4 : _TIFFfreeExt(tif, data);
7849 1 : data = resizeddata;
7850 : }
7851 85942 : *lpp = data;
7852 85942 : return (1);
7853 : }
7854 :
7855 : /*
7856 : * Fetch and set the SubjectDistance EXIF tag.
7857 : */
7858 0 : static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
7859 : {
7860 : static const char module[] = "TIFFFetchSubjectDistance";
7861 : enum TIFFReadDirEntryErr err;
7862 : UInt64Aligned_t m;
7863 0 : m.l = 0;
7864 : assert(sizeof(double) == 8);
7865 : assert(sizeof(uint64_t) == 8);
7866 : assert(sizeof(uint32_t) == 4);
7867 0 : if (dir->tdir_count != 1)
7868 0 : err = TIFFReadDirEntryErrCount;
7869 0 : else if (dir->tdir_type != TIFF_RATIONAL)
7870 0 : err = TIFFReadDirEntryErrType;
7871 : else
7872 : {
7873 0 : if (!(tif->tif_flags & TIFF_BIGTIFF))
7874 : {
7875 : uint32_t offset;
7876 0 : offset = *(uint32_t *)(&dir->tdir_offset);
7877 0 : if (tif->tif_flags & TIFF_SWAB)
7878 0 : TIFFSwabLong(&offset);
7879 0 : err = TIFFReadDirEntryData(tif, offset, 8, m.i);
7880 : }
7881 : else
7882 : {
7883 0 : m.l = dir->tdir_offset.toff_long8;
7884 0 : err = TIFFReadDirEntryErrOk;
7885 : }
7886 : }
7887 0 : if (err == TIFFReadDirEntryErrOk)
7888 : {
7889 : double n;
7890 0 : if (tif->tif_flags & TIFF_SWAB)
7891 0 : TIFFSwabArrayOfLong(m.i, 2);
7892 0 : if (m.i[0] == 0)
7893 0 : n = 0.0;
7894 0 : 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 0 : n = -1.0;
7901 : else
7902 0 : n = (double)m.i[0] / (double)m.i[1];
7903 0 : return (TIFFSetField(tif, dir->tdir_tag, n));
7904 : }
7905 : else
7906 : {
7907 0 : TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
7908 0 : return (0);
7909 : }
7910 : }
7911 :
7912 159 : static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
7913 : uint64_t stripbytes,
7914 : uint32_t rowsperstrip)
7915 : {
7916 159 : 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 159 : offset = TIFFGetStrileOffset(tif, 0);
7926 159 : last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
7927 159 : last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
7928 159 : if (last_offset > UINT64_MAX - last_bytecount ||
7929 159 : last_offset + last_bytecount < offset)
7930 : {
7931 0 : return;
7932 : }
7933 159 : 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 159 : const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
7940 159 : if (allocsize > 100 * 1024 * 1024)
7941 : {
7942 0 : const uint64_t filesize = TIFFGetFileSize(tif);
7943 0 : if (allocsize > filesize)
7944 : {
7945 0 : 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 0 : return;
7952 : }
7953 : }
7954 :
7955 : newcounts =
7956 159 : (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
7957 : "for chopped \"StripByteCounts\" array");
7958 159 : newoffsets = (uint64_t *)_TIFFCheckMalloc(
7959 : tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7960 159 : 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 0 : if (newcounts != NULL)
7967 0 : _TIFFfreeExt(tif, newcounts);
7968 0 : if (newoffsets != NULL)
7969 0 : _TIFFfreeExt(tif, newoffsets);
7970 0 : return;
7971 : }
7972 :
7973 : /*
7974 : * Fill the strip information arrays with new bytecounts and offsets
7975 : * that reflect the broken-up format.
7976 : */
7977 106924 : for (i = 0; i < nstrips; i++)
7978 : {
7979 106765 : if (stripbytes > bytecount)
7980 53 : stripbytes = bytecount;
7981 106765 : newcounts[i] = stripbytes;
7982 106765 : newoffsets[i] = stripbytes ? offset : 0;
7983 106765 : offset += stripbytes;
7984 106765 : bytecount -= stripbytes;
7985 : }
7986 :
7987 : /*
7988 : * Replace old single strip info with multi-strip info.
7989 : */
7990 159 : td->td_stripsperimage = td->td_nstrips = nstrips;
7991 159 : TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
7992 :
7993 159 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
7994 159 : _TIFFfreeExt(tif, td->td_stripoffset_p);
7995 159 : td->td_stripbytecount_p = newcounts;
7996 159 : td->td_stripoffset_p = newoffsets;
7997 : #ifdef STRIPBYTECOUNTSORTED_UNUSED
7998 : td->td_stripbytecountsorted = 1;
7999 : #endif
8000 159 : 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 22451 : static void ChopUpSingleUncompressedStrip(TIFF *tif)
8010 : {
8011 22451 : 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 22451 : 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 22396 : if (bytecount == 0 && tif->tif_mode != O_RDONLY)
8025 590 : return;
8026 21806 : offset = TIFFGetStrileOffset(tif, 0);
8027 21832 : assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
8028 21807 : if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
8029 1 : rowblock = td->td_ycbcrsubsampling[1];
8030 : else
8031 21806 : rowblock = 1;
8032 21807 : 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 21776 : if (rowblockbytes > STRIP_SIZE_DEFAULT)
8038 : {
8039 23 : stripbytes = rowblockbytes;
8040 23 : rowsperstrip = rowblock;
8041 : }
8042 21753 : else if (rowblockbytes > 0)
8043 : {
8044 : uint32_t rowblocksperstrip;
8045 21715 : rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
8046 21715 : rowsperstrip = rowblocksperstrip * rowblock;
8047 21715 : stripbytes = rowblocksperstrip * rowblockbytes;
8048 : }
8049 : else
8050 38 : return;
8051 :
8052 : /*
8053 : * never increase the number of rows per strip
8054 : */
8055 21738 : if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
8056 21580 : return;
8057 158 : nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
8058 158 : if (nstrips == 0)
8059 0 : 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 159 : if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
8064 1 : (offset >= TIFFGetFileSize(tif) ||
8065 1 : stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
8066 : {
8067 1 : return;
8068 : }
8069 :
8070 157 : 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 5 : static void TryChopUpUncompressedBigTiff(TIFF *tif)
8080 : {
8081 5 : 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 5 : stripsize = TIFFStripSize64(tif);
8092 :
8093 5 : assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
8094 5 : assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
8095 5 : assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
8096 : TIFF_STRIPCHOP);
8097 5 : 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 5 : if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
8103 0 : return;
8104 :
8105 5 : if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
8106 0 : rowblock = td->td_ycbcrsubsampling[1];
8107 : else
8108 5 : rowblock = 1;
8109 5 : rowblockbytes = TIFFVStripSize64(tif, rowblock);
8110 5 : if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
8111 : {
8112 : /* In case of file with gigantic width */
8113 3 : return;
8114 : }
8115 :
8116 : /* Check that the strips are contiguous and of the expected size */
8117 5 : for (i = 0; i < td->td_nstrips; i++)
8118 : {
8119 3 : if (i == td->td_nstrips - 1)
8120 : {
8121 4 : if (TIFFGetStrileByteCount(tif, i) <
8122 2 : TIFFVStripSize64(tif,
8123 2 : td->td_imagelength - i * td->td_rowsperstrip))
8124 : {
8125 0 : return;
8126 : }
8127 : }
8128 : else
8129 : {
8130 1 : if (TIFFGetStrileByteCount(tif, i) != stripsize)
8131 : {
8132 0 : return;
8133 : }
8134 1 : if (i > 0 && TIFFGetStrileOffset(tif, i) !=
8135 0 : TIFFGetStrileOffset(tif, i - 1) +
8136 0 : TIFFGetStrileByteCount(tif, i - 1))
8137 : {
8138 0 : return;
8139 : }
8140 : }
8141 : }
8142 :
8143 : /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
8144 2 : rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
8145 2 : if (rowblocksperstrip == 0)
8146 0 : rowblocksperstrip = 1;
8147 2 : rowsperstrip = rowblocksperstrip * rowblock;
8148 2 : stripbytes = rowblocksperstrip * rowblockbytes;
8149 2 : assert(stripbytes <= 0x7FFFFFFFUL);
8150 :
8151 2 : if (rowsperstrip == 0)
8152 0 : return;
8153 2 : nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
8154 2 : if (nstrips == 0)
8155 0 : 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 2 : if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
8160 : {
8161 0 : uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
8162 0 : uint64_t filesize = TIFFGetFileSize(tif);
8163 : uint64_t last_bytecount =
8164 0 : TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
8165 0 : if (last_offset > filesize || last_bytecount > filesize - last_offset)
8166 : {
8167 0 : return;
8168 : }
8169 : }
8170 :
8171 2 : allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
8172 : }
8173 :
8174 : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
8175 6337620 : static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
8176 : {
8177 6337620 : 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 6650 : 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 6650 : 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 6650 : const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
8203 : unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
8204 :
8205 6650 : assert(dirent->tdir_count > 4);
8206 :
8207 6650 : if (dirent->tdir_type == TIFF_SHORT)
8208 : {
8209 1982 : sizeofval = sizeof(uint16_t);
8210 : }
8211 4668 : else if (dirent->tdir_type == TIFF_LONG)
8212 : {
8213 4632 : sizeofval = sizeof(uint32_t);
8214 : }
8215 36 : else if (dirent->tdir_type == TIFF_LONG8)
8216 : {
8217 36 : sizeofval = sizeof(uint64_t);
8218 : }
8219 0 : 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 0 : sizeofval = sizeof(int64_t);
8224 : }
8225 : else
8226 : {
8227 0 : TIFFErrorExtR(tif, module,
8228 : "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
8229 0 : panVals[strile] = 0;
8230 0 : return 0;
8231 : }
8232 6650 : sizeofvalint = (int)(sizeofval);
8233 :
8234 6650 : if (tif->tif_flags & TIFF_BIGTIFF)
8235 : {
8236 66 : uint64_t offset = dirent->tdir_offset.toff_long8;
8237 66 : if (bSwab)
8238 0 : TIFFSwabLong8(&offset);
8239 66 : nBaseOffset = offset;
8240 : }
8241 : else
8242 : {
8243 6584 : uint32_t offset = dirent->tdir_offset.toff_long;
8244 6584 : if (bSwab)
8245 88 : TIFFSwabLong(&offset);
8246 6584 : nBaseOffset = offset;
8247 : }
8248 : /* To avoid later unsigned integer overflows */
8249 6650 : if (nBaseOffset > (uint64_t)INT64_MAX)
8250 : {
8251 0 : TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
8252 : strile);
8253 0 : panVals[strile] = 0;
8254 0 : return 0;
8255 : }
8256 6650 : nOffset = nBaseOffset + (uint64_t)sizeofval * (uint64_t)strile;
8257 6650 : nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
8258 6650 : nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
8259 :
8260 6650 : if (nOffset + sizeofval > nOffsetEndPage)
8261 1978 : nOffsetEndPage += IO_CACHE_PAGE_SIZE;
8262 : #undef IO_CACHE_PAGE_SIZE
8263 :
8264 6650 : nLastStripOffset = nBaseOffset + (uint64_t)arraySize * sizeofval;
8265 6650 : if (nLastStripOffset < nOffsetEndPage)
8266 3603 : nOffsetEndPage = nLastStripOffset;
8267 6650 : if (nOffsetStartPage >= nOffsetEndPage)
8268 : {
8269 0 : TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
8270 : strile);
8271 0 : panVals[strile] = 0;
8272 0 : return 0;
8273 : }
8274 6650 : if (!SeekOK(tif, nOffsetStartPage))
8275 : {
8276 0 : panVals[strile] = 0;
8277 0 : return 0;
8278 : }
8279 :
8280 6650 : nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
8281 6650 : nRead = TIFFReadFile(tif, buffer, nToRead);
8282 6650 : if (nRead < nToRead)
8283 : {
8284 2 : TIFFErrorExtR(tif, module,
8285 : "Cannot read offset/size for strile around ~%d", strile);
8286 2 : return 0;
8287 : }
8288 6648 : iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
8289 6648 : if (strile + iStartBefore < 0)
8290 3633 : iStartBefore = -strile;
8291 6648 : for (i = iStartBefore;
8292 12678900 : (uint32_t)(strile + i) < arraySize &&
8293 6337630 : _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
8294 : nOffsetEndPage;
8295 6334590 : ++i)
8296 : {
8297 6334590 : if (dirent->tdir_type == TIFF_SHORT)
8298 : {
8299 : uint16_t val;
8300 2131200 : memcpy(&val,
8301 2131200 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8302 : sizeof(val));
8303 2131200 : if (bSwab)
8304 5220 : TIFFSwabShort(&val);
8305 2131200 : panVals[strile + i] = val;
8306 : }
8307 4203390 : else if (dirent->tdir_type == TIFF_LONG)
8308 : {
8309 : uint32_t val;
8310 4201940 : memcpy(&val,
8311 4201940 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8312 : sizeof(val));
8313 4201940 : if (bSwab)
8314 5528 : TIFFSwabLong(&val);
8315 4201940 : panVals[strile + i] = val;
8316 : }
8317 1458 : else if (dirent->tdir_type == TIFF_LONG8)
8318 : {
8319 : uint64_t val;
8320 1458 : memcpy(&val,
8321 1458 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8322 : sizeof(val));
8323 1458 : if (bSwab)
8324 0 : TIFFSwabLong8(&val);
8325 1458 : panVals[strile + i] = val;
8326 : }
8327 : else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
8328 : {
8329 : /* Non conformant data type */
8330 : int64_t val;
8331 0 : memcpy(&val,
8332 0 : buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8333 : sizeof(val));
8334 0 : if (bSwab)
8335 0 : TIFFSwabLong8((uint64_t *)&val);
8336 0 : panVals[strile + i] = (uint64_t)val;
8337 : }
8338 : }
8339 6643 : return 1;
8340 : }
8341 :
8342 6395860 : static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
8343 : TIFFDirEntry *dirent, uint64_t **parray)
8344 : {
8345 : static const char module[] = "_TIFFFetchStrileValue";
8346 6395860 : TIFFDirectory *td = &tif->tif_dir;
8347 6395860 : if (strile >= dirent->tdir_count)
8348 : {
8349 0 : return 0;
8350 : }
8351 6395860 : if (strile >= td->td_stripoffsetbyteallocsize)
8352 : {
8353 1820 : 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 1820 : if (strile > 1000000)
8361 : {
8362 3 : 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 3 : if (strile > filesize / sizeof(uint32_t))
8367 : {
8368 2 : TIFFErrorExtR(tif, module, "File too short");
8369 2 : return 0;
8370 : }
8371 : }
8372 :
8373 1818 : if (td->td_stripoffsetbyteallocsize == 0 &&
8374 1817 : td->td_nstrips < 1024 * 1024)
8375 : {
8376 1816 : 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 2 : nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
8383 2 : if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
8384 2 : nStripArrayAllocNew *= 2;
8385 2 : nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
8386 : }
8387 1818 : assert(strile < nStripArrayAllocNew);
8388 1818 : nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
8389 1818 : 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 1818 : offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
8399 : (tmsize_t)nArraySize));
8400 1818 : bytecountArray = (uint64_t *)(_TIFFreallocExt(
8401 1818 : tif, td->td_stripbytecount_p, (tmsize_t)nArraySize));
8402 1818 : if (offsetArray)
8403 1818 : td->td_stripoffset_p = offsetArray;
8404 1818 : if (bytecountArray)
8405 1818 : td->td_stripbytecount_p = bytecountArray;
8406 1818 : if (offsetArray && bytecountArray)
8407 : {
8408 1818 : td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
8409 : /* Initialize new entries to ~0 / -1 */
8410 : /* coverity[overrun-buffer-arg] */
8411 1818 : memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
8412 1818 : (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
8413 : sizeof(uint64_t));
8414 : /* coverity[overrun-buffer-arg] */
8415 1818 : memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
8416 1818 : (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
8417 : sizeof(uint64_t));
8418 : }
8419 : else
8420 : {
8421 0 : TIFFErrorExtR(tif, module,
8422 : "Cannot allocate strip offset and bytecount arrays");
8423 0 : _TIFFfreeExt(tif, td->td_stripoffset_p);
8424 0 : td->td_stripoffset_p = NULL;
8425 0 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
8426 0 : td->td_stripbytecount_p = NULL;
8427 0 : td->td_stripoffsetbyteallocsize = 0;
8428 : }
8429 : }
8430 6395860 : if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
8431 0 : return 0;
8432 :
8433 6395860 : if (~((*parray)[strile]) == 0)
8434 : {
8435 6650 : if (!_TIFFPartialReadStripArray(tif, dirent, (int)strile, *parray))
8436 : {
8437 2 : (*parray)[strile] = 0;
8438 2 : return 0;
8439 : }
8440 : }
8441 :
8442 6395860 : return 1;
8443 : }
8444 :
8445 6901900 : static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
8446 : TIFFDirEntry *dirent,
8447 : uint64_t **parray,
8448 : int *pbErr)
8449 : {
8450 6901900 : TIFFDirectory *td = &tif->tif_dir;
8451 6901900 : if (pbErr)
8452 4328330 : *pbErr = 0;
8453 :
8454 : /* Check that StripOffsets and StripByteCounts tags have the same number
8455 : * of declared entries. Otherwise we might take the "dirent->tdir_count <=
8456 : * 4" code path for one of them, and the other code path for the other one,
8457 : * which will lead to inconsistencies and potential out-of-bounds reads.
8458 : */
8459 6901900 : if (td->td_stripoffset_entry.tdir_count !=
8460 6901900 : td->td_stripbytecount_entry.tdir_count)
8461 : {
8462 23 : if (pbErr)
8463 3 : *pbErr = 1;
8464 23 : return 0;
8465 : }
8466 :
8467 6901880 : if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
8468 6732040 : !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
8469 : {
8470 6725490 : if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) ||
8471 : /* If the values may fit in the toff_long/toff_long8 member */
8472 : /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
8473 6715170 : dirent->tdir_count <= 4)
8474 : {
8475 329624 : if (!_TIFFFillStriles(tif))
8476 : {
8477 2 : if (pbErr)
8478 2 : *pbErr = 1;
8479 : /* Do not return, as we want this function to always */
8480 : /* return the same value if called several times with */
8481 : /* the same arguments */
8482 : }
8483 : }
8484 : else
8485 : {
8486 6395860 : if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
8487 : {
8488 4 : if (pbErr)
8489 4 : *pbErr = 1;
8490 4 : return 0;
8491 : }
8492 : }
8493 : }
8494 6901930 : if (*parray == NULL || strile >= td->td_nstrips)
8495 : {
8496 22 : if (pbErr)
8497 2 : *pbErr = 1;
8498 22 : return 0;
8499 : }
8500 6901910 : return (*parray)[strile];
8501 : }
8502 :
8503 : /* Return the value of the TileOffsets/StripOffsets array for the specified
8504 : * tile/strile */
8505 2189300 : uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
8506 : {
8507 2189300 : return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
8508 : }
8509 :
8510 : /* Return the value of the TileOffsets/StripOffsets array for the specified
8511 : * tile/strile */
8512 4331540 : uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8513 : {
8514 4331540 : TIFFDirectory *td = &tif->tif_dir;
8515 4331540 : return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
8516 : &(td->td_stripoffset_entry),
8517 : &(td->td_stripoffset_p), pbErr);
8518 : }
8519 :
8520 : /* Return the value of the TileByteCounts/StripByteCounts array for the
8521 : * specified tile/strile */
8522 384392 : uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
8523 : {
8524 384392 : return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
8525 : }
8526 :
8527 : /* Return the value of the TileByteCounts/StripByteCounts array for the
8528 : * specified tile/strile */
8529 2570410 : uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8530 : {
8531 2570410 : TIFFDirectory *td = &tif->tif_dir;
8532 2570410 : return _TIFFGetStrileOffsetOrByteCountValue(
8533 : tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
8534 : pbErr);
8535 : }
8536 :
8537 739797 : int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
8538 :
8539 739824 : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
8540 : {
8541 739824 : TIFFDirectory *td = &tif->tif_dir;
8542 739824 : int return_value = 1;
8543 :
8544 : /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
8545 739824 : if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
8546 356355 : (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
8547 383573 : return 1;
8548 :
8549 356251 : if ((tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) &&
8550 322820 : !(tif->tif_flags & TIFF_LAZYSTRILELOAD_DONE))
8551 : {
8552 : /* In case of lazy loading, reload completely the arrays */
8553 23733 : _TIFFfreeExt(tif, td->td_stripoffset_p);
8554 23725 : _TIFFfreeExt(tif, td->td_stripbytecount_p);
8555 23797 : td->td_stripoffset_p = NULL;
8556 23797 : td->td_stripbytecount_p = NULL;
8557 23797 : td->td_stripoffsetbyteallocsize = 0;
8558 23797 : tif->tif_flags |= TIFF_LAZYSTRILELOAD_DONE;
8559 : }
8560 :
8561 : /* If stripoffset array is already loaded, exit with success */
8562 356315 : if (td->td_stripoffset_p != NULL)
8563 331068 : return 1;
8564 :
8565 : /* If tdir_count was canceled, then we already got there, but in error */
8566 25247 : if (td->td_stripoffset_entry.tdir_count == 0)
8567 132 : return 0;
8568 :
8569 25115 : if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
8570 : &td->td_stripoffset_p))
8571 : {
8572 1 : return_value = 0;
8573 : }
8574 :
8575 50227 : if (loadStripByteCount &&
8576 25067 : !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
8577 : td->td_nstrips, &td->td_stripbytecount_p))
8578 : {
8579 1 : return_value = 0;
8580 : }
8581 :
8582 25160 : _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
8583 25168 : _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
8584 :
8585 : #ifdef STRIPBYTECOUNTSORTED_UNUSED
8586 : if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
8587 : {
8588 : uint32_t strip;
8589 :
8590 : tif->tif_dir.td_stripbytecountsorted = 1;
8591 : for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
8592 : {
8593 : if (tif->tif_dir.td_stripoffset_p[strip - 1] >
8594 : tif->tif_dir.td_stripoffset_p[strip])
8595 : {
8596 : tif->tif_dir.td_stripbytecountsorted = 0;
8597 : break;
8598 : }
8599 : }
8600 : }
8601 : #endif
8602 :
8603 25150 : return return_value;
8604 : }
|