Coverage Report

Created: 2025-07-23 08:18

/src/libtiff/libtiff/tif_dirread.c
Line
Count
Source (jump to first uncovered line)
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
2.97M
#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
1.24k
{
301
1.24k
    enum TIFFReadDirEntryErr err;
302
1.24k
    if (direntry->tdir_count != 1)
303
375
        return (TIFFReadDirEntryErrCount);
304
868
    switch (direntry->tdir_type)
305
868
    {
306
5
        case TIFF_BYTE:
307
18
        case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
308
                                field_readcount==1 */
309
18
            TIFFReadDirEntryCheckedByte(tif, direntry, value);
310
18
            return (TIFFReadDirEntryErrOk);
311
19
        case TIFF_SBYTE:
312
19
        {
313
19
            int8_t m;
314
19
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
315
19
            err = TIFFReadDirEntryCheckRangeByteSbyte(m);
316
19
            if (err != TIFFReadDirEntryErrOk)
317
7
                return (err);
318
12
            *value = (uint8_t)m;
319
12
            return (TIFFReadDirEntryErrOk);
320
19
        }
321
46
        case TIFF_SHORT:
322
46
        {
323
46
            uint16_t m;
324
46
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
325
46
            err = TIFFReadDirEntryCheckRangeByteShort(m);
326
46
            if (err != TIFFReadDirEntryErrOk)
327
11
                return (err);
328
35
            *value = (uint8_t)m;
329
35
            return (TIFFReadDirEntryErrOk);
330
46
        }
331
39
        case TIFF_SSHORT:
332
39
        {
333
39
            int16_t m;
334
39
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
335
39
            err = TIFFReadDirEntryCheckRangeByteSshort(m);
336
39
            if (err != TIFFReadDirEntryErrOk)
337
22
                return (err);
338
17
            *value = (uint8_t)m;
339
17
            return (TIFFReadDirEntryErrOk);
340
39
        }
341
55
        case TIFF_LONG:
342
55
        {
343
55
            uint32_t m;
344
55
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
345
55
            err = TIFFReadDirEntryCheckRangeByteLong(m);
346
55
            if (err != TIFFReadDirEntryErrOk)
347
49
                return (err);
348
6
            *value = (uint8_t)m;
349
6
            return (TIFFReadDirEntryErrOk);
350
55
        }
351
93
        case TIFF_SLONG:
352
93
        {
353
93
            int32_t m;
354
93
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
355
93
            err = TIFFReadDirEntryCheckRangeByteSlong(m);
356
93
            if (err != TIFFReadDirEntryErrOk)
357
83
                return (err);
358
10
            *value = (uint8_t)m;
359
10
            return (TIFFReadDirEntryErrOk);
360
93
        }
361
194
        case TIFF_LONG8:
362
194
        {
363
194
            uint64_t m;
364
194
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
365
194
            if (err != TIFFReadDirEntryErrOk)
366
16
                return (err);
367
178
            err = TIFFReadDirEntryCheckRangeByteLong8(m);
368
178
            if (err != TIFFReadDirEntryErrOk)
369
169
                return (err);
370
9
            *value = (uint8_t)m;
371
9
            return (TIFFReadDirEntryErrOk);
372
178
        }
373
392
        case TIFF_SLONG8:
374
392
        {
375
392
            int64_t m;
376
392
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
377
392
            if (err != TIFFReadDirEntryErrOk)
378
22
                return (err);
379
370
            err = TIFFReadDirEntryCheckRangeByteSlong8(m);
380
370
            if (err != TIFFReadDirEntryErrOk)
381
360
                return (err);
382
10
            *value = (uint8_t)m;
383
10
            return (TIFFReadDirEntryErrOk);
384
370
        }
385
12
        default:
386
12
            return (TIFFReadDirEntryErrType);
387
868
    }
388
868
}
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
331k
{
487
331k
    enum TIFFReadDirEntryErr err;
488
331k
    if (direntry->tdir_count != 1)
489
36.0k
        return (TIFFReadDirEntryErrCount);
490
295k
    switch (direntry->tdir_type)
491
295k
    {
492
4.70k
        case TIFF_BYTE:
493
4.70k
        {
494
4.70k
            uint8_t m;
495
4.70k
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
496
4.70k
            *value = (uint16_t)m;
497
4.70k
            return (TIFFReadDirEntryErrOk);
498
0
        }
499
3.43k
        case TIFF_SBYTE:
500
3.43k
        {
501
3.43k
            int8_t m;
502
3.43k
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
503
3.43k
            err = TIFFReadDirEntryCheckRangeShortSbyte(m);
504
3.43k
            if (err != TIFFReadDirEntryErrOk)
505
18
                return (err);
506
3.41k
            *value = (uint16_t)m;
507
3.41k
            return (TIFFReadDirEntryErrOk);
508
3.43k
        }
509
272k
        case TIFF_SHORT:
510
272k
            TIFFReadDirEntryCheckedShort(tif, direntry, value);
511
272k
            return (TIFFReadDirEntryErrOk);
512
3.88k
        case TIFF_SSHORT:
513
3.88k
        {
514
3.88k
            int16_t m;
515
3.88k
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
516
3.88k
            err = TIFFReadDirEntryCheckRangeShortSshort(m);
517
3.88k
            if (err != TIFFReadDirEntryErrOk)
518
44
                return (err);
519
3.84k
            *value = (uint16_t)m;
520
3.84k
            return (TIFFReadDirEntryErrOk);
521
3.88k
        }
522
8.94k
        case TIFF_LONG:
523
8.94k
        {
524
8.94k
            uint32_t m;
525
8.94k
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
526
8.94k
            err = TIFFReadDirEntryCheckRangeShortLong(m);
527
8.94k
            if (err != TIFFReadDirEntryErrOk)
528
106
                return (err);
529
8.83k
            *value = (uint16_t)m;
530
8.83k
            return (TIFFReadDirEntryErrOk);
531
8.94k
        }
532
821
        case TIFF_SLONG:
533
821
        {
534
821
            int32_t m;
535
821
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
536
821
            err = TIFFReadDirEntryCheckRangeShortSlong(m);
537
821
            if (err != TIFFReadDirEntryErrOk)
538
181
                return (err);
539
640
            *value = (uint16_t)m;
540
640
            return (TIFFReadDirEntryErrOk);
541
821
        }
542
509
        case TIFF_LONG8:
543
509
        {
544
509
            uint64_t m;
545
509
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
546
509
            if (err != TIFFReadDirEntryErrOk)
547
110
                return (err);
548
399
            err = TIFFReadDirEntryCheckRangeShortLong8(m);
549
399
            if (err != TIFFReadDirEntryErrOk)
550
270
                return (err);
551
129
            *value = (uint16_t)m;
552
129
            return (TIFFReadDirEntryErrOk);
553
399
        }
554
587
        case TIFF_SLONG8:
555
587
        {
556
587
            int64_t m;
557
587
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
558
587
            if (err != TIFFReadDirEntryErrOk)
559
113
                return (err);
560
474
            err = TIFFReadDirEntryCheckRangeShortSlong8(m);
561
474
            if (err != TIFFReadDirEntryErrOk)
562
418
                return (err);
563
56
            *value = (uint16_t)m;
564
56
            return (TIFFReadDirEntryErrOk);
565
474
        }
566
416
        default:
567
416
            return (TIFFReadDirEntryErrType);
568
295k
    }
569
295k
} /*-- 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
236k
{
658
236k
    enum TIFFReadDirEntryErr err;
659
236k
    if (direntry->tdir_count != 1)
660
3.19k
        return (TIFFReadDirEntryErrCount);
661
233k
    switch (direntry->tdir_type)
662
233k
    {
663
11.0k
        case TIFF_BYTE:
664
11.0k
        {
665
11.0k
            uint8_t m;
666
11.0k
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
667
11.0k
            *value = (uint32_t)m;
668
11.0k
            return (TIFFReadDirEntryErrOk);
669
0
        }
670
4.77k
        case TIFF_SBYTE:
671
4.77k
        {
672
4.77k
            int8_t m;
673
4.77k
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
674
4.77k
            err = TIFFReadDirEntryCheckRangeLongSbyte(m);
675
4.77k
            if (err != TIFFReadDirEntryErrOk)
676
12
                return (err);
677
4.76k
            *value = (uint32_t)m;
678
4.76k
            return (TIFFReadDirEntryErrOk);
679
4.77k
        }
680
190k
        case TIFF_SHORT:
681
190k
        {
682
190k
            uint16_t m;
683
190k
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
684
190k
            *value = (uint32_t)m;
685
190k
            return (TIFFReadDirEntryErrOk);
686
4.77k
        }
687
2.57k
        case TIFF_SSHORT:
688
2.57k
        {
689
2.57k
            int16_t m;
690
2.57k
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
691
2.57k
            err = TIFFReadDirEntryCheckRangeLongSshort(m);
692
2.57k
            if (err != TIFFReadDirEntryErrOk)
693
21
                return (err);
694
2.55k
            *value = (uint32_t)m;
695
2.55k
            return (TIFFReadDirEntryErrOk);
696
2.57k
        }
697
20.7k
        case TIFF_LONG:
698
20.7k
            TIFFReadDirEntryCheckedLong(tif, direntry, value);
699
20.7k
            return (TIFFReadDirEntryErrOk);
700
2.54k
        case TIFF_SLONG:
701
2.54k
        {
702
2.54k
            int32_t m;
703
2.54k
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
704
2.54k
            err = TIFFReadDirEntryCheckRangeLongSlong(m);
705
2.54k
            if (err != TIFFReadDirEntryErrOk)
706
83
                return (err);
707
2.46k
            *value = (uint32_t)m;
708
2.46k
            return (TIFFReadDirEntryErrOk);
709
2.54k
        }
710
420
        case TIFF_LONG8:
711
420
        {
712
420
            uint64_t m;
713
420
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
714
420
            if (err != TIFFReadDirEntryErrOk)
715
94
                return (err);
716
326
            err = TIFFReadDirEntryCheckRangeLongLong8(m);
717
326
            if (err != TIFFReadDirEntryErrOk)
718
203
                return (err);
719
123
            *value = (uint32_t)m;
720
123
            return (TIFFReadDirEntryErrOk);
721
326
        }
722
1.20k
        case TIFF_SLONG8:
723
1.20k
        {
724
1.20k
            int64_t m;
725
1.20k
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
726
1.20k
            if (err != TIFFReadDirEntryErrOk)
727
91
                return (err);
728
1.11k
            err = TIFFReadDirEntryCheckRangeLongSlong8(m);
729
1.11k
            if (err != TIFFReadDirEntryErrOk)
730
523
                return (err);
731
594
            *value = (uint32_t)m;
732
594
            return (TIFFReadDirEntryErrOk);
733
1.11k
        }
734
216
        default:
735
216
            return (TIFFReadDirEntryErrType);
736
233k
    }
737
233k
} /*-- 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
0
{
820
0
    enum TIFFReadDirEntryErr err;
821
0
    if (direntry->tdir_count != 1)
822
0
        return (TIFFReadDirEntryErrCount);
823
0
    switch (direntry->tdir_type)
824
0
    {
825
0
        case TIFF_BYTE:
826
0
        {
827
0
            uint8_t m;
828
0
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
829
0
            *value = (uint64_t)m;
830
0
            return (TIFFReadDirEntryErrOk);
831
0
        }
832
0
        case TIFF_SBYTE:
833
0
        {
834
0
            int8_t m;
835
0
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
836
0
            err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
837
0
            if (err != TIFFReadDirEntryErrOk)
838
0
                return (err);
839
0
            *value = (uint64_t)m;
840
0
            return (TIFFReadDirEntryErrOk);
841
0
        }
842
0
        case TIFF_SHORT:
843
0
        {
844
0
            uint16_t m;
845
0
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
846
0
            *value = (uint64_t)m;
847
0
            return (TIFFReadDirEntryErrOk);
848
0
        }
849
0
        case TIFF_SSHORT:
850
0
        {
851
0
            int16_t m;
852
0
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
853
0
            err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
854
0
            if (err != TIFFReadDirEntryErrOk)
855
0
                return (err);
856
0
            *value = (uint64_t)m;
857
0
            return (TIFFReadDirEntryErrOk);
858
0
        }
859
0
        case TIFF_LONG:
860
0
        {
861
0
            uint32_t m;
862
0
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
863
0
            *value = (uint64_t)m;
864
0
            return (TIFFReadDirEntryErrOk);
865
0
        }
866
0
        case TIFF_SLONG:
867
0
        {
868
0
            int32_t m;
869
0
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
870
0
            err = TIFFReadDirEntryCheckRangeLong8Slong(m);
871
0
            if (err != TIFFReadDirEntryErrOk)
872
0
                return (err);
873
0
            *value = (uint64_t)m;
874
0
            return (TIFFReadDirEntryErrOk);
875
0
        }
876
0
        case TIFF_LONG8:
877
0
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
878
0
            return (err);
879
0
        case TIFF_SLONG8:
880
0
        {
881
0
            int64_t m;
882
0
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
883
0
            if (err != TIFFReadDirEntryErrOk)
884
0
                return (err);
885
0
            err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
886
0
            if (err != TIFFReadDirEntryErrOk)
887
0
                return (err);
888
0
            *value = (uint64_t)m;
889
0
            return (TIFFReadDirEntryErrOk);
890
0
        }
891
0
        default:
892
0
            return (TIFFReadDirEntryErrType);
893
0
    }
894
0
} /*-- 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
16.6k
{
969
16.6k
    enum TIFFReadDirEntryErr err;
970
16.6k
    if (direntry->tdir_count != 1)
971
3.32k
        return (TIFFReadDirEntryErrCount);
972
13.3k
    switch (direntry->tdir_type)
973
13.3k
    {
974
18
        case TIFF_BYTE:
975
18
        {
976
18
            uint8_t m;
977
18
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
978
18
            *value = (float)m;
979
18
            return (TIFFReadDirEntryErrOk);
980
0
        }
981
21
        case TIFF_SBYTE:
982
21
        {
983
21
            int8_t m;
984
21
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
985
21
            *value = (float)m;
986
21
            return (TIFFReadDirEntryErrOk);
987
0
        }
988
652
        case TIFF_SHORT:
989
652
        {
990
652
            uint16_t m;
991
652
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
992
652
            *value = (float)m;
993
652
            return (TIFFReadDirEntryErrOk);
994
0
        }
995
39
        case TIFF_SSHORT:
996
39
        {
997
39
            int16_t m;
998
39
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
999
39
            *value = (float)m;
1000
39
            return (TIFFReadDirEntryErrOk);
1001
0
        }
1002
513
        case TIFF_LONG:
1003
513
        {
1004
513
            uint32_t m;
1005
513
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1006
513
            *value = (float)m;
1007
513
            return (TIFFReadDirEntryErrOk);
1008
0
        }
1009
73
        case TIFF_SLONG:
1010
73
        {
1011
73
            int32_t m;
1012
73
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1013
73
            *value = (float)m;
1014
73
            return (TIFFReadDirEntryErrOk);
1015
0
        }
1016
75
        case TIFF_LONG8:
1017
75
        {
1018
75
            uint64_t m;
1019
75
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1020
75
            if (err != TIFFReadDirEntryErrOk)
1021
35
                return (err);
1022
40
            *value = (float)m;
1023
40
            return (TIFFReadDirEntryErrOk);
1024
75
        }
1025
130
        case TIFF_SLONG8:
1026
130
        {
1027
130
            int64_t m;
1028
130
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1029
130
            if (err != TIFFReadDirEntryErrOk)
1030
54
                return (err);
1031
76
            *value = (float)m;
1032
76
            return (TIFFReadDirEntryErrOk);
1033
130
        }
1034
10.4k
        case TIFF_RATIONAL:
1035
10.4k
        {
1036
10.4k
            double m;
1037
10.4k
            err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
1038
10.4k
            if (err != TIFFReadDirEntryErrOk)
1039
724
                return (err);
1040
9.71k
            *value = (float)m;
1041
9.71k
            return (TIFFReadDirEntryErrOk);
1042
10.4k
        }
1043
761
        case TIFF_SRATIONAL:
1044
761
        {
1045
761
            double m;
1046
761
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
1047
761
            if (err != TIFFReadDirEntryErrOk)
1048
230
                return (err);
1049
531
            *value = (float)m;
1050
531
            return (TIFFReadDirEntryErrOk);
1051
761
        }
1052
85
        case TIFF_FLOAT:
1053
85
            TIFFReadDirEntryCheckedFloat(tif, direntry, value);
1054
85
            return (TIFFReadDirEntryErrOk);
1055
341
        case TIFF_DOUBLE:
1056
341
        {
1057
341
            double m;
1058
341
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
1059
341
            if (err != TIFFReadDirEntryErrOk)
1060
195
                return (err);
1061
146
            if ((m > FLT_MAX) || (m < -FLT_MAX))
1062
76
                return (TIFFReadDirEntryErrRange);
1063
70
            *value = (float)m;
1064
70
            return (TIFFReadDirEntryErrOk);
1065
146
        }
1066
155
        default:
1067
155
            return (TIFFReadDirEntryErrType);
1068
13.3k
    }
1069
13.3k
}
1070
1071
static enum TIFFReadDirEntryErr
1072
TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
1073
163
{
1074
163
    enum TIFFReadDirEntryErr err;
1075
163
    if (direntry->tdir_count != 1)
1076
100
        return (TIFFReadDirEntryErrCount);
1077
63
    switch (direntry->tdir_type)
1078
63
    {
1079
4
        case TIFF_BYTE:
1080
4
        {
1081
4
            uint8_t m;
1082
4
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1083
4
            *value = (double)m;
1084
4
            return (TIFFReadDirEntryErrOk);
1085
0
        }
1086
2
        case TIFF_SBYTE:
1087
2
        {
1088
2
            int8_t m;
1089
2
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1090
2
            *value = (double)m;
1091
2
            return (TIFFReadDirEntryErrOk);
1092
0
        }
1093
6
        case TIFF_SHORT:
1094
6
        {
1095
6
            uint16_t m;
1096
6
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1097
6
            *value = (double)m;
1098
6
            return (TIFFReadDirEntryErrOk);
1099
0
        }
1100
5
        case TIFF_SSHORT:
1101
5
        {
1102
5
            int16_t m;
1103
5
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1104
5
            *value = (double)m;
1105
5
            return (TIFFReadDirEntryErrOk);
1106
0
        }
1107
5
        case TIFF_LONG:
1108
5
        {
1109
5
            uint32_t m;
1110
5
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1111
5
            *value = (double)m;
1112
5
            return (TIFFReadDirEntryErrOk);
1113
0
        }
1114
6
        case TIFF_SLONG:
1115
6
        {
1116
6
            int32_t m;
1117
6
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1118
6
            *value = (double)m;
1119
6
            return (TIFFReadDirEntryErrOk);
1120
0
        }
1121
6
        case TIFF_LONG8:
1122
6
        {
1123
6
            uint64_t m;
1124
6
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1125
6
            if (err != TIFFReadDirEntryErrOk)
1126
3
                return (err);
1127
3
            *value = (double)m;
1128
3
            return (TIFFReadDirEntryErrOk);
1129
6
        }
1130
5
        case TIFF_SLONG8:
1131
5
        {
1132
5
            int64_t m;
1133
5
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1134
5
            if (err != TIFFReadDirEntryErrOk)
1135
3
                return (err);
1136
2
            *value = (double)m;
1137
2
            return (TIFFReadDirEntryErrOk);
1138
5
        }
1139
3
        case TIFF_RATIONAL:
1140
3
            err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
1141
3
            return (err);
1142
4
        case TIFF_SRATIONAL:
1143
4
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
1144
4
            return (err);
1145
5
        case TIFF_FLOAT:
1146
5
        {
1147
5
            float m;
1148
5
            TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
1149
5
            *value = (double)m;
1150
5
            return (TIFFReadDirEntryErrOk);
1151
5
        }
1152
7
        case TIFF_DOUBLE:
1153
7
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
1154
7
            return (err);
1155
5
        default:
1156
5
            return (TIFFReadDirEntryErrType);
1157
63
    }
1158
63
}
1159
1160
static enum TIFFReadDirEntryErr
1161
TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1162
1.33k
{
1163
1.33k
    enum TIFFReadDirEntryErr err;
1164
1.33k
    if (direntry->tdir_count != 1)
1165
541
        return (TIFFReadDirEntryErrCount);
1166
789
    switch (direntry->tdir_type)
1167
789
    {
1168
593
        case TIFF_LONG:
1169
607
        case TIFF_IFD:
1170
607
        {
1171
607
            uint32_t m;
1172
607
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1173
607
            *value = (uint64_t)m;
1174
607
            return (TIFFReadDirEntryErrOk);
1175
593
        }
1176
72
        case TIFF_LONG8:
1177
87
        case TIFF_IFD8:
1178
87
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1179
87
            return (err);
1180
95
        default:
1181
95
            return (TIFFReadDirEntryErrType);
1182
789
    }
1183
789
}
1184
1185
24.4k
#define INITIAL_THRESHOLD (1024 * 1024)
1186
28.2k
#define THRESHOLD_MULTIPLIER 10
1187
#define MAX_THRESHOLD                                                          \
1188
7.05k
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
1189
7.05k
     INITIAL_THRESHOLD)
1190
1191
static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
1192
                                                               uint64_t offset,
1193
                                                               tmsize_t size,
1194
                                                               void **pdest)
1195
17.4k
{
1196
17.4k
#if SIZEOF_SIZE_T == 8
1197
17.4k
    tmsize_t threshold = INITIAL_THRESHOLD;
1198
17.4k
#endif
1199
17.4k
    tmsize_t already_read = 0;
1200
1201
17.4k
    assert(!isMapped(tif));
1202
1203
17.4k
    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
19.7k
    while (already_read < size)
1213
17.4k
    {
1214
17.4k
        void *new_dest;
1215
17.4k
        tmsize_t bytes_read;
1216
17.4k
        tmsize_t to_read = size - already_read;
1217
17.4k
#if SIZEOF_SIZE_T == 8
1218
17.4k
        if (to_read >= threshold && threshold < MAX_THRESHOLD)
1219
7.05k
        {
1220
7.05k
            to_read = threshold;
1221
7.05k
            threshold *= THRESHOLD_MULTIPLIER;
1222
7.05k
        }
1223
17.4k
#endif
1224
1225
17.4k
        new_dest =
1226
17.4k
            (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
1227
17.4k
        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
17.4k
        *pdest = new_dest;
1238
1239
17.4k
        bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
1240
17.4k
        already_read += bytes_read;
1241
17.4k
        if (bytes_read != to_read)
1242
15.0k
        {
1243
15.0k
            return TIFFReadDirEntryErrIo;
1244
15.0k
        }
1245
17.4k
    }
1246
2.34k
    return TIFFReadDirEntryErrOk;
1247
17.4k
}
1248
1249
/* Caution: if raising that value, make sure int32 / uint32 overflows can't
1250
 * occur elsewhere */
1251
836k
#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
447k
{
1258
447k
    int typesize;
1259
447k
    uint32_t datasize;
1260
447k
    void *data;
1261
447k
    uint64_t target_count64;
1262
447k
    int original_datasize_clamped;
1263
447k
    typesize = TIFFDataWidth(direntry->tdir_type);
1264
1265
447k
    target_count64 =
1266
447k
        (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
1267
1268
447k
    if ((target_count64 == 0) || (typesize == 0))
1269
7.99k
    {
1270
7.99k
        *value = 0;
1271
7.99k
        return (TIFFReadDirEntryErrOk);
1272
7.99k
    }
1273
439k
    (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
439k
    original_datasize_clamped =
1279
439k
        ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
1280
439k
        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
439k
    if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64)
1288
43.4k
        return (TIFFReadDirEntryErrSizesan);
1289
396k
    if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
1290
122
        return (TIFFReadDirEntryErrSizesan);
1291
1292
396k
    *count = (uint32_t)target_count64;
1293
396k
    datasize = (*count) * typesize;
1294
396k
    assert((tmsize_t)datasize > 0);
1295
1296
396k
    if (datasize > 100 * 1024 * 1024)
1297
22.8k
    {
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
22.8k
        const uint64_t filesize = TIFFGetFileSize(tif);
1302
22.8k
        if (datasize > filesize)
1303
22.8k
        {
1304
22.8k
            TIFFWarningExtR(tif, "ReadDirEntryArray",
1305
22.8k
                            "Requested memory size for tag %d (0x%x) %" PRIu32
1306
22.8k
                            " is greater than filesize %" PRIu64
1307
22.8k
                            ". Memory not allocated, tag not read",
1308
22.8k
                            direntry->tdir_tag, direntry->tdir_tag, datasize,
1309
22.8k
                            filesize);
1310
22.8k
            return (TIFFReadDirEntryErrAlloc);
1311
22.8k
        }
1312
22.8k
    }
1313
1314
373k
    if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
1315
73.0k
        return TIFFReadDirEntryErrIo;
1316
1317
300k
    if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
1318
25.0k
                           (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
1319
16.8k
    {
1320
16.8k
        data = NULL;
1321
16.8k
    }
1322
283k
    else
1323
283k
    {
1324
283k
        data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
1325
283k
        if (data == 0)
1326
0
            return (TIFFReadDirEntryErrAlloc);
1327
283k
    }
1328
300k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
1329
298k
    {
1330
        /* Only the condition on original_datasize_clamped. The second
1331
         * one is implied, but Coverity Scan cannot see it. */
1332
298k
        if (original_datasize_clamped <= 4 && datasize <= 4)
1333
100k
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1334
198k
        else
1335
198k
        {
1336
198k
            enum TIFFReadDirEntryErr err;
1337
198k
            uint32_t offset = direntry->tdir_offset.toff_long;
1338
198k
            if (tif->tif_flags & TIFF_SWAB)
1339
5.09k
                TIFFSwabLong(&offset);
1340
198k
            if (isMapped(tif))
1341
180k
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1342
180k
                                           (tmsize_t)datasize, data);
1343
17.4k
            else
1344
17.4k
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1345
17.4k
                                                     (tmsize_t)datasize, &data);
1346
198k
            if (err != TIFFReadDirEntryErrOk)
1347
53.3k
            {
1348
53.3k
                _TIFFfreeExt(tif, data);
1349
53.3k
                return (err);
1350
53.3k
            }
1351
198k
        }
1352
298k
    }
1353
1.80k
    else
1354
1.80k
    {
1355
        /* See above comment for the Classic TIFF case */
1356
1.80k
        if (original_datasize_clamped <= 8 && datasize <= 8)
1357
628
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1358
1.17k
        else
1359
1.17k
        {
1360
1.17k
            enum TIFFReadDirEntryErr err;
1361
1.17k
            uint64_t offset = direntry->tdir_offset.toff_long8;
1362
1.17k
            if (tif->tif_flags & TIFF_SWAB)
1363
138
                TIFFSwabLong8(&offset);
1364
1.17k
            if (isMapped(tif))
1365
1.17k
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1366
1.17k
                                           (tmsize_t)datasize, data);
1367
0
            else
1368
0
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1369
0
                                                     (tmsize_t)datasize, &data);
1370
1.17k
            if (err != TIFFReadDirEntryErrOk)
1371
655
            {
1372
655
                _TIFFfreeExt(tif, data);
1373
655
                return (err);
1374
655
            }
1375
1.17k
        }
1376
1.80k
    }
1377
246k
    *value = data;
1378
246k
    return (TIFFReadDirEntryErrOk);
1379
300k
}
1380
1381
static enum TIFFReadDirEntryErr
1382
TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
1383
                      uint32_t desttypesize, void **value)
1384
342k
{
1385
342k
    return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
1386
342k
                                          value, ~((uint64_t)0));
