Coverage Report

Created: 2025-09-27 07:21

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