Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_dirread.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library.
27
 *
28
 * Directory Read Support Routines.
29
 */
30
31
/* Suggested pending improvements:
32
 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33
 *   the pointer to the appropriate TIFFField structure early on in
34
 *   TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
35
 */
36
37
#include "tiffconf.h"
38
#include "tiffiop.h"
39
#include <float.h>
40
#include <limits.h>
41
#include <stdlib.h>
42
#include <string.h>
43
44
13.6M
#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
108k
{
284
108k
    UInt64Aligned_t result;
285
286
108k
    result.c[0] = value[0];
287
108k
    result.c[1] = value[1];
288
108k
    result.c[2] = value[2];
289
108k
    result.c[3] = value[3];
290
108k
    result.c[4] = value[4];
291
108k
    result.c[5] = value[5];
292
108k
    result.c[6] = value[6];
293
108k
    result.c[7] = value[7];
294
295
108k
    return result.l;
296
108k
}
297
298
static enum TIFFReadDirEntryErr
299
TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
300
11.2k
{
301
11.2k
    enum TIFFReadDirEntryErr err;
302
11.2k
    if (direntry->tdir_count != 1)
303
4.24k
        return (TIFFReadDirEntryErrCount);
304
6.97k
    switch (direntry->tdir_type)
305
6.97k
    {
306
1.11k
        case TIFF_BYTE:
307
1.25k
        case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
308
                                field_readcount==1 */
309
1.25k
            TIFFReadDirEntryCheckedByte(tif, direntry, value);
310
1.25k
            return (TIFFReadDirEntryErrOk);
311
191
        case TIFF_SBYTE:
312
191
        {
313
191
            int8_t m;
314
191
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
315
191
            err = TIFFReadDirEntryCheckRangeByteSbyte(m);
316
191
            if (err != TIFFReadDirEntryErrOk)
317
86
                return (err);
318
105
            *value = (uint8_t)m;
319
105
            return (TIFFReadDirEntryErrOk);
320
191
        }
321
621
        case TIFF_SHORT:
322
621
        {
323
621
            uint16_t m;
324
621
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
325
621
            err = TIFFReadDirEntryCheckRangeByteShort(m);
326
621
            if (err != TIFFReadDirEntryErrOk)
327
312
                return (err);
328
309
            *value = (uint8_t)m;
329
309
            return (TIFFReadDirEntryErrOk);
330
621
        }
331
373
        case TIFF_SSHORT:
332
373
        {
333
373
            int16_t m;
334
373
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
335
373
            err = TIFFReadDirEntryCheckRangeByteSshort(m);
336
373
            if (err != TIFFReadDirEntryErrOk)
337
287
                return (err);
338
86
            *value = (uint8_t)m;
339
86
            return (TIFFReadDirEntryErrOk);
340
373
        }
341
458
        case TIFF_LONG:
342
458
        {
343
458
            uint32_t m;
344
458
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
345
458
            err = TIFFReadDirEntryCheckRangeByteLong(m);
346
458
            if (err != TIFFReadDirEntryErrOk)
347
337
                return (err);
348
121
            *value = (uint8_t)m;
349
121
            return (TIFFReadDirEntryErrOk);
350
458
        }
351
591
        case TIFF_SLONG:
352
591
        {
353
591
            int32_t m;
354
591
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
355
591
            err = TIFFReadDirEntryCheckRangeByteSlong(m);
356
591
            if (err != TIFFReadDirEntryErrOk)
357
465
                return (err);
358
126
            *value = (uint8_t)m;
359
126
            return (TIFFReadDirEntryErrOk);
360
591
        }
361
887
        case TIFF_LONG8:
362
887
        {
363
887
            uint64_t m;
364
887
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
365
887
            if (err != TIFFReadDirEntryErrOk)
366
169
                return (err);
367
718
            err = TIFFReadDirEntryCheckRangeByteLong8(m);
368
718
            if (err != TIFFReadDirEntryErrOk)
369
654
                return (err);
370
64
            *value = (uint8_t)m;
371
64
            return (TIFFReadDirEntryErrOk);
372
718
        }
373
2.21k
        case TIFF_SLONG8:
374
2.21k
        {
375
2.21k
            int64_t m;
376
2.21k
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
377
2.21k
            if (err != TIFFReadDirEntryErrOk)
378
1.06k
                return (err);
379
1.14k
            err = TIFFReadDirEntryCheckRangeByteSlong8(m);
380
1.14k
            if (err != TIFFReadDirEntryErrOk)
381
1.04k
                return (err);
382
106
            *value = (uint8_t)m;
383
106
            return (TIFFReadDirEntryErrOk);
384
1.14k
        }
385
392
        default:
386
392
            return (TIFFReadDirEntryErrType);
387
6.97k
    }
388
6.97k
}
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
514k
{
487
514k
    enum TIFFReadDirEntryErr err;
488
514k
    if (direntry->tdir_count != 1)
489
69.0k
        return (TIFFReadDirEntryErrCount);
490
445k
    switch (direntry->tdir_type)
491
445k
    {
492
21.0k
        case TIFF_BYTE:
493
21.0k
        {
494
21.0k
            uint8_t m;
495
21.0k
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
496
21.0k
            *value = (uint16_t)m;
497
21.0k
            return (TIFFReadDirEntryErrOk);
498
0
        }
499
15.7k
        case TIFF_SBYTE:
500
15.7k
        {
501
15.7k
            int8_t m;
502
15.7k
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
503
15.7k
            err = TIFFReadDirEntryCheckRangeShortSbyte(m);
504
15.7k
            if (err != TIFFReadDirEntryErrOk)
505
147
                return (err);
506
15.6k
            *value = (uint16_t)m;
507
15.6k
            return (TIFFReadDirEntryErrOk);
508
15.7k
        }
509
364k
        case TIFF_SHORT:
510
364k
            TIFFReadDirEntryCheckedShort(tif, direntry, value);
511
364k
            return (TIFFReadDirEntryErrOk);
512
10.7k
        case TIFF_SSHORT:
513
10.7k
        {
514
10.7k
            int16_t m;
515
10.7k
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
516
10.7k
            err = TIFFReadDirEntryCheckRangeShortSshort(m);
517
10.7k
            if (err != TIFFReadDirEntryErrOk)
518
163
                return (err);
519
10.6k
            *value = (uint16_t)m;
520
10.6k
            return (TIFFReadDirEntryErrOk);
521
10.7k
        }
522
21.2k
        case TIFF_LONG:
523
21.2k
        {
524
21.2k
            uint32_t m;
525
21.2k
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
526
21.2k
            err = TIFFReadDirEntryCheckRangeShortLong(m);
527
21.2k
            if (err != TIFFReadDirEntryErrOk)
528
685
                return (err);
529
20.5k
            *value = (uint16_t)m;
530
20.5k
            return (TIFFReadDirEntryErrOk);
531
21.2k
        }
532
3.95k
        case TIFF_SLONG:
533
3.95k
        {
534
3.95k
            int32_t m;
535
3.95k
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
536
3.95k
            err = TIFFReadDirEntryCheckRangeShortSlong(m);
537
3.95k
            if (err != TIFFReadDirEntryErrOk)
538
514
                return (err);
539
3.44k
            *value = (uint16_t)m;
540
3.44k
            return (TIFFReadDirEntryErrOk);
541
3.95k
        }
542
1.65k
        case TIFF_LONG8:
543
1.65k
        {
544
1.65k
            uint64_t m;
545
1.65k
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
546
1.65k
            if (err != TIFFReadDirEntryErrOk)
547
320
                return (err);
548
1.33k
            err = TIFFReadDirEntryCheckRangeShortLong8(m);
549
1.33k
            if (err != TIFFReadDirEntryErrOk)
550
1.02k
                return (err);
551
316
            *value = (uint16_t)m;
552
316
            return (TIFFReadDirEntryErrOk);
553
1.33k
        }
554
2.01k
        case TIFF_SLONG8:
555
2.01k
        {
556
2.01k
            int64_t m;
557
2.01k
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
558
2.01k
            if (err != TIFFReadDirEntryErrOk)
559
466
                return (err);
560
1.54k
            err = TIFFReadDirEntryCheckRangeShortSlong8(m);
561
1.54k
            if (err != TIFFReadDirEntryErrOk)
562
1.14k
                return (err);
563
405
            *value = (uint16_t)m;
564
405
            return (TIFFReadDirEntryErrOk);
565
1.54k
        }
566
4.95k
        default:
567
4.95k
            return (TIFFReadDirEntryErrType);
568
445k
    }
569
445k
} /*-- 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
466k
{
658
466k
    enum TIFFReadDirEntryErr err;
659
466k
    if (direntry->tdir_count != 1)
660
22.0k
        return (TIFFReadDirEntryErrCount);
661
444k
    switch (direntry->tdir_type)
662
444k
    {
663
44.6k
        case TIFF_BYTE:
664
44.6k
        {
665
44.6k
            uint8_t m;
666
44.6k
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
667
44.6k
            *value = (uint32_t)m;
668
44.6k
            return (TIFFReadDirEntryErrOk);
669
0
        }
670
4.25k
        case TIFF_SBYTE:
671
4.25k
        {
672
4.25k
            int8_t m;
673
4.25k
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
674
4.25k
            err = TIFFReadDirEntryCheckRangeLongSbyte(m);
675
4.25k
            if (err != TIFFReadDirEntryErrOk)
676
160
                return (err);
677
4.09k
            *value = (uint32_t)m;
678
4.09k
            return (TIFFReadDirEntryErrOk);
679
4.25k
        }
680
254k
        case TIFF_SHORT:
681
254k
        {
682
254k
            uint16_t m;
683
254k
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
684
254k
            *value = (uint32_t)m;
685
254k
            return (TIFFReadDirEntryErrOk);
686
4.25k
        }
687
17.3k
        case TIFF_SSHORT:
688
17.3k
        {
689
17.3k
            int16_t m;
690
17.3k
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
691
17.3k
            err = TIFFReadDirEntryCheckRangeLongSshort(m);
692
17.3k
            if (err != TIFFReadDirEntryErrOk)
693
168
                return (err);
694
17.1k
            *value = (uint32_t)m;
695
17.1k
            return (TIFFReadDirEntryErrOk);
696
17.3k
        }
697
103k
        case TIFF_LONG:
698
103k
            TIFFReadDirEntryCheckedLong(tif, direntry, value);
699
103k
            return (TIFFReadDirEntryErrOk);
700
11.9k
        case TIFF_SLONG:
701
11.9k
        {
702
11.9k
            int32_t m;
703
11.9k
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
704
11.9k
            err = TIFFReadDirEntryCheckRangeLongSlong(m);
705
11.9k
            if (err != TIFFReadDirEntryErrOk)
706
240
                return (err);
707
11.6k
            *value = (uint32_t)m;
708
11.6k
            return (TIFFReadDirEntryErrOk);
709
11.9k
        }
710
3.35k
        case TIFF_LONG8:
711
3.35k
        {
712
3.35k
            uint64_t m;
713
3.35k
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
714
3.35k
            if (err != TIFFReadDirEntryErrOk)
715
347
                return (err);
716
3.00k
            err = TIFFReadDirEntryCheckRangeLongLong8(m);
717
3.00k
            if (err != TIFFReadDirEntryErrOk)
718
531
                return (err);
719
2.47k
            *value = (uint32_t)m;
720
2.47k
            return (TIFFReadDirEntryErrOk);
721
3.00k
        }
722
2.45k
        case TIFF_SLONG8:
723
2.45k
        {
724
2.45k
            int64_t m;
725
2.45k
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
726
2.45k
            if (err != TIFFReadDirEntryErrOk)
727
545
                return (err);
728
1.91k
            err = TIFFReadDirEntryCheckRangeLongSlong8(m);
729
1.91k
            if (err != TIFFReadDirEntryErrOk)
730
1.00k
                return (err);
731
906
            *value = (uint32_t)m;
732
906
            return (TIFFReadDirEntryErrOk);
733
1.91k
        }
734
2.58k
        default:
735
2.58k
            return (TIFFReadDirEntryErrType);
736
444k
    }
737
444k
} /*-- 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
12.4k
{
820
12.4k
    enum TIFFReadDirEntryErr err;
821
12.4k
    if (direntry->tdir_count != 1)
822
3.39k
        return (TIFFReadDirEntryErrCount);
823
9.08k
    switch (direntry->tdir_type)
824
9.08k
    {
825
500
        case TIFF_BYTE:
826
500
        {
827
500
            uint8_t m;
828
500
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
829
500
            *value = (uint64_t)m;
830
500
            return (TIFFReadDirEntryErrOk);
831
0
        }
832
514
        case TIFF_SBYTE:
833
514
        {
834
514
            int8_t m;
835
514
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
836
514
            err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
837
514
            if (err != TIFFReadDirEntryErrOk)
838
103
                return (err);
839
411
            *value = (uint64_t)m;
840
411
            return (TIFFReadDirEntryErrOk);
841
514
        }
842
1.58k
        case TIFF_SHORT:
843
1.58k
        {
844
1.58k
            uint16_t m;
845
1.58k
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
846
1.58k
            *value = (uint64_t)m;
847
1.58k
            return (TIFFReadDirEntryErrOk);
848
514
        }
849
686
        case TIFF_SSHORT:
850
686
        {
851
686
            int16_t m;
852
686
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
853
686
            err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
854
686
            if (err != TIFFReadDirEntryErrOk)
855
136
                return (err);
856
550
            *value = (uint64_t)m;
857
550
            return (TIFFReadDirEntryErrOk);
858
686
        }
859
2.36k
        case TIFF_LONG:
860
2.36k
        {
861
2.36k
            uint32_t m;
862
2.36k
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
863
2.36k
            *value = (uint64_t)m;
864
2.36k
            return (TIFFReadDirEntryErrOk);
865
686
        }
866
438
        case TIFF_SLONG:
867
438
        {
868
438
            int32_t m;
869
438
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
870
438
            err = TIFFReadDirEntryCheckRangeLong8Slong(m);
871
438
            if (err != TIFFReadDirEntryErrOk)
872
103
                return (err);
873
335
            *value = (uint64_t)m;
874
335
            return (TIFFReadDirEntryErrOk);
875
438
        }
876
1.44k
        case TIFF_LONG8:
877
1.44k
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
878
1.44k
            return (err);
879
1.11k
        case TIFF_SLONG8:
880
1.11k
        {
881
1.11k
            int64_t m;
882
1.11k
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
883
1.11k
            if (err != TIFFReadDirEntryErrOk)
884
151
                return (err);
885
959
            err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
886
959
            if (err != TIFFReadDirEntryErrOk)
887
524
                return (err);
888
435
            *value = (uint64_t)m;
889
435
            return (TIFFReadDirEntryErrOk);
890
959
        }
891
442
        default:
892
442
            return (TIFFReadDirEntryErrType);
893
9.08k
    }
894
9.08k
} /*-- 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
47.7k
{
969
47.7k
    enum TIFFReadDirEntryErr err;
970
47.7k
    if (direntry->tdir_count != 1)
971
22.9k
        return (TIFFReadDirEntryErrCount);
972
24.8k
    switch (direntry->tdir_type)
973
24.8k
    {
974
535
        case TIFF_BYTE:
975
535
        {
976
535
            uint8_t m;
977
535
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
978
535
            *value = (float)m;
979
535
            return (TIFFReadDirEntryErrOk);
980
0
        }
981
213
        case TIFF_SBYTE:
982
213
        {
983
213
            int8_t m;
984
213
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
985
213
            *value = (float)m;
986
213
            return (TIFFReadDirEntryErrOk);
987
0
        }
988
4.16k
        case TIFF_SHORT:
989
4.16k
        {
990
4.16k
            uint16_t m;
991
4.16k
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
992
4.16k
            *value = (float)m;
993
4.16k
            return (TIFFReadDirEntryErrOk);
994
0
        }
995
423
        case TIFF_SSHORT:
996
423
        {
997
423
            int16_t m;
998
423
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
999
423
            *value = (float)m;
1000
423
            return (TIFFReadDirEntryErrOk);
1001
0
        }
1002
1.06k
        case TIFF_LONG:
1003
1.06k
        {
1004
1.06k
            uint32_t m;
1005
1.06k
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1006
1.06k
            *value = (float)m;
1007
1.06k
            return (TIFFReadDirEntryErrOk);
1008
0
        }
1009
785
        case TIFF_SLONG:
1010
785
        {
1011
785
            int32_t m;
1012
785
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1013
785
            *value = (float)m;
1014
785
            return (TIFFReadDirEntryErrOk);
1015
0
        }
1016
627
        case TIFF_LONG8:
1017
627
        {
1018
627
            uint64_t m;
1019
627
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1020
627
            if (err != TIFFReadDirEntryErrOk)
1021
178
                return (err);
1022
449
            *value = (float)m;
1023
449
            return (TIFFReadDirEntryErrOk);
1024
627
        }
1025
996
        case TIFF_SLONG8:
1026
996
        {
1027
996
            int64_t m;
1028
996
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1029
996
            if (err != TIFFReadDirEntryErrOk)
1030
574
                return (err);
1031
422
            *value = (float)m;
1032
422
            return (TIFFReadDirEntryErrOk);
1033
996
        }
1034
9.08k
        case TIFF_RATIONAL:
1035
9.08k
        {
1036
9.08k
            double m;
1037
9.08k
            err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
1038
9.08k
            if (err != TIFFReadDirEntryErrOk)
1039
2.59k
                return (err);
1040
6.48k
            *value = (float)m;
1041
6.48k
            return (TIFFReadDirEntryErrOk);
1042
9.08k
        }
1043
2.66k
        case TIFF_SRATIONAL:
1044
2.66k
        {
1045
2.66k
            double m;
1046
2.66k
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
1047
2.66k
            if (err != TIFFReadDirEntryErrOk)
1048
1.07k
                return (err);
1049
1.58k
            *value = (float)m;
1050
1.58k
            return (TIFFReadDirEntryErrOk);
1051
2.66k
        }
1052
848
        case TIFF_FLOAT:
1053
848
            TIFFReadDirEntryCheckedFloat(tif, direntry, value);
1054
848
            return (TIFFReadDirEntryErrOk);
1055
1.02k
        case TIFF_DOUBLE:
1056
1.02k
        {
1057
1.02k
            double m;
1058
1.02k
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
1059
1.02k
            if (err != TIFFReadDirEntryErrOk)
1060
367
                return (err);
1061
657
            if ((m > FLT_MAX) || (m < -FLT_MAX))
1062
303
                return (TIFFReadDirEntryErrRange);
1063
354
            *value = (float)m;
1064
354
            return (TIFFReadDirEntryErrOk);
1065
657
        }
1066
2.43k
        default:
1067
2.43k
            return (TIFFReadDirEntryErrType);
1068
24.8k
    }
1069
24.8k
}
1070
1071
static enum TIFFReadDirEntryErr
1072
TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
1073
5.03k
{
1074
5.03k
    enum TIFFReadDirEntryErr err;
1075
5.03k
    if (direntry->tdir_count != 1)
1076
2.72k
        return (TIFFReadDirEntryErrCount);
1077
2.31k
    switch (direntry->tdir_type)
1078
2.31k
    {
1079
69
        case TIFF_BYTE:
1080
69
        {
1081
69
            uint8_t m;
1082
69
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1083
69
            *value = (double)m;
1084
69
            return (TIFFReadDirEntryErrOk);
1085
0
        }
1086
55
        case TIFF_SBYTE:
1087
55
        {
1088
55
            int8_t m;
1089
55
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1090
55
            *value = (double)m;
1091
55
            return (TIFFReadDirEntryErrOk);
1092
0
        }
1093
177
        case TIFF_SHORT:
1094
177
        {
1095
177
            uint16_t m;
1096
177
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1097
177
            *value = (double)m;
1098
177
            return (TIFFReadDirEntryErrOk);
1099
0
        }
1100
98
        case TIFF_SSHORT:
1101
98
        {
1102
98
            int16_t m;
1103
98
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1104
98
            *value = (double)m;
1105
98
            return (TIFFReadDirEntryErrOk);
1106
0
        }
1107
159
        case TIFF_LONG:
1108
159
        {
1109
159
            uint32_t m;
1110
159
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1111
159
            *value = (double)m;
1112
159
            return (TIFFReadDirEntryErrOk);
1113
0
        }
1114
125
        case TIFF_SLONG:
1115
125
        {
1116
125
            int32_t m;
1117
125
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1118
125
            *value = (double)m;
1119
125
            return (TIFFReadDirEntryErrOk);
1120
0
        }
1121
140
        case TIFF_LONG8:
1122
140
        {
1123
140
            uint64_t m;
1124
140
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1125
140
            if (err != TIFFReadDirEntryErrOk)
1126
65
                return (err);
1127
75
            *value = (double)m;
1128
75
            return (TIFFReadDirEntryErrOk);
1129
140
        }
1130
135
        case TIFF_SLONG8:
1131
135
        {
1132
135
            int64_t m;
1133
135
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1134
135
            if (err != TIFFReadDirEntryErrOk)
1135
58
                return (err);
1136
77
            *value = (double)m;
1137
77
            return (TIFFReadDirEntryErrOk);
1138
135
        }
1139
866
        case TIFF_RATIONAL:
1140
866
            err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
1141
866
            return (err);
1142
101
        case TIFF_SRATIONAL:
1143
101
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
1144
101
            return (err);
1145
127
        case TIFF_FLOAT:
1146
127
        {
1147
127
            float m;
1148
127
            TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
1149
127
            *value = (double)m;
1150
127
            return (TIFFReadDirEntryErrOk);
1151
135
        }
1152
83
        case TIFF_DOUBLE:
1153
83
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
1154
83
            return (err);
1155
179
        default:
1156
179
            return (TIFFReadDirEntryErrType);
1157
2.31k
    }
1158
2.31k
}
1159
1160
static enum TIFFReadDirEntryErr
1161
TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1162
88.9k
{
1163
88.9k
    enum TIFFReadDirEntryErr err;
1164
88.9k
    if (direntry->tdir_count != 1)
1165
6.68k
        return (TIFFReadDirEntryErrCount);
1166
82.2k
    switch (direntry->tdir_type)
1167
82.2k
    {
1168
74.4k
        case TIFF_LONG:
1169
80.1k
        case TIFF_IFD:
1170
80.1k
        {
1171
80.1k
            uint32_t m;
1172
80.1k
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1173
80.1k
            *value = (uint64_t)m;
1174
80.1k
            return (TIFFReadDirEntryErrOk);
1175
74.4k
        }
1176
573
        case TIFF_LONG8:
1177
673
        case TIFF_IFD8:
1178
673
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1179
673
            return (err);
1180
1.41k
        default:
1181
1.41k
            return (TIFFReadDirEntryErrType);
1182
82.2k
    }
1183
82.2k
}
1184
1185
0
#define INITIAL_THRESHOLD (1024 * 1024)
1186
0
#define THRESHOLD_MULTIPLIER 10
1187
#define MAX_THRESHOLD                                                          \
1188
0
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
1189
0
     INITIAL_THRESHOLD)
1190
1191
static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
1192
                                                               uint64_t offset,
1193
                                                               tmsize_t size,
1194
                                                               void **pdest)
1195
0
{
1196
0
#if SIZEOF_SIZE_T == 8
1197
0
    tmsize_t threshold = INITIAL_THRESHOLD;
1198
0
#endif
1199
0
    tmsize_t already_read = 0;
1200
1201
0
    assert(!isMapped(tif));
1202
1203
0
    if (!SeekOK(tif, offset))
1204
0
        return (TIFFReadDirEntryErrIo);
1205
1206
    /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
1207
    /* so as to avoid allocating too much memory in case the file is too */
1208
    /* short. We could ask for the file size, but this might be */
1209
    /* expensive with some I/O layers (think of reading a gzipped file) */
1210
    /* Restrict to 64 bit processes, so as to avoid reallocs() */
1211
    /* on 32 bit processes where virtual memory is scarce.  */
1212
0
    while (already_read < size)
1213
0
    {
1214
0
        void *new_dest;
1215
0
        tmsize_t bytes_read;
1216
0
        tmsize_t to_read = size - already_read;
1217
0
#if SIZEOF_SIZE_T == 8
1218
0
        if (to_read >= threshold && threshold < MAX_THRESHOLD)
1219
0
        {
1220
0
            to_read = threshold;
1221
0
            threshold *= THRESHOLD_MULTIPLIER;
1222
0
        }
1223
0
#endif
1224
1225
0
        new_dest =
1226
0
            (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
1227
0
        if (new_dest == NULL)
1228
0
        {
1229
0
            TIFFErrorExtR(tif, tif->tif_name,
1230
0
                          "Failed to allocate memory for %s "
1231
0
                          "(%" TIFF_SSIZE_FORMAT
1232
0
                          " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
1233
0
                          "TIFFReadDirEntryArray", (tmsize_t)1,
1234
0
                          already_read + to_read);
1235
0
            return TIFFReadDirEntryErrAlloc;
1236
0
        }
1237
0
        *pdest = new_dest;
1238
1239
0
        bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
1240
0
        already_read += bytes_read;
1241
0
        if (bytes_read != to_read)
1242
0
        {
1243
0
            return TIFFReadDirEntryErrIo;
1244
0
        }
1245
0
    }
1246
0
    return TIFFReadDirEntryErrOk;
1247
0
}
1248
1249
/* Caution: if raising that value, make sure int32 / uint32 overflows can't
1250
 * occur elsewhere */
1251
2.20M
#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
1.27M
{
1258
1.27M
    int typesize;
1259
1.27M
    uint32_t datasize;
1260
1.27M
    void *data;
1261
1.27M
    uint64_t target_count64;
1262
1.27M
    int original_datasize_clamped;
1263
1.27M
    typesize = TIFFDataWidth(direntry->tdir_type);
1264
1265
1.27M
    target_count64 =
1266
1.27M
        (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
1267
1268
1.27M
    if ((target_count64 == 0) || (typesize == 0))
1269
101k
    {
1270
101k
        *value = 0;
1271
101k
        return (TIFFReadDirEntryErrOk);
1272
101k
    }
1273
1.17M
    (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
1.17M
    original_datasize_clamped =
1279
1.17M
        ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
1280
1.17M
        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
1.17M
    if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64)
1288
147k
        return (TIFFReadDirEntryErrSizesan);
1289
1.02M
    if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
1290
2.30k
        return (TIFFReadDirEntryErrSizesan);
1291
1292
1.02M
    *count = (uint32_t)target_count64;
1293
1.02M
    datasize = (*count) * typesize;
1294
1.02M
    assert((tmsize_t)datasize > 0);
1295
1296
1.02M
    if (datasize > 100 * 1024 * 1024)
1297
107k
    {
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
107k
        const uint64_t filesize = TIFFGetFileSize(tif);
1302
107k
        if (datasize > filesize)
1303
107k
        {
1304
107k
            TIFFWarningExtR(tif, "ReadDirEntryArray",
1305
107k
                            "Requested memory size for tag %d (0x%x) %" PRIu32
1306
107k
                            " is greater than filesize %" PRIu64
1307
107k
                            ". Memory not allocated, tag not read",
1308
107k
                            direntry->tdir_tag, direntry->tdir_tag, datasize,
1309
107k
                            filesize);
1310
107k
            return (TIFFReadDirEntryErrAlloc);
1311
107k
        }
1312
107k
    }
1313
1314
916k
    if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
1315
275k
        return TIFFReadDirEntryErrIo;
1316
1317
640k
    if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
1318
4.72k
                           (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
1319
0
    {
1320
0
        data = NULL;
1321
0
    }
1322
640k
    else
1323
640k
    {
1324
640k
        data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
1325
640k
        if (data == 0)
1326
0
            return (TIFFReadDirEntryErrAlloc);
1327
640k
    }
1328
640k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
1329
637k
    {
1330
        /* Only the condition on original_datasize_clamped. The second
1331
         * one is implied, but Coverity Scan cannot see it. */
1332
637k
        if (original_datasize_clamped <= 4 && datasize <= 4)
1333
306k
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1334
330k
        else
1335
330k
        {
1336
330k
            enum TIFFReadDirEntryErr err;
1337
330k
            uint32_t offset = direntry->tdir_offset.toff_long;
1338
330k
            if (tif->tif_flags & TIFF_SWAB)
1339
151k
                TIFFSwabLong(&offset);
1340
330k
            if (isMapped(tif))
1341
330k
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1342
330k
                                           (tmsize_t)datasize, data);
1343
0
            else
1344
0
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1345
0
                                                     (tmsize_t)datasize, &data);
1346
330k
            if (err != TIFFReadDirEntryErrOk)
1347
66.6k
            {
1348
66.6k
                _TIFFfreeExt(tif, data);
1349
66.6k
                return (err);
1350
66.6k
            }
1351
330k
        }
1352
637k
    }
1353
2.80k
    else
1354
2.80k
    {
1355
        /* See above comment for the Classic TIFF case */
1356
2.80k
        if (original_datasize_clamped <= 8 && datasize <= 8)
1357
752
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1358
2.05k
        else
1359
2.05k
        {
1360
2.05k
            enum TIFFReadDirEntryErr err;
1361
2.05k
            uint64_t offset = direntry->tdir_offset.toff_long8;
1362
2.05k
            if (tif->tif_flags & TIFF_SWAB)
1363
120
                TIFFSwabLong8(&offset);
1364
2.05k
            if (isMapped(tif))
1365
2.05k
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1366
2.05k
                                           (tmsize_t)datasize, data);
1367
0
            else
1368
0
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1369
0
                                                     (tmsize_t)datasize, &data);
1370
2.05k
            if (err != TIFFReadDirEntryErrOk)
1371
1.13k
            {
1372
1.13k
                _TIFFfreeExt(tif, data);
1373
1.13k
                return (err);
1374
1.13k
            }
1375
2.05k
        }