1387
342k
}
1388
1389
static enum TIFFReadDirEntryErr
1390
TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
1391
114k
{
1392
114k
    enum TIFFReadDirEntryErr err;
1393
114k
    uint32_t count;
1394
114k
    void *origdata;
1395
114k
    uint8_t *data;
1396
114k
    switch (direntry->tdir_type)
1397
114k
    {
1398
68.4k
        case TIFF_ASCII:
1399
71.1k
        case TIFF_UNDEFINED:
1400
98.2k
        case TIFF_BYTE:
1401
98.6k
        case TIFF_SBYTE:
1402
102k
        case TIFF_SHORT:
1403
102k
        case TIFF_SSHORT:
1404
104k
        case TIFF_LONG:
1405
105k
        case TIFF_SLONG:
1406
105k
        case TIFF_LONG8:
1407
106k
        case TIFF_SLONG8:
1408
106k
            break;
1409
8.38k
        default:
1410
8.38k
            return (TIFFReadDirEntryErrType);
1411
114k
    }
1412
106k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1413
106k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1414
67.9k
    {
1415
67.9k
        *value = 0;
1416
67.9k
        return (err);
1417
67.9k
    }
1418
38.0k
    switch (direntry->tdir_type)
1419
38.0k
    {
1420
24.2k
        case TIFF_ASCII:
1421
25.6k
        case TIFF_UNDEFINED:
1422
32.7k
        case TIFF_BYTE:
1423
32.7k
            *value = (uint8_t *)origdata;
1424
32.7k
            return (TIFFReadDirEntryErrOk);
1425
309
        case TIFF_SBYTE:
1426
309
        {
1427
309
            int8_t *m;
1428
309
            uint32_t n;
1429
309
            m = (int8_t *)origdata;
1430
1.84k
            for (n = 0; n < count; n++)
1431
1.60k
            {
1432
1.60k
                err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
1433
1.60k
                if (err != TIFFReadDirEntryErrOk)
1434
73
                {
1435
73
                    _TIFFfreeExt(tif, origdata);
1436
73
                    return (err);
1437
73
                }
1438
1.53k
                m++;
1439
1.53k
            }
1440
236
            *value = (uint8_t *)origdata;
1441
236
            return (TIFFReadDirEntryErrOk);
1442
309
        }
1443
38.0k
    }
1444
4.98k
    data = (uint8_t *)_TIFFmallocExt(tif, count);
1445
4.98k
    if (data == 0)
1446
0
    {
1447
0
        _TIFFfreeExt(tif, origdata);
1448
0
        return (TIFFReadDirEntryErrAlloc);
1449
0
    }
1450
4.98k
    switch (direntry->tdir_type)
1451
4.98k
    {
1452
2.73k
        case TIFF_SHORT:
1453
2.73k
        {
1454
2.73k
            uint16_t *ma;
1455
2.73k
            uint8_t *mb;
1456
2.73k
            uint32_t n;
1457
2.73k
            ma = (uint16_t *)origdata;
1458
2.73k
            mb = data;
1459
6.72k
            for (n = 0; n < count; n++)
1460
4.48k
            {
1461
4.48k
                if (tif->tif_flags & TIFF_SWAB)
1462
659
                    TIFFSwabShort(ma);
1463
4.48k
                err = TIFFReadDirEntryCheckRangeByteShort(*ma);
1464
4.48k
                if (err != TIFFReadDirEntryErrOk)
1465
500
                    break;
1466
3.98k
                *mb++ = (uint8_t)(*ma++);
1467
3.98k
            }
1468
2.73k
        }
1469
2.73k
        break;
1470
135
        case TIFF_SSHORT:
1471
135
        {
1472
135
            int16_t *ma;
1473
135
            uint8_t *mb;
1474
135
            uint32_t n;
1475
135
            ma = (int16_t *)origdata;
1476
135
            mb = data;
1477
1.09k
            for (n = 0; n < count; n++)
1478
1.04k
            {
1479
1.04k
                if (tif->tif_flags & TIFF_SWAB)
1480
395
                    TIFFSwabShort((uint16_t *)ma);
1481
1.04k
                err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
1482
1.04k
                if (err != TIFFReadDirEntryErrOk)
1483
93
                    break;
1484
956
                *mb++ = (uint8_t)(*ma++);
1485
956
            }
1486
135
        }
1487
135
        break;
1488
1.16k
        case TIFF_LONG:
1489
1.16k
        {
1490
1.16k
            uint32_t *ma;
1491
1.16k
            uint8_t *mb;
1492
1.16k
            uint32_t n;
1493
1.16k
            ma = (uint32_t *)origdata;
1494
1.16k
            mb = data;
1495
3.40k
            for (n = 0; n < count; n++)
1496
3.00k
            {
1497
3.00k
                if (tif->tif_flags & TIFF_SWAB)
1498
683
                    TIFFSwabLong(ma);
1499
3.00k
                err = TIFFReadDirEntryCheckRangeByteLong(*ma);
1500
3.00k
                if (err != TIFFReadDirEntryErrOk)
1501
763
                    break;
1502
2.24k
                *mb++ = (uint8_t)(*ma++);
1503
2.24k
            }
1504
1.16k
        }
1505
1.16k
        break;
1506
243
        case TIFF_SLONG:
1507
243
        {
1508
243
            int32_t *ma;
1509
243
            uint8_t *mb;
1510
243
            uint32_t n;
1511
243
            ma = (int32_t *)origdata;
1512
243
            mb = data;
1513
1.21k
            for (n = 0; n < count; n++)
1514
1.18k
            {
1515
1.18k
                if (tif->tif_flags & TIFF_SWAB)
1516
249
                    TIFFSwabLong((uint32_t *)ma);
1517
1.18k
                err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
1518
1.18k
                if (err != TIFFReadDirEntryErrOk)
1519
216
                    break;
1520
970
                *mb++ = (uint8_t)(*ma++);
1521
970
            }
1522
243
        }
1523
243
        break;
1524
266
        case TIFF_LONG8:
1525
266
        {
1526
266
            uint64_t *ma;
1527
266
            uint8_t *mb;
1528
266
            uint32_t n;
1529
266
            ma = (uint64_t *)origdata;
1530
266
            mb = data;
1531
1.16k
            for (n = 0; n < count; n++)
1532
1.13k
            {
1533
1.13k
                if (tif->tif_flags & TIFF_SWAB)
1534
667
                    TIFFSwabLong8(ma);
1535
1.13k
                err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
1536
1.13k
                if (err != TIFFReadDirEntryErrOk)
1537
236
                    break;
1538
894
                *mb++ = (uint8_t)(*ma++);
1539
894
            }
1540
266
        }
1541
266
        break;
1542
446
        case TIFF_SLONG8:
1543
446
        {
1544
446
            int64_t *ma;
1545
446
            uint8_t *mb;
1546
446
            uint32_t n;
1547
446
            ma = (int64_t *)origdata;
1548
446
            mb = data;
1549
1.80k
            for (n = 0; n < count; n++)
1550
1.77k
            {
1551
1.77k
                if (tif->tif_flags & TIFF_SWAB)
1552
549
                    TIFFSwabLong8((uint64_t *)ma);
1553
1.77k
                err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1554
1.77k
                if (err != TIFFReadDirEntryErrOk)
1555
417
                    break;
1556
1.35k
                *mb++ = (uint8_t)(*ma++);
1557
1.35k
            }
1558
446
        }
1559
446
        break;
1560
4.98k
    }
1561
4.98k
    _TIFFfreeExt(tif, origdata);
1562
4.98k
    if (err != TIFFReadDirEntryErrOk)
1563
2.22k
    {
1564
2.22k
        _TIFFfreeExt(tif, data);
1565
2.22k
        return (err);
1566
2.22k
    }
1567
2.76k
    *value = data;
1568
2.76k
    return (TIFFReadDirEntryErrOk);
1569
4.98k
}
1570
1571
static enum TIFFReadDirEntryErr
1572
TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
1573
8.19k
{
1574
8.19k
    enum TIFFReadDirEntryErr err;
1575
8.19k
    uint32_t count;
1576
8.19k
    void *origdata;
1577
8.19k
    int8_t *data;
1578
8.19k
    switch (direntry->tdir_type)
1579
8.19k
    {
1580
0
        case TIFF_UNDEFINED:
1581
0
        case TIFF_BYTE:
1582
8.19k
        case TIFF_SBYTE:
1583
8.19k
        case TIFF_SHORT:
1584
8.19k
        case TIFF_SSHORT:
1585
8.19k
        case TIFF_LONG:
1586
8.19k
        case TIFF_SLONG:
1587
8.19k
        case TIFF_LONG8:
1588
8.19k
        case TIFF_SLONG8:
1589
8.19k
            break;
1590
0
        default:
1591
0
            return (TIFFReadDirEntryErrType);
1592
8.19k
    }
1593
8.19k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1594
8.19k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1595
7.64k
    {
1596
7.64k
        *value = 0;
1597
7.64k
        return (err);
1598
7.64k
    }
1599
547
    switch (direntry->tdir_type)
1600
547
    {
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
547
        case TIFF_SBYTE:
1621
547
            *value = (int8_t *)origdata;
1622
547
            return (TIFFReadDirEntryErrOk);
1623
547
    }
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
126k
{
1754
126k
    enum TIFFReadDirEntryErr err;
1755
126k
    uint32_t count;
1756
126k
    void *origdata;
1757
126k
    uint16_t *data;
1758
126k
    switch (direntry->tdir_type)
1759
126k
    {
1760
2.58k
        case TIFF_BYTE:
1761
3.07k
        case TIFF_SBYTE:
1762
123k
        case TIFF_SHORT:
1763
124k
        case TIFF_SSHORT:
1764
124k
        case TIFF_LONG:
1765
124k
        case TIFF_SLONG:
1766
125k
        case TIFF_LONG8:
1767
126k
        case TIFF_SLONG8:
1768
126k
            break;
1769
659
        default:
1770
659
            return (TIFFReadDirEntryErrType);
1771
126k
    }
1772
126k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1773
126k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1774
53.2k
    {
1775
53.2k
        *value = 0;
1776
53.2k
        return (err);
1777
53.2k
    }
1778
73.0k
    switch (direntry->tdir_type)
1779
73.0k
    {
1780
68.7k
        case TIFF_SHORT:
1781
68.7k
            *value = (uint16_t *)origdata;
1782
68.7k
            if (tif->tif_flags & TIFF_SWAB)
1783
1.54k
                TIFFSwabArrayOfShort(*value, count);
1784
68.7k
            return (TIFFReadDirEntryErrOk);
1785
235
        case TIFF_SSHORT:
1786
235
        {
1787
235
            int16_t *m;
1788
235
            uint32_t n;
1789
235
            m = (int16_t *)origdata;
1790
2.61k
            for (n = 0; n < count; n++)
1791
2.43k
            {
1792
2.43k
                if (tif->tif_flags & TIFF_SWAB)
1793
959
                    TIFFSwabShort((uint16_t *)m);
1794
2.43k
                err = TIFFReadDirEntryCheckRangeShortSshort(*m);
1795
2.43k
                if (err != TIFFReadDirEntryErrOk)
1796
55
                {
1797
55
                    _TIFFfreeExt(tif, origdata);
1798
55
                    return (err);
1799
55
                }
1800
2.37k
                m++;
1801
2.37k
            }
1802
180
            *value = (uint16_t *)origdata;
1803
180
            return (TIFFReadDirEntryErrOk);
1804
235
        }
1805
73.0k
    }
1806
4.02k
    data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
1807
4.02k
    if (data == 0)
1808
0
    {
1809
0
        _TIFFfreeExt(tif, origdata);
1810
0
        return (TIFFReadDirEntryErrAlloc);
1811
0
    }
1812
4.02k
    switch (direntry->tdir_type)
1813
4.02k
    {
1814
2.18k
        case TIFF_BYTE:
1815
2.18k
        {
1816
2.18k
            uint8_t *ma;
1817
2.18k
            uint16_t *mb;
1818
2.18k
            uint32_t n;
1819
2.18k
            ma = (uint8_t *)origdata;
1820
2.18k
            mb = data;
1821
110k
            for (n = 0; n < count; n++)
1822
108k
                *mb++ = (uint16_t)(*ma++);
1823
2.18k
        }
1824
2.18k
        break;
1825
399
        case TIFF_SBYTE:
1826
399
        {
1827
399
            int8_t *ma;
1828
399
            uint16_t *mb;
1829
399
            uint32_t n;
1830
399
            ma = (int8_t *)origdata;
1831
399
            mb = data;
1832
5.05k
            for (n = 0; n < count; n++)
1833
4.67k
            {
1834
4.67k
                err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1835
4.67k
                if (err != TIFFReadDirEntryErrOk)
1836
20
                    break;
1837
4.65k
                *mb++ = (uint16_t)(*ma++);
1838
4.65k
            }
1839
399
        }
1840
399
        break;
1841
285
        case TIFF_LONG:
1842
285
        {
1843
285
            uint32_t *ma;
1844
285
            uint16_t *mb;
1845
285
            uint32_t n;
1846
285
            ma = (uint32_t *)origdata;
1847
285
            mb = data;
1848
1.69k
            for (n = 0; n < count; n++)
1849
1.54k
            {
1850
1.54k
                if (tif->tif_flags & TIFF_SWAB)
1851
664
                    TIFFSwabLong(ma);
1852
1.54k
                err = TIFFReadDirEntryCheckRangeShortLong(*ma);
1853
1.54k
                if (err != TIFFReadDirEntryErrOk)
1854
139
                    break;
1855
1.40k
                *mb++ = (uint16_t)(*ma++);
1856
1.40k
            }
1857
285
        }
1858
285
        break;
1859
264
        case TIFF_SLONG:
1860
264
        {
1861
264
            int32_t *ma;
1862
264
            uint16_t *mb;
1863
264
            uint32_t n;
1864
264
            ma = (int32_t *)origdata;
1865
264
            mb = data;
1866
1.38k
            for (n = 0; n < count; n++)
1867
1.32k
            {
1868
1.32k
                if (tif->tif_flags & TIFF_SWAB)
1869
631
                    TIFFSwabLong((uint32_t *)ma);
1870
1.32k
                err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
1871
1.32k
                if (err != TIFFReadDirEntryErrOk)
1872
210
                    break;
1873
1.11k
                *mb++ = (uint16_t)(*ma++);
1874
1.11k
            }
1875
264
        }
1876
264
        break;
1877
288
        case TIFF_LONG8:
1878
288
        {
1879
288
            uint64_t *ma;
1880
288
            uint16_t *mb;
1881
288
            uint32_t n;
1882
288
            ma = (uint64_t *)origdata;
1883
288
            mb = data;
1884
1.66k
            for (n = 0; n < count; n++)
1885
1.63k
            {
1886
1.63k
                if (tif->tif_flags & TIFF_SWAB)
1887
802
                    TIFFSwabLong8(ma);
1888
1.63k
                err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
1889
1.63k
                if (err != TIFFReadDirEntryErrOk)
1890
249
                    break;
1891
1.38k
                *mb++ = (uint16_t)(*ma++);
1892
1.38k
            }
1893
288
        }
1894
288
        break;
1895
603
        case TIFF_SLONG8:
1896
603
        {
1897
603
            int64_t *ma;
1898
603
            uint16_t *mb;
1899
603
            uint32_t n;
1900
603
            ma = (int64_t *)origdata;
1901
603
            mb = data;
1902
2.26k
            for (n = 0; n < count; n++)
1903
2.21k
            {
1904
2.21k
                if (tif->tif_flags & TIFF_SWAB)
1905
738
                    TIFFSwabLong8((uint64_t *)ma);
1906
2.21k
                err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1907
2.21k
                if (err != TIFFReadDirEntryErrOk)
1908
556
                    break;
1909
1.65k
                *mb++ = (uint16_t)(*ma++);
1910
1.65k
            }
1911
603
        }
1912
603
        break;
1913
4.02k
    }
1914
4.02k
    _TIFFfreeExt(tif, origdata);
1915
4.02k
    if (err != TIFFReadDirEntryErrOk)
1916
1.17k
    {
1917
1.17k
        _TIFFfreeExt(tif, data);
1918
1.17k
        return (err);
1919
1.17k
    }
1920
2.84k
    *value = data;
1921
2.84k
    return (TIFFReadDirEntryErrOk);
1922
4.02k
}
1923
1924
static enum TIFFReadDirEntryErr
1925
TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
1926
3.82k
{
1927
3.82k
    enum TIFFReadDirEntryErr err;
1928
3.82k
    uint32_t count;
1929
3.82k
    void *origdata;
1930
3.82k
    int16_t *data;
1931
3.82k
    switch (direntry->tdir_type)
1932
3.82k
    {
1933
0
        case TIFF_BYTE:
1934
0
        case TIFF_SBYTE:
1935
0
        case TIFF_SHORT:
1936
3.82k
        case TIFF_SSHORT:
1937
3.82k
        case TIFF_LONG:
1938
3.82k
        case TIFF_SLONG:
1939
3.82k
        case TIFF_LONG8:
1940
3.82k
        case TIFF_SLONG8:
1941
3.82k
            break;
1942
0
        default:
1943
0
            return (TIFFReadDirEntryErrType);
1944
3.82k
    }
1945
3.82k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1946
3.82k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1947
2.25k
    {
1948
2.25k
        *value = 0;
1949
2.25k
        return (err);
1950
2.25k
    }
1951
1.57k
    switch (direntry->tdir_type)
1952
1.57k
    {
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
1.57k
        case TIFF_SSHORT:
1974
1.57k
            *value = (int16_t *)origdata;
1975
1.57k
            if (tif->tif_flags & TIFF_SWAB)
1976
79
                TIFFSwabArrayOfShort((uint16_t *)(*value), count);
1977
1.57k
            return (TIFFReadDirEntryErrOk);
1978
1.57k
    }
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
28.0k
{
2095
28.0k
    enum TIFFReadDirEntryErr err;
2096
28.0k
    uint32_t count;
2097
28.0k
    void *origdata;
2098
28.0k
    uint32_t *data;
2099
28.0k
    switch (direntry->tdir_type)
2100
28.0k
    {
2101
50
        case TIFF_BYTE:
2102
284
        case TIFF_SBYTE:
2103
408
        case TIFF_SHORT:
2104
509
        case TIFF_SSHORT:
2105
27.0k
        case TIFF_LONG:
2106
27.1k
        case TIFF_SLONG:
2107
27.5k
        case TIFF_LONG8:
2108
27.9k
        case TIFF_SLONG8:
2109
27.9k
            break;
2110
35
        default:
2111
35
            return (TIFFReadDirEntryErrType);
2112
28.0k
    }
2113
27.9k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2114
27.9k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2115
17.6k
    {
2116
17.6k
        *value = 0;
2117
17.6k
        return (err);
2118
17.6k
    }
2119
10.3k
    switch (direntry->tdir_type)
2120
10.3k
    {
2121
9.02k
        case TIFF_LONG:
2122
9.02k
            *value = (uint32_t *)origdata;
2123
9.02k
            if (tif->tif_flags & TIFF_SWAB)
2124
248
                TIFFSwabArrayOfLong(*value, count);
2125
9.02k
            return (TIFFReadDirEntryErrOk);
2126
124
        case TIFF_SLONG:
2127
124
        {
2128
124
            int32_t *m;
2129
124
            uint32_t n;
2130
124
            m = (int32_t *)origdata;
2131
1.63k
            for (n = 0; n < count; n++)
2132
1.59k
            {
2133
1.59k
                if (tif->tif_flags & TIFF_SWAB)
2134
879
                    TIFFSwabLong((uint32_t *)m);
2135
1.59k
                err = TIFFReadDirEntryCheckRangeLongSlong(*m);
2136
1.59k
                if (err != TIFFReadDirEntryErrOk)
2137
89
                {
2138
89
                    _TIFFfreeExt(tif, origdata);
2139
89
                    return (err);
2140
89
                }
2141
1.50k
                m++;
2142
1.50k
            }
2143
35
            *value = (uint32_t *)origdata;
2144
35
            return (TIFFReadDirEntryErrOk);
2145
124
        }
2146
10.3k
    }
2147
1.22k
    data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
2148
1.22k
    if (data == 0)
2149
0
    {
2150
0
        _TIFFfreeExt(tif, origdata);
2151
0
        return (TIFFReadDirEntryErrAlloc);
2152
0
    }
2153
1.22k
    switch (direntry->tdir_type)
2154
1.22k
    {
2155
45
        case TIFF_BYTE:
2156
45
        {
2157
45
            uint8_t *ma;
2158
45
            uint32_t *mb;
2159
45
            uint32_t n;
2160
45
            ma = (uint8_t *)origdata;
2161
45
            mb = data;
2162
1.20k
            for (n = 0; n < count; n++)
2163
1.16k
                *mb++ = (uint32_t)(*ma++);
2164
45
        }
2165
45
        break;
2166
227
        case TIFF_SBYTE:
2167
227
        {
2168
227
            int8_t *ma;
2169
227
            uint32_t *mb;
2170
227
            uint32_t n;
2171
227
            ma = (int8_t *)origdata;
2172
227
            mb = data;
2173
2.76k
            for (n = 0; n < count; n++)
2174
2.58k
            {
2175
2.58k
                err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
2176
2.58k
                if (err != TIFFReadDirEntryErrOk)
2177
47
                    break;
2178
2.53k
                *mb++ = (uint32_t)(*ma++);
2179
2.53k
            }
2180
227
        }
2181
227
        break;
2182
119
        case TIFF_SHORT:
2183
119
        {
2184
119
            uint16_t *ma;
2185
119
            uint32_t *mb;
2186
119
            uint32_t n;
2187
119
            ma = (uint16_t *)origdata;
2188
119
            mb = data;
2189
1.74k
            for (n = 0; n < count; n++)
2190
1.63k
            {
2191
1.63k
                if (tif->tif_flags & TIFF_SWAB)
2192
874
                    TIFFSwabShort(ma);
2193
1.63k
                *mb++ = (uint32_t)(*ma++);
2194
1.63k
            }
2195
119
        }
2196
119
        break;
2197
93
        case TIFF_SSHORT:
2198
93
        {
2199
93
            int16_t *ma;
2200
93
            uint32_t *mb;
2201
93
            uint32_t n;
2202
93
            ma = (int16_t *)origdata;
2203
93
            mb = data;
2204
2.07k
            for (n = 0; n < count; n++)
2205
2.02k
            {
2206
2.02k
                if (tif->tif_flags & TIFF_SWAB)
2207
1.05k
                    TIFFSwabShort((uint16_t *)ma);
2208
2.02k
                err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
2209
2.02k
                if (err != TIFFReadDirEntryErrOk)
2210
44
                    break;
2211
1.98k
                *mb++ = (uint32_t)(*ma++);
2212
1.98k
            }
2213
93
        }
2214
93
        break;
2215
336
        case TIFF_LONG8:
2216
336
        {
2217
336
            uint64_t *ma;
2218
336
            uint32_t *mb;
2219
336
            uint32_t n;
2220
336
            ma = (uint64_t *)origdata;
2221
336
            mb = data;
2222
1.55k
            for (n = 0; n < count; n++)
2223
1.51k
            {
2224
1.51k
                if (tif->tif_flags & TIFF_SWAB)
2225
767
                    TIFFSwabLong8(ma);
2226
1.51k
                err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
2227
1.51k
                if (err != TIFFReadDirEntryErrOk)
2228
292
                    break;
2229
1.21k
                *mb++ = (uint32_t)(*ma++);
2230
1.21k
            }
2231
336
        }
2232
336
        break;
2233
401
        case TIFF_SLONG8:
2234
401
        {
2235
401
            int64_t *ma;
2236
401
            uint32_t *mb;
2237
401
            uint32_t n;
2238
401
            ma = (int64_t *)origdata;
2239
401
            mb = data;
2240
38.3k
            for (n = 0; n < count; n++)
2241
38.2k
            {
2242
38.2k
                if (tif->tif_flags & TIFF_SWAB)
2243
36.1k
                    TIFFSwabLong8((uint64_t *)ma);
2244
38.2k
                err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
2245
38.2k
                if (err != TIFFReadDirEntryErrOk)
2246
345
                    break;
2247
37.9k
                *mb++ = (uint32_t)(*ma++);
2248
37.9k
            }
2249
401
        }
2250
401
        break;
2251
1.22k
    }
2252
1.22k
    _TIFFfreeExt(tif, origdata);
2253
1.22k
    if (err != TIFFReadDirEntryErrOk)
2254
728
    {
2255
728
        _TIFFfreeExt(tif, data);
2256
728
        return (err);
2257
728
    }
2258
493
    *value = data;
2259
493
    return (TIFFReadDirEntryErrOk);
2260
1.22k
}
2261
2262
static enum TIFFReadDirEntryErr
2263
TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
2264
2.57k
{
2265
2.57k
    enum TIFFReadDirEntryErr err;
2266
2.57k
    uint32_t count;
2267
2.57k
    void *origdata;
2268
2.57k
    int32_t *data;
2269
2.57k
    switch (direntry->tdir_type)
2270
2.57k
    {
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
2.57k
        case TIFF_SLONG:
2277
2.57k
        case TIFF_LONG8:
2278
2.57k
        case TIFF_SLONG8:
2279
2.57k
            break;
2280
0
        default:
2281
0
            return (TIFFReadDirEntryErrType);
2282
2.57k
    }
2283
2.57k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2284
2.57k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2285
1.85k
    {
2286
1.85k
        *value = 0;
2287
1.85k
        return (err);
2288
1.85k
    }
2289
718
    switch (direntry->tdir_type)
2290
718
    {
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
718
        case TIFF_SLONG:
2312
718
            *value = (int32_t *)origdata;
2313
718
            if (tif->tif_flags & TIFF_SWAB)
2314
91
                TIFFSwabArrayOfLong((uint32_t *)(*value), count);
2315
718
            return (TIFFReadDirEntryErrOk);
2316
718
    }
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
106k
{
2428
106k
    enum TIFFReadDirEntryErr err;
2429
106k
    uint32_t count;
2430
106k
    void *origdata;
2431
106k
    uint64_t *data;
2432
106k
    switch (direntry->tdir_type)
2433
106k
    {
2434
26.5k
        case TIFF_BYTE:
2435
27.9k
        case TIFF_SBYTE:
2436
35.9k
        case TIFF_SHORT:
2437
38.3k
        case TIFF_SSHORT:
2438
94.5k
        case TIFF_LONG:
2439
94.9k
        case TIFF_SLONG:
2440
104k
        case TIFF_LONG8:
2441
105k
        case TIFF_SLONG8:
2442
105k
            break;
2443
1.20k
        default:
2444
1.20k
            return (TIFFReadDirEntryErrType);
2445
106k
    }
2446
105k
    err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
2447
105k
                                         maxcount);
2448
105k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2449
5.91k
    {
2450
5.91k
        *value = 0;
2451
5.91k
        return (err);
2452
5.91k
    }
2453
99.5k
    switch (direntry->tdir_type)
2454
99.5k
    {
2455
7.25k
        case TIFF_LONG8:
2456
7.25k
            *value = (uint64_t *)origdata;
2457
7.25k
            if (tif->tif_flags & TIFF_SWAB)
2458
448
                TIFFSwabArrayOfLong8(*value, count);
2459
7.25k
            return (TIFFReadDirEntryErrOk);
2460
1.29k
        case TIFF_SLONG8:
2461
1.29k
        {
2462
1.29k
            int64_t *m;
2463
1.29k
            uint32_t n;
2464
1.29k
            m = (int64_t *)origdata;
2465
4.08k
            for (n = 0; n < count; n++)
2466
2.98k
            {
2467
2.98k
                if (tif->tif_flags & TIFF_SWAB)
2468
234
                    TIFFSwabLong8((uint64_t *)m);
2469
2.98k
                err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2470
2.98k
                if (err != TIFFReadDirEntryErrOk)
2471
191
                {
2472
191
                    _TIFFfreeExt(tif, origdata);
2473
191
                    return (err);
2474
191
                }
2475
2.79k
                m++;
2476
2.79k
            }
2477
1.10k
            *value = (uint64_t *)origdata;
2478
1.10k
            return (TIFFReadDirEntryErrOk);
2479
1.29k
        }
2480
99.5k
    }
2481
91.0k
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
2482
91.0k
    if (data == 0)
2483
0
    {
2484
0
        _TIFFfreeExt(tif, origdata);
2485
0
        return (TIFFReadDirEntryErrAlloc);
2486
0
    }
2487
91.0k
    switch (direntry->tdir_type)
2488
91.0k
    {
2489
25.4k
        case TIFF_BYTE:
2490
25.4k
        {
2491
25.4k
            uint8_t *ma;
2492
25.4k
            uint64_t *mb;
2493
25.4k
            uint32_t n;
2494
25.4k
            ma = (uint8_t *)origdata;
2495
25.4k
            mb = data;
2496
875k
            for (n = 0; n < count; n++)
2497
849k
                *mb++ = (uint64_t)(*ma++);
2498
25.4k
        }
2499
25.4k
        break;
2500
1.03k
        case TIFF_SBYTE:
2501
1.03k
        {
2502
1.03k
            int8_t *ma;
2503
1.03k
            uint64_t *mb;
2504
1.03k
            uint32_t n;
2505
1.03k
            ma = (int8_t *)origdata;
2506
1.03k
            mb = data;
2507
3.74k
            for (n = 0; n < count; n++)
2508
2.71k
            {
2509
2.71k
                err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2510
2.71k
                if (err != TIFFReadDirEntryErrOk)
2511
14
                    break;
2512
2.70k
                *mb++ = (uint64_t)(*ma++);
2513
2.70k
            }
2514
1.03k
        }
2515
1.03k
        break;
2516
7.73k
        case TIFF_SHORT:
2517
7.73k
        {
2518
7.73k
            uint16_t *ma;
2519
7.73k
            uint64_t *mb;
2520
7.73k
            uint32_t n;
2521
7.73k
            ma = (uint16_t *)origdata;
2522
7.73k
            mb = data;
2523
152k
            for (n = 0; n < count; n++)
2524
145k
            {
2525
145k
                if (tif->tif_flags & TIFF_SWAB)
2526
4.17k
                    TIFFSwabShort(ma);
2527
145k
                *mb++ = (uint64_t)(*ma++);
2528
145k
            }
2529
7.73k
        }
2530
7.73k
        break;
2531
2.24k
        case TIFF_SSHORT:
2532
2.24k
        {
2533
2.24k
            int16_t *ma;
2534
2.24k
            uint64_t *mb;
2535
2.24k
            uint32_t n;
2536
2.24k
            ma = (int16_t *)origdata;
2537
2.24k
            mb = data;
2538
6.08k
            for (n = 0; n < count; n++)
2539
3.90k
            {
2540
3.90k
                if (tif->tif_flags & TIFF_SWAB)
2541
991
                    TIFFSwabShort((uint16_t *)ma);
2542
3.90k
                err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2543
3.90k
                if (err != TIFFReadDirEntryErrOk)
2544
67
                    break;
2545
3.83k
                *mb++ = (uint64_t)(*ma++);
2546
3.83k
            }
2547
2.24k
        }
2548
2.24k
        break;
2549
54.2k
        case TIFF_LONG:
2550
54.2k
        {
2551
54.2k
            uint32_t *ma;
2552
54.2k
            uint64_t *mb;
2553
54.2k
            uint32_t n;
2554
54.2k
            ma = (uint32_t *)origdata;
2555
54.2k
            mb = data;
2556
684k
            for (n = 0; n < count; n++)
2557
629k
            {
2558
629k
                if (tif->tif_flags & TIFF_SWAB)
2559
7.59k
                    TIFFSwabLong(ma);
2560
629k
                *mb++ = (uint64_t)(*ma++);
2561
629k
            }
2562
54.2k
        }
2563
54.2k
        break;
2564
312
        case TIFF_SLONG:
2565
312
        {
2566
312
            int32_t *ma;
2567
312
            uint64_t *mb;
2568
312
            uint32_t n;
2569
312
            ma = (int32_t *)origdata;
2570
312
            mb = data;
2571
1.76k
            for (n = 0; n < count; n++)
2572
1.50k
            {
2573
1.50k
                if (tif->tif_flags & TIFF_SWAB)
2574
588
                    TIFFSwabLong((uint32_t *)ma);
2575
1.50k
                err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2576
1.50k
                if (err != TIFFReadDirEntryErrOk)
2577
54
                    break;
2578
1.45k
                *mb++ = (uint64_t)(*ma++);
2579
1.45k
            }
2580
312
        }
2581
312
        break;
2582
91.0k
    }
2583
91.0k
    _TIFFfreeExt(tif, origdata);
2584
91.0k
    if (err != TIFFReadDirEntryErrOk)
2585
135
    {
2586
135
        _TIFFfreeExt(tif, data);
2587
135
        return (err);
2588
135
    }
2589
90.9k
    *value = data;
2590
90.9k
    return (TIFFReadDirEntryErrOk);
2591
91.0k
}
2592
2593
static enum TIFFReadDirEntryErr
2594
TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
2595
1.75k
{
2596
1.75k
    return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
2597
1.75k
                                               ~((uint64_t)0));
2598
1.75k
}
2599
2600
static enum TIFFReadDirEntryErr
2601
TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
2602
1.77k
{
2603
1.77k
    enum TIFFReadDirEntryErr err;
2604
1.77k
    uint32_t count;
2605
1.77k
    void *origdata;
2606
1.77k
    int64_t *data;
2607
1.77k
    switch (direntry->tdir_type)
2608
1.77k
    {
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
1.77k
        case TIFF_SLONG8:
2617
1.77k
            break;
2618
0
        default:
2619
0
            return (TIFFReadDirEntryErrType);
2620
1.77k
    }
2621
1.77k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
2622
1.77k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2623
1.01k
    {
2624
1.01k
        *value = 0;
2625
1.01k
        return (err);
2626
1.01k
    }
2627
760
    switch (direntry->tdir_type)
2628
760
    {
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
760
        case TIFF_SLONG8:
2650
760
            *value = (int64_t *)origdata;
2651
760
            if (tif->tif_flags & TIFF_SWAB)
2652
83
                TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
2653
760
            return (TIFFReadDirEntryErrOk);
2654
760
    }
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
18.2k
{
2754
18.2k
    enum TIFFReadDirEntryErr err;
2755
18.2k
    uint32_t count;
2756
18.2k
    void *origdata;
2757
18.2k
    float *data;
2758
18.2k
    switch (direntry->tdir_type)
2759
18.2k
    {
2760
79
        case TIFF_BYTE:
2761
165
        case TIFF_SBYTE:
2762
283
        case TIFF_SHORT:
2763
364
        case TIFF_SSHORT:
2764
1.02k
        case TIFF_LONG:
2765
1.20k
        case TIFF_SLONG:
2766
1.31k
        case TIFF_LONG8:
2767
1.46k
        case TIFF_SLONG8:
2768
13.0k
        case TIFF_RATIONAL:
2769
15.4k
        case TIFF_SRATIONAL:
2770
17.7k
        case TIFF_FLOAT:
2771
17.9k
        case TIFF_DOUBLE:
2772
17.9k
            break;
2773
291
        default:
2774
291
            return (TIFFReadDirEntryErrType);
2775
18.2k
    }
2776
17.9k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2777
17.9k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2778
13.3k
    {
2779
13.3k
        *value = 0;
2780
13.3k
        return (err);
2781
13.3k
    }
2782
4.61k
    switch (direntry->tdir_type)
2783
4.61k
    {
2784
298
        case TIFF_FLOAT:
2785
298
            if (tif->tif_flags & TIFF_SWAB)
2786
42
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
2787
298
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
2788
298
            *value = (float *)origdata;
2789
298
            return (TIFFReadDirEntryErrOk);
2790
4.61k
    }
2791
4.31k
    data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
2792
4.31k
    if (data == 0)
2793
0
    {
2794
0
        _TIFFfreeExt(tif, origdata);
2795
0
        return (TIFFReadDirEntryErrAlloc);
2796
0
    }
2797
4.31k
    switch (direntry->tdir_type)
2798
4.31k
    {
2799
72
        case TIFF_BYTE:
2800
72
        {
2801
72
            uint8_t *ma;
2802
72
            float *mb;
2803
72
            uint32_t n;
2804
72
            ma = (uint8_t *)origdata;
2805
72
            mb = data;
2806
3.66k
            for (n = 0; n < count; n++)
2807
3.59k
                *mb++ = (float)(*ma++);
2808
72
        }
2809
72
        break;
2810
79
        case TIFF_SBYTE:
2811
79
        {
2812
79
            int8_t *ma;
2813
79
            float *mb;
2814
79
            uint32_t n;
2815
79
            ma = (int8_t *)origdata;
2816
79
            mb = data;
2817
963
            for (n = 0; n < count; n++)
2818
884
                *mb++ = (float)(*ma++);
2819
79
        }
2820
79
        break;
2821
102
        case TIFF_SHORT:
2822
102
        {
2823
102
            uint16_t *ma;
2824
102
            float *mb;
2825
102
            uint32_t n;
2826
102
            ma = (uint16_t *)origdata;
2827
102
            mb = data;
2828
2.11k
            for (n = 0; n < count; n++)
2829
2.01k
            {
2830
2.01k
                if (tif->tif_flags & TIFF_SWAB)
2831
624
                    TIFFSwabShort(ma);
2832
2.01k
                *mb++ = (float)(*ma++);
2833
2.01k
            }
2834
102
        }
2835
102
        break;
2836
76
        case TIFF_SSHORT:
2837
76
        {
2838
76
            int16_t *ma;
2839
76
            float *mb;
2840
76
            uint32_t n;
2841
76
            ma = (int16_t *)origdata;
2842
76
            mb = data;
2843
1.55k
            for (n = 0; n < count; n++)
2844
1.48k
            {
2845
1.48k
                if (tif->tif_flags & TIFF_SWAB)
2846
669
                    TIFFSwabShort((uint16_t *)ma);
2847
1.48k
                *mb++ = (float)(*ma++);
2848
1.48k
            }
2849
76
        }
2850
76
        break;
2851
433
        case TIFF_LONG:
2852
433
        {
2853
433
            uint32_t *ma;
2854
433
            float *mb;
2855
433
            uint32_t n;
2856
433
            ma = (uint32_t *)origdata;
2857
433
            mb = data;
2858
11.8k
            for (n = 0; n < count; n++)
2859
11.4k
            {
2860
11.4k
                if (tif->tif_flags & TIFF_SWAB)
2861
1.04k
                    TIFFSwabLong(ma);
2862
11.4k
                *mb++ = (float)(*ma++);
2863
11.4k
            }
2864
433
        }
2865
433
        break;
2866
119
        case TIFF_SLONG:
2867
119
        {
2868
119
            int32_t *ma;
2869
119
            float *mb;
2870
119
            uint32_t n;
2871
119
            ma = (int32_t *)origdata;
2872
119
            mb = data;
2873
2.95k
            for (n = 0; n < count; n++)
2874
2.83k
            {
2875
2.83k
                if (tif->tif_flags & TIFF_SWAB)
2876
880
                    TIFFSwabLong((uint32_t *)ma);
2877
2.83k
                *mb++ = (float)(*ma++);
2878
2.83k
            }
2879
119
        }
2880
119
        break;
2881
97
        case TIFF_LONG8:
2882
97
        {
2883
97
            uint64_t *ma;
2884
97
            float *mb;
2885
97
            uint32_t n;
2886
97
            ma = (uint64_t *)origdata;
2887
97
            mb = data;
2888
1.74k
            for (n = 0; n < count; n++)
2889
1.64k
            {
2890
1.64k
                if (tif->tif_flags & TIFF_SWAB)
2891
830
                    TIFFSwabLong8(ma);
2892
1.64k
                *mb++ = (float)(*ma++);
2893
1.64k
            }
2894
97
        }
2895
97
        break;
2896
132
        case TIFF_SLONG8:
2897
132
        {
2898
132
            int64_t *ma;
2899
132
            float *mb;
2900
132
            uint32_t n;
2901
132
            ma = (int64_t *)origdata;
2902
132
            mb = data;
2903
66.8k
            for (n = 0; n < count; n++)
2904
66.7k
            {
2905
66.7k
                if (tif->tif_flags & TIFF_SWAB)
2906
66.0k
                    TIFFSwabLong8((uint64_t *)ma);
2907
66.7k
                *mb++ = (float)(*ma++);
2908
66.7k
            }
2909
132
        }
2910
132
        break;
2911
2.00k
        case TIFF_RATIONAL:
2912
2.00k
        {
2913
2.00k
            uint32_t *ma;
2914
2.00k
            uint32_t maa;
2915
2.00k
            uint32_t mab;
2916
2.00k
            float *mb;
2917
2.00k
            uint32_t n;
2918
2.00k
            ma = (uint32_t *)origdata;
2919
2.00k
            mb = data;
2920
123k
            for (n = 0; n < count; n++)
2921
121k
            {
2922
121k
                if (tif->tif_flags & TIFF_SWAB)
2923
2.65k
                    TIFFSwabLong(ma);
2924
121k
                maa = *ma++;
2925
121k
                if (tif->tif_flags & TIFF_SWAB)
2926
2.65k
                    TIFFSwabLong(ma);
2927
121k
                mab = *ma++;
2928
121k
                if (mab == 0)
2929
46.5k
                    *mb++ = 0.0;
2930
74.5k
                else
2931
74.5k
                    *mb++ = (float)maa / (float)mab;
2932
121k
            }
2933
2.00k
        }
2934
2.00k
        break;
2935
1.07k
        case TIFF_SRATIONAL:
2936
1.07k
        {
2937
1.07k
            uint32_t *ma;
2938
1.07k
            int32_t maa;
2939
1.07k
            uint32_t mab;
2940
1.07k
            float *mb;
2941
1.07k
            uint32_t n;
2942
1.07k
            ma = (uint32_t *)origdata;
2943
1.07k
            mb = data;
2944
52.6k
            for (n = 0; n < count; n++)
2945
51.5k
            {
2946
51.5k
                if (tif->tif_flags & TIFF_SWAB)
2947
1.11k
                    TIFFSwabLong(ma);
2948
51.5k
                maa = *(int32_t *)ma;
2949
51.5k
                ma++;
2950
51.5k
                if (tif->tif_flags & TIFF_SWAB)
2951
1.11k
                    TIFFSwabLong(ma);
2952
51.5k
                mab = *ma++;
2953
51.5k
                if (mab == 0)
2954
10.6k
                    *mb++ = 0.0;
2955
40.9k
                else
2956
40.9k
                    *mb++ = (float)maa / (float)mab;
2957
51.5k
            }
2958
1.07k
        }
2959
1.07k
        break;
2960
129
        case TIFF_DOUBLE:
2961
129
        {
2962
129
            double *ma;
2963
129
            float *mb;
2964
129
            uint32_t n;
2965
129
            if (tif->tif_flags & TIFF_SWAB)
2966
24
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
2967
129
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
2968
129
            ma = (double *)origdata;
2969
129
            mb = data;
2970
5.34k
            for (n = 0; n < count; n++)
2971
5.21k
            {
2972
5.21k
                double val = *ma++;
2973
5.21k
                if (val > FLT_MAX)
2974
950
                    val = FLT_MAX;
2975
4.26k
                else if (val < -FLT_MAX)
2976
1.46k
                    val = -FLT_MAX;
2977
5.21k
                *mb++ = (float)val;
2978
5.21k
            }
2979
129
        }
2980
129
        break;
2981
4.31k
    }
2982
4.31k
    _TIFFfreeExt(tif, origdata);
2983
4.31k
    *value = data;
2984
4.31k
    return (TIFFReadDirEntryErrOk);
2985
4.31k
}
2986
2987
static enum TIFFReadDirEntryErr
2988
TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
2989
44.9k
{
2990
44.9k
    enum TIFFReadDirEntryErr err;
2991
44.9k
    uint32_t count;
2992
44.9k
    void *origdata;
2993
44.9k
    double *data;
2994
44.9k
    switch (direntry->tdir_type)
2995
44.9k
    {
2996
24
        case TIFF_BYTE:
2997
59
        case TIFF_SBYTE:
2998
793
        case TIFF_SHORT:
2999
832
        case TIFF_SSHORT:
3000
945
        case TIFF_LONG:
3001
986
        case TIFF_SLONG:
3002
1.02k
        case TIFF_LONG8:
3003
1.06k
        case TIFF_SLONG8:
3004
1.16k
        case TIFF_RATIONAL:
3005
1.33k
        case TIFF_SRATIONAL:
3006
1.42k
        case TIFF_FLOAT:
3007
44.9k
        case TIFF_DOUBLE:
3008
44.9k
            break;
3009
5
        default:
3010
5
            return (TIFFReadDirEntryErrType);
3011
44.9k
    }
3012
44.9k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3013
44.9k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3014
28.3k
    {
3015
28.3k
        *value = 0;
3016
28.3k
        return (err);
3017
28.3k
    }
3018
16.5k
    switch (direntry->tdir_type)
3019
16.5k
    {
3020
15.1k
        case TIFF_DOUBLE:
3021
15.1k
            if (tif->tif_flags & TIFF_SWAB)
3022
28
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3023
15.1k
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3024
15.1k
            *value = (double *)origdata;
3025
15.1k
            return (TIFFReadDirEntryErrOk);
3026
16.5k
    }
3027
1.41k
    data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
3028
1.41k
    if (data == 0)
3029
0
    {
3030
0
        _TIFFfreeExt(tif, origdata);
3031
0
        return (TIFFReadDirEntryErrAlloc);
3032
0
    }
3033
1.41k
    switch (direntry->tdir_type)
3034
1.41k
    {
3035
24
        case TIFF_BYTE:
3036
24
        {
3037
24
            uint8_t *ma;
3038
24
            double *mb;
3039
24
            uint32_t n;
3040
24
            ma = (uint8_t *)origdata;
3041
24
            mb = data;
3042
305
            for (n = 0; n < count; n++)
3043
281
                *mb++ = (double)(*ma++);
3044
24
        }
3045
24
        break;
3046
35
        case TIFF_SBYTE:
3047
35
        {
3048
35
            int8_t *ma;
3049
35
            double *mb;
3050
35
            uint32_t n;
3051
35
            ma = (int8_t *)origdata;
3052
35
            mb = data;
3053
606
            for (n = 0; n < count; n++)
3054
571
                *mb++ = (double)(*ma++);
3055
35
        }
3056
35
        break;
3057
734
        case TIFF_SHORT:
3058
734
        {
3059
734
            uint16_t *ma;
3060
734
            double *mb;
3061
734
            uint32_t n;
3062
734
            ma = (uint16_t *)origdata;
3063
734
            mb = data;
3064
2.35k
            for (n = 0; n < count; n++)
3065
1.62k
            {
3066
1.62k
                if (tif->tif_flags & TIFF_SWAB)
3067
616
                    TIFFSwabShort(ma);
3068
1.62k
                *mb++ = (double)(*ma++);
3069
1.62k
            }
3070
734
        }
3071
734
        break;
3072
39
        case TIFF_SSHORT:
3073
39
        {
3074
39
            int16_t *ma;
3075
39
            double *mb;
3076
39
            uint32_t n;
3077
39
            ma = (int16_t *)origdata;
3078
39
            mb = data;
3079
450
            for (n = 0; n < count; n++)
3080
411
            {
3081
411
                if (tif->tif_flags & TIFF_SWAB)
3082
116
                    TIFFSwabShort((uint16_t *)ma);
3083
411
                *mb++ = (double)(*ma++);
3084
411
            }
3085
39
        }
3086
39
        break;
3087
113
        case TIFF_LONG:
3088
113
        {
3089
113
            uint32_t *ma;
3090
113
            double *mb;
3091
113
            uint32_t n;
3092
113
            ma = (uint32_t *)origdata;
3093
113
            mb = data;
3094
458
            for (n = 0; n < count; n++)
3095
345
            {
3096
345
                if (tif->tif_flags & TIFF_SWAB)
3097
88
                    TIFFSwabLong(ma);
3098
345
                *mb++ = (double)(*ma++);
3099
345
            }
3100
113
        }
3101
113
        break;
3102
41
        case TIFF_SLONG:
3103
41
        {
3104
41
            int32_t *ma;
3105
41
            double *mb;
3106
41
            uint32_t n;
3107
41
            ma = (int32_t *)origdata;
3108
41
            mb = data;
3109
831
            for (n = 0; n < count; n++)
3110
790
            {
3111
790
                if (tif->tif_flags & TIFF_SWAB)
3112
592
                    TIFFSwabLong((uint32_t *)ma);
3113
790
                *mb++ = (double)(*ma++);
3114
790
            }
3115
41
        }
3116
41
        break;
3117
35
        case TIFF_LONG8:
3118
35
        {
3119
35
            uint64_t *ma;
3120
35
            double *mb;
3121
35
            uint32_t n;
3122
35
            ma = (uint64_t *)origdata;
3123
35
            mb = data;
3124
291
            for (n = 0; n < count; n++)
3125
256
            {
3126
256
                if (tif->tif_flags & TIFF_SWAB)
3127
182
                    TIFFSwabLong8(ma);
3128
256
                *mb++ = (double)(*ma++);
3129
256
            }
3130
35
        }
3131
35
        break;
3132
41
        case TIFF_SLONG8:
3133
41
        {
3134
41
            int64_t *ma;
3135
41
            double *mb;
3136
41
            uint32_t n;
3137
41
            ma = (int64_t *)origdata;
3138
41
            mb = data;
3139
470
            for (n = 0; n < count; n++)
3140
429
            {
3141
429
                if (tif->tif_flags & TIFF_SWAB)
3142
160
                    TIFFSwabLong8((uint64_t *)ma);
3143
429
                *mb++ = (double)(*ma++);
3144
429
            }
3145
41
        }
3146
41
        break;
3147
95
        case TIFF_RATIONAL:
3148
95
        {
3149
95
            uint32_t *ma;
3150
95
            uint32_t maa;
3151
95
            uint32_t mab;
3152
95
            double *mb;
3153
95
            uint32_t n;
3154
95
            ma = (uint32_t *)origdata;
3155
95
            mb = data;
3156
1.17k
            for (n = 0; n < count; n++)
3157
1.07k
            {
3158
1.07k
                if (tif->tif_flags & TIFF_SWAB)
3159
299
                    TIFFSwabLong(ma);
3160
1.07k
                maa = *ma++;
3161
1.07k
                if (tif->tif_flags & TIFF_SWAB)
3162
299
                    TIFFSwabLong(ma);
3163
1.07k
                mab = *ma++;
3164
1.07k
                if (mab == 0)
3165
280
                    *mb++ = 0.0;
3166
798
                else
3167
798
                    *mb++ = (double)maa / (double)mab;
3168
1.07k
            }
3169
95
        }
3170
95
        break;
3171
170
        case TIFF_SRATIONAL:
3172
170
        {
3173
170
            uint32_t *ma;
3174
170
            int32_t maa;
3175
170
            uint32_t mab;
3176
170
            double *mb;
3177
170
            uint32_t n;
3178
170
            ma = (uint32_t *)origdata;
3179
170
            mb = data;
3180
3.11k
            for (n = 0; n < count; n++)
3181
2.94k
            {
3182
2.94k
                if (tif->tif_flags & TIFF_SWAB)
3183
2.39k
                    TIFFSwabLong(ma);
3184
2.94k
                maa = *(int32_t *)ma;
3185
2.94k
                ma++;
3186
2.94k
                if (tif->tif_flags & TIFF_SWAB)
3187
2.39k
                    TIFFSwabLong(ma);
3188
2.94k
                mab = *ma++;
3189
2.94k
                if (mab == 0)
3190
1.04k
                    *mb++ = 0.0;
3191
1.89k
                else
3192
1.89k
                    *mb++ = (double)maa / (double)mab;
3193
2.94k
            }
3194
170
        }
3195
170
        break;
3196
92
        case TIFF_FLOAT:
3197
92
        {
3198
92
            float *ma;
3199
92
            double *mb;
3200
92
            uint32_t n;
3201
92
            if (tif->tif_flags & TIFF_SWAB)
3202
7
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
3203
92
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3204
92
            ma = (float *)origdata;
3205
92
            mb = data;
3206
351
            for (n = 0; n < count; n++)
3207
259
                *mb++ = (double)(*ma++);
3208
92
        }
3209
92
        break;
3210
1.41k
    }
3211
1.41k
    _TIFFfreeExt(tif, origdata);
3212
1.41k
    *value = data;
3213
1.41k
    return (TIFFReadDirEntryErrOk);
3214
1.41k
}
3215
3216
static enum TIFFReadDirEntryErr
3217
TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
3218
2.91k
{
3219
2.91k
    enum TIFFReadDirEntryErr err;
3220
2.91k
    uint32_t count;
3221
2.91k
    void *origdata;
3222
2.91k
    uint64_t *data;
3223
2.91k
    switch (direntry->tdir_type)
3224
2.91k
    {
3225
265
        case TIFF_LONG:
3226
276
        case TIFF_LONG8:
3227
1.86k
        case TIFF_IFD:
3228
2.79k
        case TIFF_IFD8:
3229
2.79k
            break;
3230
121
        default:
3231
121
            return (TIFFReadDirEntryErrType);
3232
2.91k
    }
3233
2.79k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3234
2.79k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3235
2.31k
    {
3236
2.31k
        *value = 0;
3237
2.31k
        return (err);
3238
2.31k
    }
3239
472
    switch (direntry->tdir_type)
3240
472
    {
3241
8
        case TIFF_LONG8:
3242
195
        case TIFF_IFD8:
3243
195
            *value = (uint64_t *)origdata;
3244
195
            if (tif->tif_flags & TIFF_SWAB)
3245
31
                TIFFSwabArrayOfLong8(*value, count);
3246
195
            return (TIFFReadDirEntryErrOk);
3247
472
    }
3248
277
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
3249
277
    if (data == 0)
3250
0
    {
3251
0
        _TIFFfreeExt(tif, origdata);
3252
0
        return (TIFFReadDirEntryErrAlloc);
3253
0
    }
3254
277
    switch (direntry->tdir_type)
3255
277
    {
3256
51
        case TIFF_LONG:
3257
277
        case TIFF_IFD:
3258
277
        {
3259
277
            uint32_t *ma;
3260
277
            uint64_t *mb;
3261
277
            uint32_t n;
3262
277
            ma = (uint32_t *)origdata;
3263
277
            mb = data;
3264
5.92k
            for (n = 0; n < count; n++)
3265
5.64k
            {
3266
5.64k
                if (tif->tif_flags & TIFF_SWAB)
3267
2.23k
                    TIFFSwabLong(ma);
3268
5.64k
                *mb++ = (uint64_t)(*ma++);
3269
5.64k
            }
3270
277
        }
3271
277
        break;
3272
277
    }
3273
277
    _TIFFfreeExt(tif, origdata);
3274
277
    *value = data;
3275
277
    return (TIFFReadDirEntryErrOk);
3276
277
}
3277
3278
static enum TIFFReadDirEntryErr
3279
TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
3280
                               uint16_t *value)
3281
22.4k
{
3282
22.4k
    enum TIFFReadDirEntryErr err;
3283
22.4k
    uint16_t *m;
3284
22.4k
    uint16_t *na;
3285
22.4k
    uint16_t nb;
3286
22.4k
    if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel)
3287
48
        return (TIFFReadDirEntryErrCount);
3288
22.3k
    err = TIFFReadDirEntryShortArray(tif, direntry, &m);
3289
22.3k
    if (err != TIFFReadDirEntryErrOk || m == NULL)
3290
2.22k
        return (err);
3291
20.1k
    na = m;
3292
20.1k
    nb = tif->tif_dir.td_samplesperpixel;
3293
20.1k
    *value = *na++;
3294
20.1k
    nb--;
3295
59.8k
    while (nb > 0)
3296
39.7k
    {
3297
39.7k
        if (*na++ != *value)
3298
43
        {
3299
43
            err = TIFFReadDirEntryErrPsdif;
3300
43
            break;
3301
43
        }
3302
39.7k
        nb--;
3303
39.7k
    }
3304
20.1k
    _TIFFfreeExt(tif, m);
3305
20.1k
    return (err);
3306
22.3k
}
3307
3308
static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
3309
                                        uint8_t *value)
