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