1376
2.80k
    }
1377
572k
    *value = data;
1378
572k
    return (TIFFReadDirEntryErrOk);
1379
640k
}
1380
1381
static enum TIFFReadDirEntryErr
1382
TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
1383
                      uint32_t desttypesize, void **value)
1384
1.02M
{
1385
1.02M
    return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
1386
1.02M
                                          value, ~((uint64_t)0));
1387
1.02M
}
1388
1389
static enum TIFFReadDirEntryErr
1390
TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
1391
396k
{
1392
396k
    enum TIFFReadDirEntryErr err;
1393
396k
    uint32_t count;
1394
396k
    void *origdata;
1395
396k
    uint8_t *data;
1396
396k
    switch (direntry->tdir_type)
1397
396k
    {
1398
112k
        case TIFF_ASCII:
1399
124k
        case TIFF_UNDEFINED:
1400
325k
        case TIFF_BYTE:
1401
329k
        case TIFF_SBYTE:
1402
343k
        case TIFF_SHORT:
1403
345k
        case TIFF_SSHORT:
1404
349k
        case TIFF_LONG:
1405
352k
        case TIFF_SLONG:
1406
354k
        case TIFF_LONG8:
1407
356k
        case TIFF_SLONG8:
1408
356k
            break;
1409
40.1k
        default:
1410
40.1k
            return (TIFFReadDirEntryErrType);
1411
396k
    }
1412
356k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1413
356k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1414
258k
    {
1415
258k
        *value = 0;
1416
258k
        return (err);
1417
258k
    }
1418
98.0k
    switch (direntry->tdir_type)
1419
98.0k
    {
1420
36.1k
        case TIFF_ASCII:
1421
40.5k
        case TIFF_UNDEFINED:
1422
79.4k
        case TIFF_BYTE:
1423
79.4k
            *value = (uint8_t *)origdata;
1424
79.4k
            return (TIFFReadDirEntryErrOk);
1425
1.87k
        case TIFF_SBYTE:
1426
1.87k
        {
1427
1.87k
            int8_t *m;
1428
1.87k
            uint32_t n;
1429
1.87k
            m = (int8_t *)origdata;
1430
12.6k
            for (n = 0; n < count; n++)
1431
11.3k
            {
1432
11.3k
                err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
1433
11.3k
                if (err != TIFFReadDirEntryErrOk)
1434
601
                {
1435
601
                    _TIFFfreeExt(tif, origdata);
1436
601
                    return (err);
1437
601
                }
1438
10.7k
                m++;
1439
10.7k
            }
1440
1.27k
            *value = (uint8_t *)origdata;
1441
1.27k
            return (TIFFReadDirEntryErrOk);
1442
1.87k
        }
1443
98.0k
    }
1444
16.6k
    data = (uint8_t *)_TIFFmallocExt(tif, count);
1445
16.6k
    if (data == 0)
1446
0
    {
1447
0
        _TIFFfreeExt(tif, origdata);
1448
0
        return (TIFFReadDirEntryErrAlloc);
1449
0
    }
1450
16.6k
    switch (direntry->tdir_type)
1451
16.6k
    {
1452
7.61k
        case TIFF_SHORT:
1453
7.61k
        {
1454
7.61k
            uint16_t *ma;
1455
7.61k
            uint8_t *mb;
1456
7.61k
            uint32_t n;
1457
7.61k
            ma = (uint16_t *)origdata;
1458
7.61k
            mb = data;
1459
16.8k
            for (n = 0; n < count; n++)
1460
11.5k
            {
1461
11.5k
                if (tif->tif_flags & TIFF_SWAB)
1462
4.30k
                    TIFFSwabShort(ma);
1463
11.5k
                err = TIFFReadDirEntryCheckRangeByteShort(*ma);
1464
11.5k
                if (err != TIFFReadDirEntryErrOk)
1465
2.22k
                    break;
1466
9.28k
                *mb++ = (uint8_t)(*ma++);
1467
9.28k
            }
1468
7.61k
        }
1469
7.61k
        break;
1470
1.80k
        case TIFF_SSHORT:
1471
1.80k
        {
1472
1.80k
            int16_t *ma;
1473
1.80k
            uint8_t *mb;
1474
1.80k
            uint32_t n;
1475
1.80k
            ma = (int16_t *)origdata;
1476
1.80k
            mb = data;
1477
8.53k
            for (n = 0; n < count; n++)
1478
8.18k
            {
1479
8.18k
                if (tif->tif_flags & TIFF_SWAB)
1480
4.79k
                    TIFFSwabShort((uint16_t *)ma);
1481
8.18k
                err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
1482
8.18k
                if (err != TIFFReadDirEntryErrOk)
1483
1.45k
                    break;
1484
6.73k
                *mb++ = (uint8_t)(*ma++);
1485
6.73k
            }
1486
1.80k
        }
1487
1.80k
        break;
1488
2.67k
        case TIFF_LONG:
1489
2.67k
        {
1490
2.67k
            uint32_t *ma;
1491
2.67k
            uint8_t *mb;
1492
2.67k
            uint32_t n;
1493
2.67k
            ma = (uint32_t *)origdata;
1494
2.67k
            mb = data;
1495
6.67k
            for (n = 0; n < count; n++)
1496
5.55k
            {
1497
5.55k
                if (tif->tif_flags & TIFF_SWAB)
1498
3.41k
                    TIFFSwabLong(ma);
1499
5.55k
                err = TIFFReadDirEntryCheckRangeByteLong(*ma);
1500
5.55k
                if (err != TIFFReadDirEntryErrOk)
1501
1.56k
                    break;
1502
3.99k
                *mb++ = (uint8_t)(*ma++);
1503
3.99k
            }
1504
2.67k
        }
1505
2.67k
        break;
1506
1.39k
        case TIFF_SLONG:
1507
1.39k
        {
1508
1.39k
            int32_t *ma;
1509
1.39k
            uint8_t *mb;
1510
1.39k
            uint32_t n;
1511
1.39k
            ma = (int32_t *)origdata;
1512
1.39k
            mb = data;
1513
7.40k
            for (n = 0; n < count; n++)
1514
7.12k
            {
1515
7.12k
                if (tif->tif_flags & TIFF_SWAB)
1516
3.52k
                    TIFFSwabLong((uint32_t *)ma);
1517
7.12k
                err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
1518
7.12k
                if (err != TIFFReadDirEntryErrOk)
1519
1.11k
                    break;
1520
6.01k
                *mb++ = (uint8_t)(*ma++);
1521
6.01k
            }
1522
1.39k
        }
1523
1.39k
        break;
1524
1.27k
        case TIFF_LONG8:
1525
1.27k
        {
1526
1.27k
            uint64_t *ma;
1527
1.27k
            uint8_t *mb;
1528
1.27k
            uint32_t n;
1529
1.27k
            ma = (uint64_t *)origdata;
1530
1.27k
            mb = data;
1531
4.62k
            for (n = 0; n < count; n++)
1532
4.43k
            {
1533
4.43k
                if (tif->tif_flags & TIFF_SWAB)
1534
2.28k
                    TIFFSwabLong8(ma);
1535
4.43k
                err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
1536
4.43k
                if (err != TIFFReadDirEntryErrOk)
1537
1.08k
                    break;
1538
3.34k
                *mb++ = (uint8_t)(*ma++);
1539
3.34k
            }
1540
1.27k
        }
1541
1.27k
        break;
1542
1.91k
        case TIFF_SLONG8:
1543
1.91k
        {
1544
1.91k
            int64_t *ma;
1545
1.91k
            uint8_t *mb;
1546
1.91k
            uint32_t n;
1547
1.91k
            ma = (int64_t *)origdata;
1548
1.91k
            mb = data;
1549
5.82k
            for (n = 0; n < count; n++)
1550
5.52k
            {
1551
5.52k
                if (tif->tif_flags & TIFF_SWAB)
1552
2.70k
                    TIFFSwabLong8((uint64_t *)ma);
1553
5.52k
                err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1554
5.52k
                if (err != TIFFReadDirEntryErrOk)
1555
1.60k
                    break;
1556
3.91k
                *mb++ = (uint8_t)(*ma++);
1557
3.91k
            }
1558
1.91k
        }
1559
1.91k
        break;
1560
16.6k
    }
1561
16.6k
    _TIFFfreeExt(tif, origdata);
1562
16.6k
    if (err != TIFFReadDirEntryErrOk)
1563
9.05k
    {
1564
9.05k
        _TIFFfreeExt(tif, data);
1565
9.05k
        return (err);
1566
9.05k
    }
1567
7.62k
    *value = data;
1568
7.62k
    return (TIFFReadDirEntryErrOk);
1569
16.6k
}
1570
1571
static enum TIFFReadDirEntryErr
1572
TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
1573
30.3k
{
1574
30.3k
    enum TIFFReadDirEntryErr err;
1575
30.3k
    uint32_t count;
1576
30.3k
    void *origdata;
1577
30.3k
    int8_t *data;
1578
30.3k
    switch (direntry->tdir_type)
1579
30.3k
    {
1580
0
        case TIFF_UNDEFINED:
1581
0
        case TIFF_BYTE:
1582
30.3k
        case TIFF_SBYTE:
1583
30.3k
        case TIFF_SHORT:
1584
30.3k
        case TIFF_SSHORT:
1585
30.3k
        case TIFF_LONG:
1586
30.3k
        case TIFF_SLONG:
1587
30.3k
        case TIFF_LONG8:
1588
30.3k
        case TIFF_SLONG8:
1589
30.3k
            break;
1590
0
        default:
1591
0
            return (TIFFReadDirEntryErrType);
1592
30.3k
    }
1593
30.3k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1594
30.3k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1595
22.8k
    {
1596
22.8k
        *value = 0;
1597
22.8k
        return (err);
1598
22.8k
    }
1599
7.50k
    switch (direntry->tdir_type)
1600
7.50k
    {
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
7.50k
        case TIFF_SBYTE:
1621
7.50k
            *value = (int8_t *)origdata;
1622
7.50k
            return (TIFFReadDirEntryErrOk);
1623
7.50k
    }
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
253k
{
1754
253k
    enum TIFFReadDirEntryErr err;
1755
253k
    uint32_t count;
1756
253k
    void *origdata;
1757
253k
    uint16_t *data;
1758
253k
    switch (direntry->tdir_type)
1759
253k
    {
1760
6.84k
        case TIFF_BYTE:
1761
8.52k
        case TIFF_SBYTE:
1762
243k
        case TIFF_SHORT:
1763
245k
        case TIFF_SSHORT:
1764
246k
        case TIFF_LONG:
1765
248k
        case TIFF_SLONG:
1766
249k
        case TIFF_LONG8:
1767
251k
        case TIFF_SLONG8:
1768
251k
            break;
1769
2.18k
        default:
1770
2.18k
            return (TIFFReadDirEntryErrType);
1771
253k
    }
1772
251k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1773
251k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1774
115k
    {
1775
115k
        *value = 0;
1776
115k
        return (err);
1777
115k
    }
1778
136k
    switch (direntry->tdir_type)
1779
136k
    {
1780
122k
        case TIFF_SHORT:
1781
122k
            *value = (uint16_t *)origdata;
1782
122k
            if (tif->tif_flags & TIFF_SWAB)
1783
54.6k
                TIFFSwabArrayOfShort(*value, count);
1784
122k
            return (TIFFReadDirEntryErrOk);
1785
1.70k
        case TIFF_SSHORT:
1786
1.70k
        {
1787
1.70k
            int16_t *m;
1788
1.70k
            uint32_t n;
1789
1.70k
            m = (int16_t *)origdata;
1790
10.8k
            for (n = 0; n < count; n++)
1791
9.47k
            {
1792
9.47k
                if (tif->tif_flags & TIFF_SWAB)
1793
4.44k
                    TIFFSwabShort((uint16_t *)m);
1794
9.47k
                err = TIFFReadDirEntryCheckRangeShortSshort(*m);
1795
9.47k
                if (err != TIFFReadDirEntryErrOk)
1796
298
                {
1797
298
                    _TIFFfreeExt(tif, origdata);
1798
298
                    return (err);
1799
298
                }
1800
9.17k
                m++;
1801
9.17k
            }
1802
1.40k
            *value = (uint16_t *)origdata;
1803
1.40k
            return (TIFFReadDirEntryErrOk);
1804
1.70k
        }
1805
136k
    }
1806
11.6k
    data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
1807
11.6k
    if (data == 0)
1808
0
    {
1809
0
        _TIFFfreeExt(tif, origdata);
1810
0
        return (TIFFReadDirEntryErrAlloc);
1811
0
    }
1812
11.6k
    switch (direntry->tdir_type)
1813
11.6k
    {
1814
5.72k
        case TIFF_BYTE:
1815
5.72k
        {
1816
5.72k
            uint8_t *ma;
1817
5.72k
            uint16_t *mb;
1818
5.72k
            uint32_t n;
1819
5.72k
            ma = (uint8_t *)origdata;
1820
5.72k
            mb = data;
1821
222k
            for (n = 0; n < count; n++)
1822
216k
                *mb++ = (uint16_t)(*ma++);
1823
5.72k
        }
1824
5.72k
        break;
1825
1.50k
        case TIFF_SBYTE:
1826
1.50k
        {
1827
1.50k
            int8_t *ma;
1828
1.50k
            uint16_t *mb;
1829
1.50k
            uint32_t n;
1830
1.50k
            ma = (int8_t *)origdata;
1831
1.50k
            mb = data;
1832
10.1k
            for (n = 0; n < count; n++)
1833
8.93k
            {
1834
8.93k
                err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1835
8.93k
                if (err != TIFFReadDirEntryErrOk)
1836
295
                    break;
1837
8.63k
                *mb++ = (uint16_t)(*ma++);
1838
8.63k
            }
1839
1.50k
        }
1840
1.50k
        break;
1841
1.20k
        case TIFF_LONG:
1842
1.20k
        {
1843
1.20k
            uint32_t *ma;
1844
1.20k
            uint16_t *mb;
1845
1.20k
            uint32_t n;
1846
1.20k
            ma = (uint32_t *)origdata;
1847
1.20k
            mb = data;
1848
5.88k
            for (n = 0; n < count; n++)
1849
5.07k
            {
1850
5.07k
                if (tif->tif_flags & TIFF_SWAB)
1851
2.25k
                    TIFFSwabLong(ma);
1852
5.07k
                err = TIFFReadDirEntryCheckRangeShortLong(*ma);
1853
5.07k
                if (err != TIFFReadDirEntryErrOk)
1854
384
                    break;
1855
4.68k
                *mb++ = (uint16_t)(*ma++);
1856
4.68k
            }
1857
1.20k
        }
1858
1.20k
        break;
1859
949
        case TIFF_SLONG:
1860
949
        {
1861
949
            int32_t *ma;
1862
949
            uint16_t *mb;
1863
949
            uint32_t n;
1864
949
            ma = (int32_t *)origdata;
1865
949
            mb = data;
1866
4.04k
            for (n = 0; n < count; n++)
1867
3.82k
            {
1868
3.82k
                if (tif->tif_flags & TIFF_SWAB)
1869
1.25k
                    TIFFSwabLong((uint32_t *)ma);
1870
3.82k
                err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
1871
3.82k
                if (err != TIFFReadDirEntryErrOk)
1872
727
                    break;
1873
3.09k
                *mb++ = (uint16_t)(*ma++);
1874
3.09k
            }
1875
949
        }
1876
949
        break;
1877
751
        case TIFF_LONG8:
1878
751
        {
1879
751
            uint64_t *ma;
1880
751
            uint16_t *mb;
1881
751
            uint32_t n;
1882
751
            ma = (uint64_t *)origdata;
1883
751
            mb = data;
1884
3.46k
            for (n = 0; n < count; n++)
1885
3.37k
            {
1886
3.37k
                if (tif->tif_flags & TIFF_SWAB)
1887
1.63k
                    TIFFSwabLong8(ma);
1888
3.37k
                err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
1889
3.37k
                if (err != TIFFReadDirEntryErrOk)
1890
653
                    break;
1891
2.71k
                *mb++ = (uint16_t)(*ma++);
1892
2.71k
            }
1893
751
        }
1894
751
        break;
1895
1.56k
        case TIFF_SLONG8:
1896
1.56k
        {
1897
1.56k
            int64_t *ma;
1898
1.56k
            uint16_t *mb;
1899
1.56k
            uint32_t n;
1900
1.56k
            ma = (int64_t *)origdata;
1901
1.56k
            mb = data;
1902
5.23k
            for (n = 0; n < count; n++)
1903
5.05k
            {
1904
5.05k
                if (tif->tif_flags & TIFF_SWAB)
1905
2.48k
                    TIFFSwabLong8((uint64_t *)ma);
1906
5.05k
                err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1907
5.05k
                if (err != TIFFReadDirEntryErrOk)
1908
1.38k
                    break;
1909
3.66k
                *mb++ = (uint16_t)(*ma++);
1910
3.66k
            }
1911
1.56k
        }
1912
1.56k
        break;
1913
11.6k
    }
1914
11.6k
    _TIFFfreeExt(tif, origdata);
1915
11.6k
    if (err != TIFFReadDirEntryErrOk)
1916
3.44k
    {
1917
3.44k
        _TIFFfreeExt(tif, data);
1918
3.44k
        return (err);
1919
3.44k
    }
1920
8.24k
    *value = data;
1921
8.24k
    return (TIFFReadDirEntryErrOk);
1922
11.6k
}
1923
1924
static enum TIFFReadDirEntryErr
1925
TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
1926
54.6k
{
1927
54.6k
    enum TIFFReadDirEntryErr err;
1928
54.6k
    uint32_t count;
1929
54.6k
    void *origdata;
1930
54.6k
    int16_t *data;
1931
54.6k
    switch (direntry->tdir_type)
1932
54.6k
    {
1933
0
        case TIFF_BYTE:
1934
0
        case TIFF_SBYTE:
1935
0
        case TIFF_SHORT:
1936
54.6k
        case TIFF_SSHORT:
1937
54.6k
        case TIFF_LONG:
1938
54.6k
        case TIFF_SLONG:
1939
54.6k
        case TIFF_LONG8:
1940
54.6k
        case TIFF_SLONG8:
1941
54.6k
            break;
1942
0
        default:
1943
0
            return (TIFFReadDirEntryErrType);
1944
54.6k
    }
1945
54.6k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1946
54.6k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1947
45.9k
    {
1948
45.9k
        *value = 0;
1949
45.9k
        return (err);
1950
45.9k
    }
1951
8.69k
    switch (direntry->tdir_type)
1952
8.69k
    {
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
8.69k
        case TIFF_SSHORT:
1974
8.69k
            *value = (int16_t *)origdata;
1975
8.69k
            if (tif->tif_flags & TIFF_SWAB)
1976
5.06k
                TIFFSwabArrayOfShort((uint16_t *)(*value), count);
1977
8.69k
            return (TIFFReadDirEntryErrOk);
1978
8.69k
    }
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
153k
{
2095
153k
    enum TIFFReadDirEntryErr err;
2096
153k
    uint32_t count;
2097
153k
    void *origdata;
2098
153k
    uint32_t *data;
2099
153k
    switch (direntry->tdir_type)
2100
153k
    {
2101
270
        case TIFF_BYTE:
2102
583
        case TIFF_SBYTE:
2103
989
        case TIFF_SHORT:
2104
1.51k
        case TIFF_SSHORT:
2105
149k
        case TIFF_LONG:
2106
150k
        case TIFF_SLONG:
2107
150k
        case TIFF_LONG8:
2108
152k
        case TIFF_SLONG8:
2109
152k
            break;
2110
432
        default:
2111
432
            return (TIFFReadDirEntryErrType);
2112
153k
    }
2113
152k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2114
152k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2115
88.3k
    {
2116
88.3k
        *value = 0;
2117
88.3k
        return (err);
2118
88.3k
    }
2119
64.2k
    switch (direntry->tdir_type)
2120
64.2k
    {
2121
60.1k
        case TIFF_LONG:
2122
60.1k
            *value = (uint32_t *)origdata;
2123
60.1k
            if (tif->tif_flags & TIFF_SWAB)
2124
44.4k
                TIFFSwabArrayOfLong(*value, count);
2125
60.1k
            return (TIFFReadDirEntryErrOk);
2126
572
        case TIFF_SLONG:
2127
572
        {
2128
572
            int32_t *m;
2129
572
            uint32_t n;
2130
572
            m = (int32_t *)origdata;
2131
7.52k
            for (n = 0; n < count; n++)
2132
7.32k
            {
2133
7.32k
                if (tif->tif_flags & TIFF_SWAB)
2134
2.37k
                    TIFFSwabLong((uint32_t *)m);
2135
7.32k
                err = TIFFReadDirEntryCheckRangeLongSlong(*m);
2136
7.32k
                if (err != TIFFReadDirEntryErrOk)
2137
380
                {
2138
380
                    _TIFFfreeExt(tif, origdata);
2139
380
                    return (err);
2140
380
                }
2141
6.94k
                m++;
2142
6.94k
            }
2143
192
            *value = (uint32_t *)origdata;
2144
192
            return (TIFFReadDirEntryErrOk);
2145
572
        }
2146
64.2k
    }
2147
3.48k
    data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
2148
3.48k
    if (data == 0)
2149
0
    {
2150
0
        _TIFFfreeExt(tif, origdata);
2151
0
        return (TIFFReadDirEntryErrAlloc);
2152
0
    }
2153
3.48k
    switch (direntry->tdir_type)
2154
3.48k
    {
2155
200
        case TIFF_BYTE:
2156
200
        {
2157
200
            uint8_t *ma;
2158
200
            uint32_t *mb;
2159
200
            uint32_t n;
2160
200
            ma = (uint8_t *)origdata;
2161
200
            mb = data;
2162
9.92k
            for (n = 0; n < count; n++)
2163
9.72k
                *mb++ = (uint32_t)(*ma++);
2164
200
        }
2165
200
        break;
2166
277
        case TIFF_SBYTE:
2167
277
        {
2168
277
            int8_t *ma;
2169
277
            uint32_t *mb;
2170
277
            uint32_t n;
2171
277
            ma = (int8_t *)origdata;
2172
277
            mb = data;
2173
3.53k
            for (n = 0; n < count; n++)
2174
3.43k
            {
2175
3.43k
                err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
2176
3.43k
                if (err != TIFFReadDirEntryErrOk)
2177
170
                    break;
2178
3.26k
                *mb++ = (uint32_t)(*ma++);
2179
3.26k
            }
2180
277
        }
2181
277
        break;
2182
310
        case TIFF_SHORT:
2183
310
        {
2184
310
            uint16_t *ma;
2185
310
            uint32_t *mb;
2186
310
            uint32_t n;
2187
310
            ma = (uint16_t *)origdata;
2188
310
            mb = data;
2189
7.39k
            for (n = 0; n < count; n++)
2190
7.08k
            {
2191
7.08k
                if (tif->tif_flags & TIFF_SWAB)
2192
5.23k
                    TIFFSwabShort(ma);
2193
7.08k
                *mb++ = (uint32_t)(*ma++);
2194
7.08k
            }
2195
310
        }
2196
310
        break;
2197
449
        case TIFF_SSHORT:
2198
449
        {
2199
449
            int16_t *ma;
2200
449
            uint32_t *mb;
2201
449
            uint32_t n;
2202
449
            ma = (int16_t *)origdata;
2203
449
            mb = data;
2204
5.59k
            for (n = 0; n < count; n++)
2205
5.42k
            {
2206
5.42k
                if (tif->tif_flags & TIFF_SWAB)
2207
2.93k
                    TIFFSwabShort((uint16_t *)ma);
2208
5.42k
                err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
2209
5.42k
                if (err != TIFFReadDirEntryErrOk)
2210
287
                    break;
2211
5.14k
                *mb++ = (uint32_t)(*ma++);
2212
5.14k
            }
2213
449
        }
2214
449
        break;
2215
792
        case TIFF_LONG8:
2216
792
        {
2217
792
            uint64_t *ma;
2218
792
            uint32_t *mb;
2219
792
            uint32_t n;
2220
792
            ma = (uint64_t *)origdata;
2221
792
            mb = data;
2222
4.17k
            for (n = 0; n < count; n++)
2223
4.01k
            {
2224
4.01k
                if (tif->tif_flags & TIFF_SWAB)
2225
2.01k
                    TIFFSwabLong8(ma);
2226
4.01k
                err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
2227
4.01k
                if (err != TIFFReadDirEntryErrOk)
2228
632
                    break;
2229
3.38k
                *mb++ = (uint32_t)(*ma++);
2230
3.38k
            }
2231
792
        }
2232
792
        break;
2233
1.45k
        case TIFF_SLONG8:
2234
1.45k
        {
2235
1.45k
            int64_t *ma;
2236
1.45k
            uint32_t *mb;
2237
1.45k
            uint32_t n;
2238
1.45k
            ma = (int64_t *)origdata;
2239
1.45k
            mb = data;
2240
4.61k
            for (n = 0; n < count; n++)
2241
4.46k
            {
2242
4.46k
                if (tif->tif_flags & TIFF_SWAB)
2243
1.97k
                    TIFFSwabLong8((uint64_t *)ma);
2244
4.46k
                err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
2245
4.46k
                if (err != TIFFReadDirEntryErrOk)
2246
1.31k
                    break;
2247
3.15k
                *mb++ = (uint32_t)(*ma++);
2248
3.15k
            }
2249
1.45k
        }
2250
1.45k
        break;
2251
3.48k
    }
2252
3.48k
    _TIFFfreeExt(tif, origdata);
2253
3.48k
    if (err != TIFFReadDirEntryErrOk)
2254
2.40k
    {
2255
2.40k
        _TIFFfreeExt(tif, data);
2256
2.40k
        return (err);
2257
2.40k
    }
2258
1.08k
    *value = data;
2259
1.08k
    return (TIFFReadDirEntryErrOk);
2260
3.48k
}
2261
2262
static enum TIFFReadDirEntryErr
2263
TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
2264
23.4k
{
2265
23.4k
    enum TIFFReadDirEntryErr err;
2266
23.4k
    uint32_t count;
2267
23.4k
    void *origdata;
2268
23.4k
    int32_t *data;
2269
23.4k
    switch (direntry->tdir_type)
2270
23.4k
    {
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
23.4k
        case TIFF_SLONG:
2277
23.4k
        case TIFF_LONG8:
2278
23.4k
        case TIFF_SLONG8:
2279
23.4k
            break;
2280
0
        default:
2281
0
            return (TIFFReadDirEntryErrType);
2282
23.4k
    }
2283
23.4k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2284
23.4k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2285
18.1k
    {
2286
18.1k
        *value = 0;
2287
18.1k
        return (err);
2288
18.1k
    }
2289
5.30k
    switch (direntry->tdir_type)
2290
5.30k
    {
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
5.30k
        case TIFF_SLONG:
2312
5.30k
            *value = (int32_t *)origdata;
2313
5.30k
            if (tif->tif_flags & TIFF_SWAB)
2314
2.74k
                TIFFSwabArrayOfLong((uint32_t *)(*value), count);
2315
5.30k
            return (TIFFReadDirEntryErrOk);
2316
5.30k
    }
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
255k
{
2428
255k
    enum TIFFReadDirEntryErr err;
2429
255k
    uint32_t count;
2430
255k
    void *origdata;
2431
255k
    uint64_t *data;
2432
255k
    switch (direntry->tdir_type)
2433
255k
    {
2434
75.4k
        case TIFF_BYTE:
2435
86.9k
        case TIFF_SBYTE:
2436
109k
        case TIFF_SHORT:
2437
113k
        case TIFF_SSHORT:
2438
200k
        case TIFF_LONG:
2439
204k
        case TIFF_SLONG:
2440
246k
        case TIFF_LONG8:
2441
252k
        case TIFF_SLONG8:
2442
252k
            break;
2443
2.90k
        default:
2444
2.90k
            return (TIFFReadDirEntryErrType);
2445
255k
    }
2446
252k
    err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
2447
252k
                                         maxcount);
2448
252k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2449
42.7k
    {
2450
42.7k
        *value = 0;
2451
42.7k
        return (err);
2452
42.7k
    }
2453
209k
    switch (direntry->tdir_type)
2454
209k
    {
2455
24.0k
        case TIFF_LONG8:
2456
24.0k
            *value = (uint64_t *)origdata;
2457
24.0k
            if (tif->tif_flags & TIFF_SWAB)
2458
6.60k
                TIFFSwabArrayOfLong8(*value, count);
2459
24.0k
            return (TIFFReadDirEntryErrOk);
2460
4.17k
        case TIFF_SLONG8:
2461
4.17k
        {
2462
4.17k
            int64_t *m;
2463
4.17k
            uint32_t n;
2464
4.17k
            m = (int64_t *)origdata;
2465
11.3k
            for (n = 0; n < count; n++)
2466
7.70k
            {
2467
7.70k
                if (tif->tif_flags & TIFF_SWAB)
2468
3.43k
                    TIFFSwabLong8((uint64_t *)m);
2469
7.70k
                err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2470
7.70k
                if (err != TIFFReadDirEntryErrOk)
2471
505
                {
2472
505
                    _TIFFfreeExt(tif, origdata);
2473
505
                    return (err);
2474
505
                }
2475
7.19k
                m++;
2476
7.19k
            }
2477
3.67k
            *value = (uint64_t *)origdata;
2478
3.67k
            return (TIFFReadDirEntryErrOk);
2479
4.17k
        }
2480
209k
    }
2481
181k
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
2482
181k
    if (data == 0)
2483
0
    {
2484
0
        _TIFFfreeExt(tif, origdata);
2485
0
        return (TIFFReadDirEntryErrAlloc);
2486
0
    }
2487
181k
    switch (direntry->tdir_type)
2488
181k
    {
2489
71.0k
        case TIFF_BYTE:
2490
71.0k
        {
2491
71.0k
            uint8_t *ma;
2492
71.0k
            uint64_t *mb;
2493
71.0k
            uint32_t n;
2494
71.0k
            ma = (uint8_t *)origdata;
2495
71.0k
            mb = data;
2496
3.52M
            for (n = 0; n < count; n++)
2497
3.45M
                *mb++ = (uint64_t)(*ma++);
2498
71.0k
        }
2499
71.0k
        break;
2500
7.28k
        case TIFF_SBYTE:
2501
7.28k
        {
2502
7.28k
            int8_t *ma;
2503
7.28k
            uint64_t *mb;
2504
7.28k
            uint32_t n;
2505
7.28k
            ma = (int8_t *)origdata;
2506
7.28k
            mb = data;
2507
20.2k
            for (n = 0; n < count; n++)
2508
13.0k
            {
2509
13.0k
                err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2510
13.0k
                if (err != TIFFReadDirEntryErrOk)
2511
126
                    break;
2512
12.9k
                *mb++ = (uint64_t)(*ma++);
2513
12.9k
            }
2514
7.28k
        }
2515
7.28k
        break;
2516
19.3k
        case TIFF_SHORT:
2517
19.3k
        {
2518
19.3k
            uint16_t *ma;
2519
19.3k
            uint64_t *mb;
2520
19.3k
            uint32_t n;
2521
19.3k
            ma = (uint16_t *)origdata;
2522
19.3k
            mb = data;
2523
201k
            for (n = 0; n < count; n++)
2524
182k
            {
2525
182k
                if (tif->tif_flags & TIFF_SWAB)
2526
123k
                    TIFFSwabShort(ma);
2527
182k
                *mb++ = (uint64_t)(*ma++);
2528
182k
            }
2529
19.3k
        }
2530
19.3k
        break;
2531
3.97k
        case TIFF_SSHORT:
2532
3.97k
        {
2533
3.97k
            int16_t *ma;
2534
3.97k
            uint64_t *mb;
2535
3.97k
            uint32_t n;
2536
3.97k
            ma = (int16_t *)origdata;
2537
3.97k
            mb = data;
2538
15.5k
            for (n = 0; n < count; n++)
2539
11.8k
            {
2540
11.8k
                if (tif->tif_flags & TIFF_SWAB)
2541
8.35k
                    TIFFSwabShort((uint16_t *)ma);
2542
11.8k
                err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2543
11.8k
                if (err != TIFFReadDirEntryErrOk)
2544
341
                    break;
2545
11.5k
                *mb++ = (uint64_t)(*ma++);
2546
11.5k
            }
2547
3.97k
        }
2548
3.97k
        break;
2549
76.9k
        case TIFF_LONG:
2550
76.9k
        {
2551
76.9k
            uint32_t *ma;
2552
76.9k
            uint64_t *mb;
2553
76.9k
            uint32_t n;
2554
76.9k
            ma = (uint32_t *)origdata;
2555
76.9k
            mb = data;
2556
323k
            for (n = 0; n < count; n++)
2557
246k
            {
2558
246k
                if (tif->tif_flags & TIFF_SWAB)
2559
51.0k
                    TIFFSwabLong(ma);
2560
246k
                *mb++ = (uint64_t)(*ma++);
2561
246k
            }
2562
76.9k
        }
2563
76.9k
        break;
2564
2.60k
        case TIFF_SLONG:
2565
2.60k
        {
2566
2.60k
            int32_t *ma;
2567
2.60k
            uint64_t *mb;
2568
2.60k
            uint32_t n;
2569
2.60k
            ma = (int32_t *)origdata;
2570
2.60k
            mb = data;
2571
8.96k
            for (n = 0; n < count; n++)
2572
6.62k
            {
2573
6.62k
                if (tif->tif_flags & TIFF_SWAB)
2574
3.64k
                    TIFFSwabLong((uint32_t *)ma);
2575
6.62k
                err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2576
6.62k
                if (err != TIFFReadDirEntryErrOk)
2577
264
                    break;
2578
6.35k
                *mb++ = (uint64_t)(*ma++);
2579
6.35k
            }
2580
2.60k
        }
2581
2.60k
        break;
2582
181k
    }
2583
181k
    _TIFFfreeExt(tif, origdata);
2584
181k
    if (err != TIFFReadDirEntryErrOk)
2585
731
    {
2586
731
        _TIFFfreeExt(tif, data);
2587
731
        return (err);
2588
731
    }
2589
180k
    *value = data;
2590
180k
    return (TIFFReadDirEntryErrOk);
2591
181k
}
2592
2593
static enum TIFFReadDirEntryErr
2594
TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
2595
31.4k
{
2596
31.4k
    return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
2597
31.4k
                                               ~((uint64_t)0));
2598
31.4k
}
2599
2600
static enum TIFFReadDirEntryErr
2601
TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
2602
22.0k
{
2603
22.0k
    enum TIFFReadDirEntryErr err;
2604
22.0k
    uint32_t count;
2605
22.0k
    void *origdata;
2606
22.0k
    int64_t *data;
2607
22.0k
    switch (direntry->tdir_type)
2608
22.0k
    {
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
22.0k
        case TIFF_SLONG8:
2617
22.0k
            break;
2618
0
        default:
2619
0
            return (TIFFReadDirEntryErrType);
2620
22.0k
    }
2621
22.0k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
2622
22.0k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2623
17.9k
    {
2624
17.9k
        *value = 0;
2625
17.9k
        return (err);
2626
17.9k
    }
2627
4.11k
    switch (direntry->tdir_type)
2628
4.11k
    {
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
4.11k
        case TIFF_SLONG8:
2650
4.11k
            *value = (int64_t *)origdata;
2651
4.11k
            if (tif->tif_flags & TIFF_SWAB)
2652
2.04k
                TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
2653
4.11k
            return (TIFFReadDirEntryErrOk);
2654
4.11k
    }
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
79.9k
{
2754
79.9k
    enum TIFFReadDirEntryErr err;
2755
79.9k
    uint32_t count;
2756
79.9k
    void *origdata;
2757
79.9k
    float *data;
2758
79.9k
    switch (direntry->tdir_type)
2759
79.9k
    {
2760
795
        case TIFF_BYTE:
2761
1.28k
        case TIFF_SBYTE:
2762
2.39k
        case TIFF_SHORT:
2763
3.03k
        case TIFF_SSHORT:
2764
4.17k
        case TIFF_LONG:
2765
4.76k
        case TIFF_SLONG:
2766
5.89k
        case TIFF_LONG8:
2767
6.46k
        case TIFF_SLONG8:
2768
45.2k
        case TIFF_RATIONAL:
2769
64.1k
        case TIFF_SRATIONAL:
2770
76.9k
        case TIFF_FLOAT:
2771
78.1k
        case TIFF_DOUBLE:
2772
78.1k
            break;
2773
1.80k
        default:
2774
1.80k
            return (TIFFReadDirEntryErrType);
2775
79.9k
    }
2776
78.1k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2777
78.1k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2778
51.8k
    {
2779
51.8k
        *value = 0;
2780
51.8k
        return (err);
2781
51.8k
    }
2782
26.2k
    switch (direntry->tdir_type)
2783
26.2k
    {
2784
4.60k
        case TIFF_FLOAT:
2785
4.60k
            if (tif->tif_flags & TIFF_SWAB)
2786
3.35k
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
2787
4.60k
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
2788
4.60k
            *value = (float *)origdata;
2789
4.60k
            return (TIFFReadDirEntryErrOk);
2790
26.2k
    }
2791
21.6k
    data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
2792
21.6k
    if (data == 0)
2793
0
    {
2794
0
        _TIFFfreeExt(tif, origdata);
2795
0
        return (TIFFReadDirEntryErrAlloc);
2796
0
    }
2797
21.6k
    switch (direntry->tdir_type)
2798
21.6k
    {
2799
632
        case TIFF_BYTE:
2800
632
        {
2801
632
            uint8_t *ma;
2802
632
            float *mb;
2803
632
            uint32_t n;
2804
632
            ma = (uint8_t *)origdata;
2805
632
            mb = data;
2806
15.8k
            for (n = 0; n < count; n++)
2807
15.1k
                *mb++ = (float)(*ma++);
2808
632
        }
2809
632
        break;
2810
344
        case TIFF_SBYTE:
2811
344
        {
2812
344
            int8_t *ma;
2813
344
            float *mb;
2814
344
            uint32_t n;
2815
344
            ma = (int8_t *)origdata;
2816
344
            mb = data;
2817
5.15k
            for (n = 0; n < count; n++)
2818
4.81k
                *mb++ = (float)(*ma++);
2819
344
        }
2820
344
        break;
2821
969
        case TIFF_SHORT:
2822
969
        {
2823
969
            uint16_t *ma;
2824
969
            float *mb;
2825
969
            uint32_t n;
2826
969
            ma = (uint16_t *)origdata;
2827
969
            mb = data;
2828
22.3k
            for (n = 0; n < count; n++)
2829
21.4k
            {
2830
21.4k
                if (tif->tif_flags & TIFF_SWAB)
2831
3.30k
                    TIFFSwabShort(ma);
2832
21.4k
                *mb++ = (float)(*ma++);
2833
21.4k
            }
2834
969
        }
2835
969
        break;
2836
560
        case TIFF_SSHORT:
2837
560
        {
2838
560
            int16_t *ma;
2839
560
            float *mb;
2840
560
            uint32_t n;
2841
560
            ma = (int16_t *)origdata;
2842
560
            mb = data;
2843
19.7k
            for (n = 0; n < count; n++)
2844
19.2k
            {
2845
19.2k
                if (tif->tif_flags & TIFF_SWAB)
2846
2.92k
                    TIFFSwabShort((uint16_t *)ma);
2847
19.2k
                *mb++ = (float)(*ma++);
2848
19.2k
            }
2849
560
        }
2850
560
        break;
2851
955
        case TIFF_LONG:
2852
955
        {
2853
955
            uint32_t *ma;
2854
955
            float *mb;
2855
955
            uint32_t n;
2856
955
            ma = (uint32_t *)origdata;
2857
955
            mb = data;
2858
10.0k
            for (n = 0; n < count; n++)
2859
9.13k
            {
2860
9.13k
                if (tif->tif_flags & TIFF_SWAB)
2861
6.14k
                    TIFFSwabLong(ma);
2862
9.13k
                *mb++ = (float)(*ma++);
2863
9.13k
            }
2864
955
        }
2865
955
        break;
2866
512
        case TIFF_SLONG:
2867
512
        {
2868
512
            int32_t *ma;
2869
512
            float *mb;
2870
512
            uint32_t n;
2871
512
            ma = (int32_t *)origdata;
2872
512
            mb = data;
2873
6.78k
            for (n = 0; n < count; n++)
2874
6.27k
            {
2875
6.27k
                if (tif->tif_flags & TIFF_SWAB)
2876
2.56k
                    TIFFSwabLong((uint32_t *)ma);
2877
6.27k
                *mb++ = (float)(*ma++);
2878
6.27k
            }
2879
512
        }
2880
512
        break;
2881
827
        case TIFF_LONG8:
2882
827
        {
2883
827
            uint64_t *ma;
2884
827
            float *mb;
2885
827
            uint32_t n;
2886
827
            ma = (uint64_t *)origdata;
2887
827
            mb = data;
2888
11.8k
            for (n = 0; n < count; n++)
2889
11.0k
            {
2890
11.0k
                if (tif->tif_flags & TIFF_SWAB)
2891
3.67k
                    TIFFSwabLong8(ma);
2892
11.0k
                *mb++ = (float)(*ma++);
2893
11.0k
            }
2894
827
        }
2895
827
        break;
2896
447
        case TIFF_SLONG8:
2897
447
        {
2898
447
            int64_t *ma;
2899
447
            float *mb;
2900
447
            uint32_t n;
2901
447
            ma = (int64_t *)origdata;
2902
447
            mb = data;
2903
8.28k
            for (n = 0; n < count; n++)
2904
7.83k
            {
2905
7.83k
                if (tif->tif_flags & TIFF_SWAB)
2906
5.00k
                    TIFFSwabLong8((uint64_t *)ma);
2907
7.83k
                *mb++ = (float)(*ma++);
2908
7.83k
            }
2909
447
        }
2910
447
        break;
2911
10.3k
        case TIFF_RATIONAL:
2912
10.3k
        {
2913
10.3k
            uint32_t *ma;
2914
10.3k
            uint32_t maa;
2915
10.3k
            uint32_t mab;
2916
10.3k
            float *mb;
2917
10.3k
            uint32_t n;
2918
10.3k
            ma = (uint32_t *)origdata;
2919
10.3k
            mb = data;
2920
106k
            for (n = 0; n < count; n++)
2921
95.9k
            {
2922
95.9k
                if (tif->tif_flags & TIFF_SWAB)
2923
38.6k
                    TIFFSwabLong(ma);
2924
95.9k
                maa = *ma++;
2925
95.9k
                if (tif->tif_flags & TIFF_SWAB)
2926
38.6k
                    TIFFSwabLong(ma);
2927
95.9k
                mab = *ma++;
2928
95.9k
                if (mab == 0)
2929
11.2k
                    *mb++ = 0.0;
2930
84.7k
                else
2931
84.7k
                    *mb++ = (float)maa / (float)mab;
2932
95.9k
            }
2933
10.3k
        }
2934
10.3k
        break;
2935
5.18k
        case TIFF_SRATIONAL:
2936
5.18k
        {
2937
5.18k
            uint32_t *ma;
2938
5.18k
            int32_t maa;
2939
5.18k
            uint32_t mab;
2940
5.18k
            float *mb;
2941
5.18k
            uint32_t n;
2942
5.18k
            ma = (uint32_t *)origdata;
2943
5.18k
            mb = data;
2944
175k
            for (n = 0; n < count; n++)
2945
170k
            {
2946
170k
                if (tif->tif_flags & TIFF_SWAB)
2947
122k
                    TIFFSwabLong(ma);
2948
170k
                maa = *(int32_t *)ma;
2949
170k
                ma++;
2950
170k
                if (tif->tif_flags & TIFF_SWAB)
2951
122k
                    TIFFSwabLong(ma);
2952
170k
                mab = *ma++;
2953
170k
                if (mab == 0)
2954
11.8k
                    *mb++ = 0.0;
2955
158k
                else
2956
158k
                    *mb++ = (float)maa / (float)mab;
2957
170k
            }
2958
5.18k
        }
2959
5.18k
        break;
2960
880
        case TIFF_DOUBLE:
2961
880
        {
2962
880
            double *ma;
2963
880
            float *mb;
2964
880
            uint32_t n;
2965
880
            if (tif->tif_flags & TIFF_SWAB)
2966
189
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
2967
880
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
2968
880
            ma = (double *)origdata;
2969
880
            mb = data;
2970
37.7k
            for (n = 0; n < count; n++)
2971
36.9k
            {
2972
36.9k
                double val = *ma++;
2973
36.9k
                if (val > FLT_MAX)
2974
3.81k
                    val = FLT_MAX;
2975
33.0k
                else if (val < -FLT_MAX)
2976
4.57k
                    val = -FLT_MAX;
2977
36.9k
                *mb++ = (float)val;
2978
36.9k
            }
2979
880
        }
2980
880
        break;
2981
21.6k
    }
2982
21.6k
    _TIFFfreeExt(tif, origdata);
2983
21.6k
    *value = data;
2984
21.6k
    return (TIFFReadDirEntryErrOk);
2985
21.6k
}
2986
2987
static enum TIFFReadDirEntryErr
2988
TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
2989
26.3k
{
2990
26.3k
    enum TIFFReadDirEntryErr err;
2991
26.3k
    uint32_t count;
2992
26.3k
    void *origdata;
2993
26.3k
    double *data;
2994
26.3k
    switch (direntry->tdir_type)
2995
26.3k
    {
2996
327
        case TIFF_BYTE:
2997
1.16k
        case TIFF_SBYTE:
2998
1.68k
        case TIFF_SHORT:
2999
2.15k
        case TIFF_SSHORT:
3000
2.46k
        case TIFF_LONG:
3001
2.67k
        case TIFF_SLONG:
3002
2.88k
        case TIFF_LONG8:
3003
3.08k
        case TIFF_SLONG8:
3004
3.83k
        case TIFF_RATIONAL:
3005
4.17k
        case TIFF_SRATIONAL:
3006
4.40k
        case TIFF_FLOAT:
3007
26.3k
        case TIFF_DOUBLE:
3008
26.3k
            break;
3009
9
        default:
3010
9
            return (TIFFReadDirEntryErrType);
3011
26.3k
    }
3012
26.3k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3013
26.3k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3014
19.4k
    {
3015
19.4k
        *value = 0;
3016
19.4k
        return (err);
3017
19.4k
    }
3018
6.93k
    switch (direntry->tdir_type)
3019
6.93k
    {
3020
2.75k
        case TIFF_DOUBLE:
3021
2.75k
            if (tif->tif_flags & TIFF_SWAB)
3022
866
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3023
2.75k
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3024
2.75k
            *value = (double *)origdata;
3025
2.75k
            return (TIFFReadDirEntryErrOk);
3026
6.93k
    }
3027
4.17k
    data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
3028
4.17k
    if (data == 0)
3029
0
    {
3030
0
        _TIFFfreeExt(tif, origdata);
3031
0
        return (TIFFReadDirEntryErrAlloc);
3032
0
    }
3033
4.17k
    switch (direntry->tdir_type)
3034
4.17k
    {
3035
327
        case TIFF_BYTE:
3036
327
        {
3037
327
            uint8_t *ma;
3038
327
            double *mb;
3039
327
            uint32_t n;
3040
327
            ma = (uint8_t *)origdata;
3041
327
            mb = data;
3042
1.82k
            for (n = 0; n < count; n++)
3043
1.49k
                *mb++ = (double)(*ma++);
3044
327
        }
3045
327
        break;
3046
842
        case TIFF_SBYTE:
3047
842
        {
3048
842
            int8_t *ma;
3049
842
            double *mb;
3050
842
            uint32_t n;
3051
842
            ma = (int8_t *)origdata;
3052
842
            mb = data;
3053
2.72k
            for (n = 0; n < count; n++)
3054
1.88k
                *mb++ = (double)(*ma++);
3055
842
        }
3056
842
        break;
3057
518
        case TIFF_SHORT:
3058
518
        {
3059
518
            uint16_t *ma;
3060
518
            double *mb;
3061
518
            uint32_t n;
3062
518
            ma = (uint16_t *)origdata;
3063
518
            mb = data;
3064
3.14k
            for (n = 0; n < count; n++)
3065
2.62k
            {
3066
2.62k
                if (tif->tif_flags & TIFF_SWAB)
3067
1.58k
                    TIFFSwabShort(ma);
3068
2.62k
                *mb++ = (double)(*ma++);
3069
2.62k
            }
3070
518
        }
3071
518
        break;
3072
467
        case TIFF_SSHORT:
3073
467
        {
3074
467
            int16_t *ma;
3075
467
            double *mb;
3076
467
            uint32_t n;
3077
467
            ma = (int16_t *)origdata;
3078
467
            mb = data;
3079
2.20k
            for (n = 0; n < count; n++)
3080
1.73k
            {
3081
1.73k
                if (tif->tif_flags & TIFF_SWAB)
3082
582
                    TIFFSwabShort((uint16_t *)ma);
3083
1.73k
                *mb++ = (double)(*ma++);
3084
1.73k
            }
3085
467
        }
3086
467
        break;
3087
306
        case TIFF_LONG:
3088
306
        {
3089
306
            uint32_t *ma;
3090
306
            double *mb;
3091
306
            uint32_t n;
3092
306
            ma = (uint32_t *)origdata;
3093
306
            mb = data;
3094
2.42k
            for (n = 0; n < count; n++)
3095
2.11k
            {
3096
2.11k
                if (tif->tif_flags & TIFF_SWAB)
3097
727
                    TIFFSwabLong(ma);
3098
2.11k
                *mb++ = (double)(*ma++);
3099
2.11k
            }
3100
306
        }
3101
306
        break;
3102
212
        case TIFF_SLONG:
3103
212
        {
3104
212
            int32_t *ma;
3105
212
            double *mb;
3106
212
            uint32_t n;
3107
212
            ma = (int32_t *)origdata;
3108
212
            mb = data;
3109
1.98k
            for (n = 0; n < count; n++)
3110
1.76k
            {
3111
1.76k
                if (tif->tif_flags & TIFF_SWAB)
3112
776
                    TIFFSwabLong((uint32_t *)ma);
3113
1.76k
                *mb++ = (double)(*ma++);
3114
1.76k
            }
3115
212
        }
3116
212
        break;
3117
212
        case TIFF_LONG8:
3118
212
        {
3119
212
            uint64_t *ma;
3120
212
            double *mb;
3121
212
            uint32_t n;
3122
212
            ma = (uint64_t *)origdata;
3123
212
            mb = data;
3124
2.26k
            for (n = 0; n < count; n++)
3125
2.05k
            {
3126
2.05k
                if (tif->tif_flags & TIFF_SWAB)
3127
975
                    TIFFSwabLong8(ma);
3128
2.05k
                *mb++ = (double)(*ma++);
3129
2.05k
            }
3130
212
        }
3131
212
        break;
3132
203
        case TIFF_SLONG8:
3133
203
        {
3134
203
            int64_t *ma;
3135
203
            double *mb;
3136
203
            uint32_t n;
3137
203
            ma = (int64_t *)origdata;
3138
203
            mb = data;
3139
2.11k
            for (n = 0; n < count; n++)
3140
1.91k
            {
3141
1.91k
                if (tif->tif_flags & TIFF_SWAB)
3142
830
                    TIFFSwabLong8((uint64_t *)ma);
3143
1.91k
                *mb++ = (double)(*ma++);
3144
1.91k
            }
3145
203
        }
3146
203
        break;
3147
512
        case TIFF_RATIONAL:
3148
512
        {
3149
512
            uint32_t *ma;
3150
512
            uint32_t maa;
3151
512
            uint32_t mab;
3152
512
            double *mb;
3153
512
            uint32_t n;
3154
512
            ma = (uint32_t *)origdata;
3155
512
            mb = data;
3156
4.92k
            for (n = 0; n < count; n++)
3157
4.40k
            {
3158
4.40k
                if (tif->tif_flags & TIFF_SWAB)
3159
2.16k
                    TIFFSwabLong(ma);
3160
4.40k
                maa = *ma++;
3161
4.40k
                if (tif->tif_flags & TIFF_SWAB)
3162
2.16k
                    TIFFSwabLong(ma);
3163
4.40k
                mab = *ma++;
3164
4.40k
                if (mab == 0)
3165
1.22k
                    *mb++ = 0.0;
3166
3.18k
                else
3167
3.18k
                    *mb++ = (double)maa / (double)mab;
3168
4.40k
            }
3169
512
        }
3170
512
        break;
3171
345
        case TIFF_SRATIONAL:
3172
345
        {
3173
345
            uint32_t *ma;
3174
345
            int32_t maa;
3175
345
            uint32_t mab;
3176
345
            double *mb;
3177
345
            uint32_t n;
3178
345
            ma = (uint32_t *)origdata;
3179
345
            mb = data;
3180
4.83k
            for (n = 0; n < count; n++)
3181
4.48k
            {
3182
4.48k
                if (tif->tif_flags & TIFF_SWAB)
3183
1.94k
                    TIFFSwabLong(ma);
3184
4.48k
                maa = *(int32_t *)ma;
3185
4.48k
                ma++;
3186
4.48k
                if (tif->tif_flags & TIFF_SWAB)
3187
1.94k
                    TIFFSwabLong(ma);
3188
4.48k
                mab = *ma++;
3189
4.48k
                if (mab == 0)
3190
832
                    *mb++ = 0.0;
3191
3.65k
                else
3192
3.65k
                    *mb++ = (double)maa / (double)mab;
3193
4.48k
            }
3194
345
        }
3195
345
        break;
3196
229
        case TIFF_FLOAT:
3197
229
        {
3198
229
            float *ma;
3199
229
            double *mb;
3200
229
            uint32_t n;
3201
229
            if (tif->tif_flags & TIFF_SWAB)
3202
127
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
3203
229
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3204
229
            ma = (float *)origdata;
3205
229
            mb = data;
3206
1.73k
            for (n = 0; n < count; n++)
3207
1.51k
                *mb++ = (double)(*ma++);
3208
229
        }
3209
229
        break;
3210
4.17k
    }
3211
4.17k
    _TIFFfreeExt(tif, origdata);
3212
4.17k
    *value = data;
3213
4.17k
    return (TIFFReadDirEntryErrOk);
3214
4.17k
}
3215
3216
static enum TIFFReadDirEntryErr
3217
TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
3218
29.4k
{
3219
29.4k
    enum TIFFReadDirEntryErr err;
3220
29.4k
    uint32_t count;
3221
29.4k
    void *origdata;
3222
29.4k
    uint64_t *data;
3223
29.4k
    switch (direntry->tdir_type)
3224
29.4k
    {
3225
618
        case TIFF_LONG:
3226
811
        case TIFF_LONG8:
3227
16.4k
        case TIFF_IFD:
3228
27.2k
        case TIFF_IFD8:
3229
27.2k
            break;
3230
2.18k
        default:
3231
2.18k
            return (TIFFReadDirEntryErrType);
3232
29.4k
    }
3233
27.2k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3234
27.2k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3235
21.2k
    {
3236
21.2k
        *value = 0;
3237
21.2k
        return (err);
3238
21.2k
    }
3239
6.03k
    switch (direntry->tdir_type)
3240
6.03k
    {
3241
91
        case TIFF_LONG8:
3242
2.00k
        case TIFF_IFD8:
3243
2.00k
            *value = (uint64_t *)origdata;
3244
2.00k
            if (tif->tif_flags & TIFF_SWAB)
3245
1.01k
                TIFFSwabArrayOfLong8(*value, count);
3246
2.00k
            return (TIFFReadDirEntryErrOk);
3247
6.03k
    }
3248
4.02k
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
3249
4.02k
    if (data == 0)
3250
0
    {
3251
0
        _TIFFfreeExt(tif, origdata);
3252
0
        return (TIFFReadDirEntryErrAlloc);
3253
0
    }
3254
4.02k
    switch (direntry->tdir_type)
3255
4.02k
    {
3256
274
        case TIFF_LONG:
3257
4.02k
        case TIFF_IFD:
3258
4.02k
        {
3259
4.02k
            uint32_t *ma;
3260
4.02k
            uint64_t *mb;
3261
4.02k
            uint32_t n;
3262
4.02k
            ma = (uint32_t *)origdata;
3263
4.02k
            mb = data;
3264
83.0k
            for (n = 0; n < count; n++)
3265
79.0k
            {
3266
79.0k
                if (tif->tif_flags & TIFF_SWAB)
3267
59.9k
                    TIFFSwabLong(ma);
3268
79.0k
                *mb++ = (uint64_t)(*ma++);
3269
79.0k
            }
3270
4.02k
        }
3271
4.02k
        break;
3272
4.02k
    }
3273
4.02k
    _TIFFfreeExt(tif, origdata);
3274
4.02k
    *value = data;
3275
4.02k
    return (TIFFReadDirEntryErrOk);
3276
4.02k
}
3277
3278
static enum TIFFReadDirEntryErr
3279
TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
3280
                               uint16_t *value)
3281
14.7k
{
3282
14.7k
    enum TIFFReadDirEntryErr err;
3283
14.7k
    uint16_t *m;
3284
14.7k
    uint16_t *na;
3285
14.7k
    uint16_t nb;
3286
14.7k
    if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel)
3287
39
        return (TIFFReadDirEntryErrCount);
3288
14.7k
    err = TIFFReadDirEntryShortArray(tif, direntry, &m);
3289
14.7k
    if (err != TIFFReadDirEntryErrOk || m == NULL)
3290
3.79k
        return (err);
3291
10.9k
    na = m;
3292
10.9k
    nb = tif->tif_dir.td_samplesperpixel;
3293
10.9k
    *value = *na++;
3294
10.9k
    nb--;
3295
16.3k
    while (nb > 0)
3296
5.49k
    {
3297
5.49k
        if (*na++ != *value)
3298
80
        {
3299
80
            err = TIFFReadDirEntryErrPsdif;
3300
80
            break;
3301
80
        }
3302
5.41k
        nb--;
3303
5.41k
    }
3304
10.9k
    _TIFFfreeExt(tif, m);
3305
10.9k
    return (err);
3306
14.7k
}
3307
3308
static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
3309
                                        uint8_t *value)