3310
15.7k
{
3311
15.7k
    (void)tif;
3312
15.7k
    *value = *(uint8_t *)(&direntry->tdir_offset);
3313
15.7k
}
3314
3315
static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
3316
                                         int8_t *value)
3317
8.25k
{
3318
8.25k
    (void)tif;
3319
8.25k
    *value = *(int8_t *)(&direntry->tdir_offset);
3320
8.25k
}
3321
3322
static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
3323
                                         uint16_t *value)
3324
462k
{
3325
462k
    *value = direntry->tdir_offset.toff_short;
3326
    /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3327
462k
    if (tif->tif_flags & TIFF_SWAB)
3328
7.18k
        TIFFSwabShort(value);
3329
462k
}
3330
3331
static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
3332
                                          int16_t *value)
3333
6.54k
{
3334
6.54k
    *value = *(int16_t *)(&direntry->tdir_offset);
3335
6.54k
    if (tif->tif_flags & TIFF_SWAB)
3336
334
        TIFFSwabShort((uint16_t *)value);
3337
6.54k
}
3338
3339
static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
3340
                                        uint32_t *value)
3341
30.8k
{
3342
30.8k
    *value = *(uint32_t *)(&direntry->tdir_offset);
3343
30.8k
    if (tif->tif_flags & TIFF_SWAB)
3344
2.21k
        TIFFSwabLong(value);
3345
30.8k
}
3346
3347
static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
3348
                                         int32_t *value)
3349
3.53k
{
3350
3.53k
    *value = *(int32_t *)(&direntry->tdir_offset);
3351
3.53k
    if (tif->tif_flags & TIFF_SWAB)
3352
718
        TIFFSwabLong((uint32_t *)value);
3353
3.53k
}
3354
3355
static enum TIFFReadDirEntryErr
3356
TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
3357
1.29k
{
3358
1.29k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3359
1.22k
    {
3360
1.22k
        enum TIFFReadDirEntryErr err;
3361
1.22k
        uint32_t offset = direntry->tdir_offset.toff_long;
3362
1.22k
        if (tif->tif_flags & TIFF_SWAB)
3363
194
            TIFFSwabLong(&offset);
3364
1.22k
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3365
1.22k
        if (err != TIFFReadDirEntryErrOk)
3366
270
            return (err);
3367
1.22k
    }
3368
67
    else
3369
67
        *value = direntry->tdir_offset.toff_long8;
3370
1.02k
    if (tif->tif_flags & TIFF_SWAB)
3371
132
        TIFFSwabLong8(value);
3372
1.02k
    return (TIFFReadDirEntryErrOk);
3373
1.29k
}
3374
3375
static enum TIFFReadDirEntryErr
3376
TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
3377
2.32k
{
3378
2.32k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3379
2.27k
    {
3380
2.27k
        enum TIFFReadDirEntryErr err;
3381
2.27k
        uint32_t offset = direntry->tdir_offset.toff_long;
3382
2.27k
        if (tif->tif_flags & TIFF_SWAB)
3383
140
            TIFFSwabLong(&offset);
3384
2.27k
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3385
2.27k
        if (err != TIFFReadDirEntryErrOk)
3386
283
            return (err);
3387
2.27k
    }
3388
50
    else
3389
50
        *value = *(int64_t *)(&direntry->tdir_offset);
3390
2.03k
    if (tif->tif_flags & TIFF_SWAB)
3391
118
        TIFFSwabLong8((uint64_t *)value);
3392
2.03k
    return (TIFFReadDirEntryErrOk);
3393
2.32k
}
3394
3395
static enum TIFFReadDirEntryErr
3396
TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
3397
                                double *value)
3398
10.4k
{
3399
10.4k
    UInt64Aligned_t m;
3400
3401
10.4k
    assert(sizeof(double) == 8);
3402
10.4k
    assert(sizeof(uint64_t) == 8);
3403
10.4k
    assert(sizeof(uint32_t) == 4);
3404
10.4k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3405
10.4k
    {
3406
10.4k
        enum TIFFReadDirEntryErr err;
3407
10.4k
        uint32_t offset = direntry->tdir_offset.toff_long;
3408
10.4k
        if (tif->tif_flags & TIFF_SWAB)
3409
451
            TIFFSwabLong(&offset);
3410
10.4k
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3411
10.4k
        if (err != TIFFReadDirEntryErrOk)
3412
725
            return (err);
3413
10.4k
    }
3414
10
    else
3415
10
        m.l = direntry->tdir_offset.toff_long8;
3416
9.71k
    if (tif->tif_flags & TIFF_SWAB)
3417
285
        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
9.71k
    if (m.i[0] == 0 || m.i[1] == 0)
3422
249
        *value = 0.0;
3423
9.46k
    else
3424
9.46k
        *value = (double)m.i[0] / (double)m.i[1];
3425
9.71k
    return (TIFFReadDirEntryErrOk);
3426
10.4k
}
3427
3428
static enum TIFFReadDirEntryErr
3429
TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
3430
                                 double *value)
3431
765
{
3432
765
    UInt64Aligned_t m;
3433
765
    assert(sizeof(double) == 8);
3434
765
    assert(sizeof(uint64_t) == 8);
3435
765
    assert(sizeof(int32_t) == 4);
3436
765
    assert(sizeof(uint32_t) == 4);
3437
765
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3438
762
    {
3439
762
        enum TIFFReadDirEntryErr err;
3440
762
        uint32_t offset = direntry->tdir_offset.toff_long;
3441
762
        if (tif->tif_flags & TIFF_SWAB)
3442
31
            TIFFSwabLong(&offset);
3443
762
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3444
762
        if (err != TIFFReadDirEntryErrOk)
3445
231
            return (err);
3446
762
    }
3447
3
    else
3448
3
        m.l = direntry->tdir_offset.toff_long8;
3449
534
    if (tif->tif_flags & TIFF_SWAB)
3450
14
        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
534
    if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
3455
111
        *value = 0.0;
3456
423
    else
3457
423
        *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
3458
534
    return (TIFFReadDirEntryErrOk);
3459
765
}
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
90
{
3507
90
    union
3508
90
    {
3509
90
        float f;
3510
90
        uint32_t i;
3511
90
    } float_union;
3512
90
    assert(sizeof(float) == 4);
3513
90
    assert(sizeof(uint32_t) == 4);
3514
90
    assert(sizeof(float_union) == 4);
3515
90
    float_union.i = *(uint32_t *)(&direntry->tdir_offset);
3516
90
    *value = float_union.f;
3517
90
    if (tif->tif_flags & TIFF_SWAB)
3518
42
        TIFFSwabLong((uint32_t *)value);
3519
90
}
3520
3521
static enum TIFFReadDirEntryErr
3522
TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
3523
348
{
3524
348
    assert(sizeof(double) == 8);
3525
348
    assert(sizeof(uint64_t) == 8);
3526
348
    assert(sizeof(UInt64Aligned_t) == 8);
3527
348
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3528
316
    {
3529
316
        enum TIFFReadDirEntryErr err;
3530
316
        uint32_t offset = direntry->tdir_offset.toff_long;
3531
316
        if (tif->tif_flags & TIFF_SWAB)
3532
137
            TIFFSwabLong(&offset);
3533
316
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3534
316
        if (err != TIFFReadDirEntryErrOk)
3535
199
            return (err);
3536
316
    }
3537
32
    else
3538
32
    {
3539
32
        UInt64Aligned_t uint64_union;
3540
32
        uint64_union.l = direntry->tdir_offset.toff_long8;
3541
32
        *value = uint64_union.d;
3542
32
    }
3543
149
    if (tif->tif_flags & TIFF_SWAB)
3544
100
        TIFFSwabLong8((uint64_t *)value);
3545
149
    return (TIFFReadDirEntryErrOk);
3546
348
}
3547
3548
static enum TIFFReadDirEntryErr
3549
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
3550
1.62k
{
3551
1.62k
    if (value < 0)
3552
80
        return (TIFFReadDirEntryErrRange);
3553
1.54k
    else
3554
1.54k
        return (TIFFReadDirEntryErrOk);
3555
1.62k
}
3556
3557
static enum TIFFReadDirEntryErr
3558
TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
3559
4.53k
{
3560
4.53k
    if (value > 0xFF)
3561
511
        return (TIFFReadDirEntryErrRange);
3562
4.02k
    else
3563
4.02k
        return (TIFFReadDirEntryErrOk);
3564
4.53k
}
3565
3566
static enum TIFFReadDirEntryErr
3567
TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
3568
1.08k
{
3569
1.08k
    if ((value < 0) || (value > 0xFF))
3570
115
        return (TIFFReadDirEntryErrRange);
3571
973
    else
3572
973
        return (TIFFReadDirEntryErrOk);
3573
1.08k
}
3574
3575
static enum TIFFReadDirEntryErr
3576
TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
3577
3.05k
{
3578
3.05k
    if (value > 0xFF)
3579
812
        return (TIFFReadDirEntryErrRange);
3580
2.24k
    else
3581
2.24k
        return (TIFFReadDirEntryErrOk);
3582
3.05k
}
3583
3584
static enum TIFFReadDirEntryErr
3585
TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
3586
1.27k
{
3587
1.27k
    if ((value < 0) || (value > 0xFF))
3588
299
        return (TIFFReadDirEntryErrRange);
3589
980
    else
3590
980
        return (TIFFReadDirEntryErrOk);
3591
1.27k
}
3592
3593
static enum TIFFReadDirEntryErr
3594
TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
3595
1.30k
{
3596
1.30k
    if (value > 0xFF)
3597
405
        return (TIFFReadDirEntryErrRange);
3598
903
    else
3599
903
        return (TIFFReadDirEntryErrOk);
3600
1.30k
}
3601
3602
static enum TIFFReadDirEntryErr
3603
TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
3604
2.14k
{
3605
2.14k
    if ((value < 0) || (value > 0xFF))
3606
777
        return (TIFFReadDirEntryErrRange);
3607
1.36k
    else
3608
1.36k
        return (TIFFReadDirEntryErrOk);
3609
2.14k
}
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
8.10k
{
3677
8.10k
    if (value < 0)
3678
38
        return (TIFFReadDirEntryErrRange);
3679
8.07k
    else
3680
8.07k
        return (TIFFReadDirEntryErrOk);
3681
8.10k
}
3682
3683
static enum TIFFReadDirEntryErr
3684
TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
3685
6.31k
{
3686
6.31k
    if (value < 0)
3687
99
        return (TIFFReadDirEntryErrRange);
3688
6.22k
    else
3689
6.22k
        return (TIFFReadDirEntryErrOk);
3690
6.31k
}
3691
3692
static enum TIFFReadDirEntryErr
3693
TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
3694
10.4k
{
3695
10.4k
    if (value > 0xFFFF)
3696
245
        return (TIFFReadDirEntryErrRange);
3697
10.2k
    else
3698
10.2k
        return (TIFFReadDirEntryErrOk);
3699
10.4k
}
3700
3701
static enum TIFFReadDirEntryErr
3702
TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
3703
2.15k
{
3704
2.15k
    if ((value < 0) || (value > 0xFFFF))
3705
391
        return (TIFFReadDirEntryErrRange);
3706
1.75k
    else
3707
1.75k
        return (TIFFReadDirEntryErrOk);
3708
2.15k
}
3709
3710
static enum TIFFReadDirEntryErr
3711
TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
3712
2.02k
{
3713
2.02k
    if (value > 0xFFFF)
3714
519
        return (TIFFReadDirEntryErrRange);
3715
1.51k
    else
3716
1.51k
        return (TIFFReadDirEntryErrOk);
3717
2.02k
}
3718
3719
static enum TIFFReadDirEntryErr
3720
TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
3721
2.68k
{
3722
2.68k
    if ((value < 0) || (value > 0xFFFF))
3723
974
        return (TIFFReadDirEntryErrRange);
3724
1.71k
    else
3725
1.71k
        return (TIFFReadDirEntryErrOk);
3726
2.68k
}
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
7.36k
{
3776
7.36k
    if (value < 0)
3777
59
        return (TIFFReadDirEntryErrRange);
3778
7.30k
    else
3779
7.30k
        return (TIFFReadDirEntryErrOk);
3780
7.36k
}
3781
3782
static enum TIFFReadDirEntryErr
3783
TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
3784
4.60k
{
3785
4.60k
    if (value < 0)
3786
65
        return (TIFFReadDirEntryErrRange);
3787
4.53k
    else
3788
4.53k
        return (TIFFReadDirEntryErrOk);
3789
4.60k
}
3790
3791
static enum TIFFReadDirEntryErr
3792
TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
3793
4.13k
{
3794
4.13k
    if (value < 0)
3795
172
        return (TIFFReadDirEntryErrRange);
3796
3.96k
    else
3797
3.96k
        return (TIFFReadDirEntryErrOk);
3798
4.13k
}
3799
3800
static enum TIFFReadDirEntryErr
3801
TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
3802
1.83k
{
3803
1.83k
    if (value > UINT32_MAX)
3804
495
        return (TIFFReadDirEntryErrRange);
3805
1.34k
    else
3806
1.34k
        return (TIFFReadDirEntryErrOk);
3807
1.83k
}
3808
3809
static enum TIFFReadDirEntryErr
3810
TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
3811
39.4k
{
3812
39.4k
    if ((value < 0) || (value > (int64_t)UINT32_MAX))
3813
868
        return (TIFFReadDirEntryErrRange);
3814
38.5k
    else
3815
38.5k
        return (TIFFReadDirEntryErrOk);
3816
39.4k
}
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
2.71k
{
3850
2.71k
    if (value < 0)
3851
14
        return (TIFFReadDirEntryErrRange);
3852
2.70k
    else
3853
2.70k
        return (TIFFReadDirEntryErrOk);
3854
2.71k
}
3855
3856
static enum TIFFReadDirEntryErr
3857
TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
3858
3.90k
{
3859
3.90k
    if (value < 0)
3860
67
        return (TIFFReadDirEntryErrRange);
3861
3.83k
    else
3862
3.83k
        return (TIFFReadDirEntryErrOk);
3863
3.90k
}
3864
3865
static enum TIFFReadDirEntryErr
3866
TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
3867
1.50k
{
3868
1.50k
    if (value < 0)
3869
54
        return (TIFFReadDirEntryErrRange);
3870
1.45k
    else
3871
1.45k
        return (TIFFReadDirEntryErrOk);
3872
1.50k
}
3873
3874
static enum TIFFReadDirEntryErr
3875
TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
3876
2.98k
{
3877
2.98k
    if (value < 0)
3878
191
        return (TIFFReadDirEntryErrRange);
3879
2.79k
    else
3880
2.79k
        return (TIFFReadDirEntryErrOk);
3881
2.98k
}
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
197k
{
3895
197k
    assert(size > 0);
3896
197k
    if (!isMapped(tif))
3897
4.87k
    {
3898
4.87k
        if (!SeekOK(tif, offset))
3899
0
            return (TIFFReadDirEntryErrIo);
3900
4.87k
        if (!ReadOK(tif, dest, size))
3901
94
            return (TIFFReadDirEntryErrIo);
3902
4.87k
    }
3903
192k
    else
3904
192k
    {
3905
192k
        size_t ma, mb;
3906
192k
        ma = (size_t)offset;
3907
192k
        if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
3908
10
        {
3909
10
            return TIFFReadDirEntryErrIo;
3910
10
        }
3911
192k
        mb = ma + size;
3912
192k
        if (mb > (uint64_t)tif->tif_size)
3913
40.5k
            return (TIFFReadDirEntryErrIo);
3914
151k
        _TIFFmemcpy(dest, tif->tif_base + ma, size);
3915
151k
    }
3916
156k
    return (TIFFReadDirEntryErrOk);
3917
197k
}
3918
3919
static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
3920
                                      const char *module, const char *tagname,
