Coverage Report

Created: 2025-10-12 07:48

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