3310
68.0k
{
3311
68.0k
    (void)tif;
3312
68.0k
    *value = *(uint8_t *)(&direntry->tdir_offset);
3313
68.0k
}
3314
3315
static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
3316
                                         int8_t *value)
3317
20.9k
{
3318
20.9k
    (void)tif;
3319
20.9k
    *value = *(int8_t *)(&direntry->tdir_offset);
3320
20.9k
}
3321
3322
static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
3323
                                         uint16_t *value)
3324
625k
{
3325
625k
    *value = direntry->tdir_offset.toff_short;
3326
    /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3327
625k
    if (tif->tif_flags & TIFF_SWAB)
3328
154k
        TIFFSwabShort(value);
3329
625k
}
3330
3331
static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
3332
                                          int16_t *value)
3333
29.7k
{
3334
29.7k
    *value = *(int16_t *)(&direntry->tdir_offset);
3335
29.7k
    if (tif->tif_flags & TIFF_SWAB)
3336
18.2k
        TIFFSwabShort((uint16_t *)value);
3337
29.7k
}
3338
3339
static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
3340
                                        uint32_t *value)
3341
208k
{
3342
208k
    *value = *(uint32_t *)(&direntry->tdir_offset);
3343
208k
    if (tif->tif_flags & TIFF_SWAB)
3344
145k
        TIFFSwabLong(value);
3345
208k
}
3346
3347
static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
3348
                                         int32_t *value)