3921
                                      int recover)
3922
245k
{
3923
245k
    if (!recover)
3924
9.28k
    {
3925
9.28k
        switch (err)
3926
9.28k
        {
3927
1.28k
            case TIFFReadDirEntryErrCount:
3928
1.28k
                TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
3929
1.28k
                              tagname);
3930
1.28k
                break;
3931
1.77k
            case TIFFReadDirEntryErrType:
3932
1.77k
                TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
3933
1.77k
                              tagname);
3934
1.77k
                break;
3935
2.35k
            case TIFFReadDirEntryErrIo:
3936
2.35k
                TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
3937
2.35k
                              tagname);
3938
2.35k
                break;
3939
2.95k
            case TIFFReadDirEntryErrRange:
3940
2.95k
                TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
3941
2.95k
                              tagname);
3942
2.95k
                break;
3943
43
            case TIFFReadDirEntryErrPsdif:
3944
43
                TIFFErrorExtR(
3945
43
                    tif, module,
3946
43
                    "Cannot handle different values per sample for \"%s\"",
3947
43
                    tagname);
3948
43
                break;
3949
776
            case TIFFReadDirEntryErrSizesan:
3950
776
                TIFFErrorExtR(tif, module,
3951
776
                              "Sanity check on size of \"%s\" value failed",
3952
776
                              tagname);
3953
776
                break;
3954
108
            case TIFFReadDirEntryErrAlloc:
3955
108
                TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
3956
108
                              tagname);
3957
108
                break;
3958
0
            default:
3959
0
                assert(0); /* we should never get here */
3960
0
                break;
3961
9.28k
        }
3962
9.28k
    }
3963
236k
    else
3964
236k
    {
3965
236k
        switch (err)
3966
236k
        {
3967
30.4k
            case TIFFReadDirEntryErrCount:
3968
30.4k
                TIFFWarningExtR(tif, module,
3969
30.4k
                                "Incorrect count for \"%s\"; tag ignored",
3970
30.4k
                                tagname);
3971
30.4k
                break;
3972
9.82k
            case TIFFReadDirEntryErrType:
3973
9.82k
                TIFFWarningExtR(tif, module,
3974
9.82k
                                "Incompatible type for \"%s\"; tag ignored",
3975
9.82k
                                tagname);
3976
9.82k
                break;
3977
126k
            case TIFFReadDirEntryErrIo:
3978
126k
                TIFFWarningExtR(
3979
126k
                    tif, module,
3980
126k
                    "IO error during reading of \"%s\"; tag ignored", tagname);
3981
126k
                break;
3982
4.37k
            case TIFFReadDirEntryErrRange:
3983
4.37k
                TIFFWarningExtR(tif, module,
3984
4.37k
                                "Incorrect value for \"%s\"; tag ignored",
3985
4.37k
                                tagname);
3986
4.37k
                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
42.8k
            case TIFFReadDirEntryErrSizesan:
3994
42.8k
                TIFFWarningExtR(
3995
42.8k
                    tif, module,
3996
42.8k
                    "Sanity check on size of \"%s\" value failed; tag ignored",
3997
42.8k
                    tagname);
3998
42.8k
                break;
3999
22.7k
            case TIFFReadDirEntryErrAlloc:
4000
22.7k
                TIFFWarningExtR(tif, module,
4001
22.7k
                                "Out of memory reading of \"%s\"; tag ignored",
4002
22.7k
                                tagname);
4003
22.7k
                break;
4004
0
            default:
4005
0
                assert(0); /* we should never get here */
4006
0
                break;
4007
236k
        }
4008
236k
    }
4009
245k
}
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
76.9k
{
4018
76.9k
    switch (photometric)
4019
76.9k
    {
4020
2.19k
        case PHOTOMETRIC_PALETTE:
4021
21.6k
        case PHOTOMETRIC_MINISWHITE:
4022
38.7k
        case PHOTOMETRIC_MINISBLACK:
4023
38.7k
            return 1;
4024
12.2k
        case PHOTOMETRIC_YCBCR:
4025
24.9k
        case PHOTOMETRIC_RGB:
4026
30.6k
        case PHOTOMETRIC_CIELAB:
4027
31.2k
        case PHOTOMETRIC_LOGLUV:
4028
31.2k
        case PHOTOMETRIC_ITULAB:
4029
31.2k
        case PHOTOMETRIC_ICCLAB:
4030
31.2k
            return 3;
4031
6.30k
        case PHOTOMETRIC_SEPARATED:
4032
6.31k
        case PHOTOMETRIC_MASK:
4033
6.31k
            return 4;
4034
235
        case PHOTOMETRIC_LOGL:
4035
235
        case PHOTOMETRIC_CFA:
4036
637
        default:
4037
637
            return 0;
4038
76.9k
    }
4039
76.9k
}
4040
4041
static int ByteCountLooksBad(TIFF *tif)
4042
10.2k
{
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
10.2k
    uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
4057
10.2k
    uint64_t offset = TIFFGetStrileOffset(tif, 0);
4058
10.2k
    uint64_t filesize;
4059
4060
10.2k
    if (offset == 0)
4061
820
        return 0;
4062
9.39k
    if (bytecount == 0)
4063
1.18k
        return 1;
4064
8.21k
    if (tif->tif_dir.td_compression != COMPRESSION_NONE)
4065
6.40k
        return 0;
4066
1.80k
    filesize = TIFFGetFileSize(tif);
4067
1.80k
    if (offset <= filesize && bytecount > filesize - offset)
4068
355
        return 1;
4069
1.44k
    if (tif->tif_mode == O_RDONLY)
4070
1.44k
    {
4071
1.44k
        uint64_t scanlinesize = TIFFScanlineSize64(tif);
4072
1.44k
        if (tif->tif_dir.td_imagelength > 0 &&
4073
1.44k
            scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
4074
3
        {
4075
3
            return 1;
4076
3
        }
4077
1.44k
        if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
4078
168
            return 1;
4079
1.44k
    }
4080
1.27k
    return 0;
4081
1.44k
}
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
326k
{
4089
326k
    const uint64_t data_width = TIFFDataWidth(dp->tdir_type);
4090
326k
    if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
4091
106
    {
4092
106
        TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4093
106
                      "Too large IFD data size");
4094
106
        return false;
4095
106
    }
4096
326k
    const uint64_t datalength = dp->tdir_count * data_width;
4097
326k
    if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
4098
154k
    {
4099
154k
        if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
4100
3
        {
4101
3
            TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4102
3
                          "Too large IFD data size");
4103
3
            return false;
4104
3
        }
4105
154k
        tif->tif_dir.td_dirdatasize_read += datalength;
4106
154k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
4107
153k
        {
4108
            /* The offset of TIFFDirEntry are not swapped when read in. That has
4109
             * to be done when used. */
4110
153k
            uint32_t offset = dp->tdir_offset.toff_long;
4111
153k
            if (tif->tif_flags & TIFF_SWAB)
4112
4.42k
                TIFFSwabLong(&offset);
4113
153k
            tif->tif_dir
4114
153k
                .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4115
153k
                .offset = (uint64_t)offset;
4116
153k
        }
4117
1.03k
        else
4118
1.03k
        {
4119
1.03k
            tif->tif_dir
4120
1.03k
                .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4121
1.03k
                .offset = dp->tdir_offset.toff_long8;
4122
1.03k
            if (tif->tif_flags & TIFF_SWAB)
4123
116
                TIFFSwabLong8(
4124
116
                    &tif->tif_dir
4125
116
                         .td_dirdatasize_offsets[tif->tif_dir
4126
116
                                                     .td_dirdatasize_Noffsets]
4127
116
                         .offset);
4128
1.03k
        }
4129
154k
        tif->tif_dir
4130
154k
            .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4131
154k
            .length = datalength;
4132
154k
        tif->tif_dir.td_dirdatasize_Noffsets++;
4133
154k
    }
4134
326k
    return true;
4135
326k
}
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
88.5k
{
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
88.5k
    if (tif->tif_mode == O_RDONLY)
4166
88.5k
        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
101k
{
4238
101k
    static const char module[] = "TIFFReadDirectory";
4239
101k
    TIFFDirEntry *dir;
4240
101k
    uint16_t dircount;
4241
101k
    TIFFDirEntry *dp;
4242
101k
    uint16_t di;
4243
101k
    const TIFFField *fip;
4244
101k
    uint32_t fii = FAILED_FII;
4245
101k
    toff_t nextdiroff;
4246
101k
    int bitspersample_read = FALSE;
4247
101k
    int color_channels;
4248
4249
101k
    if (tif->tif_nextdiroff == 0)
4250
1.86k
    {
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
1.86k
        tif->tif_diroff = tif->tif_nextdiroff;
4255
1.86k
        return 0;
4256
1.86k
    }
4257
4258
100k
    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
100k
    if (!_TIFFCheckDirNumberAndOffset(tif,
4263
100k
                                      tif->tif_curdir ==
4264
100k
                                              TIFF_NON_EXISTENT_DIR_NUMBER
4265
100k
                                          ? 0
4266
100k
                                          : tif->tif_curdir + 1,
4267
100k
                                      nextdiroff))
4268
0
    {
4269
0
        return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
4270
                     IFDs) */
4271
0
    }
4272
100k
    dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
4273
100k
    if (!dircount)
4274
2.45k
    {
4275
2.45k
        TIFFErrorExtR(tif, module,
4276
2.45k
                      "Failed to read directory at offset %" PRIu64,
4277
2.45k
                      nextdiroff);
4278
2.45k
        return 0;
4279
2.45k
    }
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
97.6k
    if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
4284
97.6k
        tif->tif_curdir = 0;
4285
0
    else
4286
0
        tif->tif_curdir++;
4287
4288
97.6k
    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
97.6k
    {
4295
97.6k
        TIFFDirEntry *ma;
4296
97.6k
        uint16_t mb;
4297
2.20M
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
4298
2.10M
        {
4299
2.10M
            TIFFDirEntry *na;
4300
2.10M
            uint16_t nb;
4301
1.33G
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
4302
1.33G
            {
4303
1.33G
                if (ma->tdir_tag == na->tdir_tag)
4304
120M
                {
4305
120M
                    na->tdir_ignore = TRUE;
4306
120M
                }
4307
1.33G
            }
4308
2.10M
        }
4309
97.6k
    }
4310
4311
97.6k
    tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
4312
97.6k
    tif->tif_flags &= ~TIFF_BUF4WRITE;   /* reset before new dir */
4313
97.6k
    tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
4314
4315
    /* free any old stuff and reinit */
4316
97.6k
    TIFFFreeDirectory(tif);
4317
97.6k
    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
97.6k
    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
97.6k
    tif->tif_dir.td_dirdatasize_offsets =
4327
97.6k
        (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
4328
97.6k
            tif, dircount * sizeof(TIFFEntryOffsetAndLength));
4329
97.6k
    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
97.6k
    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
97.6k
    dp =
4363
97.6k
        TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
4364
97.6k
    if (dp)
4365
51.0k
    {
4366
51.0k
        if (!TIFFFetchNormalTag(tif, dp, 0))
4367
83
            goto bad;
4368
50.9k
        dp->tdir_ignore = TRUE;
4369
50.9k
    }
4370
97.5k
    dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
4371
97.5k
    if (dp)
4372
68.3k
    {
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
68.3k
        uint16_t value;
4380
68.3k
        enum TIFFReadDirEntryErr err;
4381
68.3k
        err = TIFFReadDirEntryShort(tif, dp, &value);
4382
68.3k
        if (err == TIFFReadDirEntryErrCount)
4383
4.52k
            err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
4384
68.3k
        if (err != TIFFReadDirEntryErrOk)
4385
2.41k
        {
4386
2.41k
            TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
4387
2.41k
            goto bad;
4388
2.41k
        }
4389
65.9k
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
4390
0
            goto bad;
4391
65.9k
        dp->tdir_ignore = TRUE;
4392
65.9k
    }
4393
29.2k
    else
4394
29.2k
    {
4395
29.2k
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
4396
0
            goto bad;
4397
29.2k
    }
4398
    /*
4399
     * First real pass over the directory.
4400
     */
4401
2.17M
    for (di = 0, dp = dir; di < dircount; di++, dp++)
4402
2.08M
    {
4403
2.08M
        if (!dp->tdir_ignore)
4404
1.22M
        {
4405
1.22M
            TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4406
1.22M
            if (fii == FAILED_FII)
4407
592k
            {
4408
592k
                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
592k
                const TIFFField *fld = _TIFFCreateAnonField(
4418
592k
                    tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
4419
592k
                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
592k
                else
4429
592k
                {
4430
592k
                    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4431
592k
                    assert(fii != FAILED_FII);
4432
592k
                }
4433
592k
            }
4434
1.22M
        }
4435
2.08M
        if (!dp->tdir_ignore)
4436
1.22M
        {
4437
1.22M
            fip = tif->tif_fields[fii];
4438
1.22M
            if (fip->field_bit == FIELD_IGNORE)
4439
4.48k
                dp->tdir_ignore = TRUE;
4440
1.22M
            else
4441
1.22M
            {
4442
1.22M
                switch (dp->tdir_tag)
4443
1.22M
                {
4444
69.3k
                    case TIFFTAG_STRIPOFFSETS:
4445
93.3k
                    case TIFFTAG_STRIPBYTECOUNTS:
4446
105k
                    case TIFFTAG_TILEOFFSETS:
4447
106k
                    case TIFFTAG_TILEBYTECOUNTS:
4448
106k
                        TIFFSetFieldBit(tif, fip->field_bit);
4449
106k
                        break;
4450
85.5k
                    case TIFFTAG_IMAGEWIDTH:
4451
166k
                    case TIFFTAG_IMAGELENGTH:
4452
166k
                    case TIFFTAG_IMAGEDEPTH:
4453
178k
                    case TIFFTAG_TILELENGTH:
4454
191k
                    case TIFFTAG_TILEWIDTH:
4455
191k
                    case TIFFTAG_TILEDEPTH:
4456
218k
                    case TIFFTAG_PLANARCONFIG:
4457
254k
                    case TIFFTAG_ROWSPERSTRIP:
4458
264k
                    case TIFFTAG_EXTRASAMPLES:
4459
264k
                        if (!TIFFFetchNormalTag(tif, dp, 0))
4460
2.73k
                            goto bad;
4461
261k
                        dp->tdir_ignore = TRUE;
4462
261k
                        break;
4463
853k
                    default:
4464
853k
                        if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
4465
11.5k
                            dp->tdir_ignore = TRUE;
4466
853k
                        break;
4467
1.22M
                }
4468
1.22M
            }
4469
1.22M
        }
4470
2.08M
    }
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
92.4k
    if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4480
92.4k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
4481
91
    {
4482
91
        if (!_TIFFFillStriles(tif))
4483
0
            goto bad;
4484
91
        dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4485
91
                                        TIFFTAG_STRIPOFFSETS);
4486
91
        if ((dp != 0) && (dp->tdir_count == 1))
4487
40
        {
4488
40
            dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4489
40
                                            TIFFTAG_STRIPBYTECOUNTS);
4490
40
            if ((dp != 0) && (dp->tdir_count == 1))
4491
4
            {
4492
4
                tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
4493
4
                TIFFWarningExtR(tif, module,
4494
4
                                "Planarconfig tag value assumed incorrect, "
4495
4
                                "assuming data is contig instead of chunky");
4496
4
            }
4497
40
        }
4498
91
    }
4499
    /*
4500
     * Allocate directory structure and setup defaults.
4501
     */
4502
92.4k
    if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
4503
2.69k
    {
4504
2.69k
        MissingRequired(tif, "ImageLength");
4505
2.69k
        goto bad;
4506
2.69k
    }
4507
4508
    /*
4509
     * Second pass: extract other information.
4510
     */
4511
2.10M
    for (di = 0, dp = dir; di < dircount; di++, dp++)
4512
2.01M
    {
4513
2.01M
        if (!dp->tdir_ignore)
4514
927k
        {
4515
927k
            switch (dp->tdir_tag)
4516
927k
            {
4517
382
                case TIFFTAG_MINSAMPLEVALUE:
4518
1.52k
                case TIFFTAG_MAXSAMPLEVALUE:
4519
51.9k
                case TIFFTAG_BITSPERSAMPLE:
4520
51.9k
                case TIFFTAG_DATATYPE:
4521
81.8k
                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
81.8k
                    {
4533
81.8k
                        uint16_t value;
4534
81.8k
                        enum TIFFReadDirEntryErr err;
4535
81.8k
                        err = TIFFReadDirEntryShort(tif, dp, &value);
4536
81.8k
                        if (!EvaluateIFDdatasizeReading(tif, dp))
4537
5
                            goto bad;
4538
81.7k
                        if (err == TIFFReadDirEntryErrCount)
4539
17.9k
                            err =
4540
17.9k
                                TIFFReadDirEntryPersampleShort(tif, dp, &value);
4541
81.7k
                        if (err != TIFFReadDirEntryErrOk)
4542
716
                        {
4543
716
                            fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4544
716
                            TIFFReadDirEntryOutputErr(
4545
716
                                tif, err, module,
4546
716
                                fip ? fip->field_name : "unknown tagname", 0);
4547
716
                            goto bad;
4548
716
                        }
4549
81.0k
                        if (!TIFFSetField(tif, dp->tdir_tag, value))
4550
31
                            goto bad;
4551
81.0k
                        if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
4552
49.8k
                            bitspersample_read = TRUE;
4553
81.0k
                    }
4554
0
                    break;
4555
786
                case TIFFTAG_SMINSAMPLEVALUE:
4556
1.75k
                case TIFFTAG_SMAXSAMPLEVALUE:
4557
1.75k
                {
4558
4559
1.75k
                    double *data = NULL;
4560
1.75k
                    enum TIFFReadDirEntryErr err;
4561
1.75k
                    uint32_t saved_flags;
4562
1.75k
                    int m;
4563
1.75k
                    if (dp->tdir_count !=
4564
1.75k
                        (uint64_t)tif->tif_dir.td_samplesperpixel)
4565
321
                        err = TIFFReadDirEntryErrCount;
4566
1.42k
                    else
4567
1.42k
                        err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
4568
1.75k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4569
28
                        goto bad;
4570
1.72k
                    if (err != TIFFReadDirEntryErrOk)
4571
303
                    {
4572
303
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4573
303
                        TIFFReadDirEntryOutputErr(
4574
303
                            tif, err, module,
4575
303
                            fip ? fip->field_name : "unknown tagname", 0);
4576
303
                        goto bad;
4577
303
                    }
4578
1.41k
                    saved_flags = tif->tif_flags;
4579
1.41k
                    tif->tif_flags |= TIFF_PERSAMPLE;
4580
1.41k
                    m = TIFFSetField(tif, dp->tdir_tag, data);
4581
1.41k
                    tif->tif_flags = saved_flags;
4582
1.41k
                    _TIFFfreeExt(tif, data);
4583
1.41k
                    if (!m)
4584
0
                        goto bad;
4585
1.41k
                }
4586
1.41k
                break;
4587
69.1k
                case TIFFTAG_STRIPOFFSETS:
4588
80.9k
                case TIFFTAG_TILEOFFSETS:
4589
80.9k
                {
4590
80.9k
                    switch (dp->tdir_type)
4591
80.9k
                    {
4592
7.73k
                        case TIFF_SHORT:
4593
49.1k
                        case TIFF_LONG:
4594
52.7k
                        case TIFF_LONG8:
4595
52.7k
                            break;
4596
28.1k
                        default:
4597
                            /* Warn except if directory typically created with
4598
                             * TIFFDeferStrileArrayWriting() */
4599
28.1k
                            if (!(tif->tif_mode == O_RDWR &&
4600
28.1k
                                  dp->tdir_count == 0 && dp->tdir_type == 0 &&
4601
28.1k
                                  dp->tdir_offset.toff_long8 == 0))
4602
28.1k
                            {
4603
28.1k
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4604
28.1k
                                TIFFWarningExtR(
4605
28.1k
                                    tif, module, "Invalid data type for tag %s",
4606
28.1k
                                    fip ? fip->field_name : "unknown tagname");
4607
28.1k
                            }
4608
28.1k
                            break;
4609
80.9k
                    }
4610
80.9k
                    _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
4611
80.9k
                                sizeof(TIFFDirEntry));
4612
80.9k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4613
69
                        goto bad;
4614
80.9k
                }
4615
80.8k
                break;
4616
80.8k
                case TIFFTAG_STRIPBYTECOUNTS:
4617
25.6k
                case TIFFTAG_TILEBYTECOUNTS:
4618
25.6k
                {
4619
25.6k
                    switch (dp->tdir_type)
4620
25.6k
                    {
4621
379
                        case TIFF_SHORT:
4622
15.6k
                        case TIFF_LONG:
4623
19.6k
                        case TIFF_LONG8:
4624
19.6k
                            break;
4625
6.05k
                        default:
4626
                            /* Warn except if directory typically created with
4627
                             * TIFFDeferStrileArrayWriting() */
4628
6.05k
                            if (!(tif->tif_mode == O_RDWR &&
4629
6.05k
                                  dp->tdir_count == 0 && dp->tdir_type == 0 &&
4630
6.05k
                                  dp->tdir_offset.toff_long8 == 0))
4631
6.05k
                            {
4632
6.05k
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4633
6.05k
                                TIFFWarningExtR(
4634
6.05k
                                    tif, module, "Invalid data type for tag %s",
4635
6.05k
                                    fip ? fip->field_name : "unknown tagname");
4636
6.05k
                            }
4637
6.05k
                            break;
4638
25.6k
                    }
4639
25.6k
                    _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
4640
25.6k
                                sizeof(TIFFDirEntry));
4641
25.6k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4642
4
                        goto bad;
4643
25.6k
                }
4644
25.6k
                break;
4645
25.6k
                case TIFFTAG_COLORMAP:
4646
4.60k
                case TIFFTAG_TRANSFERFUNCTION:
4647
4.60k
                {
4648
4.60k
                    enum TIFFReadDirEntryErr err;
4649
4.60k
                    uint32_t countpersample;
4650
4.60k
                    uint32_t countrequired;
4651
4.60k
                    uint32_t incrementpersample;
4652
4.60k
                    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
4.60k
                    if (!bitspersample_read)
4661
1.85k
                    {
4662
1.85k
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4663
1.85k
                        TIFFWarningExtR(
4664
1.85k
                            tif, module,
4665
1.85k
                            "Ignoring %s since BitsPerSample tag not found",
4666
1.85k
                            fip ? fip->field_name : "unknown tagname");
4667
1.85k
                        continue;
4668
1.85k
                    }
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
2.74k
                    if (tif->tif_dir.td_bitspersample > 24)
4673
51
                    {
4674
51
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4675
51
                        TIFFWarningExtR(
4676
51
                            tif, module,
4677
51
                            "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
4678
51
                            fip ? fip->field_name : "unknown tagname",
4679
51
                            tif->tif_dir.td_bitspersample);
4680
51
                        continue;
4681
51
                    }
4682
2.69k
                    countpersample = (1U << tif->tif_dir.td_bitspersample);
4683
2.69k
                    if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
4684
2.69k
                        (dp->tdir_count == (uint64_t)countpersample))
4685
46
                    {
4686
46
                        countrequired = countpersample;
4687
46
                        incrementpersample = 0;
4688
46
                    }
4689
2.64k
                    else
4690
2.64k
                    {
4691
2.64k
                        countrequired = 3 * countpersample;
4692
2.64k
                        incrementpersample = countpersample;
4693
2.64k
                    }
4694
2.69k
                    if (dp->tdir_count != (uint64_t)countrequired)
4695
867
                        err = TIFFReadDirEntryErrCount;
4696
1.82k
                    else
4697
1.82k
                        err = TIFFReadDirEntryShortArray(tif, dp, &value);
4698
2.69k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4699
3
                        goto bad;
4700
2.69k
                    if (err != TIFFReadDirEntryErrOk)
4701
1.01k
                    {
4702
1.01k
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4703
1.01k
                        TIFFReadDirEntryOutputErr(
4704
1.01k
                            tif, err, module,
4705
1.01k
                            fip ? fip->field_name : "unknown tagname", 1);
4706
1.01k
                    }
4707
1.67k
                    else
4708
1.67k
                    {
4709
1.67k
                        TIFFSetField(tif, dp->tdir_tag, value,
4710
1.67k
                                     value + incrementpersample,
4711
1.67k
                                     value + 2 * incrementpersample);
4712
1.67k
                        _TIFFfreeExt(tif, value);
4713
1.67k
                    }
4714
2.69k
                }
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
732k
                default:
4788
732k
                    (void)TIFFFetchNormalTag(tif, dp, TRUE);
4789
732k
                    break;
4790
927k
            } /* -- switch (dp->tdir_tag) -- */
4791
927k
        }     /* -- if (!dp->tdir_ignore) */
4792
2.01M
    }         /* -- for-loop -- */
4793
4794
    /* Evaluate final IFD data size. */
4795
88.5k
    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
88.5k
    if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
4814
475
    {
4815
475
        if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
4816
341
        {
4817
341
            TIFFWarningExtR(
4818
341
                tif, module,
4819
341
                "Photometric tag is missing, assuming data is YCbCr");
4820
341
            if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
4821
0
                goto bad;
4822
341
        }
4823
134
        else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4824
4
        {
4825
4
            tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
4826
4
            TIFFWarningExtR(tif, module,
4827
4
                            "Photometric tag value assumed incorrect, "
4828
4
                            "assuming data is YCbCr instead of RGB");
4829
4
        }
4830
475
        if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
4831
396
        {
4832
396
            TIFFWarningExtR(
4833
396
                tif, module,
4834
396
                "BitsPerSample tag is missing, assuming 8 bits per sample");
4835
396
            if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
4836
0
                goto bad;
4837
396
        }
4838
475
        if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
4839
427
        {
4840
427
            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
427
            if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
4849
324
            {
4850
324
                TIFFWarningExtR(tif, module,
4851
324
                                "SamplesPerPixel tag is missing, "
4852
324
                                "applying correct SamplesPerPixel value of 3");
4853
324
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4854
0
                    goto bad;
4855
324
            }
4856
103
            else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
4857
103
                     (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
4858
89
            {
4859
                /*
4860
                 * SamplesPerPixel tag is missing, but is not required
4861
                 * by spec.  Assume correct SamplesPerPixel value of 1.
4862
                 */
4863
89
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
4864
0
                    goto bad;
4865
89
            }
4866
427
        }
4867
475
    }
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
88.5k
    if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
4875
74.0k
    {
4876
74.0k
        tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
4877
74.0k
        tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
4878
74.0k
        tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
4879
74.0k
        tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
4880
74.0k
        tif->tif_flags &= ~TIFF_ISTILED;
4881
74.0k
    }
4882
14.5k
    else
4883
14.5k
    {
4884
14.5k
        tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
4885
14.5k
        tif->tif_flags |= TIFF_ISTILED;
4886
14.5k
    }
4887
88.5k
    if (!tif->tif_dir.td_nstrips)
4888
579
    {
4889
579
        TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
4890
579
                      isTiled(tif) ? "tiles" : "strips");
4891
579
        goto bad;
4892
579
    }
4893
88.0k
    tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
4894
88.0k
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
4895
8.01k
        tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
4896
88.0k
    if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
4897
7.85k
    {
4898
#ifdef OJPEG_SUPPORT
4899
        if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4900
            (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
4901
        {
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
            TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
4911
        }
4912
        else
4913
#endif
4914
7.85k
        {
4915
7.85k
            MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
4916
7.85k
            goto bad;
4917
7.85k
        }
4918
7.85k
    }
4919
4920
80.1k
    if (tif->tif_mode == O_RDWR &&
4921
80.1k
        tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
4922
80.1k
        tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
4923
80.1k
        tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
4924
80.1k
        tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
4925
80.1k
        tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
4926
80.1k
        tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
4927
80.1k
        tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
4928
80.1k
        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
80.1k
    else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
4934
80.1k
    {
4935
80.1k
        if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
4936
80.1k
        {
4937
80.1k
            if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
4938
80.1k
                                     tif->tif_dir.td_nstrips,
4939
80.1k
                                     &tif->tif_dir.td_stripoffset_p))
4940
3.01k
            {
4941
3.01k
                goto bad;
4942
3.01k
            }
4943
80.1k
        }
4944
77.1k
        if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
4945
24.8k
        {
4946
24.8k
            if (!TIFFFetchStripThing(
4947
24.8k
                    tif, &(tif->tif_dir.td_stripbytecount_entry),
4948
24.8k
                    tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
4949
172
            {
4950
172
                goto bad;
4951
172
            }
4952
24.8k
        }
4953
77.1k
    }
4954
4955
    /*
4956
     * Make sure all non-color channels are extrasamples.
4957
     * If it's not the case, define them as such.
4958
     */
4959
76.9k
    color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
4960
76.9k
    if (color_channels &&
4961
76.9k
        tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
4962
76.3k
            color_channels)
4963
11.2k
    {
4964
11.2k
        uint16_t old_extrasamples;
4965
11.2k
        uint16_t *new_sampleinfo;
4966
4967
11.2k
        TIFFWarningExtR(
4968
11.2k
            tif, module,
4969
11.2k
            "Sum of Photometric type-related "
4970
11.2k
            "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4971
11.2k
            "Defining non-color channels as ExtraSamples.");
4972
4973
11.2k
        old_extrasamples = tif->tif_dir.td_extrasamples;
4974
11.2k
        tif->tif_dir.td_extrasamples =
4975
11.2k
            (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
4976
4977
        // sampleinfo should contain information relative to these new extra
4978
        // samples
4979
11.2k
        new_sampleinfo = (uint16_t *)_TIFFcallocExt(
4980
11.2k
            tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
4981
11.2k
        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.2k
        if (old_extrasamples > 0)
4992
3.12k
            memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
4993
3.12k
                   old_extrasamples * sizeof(uint16_t));
4994
11.2k
        _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
4995
11.2k
                              tif->tif_dir.td_extrasamples);
4996
11.2k
        _TIFFfreeExt(tif, new_sampleinfo);
4997
11.2k
    }
4998
4999
    /*
5000
     * Verify Palette image has a Colormap.
5001
     */
5002
76.9k
    if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
5003
76.9k
        !TIFFFieldSet(tif, FIELD_COLORMAP))
5004
586
    {
5005
586
        if (tif->tif_dir.td_bitspersample >= 8 &&
5006
586
            tif->tif_dir.td_samplesperpixel == 3)
5007
20
            tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
5008
566
        else if (tif->tif_dir.td_bitspersample >= 8)
5009
535
            tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
5010
31
        else
5011
31
        {
5012
31
            MissingRequired(tif, "Colormap");
5013
31
            goto bad;
5014
31
        }
5015
586
    }
5016
    /*
5017
     * OJPEG hack:
5018
     * We do no further messing with strip/tile offsets/bytecounts in OJPEG
5019
     * TIFFs
5020
     */
5021
76.9k
    if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
5022
76.7k
    {
5023
        /*
5024
         * Attempt to deal with a missing StripByteCounts tag.
5025
         */
5026
76.7k
        if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
5027
52.1k
        {
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.1k
            if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5034
52.1k
                 tif->tif_dir.td_nstrips > 1) ||
5035
52.1k
                (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
5036
52.0k
                 tif->tif_dir.td_nstrips !=
5037
6.25k
                     (uint32_t)tif->tif_dir.td_samplesperpixel))
5038
160
            {
5039
160
                MissingRequired(tif, "StripByteCounts");
5040
160
                goto bad;
5041
160
            }
5042
51.9k
            TIFFWarningExtR(
5043
51.9k
                tif, module,
5044
51.9k
                "TIFF directory is missing required "
5045
51.9k
                "\"StripByteCounts\" field, calculating from imagelength");
5046
51.9k
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5047
375
                goto bad;
5048
51.9k
        }
