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