3349
17.8k
{
3350
17.8k
    *value = *(int32_t *)(&direntry->tdir_offset);
3351
17.8k
    if (tif->tif_flags & TIFF_SWAB)
3352
8.70k
        TIFFSwabLong((uint32_t *)value);
3353
17.8k
}
3354
3355
static enum TIFFReadDirEntryErr
3356
TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
3357
8.78k
{
3358
8.78k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3359
6.42k
    {
3360
6.42k
        enum TIFFReadDirEntryErr err;
3361
6.42k
        uint32_t offset = direntry->tdir_offset.toff_long;
3362
6.42k
        if (tif->tif_flags & TIFF_SWAB)
3363
1.54k
            TIFFSwabLong(&offset);
3364
6.42k
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3365
6.42k
        if (err != TIFFReadDirEntryErrOk)
3366
1.32k
            return (err);
3367
6.42k
    }
3368
2.36k
    else
3369
2.36k
        *value = direntry->tdir_offset.toff_long8;
3370
7.45k
    if (tif->tif_flags & TIFF_SWAB)
3371
950
        TIFFSwabLong8(value);
3372
7.45k
    return (TIFFReadDirEntryErrOk);
3373
8.78k
}
3374
3375
static enum TIFFReadDirEntryErr
3376
TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
3377
8.92k
{
3378
8.92k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3379
8.41k
    {
3380
8.41k
        enum TIFFReadDirEntryErr err;
3381
8.41k
        uint32_t offset = direntry->tdir_offset.toff_long;
3382
8.41k
        if (tif->tif_flags & TIFF_SWAB)
3383
2.79k
            TIFFSwabLong(&offset);
3384
8.41k
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3385
8.41k
        if (err != TIFFReadDirEntryErrOk)
3386
2.86k
            return (err);
3387
8.41k
    }
3388
510
    else
3389
510
        *value = *(int64_t *)(&direntry->tdir_offset);
3390
6.06k
    if (tif->tif_flags & TIFF_SWAB)
3391
1.28k
        TIFFSwabLong8((uint64_t *)value);
3392
6.06k
    return (TIFFReadDirEntryErrOk);
3393
8.92k
}
3394
3395
static enum TIFFReadDirEntryErr
3396
TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
3397
                                double *value)
3398
9.94k
{
3399
9.94k
    UInt64Aligned_t m;
3400
3401
9.94k
    assert(sizeof(double) == 8);
3402
9.94k
    assert(sizeof(uint64_t) == 8);
3403
9.94k
    assert(sizeof(uint32_t) == 4);
3404
9.94k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3405
9.93k
    {
3406
9.93k
        enum TIFFReadDirEntryErr err;
3407
9.93k
        uint32_t offset = direntry->tdir_offset.toff_long;
3408
9.93k
        if (tif->tif_flags & TIFF_SWAB)
3409
2.19k
            TIFFSwabLong(&offset);
3410
9.93k
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3411
9.93k
        if (err != TIFFReadDirEntryErrOk)
3412
3.14k
            return (err);
3413
9.93k
    }
3414
14
    else
3415
14
        m.l = direntry->tdir_offset.toff_long8;
3416
6.80k
    if (tif->tif_flags & TIFF_SWAB)
3417
1.11k
        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
6.80k
    if (m.i[0] == 0 || m.i[1] == 0)
3422
1.39k
        *value = 0.0;
3423
5.40k
    else
3424
5.40k
        *value = (double)m.i[0] / (double)m.i[1];
3425
6.80k
    return (TIFFReadDirEntryErrOk);
3426
9.94k
}
3427
3428
static enum TIFFReadDirEntryErr
3429
TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
3430
                                 double *value)
3431
2.76k
{
3432
2.76k
    UInt64Aligned_t m;
3433
2.76k
    assert(sizeof(double) == 8);
3434
2.76k
    assert(sizeof(uint64_t) == 8);
3435
2.76k
    assert(sizeof(int32_t) == 4);
3436
2.76k
    assert(sizeof(uint32_t) == 4);
3437
2.76k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3438
2.75k
    {
3439
2.75k
        enum TIFFReadDirEntryErr err;
3440
2.75k
        uint32_t offset = direntry->tdir_offset.toff_long;
3441
2.75k
        if (tif->tif_flags & TIFF_SWAB)
3442
961
            TIFFSwabLong(&offset);
3443
2.75k
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3444
2.75k
        if (err != TIFFReadDirEntryErrOk)
3445
1.14k
            return (err);
3446
2.75k
    }
3447
8
    else
3448
8
        m.l = direntry->tdir_offset.toff_long8;
3449
1.61k
    if (tif->tif_flags & TIFF_SWAB)
3450
299
        TIFFSwabArrayOfLong(m.i, 2);
3451
    /* Not completely sure what we should do when m.i[1]==0, but some */
3452
    /* sanitizers do not like division by 0.0: */
3453
    /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3454
1.61k
    if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
3455
408
        *value = 0.0;
3456
1.20k
    else
3457
1.20k
        *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
3458
1.61k
    return (TIFFReadDirEntryErrOk);
3459
2.76k
}
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
975
{
3507
975
    union
3508
975
    {
3509
975
        float f;
3510
975
        uint32_t i;
3511
975
    } float_union;
3512
975
    assert(sizeof(float) == 4);
3513
975
    assert(sizeof(uint32_t) == 4);
3514
975
    assert(sizeof(float_union) == 4);
3515
975
    float_union.i = *(uint32_t *)(&direntry->tdir_offset);
3516
975
    *value = float_union.f;
3517
975
    if (tif->tif_flags & TIFF_SWAB)
3518
536
        TIFFSwabLong((uint32_t *)value);
3519
975
}
3520
3521
static enum TIFFReadDirEntryErr
3522
TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
3523
1.10k
{
3524
1.10k
    assert(sizeof(double) == 8);
3525
1.10k
    assert(sizeof(uint64_t) == 8);
3526
1.10k
    assert(sizeof(UInt64Aligned_t) == 8);
3527
1.10k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3528
1.07k
    {
3529
1.07k
        enum TIFFReadDirEntryErr err;
3530
1.07k
        uint32_t offset = direntry->tdir_offset.toff_long;
3531
1.07k
        if (tif->tif_flags & TIFF_SWAB)
3532
496
            TIFFSwabLong(&offset);
3533
1.07k
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3534
1.07k
        if (err != TIFFReadDirEntryErrOk)
3535
403
            return (err);
3536
1.07k
    }
3537
37
    else
3538
37
    {
3539
37
        UInt64Aligned_t uint64_union;
3540
37
        uint64_union.l = direntry->tdir_offset.toff_long8;
3541
37
        *value = uint64_union.d;
3542
37
    }
3543
704
    if (tif->tif_flags & TIFF_SWAB)
3544
371
        TIFFSwabLong8((uint64_t *)value);
3545
704
    return (TIFFReadDirEntryErrOk);
3546
1.10k
}
3547
3548
static enum TIFFReadDirEntryErr
3549
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
3550
11.5k
{
3551
11.5k
    if (value < 0)
3552
687
        return (TIFFReadDirEntryErrRange);
3553
10.8k
    else
3554
10.8k
        return (TIFFReadDirEntryErrOk);
3555
11.5k
}
3556
3557
static enum TIFFReadDirEntryErr
3558
TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
3559
12.1k
{
3560
12.1k
    if (value > 0xFF)
3561
2.54k
        return (TIFFReadDirEntryErrRange);
3562
9.59k
    else
3563
9.59k
        return (TIFFReadDirEntryErrOk);
3564
12.1k
}
3565
3566
static enum TIFFReadDirEntryErr
3567
TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
3568
8.56k
{
3569
8.56k
    if ((value < 0) || (value > 0xFF))
3570
1.74k
        return (TIFFReadDirEntryErrRange);
3571
6.81k
    else
3572
6.81k
        return (TIFFReadDirEntryErrOk);
3573
8.56k
}
3574
3575
static enum TIFFReadDirEntryErr
3576
TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
3577
6.01k
{
3578
6.01k
    if (value > 0xFF)
3579
1.89k
        return (TIFFReadDirEntryErrRange);
3580
4.11k
    else
3581
4.11k
        return (TIFFReadDirEntryErrOk);
3582
6.01k
}
3583
3584
static enum TIFFReadDirEntryErr
3585
TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
3586
7.72k
{
3587
7.72k
    if ((value < 0) || (value > 0xFF))
3588
1.57k
        return (TIFFReadDirEntryErrRange);
3589
6.14k
    else
3590
6.14k
        return (TIFFReadDirEntryErrOk);
3591
7.72k
}
3592
3593
static enum TIFFReadDirEntryErr
3594
TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
3595
5.15k
{
3596
5.15k
    if (value > 0xFF)
3597
1.74k
        return (TIFFReadDirEntryErrRange);
3598
3.41k
    else
3599
3.41k
        return (TIFFReadDirEntryErrOk);
3600
5.15k
}
3601
3602
static enum TIFFReadDirEntryErr
3603
TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
3604
6.66k
{
3605
6.66k
    if ((value < 0) || (value > 0xFF))
3606
2.64k
        return (TIFFReadDirEntryErrRange);
3607
4.02k
    else
3608
4.02k
        return (TIFFReadDirEntryErrOk);
3609
6.66k
}
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
24.6k
{
3677
24.6k
    if (value < 0)
3678
442
        return (TIFFReadDirEntryErrRange);
3679
24.2k
    else
3680
24.2k
        return (TIFFReadDirEntryErrOk);
3681
24.6k
}
3682
3683
static enum TIFFReadDirEntryErr
3684
TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
3685
20.2k
{
3686
20.2k
    if (value < 0)
3687
461
        return (TIFFReadDirEntryErrRange);
3688
19.8k
    else
3689
19.8k
        return (TIFFReadDirEntryErrOk);
3690
20.2k
}
3691
3692
static enum TIFFReadDirEntryErr
3693
TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
3694
26.2k
{
3695
26.2k
    if (value > 0xFFFF)
3696
1.06k
        return (TIFFReadDirEntryErrRange);
3697
25.2k
    else
3698
25.2k
        return (TIFFReadDirEntryErrOk);
3699
26.2k
}
3700
3701
static enum TIFFReadDirEntryErr
3702
TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
3703
7.78k
{
3704
7.78k
    if ((value < 0) || (value > 0xFFFF))
3705
1.24k
        return (TIFFReadDirEntryErrRange);
3706
6.53k
    else
3707
6.53k
        return (TIFFReadDirEntryErrOk);
3708
7.78k
}
3709
3710
static enum TIFFReadDirEntryErr
3711
TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
3712
4.70k
{
3713
4.70k
    if (value > 0xFFFF)
3714
1.67k
        return (TIFFReadDirEntryErrRange);
3715
3.03k
    else
3716
3.03k
        return (TIFFReadDirEntryErrOk);
3717
4.70k
}
3718
3719
static enum TIFFReadDirEntryErr
3720
TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
3721
6.59k
{
3722
6.59k
    if ((value < 0) || (value > 0xFFFF))
3723
2.52k
        return (TIFFReadDirEntryErrRange);
3724
4.06k
    else
3725
4.06k
        return (TIFFReadDirEntryErrOk);
3726
6.59k
}
3727
3728
static enum TIFFReadDirEntryErr
3729
TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
3730
0
{
3731
0
    if (value > 0x7FFF)
3732
0
        return (TIFFReadDirEntryErrRange);
3733
0
    else
3734
0
        return (TIFFReadDirEntryErrOk);
3735
0
}
3736
3737
static enum TIFFReadDirEntryErr
3738
TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
3739
0
{
3740
0
    if (value > 0x7FFF)
3741
0
        return (TIFFReadDirEntryErrRange);
3742
0
    else
3743
0
        return (TIFFReadDirEntryErrOk);
3744
0
}
3745
3746
static enum TIFFReadDirEntryErr
3747
TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
3748
0
{
3749
0
    if ((value < -0x8000) || (value > 0x7FFF))
3750
0
        return (TIFFReadDirEntryErrRange);
3751
0
    else
3752
0
        return (TIFFReadDirEntryErrOk);
3753
0
}
3754
3755
static enum TIFFReadDirEntryErr
3756
TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
3757
0
{
3758
0
    if (value > 0x7FFF)
3759
0
        return (TIFFReadDirEntryErrRange);
3760
0
    else
3761
0
        return (TIFFReadDirEntryErrOk);
3762
0
}
3763
3764
static enum TIFFReadDirEntryErr
3765
TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
3766
0
{
3767
0
    if ((value < -0x8000) || (value > 0x7FFF))
3768
0
        return (TIFFReadDirEntryErrRange);
3769
0
    else
3770
0
        return (TIFFReadDirEntryErrOk);
3771
0
}
3772
3773
static enum TIFFReadDirEntryErr
3774
TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
3775
7.68k
{
3776
7.68k
    if (value < 0)
3777
330
        return (TIFFReadDirEntryErrRange);
3778
7.35k
    else
3779
7.35k
        return (TIFFReadDirEntryErrOk);
3780
7.68k
}
3781
3782
static enum TIFFReadDirEntryErr
3783
TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
3784
22.7k
{
3785
22.7k
    if (value < 0)
3786
455
        return (TIFFReadDirEntryErrRange);
3787
22.3k
    else
3788
22.3k
        return (TIFFReadDirEntryErrOk);
3789
22.7k
}
3790
3791
static enum TIFFReadDirEntryErr
3792
TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
3793
19.2k
{
3794
19.2k
    if (value < 0)
3795
620
        return (TIFFReadDirEntryErrRange);
3796
18.6k
    else
3797
18.6k
        return (TIFFReadDirEntryErrOk);
3798
19.2k
}
3799
3800
static enum TIFFReadDirEntryErr
3801
TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
3802
7.01k
{
3803
7.01k
    if (value > UINT32_MAX)
3804
1.16k
        return (TIFFReadDirEntryErrRange);
3805
5.85k
    else
3806
5.85k
        return (TIFFReadDirEntryErrOk);
3807
7.01k
}
3808
3809
static enum TIFFReadDirEntryErr
3810
TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
3811
6.37k
{
3812
6.37k
    if ((value < 0) || (value > (int64_t)UINT32_MAX))
3813
2.31k
        return (TIFFReadDirEntryErrRange);
3814
4.06k
    else
3815
4.06k
        return (TIFFReadDirEntryErrOk);
3816
6.37k
}
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
13.5k
{
3850
13.5k
    if (value < 0)
3851
229
        return (TIFFReadDirEntryErrRange);
3852
13.3k
    else
3853
13.3k
        return (TIFFReadDirEntryErrOk);
3854
13.5k
}
3855
3856
static enum TIFFReadDirEntryErr
3857
TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
3858
12.5k
{
3859
12.5k
    if (value < 0)
3860
477
        return (TIFFReadDirEntryErrRange);
3861
12.1k
    else
3862
12.1k
        return (TIFFReadDirEntryErrOk);
3863
12.5k
}
3864
3865
static enum TIFFReadDirEntryErr
3866
TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
3867
7.06k
{
3868
7.06k
    if (value < 0)
3869
367
        return (TIFFReadDirEntryErrRange);
3870
6.69k
    else
3871
6.69k
        return (TIFFReadDirEntryErrOk);
3872
7.06k
}
3873
3874
static enum TIFFReadDirEntryErr
3875
TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
3876
8.66k
{
3877
8.66k
    if (value < 0)
3878
1.02k
        return (TIFFReadDirEntryErrRange);
3879
7.63k
    else
3880
7.63k
        return (TIFFReadDirEntryErrOk);
3881
8.66k
}
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
362k
{
3895
362k
    assert(size > 0);
3896
362k
    if (!isMapped(tif))
3897
166
    {
3898
166
        if (!SeekOK(tif, offset))
3899
0
            return (TIFFReadDirEntryErrIo);
3900
166
        if (!ReadOK(tif, dest, size))
3901
0
            return (TIFFReadDirEntryErrIo);
3902
166
    }
3903
362k
    else
3904
362k
    {
3905
362k
        size_t ma, mb;
3906
362k
        ma = (size_t)offset;
3907
362k
        if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
3908
35
        {
3909
35
            return TIFFReadDirEntryErrIo;
3910
35
        }
3911
362k
        mb = ma + size;
3912
362k
        if (mb > (uint64_t)tif->tif_size)
3913
76.8k
            return (TIFFReadDirEntryErrIo);
3914
285k
        _TIFFmemcpy(dest, tif->tif_base + ma, size);
3915
285k
    }
3916
285k
    return (TIFFReadDirEntryErrOk);
3917
362k
}
3918
3919
static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
3920
                                      const char *module, const char *tagname,
3921
                                      int recover)
3922
865k
{
3923
865k
    if (!recover)
3924
13.3k
    {
3925
13.3k
        switch (err)
3926
13.3k
        {
3927
1.95k
            case TIFFReadDirEntryErrCount:
3928
1.95k
                TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
3929
1.95k
                              tagname);
3930
1.95k
                break;
3931
1.75k
            case TIFFReadDirEntryErrType:
3932
1.75k
                TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
3933
1.75k
                              tagname);
3934
1.75k
                break;
3935
2.93k
            case TIFFReadDirEntryErrIo:
3936
2.93k
                TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
3937
2.93k
                              tagname);
3938
2.93k
                break;
3939
5.28k
            case TIFFReadDirEntryErrRange:
3940
5.28k
                TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
3941
5.28k
                              tagname);
3942
5.28k
                break;
3943
80
            case TIFFReadDirEntryErrPsdif:
3944
80
                TIFFErrorExtR(
3945
80
                    tif, module,
3946
80
                    "Cannot handle different values per sample for \"%s\"",
3947
80
                    tagname);
3948
80
                break;
3949
1.16k
            case TIFFReadDirEntryErrSizesan:
3950
1.16k
                TIFFErrorExtR(tif, module,
3951
1.16k
                              "Sanity check on size of \"%s\" value failed",
3952
1.16k
                              tagname);
3953
1.16k
                break;
3954
151
            case TIFFReadDirEntryErrAlloc:
3955
151
                TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
3956
151
                              tagname);
3957
151
                break;
3958
0
            default:
3959
0
                assert(0); /* we should never get here */
3960
0
                break;
3961
13.3k
        }
3962
13.3k
    }
3963
852k
    else
3964
852k
    {
3965
852k
        switch (err)
3966
852k
        {
3967
163k
            case TIFFReadDirEntryErrCount:
3968
163k
                TIFFWarningExtR(tif, module,
3969
163k
                                "Incorrect count for \"%s\"; tag ignored",
3970
163k
                                tagname);
3971
163k
                break;
3972
60.2k
            case TIFFReadDirEntryErrType:
3973
60.2k
                TIFFWarningExtR(tif, module,
3974
60.2k
                                "Incompatible type for \"%s\"; tag ignored",
3975
60.2k
                                tagname);
3976
60.2k
                break;
3977
349k
            case TIFFReadDirEntryErrIo:
3978
349k
                TIFFWarningExtR(
3979
349k
                    tif, module,
3980
349k
                    "IO error during reading of \"%s\"; tag ignored", tagname);
3981
349k
                break;
3982
22.2k
            case TIFFReadDirEntryErrRange:
3983
22.2k
                TIFFWarningExtR(tif, module,
3984
22.2k
                                "Incorrect value for \"%s\"; tag ignored",
3985
22.2k
                                tagname);
3986
22.2k
                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
148k
            case TIFFReadDirEntryErrSizesan:
3994
148k
                TIFFWarningExtR(
3995
148k
                    tif, module,
3996
148k
                    "Sanity check on size of \"%s\" value failed; tag ignored",
3997
148k
                    tagname);
3998
148k
                break;
3999
107k
            case TIFFReadDirEntryErrAlloc:
4000
107k
                TIFFWarningExtR(tif, module,
4001
107k
                                "Out of memory reading of \"%s\"; tag ignored",
4002
107k
                                tagname);
4003
107k
                break;
4004
0
            default:
4005
0
                assert(0); /* we should never get here */
4006
0
                break;
4007
852k
        }
4008
852k
    }
4009
865k
}
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
157k
{
4018
157k
    switch (photometric)
4019
157k
    {
4020
2.58k
        case PHOTOMETRIC_PALETTE:
4021
37.9k
        case PHOTOMETRIC_MINISWHITE:
4022
53.0k
        case PHOTOMETRIC_MINISBLACK:
4023
53.0k
            return 1;
4024
35.9k
        case PHOTOMETRIC_YCBCR:
4025
47.2k
        case PHOTOMETRIC_RGB:
4026
50.0k
        case PHOTOMETRIC_CIELAB:
4027
51.5k
        case PHOTOMETRIC_LOGLUV:
4028
52.1k
        case PHOTOMETRIC_ITULAB:
4029
52.7k
        case PHOTOMETRIC_ICCLAB:
4030
52.7k
            return 3;
4031
9.28k
        case PHOTOMETRIC_SEPARATED:
4032
10.2k
        case PHOTOMETRIC_MASK:
4033
10.2k
            return 4;
4034
732
        case PHOTOMETRIC_LOGL:
4035
732
        case PHOTOMETRIC_CFA:
4036
41.5k
        default:
4037
41.5k
            return 0;
4038
157k
    }
4039
157k
}
4040
4041
static int ByteCountLooksBad(TIFF *tif)
4042
36.3k
{
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
36.3k
    uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
4057
36.3k
    uint64_t offset = TIFFGetStrileOffset(tif, 0);
4058
36.3k
    uint64_t filesize;
4059
4060
36.3k
    if (offset == 0)
4061
7.50k
        return 0;
4062
28.8k
    if (bytecount == 0)
4063
1.96k
        return 1;
4064
26.8k
    if (tif->tif_dir.td_compression != COMPRESSION_NONE)
4065
14.5k
        return 0;
4066
12.3k
    filesize = TIFFGetFileSize(tif);
4067
12.3k
    if (offset <= filesize && bytecount > filesize - offset)
4068
2.03k
        return 1;
4069
10.3k
    if (tif->tif_mode == O_RDONLY)
4070
10.3k
    {
4071
10.3k
        uint64_t scanlinesize = TIFFScanlineSize64(tif);
4072
10.3k
        if (tif->tif_dir.td_imagelength > 0 &&
4073
9.80k
            scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
4074
12
        {
4075
12
            return 1;
4076
12
        }
4077
10.3k
        if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
4078
1.41k
            return 1;
4079
10.3k
    }
4080
8.90k
    return 0;
4081
10.3k
}
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
849k
{
4089
849k
    const uint64_t data_width = TIFFDataWidth(dp->tdir_type);
4090
849k
    if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
4091
56
    {
4092
56
        TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4093
56
                      "Too large IFD data size");
4094
56
        return false;
4095
56
    }
4096
849k
    const uint64_t datalength = dp->tdir_count * data_width;
4097
849k
    if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
4098
270k
    {
4099
270k
        if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
4100
345
        {
4101
345
            TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
4102
345
                          "Too large IFD data size");
4103
345
            return false;
4104
345
        }
4105
270k
        tif->tif_dir.td_dirdatasize_read += datalength;
4106
270k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
4107
268k
        {
4108
            /* The offset of TIFFDirEntry are not swapped when read in. That has
4109
             * to be done when used. */
4110
268k
            uint32_t offset = dp->tdir_offset.toff_long;
4111
268k
            if (tif->tif_flags & TIFF_SWAB)
4112
106k
                TIFFSwabLong(&offset);
4113
268k
            tif->tif_dir
4114
268k
                .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4115
268k
                .offset = (uint64_t)offset;
4116
268k
        }
4117
1.77k
        else
4118
1.77k
        {
4119
1.77k
            tif->tif_dir
4120
1.77k
                .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4121
1.77k
                .offset = dp->tdir_offset.toff_long8;
4122
1.77k
            if (tif->tif_flags & TIFF_SWAB)
4123
115
                TIFFSwabLong8(
4124
115
                    &tif->tif_dir
4125
115
                         .td_dirdatasize_offsets[tif->tif_dir
4126
115
                                                     .td_dirdatasize_Noffsets]
4127
115
                         .offset);
4128
1.77k
        }
4129
270k
        tif->tif_dir
4130
270k
            .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
4131
270k
            .length = datalength;
4132
270k
        tif->tif_dir.td_dirdatasize_Noffsets++;
4133
270k
    }
4134
849k
    return true;