5049
24.5k
        else if (tif->tif_dir.td_nstrips == 1 &&
5050
24.5k
                 !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
5051
1.70k
        {
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
1.70k
            TIFFWarningExtR(tif, module,
5059
1.70k
                            "Bogus \"StripByteCounts\" field, ignoring and "
5060
1.70k
                            "calculating from imagelength");
5061
1.70k
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5062
27
                goto bad;
5063
1.70k
        }
5064
22.8k
        else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
5065
22.8k
                 tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5066
22.8k
                 tif->tif_dir.td_nstrips > 2 &&
5067
22.8k
                 tif->tif_dir.td_compression == COMPRESSION_NONE &&
5068
22.8k
                 TIFFGetStrileByteCount(tif, 0) !=
5069
6.38k
                     TIFFGetStrileByteCount(tif, 1) &&
5070
22.8k
                 TIFFGetStrileByteCount(tif, 0) != 0 &&
5071
22.8k
                 TIFFGetStrileByteCount(tif, 1) != 0)
5072
5.72k
        {
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
5.72k
            TIFFWarningExtR(tif, module,
5084
5.72k
                            "Wrong \"StripByteCounts\" field, ignoring and "
5085
5.72k
                            "calculating from imagelength");
5086
5.72k
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5087
1
                goto bad;
5088
5.72k
        }
5089
76.7k
    }
5090
76.3k
    if (dir)
5091
76.3k
    {
5092
76.3k
        _TIFFfreeExt(tif, dir);
5093
76.3k
        dir = NULL;
5094
76.3k
    }
5095
76.3k
    if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
5096
75.3k
    {
5097
75.3k
        if (tif->tif_dir.td_bitspersample >= 16)
5098
8.71k
            tif->tif_dir.td_maxsamplevalue = 0xFFFF;
5099
66.6k
        else
5100
66.6k
            tif->tif_dir.td_maxsamplevalue =
5101
66.6k
                (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
5102
75.3k
    }
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
76.3k
    (*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
76.3k
    if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
5142
76.3k
        (tif->tif_dir.td_nstrips == 1) &&
5143
76.3k
        (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
5144
76.3k
        ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
5145
5.89k
    {
5146
5.89k
        ChopUpSingleUncompressedStrip(tif);
5147
5.89k
    }
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
76.3k
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5153
76.3k
        tif->tif_dir.td_compression == COMPRESSION_NONE &&
5154
76.3k
        (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
5155
76.3k
        TIFFStripSize64(tif) > 0x7FFFFFFFUL)
5156
2.19k
    {
5157
2.19k
        TryChopUpUncompressedBigTiff(tif);
5158
2.19k
    }
5159
5160
    /*
5161
     * Clear the dirty directory flag.
5162
     */
5163
76.3k
    tif->tif_flags &= ~TIFF_DIRTYDIRECT;
5164
76.3k
    tif->tif_flags &= ~TIFF_DIRTYSTRIP;
5165
5166
    /*
5167
     * Reinitialize i/o since we are starting on a new directory.
5168
     */
5169
76.3k
    tif->tif_row = (uint32_t)-1;
5170
76.3k
    tif->tif_curstrip = (uint32_t)-1;
5171
76.3k
    tif->tif_col = (uint32_t)-1;
5172
76.3k
    tif->tif_curtile = (uint32_t)-1;
5173
76.3k
    tif->tif_tilesize = (tmsize_t)-1;
5174
5175
76.3k
    tif->tif_scanlinesize = TIFFScanlineSize(tif);
5176
76.3k
    if (!tif->tif_scanlinesize)
5177
2.31k
    {
5178
2.31k
        TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
5179
2.31k
        return (0);
5180
2.31k
    }
5181
5182
74.0k
    if (isTiled(tif))
5183
14.0k
    {
5184
14.0k
        tif->tif_tilesize = TIFFTileSize(tif);
5185
14.0k
        if (!tif->tif_tilesize)
5186
31
        {
5187
31
            TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
5188
31
            return (0);
5189
31
        }
5190
14.0k
    }
5191
60.0k
    else
5192
60.0k
    {
5193
60.0k
        if (!TIFFStripSize(tif))
5194
1.78k
        {
5195
1.78k
            TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
5196
1.78k
            return (0);
5197
1.78k
        }
5198
60.0k
    }
5199
72.2k
    return (1);
5200
21.2k
bad:
5201
21.2k
    if (dir)
5202
21.2k
        _TIFFfreeExt(tif, dir);
5203
21.2k
    return (0);
5204
74.0k
} /*-- TIFFReadDirectory() --*/
5205
5206
static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
5207
                                        uint16_t dircount)
5208
97.6k
{
5209
97.6k
    static const char module[] = "TIFFReadDirectoryCheckOrder";
5210
97.6k
    uint32_t m;
5211
97.6k
    uint16_t n;
5212
97.6k
    TIFFDirEntry *o;
5213
97.6k
    m = 0;
5214
601k
    for (n = 0, o = dir; n < dircount; n++, o++)
5215
582k
    {
5216
582k
        if (o->tdir_tag < m)
5217
78.6k
        {
5218
78.6k
            TIFFWarningExtR(tif, module,
5219
78.6k
                            "Invalid TIFF directory; tags are not sorted in "
5220
78.6k
                            "ascending order");
5221
78.6k
            break;
5222
78.6k
        }
5223
504k
        m = o->tdir_tag + 1;
5224
504k
    }
5225
97.6k
}
5226
5227
static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
5228
                                                uint16_t dircount,
5229
                                                uint16_t tagid)
5230
195k
{
5231
195k
    TIFFDirEntry *m;
5232
195k
    uint16_t n;
5233
195k
    (void)tif;
5234
2.32M
    for (m = dir, n = 0; n < dircount; m++, n++)
5235
2.24M
    {
5236
2.24M
        if (m->tdir_tag == tagid)
5237
119k
            return (m);
5238
2.24M
    }
5239
75.9k
    return (0);
5240
195k
}
5241
5242
static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
5243
                                           uint32_t *fii)
5244
2.87M
{
5245
2.87M
    int32_t ma, mb, mc;
5246
2.87M
    ma = -1;
5247
2.87M
    mc = (int32_t)tif->tif_nfields;
5248
21.1M
    while (1)
5249
21.1M
    {
5250
21.1M
        if (ma + 1 == mc)
5251
592k
        {
5252
592k
            *fii = FAILED_FII;
5253
592k
            return;
5254
592k
        }
5255
20.6M
        mb = (ma + mc) / 2;
5256
20.6M
        if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
5257
2.27M
            break;
5258
18.3M
        if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
5259
8.12M
            ma = mb;
5260
10.2M
        else
5261
10.2M
            mc = mb;
5262
18.3M
    }
5263
2.27M
    while (1)
5264
2.27M
    {
5265
2.27M
        if (mb == 0)
5266
75.7k
            break;
5267
2.20M
        if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
5268
2.20M
            break;
5269
0
        mb--;
5270
0
    }
5271
2.27M
    *fii = mb;
5272
2.27M
}
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
0
{
5281
0
    static const char module[] = "TIFFReadCustomDirectory";
5282
0
    TIFFDirEntry *dir;
5283
0
    uint16_t dircount;
5284
0
    TIFFDirEntry *dp;
5285
0
    uint16_t di;
5286
0
    const TIFFField *fip;
5287
0
    uint32_t fii;
5288
5289
0
    dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
5290
0
    if (!dircount)
5291
0
    {
5292
0
        TIFFErrorExtR(tif, module,
5293
0
                      "Failed to read custom directory at offset %" PRIu64,
5294
0
                      diroff);
5295
0
        return 0;
5296
0
    }
5297
0
    TIFFReadDirectoryCheckOrder(tif, dir, dircount);
5298
5299
    /*
5300
     * Mark duplicates of any tag to be ignored (bugzilla 1994)
5301
     * to avoid certain pathological problems.
5302
     */
5303
0
    {
5304
0
        TIFFDirEntry *ma;
5305
0
        uint16_t mb;
5306
0
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
5307
0
        {
5308
0
            TIFFDirEntry *na;
5309
0
            uint16_t nb;
5310
0
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
5311
0
            {
5312
0
                if (ma->tdir_tag == na->tdir_tag)
5313
0
                {
5314
0
                    na->tdir_ignore = TRUE;
5315
0
                }
5316
0
            }
5317
0
        }
5318
0
    }
5319
5320
    /* Free any old stuff and reinit. */
5321
0
    TIFFFreeDirectory(tif);
5322
    /* Even if custom directories do not need the default settings of a standard
5323
     * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e.
5324
     * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to
5325
     * be initialized, which is done in TIFFDefaultDirectory().
5326
     * After that, the field array for the custom tags needs to be setup again.
5327
     */
5328
0
    TIFFDefaultDirectory(tif);
5329
0
    _TIFFSetupFields(tif, infoarray);
5330
5331
    /* Allocate arrays for offset values outside IFD entry for IFD data size
5332
     * checking. Note: Counter are reset within TIFFFreeDirectory(). */
5333
0
    tif->tif_dir.td_dirdatasize_offsets =
5334
0
        (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
5335
0
            tif, dircount * sizeof(TIFFEntryOffsetAndLength));
5336
0
    if (tif->tif_dir.td_dirdatasize_offsets == NULL)
5337
0
    {
5338
0
        TIFFErrorExtR(
5339
0
            tif, module,
5340
0
            "Failed to allocate memory for counting IFD data size at reading");
5341
0
        if (dir)
5342
0
            _TIFFfreeExt(tif, dir);
5343
0
        return 0;
5344
0
    }
5345
5346
0
    for (di = 0, dp = dir; di < dircount; di++, dp++)
5347
0
    {
5348
0
        TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5349
0
        if (fii == FAILED_FII)
5350
0
        {
5351
0
            if (tif->tif_warn_about_unknown_tags)
5352
0
            {
5353
0
                TIFFWarningExtR(tif, module,
5354
0
                                "Unknown field with tag %" PRIu16 " (0x%" PRIx16
5355
0
                                ") encountered",
5356
0
                                dp->tdir_tag, dp->tdir_tag);
5357
0
            }
5358
0
            const TIFFField *fld = _TIFFCreateAnonField(
5359
0
                tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
5360
0
            if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
5361
0
            {
5362
0
                if (tif->tif_warn_about_unknown_tags)
5363
0
                {
5364
0
                    TIFFWarningExtR(
5365
0
                        tif, module,
5366
0
                        "Registering anonymous field with tag %" PRIu16
5367
0
                        " (0x%" PRIx16 ") failed",
5368
0
                        dp->tdir_tag, dp->tdir_tag);
5369
0
                }
5370
0
                dp->tdir_ignore = TRUE;
5371
0
            }
5372
0
            else
5373
0
            {
5374
0
                TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5375
0
                assert(fii != FAILED_FII);
5376
0
            }
5377
0
        }
5378
0
        if (!dp->tdir_ignore)
5379
0
        {
5380
0
            fip = tif->tif_fields[fii];
5381
0
            if (fip->field_bit == FIELD_IGNORE)
5382
0
                dp->tdir_ignore = TRUE;
5383
0
            else
5384
0
            {
5385
                /* check data type */
5386
0
                while ((fip->field_type != TIFF_ANY) &&
5387
0
                       (fip->field_type != dp->tdir_type))
5388
0
                {
5389
0
                    fii++;
5390
0
                    if ((fii == tif->tif_nfields) ||
5391
0
                        (tif->tif_fields[fii]->field_tag !=
5392
0
                         (uint32_t)dp->tdir_tag))
5393
0
                    {
5394
0
                        fii = 0xFFFF;
5395
0
                        break;
5396
0
                    }
5397
0
                    fip = tif->tif_fields[fii];
5398
0
                }
5399
0
                if (fii == 0xFFFF)
5400
0
                {
5401
0
                    TIFFWarningExtR(tif, module,
5402
0
                                    "Wrong data type %" PRIu16
5403
0
                                    " for \"%s\"; tag ignored",
5404
0
                                    dp->tdir_type, fip->field_name);
5405
0
                    dp->tdir_ignore = TRUE;
5406
0
                }
5407
0
                else
5408
0
                {
5409
                    /* check count if known in advance */
5410
0
                    if ((fip->field_readcount != TIFF_VARIABLE) &&
5411
0
                        (fip->field_readcount != TIFF_VARIABLE2))
5412
0
                    {
5413
0
                        uint32_t expected;
5414
0
                        if (fip->field_readcount == TIFF_SPP)
5415
0
                            expected =
5416
0
                                (uint32_t)tif->tif_dir.td_samplesperpixel;
5417
0
                        else
5418
0
                            expected = (uint32_t)fip->field_readcount;
5419
0
                        if (!CheckDirCount(tif, dp, expected))
5420
0
                            dp->tdir_ignore = TRUE;
5421
0
                    }
5422
0
                }
5423
0
            }
5424
0
            if (!dp->tdir_ignore)
5425
0
            {
5426
0
                switch (dp->tdir_tag)
5427
0
                {
5428
0
                    case EXIFTAG_SUBJECTDISTANCE:
5429
0
                        if (!TIFFFieldIsAnonymous(fip))
5430
0
                        {
5431
                            /* should only be called on a Exif directory */
5432
                            /* when exifFields[] is active */
5433
0
                            (void)TIFFFetchSubjectDistance(tif, dp);
5434
0
                        }
5435
0
                        else
5436
0
                        {
5437
0
                            (void)TIFFFetchNormalTag(tif, dp, TRUE);
5438
0
                        }
5439
0
                        break;
5440
0
                    default:
5441
0
                        (void)TIFFFetchNormalTag(tif, dp, TRUE);
5442
0
                        break;
5443
0
                }
5444
0
            } /*-- if (!dp->tdir_ignore) */
5445
0
        }
5446
0
    }
5447
    /* Evaluate final IFD data size. */
5448
0
    CalcFinalIFDdatasizeReading(tif, dircount);
5449
5450
    /* To be able to return from SubIFD or custom-IFD to main-IFD */
5451
0
    tif->tif_setdirectory_force_absolute = TRUE;
5452
0
    if (dir)
5453
0
        _TIFFfreeExt(tif, dir);
5454
0
    return 1;
5455
0
}
5456
5457
/*
5458
 * EXIF is important special case of custom IFD, so we have a special
5459
 * function to read it.
5460
 */
5461
int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
5462
0
{
5463
0
    return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
5464
0
}
5465
5466
/*
5467
 *--: EXIF-GPS custom directory reading as another special case of custom IFD.
5468
 */
5469
int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
5470
0
{
5471
0
    return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
5472
0
}
5473
5474
static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
5475
                                   uint16_t dircount)
5476
59.4k
{
5477
59.4k
    static const char module[] = "EstimateStripByteCounts";
5478
5479
59.4k
    TIFFDirEntry *dp;
5480
59.4k
    TIFFDirectory *td = &tif->tif_dir;
5481
59.4k
    uint32_t strip;
5482
5483
    /* Do not try to load stripbytecount as we will compute it */
5484
59.4k
    if (!_TIFFFillStrilesInternal(tif, 0))
5485
0
        return -1;
5486
5487
59.4k
    const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
5488
59.4k
    uint64_t filesize = 0;
5489
59.4k
    if (allocsize > 100 * 1024 * 1024)
5490
0
    {
5491
        /* Before allocating a huge amount of memory for corrupted files, check
5492
         * if size of requested memory is not greater than file size. */
5493
0
        filesize = TIFFGetFileSize(tif);
5494
0
        if (allocsize > filesize)
5495
0
        {
5496
0
            TIFFWarningExtR(
5497
0
                tif, module,
5498
0
                "Requested memory size for StripByteCounts of %" PRIu64
5499
0
                " is greater than filesize %" PRIu64 ". Memory not allocated",
5500
0
                allocsize, filesize);
5501
0
            return -1;
5502
0
        }
5503
0
    }
5504
5505
59.4k
    if (td->td_stripbytecount_p)
5506
7.43k
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
5507
59.4k
    td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
5508
59.4k
        tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
5509
59.4k
    if (td->td_stripbytecount_p == NULL)
5510
0
        return -1;
5511
5512
59.4k
    if (td->td_compression != COMPRESSION_NONE)
5513
47.8k
    {
5514
47.8k
        uint64_t space;
5515
47.8k
        uint16_t n;
5516
47.8k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
5517
47.5k
            space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
5518
299
        else
5519
299
            space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
5520
        /* calculate amount of space used by indirect values */
5521
649k
        for (dp = dir, n = dircount; n > 0; n--, dp++)
5522
601k
        {
5523
601k
            uint32_t typewidth;
5524
601k
            uint64_t datasize;
5525
601k
            typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type);
5526
601k
            if (typewidth == 0)
5527
355
            {
5528
355
                TIFFErrorExtR(
5529
355
                    tif, module,
5530
355
                    "Cannot determine size of unknown tag type %" PRIu16,
5531
355
                    dp->tdir_type);
5532
355
                return -1;
5533
355
            }
5534
601k
            if (dp->tdir_count > UINT64_MAX / typewidth)
5535
6
                return -1;
5536
601k
            datasize = (uint64_t)typewidth * dp->tdir_count;
5537
601k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
5538
600k
            {
5539
600k
                if (datasize <= 4)
5540
386k
                    datasize = 0;
5541
600k
            }
5542
1.42k
            else
5543
1.42k
            {
5544
1.42k
                if (datasize <= 8)
5545
944
                    datasize = 0;
5546
1.42k
            }
5547
601k
            if (space > UINT64_MAX - datasize)
5548
27
                return -1;
5549
601k
            space += datasize;
5550
601k
        }
5551
47.4k
        if (filesize == 0)
5552
47.4k
            filesize = TIFFGetFileSize(tif);
5553
47.4k
        if (filesize < space)
5554
            /* we should perhaps return in error ? */
5555
44.1k
            space = filesize;
5556
3.30k
        else
5557
3.30k
            space = filesize - space;
5558
47.4k
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
5559
4.71k
            space /= td->td_samplesperpixel;
5560
598k
        for (strip = 0; strip < td->td_nstrips; strip++)
5561
551k
            td->td_stripbytecount_p[strip] = space;
5562
        /*
5563
         * This gross hack handles the case were the offset to
5564
         * the last strip is past the place where we think the strip
5565
         * should begin.  Since a strip of data must be contiguous,
5566
         * it's safe to assume that we've overestimated the amount
5567
         * of data in the strip and trim this number back accordingly.
5568
         */
5569
47.4k
        strip--;
5570
47.4k
        if (td->td_stripoffset_p[strip] >
5571
47.4k
            UINT64_MAX - td->td_stripbytecount_p[strip])
5572
3
            return -1;
5573
47.4k
        if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
5574
47.4k
            filesize)
5575
42.4k
        {
5576
42.4k
            if (td->td_stripoffset_p[strip] >= filesize)
5577
1.61k
            {
5578
                /* Not sure what we should in that case... */
5579
1.61k
                td->td_stripbytecount_p[strip] = 0;
5580
1.61k
            }
5581
40.8k
            else
5582
40.8k
            {
5583
40.8k
                td->td_stripbytecount_p[strip] =
5584
40.8k
                    filesize - td->td_stripoffset_p[strip];
5585
40.8k
            }
5586
42.4k
        }
5587
47.4k
    }
5588
11.5k
    else if (isTiled(tif))
5589
1.19k
    {
5590
1.19k
        uint64_t bytespertile = TIFFTileSize64(tif);
5591
5592
8.85M
        for (strip = 0; strip < td->td_nstrips; strip++)
5593
8.85M
            td->td_stripbytecount_p[strip] = bytespertile;
5594
1.19k
    }
5595
10.3k
    else
5596
10.3k
    {
5597
10.3k
        uint64_t rowbytes = TIFFScanlineSize64(tif);
5598
10.3k
        uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
5599
46.7M
        for (strip = 0; strip < td->td_nstrips; strip++)
5600
46.7M
        {
5601
46.7M
            if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
5602
12
                return -1;
5603
46.7M
            td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
5604
46.7M
        }
5605
10.3k
    }
5606
58.9k
    TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
5607
58.9k
    if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
5608
34.4k
        td->td_rowsperstrip = td->td_imagelength;
5609
58.9k
    return 1;
5610
59.4k
}
5611
5612
static void MissingRequired(TIFF *tif, const char *tagname)
5613
10.7k
{
5614
10.7k
    static const char module[] = "MissingRequired";
5615
5616
10.7k
    TIFFErrorExtR(tif, module,
5617
10.7k
                  "TIFF directory is missing required \"%s\" field", tagname);
5618
10.7k
}
5619
5620
static unsigned long hashFuncOffsetToNumber(const void *elt)
5621
412k
{
5622
412k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5623
412k
        (const TIFFOffsetAndDirNumber *)elt;
5624
412k
    const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
5625
412k
                          ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
5626
412k
    return hash;
5627
412k
}
5628
5629
static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
5630
8.75k
{
5631
8.75k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5632
8.75k
        (const TIFFOffsetAndDirNumber *)elt1;
5633
8.75k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5634
8.75k
        (const TIFFOffsetAndDirNumber *)elt2;
5635
8.75k
    return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
5636
8.75k
}
5637
5638
static unsigned long hashFuncNumberToOffset(const void *elt)
5639
404k
{
5640
404k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5641
404k
        (const TIFFOffsetAndDirNumber *)elt;
5642
404k
    return offsetAndDirNumber->dirNumber;
5643
404k
}
5644
5645
static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
5646
0
{
5647
0
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5648
0
        (const TIFFOffsetAndDirNumber *)elt1;
5649
0
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5650
0
        (const TIFFOffsetAndDirNumber *)elt2;
5651
0
    return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
5652
0
}
5653
5654
/*
5655
 * Check the directory number and offset against the list of already seen
5656
 * directory numbers and offsets. This is a trick to prevent IFD looping.
5657
 * The one can create TIFF file with looped directory pointers. We will
5658
 * maintain a list of already seen directories and check every IFD offset
5659
 * and its IFD number against that list. However, the offset of an IFD number
5660
 * can change - e.g. when writing updates to file.
5661
 * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
5662
 * or an error has occurred.
5663
 */
5664
int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
5665
134k
{
5666
134k
    if (diroff == 0) /* no more directories */
5667
0
        return 0;
5668
5669
134k
    if (tif->tif_map_dir_offset_to_number == NULL)
5670
126k
    {
5671
126k
        tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
5672
126k
            hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
5673
126k
        if (tif->tif_map_dir_offset_to_number == NULL)
5674
0
        {
5675
0
            TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5676
0
                          "Not enough memory");
5677
0
            return 1;
5678
0
        }
5679
126k
    }
5680
5681
134k
    if (tif->tif_map_dir_number_to_offset == NULL)
5682
126k
    {
5683
        /* No free callback for this map, as it shares the same items as
5684
         * tif->tif_map_dir_offset_to_number. */
5685
126k
        tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
5686
126k
            hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
5687
126k
        if (tif->tif_map_dir_number_to_offset == NULL)
5688
0
        {
5689
0
            TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5690
0
                          "Not enough memory");
5691
0
            return 1;
5692
0
        }
5693
126k
    }
5694
5695
    /* Check if offset is already in the list:
5696
     * - yes: check, if offset is at the same IFD number - if not, it is an IFD
5697
     * loop
5698
     * -  no: add to list or update offset at that IFD number
5699
     */
5700
134k
    TIFFOffsetAndDirNumber entry;
5701
134k
    entry.offset = diroff;
5702
134k
    entry.dirNumber = dirn;
5703
5704
134k
    TIFFOffsetAndDirNumber *foundEntry =
5705
134k
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5706
134k
            tif->tif_map_dir_offset_to_number, &entry);
5707
134k
    if (foundEntry)
5708
0
    {
5709
0
        if (foundEntry->dirNumber == dirn)
5710
0
        {
5711
0
            return 1;
5712
0
        }
5713
0
        else
5714
0
        {
5715
0
            TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
5716
0
                            "TIFF directory %d has IFD looping to directory %u "
5717
0
                            "at offset 0x%" PRIx64 " (%" PRIu64 ")",
5718
0
                            (int)dirn - 1, foundEntry->dirNumber, diroff,
5719
0
                            diroff);
5720
0
            return 0;
5721
0
        }
5722
0
    }
5723
5724
    /* Check if offset of an IFD has been changed and update offset of that IFD
5725
     * number. */
5726
134k
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5727
134k
        tif->tif_map_dir_number_to_offset, &entry);
5728
134k
    if (foundEntry)
5729
0
    {
5730
0
        if (foundEntry->offset != diroff)
5731
0
        {
5732
0
            TIFFOffsetAndDirNumber entryOld;
5733
0
            entryOld.offset = foundEntry->offset;
5734
0
            entryOld.dirNumber = dirn;
5735
            /* We must remove first from tif_map_dir_number_to_offset as the */
5736
            /* entry is owned (and thus freed) by */
5737
            /* tif_map_dir_offset_to_number */
5738
0
            TIFFOffsetAndDirNumber *foundEntryOld =
5739
0
                (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5740
0
                    tif->tif_map_dir_number_to_offset, &entryOld);
5741
0
            if (foundEntryOld)
5742
0
            {
5743
0
                TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
5744
0
                                  foundEntryOld);
5745
0
            }
5746
0
            foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5747
0
                tif->tif_map_dir_offset_to_number, &entryOld);
5748
0
            if (foundEntryOld)
5749
0
            {
5750
0
                TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
5751
0
                                  foundEntryOld);
5752
0
            }
5753
5754
0
            TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
5755
0
                sizeof(TIFFOffsetAndDirNumber));
5756
0
            if (entryPtr == NULL)
5757
0
            {
5758
0
                return 0;
5759
0
            }
5760
5761
            /* Add IFD offset and dirn to IFD directory list */
5762
0
            *entryPtr = entry;
5763
5764
0
            if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5765
0
            {
5766
0
                TIFFErrorExtR(
5767
0
                    tif, "_TIFFCheckDirNumberAndOffset",
5768
0
                    "Insertion in tif_map_dir_offset_to_number failed");
5769
0
                return 0;
5770
0
            }
5771
0
            if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5772
0
            {
5773
0
                TIFFErrorExtR(
5774
0
                    tif, "_TIFFCheckDirNumberAndOffset",
5775
0
                    "Insertion in tif_map_dir_number_to_offset failed");
5776
0
                return 0;
5777
0
            }
5778
0
        }
5779
0
        return 1;
5780
0
    }
5781
5782
    /* Arbitrary (hopefully big enough) limit */
5783
134k
    if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
5784
134k
        TIFF_MAX_DIR_COUNT)
5785
0
    {
5786
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5787
0
                      "Cannot handle more than %u TIFF directories",
5788
0
                      TIFF_MAX_DIR_COUNT);
5789
0
        return 0;
5790
0
    }
5791
5792
134k
    TIFFOffsetAndDirNumber *entryPtr =
5793
134k
        (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
5794
134k
    if (entryPtr == NULL)
5795
0
    {
5796
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5797
0
                      "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
5798
0
        return 0;
5799
0
    }
5800
5801
    /* Add IFD offset and dirn to IFD directory list */
5802
134k
    *entryPtr = entry;
5803
5804
134k
    if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5805
0
    {
5806
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5807
0
                      "Insertion in tif_map_dir_offset_to_number failed");
5808
0
        return 0;
5809
0
    }
5810
134k
    if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5811
0
    {
5812
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5813
0
                      "Insertion in tif_map_dir_number_to_offset failed");
5814
0
        return 0;
5815
0
    }
5816
5817
134k
    return 1;
5818
134k
} /* --- _TIFFCheckDirNumberAndOffset() ---*/
5819
5820
/*
5821
 * Retrieve the matching IFD directory number of a given IFD offset
5822
 * from the list of directories already seen.
5823
 * Returns 1 if the offset was in the list and the directory number
5824
 * can be returned.
5825
 * Otherwise returns 0 or if an error occurred.
5826
 */
5827
int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
5828
8.21k
{
5829
8.21k
    if (diroff == 0) /* no more directories */
5830
0
        return 0;
5831
5832
    /* Check if offset is already in the list and return matching directory
5833
     * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
5834
     * search again in IFD list.
5835
     */
5836
8.21k
    if (tif->tif_map_dir_offset_to_number == NULL)
5837
0
        return 0;
5838
8.21k
    TIFFOffsetAndDirNumber entry;
5839
8.21k
    entry.offset = diroff;
5840
8.21k
    entry.dirNumber = 0; /* not used */
5841
5842
8.21k
    TIFFOffsetAndDirNumber *foundEntry =
5843
8.21k
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5844
8.21k
            tif->tif_map_dir_offset_to_number, &entry);
5845
8.21k
    if (foundEntry)
5846
8.21k
    {
5847
8.21k
        *dirn = foundEntry->dirNumber;
5848
8.21k
        return 1;
5849
8.21k
    }
5850
5851
    /* This updates the directory list for all main-IFDs in the file. */
5852
0
    TIFFNumberOfDirectories(tif);
5853
5854
0
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5855
0
        tif->tif_map_dir_offset_to_number, &entry);
5856
0
    if (foundEntry)
5857
0
    {
5858
0
        *dirn = foundEntry->dirNumber;
5859
0
        return 1;
5860
0
    }
5861
5862
0
    return 0;
5863
0
} /*--- _TIFFGetDirNumberFromOffset() ---*/
5864
5865
/*
5866
 * Retrieve the matching IFD directory offset of a given IFD number
5867
 * from the list of directories already seen.
5868
 * Returns 1 if the offset was in the list of already seen IFDs and the
5869
 * directory offset can be returned. The directory list is not updated.
5870
 * Otherwise returns 0 or if an error occurred.
5871
 */
5872
int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
5873
0
{
5874
5875
0
    if (tif->tif_map_dir_number_to_offset == NULL)
5876
0
        return 0;
5877
0
    TIFFOffsetAndDirNumber entry;
5878
0
    entry.offset = 0; /* not used */
5879
0
    entry.dirNumber = dirn;
5880
5881
0
    TIFFOffsetAndDirNumber *foundEntry =
5882
0
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5883
0
            tif->tif_map_dir_number_to_offset, &entry);
5884
0
    if (foundEntry)
5885
0
    {
5886
0
        *diroff = foundEntry->offset;
5887
0
        return 1;
5888
0
    }
5889
5890
0
    return 0;
5891
0
} /*--- _TIFFGetOffsetFromDirNumber() ---*/
5892
5893
/*
5894
 * Remove an entry from the directory list of already seen directories
5895
 * by directory offset.
5896
 * If an entry is to be removed from the list, it is also okay if the entry
5897
 * is not in the list or the list does not exist.
5898
 */
5899
int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
5900
0
{
5901
0
    if (tif->tif_map_dir_offset_to_number == NULL)
5902
0
        return 1;
5903
5904
0
    TIFFOffsetAndDirNumber entryOld;
5905
0
    entryOld.offset = diroff;
5906
0
    entryOld.dirNumber = 0;
5907
    /* We must remove first from tif_map_dir_number_to_offset as the
5908
     * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
5909
     * However, we need firstly to find the directory number from offset. */
5910
5911
0
    TIFFOffsetAndDirNumber *foundEntryOldOff =
5912
0
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5913
0
            tif->tif_map_dir_offset_to_number, &entryOld);
5914
0
    if (foundEntryOldOff)
5915
0
    {
5916
0
        entryOld.dirNumber = foundEntryOldOff->dirNumber;
5917
0
        if (tif->tif_map_dir_number_to_offset != NULL)
5918
0
        {
5919
0
            TIFFOffsetAndDirNumber *foundEntryOldDir =
5920
0
                (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5921
0
                    tif->tif_map_dir_number_to_offset, &entryOld);
5922
0
            if (foundEntryOldDir)
5923
0
            {
5924
0
                TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
5925
0
                                  foundEntryOldDir);
5926
0
                TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
5927
0
                                  foundEntryOldOff);
5928
0
                return 1;
5929
0
            }
