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