4135
849k
}
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
199k
{
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
199k
    if (tif->tif_mode == O_RDONLY)
4166
199k
        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
256k
{
4238
256k
    static const char module[] = "TIFFReadDirectory";
4239
256k
    TIFFDirEntry *dir;
4240
256k
    uint16_t dircount;
4241
256k
    TIFFDirEntry *dp;
4242
256k
    uint16_t di;
4243
256k
    const TIFFField *fip;
4244
256k
    uint32_t fii = FAILED_FII;
4245
256k
    toff_t nextdiroff;
4246
256k
    int bitspersample_read = FALSE;
4247
256k
    int color_channels;
4248
4249
256k
    if (tif->tif_nextdiroff == 0)
4250
19.0k
    {
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
19.0k
        tif->tif_diroff = tif->tif_nextdiroff;
4255
19.0k
        return 0;
4256
19.0k
    }
4257
4258
237k
    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
237k
    if (!_TIFFCheckDirNumberAndOffset(tif,
4263
237k
                                      tif->tif_curdir ==
4264
237k
                                              TIFF_NON_EXISTENT_DIR_NUMBER
4265
237k
                                          ? 0
4266
237k
                                          : tif->tif_curdir + 1,
4267
237k
                                      nextdiroff))
4268
289
    {
4269
289
        return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
4270
                     IFDs) */
4271
289
    }
4272
236k
    dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
4273
236k
    if (!dircount)
4274
41.7k
    {
4275
41.7k
        TIFFErrorExtR(tif, module,
4276
41.7k
                      "Failed to read directory at offset %" PRIu64,
4277
41.7k
                      nextdiroff);
4278
41.7k
        return 0;
4279
41.7k
    }
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
195k
    if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
4284
165k
        tif->tif_curdir = 0;
4285
29.7k
    else
4286
29.7k
        tif->tif_curdir++;
4287
4288
195k
    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
195k
    {
4295
195k
        TIFFDirEntry *ma;
4296
195k
        uint16_t mb;
4297
6.72M
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
4298
6.53M
        {
4299
6.53M
            TIFFDirEntry *na;
4300
6.53M
            uint16_t nb;
4301
553M
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
4302
546M
            {
4303
546M
                if (ma->tdir_tag == na->tdir_tag)
4304
33.1M
                {
4305
33.1M
                    na->tdir_ignore = TRUE;
4306
33.1M
                }
4307
546M
            }
4308
6.53M
        }
4309
195k
    }
4310
4311
195k
    tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
4312
195k
    tif->tif_flags &= ~TIFF_BUF4WRITE;   /* reset before new dir */
4313
195k
    tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
4314
4315
    /* free any old stuff and reinit */
4316
195k
    TIFFFreeDirectory(tif);
4317
195k
    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
195k
    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
195k
    tif->tif_dir.td_dirdatasize_offsets =
4327
195k
        (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
4328
195k
            tif, dircount * sizeof(TIFFEntryOffsetAndLength));
4329
195k
    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
195k
    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
195k
    dp =
4363
195k
        TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
4364
195k
    if (dp)
4365
63.0k
    {
4366
63.0k
        if (!TIFFFetchNormalTag(tif, dp, 0))
4367
250
            goto bad;
4368
62.7k
        dp->tdir_ignore = TRUE;
4369
62.7k
    }
4370
194k
    dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
4371
194k
    if (dp)
4372
114k
    {
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
114k
        uint16_t value;
4380
114k
        enum TIFFReadDirEntryErr err;
4381
114k
        err = TIFFReadDirEntryShort(tif, dp, &value);
4382
114k
        if (err == TIFFReadDirEntryErrCount)
4383
8.56k
            err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
4384
114k
        if (err != TIFFReadDirEntryErrOk)
4385
4.10k
        {
4386
4.10k
            TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
4387
4.10k
            goto bad;
4388
4.10k
        }
4389
110k
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
4390
0
            goto bad;
4391
110k
        dp->tdir_ignore = TRUE;
4392
110k
    }
4393
80.3k
    else
4394
80.3k
    {
4395
80.3k
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
4396
0
            goto bad;
4397
80.3k
    }
4398
    /*
4399
     * First real pass over the directory.
4400
     */
4401
6.69M
    for (di = 0, dp = dir; di < dircount; di++, dp++)
4402
6.51M
    {
4403
6.51M
        if (!dp->tdir_ignore)
4404
3.88M
        {
4405
3.88M
            TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4406
3.88M
            if (fii == FAILED_FII)
4407
2.41M
            {
4408
2.41M
                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
2.41M
                const TIFFField *fld = _TIFFCreateAnonField(
4418
2.41M
                    tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
4419
2.41M
                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
2.41M
                else
4429
2.41M
                {
4430
2.41M
                    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4431
2.41M
                    assert(fii != FAILED_FII);
4432
2.41M
                }
4433
2.41M
            }
4434
3.88M
        }
4435
6.51M
        if (!dp->tdir_ignore)
4436
3.88M
        {
4437
3.88M
            fip = tif->tif_fields[fii];
4438
3.88M
            if (fip->field_bit == FIELD_IGNORE)
4439
47.1k
                dp->tdir_ignore = TRUE;
4440
3.83M
            else
4441
3.83M
            {
4442
3.83M
                switch (dp->tdir_tag)
4443
3.83M
                {
4444
132k
                    case TIFFTAG_STRIPOFFSETS:
4445
191k
                    case TIFFTAG_STRIPBYTECOUNTS:
4446
213k
                    case TIFFTAG_TILEOFFSETS:
4447
232k
                    case TIFFTAG_TILEBYTECOUNTS:
4448
232k
                        TIFFSetFieldBit(tif, fip->field_bit);
4449
232k
                        break;
4450
173k
                    case TIFFTAG_IMAGEWIDTH:
4451
336k
                    case TIFFTAG_IMAGELENGTH:
4452
336k
                    case TIFFTAG_IMAGEDEPTH:
4453
358k
                    case TIFFTAG_TILELENGTH:
4454
381k
                    case TIFFTAG_TILEWIDTH:
4455
381k
                    case TIFFTAG_TILEDEPTH:
4456
408k
                    case TIFFTAG_PLANARCONFIG:
4457
459k
                    case TIFFTAG_ROWSPERSTRIP:
4458
474k
                    case TIFFTAG_EXTRASAMPLES:
4459
474k
                        if (!TIFFFetchNormalTag(tif, dp, 0))
4460
3.95k
                            goto bad;
4461
470k
                        dp->tdir_ignore = TRUE;
4462
470k
                        break;
4463
3.12M
                    default:
4464
3.12M
                        if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
4465
75.0k
                            dp->tdir_ignore = TRUE;
4466
3.12M
                        break;
4467
3.83M
                }
4468
3.83M
            }
4469
3.88M
        }
4470
6.51M
    }
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
186k
    if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4480
24.9k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
4481
1.07k
    {
4482
1.07k
        if (!_TIFFFillStriles(tif))
4483
0
            goto bad;
4484
1.07k
        dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4485
1.07k
                                        TIFFTAG_STRIPOFFSETS);
4486
1.07k
        if ((dp != 0) && (dp->tdir_count == 1))
4487
525
        {
4488
525
            dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4489
525
                                            TIFFTAG_STRIPBYTECOUNTS);
4490
525
            if ((dp != 0) && (dp->tdir_count == 1))
4491
153
            {
4492
153
                tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
4493
153
                TIFFWarningExtR(tif, module,
4494
153
                                "Planarconfig tag value assumed incorrect, "
4495
153
                                "assuming data is contig instead of chunky");
4496
153
            }
4497
525
        }
4498
1.07k
    }
4499
    /*
4500
     * Allocate directory structure and setup defaults.
4501
     */
4502
186k
    if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
4503
1.93k
    {
4504
1.93k
        MissingRequired(tif, "ImageLength");
4505
1.93k
        goto bad;
4506
1.93k
    }
4507
4508
    /*
4509
     * Second pass: extract other information.
4510
     */
4511
6.66M
    for (di = 0, dp = dir; di < dircount; di++, dp++)
4512
6.48M
    {
4513
6.48M
        if (!dp->tdir_ignore)
4514
3.26M
        {
4515
3.26M
            switch (dp->tdir_tag)
4516
3.26M
            {
4517
1.64k
                case TIFFTAG_MINSAMPLEVALUE:
4518
2.79k
                case TIFFTAG_MAXSAMPLEVALUE:
4519
65.5k
                case TIFFTAG_BITSPERSAMPLE:
4520
66.6k
                case TIFFTAG_DATATYPE:
4521
85.3k
                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
85.3k
                    {
4533
85.3k
                        uint16_t value;
4534
85.3k
                        enum TIFFReadDirEntryErr err;
4535
85.3k
                        err = TIFFReadDirEntryShort(tif, dp, &value);
4536
85.3k
                        if (!EvaluateIFDdatasizeReading(tif, dp))
4537
11
                            goto bad;
4538
85.3k
                        if (err == TIFFReadDirEntryErrCount)
4539
6.22k
                            err =
4540
6.22k
                                TIFFReadDirEntryPersampleShort(tif, dp, &value);
4541
85.3k
                        if (err != TIFFReadDirEntryErrOk)
4542
1.11k
                        {
4543
1.11k
                            fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4544
1.11k
                            TIFFReadDirEntryOutputErr(
4545
1.11k
                                tif, err, module,
4546
1.11k
                                fip ? fip->field_name : "unknown tagname", 0);
4547
1.11k
                            goto bad;
4548
1.11k
                        }
4549
84.2k
                        if (!TIFFSetField(tif, dp->tdir_tag, value))
4550
58
                            goto bad;
4551
84.1k
                        if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
4552
62.1k
                            bitspersample_read = TRUE;
4553
84.1k
                    }
4554
0
                    break;
4555
2.62k
                case TIFFTAG_SMINSAMPLEVALUE:
4556
4.78k
                case TIFFTAG_SMAXSAMPLEVALUE:
4557
4.78k
                {
4558
4559
4.78k
                    double *data = NULL;
4560
4.78k
                    enum TIFFReadDirEntryErr err;
4561
4.78k
                    uint32_t saved_flags;
4562
4.78k
                    int m;
4563
4.78k
                    if (dp->tdir_count !=
4564
4.78k
                        (uint64_t)tif->tif_dir.td_samplesperpixel)
4565
703
                        err = TIFFReadDirEntryErrCount;
4566
4.08k
                    else
4567
4.08k
                        err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
4568
4.78k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4569
34
                        goto bad;
4570
4.75k
                    if (err != TIFFReadDirEntryErrOk)
4571
687
                    {
4572
687
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4573
687
                        TIFFReadDirEntryOutputErr(
4574
687
                            tif, err, module,
4575
687
                            fip ? fip->field_name : "unknown tagname", 0);
4576
687
                        goto bad;
4577
687
                    }
4578
4.06k
                    saved_flags = tif->tif_flags;
4579
4.06k
                    tif->tif_flags |= TIFF_PERSAMPLE;
4580
4.06k
                    m = TIFFSetField(tif, dp->tdir_tag, data);
4581
4.06k
                    tif->tif_flags = saved_flags;
4582
4.06k
                    _TIFFfreeExt(tif, data);
4583
4.06k
                    if (!m)
4584
0
                        goto bad;
4585
4.06k
                }
4586
4.06k
                break;
4587
132k
                case TIFFTAG_STRIPOFFSETS:
4588
154k
                case TIFFTAG_TILEOFFSETS:
4589
154k
                {
4590
154k
                    switch (dp->tdir_type)
4591
154k
                    {
4592
15.3k
                        case TIFF_SHORT:
4593
72.6k
                        case TIFF_LONG:
4594
82.4k
                        case TIFF_LONG8:
4595
82.4k
                            break;
4596
71.8k
                        default:
4597
                            /* Warn except if directory typically created with
4598
                             * TIFFDeferStrileArrayWriting() */
4599
71.8k
                            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
71.8k
                            {
4603
71.8k
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4604
71.8k
                                TIFFWarningExtR(
4605
71.8k
                                    tif, module, "Invalid data type for tag %s",
4606
71.8k
                                    fip ? fip->field_name : "unknown tagname");
4607
71.8k
                            }
4608
71.8k
                            break;
4609
154k
                    }
4610
154k
                    _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
4611
154k
                                sizeof(TIFFDirEntry));
4612
154k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4613
13
                        goto bad;
4614
154k
                }
4615
154k
                break;
4616
154k
                case TIFFTAG_STRIPBYTECOUNTS:
4617
78.2k
                case TIFFTAG_TILEBYTECOUNTS:
4618
78.2k
                {
4619
78.2k
                    switch (dp->tdir_type)
4620
78.2k
                    {
4621
4.91k
                        case TIFF_SHORT:
4622
36.2k
                        case TIFF_LONG:
4623
47.1k
                        case TIFF_LONG8:
4624
47.1k
                            break;
4625
31.1k
                        default:
4626
                            /* Warn except if directory typically created with
4627
                             * TIFFDeferStrileArrayWriting() */
4628
31.1k
                            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
31.1k
                            {
4632
31.1k
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4633
31.1k
                                TIFFWarningExtR(
4634
31.1k
                                    tif, module, "Invalid data type for tag %s",
4635
31.1k
                                    fip ? fip->field_name : "unknown tagname");
4636
31.1k
                            }
4637
31.1k
                            break;
4638
78.2k
                    }
4639
78.2k
                    _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
4640
78.2k
                                sizeof(TIFFDirEntry));
4641
78.2k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4642
9
                        goto bad;
4643
78.2k
                }
4644
78.2k
                break;
4645
78.2k
                case TIFFTAG_COLORMAP:
4646
14.2k
                case TIFFTAG_TRANSFERFUNCTION:
4647
14.2k
                {
4648
14.2k
                    enum TIFFReadDirEntryErr err;
4649
14.2k
                    uint32_t countpersample;
4650
14.2k
                    uint32_t countrequired;
4651
14.2k
                    uint32_t incrementpersample;
4652
14.2k
                    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
14.2k
                    if (!bitspersample_read)
4661
8.71k
                    {
4662
8.71k
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4663
8.71k
                        TIFFWarningExtR(
4664
8.71k
                            tif, module,
4665
8.71k
                            "Ignoring %s since BitsPerSample tag not found",
4666
8.71k
                            fip ? fip->field_name : "unknown tagname");
4667
8.71k
                        continue;
4668
8.71k
                    }
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
5.53k
                    if (tif->tif_dir.td_bitspersample > 24)
4673
524
                    {
4674
524
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4675
524
                        TIFFWarningExtR(
4676
524
                            tif, module,
4677
524
                            "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
4678
524
                            fip ? fip->field_name : "unknown tagname",
4679
524
                            tif->tif_dir.td_bitspersample);
4680
524
                        continue;
4681
524
                    }
4682
5.01k
                    countpersample = (1U << tif->tif_dir.td_bitspersample);
4683
5.01k
                    if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
4684
1.84k
                        (dp->tdir_count == (uint64_t)countpersample))
4685
233
                    {
4686
233
                        countrequired = countpersample;
4687
233
                        incrementpersample = 0;
4688
233
                    }
4689
4.78k
                    else
4690
4.78k
                    {
4691
4.78k
                        countrequired = 3 * countpersample;
4692
4.78k
                        incrementpersample = countpersample;
4693
4.78k
                    }
4694
5.01k
                    if (dp->tdir_count != (uint64_t)countrequired)
4695
2.43k
                        err = TIFFReadDirEntryErrCount;
4696
2.57k
                    else
4697
2.57k
                        err = TIFFReadDirEntryShortArray(tif, dp, &value);
4698
5.01k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
4699
7
                        goto bad;
4700
5.00k
                    if (err != TIFFReadDirEntryErrOk)
4701
2.84k
                    {
4702
2.84k
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4703
2.84k
                        TIFFReadDirEntryOutputErr(
4704
2.84k
                            tif, err, module,
4705
2.84k
                            fip ? fip->field_name : "unknown tagname", 1);
4706
2.84k
                    }
4707
2.16k
                    else
4708
2.16k
                    {
4709
2.16k
                        TIFFSetField(tif, dp->tdir_tag, value,
4710
2.16k
                                     value + incrementpersample,
4711
2.16k
                                     value + 2 * incrementpersample);
4712
2.16k
                        _TIFFfreeExt(tif, value);
4713
2.16k
                    }
4714
5.00k
                }
4715
0
                break;
4716
                    /* BEGIN REV 4.0 COMPATIBILITY */
4717
0
                case TIFFTAG_OSUBFILETYPE:
4718
0
                {
4719
0
                    uint16_t valueo;
4720
0
                    uint32_t value;
4721
0
                    if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
4722
0
                        TIFFReadDirEntryErrOk)
4723
0
                    {
4724
0
                        switch (valueo)
4725
0
                        {
4726
0
                            case OFILETYPE_REDUCEDIMAGE:
4727
0
                                value = FILETYPE_REDUCEDIMAGE;
4728
0
                                break;
4729
0
                            case OFILETYPE_PAGE:
4730
0
                                value = FILETYPE_PAGE;
4731
0
                                break;
4732
0
                            default:
4733
0
                                value = 0;
4734
0
                                break;
4735
0
                        }
4736
0
                        if (value != 0)
4737
0
                            TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
4738
0
                    }
4739
0
                }
4740
0
                break;
4741
                /* END REV 4.0 COMPATIBILITY */
4742
#if 0
4743
                case TIFFTAG_EP_BATTERYLEVEL:
4744
                    /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
4745
                     * LibTiff defines it as ASCII and converts RATIONAL to an
4746
                     * ASCII string. */
4747
                    switch (dp->tdir_type)
4748
                    {
4749
                        case TIFF_RATIONAL:
4750
                        {
4751
                            /* Read rational and convert to ASCII*/
4752
                            enum TIFFReadDirEntryErr err;
4753
                            TIFFRational_t rValue;
4754
                            err = TIFFReadDirEntryCheckedRationalDirect(
4755
                                tif, dp, &rValue);
4756
                            if (err != TIFFReadDirEntryErrOk)
4757
                            {
4758
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4759
                                TIFFReadDirEntryOutputErr(
4760
                                    tif, err, module,
4761
                                    fip ? fip->field_name : "unknown tagname",
4762
                                    1);
4763
                            }
4764
                            else
4765
                            {
4766
                                char szAux[32];
4767
                                snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
4768
                                         rValue.uNum, rValue.uDenom);
4769
                                TIFFSetField(tif, dp->tdir_tag, szAux);
4770
                            }
4771
                        }
4772
                        break;
4773
                        case TIFF_ASCII:
4774
                            (void)TIFFFetchNormalTag(tif, dp, TRUE);
4775
                            break;
4776
                        default:
4777
                            fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4778
                            TIFFWarningExtR(tif, module,
4779
                                            "Invalid data type for tag %s. "
4780
                                            "ASCII or RATIONAL expected",
4781
                                            fip ? fip->field_name
4782
                                                : "unknown tagname");
4783
                            break;
4784
                    }
4785
                    break;
4786
#endif
4787
2.93M
                default:
4788
2.93M
                    (void)TIFFFetchNormalTag(tif, dp, TRUE);
4789
2.93M
                    break;
4790
3.26M
            } /* -- switch (dp->tdir_tag) -- */
4791
3.26M
        }     /* -- if (!dp->tdir_ignore) */
4792
6.48M
    }         /* -- for-loop -- */
4793
4794
    /* Evaluate final IFD data size. */
4795
183k
    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
183k
    if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
4814
24.8k
    {
4815
24.8k
        if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
4816
19.7k
        {
4817
19.7k
            TIFFWarningExtR(
4818
19.7k
                tif, module,
4819
19.7k
                "Photometric tag is missing, assuming data is YCbCr");
4820
19.7k
            if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
4821
0
                goto bad;
4822
19.7k
        }
4823
5.00k
        else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4824
266
        {
4825
266
            tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
4826
266
            TIFFWarningExtR(tif, module,
4827
266
                            "Photometric tag value assumed incorrect, "
4828
266
                            "assuming data is YCbCr instead of RGB");
4829
266
        }
4830
24.8k
        if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
4831
22.3k
        {
4832
22.3k
            TIFFWarningExtR(
4833
22.3k
                tif, module,
4834
22.3k
                "BitsPerSample tag is missing, assuming 8 bits per sample");
4835
22.3k
            if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
4836
0
                goto bad;
4837
22.3k
        }
4838
24.8k
        if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
4839
22.3k
        {
4840
22.3k
            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
22.3k
            if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
4849
19.0k
            {
4850
19.0k
                TIFFWarningExtR(tif, module,
4851
19.0k
                                "SamplesPerPixel tag is missing, "
4852
19.0k
                                "applying correct SamplesPerPixel value of 3");
4853
19.0k
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4854
0
                    goto bad;
4855
19.0k
            }
4856
3.31k
            else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
4857
2.08k
                     (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
4858
1.39k
            {
4859
                /*
4860
                 * SamplesPerPixel tag is missing, but is not required
4861
                 * by spec.  Assume correct SamplesPerPixel value of 1.
4862
                 */
4863
1.39k
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
4864
0
                    goto bad;
4865
1.39k
            }
4866
22.3k
        }
4867
24.8k
    }
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
183k
    if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
4875
156k
    {
4876
156k
        tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
4877
156k
        tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
4878
156k
        tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
4879
156k
        tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
4880
156k
        tif->tif_flags &= ~TIFF_ISTILED;
4881
156k
    }
4882
26.8k
    else
4883
26.8k
    {
4884
26.8k
        tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
4885
26.8k
        tif->tif_flags |= TIFF_ISTILED;
4886
26.8k
    }
4887
183k
    if (!tif->tif_dir.td_nstrips)
4888
868
    {
4889
868
        TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
4890
868
                      isTiled(tif) ? "tiles" : "strips");
4891
868
        goto bad;
4892
868
    }
4893
182k
    tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
4894
182k
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
4895
11.6k
        tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
4896
182k
    if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
4897
33.9k
    {
4898
33.9k
#ifdef OJPEG_SUPPORT
4899
33.9k
        if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4900
13.0k
            (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
4901
12.9k
        {
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
12.9k
            TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
4911
12.9k
        }
4912
20.9k
        else
4913
20.9k
#endif
4914
20.9k
        {
4915
20.9k
            MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
4916
20.9k
            goto bad;
4917
20.9k
        }
4918
33.9k
    }
4919
4920
161k
    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
161k
    else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
4934
161k
    {
4935
161k
        if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
4936
148k
        {
4937
148k
            if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
4938
148k
                                     tif->tif_dir.td_nstrips,
4939
148k
                                     &tif->tif_dir.td_stripoffset_p))
4940
3.27k
            {
4941
3.27k
                goto bad;
4942
3.27k
            }
4943
148k
        }
4944
157k
        if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
4945
75.3k
        {
4946
75.3k
            if (!TIFFFetchStripThing(
4947
75.3k
                    tif, &(tif->tif_dir.td_stripbytecount_entry),
4948
75.3k
                    tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
4949
271
            {
4950
271
                goto bad;
4951
271
            }
4952
75.3k
        }
4953
157k
    }
4954
4955
    /*
4956
     * Make sure all non-color channels are extrasamples.
4957
     * If it's not the case, define them as such.
4958
     */
4959
157k
    color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
4960
157k
    if (color_channels &&
4961
116k
        tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
4962
116k
            color_channels)
4963
14.3k
    {
4964
14.3k
        uint16_t old_extrasamples;
4965
14.3k
        uint16_t *new_sampleinfo;
4966
4967
14.3k
        TIFFWarningExtR(
4968
14.3k
            tif, module,
4969
14.3k
            "Sum of Photometric type-related "
4970
14.3k
            "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4971
14.3k
            "Defining non-color channels as ExtraSamples.");
4972
4973
14.3k
        old_extrasamples = tif->tif_dir.td_extrasamples;
4974
14.3k
        tif->tif_dir.td_extrasamples =
4975
14.3k
            (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
4976
4977
        // sampleinfo should contain information relative to these new extra
4978
        // samples
4979
14.3k
        new_sampleinfo = (uint16_t *)_TIFFcallocExt(
4980
14.3k
            tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
4981
14.3k
        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
14.3k
        if (old_extrasamples > 0)
4992
680
            memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
4993
680
                   old_extrasamples * sizeof(uint16_t));
4994
14.3k
        _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
4995
14.3k
                              tif->tif_dir.td_extrasamples);
4996
14.3k
        _TIFFfreeExt(tif, new_sampleinfo);
4997
14.3k
    }
4998
4999
    /*
5000
     * Verify Palette image has a Colormap.
5001
     */
5002
157k
    if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
5003
2.58k
        !TIFFFieldSet(tif, FIELD_COLORMAP))
5004
695
    {
5005
695
        if (tif->tif_dir.td_bitspersample >= 8 &&
5006
676
            tif->tif_dir.td_samplesperpixel == 3)
5007
98
            tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
5008
597
        else if (tif->tif_dir.td_bitspersample >= 8)
5009
578
            tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
5010
19
        else
5011
19
        {
5012
19
            MissingRequired(tif, "Colormap");
5013
19
            goto bad;
5014
19
        }
5015
695
    }
5016
    /*
5017
     * OJPEG hack:
5018
     * We do no further messing with strip/tile offsets/bytecounts in OJPEG
5019
     * TIFFs
5020
     */
5021
157k
    if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
5022
133k
    {
5023
        /*
5024
         * Attempt to deal with a missing StripByteCounts tag.
5025
         */
5026
133k
        if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
5027
66.6k
        {
5028
            /*
5029
             * Some manufacturers violate the spec by not giving
5030
             * the size of the strips.  In this case, assume there
5031
             * is one uncompressed strip of data.
5032
             */
5033
66.6k
            if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5034
61.3k
                 tif->tif_dir.td_nstrips > 1) ||
5035
66.4k
                (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
5036
5.33k
                 tif->tif_dir.td_nstrips !=
5037
5.33k
                     (uint32_t)tif->tif_dir.td_samplesperpixel))
5038
252
            {
5039
252
                MissingRequired(tif, "StripByteCounts");
5040
252
                goto bad;
5041
252
            }
5042
66.4k
            TIFFWarningExtR(
5043
66.4k
                tif, module,
5044
66.4k
                "TIFF directory is missing required "
5045
66.4k
                "\"StripByteCounts\" field, calculating from imagelength");
5046
66.4k
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5047
566
                goto bad;
5048
66.4k
        }
5049
66.3k
        else if (tif->tif_dir.td_nstrips == 1 &&
5050
39.5k
                 !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
5051
5.42k
        {
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
5.42k
            TIFFWarningExtR(tif, module,
5059
5.42k
                            "Bogus \"StripByteCounts\" field, ignoring and "
5060
5.42k
                            "calculating from imagelength");
5061
5.42k
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5062
25
                goto bad;
5063
5.42k
        }
5064
60.9k
        else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
5065
60.9k
                 tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5066
55.4k
                 tif->tif_dir.td_nstrips > 2 &&
5067
20.8k
                 tif->tif_dir.td_compression == COMPRESSION_NONE &&
5068
8.07k
                 TIFFGetStrileByteCount(tif, 0) !=
5069
8.07k
                     TIFFGetStrileByteCount(tif, 1) &&
5070
7.49k
                 TIFFGetStrileByteCount(tif, 0) != 0 &&
5071
7.27k
                 TIFFGetStrileByteCount(tif, 1) != 0)
5072
6.07k
        {
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
6.07k
            TIFFWarningExtR(tif, module,
5084
6.07k
                            "Wrong \"StripByteCounts\" field, ignoring and "
5085
6.07k
                            "calculating from imagelength");
5086
6.07k
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5087
6
                goto bad;
5088
6.07k
        }
5089
133k
    }
5090
156k
    if (dir)
5091
156k
    {
5092
156k
        _TIFFfreeExt(tif, dir);
5093
156k
        dir = NULL;
5094
156k
    }
5095
156k
    if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