5930
0
        }
5931
0
        else
5932
0
        {
5933
0
            TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
5934
0
                          "Unexpectedly tif_map_dir_number_to_offset is "
5935
0
                          "missing but tif_map_dir_offset_to_number exists.");
5936
0
            return 0;
5937
0
        }
5938
0
    }
5939
0
    return 1;
5940
0
} /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
5941
5942
/*
5943
 * Check the count field of a directory entry against a known value.  The
5944
 * caller is expected to skip/ignore the tag if there is a mismatch.
5945
 */
5946
static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
5947
0
{
5948
0
    if ((uint64_t)count > dir->tdir_count)
5949
0
    {
5950
0
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
5951
0
        TIFFWarningExtR(tif, tif->tif_name,
5952
0
                        "incorrect count for field \"%s\" (%" PRIu64
5953
0
                        ", expecting %" PRIu32 "); tag ignored",
5954
0
                        fip ? fip->field_name : "unknown tagname",
5955
0
                        dir->tdir_count, count);
5956
0
        return (0);
5957
0
    }
5958
0
    else if ((uint64_t)count < dir->tdir_count)
5959
0
    {
5960
0
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
5961
0
        TIFFWarningExtR(tif, tif->tif_name,
5962
0
                        "incorrect count for field \"%s\" (%" PRIu64
5963
0
                        ", expecting %" PRIu32 "); tag trimmed",
5964
0
                        fip ? fip->field_name : "unknown tagname",
5965
0
                        dir->tdir_count, count);
5966
0
        dir->tdir_count = count;
5967
0
        return (1);
5968
0
    }
5969
0
    return (1);
5970
0
}
5971
5972
/*
5973
 * Read IFD structure from the specified offset. If the pointer to
5974
 * nextdiroff variable has been specified, read it too. Function returns a
5975
 * number of fields in the directory or 0 if failed.
5976
 */
5977
static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
5978
                                   TIFFDirEntry **pdir, uint64_t *nextdiroff)
5979
100k
{
5980
100k
    static const char module[] = "TIFFFetchDirectory";
5981
5982
100k
    void *origdir;
5983
100k
    uint16_t dircount16;
5984
100k
    uint32_t dirsize;
5985
100k
    TIFFDirEntry *dir;
5986
100k
    uint8_t *ma;
5987
100k
    TIFFDirEntry *mb;
5988
100k
    uint16_t n;
5989
5990
100k
    assert(pdir);
5991
5992
100k
    tif->tif_diroff = diroff;
5993
100k
    if (nextdiroff)
5994
100k
        *nextdiroff = 0;
5995
100k
    if (!isMapped(tif))
5996
2.31k
    {
5997
2.31k
        if (!SeekOK(tif, tif->tif_diroff))
5998
0
        {
5999
0
            TIFFErrorExtR(tif, module,
6000
0
                          "%s: Seek error accessing TIFF directory",
6001
0
                          tif->tif_name);
6002
0
            return 0;
6003
0
        }
6004
2.31k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
6005
2.31k
        {
6006
2.31k
            if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
6007
0
            {
6008
0
                TIFFErrorExtR(tif, module,
6009
0
                              "%s: Can not read TIFF directory count",
6010
0
                              tif->tif_name);
6011
0
                return 0;
6012
0
            }
6013
2.31k
            if (tif->tif_flags & TIFF_SWAB)
6014
0
                TIFFSwabShort(&dircount16);
6015
2.31k
            if (dircount16 > 4096)
6016
0
            {
6017
0
                TIFFErrorExtR(tif, module,
6018
0
                              "Sanity check on directory count failed, this is "
6019
0
                              "probably not a valid IFD offset");
6020
0
                return 0;
6021
0
            }
6022
2.31k
            dirsize = 12;
6023
2.31k
        }
6024
0
        else
6025
0
        {
6026
0
            uint64_t dircount64;
6027
0
            if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
6028
0
            {
6029
0
                TIFFErrorExtR(tif, module,
6030
0
                              "%s: Can not read TIFF directory count",
6031
0
                              tif->tif_name);
6032
0
                return 0;
6033
0
            }
6034
0
            if (tif->tif_flags & TIFF_SWAB)
6035
0
                TIFFSwabLong8(&dircount64);
6036
0
            if (dircount64 > 4096)
6037
0
            {
6038
0
                TIFFErrorExtR(tif, module,
6039
0
                              "Sanity check on directory count failed, this is "
6040
0
                              "probably not a valid IFD offset");
6041
0
                return 0;
6042
0
            }
6043
0
            dircount16 = (uint16_t)dircount64;
6044
0
            dirsize = 20;
6045
0
        }
6046
2.31k
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6047
2.31k
                                   "to read TIFF directory");
6048
2.31k
        if (origdir == NULL)
6049
0
            return 0;
6050
2.31k
        if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
6051
0
        {
6052
0
            TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
6053
0
                          tif->tif_name);
6054
0
            _TIFFfreeExt(tif, origdir);
6055
0
            return 0;
6056
0
        }
6057
        /*
6058
         * Read offset to next directory for sequential scans if
6059
         * needed.
6060
         */
6061
2.31k
        if (nextdiroff)
6062
2.31k
        {
6063
2.31k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
6064
2.31k
            {
6065
2.31k
                uint32_t nextdiroff32;
6066
2.31k
                if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
6067
0
                    nextdiroff32 = 0;
6068
2.31k
                if (tif->tif_flags & TIFF_SWAB)
6069
0
                    TIFFSwabLong(&nextdiroff32);
6070
2.31k
                *nextdiroff = nextdiroff32;
6071
2.31k
            }
6072
0
            else
6073
0
            {
6074
0
                if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
6075
0
                    *nextdiroff = 0;
6076
0
                if (tif->tif_flags & TIFF_SWAB)
6077
0
                    TIFFSwabLong8(nextdiroff);
6078
0
            }
6079
2.31k
        }
6080
2.31k
    }
6081
97.8k
    else
6082
97.8k
    {
6083
97.8k
        tmsize_t m;
6084
97.8k
        tmsize_t off;
6085
97.8k
        if (tif->tif_diroff > (uint64_t)INT64_MAX)
6086
190
        {
6087
190
            TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6088
190
            return (0);
6089
190
        }
6090
97.6k
        off = (tmsize_t)tif->tif_diroff;
6091
6092
        /*
6093
         * Check for integer overflow when validating the dir_off,
6094
         * otherwise a very high offset may cause an OOB read and
6095
         * crash the client. Make two comparisons instead of
6096
         *
6097
         *  off + sizeof(uint16_t) > tif->tif_size
6098
         *
6099
         * to avoid overflow.
6100
         */
6101
97.6k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
6102
90.8k
        {
6103
90.8k
            m = off + sizeof(uint16_t);
6104
90.8k
            if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) ||
6105
90.8k
                (m > tif->tif_size))
6106
383
            {
6107
383
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6108
383
                return 0;
6109
383
            }
6110
90.4k
            else
6111
90.4k
            {
6112
90.4k
                _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
6113
90.4k
            }
6114
90.4k
            off += sizeof(uint16_t);
6115
90.4k
            if (tif->tif_flags & TIFF_SWAB)
6116
4.34k
                TIFFSwabShort(&dircount16);
6117
90.4k
            if (dircount16 > 4096)
6118
159
            {
6119
159
                TIFFErrorExtR(tif, module,
6120
159
                              "Sanity check on directory count failed, this is "
6121
159
                              "probably not a valid IFD offset");
6122
159
                return 0;
6123
159
            }
6124
90.3k
            dirsize = 12;
6125
90.3k
        }
6126
6.77k
        else
6127
6.77k
        {
6128
6.77k
            uint64_t dircount64;
6129
6.77k
            m = off + sizeof(uint64_t);
6130
6.77k
            if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
6131
6.77k
                (m > tif->tif_size))
6132
591
            {
6133
591
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6134
591
                return 0;
6135
591
            }
6136
6.18k
            else
6137
6.18k
            {
6138
6.18k
                _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
6139
6.18k
            }
6140
6.18k
            off += sizeof(uint64_t);
6141
6.18k
            if (tif->tif_flags & TIFF_SWAB)
6142
252
                TIFFSwabLong8(&dircount64);
6143
6.18k
            if (dircount64 > 4096)
6144
733
            {
6145
733
                TIFFErrorExtR(tif, module,
6146
733
                              "Sanity check on directory count failed, this is "
6147
733
                              "probably not a valid IFD offset");
6148
733
                return 0;
6149
733
            }
6150
5.44k
            dircount16 = (uint16_t)dircount64;
6151
5.44k
            dirsize = 20;
6152
5.44k
        }
6153
95.7k
        if (dircount16 == 0)
6154
26
        {
6155
26
            TIFFErrorExtR(tif, module,
6156
26
                          "Sanity check on directory count failed, zero tag "
6157
26
                          "directories not supported");
6158
26
            return 0;
6159
26
        }
6160
        /* Before allocating a huge amount of memory for corrupted files, check
6161
         * if size of requested memory is not greater than file size. */
6162
95.7k
        uint64_t filesize = TIFFGetFileSize(tif);
6163
95.7k
        uint64_t allocsize = (uint64_t)dircount16 * dirsize;
6164
95.7k
        if (allocsize > filesize)
6165
330
        {
6166
330
            TIFFWarningExtR(
6167
330
                tif, module,
6168
330
                "Requested memory size for TIFF directory of %" PRIu64
6169
330
                " is greater than filesize %" PRIu64
6170
330
                ". Memory not allocated, TIFF directory not read",
6171
330
                allocsize, filesize);
6172
330
            return 0;
6173
330
        }
6174
95.4k
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6175
95.4k
                                   "to read TIFF directory");
6176
95.4k
        if (origdir == NULL)
6177
0
            return 0;
6178
95.4k
        m = off + dircount16 * dirsize;
6179
95.4k
        if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
6180
95.4k
            (m > tif->tif_size))
6181
40
        {
6182
40
            TIFFErrorExtR(tif, module, "Can not read TIFF directory");
6183
40
            _TIFFfreeExt(tif, origdir);
6184
40
            return 0;
6185
40
        }
6186
95.3k
        else
6187
95.3k
        {
6188
95.3k
            _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
6189
95.3k
        }
6190
95.3k
        if (nextdiroff)
6191
95.3k
        {
6192
95.3k
            off += dircount16 * dirsize;
6193
95.3k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
6194
89.9k
            {
6195
89.9k
                uint32_t nextdiroff32;
6196
89.9k
                m = off + sizeof(uint32_t);
6197
89.9k
                if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) ||
6198
89.9k
                    (m > tif->tif_size))
6199
9.03k
                    nextdiroff32 = 0;
6200
80.9k
                else
6201
80.9k
                    _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
6202
80.9k
                                sizeof(uint32_t));
6203
89.9k
                if (tif->tif_flags & TIFF_SWAB)
6204
4.29k
                    TIFFSwabLong(&nextdiroff32);
6205
89.9k
                *nextdiroff = nextdiroff32;
6206
89.9k
            }
6207
5.42k
            else
6208
5.42k
            {
6209
5.42k
                m = off + sizeof(uint64_t);
6210
5.42k
                if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
6211
5.42k
                    (m > tif->tif_size))
6212
4.44k
                    *nextdiroff = 0;
6213
980
                else
6214
980
                    _TIFFmemcpy(nextdiroff, tif->tif_base + off,
6215
980
                                sizeof(uint64_t));
6216
5.42k
                if (tif->tif_flags & TIFF_SWAB)
6217
223
                    TIFFSwabLong8(nextdiroff);
6218
5.42k
            }
6219
95.3k
        }
6220
95.3k
    }
6221
    /* No check against filesize needed here because "dir" should have same size
6222
     * than "origdir" checked above. */
6223
97.6k
    dir = (TIFFDirEntry *)_TIFFCheckMalloc(
6224
97.6k
        tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
6225
97.6k
    if (dir == 0)
6226
0
    {
6227
0
        _TIFFfreeExt(tif, origdir);
6228
0
        return 0;
6229
0
    }
6230
97.6k
    ma = (uint8_t *)origdir;
6231
97.6k
    mb = dir;
6232
2.20M
    for (n = 0; n < dircount16; n++)
6233
2.10M
    {
6234
2.10M
        mb->tdir_ignore = FALSE;
6235
2.10M
        if (tif->tif_flags & TIFF_SWAB)
6236
65.0k
            TIFFSwabShort((uint16_t *)ma);
6237
2.10M
        mb->tdir_tag = *(uint16_t *)ma;
6238
2.10M
        ma += sizeof(uint16_t);
6239
2.10M
        if (tif->tif_flags & TIFF_SWAB)
6240
65.0k
            TIFFSwabShort((uint16_t *)ma);
6241
2.10M
        mb->tdir_type = *(uint16_t *)ma;
6242
2.10M
        ma += sizeof(uint16_t);
6243
2.10M
        if (!(tif->tif_flags & TIFF_BIGTIFF))
6244
2.05M
        {
6245
2.05M
            if (tif->tif_flags & TIFF_SWAB)
6246
63.3k
                TIFFSwabLong((uint32_t *)ma);
6247
2.05M
            mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
6248
2.05M
            ma += sizeof(uint32_t);
6249
2.05M
            mb->tdir_offset.toff_long8 = 0;
6250
2.05M
            *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
6251
2.05M
            ma += sizeof(uint32_t);
6252
2.05M
        }
6253
42.9k
        else
6254
42.9k
        {
6255
42.9k
            if (tif->tif_flags & TIFF_SWAB)
6256
1.66k
                TIFFSwabLong8((uint64_t *)ma);
6257
42.9k
            mb->tdir_count = TIFFReadUInt64(ma);
6258
42.9k
            ma += sizeof(uint64_t);
6259
42.9k
            mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
6260
42.9k
            ma += sizeof(uint64_t);
6261
42.9k
        }
6262
2.10M
        mb++;
6263
2.10M
    }
6264
97.6k
    _TIFFfreeExt(tif, origdir);
6265
97.6k
    *pdir = dir;
6266
97.6k
    return dircount16;
6267
97.6k
}
6268
6269
/*
6270
 * Fetch a tag that is not handled by special case code.
6271
 */
