Coverage Report

Created: 2026-07-24 07:44

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