5096
155k
    {
5097
155k
        if (tif->tif_dir.td_bitspersample >= 16)
5098
17.8k
            tif->tif_dir.td_maxsamplevalue = 0xFFFF;
5099
138k
        else
5100
138k
            tif->tif_dir.td_maxsamplevalue =
5101
138k
                (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
5102
155k
    }
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
156k
    (*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
156k
    if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
5142
145k
        (tif->tif_dir.td_nstrips == 1) &&
5143
120k
        (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
5144
52.6k
        ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
5145
51.0k
    {
5146
51.0k
        ChopUpSingleUncompressedStrip(tif);
5147
51.0k
    }
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
156k
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5153
145k
        tif->tif_dir.td_compression == COMPRESSION_NONE &&
5154
61.0k
        (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
5155
56.9k
        TIFFStripSize64(tif) > 0x7FFFFFFFUL)
5156
7.90k
    {
5157
7.90k
        TryChopUpUncompressedBigTiff(tif);
5158
7.90k
    }
5159
5160
    /*
5161
     * Clear the dirty directory flag.
5162
     */
5163
156k
    tif->tif_flags &= ~TIFF_DIRTYDIRECT;
5164
156k
    tif->tif_flags &= ~TIFF_DIRTYSTRIP;
5165
5166
    /*
5167
     * Reinitialize i/o since we are starting on a new directory.
5168
     */
5169
156k
    tif->tif_row = (uint32_t)-1;
5170
156k
    tif->tif_curstrip = (uint32_t)-1;
5171
156k
    tif->tif_col = (uint32_t)-1;
5172
156k
    tif->tif_curtile = (uint32_t)-1;
5173
156k
    tif->tif_tilesize = (tmsize_t)-1;
5174
5175
156k
    tif->tif_scanlinesize = TIFFScanlineSize(tif);
5176
156k
    if (!tif->tif_scanlinesize)
5177
5.90k
    {
5178
5.90k
        TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
5179
5.90k
        return (0);
5180
5.90k
    }
5181
5182
150k
    if (isTiled(tif))
5183
25.9k
    {
5184
25.9k
        tif->tif_tilesize = TIFFTileSize(tif);
5185
25.9k
        if (!tif->tif_tilesize)
5186
37
        {
5187
37
            TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
5188
37
            return (0);
5189
37
        }
5190
25.9k
    }
5191
124k
    else
5192
124k
    {
5193
124k
        if (!TIFFStripSize(tif))
5194
7.55k
        {
5195
7.55k
            TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
5196
7.55k
            return (0);
5197
7.55k
        }
5198
124k
    }
5199
143k
    return (1);
5200
38.3k
bad:
5201
38.3k
    if (dir)
5202
38.3k
        _TIFFfreeExt(tif, dir);
5203
38.3k
    return (0);
5204
150k
} /*-- TIFFReadDirectory() --*/
5205
5206
static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
5207
                                        uint16_t dircount)
5208
211k
{
5209
211k
    static const char module[] = "TIFFReadDirectoryCheckOrder";
5210
211k
    uint32_t m;
5211
211k
    uint16_t n;
5212
211k
    TIFFDirEntry *o;
5213
211k
    m = 0;
5214
871k
    for (n = 0, o = dir; n < dircount; n++, o++)
5215
837k
    {
5216
837k
        if (o->tdir_tag < m)
5217
177k
        {
5218
177k
            TIFFWarningExtR(tif, module,
5219
177k
                            "Invalid TIFF directory; tags are not sorted in "
5220
177k
                            "ascending order");
5221
177k
            break;
5222
177k
        }
5223
660k
        m = o->tdir_tag + 1;
5224
660k
    }
5225
211k
}
5226
5227
static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
5228
                                                uint16_t dircount,
5229
                                                uint16_t tagid)
5230
391k
{
5231
391k
    TIFFDirEntry *m;
5232
391k
    uint16_t n;
5233
391k
    (void)tif;
5234
8.77M
    for (m = dir, n = 0; n < dircount; m++, n++)
5235
8.55M
    {
5236
8.55M
        if (m->tdir_tag == tagid)
5237
178k
            return (m);
5238
8.55M
    }
5239
212k
    return (0);
5240
391k
}
5241
5242
static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
5243
                                           uint32_t *fii)
5244
13.4M
{
5245
13.4M
    int32_t ma, mb, mc;
5246
13.4M
    ma = -1;
5247
13.4M
    mc = (int32_t)tif->tif_nfields;
5248
93.7M
    while (1)
5249
93.7M
    {
5250
93.7M
        if (ma + 1 == mc)
5251
3.32M
        {
5252
3.32M
            *fii = FAILED_FII;
5253
3.32M
            return;
5254
3.32M
        }
5255
90.3M
        mb = (ma + mc) / 2;
5256
90.3M
        if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
5257
10.1M
            break;
5258
80.2M
        if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
5259
37.2M
            ma = mb;
5260
43.0M
        else
5261
43.0M
            mc = mb;
5262
80.2M
    }
5263
10.1M
    while (1)
5264
10.1M
    {
5265
10.1M
        if (mb == 0)
5266
707k
            break;
5267
9.40M
        if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
5268
9.40M
            break;
5269
0
        mb--;
5270
0
    }
5271
10.1M
    *fii = mb;
5272
10.1M
}
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
30.7k
{
5281
30.7k
    static const char module[] = "TIFFReadCustomDirectory";
5282
30.7k
    TIFFDirEntry *dir;
5283
30.7k
    uint16_t dircount;
5284
30.7k
    TIFFDirEntry *dp;
5285
30.7k
    uint16_t di;
5286
30.7k
    const TIFFField *fip;
5287
30.7k
    uint32_t fii;
5288
5289
30.7k
    assert(infoarray != NULL);
5290
30.7k
    dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
5291
30.7k
    if (!dircount)
5292
14.1k
    {
5293
14.1k
        TIFFErrorExtR(tif, module,
5294
14.1k
                      "Failed to read custom directory at offset %" PRIu64,
5295
14.1k
                      diroff);
5296
14.1k
        return 0;
5297
14.1k
    }
5298
16.5k
    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
16.5k
    {
5305
16.5k
        TIFFDirEntry *ma;
5306
16.5k
        uint16_t mb;
5307
1.86M
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
5308
1.84M
        {
5309
1.84M
            TIFFDirEntry *na;
5310
1.84M
            uint16_t nb;
5311
188M
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
5312
186M
            {
5313
186M
                if (ma->tdir_tag == na->tdir_tag)
5314
12.0M
                {
5315
12.0M
                    na->tdir_ignore = TRUE;
5316
12.0M
                }
5317
186M
            }
5318
1.84M
        }
5319
16.5k
    }
5320
5321
    /* Free any old stuff and reinit. */
5322
16.5k
    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
16.5k
    TIFFDefaultDirectory(tif);
5330
16.5k
    _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
16.5k
    tif->tif_dir.td_dirdatasize_offsets =
5335
16.5k
        (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
5336
16.5k
            tif, dircount * sizeof(TIFFEntryOffsetAndLength));
5337
16.5k
    if (tif->tif_dir.td_dirdatasize_offsets == NULL)
5338
0
    {
5339
0
        TIFFErrorExtR(
5340
0
            tif, module,
5341
0
            "Failed to allocate memory for counting IFD data size at reading");
5342
0
        if (dir)
5343
0
            _TIFFfreeExt(tif, dir);
5344
0
        return 0;
5345
0
    }
5346
5347
1.86M
    for (di = 0, dp = dir; di < dircount; di++, dp++)
5348
1.84M
    {
5349
1.84M
        TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5350
1.84M
        if (fii == FAILED_FII)
5351
908k
        {
5352
908k
            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
908k
            const TIFFField *fld = _TIFFCreateAnonField(
5360
908k
                tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
5361
908k
            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
908k
            else
5374
908k
            {
5375
908k
                TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5376
908k
                assert(fii != FAILED_FII);
5377
908k
            }
5378
908k
        }
5379
1.84M
        if (!dp->tdir_ignore)
5380
956k
        {
5381
956k
            fip = tif->tif_fields[fii];
5382
956k
            if (fip->field_bit == FIELD_IGNORE)
5383
0
                dp->tdir_ignore = TRUE;
5384
956k
            else
5385
956k
            {
5386
                /* check data type */
5387
956k
                while ((fip->field_type != TIFF_ANY) &&
5388
840k
                       (fip->field_type != dp->tdir_type))
5389
34.6k
                {
5390
34.6k
                    fii++;
5391
34.6k
                    if ((fii == tif->tif_nfields) ||
5392
34.5k
                        (tif->tif_fields[fii]->field_tag !=
5393
34.5k
                         (uint32_t)dp->tdir_tag))
5394
34.6k
                    {
5395
34.6k
                        fii = 0xFFFF;
5396
34.6k
                        break;
5397
34.6k
                    }
5398
0
                    fip = tif->tif_fields[fii];
5399
0
                }
5400
956k
                if (fii == 0xFFFF)
5401
34.6k
                {
5402
34.6k
                    TIFFWarningExtR(tif, module,
5403
34.6k
                                    "Wrong data type %" PRIu16
5404
34.6k
                                    " for \"%s\"; tag ignored",
5405
34.6k
                                    dp->tdir_type, fip->field_name);
5406
34.6k
                    dp->tdir_ignore = TRUE;
5407
34.6k
                }
5408
921k
                else
5409
921k
                {
5410
                    /* check count if known in advance */
5411
921k
                    if ((fip->field_readcount != TIFF_VARIABLE) &&
5412
921k
                        (fip->field_readcount != TIFF_VARIABLE2))
5413
12.8k
                    {
5414
12.8k
                        uint32_t expected;
5415
12.8k
                        if (fip->field_readcount == TIFF_SPP)
5416
0
                            expected =
5417
0
                                (uint32_t)tif->tif_dir.td_samplesperpixel;
5418
12.8k
                        else
5419
12.8k
                            expected = (uint32_t)fip->field_readcount;
5420
12.8k
                        if (!CheckDirCount(tif, dp, expected))
5421
1.00k
                            dp->tdir_ignore = TRUE;
5422
12.8k
                    }
5423
921k
                }
5424
956k
            }
5425
956k
            if (!dp->tdir_ignore)
5426
920k
            {
5427
920k
                switch (dp->tdir_tag)
5428
920k
                {
5429
687
                    case EXIFTAG_SUBJECTDISTANCE:
5430
687
                        if (!TIFFFieldIsAnonymous(fip))
5431
599
                        {
5432
                            /* should only be called on a Exif directory */
5433
                            /* when exifFields[] is active */
5434
599
                            (void)TIFFFetchSubjectDistance(tif, dp);
5435
599
                        }
5436
88
                        else
5437
88
                        {
5438
88
                            (void)TIFFFetchNormalTag(tif, dp, TRUE);
5439
88
                        }
5440
687
                        break;
5441
920k
                    default:
5442
920k
                        (void)TIFFFetchNormalTag(tif, dp, TRUE);
5443
920k
                        break;
5444
920k
                }
5445
920k
            } /*-- if (!dp->tdir_ignore) */
5446
956k
        }
5447
1.84M
    }
5448
    /* Evaluate final IFD data size. */
5449
16.5k
    CalcFinalIFDdatasizeReading(tif, dircount);
5450
5451
    /* To be able to return from SubIFD or custom-IFD to main-IFD */
5452
16.5k
    tif->tif_setdirectory_force_absolute = TRUE;
5453
16.5k
    if (dir)
5454
16.5k
        _TIFFfreeExt(tif, dir);
5455
16.5k
    return 1;
5456
16.5k
}
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
19.8k
{
5464
19.8k
    return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
5465
19.8k
}
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
10.8k
{
5472
10.8k
    return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
5473
10.8k
}
5474
5475
static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
5476
                                   uint16_t dircount)
5477
77.9k
{
5478
77.9k
    static const char module[] = "EstimateStripByteCounts";
5479
5480
77.9k
    TIFFDirEntry *dp;
5481
77.9k
    TIFFDirectory *td = &tif->tif_dir;
5482
77.9k
    uint32_t strip;
5483
5484
    /* Do not try to load stripbytecount as we will compute it */
5485
77.9k
    if (!_TIFFFillStrilesInternal(tif, 0))
5486
0
        return -1;
5487
5488
77.9k
    const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
5489
77.9k
    uint64_t filesize = 0;
5490
77.9k
    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
77.9k
    if (td->td_stripbytecount_p)
5507
11.4k
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
5508
77.9k
    td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
5509
77.9k
        tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
5510
77.9k
    if (td->td_stripbytecount_p == NULL)
5511
0
        return -1;
5512
5513
77.9k
    if (td->td_compression != COMPRESSION_NONE)
5514
32.5k
    {
5515
32.5k
        uint64_t space;
5516
32.5k
        uint16_t n;
5517
32.5k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
5518
31.9k
            space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
5519
578
        else
5520
578
            space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
5521
        /* calculate amount of space used by indirect values */
5522
392k
        for (dp = dir, n = dircount; n > 0; n--, dp++)
5523
360k
        {
5524
360k
            uint32_t typewidth;
5525
360k
            uint64_t datasize;
5526
360k
            typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type);
5527
360k
            if (typewidth == 0)
5528
355
            {
5529
355
                TIFFErrorExtR(
5530
355
                    tif, module,
5531
355
                    "Cannot determine size of unknown tag type %" PRIu16,
5532
355
                    dp->tdir_type);
5533
355
                return -1;
5534
355
            }
5535
359k
            if (dp->tdir_count > UINT64_MAX / typewidth)
5536
13
                return -1;
5537
359k
            datasize = (uint64_t)typewidth * dp->tdir_count;
5538
359k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
5539
356k
            {
5540
356k
                if (datasize <= 4)
5541
218k
                    datasize = 0;
5542
356k
            }
5543
3.51k
            else
5544
3.51k
            {
5545
3.51k
                if (datasize <= 8)
5546
1.77k
                    datasize = 0;
5547
3.51k
            }
5548
359k
            if (space > UINT64_MAX - datasize)
5549
180
                return -1;
5550
359k
            space += datasize;
5551
359k
        }
5552
31.9k
        if (filesize == 0)
5553
31.9k
            filesize = TIFFGetFileSize(tif);
5554
31.9k
        if (filesize < space)
5555
            /* we should perhaps return in error ? */
5556
31.4k
            space = filesize;
5557
518
        else
5558
518
            space = filesize - space;
5559
31.9k
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
5560
3.58k
            space /= td->td_samplesperpixel;
5561
513k
        for (strip = 0; strip < td->td_nstrips; strip++)
5562
481k
            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
31.9k
        strip--;
5571
31.9k
        if (td->td_stripoffset_p[strip] >
5572
31.9k
            UINT64_MAX - td->td_stripbytecount_p[strip])
5573
9
            return -1;
5574
31.9k
        if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
5575
31.9k
            filesize)
5576
25.6k
        {
5577
25.6k
            if (td->td_stripoffset_p[strip] >= filesize)
5578
2.55k
            {
5579
                /* Not sure what we should in that case... */
5580
2.55k
                td->td_stripbytecount_p[strip] = 0;
5581
2.55k
            }
5582
23.0k
            else
5583
23.0k
            {
5584
23.0k
                td->td_stripbytecount_p[strip] =
5585
23.0k
                    filesize - td->td_stripoffset_p[strip];
5586
23.0k
            }
5587
25.6k
        }
5588
31.9k
    }
5589
45.4k
    else if (isTiled(tif))
5590
2.90k
    {
5591
2.90k
        uint64_t bytespertile = TIFFTileSize64(tif);
5592
5593
11.0M
        for (strip = 0; strip < td->td_nstrips; strip++)
5594
11.0M
            td->td_stripbytecount_p[strip] = bytespertile;
5595
2.90k
    }
5596
42.5k
    else
5597
42.5k
    {
5598
42.5k
        uint64_t rowbytes = TIFFScanlineSize64(tif);
5599
42.5k
        uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
5600
104M
        for (strip = 0; strip < td->td_nstrips; strip++)
5601
104M
        {
5602
104M
            if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
5603
40
                return -1;
5604
104M
            td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
5605
104M
        }
5606
42.5k
    }
5607
77.3k
    TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
5608
77.3k
    if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
5609
62.5k
        td->td_rowsperstrip = td->td_imagelength;
5610
77.3k
    return 1;
5611
77.9k
}
5612
5613
static void MissingRequired(TIFF *tif, const char *tagname)
5614
23.1k
{
5615
23.1k
    static const char module[] = "MissingRequired";
5616
5617
23.1k
    TIFFErrorExtR(tif, module,
5618
23.1k
                  "TIFF directory is missing required \"%s\" field", tagname);
5619
23.1k
}
5620
5621
static unsigned long hashFuncOffsetToNumber(const void *elt)
5622
852k
{
5623
852k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5624
852k
        (const TIFFOffsetAndDirNumber *)elt;
5625
852k
    const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
5626
852k
                          ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
5627
852k
    return hash;
5628
852k
}
5629
5630
static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
5631
71.6k
{
5632
71.6k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5633
71.6k
        (const TIFFOffsetAndDirNumber *)elt1;
5634
71.6k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5635
71.6k
        (const TIFFOffsetAndDirNumber *)elt2;
5636
71.6k
    return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
5637
71.6k
}
5638
5639
static unsigned long hashFuncNumberToOffset(const void *elt)
5640
797k
{
5641
797k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5642
797k
        (const TIFFOffsetAndDirNumber *)elt;
5643
797k
    return offsetAndDirNumber->dirNumber;
5644
797k
}
5645
5646
static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
5647
14.1k
{
5648
14.1k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5649
14.1k
        (const TIFFOffsetAndDirNumber *)elt1;
5650
14.1k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5651
14.1k
        (const TIFFOffsetAndDirNumber *)elt2;
5652
14.1k
    return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
5653
14.1k
}
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
330k
{
5667
330k
    if (diroff == 0) /* no more directories */
5668
0
        return 0;
5669
5670
330k
    if (tif->tif_map_dir_offset_to_number == NULL)
5671
182k
    {
5672
182k
        tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
5673
182k
            hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
5674
182k
        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
182k
    }
5681
5682
330k
    if (tif->tif_map_dir_number_to_offset == NULL)
5683
182k
    {
5684
        /* No free callback for this map, as it shares the same items as
5685
         * tif->tif_map_dir_offset_to_number. */
5686
182k
        tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
5687
182k
            hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
5688
182k
        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
182k
    }
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
330k
    TIFFOffsetAndDirNumber entry;
5702
330k
    entry.offset = diroff;
5703
330k
    entry.dirNumber = dirn;
5704
5705
330k
    TIFFOffsetAndDirNumber *foundEntry =
5706
330k
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5707
330k
            tif->tif_map_dir_offset_to_number, &entry);
5708
330k
    if (foundEntry)
5709
69.0k
    {
5710
69.0k
        if (foundEntry->dirNumber == dirn)
5711
67.9k
        {
5712
67.9k
            return 1;
5713
67.9k
        }
5714
1.02k
        else
5715
1.02k
        {
5716
1.02k
            TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
5717
1.02k
                            "TIFF directory %d has IFD looping to directory %u "
5718
1.02k
                            "at offset 0x%" PRIx64 " (%" PRIu64 ")",
5719
1.02k
                            (int)dirn - 1, foundEntry->dirNumber, diroff,
5720
1.02k
                            diroff);
5721
1.02k
            return 0;
5722
1.02k
        }
5723
69.0k
    }
5724
5725
    /* Check if offset of an IFD has been changed and update offset of that IFD
5726
     * number. */
5727
261k
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5728
261k
        tif->tif_map_dir_number_to_offset, &entry);
5729
261k
    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
261k
    if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
5785
261k
        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
261k
    TIFFOffsetAndDirNumber *entryPtr =
5794
261k
        (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
5795
261k
    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
261k
    *entryPtr = entry;
5804
5805
261k
    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
261k
    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
261k
    return 1;
5819
261k
} /* --- _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
14.1k
{
5875
5876
14.1k
    if (tif->tif_map_dir_number_to_offset == NULL)
5877
0
        return 0;
5878
14.1k
    TIFFOffsetAndDirNumber entry;
5879
14.1k
    entry.offset = 0; /* not used */
5880
14.1k
    entry.dirNumber = dirn;
5881
5882
14.1k
    TIFFOffsetAndDirNumber *foundEntry =
5883
14.1k
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5884
14.1k
            tif->tif_map_dir_number_to_offset, &entry);
5885
14.1k
    if (foundEntry)
5886
14.1k
    {
5887
14.1k
        *diroff = foundEntry->offset;
5888
14.1k
        return 1;
5889
14.1k
    }
5890
5891
0
    return 0;
5892
14.1k
} /*--- _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
12.8k
{
5949
12.8k
    if ((uint64_t)count > dir->tdir_count)
5950
1.00k
    {
5951
1.00k
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
5952
1.00k
        TIFFWarningExtR(tif, tif->tif_name,
5953
1.00k
                        "incorrect count for field \"%s\" (%" PRIu64
5954
1.00k
                        ", expecting %" PRIu32 "); tag ignored",
5955
1.00k
                        fip ? fip->field_name : "unknown tagname",
5956
1.00k
                        dir->tdir_count, count);
5957
1.00k
        return (0);
5958
1.00k
    }
5959
11.7k
    else if ((uint64_t)count < dir->tdir_count)
5960
7.98k
    {
5961
7.98k
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
5962
7.98k
        TIFFWarningExtR(tif, tif->tif_name,
5963
7.98k
                        "incorrect count for field \"%s\" (%" PRIu64
5964
7.98k
                        ", expecting %" PRIu32 "); tag trimmed",
5965
7.98k
                        fip ? fip->field_name : "unknown tagname",
5966
7.98k
                        dir->tdir_count, count);
5967
7.98k
        dir->tdir_count = count;
5968
7.98k
        return (1);
5969
7.98k
    }
5970
3.81k
    return (1);
5971
12.8k
}
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
267k
{
5981
267k
    static const char module[] = "TIFFFetchDirectory";
5982
5983
267k
    void *origdir;
5984
267k
    uint16_t dircount16;
5985
267k
    uint32_t dirsize;
5986
267k
    TIFFDirEntry *dir;
5987
267k
    uint8_t *ma;
5988
267k
    TIFFDirEntry *mb;
5989
267k
    uint16_t n;
5990
5991
267k
    assert(pdir);
5992
5993
267k
    tif->tif_diroff = diroff;
5994
267k
    if (nextdiroff)
5995
236k
        *nextdiroff = 0;
5996
267k
    if (!isMapped(tif))
5997
5.43k
    {
5998
5.43k
        if (!SeekOK(tif, tif->tif_diroff))
5999
0
        {
6000
0
            TIFFErrorExtR(tif, module,
6001
0
                          "%s: Seek error accessing TIFF directory",
6002
0
                          tif->tif_name);
6003
0
            return 0;
6004
0
        }
6005
5.43k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
6006
5.43k
        {
6007
5.43k
            if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
6008
0
            {
6009
0
                TIFFErrorExtR(tif, module,
6010
0
                              "%s: Can not read TIFF directory count",
6011
0
                              tif->tif_name);
6012
0
                return 0;
6013
0
            }
6014
5.43k
            if (tif->tif_flags & TIFF_SWAB)
6015
0
                TIFFSwabShort(&dircount16);
6016
5.43k
            if (dircount16 > 4096)
6017
0
            {
6018
0
                TIFFErrorExtR(tif, module,
6019
0
                              "Sanity check on directory count failed, this is "
6020
0
                              "probably not a valid IFD offset");
6021
0
                return 0;
6022
0
            }
6023
5.43k
            dirsize = 12;
6024
5.43k
        }
6025
0
        else
6026
0
        {
6027
0
            uint64_t dircount64;
6028
0
            if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
6029
0
            {
6030
0
                TIFFErrorExtR(tif, module,
6031
0
                              "%s: Can not read TIFF directory count",
6032
0
                              tif->tif_name);
6033
0
                return 0;
6034
0
            }
6035
0
            if (tif->tif_flags & TIFF_SWAB)
6036
0
                TIFFSwabLong8(&dircount64);
6037
0
            if (dircount64 > 4096)
6038
0
            {
6039
0
                TIFFErrorExtR(tif, module,
6040
0
                              "Sanity check on directory count failed, this is "
6041
0
                              "probably not a valid IFD offset");
6042
0
                return 0;
6043
0
            }
6044
0
            dircount16 = (uint16_t)dircount64;
6045
0
            dirsize = 20;
6046
0
        }
6047
5.43k
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6048
5.43k
                                   "to read TIFF directory");
6049
5.43k
        if (origdir == NULL)
6050
0
            return 0;
6051
5.43k
        if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
6052
0
        {
6053
0
            TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
6054
0
                          tif->tif_name);
6055
0
            _TIFFfreeExt(tif, origdir);
6056
0
            return 0;
6057
0
        }
6058
        /*
6059
         * Read offset to next directory for sequential scans if
6060
         * needed.
6061
         */
6062
5.43k
        if (nextdiroff)
6063
5.43k
        {
6064
5.43k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
6065
5.43k
            {
6066
5.43k
                uint32_t nextdiroff32;
6067
5.43k
                if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
6068
0
                    nextdiroff32 = 0;
6069
5.43k
                if (tif->tif_flags & TIFF_SWAB)
6070
0
                    TIFFSwabLong(&nextdiroff32);
6071
5.43k
                *nextdiroff = nextdiroff32;
6072
5.43k
            }
6073
0
            else
6074
0
            {
6075
0
                if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
6076
0
                    *nextdiroff = 0;
6077
0
                if (tif->tif_flags & TIFF_SWAB)
6078
0
                    TIFFSwabLong8(nextdiroff);
6079
0
            }
6080
5.43k
        }
6081
5.43k
    }
6082
262k
    else
6083
262k
    {
6084
262k
        tmsize_t m;
6085
262k
        tmsize_t off;
6086
262k
        if (tif->tif_diroff > (uint64_t)INT64_MAX)
6087
419
        {
6088
419
            TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6089
419
            return (0);
6090
419
        }
6091
261k
        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
261k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
6103
252k
        {
6104
252k
            m = off + sizeof(uint16_t);
6105
252k
            if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) ||
6106
252k
                (m > tif->tif_size))
6107
44.0k
            {
6108
44.0k
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6109
44.0k
                return 0;
6110
44.0k
            }
6111
208k
            else
6112
208k
            {
6113
208k
                _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
6114
208k
            }
6115
208k
            off += sizeof(uint16_t);
6116
208k
            if (tif->tif_flags & TIFF_SWAB)
6117
78.8k
                TIFFSwabShort(&dircount16);
6118
208k
            if (dircount16 > 4096)
6119
3.10k
            {
6120
3.10k
                TIFFErrorExtR(tif, module,
6121
3.10k
                              "Sanity check on directory count failed, this is "
6122
3.10k
                              "probably not a valid IFD offset");
6123
3.10k
                return 0;
6124
3.10k
            }
6125
205k
            dirsize = 12;
6126
205k
        }
6127
9.31k
        else
6128
9.31k
        {
6129
9.31k
            uint64_t dircount64;
6130
9.31k
            m = off + sizeof(uint64_t);
6131
9.31k
            if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
6132
9.30k
                (m > tif->tif_size))
6133
905
            {
6134
905
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
6135
905
                return 0;
6136
905
            }
6137
8.40k
            else
6138
8.40k
            {
6139
8.40k
                _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
6140
8.40k
            }
6141
8.40k
            off += sizeof(uint64_t);
6142
8.40k
            if (tif->tif_flags & TIFF_SWAB)
6143
325
                TIFFSwabLong8(&dircount64);
6144
8.40k
            if (dircount64 > 4096)
6145
489
            {
6146
489
                TIFFErrorExtR(tif, module,
6147
489
                              "Sanity check on directory count failed, this is "
6148
489
                              "probably not a valid IFD offset");
6149
489
                return 0;
6150
489
            }
6151
7.91k
            dircount16 = (uint16_t)dircount64;
6152
7.91k
            dirsize = 20;
6153
7.91k
        }
6154
213k
        if (dircount16 == 0)
6155
1.12k
        {
6156
1.12k
            TIFFErrorExtR(tif, module,
6157
1.12k
                          "Sanity check on directory count failed, zero tag "
6158
1.12k
                          "directories not supported");
6159
1.12k
            return 0;
6160
1.12k
        }
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
212k
        uint64_t filesize = TIFFGetFileSize(tif);
6164
212k
        uint64_t allocsize = (uint64_t)dircount16 * dirsize;
6165
212k
        if (allocsize > filesize)
6166
4.82k
        {
6167
4.82k
            TIFFWarningExtR(
6168
4.82k
                tif, module,
6169
4.82k
                "Requested memory size for TIFF directory of %" PRIu64
6170
4.82k
                " is greater than filesize %" PRIu64
6171
4.82k
                ". Memory not allocated, TIFF directory not read",
6172
4.82k
                allocsize, filesize);
6173
4.82k
            return 0;
6174
4.82k
        }
6175
207k
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
6176
207k
                                   "to read TIFF directory");
6177
207k
        if (origdir == NULL)
6178
0
            return 0;
6179
207k
        m = off + dircount16 * dirsize;
6180
207k
        if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
6181
207k
            (m > tif->tif_size))
6182
1.02k
        {
6183
1.02k
            TIFFErrorExtR(tif, module, "Can not read TIFF directory");
6184
1.02k
            _TIFFfreeExt(tif, origdir);
6185
1.02k
            return 0;
6186
1.02k
        }
6187
206k
        else
6188
206k
        {
6189
206k
            _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
6190
206k
        }
6191
206k
        if (nextdiroff)