6272
static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
6273
1.04M
{
6274
1.04M
    static const char module[] = "TIFFFetchNormalTag";
6275
1.04M
    enum TIFFReadDirEntryErr err;
6276
1.04M
    uint32_t fii;
6277
1.04M
    const TIFFField *fip = NULL;
6278
1.04M
    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
6279
1.04M
    if (fii == FAILED_FII)
6280
0
    {
6281
0
        TIFFErrorExtR(tif, "TIFFFetchNormalTag",
6282
0
                      "No definition found for tag %" PRIu16, dp->tdir_tag);
6283
0
        return 0;
6284
0
    }
6285
1.04M
    fip = tif->tif_fields[fii];
6286
1.04M
    assert(fip != NULL); /* should not happen */
6287
1.04M
    assert(fip->set_get_field_type !=
6288
1.04M
           TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
6289
                                  this in specialized code */
6290
1.04M
    assert(fip->set_get_field_type !=
6291
1.04M
           TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
6292
                                the case for pseudo-tags */
6293
1.04M
    err = TIFFReadDirEntryErrOk;
6294
1.04M
    switch (fip->set_get_field_type)
6295
1.04M
    {
6296
277k
        case TIFF_SETGET_UNDEFINED:
6297
277k
            TIFFErrorExtR(
6298
277k
                tif, "TIFFFetchNormalTag",
6299
277k
                "Defined set_get_field_type of custom tag %u (%s) is "
6300
277k
                "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
6301
277k
                fip->field_tag, fip->field_name);
6302
277k
            break;
6303
21.1k
        case TIFF_SETGET_ASCII:
6304
21.1k
        {
6305
21.1k
            uint8_t *data;
6306
21.1k
            assert(fip->field_passcount == 0);
6307
21.1k
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
6308
21.1k
            if (err == TIFFReadDirEntryErrOk)
6309
8.05k
            {
6310
8.05k
                size_t mb = 0;
6311
8.05k
                int n;
6312
8.05k
                if (data != NULL)
6313
7.93k
                {
6314
7.93k
                    if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
6315
5.47k
                    {
6316
                        /* optimization: if data is known to be 0 terminated, we
6317
                         * can use strlen() */
6318
5.47k
                        mb = strlen((const char *)data);
6319
5.47k
                    }
6320
2.45k
                    else
6321
2.45k
                    {
6322
                        /* general case. equivalent to non-portable */
6323
                        /* mb = strnlen((const char*)data,
6324
                         * (uint32_t)dp->tdir_count); */
6325
2.45k
                        uint8_t *ma = data;
6326
16.7k
                        while (mb < (uint32_t)dp->tdir_count)
6327
14.9k
                        {
6328
14.9k
                            if (*ma == 0)
6329
625
                                break;
6330
14.3k
                            ma++;
6331
14.3k
                            mb++;
6332
14.3k
                        }
6333
2.45k
                    }
6334
7.93k
                }
6335
8.05k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6336
0
                {
6337
0
                    if (data != NULL)
6338
0
                        _TIFFfreeExt(tif, data);
6339
0
                    return (0);
6340
0
                }
6341
8.05k
                if (mb + 1 < (uint32_t)dp->tdir_count)
6342
1.18k
                    TIFFWarningExtR(
6343
1.18k
                        tif, module,
6344
1.18k
                        "ASCII value for tag \"%s\" contains null byte in "
6345
1.18k
                        "value; value incorrectly truncated during reading due "
6346
1.18k
                        "to implementation limitations",
6347
1.18k
                        fip->field_name);
6348
6.86k
                else if (mb + 1 > (uint32_t)dp->tdir_count)
6349
1.94k
                {
6350
1.94k
                    TIFFWarningExtR(tif, module,
6351
1.94k
                                    "ASCII value for tag \"%s\" does not end "
6352
1.94k
                                    "in null byte. Forcing it to be null",
6353
1.94k
                                    fip->field_name);
6354
                    /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
6355
                     * larger than MAX_SIZE_TAG_DATA */
6356
1.94k
                    assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
6357
1.94k
                    uint8_t *o =
6358
1.94k
                        _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
6359
1.94k
                    if (o == NULL)
6360
0
                    {
6361
0
                        if (data != NULL)
6362
0
                            _TIFFfreeExt(tif, data);
6363
0
                        return (0);
6364
0
                    }
6365
1.94k
                    if (dp->tdir_count > 0)
6366
1.83k
                    {
6367
1.83k
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6368
1.83k
                    }
6369
1.94k
                    o[(uint32_t)dp->tdir_count] = 0;
6370
1.94k
                    if (data != 0)
6371
1.83k
                        _TIFFfreeExt(tif, data);
6372
1.94k
                    data = o;
6373
1.94k
                }
6374
8.05k
                n = TIFFSetField(tif, dp->tdir_tag, data);
6375
8.05k
                if (data != 0)
6376
8.05k
                    _TIFFfreeExt(tif, data);
6377
8.05k
                if (!n)
6378
0
                    return (0);
6379
8.05k
            }
6380
21.1k
        }
6381
21.1k
        break;
6382
21.1k
        case TIFF_SETGET_UINT8:
6383
1.24k
        {
6384
1.24k
            uint8_t data = 0;
6385
1.24k
            assert(fip->field_readcount == 1);
6386
1.24k
            assert(fip->field_passcount == 0);
6387
1.24k
            err = TIFFReadDirEntryByte(tif, dp, &data);
6388
1.24k
            if (err == TIFFReadDirEntryErrOk)
6389
117
            {
6390
117
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6391
0
                    return (0);
6392
117
            }
6393
1.24k
        }
6394
1.24k
        break;
6395
1.24k
        case TIFF_SETGET_SINT8:
6396
0
        {
6397
0
            int8_t data = 0;
6398
0
            assert(fip->field_readcount == 1);
6399
0
            assert(fip->field_passcount == 0);
6400
0
            err = TIFFReadDirEntrySbyte(tif, dp, &data);
6401
0
            if (err == TIFFReadDirEntryErrOk)
6402
0
            {
6403
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6404
0
                    return (0);
6405
0
            }
6406
0
        }
6407
0
        break;
6408
181k
        case TIFF_SETGET_UINT16:
6409
181k
        {
6410
181k
            uint16_t data;
6411
181k
            assert(fip->field_readcount == 1);
6412
181k
            assert(fip->field_passcount == 0);
6413
181k
            err = TIFFReadDirEntryShort(tif, dp, &data);
6414
181k
            if (err == TIFFReadDirEntryErrOk)
6415
167k
            {
6416
167k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6417
211
                    return (0);
6418
167k
            }
6419
181k
        }
6420
181k
        break;
6421
181k
        case TIFF_SETGET_SINT16:
6422
0
        {
6423
0
            int16_t data;
6424
0
            assert(fip->field_readcount == 1);
6425
0
            assert(fip->field_passcount == 0);
6426
0
            err = TIFFReadDirEntrySshort(tif, dp, &data);
6427
0
            if (err == TIFFReadDirEntryErrOk)
6428
0
            {
6429
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6430
0
                    return (0);
6431
0
            }
6432
0
        }
6433
0
        break;
6434
236k
        case TIFF_SETGET_UINT32:
6435
236k
        {
6436
236k
            uint32_t data;
6437
236k
            assert(fip->field_readcount == 1);
6438
236k
            assert(fip->field_passcount == 0);
6439
236k
            err = TIFFReadDirEntryLong(tif, dp, &data);
6440
236k
            if (err == TIFFReadDirEntryErrOk)
6441
232k
            {
6442
232k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6443
12
                    return (0);
6444
232k
            }
6445
236k
        }
6446
236k
        break;
6447
236k
        case TIFF_SETGET_SINT32:
6448
0
        {
6449
0
            int32_t data;
6450
0
            assert(fip->field_readcount == 1);
6451
0
            assert(fip->field_passcount == 0);
6452
0
            err = TIFFReadDirEntrySlong(tif, dp, &data);
6453
0
            if (err == TIFFReadDirEntryErrOk)
6454
0
            {
6455
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6456
0
                    return (0);
6457
0
            }
6458
0
        }
6459
0
        break;
6460
0
        case TIFF_SETGET_UINT64:
6461
0
        {
6462
0
            uint64_t data;
6463
0
            assert(fip->field_readcount == 1);
6464
0
            assert(fip->field_passcount == 0);
6465
0
            err = TIFFReadDirEntryLong8(tif, dp, &data);
6466
0
            if (err == TIFFReadDirEntryErrOk)
6467
0
            {
6468
0
                if (!EvaluateIFDdatasizeReading(tif, dp))
6469
0
                    return 0;
6470
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6471
0
                    return (0);
6472
0
            }
6473
0
        }
6474
0
        break;
6475
0
        case TIFF_SETGET_SINT64:
6476
0
        {
6477
0
            int64_t data;
6478
0
            assert(fip->field_readcount == 1);
6479
0
            assert(fip->field_passcount == 0);
6480
0
            err = TIFFReadDirEntrySlong8(tif, dp, &data);
6481
0
            if (err == TIFFReadDirEntryErrOk)
6482
0
            {
6483
0
                if (!EvaluateIFDdatasizeReading(tif, dp))
6484
0
                    return 0;
6485
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6486
0
                    return (0);
6487
0
            }
6488
0
        }
6489
0
        break;
6490
16.6k
        case TIFF_SETGET_FLOAT:
6491
16.6k
        {
6492
16.6k
            float data;
6493
16.6k
            assert(fip->field_readcount == 1);
6494
16.6k
            assert(fip->field_passcount == 0);
6495
16.6k
            err = TIFFReadDirEntryFloat(tif, dp, &data);
6496
16.6k
            if (err == TIFFReadDirEntryErrOk)
6497
11.8k
            {
6498
11.8k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6499
0
                    return 0;
6500
11.8k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6501
119
                    return (0);
6502
11.8k
            }
6503
16.6k
        }
6504
16.5k
        break;
6505
16.5k
        case TIFF_SETGET_DOUBLE:
6506
163
        {
6507
163
            double data;
6508
163
            assert(fip->field_readcount == 1);
6509
163
            assert(fip->field_passcount == 0);
6510
163
            err = TIFFReadDirEntryDouble(tif, dp, &data);
6511
163
            if (err == TIFFReadDirEntryErrOk)
6512
46
            {
6513
46
                if (!EvaluateIFDdatasizeReading(tif, dp))
6514
0
                    return 0;
6515
46
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6516
0
                    return (0);
6517
46
            }
6518
163
        }
6519
163
        break;
6520
1.33k
        case TIFF_SETGET_IFD8:
6521
1.33k
        {
6522
1.33k
            uint64_t data;
6523
1.33k
            assert(fip->field_readcount == 1);
6524
1.33k
            assert(fip->field_passcount == 0);
6525
1.33k
            err = TIFFReadDirEntryIfd8(tif, dp, &data);
6526
1.33k
            if (err == TIFFReadDirEntryErrOk)
6527
682
            {
6528
682
                if (!EvaluateIFDdatasizeReading(tif, dp))
6529
0
                    return 0;
6530
682
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6531
65
                    return (0);
6532
682
            }
6533
1.33k
        }
6534
1.26k
        break;
6535
4.85k
        case TIFF_SETGET_UINT16_PAIR:
6536
4.85k
        {
6537
4.85k
            uint16_t *data;
6538
4.85k
            assert(fip->field_readcount == 2);
6539
4.85k
            assert(fip->field_passcount == 0);
6540
4.85k
            if (dp->tdir_count != 2)
6541
1.46k
            {
6542
1.46k
                TIFFWarningExtR(tif, module,
6543
1.46k
                                "incorrect count for field \"%s\", expected 2, "
6544
1.46k
                                "got %" PRIu64,
6545
1.46k
                                fip->field_name, dp->tdir_count);
6546
1.46k
                return (0);
6547
1.46k
            }
6548
3.39k
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
6549
3.39k
            if (err == TIFFReadDirEntryErrOk)
6550
3.05k
            {
6551
3.05k
                int m;
6552
3.05k
                assert(data); /* avoid CLang static Analyzer false positive */
6553
3.05k
                m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
6554
3.05k
                _TIFFfreeExt(tif, data);
6555
3.05k
                if (!m)
6556
0
                    return (0);
6557
3.05k
            }
6558
3.39k
        }
6559
3.39k
        break;
6560
3.39k
        case TIFF_SETGET_C0_UINT8:
6561
1.16k
        {
6562
1.16k
            uint8_t *data;
6563
1.16k
            assert(fip->field_readcount >= 1);
6564
1.16k
            assert(fip->field_passcount == 0);
6565
1.16k
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6566
460
            {
6567
460
                TIFFWarningExtR(tif, module,
6568
460
                                "incorrect count for field \"%s\", expected "
6569
460
                                "%d, got %" PRIu64,
6570
460
                                fip->field_name, (int)fip->field_readcount,
6571
460
                                dp->tdir_count);
6572
460
                return (0);
6573
460
            }
6574
700
            else
6575
700
            {
6576
700
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6577
700
                if (err == TIFFReadDirEntryErrOk)
6578
653
                {
6579
653
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6580
0
                    {
6581
0
                        if (data != 0)
6582
0
                            _TIFFfreeExt(tif, data);
6583
0
                        return 0;
6584
0
                    }
6585
653
                    int m;
6586
653
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6587
653
                    if (data != 0)
6588
653
                        _TIFFfreeExt(tif, data);
6589
653
                    if (!m)
6590
0
                        return (0);
6591
653
                }
6592
700
            }
6593
1.16k
        }
6594
700
        break;
6595
700
        case TIFF_SETGET_C0_SINT8:
6596
0
        {
6597
0
            int8_t *data;
6598
0
            assert(fip->field_readcount >= 1);
6599
0
            assert(fip->field_passcount == 0);
6600
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6601
0
            {
6602
0
                TIFFWarningExtR(tif, module,
6603
0
                                "incorrect count for field \"%s\", expected "
6604
0
                                "%d, got %" PRIu64,
6605
0
                                fip->field_name, (int)fip->field_readcount,
6606
0
                                dp->tdir_count);
6607
0
                return (0);
6608
0
            }
6609
0
            else
6610
0
            {
6611
0
                err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
6612
0
                if (err == TIFFReadDirEntryErrOk)
6613
0
                {
6614
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6615
0
                    {
6616
0
                        if (data != 0)
6617
0
                            _TIFFfreeExt(tif, data);
6618
0
                        return 0;
6619
0
                    }
6620
0
                    int m;
6621
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6622
0
                    if (data != 0)
6623
0
                        _TIFFfreeExt(tif, data);
6624
0
                    if (!m)
6625
0
                        return (0);
6626
0
                }
6627
0
            }
6628
0
        }
6629
0
        break;
6630
253
        case TIFF_SETGET_C0_UINT16:
6631
253
        {
6632
253
            uint16_t *data;
6633
253
            assert(fip->field_readcount >= 1);
6634
253
            assert(fip->field_passcount == 0);
6635
253
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6636
243
            {
6637
243
                TIFFWarningExtR(tif, module,
6638
243
                                "incorrect count for field \"%s\", expected "
6639
243
                                "%d, got %" PRIu64,
6640
243
                                fip->field_name, (int)fip->field_readcount,
6641
243
                                dp->tdir_count);
6642
243
                return (0);
6643
243
            }
6644
10
            else
6645
10
            {
6646
10
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
6647
10
                if (err == TIFFReadDirEntryErrOk)
6648
4
                {
6649
4
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6650
0
                    {
6651
0
                        if (data != 0)
6652
0
                            _TIFFfreeExt(tif, data);
6653
0
                        return 0;
6654
0
                    }
6655
4
                    int m;
6656
4
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6657
4
                    if (data != 0)
6658
4
                        _TIFFfreeExt(tif, data);
6659
4
                    if (!m)
6660
0
                        return (0);
6661
4
                }
6662
10
            }
6663
253
        }
6664
10
        break;
6665
10
        case TIFF_SETGET_C0_SINT16:
6666
0
        {
6667
0
            int16_t *data;
6668
0
            assert(fip->field_readcount >= 1);
6669
0
            assert(fip->field_passcount == 0);
6670
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6671
0
            {
6672
0
                TIFFWarningExtR(tif, module,
6673
0
                                "incorrect count for field \"%s\", expected "
6674
0
                                "%d, got %" PRIu64,
6675
0
                                fip->field_name, (int)fip->field_readcount,
6676
0
                                dp->tdir_count);
6677
0
                return (0);
6678
0
            }
6679
0
            else
6680
0
            {
6681
0
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6682
0
                if (err == TIFFReadDirEntryErrOk)
6683
0
                {
6684
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6685
0
                    {
6686
0
                        if (data != 0)
6687
0
                            _TIFFfreeExt(tif, data);
6688
0
                        return 0;
6689
0
                    }
6690
0
                    int m;
6691
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6692
0
                    if (data != 0)
6693
0
                        _TIFFfreeExt(tif, data);
6694
0
                    if (!m)
6695
0
                        return (0);
6696
0
                }
6697
0
            }
6698
0
        }
6699
0
        break;
6700
377
        case TIFF_SETGET_C0_UINT32:
6701
377
        {
6702
377
            uint32_t *data;
6703
377
            assert(fip->field_readcount >= 1);
6704
377
            assert(fip->field_passcount == 0);
6705
377
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6706
265
            {
6707
265
                TIFFWarningExtR(tif, module,
6708
265
                                "incorrect count for field \"%s\", expected "
6709
265
                                "%d, got %" PRIu64,
6710
265
                                fip->field_name, (int)fip->field_readcount,
6711
265
                                dp->tdir_count);
6712
265
                return (0);
6713
265
            }
6714
112
            else
6715
112
            {
6716
112
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
6717
112
                if (err == TIFFReadDirEntryErrOk)
6718
36
                {
6719
36
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6720
0
                    {
6721
0
                        if (data != 0)
6722
0
                            _TIFFfreeExt(tif, data);
6723
0
                        return 0;
6724
0
                    }
6725
36
                    int m;
6726
36
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6727
36
                    if (data != 0)
6728
36
                        _TIFFfreeExt(tif, data);
6729
36
                    if (!m)
6730
0
                        return (0);
6731
36
                }
6732
112
            }
6733
377
        }
6734
112
        break;
6735
112
        case TIFF_SETGET_C0_SINT32:
6736
0
        {
6737
0
            int32_t *data;
6738
0
            assert(fip->field_readcount >= 1);
6739
0
            assert(fip->field_passcount == 0);
6740
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6741
0
            {
6742
0
                TIFFWarningExtR(tif, module,
6743
0
                                "incorrect count for field \"%s\", expected "
6744
0
                                "%d, got %" PRIu64,
6745
0
                                fip->field_name, (int)fip->field_readcount,
6746
0
                                dp->tdir_count);
6747
0
                return (0);
6748
0
            }
6749
0
            else
6750
0
            {
6751
0
                err = TIFFReadDirEntrySlongArray(tif, dp, &data);
6752
0
                if (err == TIFFReadDirEntryErrOk)
6753
0
                {
6754
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6755
0
                    {
6756
0
                        if (data != 0)
6757
0
                            _TIFFfreeExt(tif, data);
6758
0
                        return 0;
6759
0
                    }
6760
0
                    int m;
6761
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6762
0
                    if (data != 0)
6763
0
                        _TIFFfreeExt(tif, data);
6764
0
                    if (!m)
6765
0
                        return (0);
6766
0
                }
6767
0
            }
6768
0
        }
6769
0
        break;
6770
0
        case TIFF_SETGET_C0_UINT64:
6771
0
        {
6772
0
            uint64_t *data;
6773
0
            assert(fip->field_readcount >= 1);
6774
0
            assert(fip->field_passcount == 0);
6775
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6776
0
            {
6777
0
                TIFFWarningExtR(tif, module,
6778
0
                                "incorrect count for field \"%s\", expected "
6779
0
                                "%d, got %" PRIu64,
6780
0
                                fip->field_name, (int)fip->field_readcount,
6781
0
                                dp->tdir_count);
6782
0
                return (0);
6783
0
            }
6784
0
            else
6785
0
            {
6786
0
                err = TIFFReadDirEntryLong8Array(tif, dp, &data);
6787
0
                if (err == TIFFReadDirEntryErrOk)
6788
0
                {
6789
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6790
0
                    {
6791
0
                        if (data != 0)
6792
0
                            _TIFFfreeExt(tif, data);
6793
0
                        return 0;
6794
0
                    }
6795
0
                    int m;
6796
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6797
0
                    if (data != 0)
6798
0
                        _TIFFfreeExt(tif, data);
6799
0
                    if (!m)
6800
0
                        return (0);
6801
0
                }
6802
0
            }
6803
0
        }
6804
0
        break;
6805
0
        case TIFF_SETGET_C0_SINT64:
6806
0
        {
6807
0
            int64_t *data;
6808
0
            assert(fip->field_readcount >= 1);
6809
0
            assert(fip->field_passcount == 0);
6810
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6811
0
            {
6812
0
                TIFFWarningExtR(tif, module,
6813
0
                                "incorrect count for field \"%s\", expected "
6814
0
                                "%d, got %" PRIu64,
6815
0
                                fip->field_name, (int)fip->field_readcount,
6816
0
                                dp->tdir_count);
6817
0
                return (0);
6818
0
            }
6819
0
            else
6820
0
            {
6821
0
                err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
6822
0
                if (err == TIFFReadDirEntryErrOk)
6823
0
                {
6824
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6825
0
                    {
6826
0
                        if (data != 0)
6827
0
                            _TIFFfreeExt(tif, data);
6828
0
                        return 0;
6829
0
                    }
6830
0
                    int m;
6831
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6832
0
                    if (data != 0)
6833
0
                        _TIFFfreeExt(tif, data);
6834
0
                    if (!m)
6835
0
                        return (0);
6836
0
                }
6837
0
            }
6838
0
        }
6839
0
        break;
6840
2.74k
        case TIFF_SETGET_C0_FLOAT:
6841
2.74k
        {
6842
2.74k
            float *data;
6843
2.74k
            assert(fip->field_readcount >= 1);
6844
2.74k
            assert(fip->field_passcount == 0);
6845
2.74k
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6846
1.08k
            {
6847
1.08k
                TIFFWarningExtR(tif, module,
6848
1.08k
                                "incorrect count for field \"%s\", expected "
6849
1.08k
                                "%d, got %" PRIu64,
6850
1.08k
                                fip->field_name, (int)fip->field_readcount,
6851
1.08k
                                dp->tdir_count);
6852
1.08k
                return (0);
6853
1.08k
            }
6854
1.66k
            else
6855
1.66k
            {
6856
1.66k
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
6857
1.66k
                if (err == TIFFReadDirEntryErrOk)
6858
993
                {
6859
993
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6860
0
                    {
6861
0
                        if (data != 0)
6862
0
                            _TIFFfreeExt(tif, data);
6863
0
                        return 0;
6864
0
                    }
6865
993
                    int m;
6866
993
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6867
993
                    if (data != 0)
6868
993
                        _TIFFfreeExt(tif, data);
6869
993
                    if (!m)
6870
0
                        return (0);
6871
993
                }
6872
1.66k
            }
6873
2.74k
        }
6874
1.66k
        break;
6875
        /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6876
         * into Double-Arrays. */
6877
1.66k
        case TIFF_SETGET_C0_DOUBLE:
6878
0
        {
6879
0
            double *data;
6880
0
            assert(fip->field_readcount >= 1);
6881
0
            assert(fip->field_passcount == 0);
6882
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6883
0
            {
6884
0
                TIFFWarningExtR(tif, module,
6885
0
                                "incorrect count for field \"%s\", expected "
6886
0
                                "%d, got %" PRIu64,
6887
0
                                fip->field_name, (int)fip->field_readcount,
6888
0
                                dp->tdir_count);
6889
0
                return (0);
6890
0
            }
6891
0
            else
6892
0
            {
6893
0
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6894
0
                if (err == TIFFReadDirEntryErrOk)
6895
0
                {
6896
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6897
0
                    {
6898
0
                        if (data != 0)
6899
0
                            _TIFFfreeExt(tif, data);
6900
0
                        return 0;
6901
0
                    }
6902
0
                    int m;
6903
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6904
0
                    if (data != 0)
6905
0
                        _TIFFfreeExt(tif, data);
6906
0
                    if (!m)
6907
0
                        return (0);
6908
0
                }
6909
0
            }
6910
0
        }
6911
0
        break;
6912
441
        case TIFF_SETGET_C16_ASCII:
6913
441
        {
6914
441
            uint8_t *data;
6915
441
            assert(fip->field_readcount == TIFF_VARIABLE);
6916
441
            assert(fip->field_passcount == 1);
6917
441
            if (dp->tdir_count > 0xFFFF)
6918
179
                err = TIFFReadDirEntryErrCount;
6919
262
            else
6920
262
            {
6921
262
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6922
262
                if (err == TIFFReadDirEntryErrOk)
6923
122
                {
6924
122
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6925
0
                    {
6926
0
                        if (data != 0)
6927
0
                            _TIFFfreeExt(tif, data);
6928
0
                        return 0;
6929
0
                    }
6930
122
                    int m;
6931
122
                    if (data != 0 && dp->tdir_count > 0 &&
6932
122
                        data[dp->tdir_count - 1] != '\0')
6933
52
                    {
6934
52
                        TIFFWarningExtR(tif, module,
6935
52
                                        "ASCII value for ASCII array tag "
6936
52
                                        "\"%s\" does not end in null "
6937
52
                                        "byte. Forcing it to be null",
6938
52
                                        fip->field_name);
6939
                        /* Enlarge buffer and add terminating null. */
6940
52
                        uint8_t *o =
6941
52
                            _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
6942
52
                        if (o == NULL)
6943
0
                        {
6944
0
                            if (data != NULL)
6945
0
                                _TIFFfreeExt(tif, data);
6946
0
                            return (0);
6947
0
                        }
6948
52
                        if (dp->tdir_count > 0)
6949
52
                        {
6950
52
                            _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6951
52
                        }
6952
52
                        o[(uint32_t)dp->tdir_count] = 0;
6953
52
                        dp->tdir_count++; /* Increment for added null. */
6954
52
                        if (data != 0)
6955
52
                            _TIFFfreeExt(tif, data);
6956
52
                        data = o;
6957
52
                    }
6958
122
                    m = TIFFSetField(tif, dp->tdir_tag,
6959
122
                                     (uint16_t)(dp->tdir_count), data);
6960
122
                    if (data != 0)
6961
104
                        _TIFFfreeExt(tif, data);
6962
122
                    if (!m)
6963
18
                        return (0);
6964
122
                }
6965
262
            }
6966
441
        }
6967
423
        break;
6968
465
        case TIFF_SETGET_C16_UINT8:
6969
465
        {
6970
465
            uint8_t *data;
6971
465
            assert(fip->field_readcount == TIFF_VARIABLE);
6972
465
            assert(fip->field_passcount == 1);
6973
465
            if (dp->tdir_count > 0xFFFF)
6974
283
                err = TIFFReadDirEntryErrCount;
6975
182
            else
6976
182
            {
6977
182
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6978
182
                if (err == TIFFReadDirEntryErrOk)
6979
68
                {
6980
68
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6981
0
                    {
6982
0
                        if (data != 0)
6983
0
                            _TIFFfreeExt(tif, data);
6984
0
                        return 0;
6985
0
                    }
6986
68
                    int m;
6987
68
                    m = TIFFSetField(tif, dp->tdir_tag,
6988
68
                                     (uint16_t)(dp->tdir_count), data);
6989
68
                    if (data != 0)
6990
32
                        _TIFFfreeExt(tif, data);
6991
68
                    if (!m)
6992
0
                        return (0);
6993
68
                }
6994
182
            }
6995
465
        }
6996
465
        break;
6997
465
        case TIFF_SETGET_C16_SINT8:
6998
0
        {
6999
0
            int8_t *data;
7000
0
            assert(fip->field_readcount == TIFF_VARIABLE);
7001
0
            assert(fip->field_passcount == 1);
7002
0
            if (dp->tdir_count > 0xFFFF)
7003
0
                err = TIFFReadDirEntryErrCount;
7004
0
            else
7005
0
            {
7006
0
                err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7007
0
                if (err == TIFFReadDirEntryErrOk)
7008
0
                {
7009
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7010
0
                    {
7011
0
                        if (data != 0)
7012
0
                            _TIFFfreeExt(tif, data);
7013
0
                        return 0;
7014
0
                    }
7015
0
                    int m;
7016
0
                    m = TIFFSetField(tif, dp->tdir_tag,
7017
0
                                     (uint16_t)(dp->tdir_count), data);
7018
0
                    if (data != 0)
7019
0
                        _TIFFfreeExt(tif, data);
7020
0
                    if (!m)
7021
0
                        return (0);
7022
0
                }
7023
0
            }
7024
0
        }
7025
0
        break;
7026
9.78k
        case TIFF_SETGET_C16_UINT16:
7027
9.78k
        {
7028
9.78k
            uint16_t *data;
7029
9.78k
            assert(fip->field_readcount == TIFF_VARIABLE);
7030
9.78k
            assert(fip->field_passcount == 1);
7031
9.78k
            if (dp->tdir_count > 0xFFFF)
7032
223
                err = TIFFReadDirEntryErrCount;
7033
9.56k
            else
7034
9.56k
            {
7035
9.56k
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
7036
9.56k
                if (err == TIFFReadDirEntryErrOk)
7037
8.81k
                {
7038
8.81k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7039
0
                    {
7040
0
                        if (data != 0)
7041
0
                            _TIFFfreeExt(tif, data);
7042
0
                        return 0;
7043
0
                    }
7044
8.81k
                    int m;
7045
8.81k
                    m = TIFFSetField(tif, dp->tdir_tag,
7046
8.81k
                                     (uint16_t)(dp->tdir_count), data);
7047
8.81k
                    if (data != 0)
7048
8.47k
                        _TIFFfreeExt(tif, data);
7049
8.81k
                    if (!m)
7050
102
                        return (0);
7051
8.81k
                }
7052
9.56k
            }
7053
9.78k
        }
7054
9.68k
        break;
7055
9.68k
        case TIFF_SETGET_C16_SINT16:
7056
0
        {
7057
0
            int16_t *data;
7058
0
            assert(fip->field_readcount == TIFF_VARIABLE);
7059
0
            assert(fip->field_passcount == 1);
7060
0
            if (dp->tdir_count > 0xFFFF)
7061
0
                err = TIFFReadDirEntryErrCount;
7062
0
            else
7063
0
            {
7064
0
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7065
0
                if (err == TIFFReadDirEntryErrOk)
7066
0
                {
7067
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7068
0
                    {
7069
0
                        if (data != 0)
7070
0
                            _TIFFfreeExt(tif, data);
7071
0
                        return 0;
7072
0
                    }
7073
0
                    int m;
7074
0
                    m = TIFFSetField(tif, dp->tdir_tag,
7075
0
                                     (uint16_t)(dp->tdir_count), data);
7076
0
                    if (data != 0)
7077
0
                        _TIFFfreeExt(tif, data);
7078
0
                    if (!m)
7079
0
                        return (0);
7080
0
                }
7081
0
            }
7082
0
        }
7083
0
        break;
7084
1.61k
        case TIFF_SETGET_C16_UINT32:
7085
1.61k
        {
7086
1.61k
            uint32_t *data;
7087
1.61k
            assert(fip->field_readcount == TIFF_VARIABLE);
7088
1.61k
            assert(fip->field_passcount == 1);
7089
1.61k
            if (dp->tdir_count > 0xFFFF)
7090
209
                err = TIFFReadDirEntryErrCount;
7091
1.40k
            else
7092
1.40k
            {
7093
1.40k
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
7094
1.40k
                if (err == TIFFReadDirEntryErrOk)
7095
528
                {
7096
528
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7097
0
                    {
7098
0
                        if (data != 0)
7099
0
                            _TIFFfreeExt(tif, data);
7100
0
                        return 0;
7101
0
                    }
7102
528
                    int m;
7103
528
                    m = TIFFSetField(tif, dp->tdir_tag,
7104
528
                                     (uint16_t)(dp->tdir_count), data);
7105
528
                    if (data != 0)
7106
495
                        _TIFFfreeExt(tif, data);
7107
528
                    if (!m)
7108
0
                        return (0);
7109
528
                }
7110
1.40k
            }
7111
1.61k
        }
7112
1.61k
        break;
7113
1.61k
        case TIFF_SETGET_C16_SINT32:
7114
0
        {
7115
0
            int32_t *data;
7116
0
            assert(fip->field_readcount == TIFF_VARIABLE);
7117
0
            assert(fip->field_passcount == 1);
7118
0
            if (dp->tdir_count > 0xFFFF)
7119
0
                err = TIFFReadDirEntryErrCount;
7120
0
            else
7121
0
            {
7122
0
                err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7123
0
                if (err == TIFFReadDirEntryErrOk)
7124
0
                {
7125
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7126
0
                    {
7127
0
                        if (data != 0)
7128
0
                            _TIFFfreeExt(tif, data);
7129
0
                        return 0;
7130
0
                    }
7131
0
                    int m;
7132
0
                    m = TIFFSetField(tif, dp->tdir_tag,
7133
0
                                     (uint16_t)(dp->tdir_count), data);
7134
0
                    if (data != 0)
7135
0
                        _TIFFfreeExt(tif, data);
7136
0
                    if (!m)
7137
0
                        return (0);
7138
0
                }
7139
0
            }
7140
0
        }
7141
0
        break;
7142
0
        case TIFF_SETGET_C16_UINT64:
7143
0
        {
7144
0
            uint64_t *data;
7145
0
            assert(fip->field_readcount == TIFF_VARIABLE);
7146
0
            assert(fip->field_passcount == 1);
7147
0
            if (dp->tdir_count > 0xFFFF)
7148
0
                err = TIFFReadDirEntryErrCount;
7149
0
            else
7150
0
            {
7151
0
                err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7152
0
                if (err == TIFFReadDirEntryErrOk)
7153
0
                {
7154
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7155
0
                    {
7156
0
                        if (data != 0)
7157
0
                            _TIFFfreeExt(tif, data);
7158
0
                        return 0;
7159
0
                    }
7160
0
                    int m;
7161
0
                    m = TIFFSetField(tif, dp->tdir_tag,
7162
0
                                     (uint16_t)(dp->tdir_count), data);
7163
0
                    if (data != 0)
7164
0
                        _TIFFfreeExt(tif, data);
7165
0
                    if (!m)
7166
0
                        return (0);
7167
0
                }
7168
0
            }
7169
0
        }
7170
0
        break;
7171
0
        case TIFF_SETGET_C16_SINT64:
7172
0
        {
7173
0
            int64_t *data;
7174
0
            assert(fip->field_readcount == TIFF_VARIABLE);
7175
0
            assert(fip->field_passcount == 1);
7176
0
            if (dp->tdir_count > 0xFFFF)
7177
0
                err = TIFFReadDirEntryErrCount;
7178
0
            else
7179
0
            {
7180
0
                err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7181
0
                if (err == TIFFReadDirEntryErrOk)
7182
0
                {
7183
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7184
0
                    {
7185
0
                        if (data != 0)
7186
0
                            _TIFFfreeExt(tif, data);
7187
0
                        return 0;
7188
0
                    }
7189
0
                    int m;
7190
0
                    m = TIFFSetField(tif, dp->tdir_tag,
7191
0
                                     (uint16_t)(dp->tdir_count), data);
7192
0
                    if (data != 0)
7193
0
                        _TIFFfreeExt(tif, data);
7194
0
                    if (!m)
7195
0
                        return (0);
7196
0
                }
7197
0
            }
7198
0
        }
7199
0
        break;
7200
3.59k
        case TIFF_SETGET_C16_FLOAT:
7201
3.59k
        {
7202
3.59k
            float *data;
7203
3.59k
            assert(fip->field_readcount == TIFF_VARIABLE);
7204
3.59k
            assert(fip->field_passcount == 1);
7205
3.59k
            if (dp->tdir_count > 0xFFFF)
7206
642
                err = TIFFReadDirEntryErrCount;
7207
2.95k
            else
7208
2.95k
            {
7209
2.95k
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7210
2.95k
                if (err == TIFFReadDirEntryErrOk)
7211
2.20k
                {
7212
2.20k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7213
0
                    {
7214
0
                        if (data != 0)
7215
0
                            _TIFFfreeExt(tif, data);
7216
0
                        return 0;
7217
0
                    }
7218
2.20k
                    int m;
7219
2.20k
                    m = TIFFSetField(tif, dp->tdir_tag,
7220
2.20k
                                     (uint16_t)(dp->tdir_count), data);
7221
2.20k
                    if (data != 0)
7222
2.13k
                        _TIFFfreeExt(tif, data);
7223
2.20k
                    if (!m)
7224
0
                        return (0);
7225
2.20k
                }
7226
2.95k
            }
7227
3.59k
        }
7228
3.59k
        break;
7229
3.59k
        case TIFF_SETGET_C16_DOUBLE:
7230
0
        {
7231
0
            double *data;
7232
0
            assert(fip->field_readcount == TIFF_VARIABLE);
7233
0
            assert(fip->field_passcount == 1);
7234
0
            if (dp->tdir_count > 0xFFFF)
7235
0
                err = TIFFReadDirEntryErrCount;
7236
0
            else
7237
0
            {
7238
0
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7239
0
                if (err == TIFFReadDirEntryErrOk)
7240
0
                {
7241
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7242
0
                    {
7243
0
                        if (data != 0)
7244
0
                            _TIFFfreeExt(tif, data);
7245
0
                        return 0;
7246
0
                    }
7247
0
                    int m;
7248
0
                    m = TIFFSetField(tif, dp->tdir_tag,
7249
0
                                     (uint16_t)(dp->tdir_count), data);
7250
0
                    if (data != 0)
7251
0
                        _TIFFfreeExt(tif, data);
7252
0
                    if (!m)
7253
0
                        return (0);
7254
0
                }
7255
0
            }
7256
0
        }
7257
0
        break;
7258
683
        case TIFF_SETGET_C16_IFD8:
7259
683
        {
7260
683
            uint64_t *data;
7261
683
            assert(fip->field_readcount == TIFF_VARIABLE);
7262
683
            assert(fip->field_passcount == 1);
7263
683
            if (dp->tdir_count > 0xFFFF)
7264
283
                err = TIFFReadDirEntryErrCount;
7265
400
            else
7266
400
            {
7267
400
                err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7268
400
                if (err == TIFFReadDirEntryErrOk)
7269
255
                {
7270
255
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7271
0
                    {
7272
0
                        if (data != 0)
7273
0
                            _TIFFfreeExt(tif, data);
7274
0
                        return 0;
7275
0
                    }
7276
255
                    int m;
7277
255
                    m = TIFFSetField(tif, dp->tdir_tag,
7278
255
                                     (uint16_t)(dp->tdir_count), data);
7279
255
                    if (data != 0)
7280
62
                        _TIFFfreeExt(tif, data);
7281
255
                    if (!m)
7282
0
                        return (0);
7283
255
                }
7284
400
            }
7285
683
        }
7286
683
        break;
7287
61.5k
        case TIFF_SETGET_C32_ASCII:
7288
61.5k
        {
7289
61.5k
            uint8_t *data;
7290
61.5k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7291
61.5k
            assert(fip->field_passcount == 1);
7292
61.5k
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
7293
61.5k
            if (err == TIFFReadDirEntryErrOk)
7294
18.7k
            {
7295
18.7k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7296
0
                {
7297
0
                    if (data != 0)
7298
0
                        _TIFFfreeExt(tif, data);
7299
0
                    return 0;
7300
0
                }
7301
18.7k
                int m;
7302
18.7k
                if (data != 0 && dp->tdir_count > 0 &&
7303
18.7k
                    data[dp->tdir_count - 1] != '\0')
7304
8.23k
                {
7305
8.23k
                    TIFFWarningExtR(
7306
8.23k
                        tif, module,
7307
8.23k
                        "ASCII value for ASCII array tag \"%s\" does not end "
7308
8.23k
                        "in null byte. Forcing it to be null",
7309
8.23k
                        fip->field_name);
7310
                    /* Enlarge buffer and add terminating null. */
7311
8.23k
                    uint8_t *o =
7312
8.23k
                        _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
7313
8.23k
                    if (o == NULL)
7314
0
                    {
7315
0
                        if (data != NULL)
7316
0
                            _TIFFfreeExt(tif, data);
7317
0
                        return (0);
7318
0
                    }
7319
8.23k
                    if (dp->tdir_count > 0)
7320
8.23k
                    {
7321
8.23k
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
7322
8.23k
                    }
7323
8.23k
                    o[(uint32_t)dp->tdir_count] = 0;
7324
8.23k
                    dp->tdir_count++; /* Increment for added null. */
7325
8.23k
                    if (data != 0)
7326
8.23k
                        _TIFFfreeExt(tif, data);
7327
8.23k
                    data = o;
7328
8.23k
                }
7329
18.7k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7330
18.7k
                                 data);
7331
18.7k
                if (data != 0)
7332
18.1k
                    _TIFFfreeExt(tif, data);
7333
18.7k
                if (!m)
7334
0
                    return (0);
7335
18.7k
            }
7336
61.5k
        }
7337
61.5k
        break;
7338
61.5k
        case TIFF_SETGET_C32_UINT8:
7339
30.6k
        {
7340
30.6k
            uint8_t *data;
7341
30.6k
            uint32_t count = 0;
7342
30.6k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7343
30.6k
            assert(fip->field_passcount == 1);
7344
30.6k
            if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
7345
30.6k
                dp->tdir_type == TIFF_LONG)
7346
32
            {
7347
                /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
7348
                 * data type LONG instead of UNDEFINED. Work around this
7349
                 * frequently found issue */
7350
32
                void *origdata;
7351
32
                err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
7352
32
                if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
7353
24
                {
7354
24
                    data = NULL;
7355
24
                }
7356
8
                else
7357
8
                {
7358
8
                    if (tif->tif_flags & TIFF_SWAB)
7359
3
                        TIFFSwabArrayOfLong((uint32_t *)origdata, count);
7360
8
                    data = (uint8_t *)origdata;
7361
8
                    count = (uint32_t)(count * 4);
7362
8
                }
7363
32
            }
7364
30.6k
            else
7365
30.6k
            {
7366
30.6k
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
7367
30.6k
                count = (uint32_t)(dp->tdir_count);
7368
30.6k
            }
7369
30.6k
            if (err == TIFFReadDirEntryErrOk)
7370
10.2k
            {
7371
10.2k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7372
0
                {
7373
0
                    if (data != 0)
7374
0
                        _TIFFfreeExt(tif, data);
7375
0
                    return 0;
7376
0
                }
7377
10.2k
                int m;
7378
10.2k
                m = TIFFSetField(tif, dp->tdir_tag, count, data);
7379
10.2k
                if (data != 0)
7380
8.87k
                    _TIFFfreeExt(tif, data);
7381
10.2k
                if (!m)
7382
4
                    return (0);
7383
10.2k
            }
7384
30.6k
        }
7385
30.6k
        break;
7386
30.6k
        case TIFF_SETGET_C32_SINT8:
7387
8.19k
        {
7388
8.19k
            int8_t *data = NULL;
7389
8.19k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7390
8.19k
            assert(fip->field_passcount == 1);
7391
8.19k
            err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7392
8.19k
            if (err == TIFFReadDirEntryErrOk)
7393
660
            {
7394
660
                if (!EvaluateIFDdatasizeReading(tif, dp))
7395
0
                {
7396
0
                    if (data != 0)
7397
0
                        _TIFFfreeExt(tif, data);
7398
0
                    return 0;
7399
0
                }
7400
660
                int m;
7401
660
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7402
660
                                 data);
7403
660
                if (data != 0)
7404
547
                    _TIFFfreeExt(tif, data);
7405
660
                if (!m)
7406
0
                    return (0);
7407
660
            }
7408
8.19k
        }
7409
8.19k
        break;
7410
89.7k
        case TIFF_SETGET_C32_UINT16:
7411
89.7k
        {
7412
89.7k
            uint16_t *data;
7413
89.7k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7414
89.7k
            assert(fip->field_passcount == 1);
7415
89.7k
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
7416
89.7k
            if (err == TIFFReadDirEntryErrOk)
7417
38.7k
            {
7418
38.7k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7419
0
                {
7420
0
                    if (data != 0)
7421
0
                        _TIFFfreeExt(tif, data);
7422
0
                    return 0;
7423
0
                }
7424
38.7k
                int m;
7425
38.7k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7426
38.7k
                                 data);
7427
38.7k
                if (data != 0)
7428
38.4k
                    _TIFFfreeExt(tif, data);
7429
38.7k
                if (!m)
7430
0
                    return (0);
7431
38.7k
            }
7432
89.7k
        }
7433
89.7k
        break;
7434
89.7k
        case TIFF_SETGET_C32_SINT16:
7435
3.82k
        {
7436
3.82k
            int16_t *data = NULL;
7437
3.82k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7438
3.82k
            assert(fip->field_passcount == 1);
7439
3.82k
            err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7440
3.82k
            if (err == TIFFReadDirEntryErrOk)
7441
1.85k
            {
7442
1.85k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7443
0
                {
7444
0
                    if (data != 0)
7445
0
                        _TIFFfreeExt(tif, data);
7446
0
                    return 0;
7447
0
                }
7448
1.85k
                int m;
7449
1.85k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7450
1.85k
                                 data);
7451
1.85k
                if (data != 0)
7452
1.57k
                    _TIFFfreeExt(tif, data);
7453
1.85k
                if (!m)
7454
0
                    return (0);
7455
1.85k
            }
7456
3.82k
        }
7457
3.82k
        break;
7458
26.5k
        case TIFF_SETGET_C32_UINT32:
7459
26.5k
        {
7460
26.5k
            uint32_t *data;
7461
26.5k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7462
26.5k
            assert(fip->field_passcount == 1);
7463
26.5k
            err = TIFFReadDirEntryLongArray(tif, dp, &data);
7464
26.5k
            if (err == TIFFReadDirEntryErrOk)
7465
9.31k
            {
7466
9.31k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7467
0
                {
7468
0
                    if (data != 0)
7469
0
                        _TIFFfreeExt(tif, data);
7470
0
                    return 0;
7471
0
                }
7472
9.31k
                int m;
7473
9.31k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7474
9.31k
                                 data);
7475
9.31k
                if (data != 0)
7476
9.02k
                    _TIFFfreeExt(tif, data);
7477
9.31k
                if (!m)
7478
0
                    return (0);
7479
9.31k
            }
7480
26.5k
        }
7481
26.5k
        break;
7482
26.5k
        case TIFF_SETGET_C32_SINT32:
7483
2.57k
        {
7484
2.57k
            int32_t *data = NULL;
7485
2.57k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7486
2.57k
            assert(fip->field_passcount == 1);
7487
2.57k
            err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7488
2.57k
            if (err == TIFFReadDirEntryErrOk)
7489
967
            {
7490
967
                if (!EvaluateIFDdatasizeReading(tif, dp))
7491
0
                {
7492
0
                    if (data != 0)
7493
0
                        _TIFFfreeExt(tif, data);
7494
0
                    return 0;
7495
0
                }
7496
967
                int m;
7497
967
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7498
967
                                 data);
7499
967
                if (data != 0)
7500
718
                    _TIFFfreeExt(tif, data);
7501
967
                if (!m)
7502
0
                    return (0);
7503
967
            }
7504
2.57k
        }
7505
2.57k
        break;
7506
2.57k
        case TIFF_SETGET_C32_UINT64:
7507
1.75k
        {
7508
1.75k
            uint64_t *data;
7509
1.75k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7510
1.75k
            assert(fip->field_passcount == 1);
7511
1.75k
            err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7512
1.75k
            if (err == TIFFReadDirEntryErrOk)
7513
667
            {
7514
667
                if (!EvaluateIFDdatasizeReading(tif, dp))
7515
0
                {
7516
0
                    if (data != 0)
7517
0
                        _TIFFfreeExt(tif, data);
7518
0
                    return 0;
7519
0
                }
7520
667
                int m;
7521
667
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7522
667
                                 data);
7523
667
                if (data != 0)
7524
361
                    _TIFFfreeExt(tif, data);
7525
667
                if (!m)
7526
286
                    return (0);
7527
667
            }
7528
1.75k
        }
7529
1.46k
        break;
7530
1.77k
        case TIFF_SETGET_C32_SINT64:
7531
1.77k
        {
7532
1.77k
            int64_t *data = NULL;
7533
1.77k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7534
1.77k
            assert(fip->field_passcount == 1);
7535
1.77k
            err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7536
1.77k
            if (err == TIFFReadDirEntryErrOk)
7537
924
            {
7538
924
                if (!EvaluateIFDdatasizeReading(tif, dp))
7539
0
                {
7540
0
                    if (data != 0)
7541
0
                        _TIFFfreeExt(tif, data);
7542
0
                    return 0;
7543
0
                }
7544
924
                int m;
7545
924
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7546
924
                                 data);
7547
924
                if (data != 0)
7548
760
                    _TIFFfreeExt(tif, data);
7549
924
                if (!m)
7550
628
                    return (0);
7551
924
            }
7552
1.77k
        }
7553
1.14k
        break;
7554
13.6k
        case TIFF_SETGET_C32_FLOAT:
7555
13.6k
        {
7556
13.6k
            float *data;
7557
13.6k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7558
13.6k
            assert(fip->field_passcount == 1);
7559
13.6k
            err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7560
13.6k
            if (err == TIFFReadDirEntryErrOk)
7561
1.81k
            {
7562
1.81k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7563
0
                {
7564
0
                    if (data != 0)
7565
0
                        _TIFFfreeExt(tif, data);
7566
0
                    return 0;
7567
0
                }
7568
1.81k
                int m;
7569
1.81k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7570
1.81k
                                 data);
7571
1.81k
                if (data != 0)
7572
1.47k
                    _TIFFfreeExt(tif, data);
7573
1.81k
                if (!m)
7574
0
                    return (0);
7575
1.81k
            }
7576
13.6k
        }
7577
13.6k
        break;
7578
43.4k
        case TIFF_SETGET_C32_DOUBLE:
7579
43.4k
        {
7580
43.4k
            double *data;
7581
43.4k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7582
43.4k
            assert(fip->field_passcount == 1);
7583
43.4k
            err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7584
43.4k
            if (err == TIFFReadDirEntryErrOk)
7585
15.2k
            {
7586
15.2k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7587
0
                {
7588
0
                    if (data != 0)
7589
0
                        _TIFFfreeExt(tif, data);
7590
0
                    return 0;
7591
0
                }
7592
15.2k
                int m;
7593
15.2k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7594
15.2k
                                 data);
7595
15.2k
                if (data != 0)
7596
15.1k
                    _TIFFfreeExt(tif, data);
7597
15.2k
                if (!m)
7598
0
                    return (0);
7599
15.2k
            }