6192
189k
        {
6193
189k
            off += dircount16 * dirsize;
6194
189k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
6195
181k
            {
6196
181k
                uint32_t nextdiroff32;
6197
181k
                m = off + sizeof(uint32_t);
6198
181k
                if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) ||
6199
181k
                    (m > tif->tif_size))
6200
24.1k
                    nextdiroff32 = 0;
6201
157k
                else
6202
157k
                    _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
6203
157k
                                sizeof(uint32_t));
6204
181k
                if (tif->tif_flags & TIFF_SWAB)
6205
66.2k
                    TIFFSwabLong(&nextdiroff32);
6206
181k
                *nextdiroff = nextdiroff32;
6207
181k
            }
6208
7.84k
            else
6209
7.84k
            {
6210
7.84k
                m = off + sizeof(uint64_t);
6211
7.84k
                if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
6212
7.84k
                    (m > tif->tif_size))
6213
5.59k
                    *nextdiroff = 0;
6214
2.25k
                else
6215
2.25k
                    _TIFFmemcpy(nextdiroff, tif->tif_base + off,
6216
2.25k
                                sizeof(uint64_t));
6217
7.84k
                if (tif->tif_flags & TIFF_SWAB)
6218
301
                    TIFFSwabLong8(nextdiroff);
6219
7.84k
            }
6220
189k
        }
6221
206k
    }
6222
    /* No check against filesize needed here because "dir" should have same size
6223
     * than "origdir" checked above. */
6224
211k
    dir = (TIFFDirEntry *)_TIFFCheckMalloc(
6225
211k
        tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
6226
211k
    if (dir == 0)
6227
0
    {
6228
0
        _TIFFfreeExt(tif, origdir);
6229
0
        return 0;
6230
0
    }
6231
211k
    ma = (uint8_t *)origdir;
6232
211k
    mb = dir;
6233
8.59M
    for (n = 0; n < dircount16; n++)
6234
8.37M
    {
6235
8.37M
        mb->tdir_ignore = FALSE;
6236
8.37M
        if (tif->tif_flags & TIFF_SWAB)
6237
3.04M
            TIFFSwabShort((uint16_t *)ma);
6238
8.37M
        mb->tdir_tag = *(uint16_t *)ma;
6239
8.37M
        ma += sizeof(uint16_t);
6240
8.37M
        if (tif->tif_flags & TIFF_SWAB)
6241
3.04M
            TIFFSwabShort((uint16_t *)ma);
6242
8.37M
        mb->tdir_type = *(uint16_t *)ma;
6243
8.37M
        ma += sizeof(uint16_t);
6244
8.37M
        if (!(tif->tif_flags & TIFF_BIGTIFF))
6245
8.32M
        {
6246
8.32M
            if (tif->tif_flags & TIFF_SWAB)
6247
3.03M
                TIFFSwabLong((uint32_t *)ma);
6248
8.32M
            mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
6249
8.32M
            ma += sizeof(uint32_t);
6250
8.32M
            mb->tdir_offset.toff_long8 = 0;
6251
8.32M
            *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
6252
8.32M
            ma += sizeof(uint32_t);
6253
8.32M
        }
6254
54.0k
        else
6255
54.0k
        {
6256
54.0k
            if (tif->tif_flags & TIFF_SWAB)
6257
1.80k
                TIFFSwabLong8((uint64_t *)ma);
6258
54.0k
            mb->tdir_count = TIFFReadUInt64(ma);
6259
54.0k
            ma += sizeof(uint64_t);
6260
54.0k
            mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
6261
54.0k
            ma += sizeof(uint64_t);
6262
54.0k
        }
6263
8.37M
        mb++;
6264
8.37M
    }
6265
211k
    _TIFFfreeExt(tif, origdir);
6266
211k
    *pdir = dir;
6267
211k
    return dircount16;
6268
211k
}
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
4.38M
{
6275
4.38M
    static const char module[] = "TIFFFetchNormalTag";
6276
4.38M
    enum TIFFReadDirEntryErr err;
6277
4.38M
    uint32_t fii;
6278
4.38M
    const TIFFField *fip = NULL;
6279
4.38M
    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
6280
4.38M
    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
4.38M
    fip = tif->tif_fields[fii];
6287
4.38M
    assert(fip != NULL); /* should not happen */
6288
4.38M
    assert(fip->set_get_field_type !=
6289
4.38M
           TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
6290
                                  this in specialized code */
6291
4.38M
    assert(fip->set_get_field_type !=
6292
4.38M
           TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
6293
                                the case for pseudo-tags */
6294
4.38M
    err = TIFFReadDirEntryErrOk;
6295
4.38M
    switch (fip->set_get_field_type)
6296
4.38M
    {
6297
2.31M
        case TIFF_SETGET_UNDEFINED:
6298
2.31M
            TIFFErrorExtR(
6299
2.31M
                tif, "TIFFFetchNormalTag",
6300
2.31M
                "Defined set_get_field_type of custom tag %u (%s) is "
6301
2.31M
                "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
6302
2.31M
                fip->field_tag, fip->field_name);
6303
2.31M
            break;
6304
74.2k
        case TIFF_SETGET_ASCII:
6305
74.2k
        {
6306
74.2k
            uint8_t *data;
6307
74.2k
            assert(fip->field_passcount == 0);
6308
74.2k
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
6309
74.2k
            if (err == TIFFReadDirEntryErrOk)
6310
24.2k
            {
6311
24.2k
                size_t mb = 0;
6312
24.2k
                int n;
6313
24.2k
                if (data != NULL)
6314
19.8k
                {
6315
19.8k
                    if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
6316
9.27k
                    {
6317
                        /* optimization: if data is known to be 0 terminated, we
6318
                         * can use strlen() */
6319
9.27k
                        mb = strlen((const char *)data);
6320
9.27k
                    }
6321
10.5k
                    else
6322
10.5k
                    {
6323
                        /* general case. equivalent to non-portable */
6324
                        /* mb = strnlen((const char*)data,
6325
                         * (uint32_t)dp->tdir_count); */
6326
10.5k
                        uint8_t *ma = data;
6327
41.1k
                        while (mb < (uint32_t)dp->tdir_count)
6328
35.5k
                        {
6329
35.5k
                            if (*ma == 0)
6330
4.99k
                                break;
6331
30.5k
                            ma++;
6332
30.5k
                            mb++;
6333
30.5k
                        }
6334
10.5k
                    }
6335
19.8k
                }
6336
24.2k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6337
15
                {
6338
15
                    if (data != NULL)
6339
15
                        _TIFFfreeExt(tif, data);
6340
15
                    return (0);
6341
15
                }
6342
24.2k
                if (mb + 1 < (uint32_t)dp->tdir_count)
6343
9.61k
                    TIFFWarningExtR(
6344
9.61k
                        tif, module,
6345
9.61k
                        "ASCII value for tag \"%s\" contains null byte in "
6346
9.61k
                        "value; value incorrectly truncated during reading due "
6347
9.61k
                        "to implementation limitations",
6348
9.61k
                        fip->field_name);
6349
14.6k
                else if (mb + 1 > (uint32_t)dp->tdir_count)
6350
9.99k
                {
6351
9.99k
                    TIFFWarningExtR(tif, module,
6352
9.99k
                                    "ASCII value for tag \"%s\" does not end "
6353
9.99k
                                    "in null byte. Forcing it to be null",
6354
9.99k
                                    fip->field_name);
6355
                    /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
6356
                     * larger than MAX_SIZE_TAG_DATA */
6357
9.99k
                    assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
6358
9.99k
                    uint8_t *o =
6359
9.99k
                        _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
6360
9.99k
                    if (o == NULL)
6361
0
                    {
6362
0
                        if (data != NULL)
6363
0
                            _TIFFfreeExt(tif, data);
6364
0
                        return (0);
6365
0
                    }
6366
9.99k
                    if (dp->tdir_count > 0)
6367
5.55k
                    {
6368
5.55k
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6369
5.55k
                    }
6370
9.99k
                    o[(uint32_t)dp->tdir_count] = 0;
6371
9.99k
                    if (data != 0)
6372
5.55k
                        _TIFFfreeExt(tif, data);
6373
9.99k
                    data = o;
6374
9.99k
                }
6375
24.2k
                n = TIFFSetField(tif, dp->tdir_tag, data);
6376
24.2k
                if (data != 0)
6377
24.2k
                    _TIFFfreeExt(tif, data);
6378
24.2k
                if (!n)
6379
0
                    return (0);
6380
24.2k
            }
6381
74.2k
        }
6382
74.2k
        break;
6383
74.2k
        case TIFF_SETGET_UINT8:
6384
11.2k
        {
6385
11.2k
            uint8_t data = 0;
6386
11.2k
            assert(fip->field_readcount == 1);
6387
11.2k
            assert(fip->field_passcount == 0);
6388
11.2k
            err = TIFFReadDirEntryByte(tif, dp, &data);
6389
11.2k
            if (err == TIFFReadDirEntryErrOk)
6390
2.16k
            {
6391
2.16k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6392
0
                    return (0);
6393
2.16k
            }
6394
11.2k
        }
6395
11.2k
        break;
6396
11.2k
        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
314k
        case TIFF_SETGET_UINT16:
6410
314k
        {
6411
314k
            uint16_t data;
6412
314k
            assert(fip->field_readcount == 1);
6413
314k
            assert(fip->field_passcount == 0);
6414
314k
            err = TIFFReadDirEntryShort(tif, dp, &data);
6415
314k
            if (err == TIFFReadDirEntryErrOk)
6416
252k
            {
6417
252k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6418
1.43k
                    return (0);
6419
252k
            }
6420
314k
        }
6421
313k
        break;
6422
313k
        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
466k
        case TIFF_SETGET_UINT32:
6436
466k
        {
6437
466k
            uint32_t data;
6438
466k
            assert(fip->field_readcount == 1);
6439
466k
            assert(fip->field_passcount == 0);
6440
466k
            err = TIFFReadDirEntryLong(tif, dp, &data);
6441
466k
            if (err == TIFFReadDirEntryErrOk)
6442
439k
            {
6443
439k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6444
14
                    return (0);
6445
439k
            }
6446
466k
        }
6447
466k
        break;
6448
466k
        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
12.4k
        case TIFF_SETGET_UINT64:
6462
12.4k
        {
6463
12.4k
            uint64_t data;
6464
12.4k
            assert(fip->field_readcount == 1);
6465
12.4k
            assert(fip->field_passcount == 0);
6466
12.4k
            err = TIFFReadDirEntryLong8(tif, dp, &data);
6467
12.4k
            if (err == TIFFReadDirEntryErrOk)
6468
7.52k
            {
6469
7.52k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6470
0
                    return 0;
6471
7.52k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6472
0
                    return (0);
6473
7.52k
            }
6474
12.4k
        }
6475
12.4k
        break;
6476
12.4k
        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
47.7k
        case TIFF_SETGET_FLOAT:
6492
47.7k
        {
6493
47.7k
            float data;
6494
47.7k
            assert(fip->field_readcount == 1);
6495
47.7k
            assert(fip->field_passcount == 0);
6496
47.7k
            err = TIFFReadDirEntryFloat(tif, dp, &data);
6497
47.7k
            if (err == TIFFReadDirEntryErrOk)
6498
17.3k
            {
6499
17.3k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6500
0
                    return 0;
6501
17.3k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6502
340
                    return (0);
6503
17.3k
            }
6504
47.7k
        }
6505
47.4k
        break;
6506
47.4k
        case TIFF_SETGET_DOUBLE:
6507
5.03k
        {
6508
5.03k
            double data;
6509
5.03k
            assert(fip->field_readcount == 1);
6510
5.03k
            assert(fip->field_passcount == 0);
6511
5.03k
            err = TIFFReadDirEntryDouble(tif, dp, &data);
6512
5.03k
            if (err == TIFFReadDirEntryErrOk)
6513
1.35k
            {
6514
1.35k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6515
0
                    return 0;
6516
1.35k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6517
0
                    return (0);
6518
1.35k
            }
6519
5.03k
        }
6520
5.03k
        break;
6521
88.9k
        case TIFF_SETGET_IFD8:
6522
88.9k
        {
6523
88.9k
            uint64_t data;
6524
88.9k
            assert(fip->field_readcount == 1);
6525
88.9k
            assert(fip->field_passcount == 0);
6526
88.9k
            err = TIFFReadDirEntryIfd8(tif, dp, &data);
6527
88.9k
            if (err == TIFFReadDirEntryErrOk)
6528
80.7k
            {
6529
80.7k
                if (!EvaluateIFDdatasizeReading(tif, dp))
6530
0
                    return 0;
6531
80.7k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6532
421
                    return (0);
6533
80.7k
            }
6534
88.9k
        }
6535
88.5k
        break;
6536
88.5k
        case TIFF_SETGET_UINT16_PAIR:
6537
13.9k
        {
6538
13.9k
            uint16_t *data;
6539
13.9k
            assert(fip->field_readcount == 2);
6540
13.9k
            assert(fip->field_passcount == 0);
6541
13.9k
            if (dp->tdir_count != 2)
6542
8.72k
            {
6543
8.72k
                TIFFWarningExtR(tif, module,
6544
8.72k
                                "incorrect count for field \"%s\", expected 2, "
6545
8.72k
                                "got %" PRIu64,
6546
8.72k
                                fip->field_name, dp->tdir_count);
6547
8.72k
                return (0);
6548
8.72k
            }
6549
5.19k
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
6550
5.19k
            if (err == TIFFReadDirEntryErrOk)
6551
4.31k
            {
6552
4.31k
                int m;
6553
4.31k
                assert(data); /* avoid CLang static Analyzer false positive */
6554
4.31k
                m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
6555
4.31k
                _TIFFfreeExt(tif, data);
6556
4.31k
                if (!m)
6557
0
                    return (0);
6558
4.31k
            }
6559
5.19k
        }
6560
5.19k
        break;
6561
9.75k
        case TIFF_SETGET_C0_UINT8:
6562
9.75k
        {
6563
9.75k
            uint8_t *data;
6564
9.75k
            assert(fip->field_readcount >= 1);
6565
9.75k
            assert(fip->field_passcount == 0);
6566
9.75k
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6567
4.58k
            {
6568
4.58k
                TIFFWarningExtR(tif, module,
6569
4.58k
                                "incorrect count for field \"%s\", expected "
6570
4.58k
                                "%d, got %" PRIu64,
6571
4.58k
                                fip->field_name, (int)fip->field_readcount,
6572
4.58k
                                dp->tdir_count);
6573
4.58k
                return (0);
6574
4.58k
            }
6575
5.16k
            else
6576
5.16k
            {
6577
5.16k
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6578
5.16k
                if (err == TIFFReadDirEntryErrOk)
6579
4.49k
                {
6580
4.49k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6581
0
                    {
6582
0
                        if (data != 0)
6583
0
                            _TIFFfreeExt(tif, data);
6584
0
                        return 0;
6585
0
                    }
6586
4.49k
                    int m;
6587
4.49k
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6588
4.49k
                    if (data != 0)
6589
4.49k
                        _TIFFfreeExt(tif, data);
6590
4.49k
                    if (!m)
6591
0
                        return (0);
6592
4.49k
                }
6593
5.16k
            }
6594
9.75k
        }
6595
5.16k
        break;
6596
5.16k
        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
2.28k
        case TIFF_SETGET_C0_UINT16:
6632
2.28k
        {
6633
2.28k
            uint16_t *data;
6634
2.28k
            assert(fip->field_readcount >= 1);
6635
2.28k
            assert(fip->field_passcount == 0);
6636
2.28k
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6637
1.70k
            {
6638
1.70k
                TIFFWarningExtR(tif, module,
6639
1.70k
                                "incorrect count for field \"%s\", expected "
6640
1.70k
                                "%d, got %" PRIu64,
6641
1.70k
                                fip->field_name, (int)fip->field_readcount,
6642
1.70k
                                dp->tdir_count);
6643
1.70k
                return (0);
6644
1.70k
            }
6645
579
            else
6646
579
            {
6647
579
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
6648
579
                if (err == TIFFReadDirEntryErrOk)
6649
118
                {
6650
118
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6651
0
                    {
6652
0
                        if (data != 0)
6653
0
                            _TIFFfreeExt(tif, data);
6654
0
                        return 0;
6655
0
                    }
6656
118
                    int m;
6657
118
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6658
118
                    if (data != 0)
6659
118
                        _TIFFfreeExt(tif, data);
6660
118
                    if (!m)
6661
0
                        return (0);
6662
118
                }
6663
579
            }
6664
2.28k
        }
6665
579
        break;
6666
579
        case TIFF_SETGET_C0_SINT16:
6667
0
        {
6668
0
            int16_t *data;
6669
0
            assert(fip->field_readcount >= 1);
6670
0
            assert(fip->field_passcount == 0);
6671
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6672
0
            {
6673
0
                TIFFWarningExtR(tif, module,
6674
0
                                "incorrect count for field \"%s\", expected "
6675
0
                                "%d, got %" PRIu64,
6676
0
                                fip->field_name, (int)fip->field_readcount,
6677
0
                                dp->tdir_count);
6678
0
                return (0);
6679
0
            }
6680
0
            else
6681
0
            {
6682
0
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6683
0
                if (err == TIFFReadDirEntryErrOk)
6684
0
                {
6685
0
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6686
0
                    {
6687
0
                        if (data != 0)
6688
0
                            _TIFFfreeExt(tif, data);
6689
0
                        return 0;
6690
0
                    }
6691
0
                    int m;
6692
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6693
0
                    if (data != 0)
6694
0
                        _TIFFfreeExt(tif, data);
6695
0
                    if (!m)
6696
0
                        return (0);
6697
0
                }
6698
0
            }
6699
0
        }
6700
0
        break;
6701
1.38k
        case TIFF_SETGET_C0_UINT32:
6702
1.38k
        {
6703
1.38k
            uint32_t *data;
6704
1.38k
            assert(fip->field_readcount >= 1);
6705
1.38k
            assert(fip->field_passcount == 0);
6706
1.38k
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6707
1.03k
            {
6708
1.03k
                TIFFWarningExtR(tif, module,
6709
1.03k
                                "incorrect count for field \"%s\", expected "
6710
1.03k
                                "%d, got %" PRIu64,
6711
1.03k
                                fip->field_name, (int)fip->field_readcount,
6712
1.03k
                                dp->tdir_count);
6713
1.03k
                return (0);
6714
1.03k
            }
6715
352
            else
6716
352
            {
6717
352
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
6718
352
                if (err == TIFFReadDirEntryErrOk)
6719
92
                {
6720
92
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6721
0
                    {
6722
0
                        if (data != 0)
6723
0
                            _TIFFfreeExt(tif, data);
6724
0
                        return 0;
6725
0
                    }
6726
92
                    int m;
6727
92
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6728
92
                    if (data != 0)
6729
92
                        _TIFFfreeExt(tif, data);
6730
92
                    if (!m)
6731
0
                        return (0);
6732
92
                }
6733
352
            }
6734
1.38k
        }
6735
352
        break;
6736
352
        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
20.2k
        case TIFF_SETGET_C0_FLOAT:
6842
20.2k
        {
6843
20.2k
            float *data;
6844
20.2k
            assert(fip->field_readcount >= 1);
6845
20.2k
            assert(fip->field_passcount == 0);
6846
20.2k
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6847
14.6k
            {
6848
14.6k
                TIFFWarningExtR(tif, module,
6849
14.6k
                                "incorrect count for field \"%s\", expected "
6850
14.6k
                                "%d, got %" PRIu64,
6851
14.6k
                                fip->field_name, (int)fip->field_readcount,
6852
14.6k
                                dp->tdir_count);
6853
14.6k
                return (0);
6854
14.6k
            }
6855
5.57k
            else
6856
5.57k
            {
6857
5.57k
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
6858
5.57k
                if (err == TIFFReadDirEntryErrOk)
6859
4.23k
                {
6860
4.23k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6861
0
                    {
6862
0
                        if (data != 0)
6863
0
                            _TIFFfreeExt(tif, data);
6864
0
                        return 0;
6865
0
                    }
6866
4.23k
                    int m;
6867
4.23k
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6868
4.23k
                    if (data != 0)
6869
4.23k
                        _TIFFfreeExt(tif, data);
6870
4.23k
                    if (!m)
6871
0
                        return (0);
6872
4.23k
                }
6873
5.57k
            }
6874
20.2k
        }
6875
5.57k
        break;
6876
        /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6877
         * into Double-Arrays. */
6878
5.57k
        case TIFF_SETGET_C0_DOUBLE:
6879
333
        {
6880
333
            double *data;
6881
333
            assert(fip->field_readcount >= 1);
6882
333
            assert(fip->field_passcount == 0);
6883
333
            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
333
            else
6893
333
            {
6894
333
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6895
333
                if (err == TIFFReadDirEntryErrOk)
6896
109
                {
6897
109
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6898
0
                    {
6899
0
                        if (data != 0)
6900
0
                            _TIFFfreeExt(tif, data);
6901
0
                        return 0;
6902
0
                    }
6903
109
                    int m;
6904
109
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6905
109
                    if (data != 0)
6906
109
                        _TIFFfreeExt(tif, data);
6907
109
                    if (!m)
6908
0
                        return (0);
6909
109
                }
6910
333
            }
6911
333
        }
6912
333
        break;
6913
5.47k
        case TIFF_SETGET_C16_ASCII:
6914
5.47k
        {
6915
5.47k
            uint8_t *data;
6916
5.47k
            assert(fip->field_readcount == TIFF_VARIABLE);
6917
5.47k
            assert(fip->field_passcount == 1);
6918
5.47k
            if (dp->tdir_count > 0xFFFF)
6919
1.96k
                err = TIFFReadDirEntryErrCount;
6920
3.51k
            else
6921
3.51k
            {
6922
3.51k
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6923
3.51k
                if (err == TIFFReadDirEntryErrOk)
6924
1.38k
                {
6925
1.38k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6926
2
                    {
6927
2
                        if (data != 0)
6928
2
                            _TIFFfreeExt(tif, data);
6929
2
                        return 0;
6930
2
                    }
6931
1.38k
                    int m;
6932
1.38k
                    if (data != 0 && dp->tdir_count > 0 &&
6933
1.27k
                        data[dp->tdir_count - 1] != '\0')
6934
776
                    {
6935
776
                        TIFFWarningExtR(tif, module,
6936
776
                                        "ASCII value for ASCII array tag "
6937
776
                                        "\"%s\" does not end in null "
6938
776
                                        "byte. Forcing it to be null",
6939
776
                                        fip->field_name);
6940
                        /* Enlarge buffer and add terminating null. */
6941
776
                        uint8_t *o =
6942
776
                            _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
6943
776
                        if (o == NULL)
6944
0
                        {
6945
0
                            if (data != NULL)
6946
0
                                _TIFFfreeExt(tif, data);
6947
0
                            return (0);
6948
0
                        }
6949
776
                        if (dp->tdir_count > 0)
6950
776
                        {
6951
776
                            _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6952
776
                        }
6953
776
                        o[(uint32_t)dp->tdir_count] = 0;
6954
776
                        dp->tdir_count++; /* Increment for added null. */
6955
776
                        if (data != 0)
6956
776
                            _TIFFfreeExt(tif, data);
6957
776
                        data = o;
6958
776
                    }
6959
1.38k
                    m = TIFFSetField(tif, dp->tdir_tag,
6960
1.38k
                                     (uint16_t)(dp->tdir_count), data);
6961
1.38k
                    if (data != 0)
6962
1.27k
                        _TIFFfreeExt(tif, data);
6963
1.38k
                    if (!m)
6964
105
                        return (0);
6965
1.38k
                }
6966
3.51k
            }
6967
5.47k
        }
6968
5.36k
        break;
6969
5.36k
        case TIFF_SETGET_C16_UINT8:
6970
2.36k
        {
6971
2.36k
            uint8_t *data;
6972
2.36k
            assert(fip->field_readcount == TIFF_VARIABLE);
6973
2.36k
            assert(fip->field_passcount == 1);
6974
2.36k
            if (dp->tdir_count > 0xFFFF)
6975
1.02k
                err = TIFFReadDirEntryErrCount;
6976
1.33k
            else
6977
1.33k
            {
6978
1.33k
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6979
1.33k
                if (err == TIFFReadDirEntryErrOk)
6980
458
                {
6981
458
                    if (!EvaluateIFDdatasizeReading(tif, dp))
6982
3
                    {
6983
3
                        if (data != 0)
6984
3
                            _TIFFfreeExt(tif, data);
6985
3
                        return 0;
6986
3
                    }
6987
455
                    int m;
6988
455
                    m = TIFFSetField(tif, dp->tdir_tag,
6989
455
                                     (uint16_t)(dp->tdir_count), data);
6990
455
                    if (data != 0)
6991
321
                        _TIFFfreeExt(tif, data);
6992
455
                    if (!m)
6993
0
                        return (0);
6994
455
                }
6995
1.33k
            }
6996
2.36k
        }
6997
2.36k
        break;
6998
2.36k
        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
19.1k
        case TIFF_SETGET_C16_UINT16:
7028
19.1k
        {
7029
19.1k
            uint16_t *data;
7030
19.1k
            assert(fip->field_readcount == TIFF_VARIABLE);
7031
19.1k
            assert(fip->field_passcount == 1);
7032
19.1k
            if (dp->tdir_count > 0xFFFF)
7033
2.19k
                err = TIFFReadDirEntryErrCount;
7034
16.9k
            else
7035
16.9k
            {
7036
16.9k
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
7037
16.9k
                if (err == TIFFReadDirEntryErrOk)
7038
13.7k
                {
7039
13.7k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7040
3
                    {
7041
3
                        if (data != 0)
7042
3
                            _TIFFfreeExt(tif, data);
7043
3
                        return 0;
7044
3
                    }
7045
13.7k
                    int m;
7046
13.7k
                    m = TIFFSetField(tif, dp->tdir_tag,
7047
13.7k
                                     (uint16_t)(dp->tdir_count), data);
7048
13.7k
                    if (data != 0)
7049
11.0k
                        _TIFFfreeExt(tif, data);
7050
13.7k
                    if (!m)
7051
250
                        return (0);
7052
13.7k
                }
7053
16.9k
            }
7054
19.1k
        }
7055
18.8k
        break;
7056
18.8k
        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
6.63k
        case TIFF_SETGET_C16_UINT32:
7086
6.63k
        {
7087
6.63k
            uint32_t *data;
7088
6.63k
            assert(fip->field_readcount == TIFF_VARIABLE);
7089
6.63k
            assert(fip->field_passcount == 1);
7090
6.63k
            if (dp->tdir_count > 0xFFFF)
7091
1.75k
                err = TIFFReadDirEntryErrCount;
7092
4.88k
            else
7093
4.88k
            {
7094
4.88k
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
7095
4.88k
                if (err == TIFFReadDirEntryErrOk)
7096
1.34k
                {
7097
1.34k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7098
3
                    {
7099
3
                        if (data != 0)
7100
3
                            _TIFFfreeExt(tif, data);
7101
3
                        return 0;
7102
3
                    }
7103
1.33k
                    int m;
7104
1.33k
                    m = TIFFSetField(tif, dp->tdir_tag,
7105
1.33k
                                     (uint16_t)(dp->tdir_count), data);
7106
1.33k
                    if (data != 0)
7107
1.19k
                        _TIFFfreeExt(tif, data);
7108
1.33k
                    if (!m)
7109
0
                        return (0);
7110
1.33k
                }
7111
4.88k
            }
7112
6.63k
        }
7113
6.62k
        break;
7114
6.62k
        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
12.8k
        case TIFF_SETGET_C16_FLOAT:
7202
12.8k
        {
7203
12.8k
            float *data;
7204
12.8k
            assert(fip->field_readcount == TIFF_VARIABLE);
7205
12.8k
            assert(fip->field_passcount == 1);
7206
12.8k
            if (dp->tdir_count > 0xFFFF)
7207
2.71k
                err = TIFFReadDirEntryErrCount;
7208
10.1k
            else
7209
10.1k
            {
7210
10.1k
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7211
10.1k
                if (err == TIFFReadDirEntryErrOk)
7212
7.01k
                {
7213
7.01k
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7214
4
                    {
7215
4
                        if (data != 0)
7216
4
                            _TIFFfreeExt(tif, data);
7217
4
                        return 0;
7218
4
                    }
7219
7.00k
                    int m;
7220
7.00k
                    m = TIFFSetField(tif, dp->tdir_tag,
7221
7.00k
                                     (uint16_t)(dp->tdir_count), data);
7222
7.00k
                    if (data != 0)
7223
6.61k
                        _TIFFfreeExt(tif, data);
7224
7.00k
                    if (!m)
7225
0
                        return (0);
7226
7.00k
                }
7227
10.1k
            }
7228
12.8k
        }
7229
12.8k
        break;
7230
12.8k
        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
8.40k
        case TIFF_SETGET_C16_IFD8:
7260
8.40k
        {
7261
8.40k
            uint64_t *data;
7262
8.40k
            assert(fip->field_readcount == TIFF_VARIABLE);
7263
8.40k
            assert(fip->field_passcount == 1);
7264
8.40k
            if (dp->tdir_count > 0xFFFF)
7265
5.39k
                err = TIFFReadDirEntryErrCount;
7266
3.01k
            else
7267
3.01k
            {
7268
3.01k
                err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7269
3.01k
                if (err == TIFFReadDirEntryErrOk)
7270
611
                {
7271
611
                    if (!EvaluateIFDdatasizeReading(tif, dp))
7272
3
                    {
7273
3
                        if (data != 0)
7274
3
                            _TIFFfreeExt(tif, data);
7275
3
                        return 0;
7276
3
                    }
7277
608
                    int m;
7278
608
                    m = TIFFSetField(tif, dp->tdir_tag,
7279
608
                                     (uint16_t)(dp->tdir_count), data);
7280
608
                    if (data != 0)
7281
363
                        _TIFFfreeExt(tif, data);
7282
608
                    if (!m)
7283
0
                        return (0);
7284
608
                }
7285
3.01k
            }
7286
8.40k
        }
7287
8.40k
        break;
7288
94.8k
        case TIFF_SETGET_C32_ASCII:
7289
94.8k
        {
7290
94.8k
            uint8_t *data;
7291
94.8k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7292
94.8k
            assert(fip->field_passcount == 1);
7293
94.8k
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
7294
94.8k
            if (err == TIFFReadDirEntryErrOk)
7295
33.2k
            {
7296
33.2k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7297
24
                {
7298
24
                    if (data != 0)
7299
24
                        _TIFFfreeExt(tif, data);
7300
24
                    return 0;
7301
24
                }
7302
33.2k
                int m;
7303
33.2k
                if (data != 0 && dp->tdir_count > 0 &&
7304
23.4k
                    data[dp->tdir_count - 1] != '\0')
7305
11.9k
                {
7306
11.9k
                    TIFFWarningExtR(
7307
11.9k
                        tif, module,
7308
11.9k
                        "ASCII value for ASCII array tag \"%s\" does not end "
7309
11.9k
                        "in null byte. Forcing it to be null",
7310
11.9k
                        fip->field_name);
7311
                    /* Enlarge buffer and add terminating null. */
7312
11.9k
                    uint8_t *o =
7313
11.9k
                        _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
7314
11.9k
                    if (o == NULL)
7315
0
                    {
7316
0
                        if (data != NULL)
7317
0
                            _TIFFfreeExt(tif, data);
7318
0
                        return (0);
7319
0
                    }
7320
11.9k
                    if (dp->tdir_count > 0)
7321
11.9k
                    {
7322
11.9k
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
7323
11.9k
                    }
7324
11.9k
                    o[(uint32_t)dp->tdir_count] = 0;
7325
11.9k
                    dp->tdir_count++; /* Increment for added null. */
7326
11.9k
                    if (data != 0)
7327
11.9k
                        _TIFFfreeExt(tif, data);
7328
11.9k
                    data = o;
7329
11.9k
                }
7330
33.2k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7331
33.2k
                                 data);
7332
33.2k
                if (data != 0)
7333
23.4k
                    _TIFFfreeExt(tif, data);
7334
33.2k
                if (!m)
7335
0
                    return (0);
7336
33.2k
            }
7337
94.8k
        }
7338
94.8k
        break;
7339
218k
        case TIFF_SETGET_C32_UINT8:
7340
218k
        {
7341
218k
            uint8_t *data;
7342
218k
            uint32_t count = 0;
7343
218k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7344
218k
            assert(fip->field_passcount == 1);
7345
218k
            if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
7346
4.40k
                dp->tdir_type == TIFF_LONG)
7347
489
            {
7348
                /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
7349
                 * data type LONG instead of UNDEFINED. Work around this
7350
                 * frequently found issue */
7351
489
                void *origdata;
7352
489
                err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
7353
489
                if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
7354
235
                {
7355
235
                    data = NULL;
7356
235
                }
7357
254
                else
7358
254
                {
7359
254
                    if (tif->tif_flags & TIFF_SWAB)
7360
67
                        TIFFSwabArrayOfLong((uint32_t *)origdata, count);
7361
254
                    data = (uint8_t *)origdata;
7362
254
                    count = (uint32_t)(count * 4);
7363
254
                }
7364
489
            }
7365
217k
            else
7366
217k
            {
7367
217k
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
7368
217k
                count = (uint32_t)(dp->tdir_count);
7369
217k
            }
7370
218k
            if (err == TIFFReadDirEntryErrOk)
7371
52.2k
            {
7372
52.2k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7373
33
                {
7374
33
                    if (data != 0)
7375
33
                        _TIFFfreeExt(tif, data);
7376
33
                    return 0;
7377
33
                }
7378
52.2k
                int m;
7379
52.2k
                m = TIFFSetField(tif, dp->tdir_tag, count, data);
7380
52.2k
                if (data != 0)
7381
39.2k
                    _TIFFfreeExt(tif, data);
7382
52.2k
                if (!m)
7383
79
                    return (0);
7384
52.2k
            }
7385
218k
        }
7386
217k
        break;
7387
217k
        case TIFF_SETGET_C32_SINT8:
7388
30.3k
        {
7389
30.3k
            int8_t *data = NULL;
7390
30.3k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7391
30.3k
            assert(fip->field_passcount == 1);
7392
30.3k
            err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
7393
30.3k
            if (err == TIFFReadDirEntryErrOk)
7394
11.1k
            {
7395
11.1k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7396
17
                {
7397
17
                    if (data != 0)
7398
17
                        _TIFFfreeExt(tif, data);
7399
17
                    return 0;
7400
17
                }
7401
11.1k
                int m;
7402
11.1k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7403
11.1k
                                 data);
7404
11.1k
                if (data != 0)
7405
7.48k
                    _TIFFfreeExt(tif, data);
7406
11.1k
                if (!m)
7407
0
                    return (0);
7408
11.1k
            }
7409
30.3k
        }
7410
30.3k
        break;
7411
213k
        case TIFF_SETGET_C32_UINT16:
7412
213k
        {
7413
213k
            uint16_t *data;
7414
213k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7415
213k
            assert(fip->field_passcount == 1);
7416
213k
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
7417
213k
            if (err == TIFFReadDirEntryErrOk)
7418
116k
            {
7419
116k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7420
30
                {
7421
30
                    if (data != 0)
7422
30
                        _TIFFfreeExt(tif, data);
7423
30
                    return 0;
7424
30
                }
7425
116k
                int m;
7426
116k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7427
116k
                                 data);
7428
116k
                if (data != 0)
7429
103k
                    _TIFFfreeExt(tif, data);
7430
116k
                if (!m)
7431
0
                    return (0);
7432
116k
            }
7433
213k
        }