7600
43.4k
        }
7601
43.4k
        break;
7602
43.4k
        case TIFF_SETGET_C32_IFD8:
7603
2.51k
        {
7604
2.51k
            uint64_t *data;
7605
2.51k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7606
2.51k
            assert(fip->field_passcount == 1);
7607
2.51k
            err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7608
2.51k
            if (err == TIFFReadDirEntryErrOk)
7609
503
            {
7610
503
                if (!EvaluateIFDdatasizeReading(tif, dp))
7611
0
                {
7612
0
                    if (data != 0)
7613
0
                        _TIFFfreeExt(tif, data);
7614
0
                    return 0;
7615
0
                }
7616
503
                int m;
7617
503
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7618
503
                                 data);
7619
503
                if (data != 0)
7620
410
                    _TIFFfreeExt(tif, data);
7621
503
                if (!m)
7622
0
                    return (0);
7623
503
            }
7624
2.51k
        }
7625
2.51k
        break;
7626
2.51k
        default:
7627
0
            assert(0); /* we should never get here */
7628
0
            break;
7629
1.04M
    }
7630
1.04M
    if (err != TIFFReadDirEntryErrOk)
7631
230k
    {
7632
230k
        TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
7633
230k
        return (0);
7634
230k
    }
7635
812k
    return (1);
7636
1.04M
}
7637
7638
/*
7639
 * Fetch a set of offsets or lengths.
7640
 * While this routine says "strips", in fact it's also used for tiles.
7641
 */
7642
static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
7643
                               uint64_t **lpp)
7644
104k
{
7645
104k
    static const char module[] = "TIFFFetchStripThing";
7646
104k
    enum TIFFReadDirEntryErr err;
7647
104k
    uint64_t *data;
7648
104k
    err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
7649
104k
    if (err != TIFFReadDirEntryErrOk)
7650
3.10k
    {
7651
3.10k
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7652
3.10k
        TIFFReadDirEntryOutputErr(tif, err, module,
7653
3.10k
                                  fip ? fip->field_name : "unknown tagname", 0);
7654
3.10k
        return (0);
7655
3.10k
    }
7656
101k
    if (dir->tdir_count < (uint64_t)nstrips)
7657
7.61k
    {
7658
7.61k
        uint64_t *resizeddata;
7659
7.61k
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7660
7.61k
        const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
7661
7.61k
        uint32_t max_nstrips = 1000000;
7662
7.61k
        if (pszMax)
7663
0
            max_nstrips = (uint32_t)atoi(pszMax);
7664
7.61k
        TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
7665
7.61k
                                  fip ? fip->field_name : "unknown tagname",
7666
7.61k
                                  (nstrips <= max_nstrips));
7667
7668
7.61k
        if (nstrips > max_nstrips)
7669
78
        {
7670
78
            _TIFFfreeExt(tif, data);
7671
78
            return (0);
7672
78
        }
7673
7674
7.53k
        const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
7675
7.53k
        if (allocsize > 100 * 1024 * 1024)
7676
0
        {
7677
            /* Before allocating a huge amount of memory for corrupted files,
7678
             * check if size of requested memory is not greater than file size.
7679
             */
7680
0
            const uint64_t filesize = TIFFGetFileSize(tif);
7681
0
            if (allocsize > filesize)
7682
0
            {
7683
0
                TIFFWarningExtR(
7684
0
                    tif, module,
7685
0
                    "Requested memory size for StripArray of %" PRIu64
7686
0
                    " is greater than filesize %" PRIu64
7687
0
                    ". Memory not allocated",
7688
0
                    allocsize, filesize);
7689
0
                _TIFFfreeExt(tif, data);
7690
0
                return (0);
7691
0
            }
7692
0
        }
7693
7.53k
        resizeddata = (uint64_t *)_TIFFCheckMalloc(
7694
7.53k
            tif, nstrips, sizeof(uint64_t), "for strip array");
7695
7.53k
        if (resizeddata == 0)
7696
0
        {
7697
0
            _TIFFfreeExt(tif, data);
7698
0
            return (0);
7699
0
        }
7700
7.53k
        if (dir->tdir_count)
7701
4.63k
            _TIFFmemcpy(resizeddata, data,
7702
4.63k
                        (uint32_t)dir->tdir_count * sizeof(uint64_t));
7703
7.53k
        _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
7704
7.53k
                    (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
7705
7.53k
        _TIFFfreeExt(tif, data);
7706
7.53k
        data = resizeddata;
7707
7.53k
    }
7708
101k
    *lpp = data;
7709
101k
    return (1);
7710
101k
}
7711
7712
/*
7713
 * Fetch and set the SubjectDistance EXIF tag.
7714
 */
7715
static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
7716
0
{
7717
0
    static const char module[] = "TIFFFetchSubjectDistance";
7718
0
    enum TIFFReadDirEntryErr err;
7719
0
    UInt64Aligned_t m;
7720
0
    m.l = 0;
7721
0
    assert(sizeof(double) == 8);
7722
0
    assert(sizeof(uint64_t) == 8);
7723
0
    assert(sizeof(uint32_t) == 4);
7724
0
    if (dir->tdir_count != 1)
7725
0
        err = TIFFReadDirEntryErrCount;
7726
0
    else if (dir->tdir_type != TIFF_RATIONAL)
7727
0
        err = TIFFReadDirEntryErrType;
7728
0
    else
7729
0
    {
7730
0
        if (!(tif->tif_flags & TIFF_BIGTIFF))
7731
0
        {
7732
0
            uint32_t offset;
7733
0
            offset = *(uint32_t *)(&dir->tdir_offset);
7734
0
            if (tif->tif_flags & TIFF_SWAB)
7735
0
                TIFFSwabLong(&offset);
7736
0
            err = TIFFReadDirEntryData(tif, offset, 8, m.i);
7737
0
        }
7738
0
        else
7739
0
        {
7740
0
            m.l = dir->tdir_offset.toff_long8;
7741
0
            err = TIFFReadDirEntryErrOk;
7742
0
        }
7743
0
    }
7744
0
    if (err == TIFFReadDirEntryErrOk)
7745
0
    {
7746
0
        double n;
7747
0
        if (tif->tif_flags & TIFF_SWAB)
7748
0
            TIFFSwabArrayOfLong(m.i, 2);
7749
0
        if (m.i[0] == 0)
7750
0
            n = 0.0;
7751
0
        else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
7752
            /*
7753
             * XXX: Numerator 0xFFFFFFFF means that we have infinite
7754
             * distance. Indicate that with a negative floating point
7755
             * SubjectDistance value.
7756
             */
7757
0
            n = -1.0;
7758
0
        else
7759
0
            n = (double)m.i[0] / (double)m.i[1];
7760
0
        return (TIFFSetField(tif, dir->tdir_tag, n));
7761
0
    }
7762
0
    else
7763
0
    {
7764
0
        TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
7765
0
        return (0);
7766
0
    }
7767
0
}
7768
7769
static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
7770
                                      uint64_t stripbytes,
7771
                                      uint32_t rowsperstrip)
7772
1.68k
{
7773
1.68k
    TIFFDirectory *td = &tif->tif_dir;
7774
1.68k
    uint64_t bytecount;
7775
1.68k
    uint64_t offset;
7776
1.68k
    uint64_t last_offset;
7777
1.68k
    uint64_t last_bytecount;
7778
1.68k
    uint32_t i;
7779
1.68k
    uint64_t *newcounts;
7780
1.68k
    uint64_t *newoffsets;
7781
7782
1.68k
    offset = TIFFGetStrileOffset(tif, 0);
7783
1.68k
    last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
7784
1.68k
    last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
7785
1.68k
    if (last_offset > UINT64_MAX - last_bytecount ||
7786
1.68k
        last_offset + last_bytecount < offset)
7787
139
    {
7788
139
        return;
7789
139
    }
7790
1.55k
    bytecount = last_offset + last_bytecount - offset;
7791
7792
    /* Before allocating a huge amount of memory for corrupted files, check if
7793
     * size of StripByteCount and StripOffset tags is not greater than
7794
     * file size.
7795
     */
7796
1.55k
    const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
7797
1.55k
    if (allocsize > 100 * 1024 * 1024)
7798
0
    {
7799
0
        const uint64_t filesize = TIFFGetFileSize(tif);
7800
0
        if (allocsize > filesize)
7801
0
        {
7802
0
            TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
7803
0
                            "Requested memory size for StripByteCount and "
7804
0
                            "StripOffsets %" PRIu64
7805
0
                            " is greater than filesize %" PRIu64
7806
0
                            ". Memory not allocated",
7807
0
                            allocsize, filesize);
7808
0
            return;
7809
0
        }
7810
0
    }
7811
7812
1.55k
    newcounts =
7813
1.55k
        (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
7814
1.55k
                                     "for chopped \"StripByteCounts\" array");
7815
1.55k
    newoffsets = (uint64_t *)_TIFFCheckMalloc(
7816
1.55k
        tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7817
1.55k
    if (newcounts == NULL || newoffsets == NULL)
7818
0
    {
7819
        /*
7820
         * Unable to allocate new strip information, give up and use
7821
         * the original one strip information.
7822
         */
7823
0
        if (newcounts != NULL)
7824
0
            _TIFFfreeExt(tif, newcounts);
7825
0
        if (newoffsets != NULL)
7826
0
            _TIFFfreeExt(tif, newoffsets);
7827
0
        return;
7828
0
    }
7829
7830
    /*
7831
     * Fill the strip information arrays with new bytecounts and offsets
7832
     * that reflect the broken-up format.
7833
     */
7834
63.3M
    for (i = 0; i < nstrips; i++)
7835
63.3M
    {
7836
63.3M
        if (stripbytes > bytecount)
7837
1.09k
            stripbytes = bytecount;
7838
63.3M
        newcounts[i] = stripbytes;
7839
63.3M
        newoffsets[i] = stripbytes ? offset : 0;
7840
63.3M
        offset += stripbytes;
7841
63.3M
        bytecount -= stripbytes;
7842
63.3M
    }
7843
7844
    /*
7845
     * Replace old single strip info with multi-strip info.
7846
     */
7847
1.55k
    td->td_stripsperimage = td->td_nstrips = nstrips;
7848
1.55k
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
7849
7850
1.55k
    _TIFFfreeExt(tif, td->td_stripbytecount_p);
7851
1.55k
    _TIFFfreeExt(tif, td->td_stripoffset_p);
7852
1.55k
    td->td_stripbytecount_p = newcounts;
7853
1.55k
    td->td_stripoffset_p = newoffsets;
7854
#ifdef STRIPBYTECOUNTSORTED_UNUSED
7855
    td->td_stripbytecountsorted = 1;
7856
#endif
7857
1.55k
    tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
7858
1.55k
}
7859
7860
/*
7861
 * Replace a single strip (tile) of uncompressed data by multiple strips
7862
 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
7863
 * dealing with large images or for dealing with machines with a limited
7864
 * amount memory.
7865
 */
7866
static void ChopUpSingleUncompressedStrip(TIFF *tif)
7867
5.89k
{
7868
5.89k
    register TIFFDirectory *td = &tif->tif_dir;
7869
5.89k
    uint64_t bytecount;
7870
5.89k
    uint64_t offset;
7871
5.89k
    uint32_t rowblock;
7872
5.89k
    uint64_t rowblockbytes;
7873
5.89k
    uint64_t stripbytes;
7874
5.89k
    uint32_t nstrips;
7875
5.89k
    uint32_t rowsperstrip;
7876
7877
5.89k
    bytecount = TIFFGetStrileByteCount(tif, 0);
7878
    /* On a newly created file, just re-opened to be filled, we */
7879
    /* don't want strip chop to trigger as it is going to cause issues */
7880
    /* later ( StripOffsets and StripByteCounts improperly filled) . */
7881
5.89k
    if (bytecount == 0 && tif->tif_mode != O_RDONLY)
7882
0
        return;
7883
5.89k
    offset = TIFFGetStrileByteCount(tif, 0);
7884
5.89k
    assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
7885
5.89k
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
7886
374
        rowblock = td->td_ycbcrsubsampling[1];
7887
5.52k
    else
7888
5.52k
        rowblock = 1;
7889
5.89k
    rowblockbytes = TIFFVTileSize64(tif, rowblock);
7890
    /*
7891
     * Make the rows hold at least one scanline, but fill specified amount
7892
     * of data if possible.
7893
     */
7894
5.89k
    if (rowblockbytes > STRIP_SIZE_DEFAULT)
7895
1.57k
    {
7896
1.57k
        stripbytes = rowblockbytes;
7897
1.57k
        rowsperstrip = rowblock;
7898
1.57k
    }
7899
4.31k
    else if (rowblockbytes > 0)
7900
3.09k
    {
7901
3.09k
        uint32_t rowblocksperstrip;
7902
3.09k
        rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
7903
3.09k
        rowsperstrip = rowblocksperstrip * rowblock;
7904
3.09k
        stripbytes = rowblocksperstrip * rowblockbytes;
7905
3.09k
    }
7906
1.21k
    else
7907
1.21k
        return;
7908
7909
    /*
7910
     * never increase the number of rows per strip
7911
     */
7912
4.67k
    if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
7913
1.89k
        return;
7914
2.78k
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
7915
2.78k
    if (nstrips == 0)
7916
107
        return;
7917
7918
    /* If we are going to allocate a lot of memory, make sure that the */
7919
    /* file is as big as needed */
7920
2.68k
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
7921
2.68k
        (offset >= TIFFGetFileSize(tif) ||
7922
1.46k
         stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
7923
1.46k
    {
7924
1.46k
        return;
7925
1.46k
    }
7926
7927
1.21k
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
7928
1.21k
}
7929
7930
/*
7931
 * Replace a file with contiguous strips > 2 GB of uncompressed data by
7932
 * multiple smaller strips. This is useful for
7933
 * dealing with large images or for dealing with machines with a limited
7934
 * amount memory.
7935
 */
7936
static void TryChopUpUncompressedBigTiff(TIFF *tif)
7937
2.19k
{
7938
2.19k
    TIFFDirectory *td = &tif->tif_dir;
7939
2.19k
    uint32_t rowblock;
7940
2.19k
    uint64_t rowblockbytes;
7941
2.19k
    uint32_t i;
7942
2.19k
    uint64_t stripsize;
7943
2.19k
    uint32_t rowblocksperstrip;
7944
2.19k
    uint32_t rowsperstrip;
7945
2.19k
    uint64_t stripbytes;
7946
2.19k
    uint32_t nstrips;
7947
7948
2.19k
    stripsize = TIFFStripSize64(tif);
7949
7950
2.19k
    assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
7951
2.19k
    assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
7952
2.19k
    assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
7953
2.19k
           TIFF_STRIPCHOP);
7954
2.19k
    assert(stripsize > 0x7FFFFFFFUL);
7955
7956
    /* On a newly created file, just re-opened to be filled, we */
7957
    /* don't want strip chop to trigger as it is going to cause issues */
7958
    /* later ( StripOffsets and StripByteCounts improperly filled) . */
7959
2.19k
    if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
7960
0
        return;
7961
7962
2.19k
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
7963
6
        rowblock = td->td_ycbcrsubsampling[1];
7964
2.19k
    else
7965
2.19k
        rowblock = 1;
7966
2.19k
    rowblockbytes = TIFFVStripSize64(tif, rowblock);
7967
2.19k
    if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
7968
172
    {
7969
        /* In case of file with gigantic width */
7970
172
        return;
7971
172
    }
7972
7973
    /* Check that the strips are contiguous and of the expected size */
7974
3.38k
    for (i = 0; i < td->td_nstrips; i++)
7975
2.09k
    {
7976
2.09k
        if (i == td->td_nstrips - 1)
7977
1.38k
        {
7978
1.38k
            if (TIFFGetStrileByteCount(tif, i) <
7979
1.38k
                TIFFVStripSize64(tif,
7980
1.38k
                                 td->td_imagelength - i * td->td_rowsperstrip))
7981
98
            {
7982
98
                return;
7983
98
            }
7984
1.38k
        }
7985
716
        else
7986
716
        {
7987
716
            if (TIFFGetStrileByteCount(tif, i) != stripsize)
7988
573
            {
7989
573
                return;
7990
573
            }
7991
143
            if (i > 0 && TIFFGetStrileOffset(tif, i) !=
7992
71
                             TIFFGetStrileOffset(tif, i - 1) +
7993
71
                                 TIFFGetStrileByteCount(tif, i - 1))
7994
69
            {
7995
69
                return;
7996
69
            }
7997
143
        }
7998
2.09k
    }
7999
8000
    /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
8001
1.28k
    rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
8002
1.28k
    if (rowblocksperstrip == 0)
8003
19
        rowblocksperstrip = 1;
8004
1.28k
    rowsperstrip = rowblocksperstrip * rowblock;
8005
1.28k
    stripbytes = rowblocksperstrip * rowblockbytes;
8006
1.28k
    assert(stripbytes <= 0x7FFFFFFFUL);
8007
8008
1.28k
    if (rowsperstrip == 0)
8009
0
        return;
8010
1.28k
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
8011
1.28k
    if (nstrips == 0)
8012
28
        return;
8013
8014
    /* If we are going to allocate a lot of memory, make sure that the */
8015
    /* file is as big as needed */
8016
1.25k
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
8017
779
    {
8018
779
        uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
8019
779
        uint64_t filesize = TIFFGetFileSize(tif);
8020
779
        uint64_t last_bytecount =
8021
779
            TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
8022
779
        if (last_offset > filesize || last_bytecount > filesize - last_offset)
8023
779
        {
8024
779
            return;
8025
779
        }
8026
779
    }
8027
8028
477
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
8029
477
}
8030
8031
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
8032
static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
8033
0
{
8034
0
    return a + b;
8035
0
}
8036
8037
/* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
8038
 * strip/tile of number strile. Also fetch the neighbouring values using a
8039
 * 4096 byte page size.
8040
 */
8041
static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent,
8042
                                      int strile, uint64_t *panVals)
8043
0
{
8044
0
    static const char module[] = "_TIFFPartialReadStripArray";
8045
0
#define IO_CACHE_PAGE_SIZE 4096
8046
8047
0
    size_t sizeofval;
8048
0
    const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
8049
0
    int sizeofvalint;
8050
0
    uint64_t nBaseOffset;
8051
0
    uint64_t nOffset;
8052
0
    uint64_t nOffsetStartPage;
8053
0
    uint64_t nOffsetEndPage;
8054
0
    tmsize_t nToRead;
8055
0
    tmsize_t nRead;
8056
0
    uint64_t nLastStripOffset;
8057
0
    int iStartBefore;
8058
0
    int i;
8059
0
    const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
8060
0
    unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
8061
8062
0
    assert(dirent->tdir_count > 4);
8063
8064
0
    if (dirent->tdir_type == TIFF_SHORT)
8065
0
    {
8066
0
        sizeofval = sizeof(uint16_t);
8067
0
    }
8068
0
    else if (dirent->tdir_type == TIFF_LONG)
8069
0
    {
8070
0
        sizeofval = sizeof(uint32_t);
8071
0
    }
8072
0
    else if (dirent->tdir_type == TIFF_LONG8)
8073
0
    {
8074
0
        sizeofval = sizeof(uint64_t);
8075
0
    }
8076
0
    else if (dirent->tdir_type == TIFF_SLONG8)
8077
0
    {
8078
        /* Non conformant but used by some images as in */
8079
        /* https://github.com/OSGeo/gdal/issues/2165 */
8080
0
        sizeofval = sizeof(int64_t);
8081
0
    }
8082
0
    else
8083
0
    {
8084
0
        TIFFErrorExtR(tif, module,
8085
0
                      "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
8086
0
        panVals[strile] = 0;
8087
0
        return 0;
8088
0
    }
8089
0
    sizeofvalint = (int)(sizeofval);
8090
8091
0
    if (tif->tif_flags & TIFF_BIGTIFF)
8092
0
    {
8093
0
        uint64_t offset = dirent->tdir_offset.toff_long8;
8094
0
        if (bSwab)
8095
0
            TIFFSwabLong8(&offset);
8096
0
        nBaseOffset = offset;
8097
0
    }
8098
0
    else
8099
0
    {
8100
0
        uint32_t offset = dirent->tdir_offset.toff_long;
8101
0
        if (bSwab)
8102
0
            TIFFSwabLong(&offset);
8103
0
        nBaseOffset = offset;
8104
0
    }
8105
    /* To avoid later unsigned integer overflows */
8106
0
    if (nBaseOffset > (uint64_t)INT64_MAX)
8107
0
    {
8108
0
        TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
8109
0
                      strile);
8110
0
        panVals[strile] = 0;
8111
0
        return 0;
8112
0
    }
8113
0
    nOffset = nBaseOffset + sizeofval * strile;
8114
0
    nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
8115
0
    nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
8116
8117
0
    if (nOffset + sizeofval > nOffsetEndPage)
8118
0
        nOffsetEndPage += IO_CACHE_PAGE_SIZE;
8119
0
#undef IO_CACHE_PAGE_SIZE
8120
8121
0
    nLastStripOffset = nBaseOffset + arraySize * sizeofval;
8122
0
    if (nLastStripOffset < nOffsetEndPage)
8123
0
        nOffsetEndPage = nLastStripOffset;
8124
0
    if (nOffsetStartPage >= nOffsetEndPage)
8125
0
    {
8126
0
        TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
8127
0
                      strile);
8128
0
        panVals[strile] = 0;
8129
0
        return 0;
8130
0
    }
8131
0
    if (!SeekOK(tif, nOffsetStartPage))
8132
0
    {
8133
0
        panVals[strile] = 0;
8134
0
        return 0;
8135
0
    }
8136
8137
0
    nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
8138
0
    nRead = TIFFReadFile(tif, buffer, nToRead);
8139
0
    if (nRead < nToRead)
8140
0
    {
8141
0
        TIFFErrorExtR(tif, module,
8142
0
                      "Cannot read offset/size for strile around ~%d", strile);
8143
0
        return 0;
8144
0
    }
8145
0
    iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
8146
0
    if (strile + iStartBefore < 0)
8147
0
        iStartBefore = -strile;
8148
0
    for (i = iStartBefore;
8149
0
         (uint32_t)(strile + i) < arraySize &&
8150
0
         _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
8151
0
             nOffsetEndPage;
8152
0
         ++i)
8153
0
    {
8154
0
        if (dirent->tdir_type == TIFF_SHORT)
8155
0
        {
8156
0
            uint16_t val;
8157
0
            memcpy(&val,
8158
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8159
0
                   sizeof(val));
8160
0
            if (bSwab)
8161
0
                TIFFSwabShort(&val);
8162
0
            panVals[strile + i] = val;
8163
0
        }
8164
0
        else if (dirent->tdir_type == TIFF_LONG)
8165
0
        {
8166
0
            uint32_t val;
8167
0
            memcpy(&val,
8168
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8169
0
                   sizeof(val));
8170
0
            if (bSwab)
8171
0
                TIFFSwabLong(&val);
8172
0
            panVals[strile + i] = val;
8173
0
        }
8174
0
        else if (dirent->tdir_type == TIFF_LONG8)
8175
0
        {
8176
0
            uint64_t val;
8177
0
            memcpy(&val,
8178
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8179
0
                   sizeof(val));
8180
0
            if (bSwab)
8181
0
                TIFFSwabLong8(&val);
8182
0
            panVals[strile + i] = val;
8183
0
        }
8184
0
        else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
8185
0
        {
8186
            /* Non conformant data type */
8187
0
            int64_t val;
8188
0
            memcpy(&val,
8189
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
8190
0
                   sizeof(val));
8191
0
            if (bSwab)
8192
0
                TIFFSwabLong8((uint64_t *)&val);
8193
0
            panVals[strile + i] = (uint64_t)val;
8194
0
        }
8195
0
    }
8196
0
    return 1;
8197
0
}
8198
8199
static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
8200
                                 TIFFDirEntry *dirent, uint64_t **parray)
8201
0
{
8202
0
    static const char module[] = "_TIFFFetchStrileValue";
8203
0
    TIFFDirectory *td = &tif->tif_dir;
8204
0
    if (strile >= dirent->tdir_count)
8205
0
    {
8206
0
        return 0;
8207
0
    }
8208
0
    if (strile >= td->td_stripoffsetbyteallocsize)
8209
0
    {
8210
0
        uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
8211
0
        uint32_t nStripArrayAllocNew;
8212
0
        uint64_t nArraySize64;
8213
0
        size_t nArraySize;
8214
0
        uint64_t *offsetArray;
8215
0
        uint64_t *bytecountArray;
8216
8217
0
        if (strile > 1000000)
8218
0
        {
8219
0
            uint64_t filesize = TIFFGetFileSize(tif);
8220
            /* Avoid excessive memory allocation attempt */
8221
            /* For such a big blockid we need at least a TIFF_LONG per strile */
8222
            /* for the offset array. */
8223
0
            if (strile > filesize / sizeof(uint32_t))
8224
0
            {
8225
0
                TIFFErrorExtR(tif, module, "File too short");
8226
0
                return 0;
8227
0
            }
8228
0
        }
8229
8230
0
        if (td->td_stripoffsetbyteallocsize == 0 &&
8231
0
            td->td_nstrips < 1024 * 1024)
8232
0
        {
8233
0
            nStripArrayAllocNew = td->td_nstrips;
8234
0
        }
8235
0
        else
8236
0
        {
8237
0
#define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
8238
0
#define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
8239
0
            nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
8240
0
            if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
8241
0
                nStripArrayAllocNew *= 2;
8242
0
            nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
8243
0
        }
8244
0
        assert(strile < nStripArrayAllocNew);
8245
0
        nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
8246
0
        nArraySize = (size_t)(nArraySize64);
8247
#if SIZEOF_SIZE_T == 4
8248
        if (nArraySize != nArraySize64)
8249
        {
8250
            TIFFErrorExtR(tif, module,
8251
                          "Cannot allocate strip offset and bytecount arrays");
8252
            return 0;
8253
        }
8254
#endif
8255
0
        offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
8256
0
                                                   nArraySize));
8257
0
        bytecountArray = (uint64_t *)(_TIFFreallocExt(
8258
0
            tif, td->td_stripbytecount_p, nArraySize));
8259
0
        if (offsetArray)
8260
0
            td->td_stripoffset_p = offsetArray;
8261
0
        if (bytecountArray)
8262
0
            td->td_stripbytecount_p = bytecountArray;
8263
0
        if (offsetArray && bytecountArray)
8264
0
        {
8265
0
            td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
8266
            /* Initialize new entries to ~0 / -1 */
8267
            /* coverity[overrun-buffer-arg] */
8268
0
            memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
8269
0
                   (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
8270
0
                       sizeof(uint64_t));
8271
            /* coverity[overrun-buffer-arg] */
8272
0
            memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
8273
0
                   (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
8274
0
                       sizeof(uint64_t));
8275
0
        }
8276
0
        else
8277
0
        {
8278
0
            TIFFErrorExtR(tif, module,
8279
0
                          "Cannot allocate strip offset and bytecount arrays");
8280
0
            _TIFFfreeExt(tif, td->td_stripoffset_p);
8281
0
            td->td_stripoffset_p = NULL;
8282
0
            _TIFFfreeExt(tif, td->td_stripbytecount_p);
8283
0
            td->td_stripbytecount_p = NULL;
8284
0
            td->td_stripoffsetbyteallocsize = 0;
8285
0
        }
8286
0
    }
8287
0
    if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
8288
0
        return 0;
8289
8290
0
    if (~((*parray)[strile]) == 0)
8291
0
    {
8292
0
        if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray))
8293
0
        {
8294
0
            (*parray)[strile] = 0;
8295
0
            return 0;
8296
0
        }
8297
0
    }
8298
8299
0
    return 1;
8300
0
}
8301
8302
static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
8303
                                                     TIFFDirEntry *dirent,
8304
                                                     uint64_t **parray,
8305
                                                     int *pbErr)
8306
4.21M
{
8307
4.21M
    TIFFDirectory *td = &tif->tif_dir;
8308
4.21M
    if (pbErr)
8309
0
        *pbErr = 0;
8310
4.21M
    if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
8311
4.21M
        !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
8312
0
    {
8313
0
        if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD) ||
8314
            /* If the values may fit in the toff_long/toff_long8 member */
8315
            /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
8316
0
            dirent->tdir_count <= 4)
8317
0
        {
8318
0
            if (!_TIFFFillStriles(tif))
8319
0
            {
8320
0
                if (pbErr)
8321
0
                    *pbErr = 1;
8322
                /* Do not return, as we want this function to always */
8323
                /* return the same value if called several times with */
8324
                /* the same arguments */
8325
0
            }
8326
0
        }
8327
0
        else
8328
0
        {
8329
0
            if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
8330
0
            {
8331
0
                if (pbErr)
8332
0
                    *pbErr = 1;
8333
0
                return 0;
8334
0
            }
8335
0
        }
8336
0
    }
8337
4.21M
    if (*parray == NULL || strile >= td->td_nstrips)
8338
0
    {
8339
0
        if (pbErr)
8340
0
            *pbErr = 1;
8341
0
        return 0;
8342
0
    }
8343
4.21M
    return (*parray)[strile];
8344
4.21M
}
8345
8346
/* Return the value of the TileOffsets/StripOffsets array for the specified
8347
 * tile/strile */
8348
uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
8349
2.76M
{
8350
2.76M
    return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
8351
2.76M
}
8352
8353
/* Return the value of the TileOffsets/StripOffsets array for the specified
8354
 * tile/strile */
8355
uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8356
2.76M
{
8357
2.76M
    TIFFDirectory *td = &tif->tif_dir;
8358
2.76M
    return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
8359
2.76M
                                                &(td->td_stripoffset_entry),
8360
2.76M
                                                &(td->td_stripoffset_p), pbErr);
8361
2.76M
}
8362
8363
/* Return the value of the TileByteCounts/StripByteCounts array for the
8364
 * specified tile/strile */
8365
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
8366
1.44M
{
8367
1.44M
    return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
8368
1.44M
}
8369
8370
/* Return the value of the TileByteCounts/StripByteCounts array for the
8371
 * specified tile/strile */
8372
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8373
1.44M
{
8374
1.44M
    TIFFDirectory *td = &tif->tif_dir;
8375
1.44M
    return _TIFFGetStrileOffsetOrByteCountValue(
8376
1.44M
        tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
8377
1.44M
        pbErr);
8378
1.44M
}
8379
8380
71.3k
int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
8381
8382
static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
8383
130k
{
8384
130k
    register TIFFDirectory *td = &tif->tif_dir;
8385
130k
    int return_value = 1;
8386
8387
    /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
8388
130k
    if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
8389
130k
        (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
8390
130k
        return 1;
8391
8392
0
    if (tif->tif_flags & TIFF_LAZYSTRILELOAD)
8393
0
    {
8394
        /* In case of lazy loading, reload completely the arrays */
8395
0
        _TIFFfreeExt(tif, td->td_stripoffset_p);
8396
0
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
8397
0
        td->td_stripoffset_p = NULL;
8398
0
        td->td_stripbytecount_p = NULL;
8399
0
        td->td_stripoffsetbyteallocsize = 0;
8400
0
        tif->tif_flags &= ~TIFF_LAZYSTRILELOAD;
8401
0
    }
8402
8403
    /* If stripoffset array is already loaded, exit with success */
8404
0
    if (td->td_stripoffset_p != NULL)
8405
0
        return 1;
8406
8407
    /* If tdir_count was canceled, then we already got there, but in error */
8408
0
    if (td->td_stripoffset_entry.tdir_count == 0)
8409
0
        return 0;
8410
8411
0
    if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
8412
0
                             &td->td_stripoffset_p))
8413
0
    {
8414
0
        return_value = 0;
8415
0
    }
8416
8417
0
    if (loadStripByteCount &&
8418
0
        !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
8419
0
                             td->td_nstrips, &td->td_stripbytecount_p))
8420
0
    {
8421
0
        return_value = 0;
8422
0
    }
8423
8424
0
    _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
8425
0
    _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
8426
8427
#ifdef STRIPBYTECOUNTSORTED_UNUSED
8428
    if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
8429
    {
8430
        uint32_t strip;
8431
8432
        tif->tif_dir.td_stripbytecountsorted = 1;
8433
        for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
8434
        {
8435
            if (tif->tif_dir.td_stripoffset_p[strip - 1] >
8436
                tif->tif_dir.td_stripoffset_p[strip])
8437
            {
8438
                tif->tif_dir.td_stripbytecountsorted = 0;
8439
                break;
8440
            }
8441
        }
8442
    }
8443
#endif
8444
8445
0
    return return_value;
8446
0
}