7434
213k
        break;
7435
213k
        case TIFF_SETGET_C32_SINT16:
7436
54.6k
        {
7437
54.6k
            int16_t *data = NULL;
7438
54.6k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7439
54.6k
            assert(fip->field_passcount == 1);
7440
54.6k
            err = TIFFReadDirEntrySshortArray(tif, dp, &data);
7441
54.6k
            if (err == TIFFReadDirEntryErrOk)
7442
14.6k
            {
7443
14.6k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7444
38
                {
7445
38
                    if (data != 0)
7446
38
                        _TIFFfreeExt(tif, data);
7447
38
                    return 0;
7448
38
                }
7449
14.6k
                int m;
7450
14.6k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7451
14.6k
                                 data);
7452
14.6k
                if (data != 0)
7453
8.65k
                    _TIFFfreeExt(tif, data);
7454
14.6k
                if (!m)
7455
0
                    return (0);
7456
14.6k
            }
7457
54.6k
        }
7458
54.5k
        break;
7459
147k
        case TIFF_SETGET_C32_UINT32:
7460
147k
        {
7461
147k
            uint32_t *data;
7462
147k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7463
147k
            assert(fip->field_passcount == 1);
7464
147k
            err = TIFFReadDirEntryLongArray(tif, dp, &data);
7465
147k
            if (err == TIFFReadDirEntryErrOk)
7466
67.2k
            {
7467
67.2k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7468
17
                {
7469
17
                    if (data != 0)
7470
17
                        _TIFFfreeExt(tif, data);
7471
17
                    return 0;
7472
17
                }
7473
67.2k
                int m;
7474
67.2k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7475
67.2k
                                 data);
7476
67.2k
                if (data != 0)
7477
60.1k
                    _TIFFfreeExt(tif, data);
7478
67.2k
                if (!m)
7479
0
                    return (0);
7480
67.2k
            }
7481
147k
        }
7482
147k
        break;
7483
147k
        case TIFF_SETGET_C32_SINT32:
7484
23.4k
        {
7485
23.4k
            int32_t *data = NULL;
7486
23.4k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7487
23.4k
            assert(fip->field_passcount == 1);
7488
23.4k
            err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7489
23.4k
            if (err == TIFFReadDirEntryErrOk)
7490
6.73k
            {
7491
6.73k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7492
23
                {
7493
23
                    if (data != 0)
7494
23
                        _TIFFfreeExt(tif, data);
7495
23
                    return 0;
7496
23
                }
7497
6.71k
                int m;
7498
6.71k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7499
6.71k
                                 data);
7500
6.71k
                if (data != 0)
7501
5.28k
                    _TIFFfreeExt(tif, data);
7502
6.71k
                if (!m)
7503
0
                    return (0);
7504
6.71k
            }
7505
23.4k
        }
7506
23.4k
        break;
7507
31.4k
        case TIFF_SETGET_C32_UINT64:
7508
31.4k
        {
7509
31.4k
            uint64_t *data;
7510
31.4k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7511
31.4k
            assert(fip->field_passcount == 1);
7512
31.4k
            err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7513
31.4k
            if (err == TIFFReadDirEntryErrOk)
7514
13.9k
            {
7515
13.9k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7516
27
                {
7517
27
                    if (data != 0)
7518
27
                        _TIFFfreeExt(tif, data);
7519
27
                    return 0;
7520
27
                }
7521
13.9k
                int m;
7522
13.9k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7523
13.9k
                                 data);
7524
13.9k
                if (data != 0)
7525
10.6k
                    _TIFFfreeExt(tif, data);
7526
13.9k
                if (!m)
7527
3.73k
                    return (0);
7528
13.9k
            }
7529
31.4k
        }
7530
27.7k
        break;
7531
27.7k
        case TIFF_SETGET_C32_SINT64:
7532
22.0k
        {
7533
22.0k
            int64_t *data = NULL;
7534
22.0k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7535
22.0k
            assert(fip->field_passcount == 1);
7536
22.0k
            err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7537
22.0k
            if (err == TIFFReadDirEntryErrOk)
7538
9.53k
            {
7539
9.53k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7540
22
                {
7541
22
                    if (data != 0)
7542
22
                        _TIFFfreeExt(tif, data);
7543
22
                    return 0;
7544
22
                }
7545
9.51k
                int m;
7546
9.51k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7547
9.51k
                                 data);
7548
9.51k
                if (data != 0)
7549
4.09k
                    _TIFFfreeExt(tif, data);
7550
9.51k
                if (!m)
7551
3.18k
                    return (0);
7552
9.51k
            }
7553
22.0k
        }
7554
18.8k
        break;
7555
64.2k
        case TIFF_SETGET_C32_FLOAT:
7556
64.2k
        {
7557
64.2k
            float *data;
7558
64.2k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7559
64.2k
            assert(fip->field_passcount == 1);
7560
64.2k
            err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7561
64.2k
            if (err == TIFFReadDirEntryErrOk)
7562
18.9k
            {
7563
18.9k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7564
21
                {
7565
21
                    if (data != 0)
7566
21
                        _TIFFfreeExt(tif, data);
7567
21
                    return 0;
7568
21
                }
7569
18.8k
                int m;
7570
18.8k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7571
18.8k
                                 data);
7572
18.8k
                if (data != 0)
7573
15.4k
                    _TIFFfreeExt(tif, data);
7574
18.8k
                if (!m)
7575
0
                    return (0);
7576
18.8k
            }
7577
64.2k
        }
7578
64.2k
        break;
7579
64.2k
        case TIFF_SETGET_C32_DOUBLE:
7580
21.9k
        {
7581
21.9k
            double *data;
7582
21.9k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7583
21.9k
            assert(fip->field_passcount == 1);
7584
21.9k
            err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7585
21.9k
            if (err == TIFFReadDirEntryErrOk)
7586
3.80k
            {
7587
3.80k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7588
20
                {
7589
20
                    if (data != 0)
7590
20
                        _TIFFfreeExt(tif, data);
7591
20
                    return 0;
7592
20
                }
7593
3.78k
                int m;
7594
3.78k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7595
3.78k
                                 data);
7596
3.78k
                if (data != 0)
7597
2.73k
                    _TIFFfreeExt(tif, data);
7598
3.78k
                if (!m)
7599
0
                    return (0);
7600
3.78k
            }
7601
21.9k
        }
7602
21.9k
        break;
7603
26.4k
        case TIFF_SETGET_C32_IFD8:
7604
26.4k
        {
7605
26.4k
            uint64_t *data;
7606
26.4k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7607
26.4k
            assert(fip->field_passcount == 1);
7608
26.4k
            err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7609
26.4k
            if (err == TIFFReadDirEntryErrOk)
7610
9.13k
            {
7611
9.13k
                if (!EvaluateIFDdatasizeReading(tif, dp))
7612
22
                {
7613
22
                    if (data != 0)
7614
22
                        _TIFFfreeExt(tif, data);
7615
22
                    return 0;
7616
22
                }
7617
9.10k
                int m;
7618
9.10k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7619
9.10k
                                 data);
7620
9.10k
                if (data != 0)
7621
5.64k
                    _TIFFfreeExt(tif, data);
7622
9.10k
                if (!m)
7623
0
                    return (0);
7624
9.10k
            }
7625
26.4k
        }
7626
26.3k
        break;
7627
26.3k
        default:
7628
0
            assert(0); /* we should never get here */
7629
0
            break;
7630
4.38M
    }
7631
4.34M
    if (err != TIFFReadDirEntryErrOk)
7632
822k
    {
7633
822k
        TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
7634
822k
        return (0);
7635
822k
    }
7636
3.52M
    return (1);
7637
4.34M
}
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
223k
{
7646
223k
    static const char module[] = "TIFFFetchStripThing";
7647
223k
    enum TIFFReadDirEntryErr err;
7648
223k
    uint64_t *data;
7649
223k
    err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
7650
223k
    if (err != TIFFReadDirEntryErrOk)
7651
3.29k
    {
7652
3.29k
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7653
3.29k
        TIFFReadDirEntryOutputErr(tif, err, module,
7654
3.29k
                                  fip ? fip->field_name : "unknown tagname", 0);
7655
3.29k
        return (0);
7656
3.29k
    }
7657
220k
    if (dir->tdir_count < (uint64_t)nstrips)
7658
31.1k
    {
7659
31.1k
        uint64_t *resizeddata;
7660
31.1k
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7661
31.1k
        const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
7662
31.1k
        uint32_t max_nstrips = 1000000;
7663
31.1k
        if (pszMax)
7664
0
            max_nstrips = (uint32_t)atoi(pszMax);
7665
31.1k
        TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
7666
31.1k
                                  fip ? fip->field_name : "unknown tagname",
7667
31.1k
                                  (nstrips <= max_nstrips));
7668
7669
31.1k
        if (nstrips > max_nstrips)
7670
248
        {
7671
248
            _TIFFfreeExt(tif, data);
7672
248
            return (0);
7673
248
        }
7674
7675
30.8k
        const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
7676
30.8k
        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
30.8k
        resizeddata = (uint64_t *)_TIFFCheckMalloc(
7695
30.8k
            tif, nstrips, sizeof(uint64_t), "for strip array");
7696
30.8k
        if (resizeddata == 0)
7697
0
        {
7698
0
            _TIFFfreeExt(tif, data);
7699
0
            return (0);
7700
0
        }
7701
30.8k
        if (dir->tdir_count)
7702
8.23k
            _TIFFmemcpy(resizeddata, data,
7703
8.23k
                        (uint32_t)dir->tdir_count * sizeof(uint64_t));
7704
30.8k
        _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
7705
30.8k
                    (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
7706
30.8k
        _TIFFfreeExt(tif, data);
7707
30.8k
        data = resizeddata;
7708
30.8k
    }
7709
220k
    *lpp = data;
7710
220k
    return (1);
7711
220k
}
7712
7713
/*
7714
 * Fetch and set the SubjectDistance EXIF tag.
7715
 */
7716
static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
7717
599
{
7718
599
    static const char module[] = "TIFFFetchSubjectDistance";
7719
599
    enum TIFFReadDirEntryErr err;
7720
599
    UInt64Aligned_t m;
7721
599
    m.l = 0;
7722
599
    assert(sizeof(double) == 8);
7723
599
    assert(sizeof(uint64_t) == 8);
7724
599
    assert(sizeof(uint32_t) == 4);
7725
599
    if (dir->tdir_count != 1)
7726
0
        err = TIFFReadDirEntryErrCount;
7727
599
    else if (dir->tdir_type != TIFF_RATIONAL)
7728
0
        err = TIFFReadDirEntryErrType;
7729
599
    else
7730
599
    {
7731
599
        if (!(tif->tif_flags & TIFF_BIGTIFF))
7732
599
        {
7733
599
            uint32_t offset;
7734
599
            offset = *(uint32_t *)(&dir->tdir_offset);
7735
599
            if (tif->tif_flags & TIFF_SWAB)
7736
207
                TIFFSwabLong(&offset);
7737
599
            err = TIFFReadDirEntryData(tif, offset, 8, m.i);
7738
599
        }
7739
0
        else
7740
0
        {
7741
0
            m.l = dir->tdir_offset.toff_long8;
7742
0
            err = TIFFReadDirEntryErrOk;
7743
0
        }
7744
599
    }
7745
599
    if (err == TIFFReadDirEntryErrOk)
7746
390
    {
7747
390
        double n;
7748
390
        if (tif->tif_flags & TIFF_SWAB)
7749
114
            TIFFSwabArrayOfLong(m.i, 2);
7750
390
        if (m.i[0] == 0)
7751
53
            n = 0.0;
7752
337
        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
114
            n = -1.0;
7759
223
        else
7760
223
            n = (double)m.i[0] / (double)m.i[1];
7761
390
        return (TIFFSetField(tif, dir->tdir_tag, n));
7762
390
    }
7763
209
    else
7764
209
    {
7765
209
        TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
7766
209
        return (0);
7767
209
    }
7768
599
}
7769
7770
static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
7771
                                      uint64_t stripbytes,
7772
                                      uint32_t rowsperstrip)
7773
21.1k
{
7774
21.1k
    TIFFDirectory *td = &tif->tif_dir;
7775
21.1k
    uint64_t bytecount;
7776
21.1k
    uint64_t offset;
7777
21.1k
    uint64_t last_offset;
7778
21.1k
    uint64_t last_bytecount;
7779
21.1k
    uint32_t i;
7780
21.1k
    uint64_t *newcounts;
7781
21.1k
    uint64_t *newoffsets;
7782
7783
21.1k
    offset = TIFFGetStrileOffset(tif, 0);
7784
21.1k
    last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
7785
21.1k
    last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
7786
21.1k
    if (last_offset > UINT64_MAX - last_bytecount ||
7787
20.8k
        last_offset + last_bytecount < offset)
7788
333
    {
7789
333
        return;
7790
333
    }
7791
20.8k
    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
20.8k
    const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
7798
20.8k
    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
20.8k
    newcounts =
7814
20.8k
        (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
7815
20.8k
                                     "for chopped \"StripByteCounts\" array");
7816
20.8k
    newoffsets = (uint64_t *)_TIFFCheckMalloc(
7817
20.8k
        tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7818
20.8k
    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
382M
    for (i = 0; i < nstrips; i++)
7836
382M
    {
7837
382M
        if (stripbytes > bytecount)
7838
15.5k
            stripbytes = bytecount;
7839
382M
        newcounts[i] = stripbytes;
7840
382M
        newoffsets[i] = stripbytes ? offset : 0;
7841
382M
        offset += stripbytes;
7842
382M
        bytecount -= stripbytes;
7843
382M
    }
7844
7845
    /*
7846
     * Replace old single strip info with multi-strip info.
7847
     */
7848
20.8k
    td->td_stripsperimage = td->td_nstrips = nstrips;
7849
20.8k
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
7850
7851
20.8k
    _TIFFfreeExt(tif, td->td_stripbytecount_p);
7852
20.8k
    _TIFFfreeExt(tif, td->td_stripoffset_p);
7853
20.8k
    td->td_stripbytecount_p = newcounts;
7854
20.8k
    td->td_stripoffset_p = newoffsets;
7855
#ifdef STRIPBYTECOUNTSORTED_UNUSED
7856
    td->td_stripbytecountsorted = 1;
7857
#endif
7858
20.8k
    tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
7859
20.8k
}
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
51.0k
{
7869
51.0k
    register TIFFDirectory *td = &tif->tif_dir;
7870
51.0k
    uint64_t bytecount;
7871
51.0k
    uint64_t offset;
7872
51.0k
    uint32_t rowblock;
7873
51.0k
    uint64_t rowblockbytes;
7874
51.0k
    uint64_t stripbytes;
7875
51.0k
    uint32_t nstrips;
7876
51.0k
    uint32_t rowsperstrip;
7877
7878
51.0k
    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
51.0k
    if (bytecount == 0 && tif->tif_mode != O_RDONLY)
7883
0
        return;
7884
51.0k
    offset = TIFFGetStrileByteCount(tif, 0);
7885
51.0k
    assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
7886
51.0k
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
7887
1.72k
        rowblock = td->td_ycbcrsubsampling[1];
7888
49.3k
    else
7889
49.3k
        rowblock = 1;
7890
51.0k
    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
51.0k
    if (rowblockbytes > STRIP_SIZE_DEFAULT)
7896
8.76k
    {
7897
8.76k
        stripbytes = rowblockbytes;
7898
8.76k
        rowsperstrip = rowblock;
7899
8.76k
    }
7900
42.2k
    else if (rowblockbytes > 0)
7901
40.3k
    {
7902
40.3k
        uint32_t rowblocksperstrip;
7903
40.3k
        rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
7904
40.3k
        rowsperstrip = rowblocksperstrip * rowblock;
7905
40.3k
        stripbytes = rowblocksperstrip * rowblockbytes;
7906
40.3k
    }
7907
1.97k
    else
7908
1.97k
        return;
7909
7910
    /*
7911
     * never increase the number of rows per strip
7912
     */
7913
49.0k
    if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
7914
23.0k
        return;
7915
26.0k
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
7916
26.0k
    if (nstrips == 0)
7917
724
        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
25.3k
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
7922
6.32k
        (offset >= TIFFGetFileSize(tif) ||
7923
759
         stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
7924
6.32k
    {
7925
6.32k
        return;
7926
6.32k
    }
7927
7928
19.0k
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
7929
19.0k
}
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
7.90k
{
7939
7.90k
    TIFFDirectory *td = &tif->tif_dir;
7940
7.90k
    uint32_t rowblock;
7941
7.90k
    uint64_t rowblockbytes;
7942
7.90k
    uint32_t i;
7943
7.90k
    uint64_t stripsize;
7944
7.90k
    uint32_t rowblocksperstrip;
7945
7.90k
    uint32_t rowsperstrip;
7946
7.90k
    uint64_t stripbytes;
7947
7.90k
    uint32_t nstrips;
7948
7949
7.90k
    stripsize = TIFFStripSize64(tif);
7950
7951
7.90k
    assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
7952
7.90k
    assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
7953
7.90k
    assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
7954
7.90k
           TIFF_STRIPCHOP);
7955
7.90k
    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
7.90k
    if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
7961
0
        return;
7962
7963
7.90k
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
7964
69
        rowblock = td->td_ycbcrsubsampling[1];
7965
7.83k
    else
7966
7.83k
        rowblock = 1;
7967
7.90k
    rowblockbytes = TIFFVStripSize64(tif, rowblock);
7968
7.90k
    if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
7969
521
    {
7970
        /* In case of file with gigantic width */
7971
521
        return;
7972
521
    }
7973
7974
    /* Check that the strips are contiguous and of the expected size */
7975
13.3k
    for (i = 0; i < td->td_nstrips; i++)
7976
7.87k
    {
7977
7.87k
        if (i == td->td_nstrips - 1)
7978
6.19k
        {
7979
6.19k
            if (TIFFGetStrileByteCount(tif, i) <
7980
6.19k
                TIFFVStripSize64(tif,
7981
6.19k
                                 td->td_imagelength - i * td->td_rowsperstrip))
7982
714
            {
7983
714
                return;
7984
714
            }
7985
6.19k
        }
7986
1.67k
        else
7987
1.67k
        {
7988
1.67k
            if (TIFFGetStrileByteCount(tif, i) != stripsize)
7989
708
            {
7990
708
                return;
7991
708
            }
7992
970
            if (i > 0 && TIFFGetStrileOffset(tif, i) !=
7993
485
                             TIFFGetStrileOffset(tif, i - 1) +
7994
485
                                 TIFFGetStrileByteCount(tif, i - 1))
7995
480
            {
7996
480
                return;
7997
480
            }
7998
970
        }
7999
7.87k
    }
8000
8001
    /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
8002
5.48k
    rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
8003
5.48k
    if (rowblocksperstrip == 0)
8004
292
        rowblocksperstrip = 1;
8005
5.48k
    rowsperstrip = rowblocksperstrip * rowblock;
8006
5.48k
    stripbytes = rowblocksperstrip * rowblockbytes;
8007
5.48k
    assert(stripbytes <= 0x7FFFFFFFUL);
8008
8009
5.48k
    if (rowsperstrip == 0)
8010
0
        return;
8011
5.48k
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
8012
5.48k
    if (nstrips == 0)
8013
313
        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
5.16k
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
8018
3.01k
    {
8019
3.01k
        uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
8020
3.01k
        uint64_t filesize = TIFFGetFileSize(tif);
8021
3.01k
        uint64_t last_bytecount =
8022
3.01k
            TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
8023
3.01k
        if (last_offset > filesize || last_bytecount > filesize - last_offset)
8024
3.01k
        {
8025
3.01k
            return;
8026
3.01k
        }
8027
3.01k
    }
8028
8029
2.15k
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
8030
2.15k
}
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
15.3M
{
8308
15.3M
    TIFFDirectory *td = &tif->tif_dir;
8309
15.3M
    if (pbErr)
8310
5.49M
        *pbErr = 0;
8311
15.3M
    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
15.3M
    if (*parray == NULL || strile >= td->td_nstrips)
8339
11.9k
    {
8340
11.9k
        if (pbErr)
8341
11.9k
            *pbErr = 1;
8342
11.9k
        return 0;
8343
11.9k
    }
8344
15.3M
    return (*parray)[strile];
8345
15.3M
}
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
6.26M
{
8351
6.26M
    return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
8352
6.26M
}
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
11.6M
{
8358
11.6M
    TIFFDirectory *td = &tif->tif_dir;
8359
11.6M
    return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
8360
11.6M
                                                &(td->td_stripoffset_entry),
8361
11.6M
                                                &(td->td_stripoffset_p), pbErr);
8362
11.6M
}
8363
8364
/* Return the value of the TileByteCounts/StripByteCounts array for the
8365
 * specified tile/strile */
8366
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
8367
3.59M
{
8368
3.59M
    return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
8369
3.59M
}
8370
8371
/* Return the value of the TileByteCounts/StripByteCounts array for the
8372
 * specified tile/strile */
8373
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
8374
3.68M
{
8375
3.68M
    TIFFDirectory *td = &tif->tif_dir;
8376
3.68M
    return _TIFFGetStrileOffsetOrByteCountValue(
8377
3.68M
        tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
8378
3.68M
        pbErr);
8379
3.68M
}
8380
8381
26.2k
int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
8382
8383
static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
8384
104k
{
8385
104k
    register TIFFDirectory *td = &tif->tif_dir;
8386
104k
    int return_value = 1;
8387
8388
    /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
8389
104k
    if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
8390
0
        (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
8391
104k
        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
}